org.el 638 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115
  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* ((version org-version)
  96. (git-version)
  97. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  98. (if (and (file-exists-p (expand-file-name ".git" dir))
  99. (executable-find "git"))
  100. (let ((pwd (substring (pwd) 10)))
  101. (cd dir)
  102. (if (eql 0 (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 version (concat version " (" git-version ")")))
  113. (cd pwd))))
  114. (setq version (format "Org-mode version %s" 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. If the number is negative, and the number of empty lines is at least -N,
  597. all empty lines are shown.
  598. Special case: when 0, never leave empty lines in collapsed view."
  599. :group 'org-cycle
  600. :type 'integer)
  601. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  602. (defcustom org-pre-cycle-hook nil
  603. "Hook that is run before visibility cycling is happening.
  604. The function(s) in this hook must accept a single argument which indicates
  605. the new state that will be set right after running this hook. The
  606. argument is a symbol. Before a global state change, it can have the values
  607. `overview', `content', or `all'. Before a local state change, it can have
  608. the values `folded', `children', or `subtree'."
  609. :group 'org-cycle
  610. :type 'hook)
  611. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  612. org-cycle-hide-drawers
  613. org-cycle-show-empty-lines
  614. org-optimize-window-after-visibility-change)
  615. "Hook that is run after `org-cycle' has changed the buffer visibility.
  616. The function(s) in this hook must accept a single argument which indicates
  617. the new state that was set by the most recent `org-cycle' command. The
  618. argument is a symbol. After a global state change, it can have the values
  619. `overview', `content', or `all'. After a local state change, it can have
  620. the values `folded', `children', or `subtree'."
  621. :group 'org-cycle
  622. :type 'hook)
  623. (defgroup org-edit-structure nil
  624. "Options concerning structure editing in Org-mode."
  625. :tag "Org Edit Structure"
  626. :group 'org-structure)
  627. (defcustom org-odd-levels-only nil
  628. "Non-nil means, skip even levels and only use odd levels for the outline.
  629. This has the effect that two stars are being added/taken away in
  630. promotion/demotion commands. It also influences how levels are
  631. handled by the exporters.
  632. Changing it requires restart of `font-lock-mode' to become effective
  633. for fontification also in regions already fontified.
  634. You may also set this on a per-file basis by adding one of the following
  635. lines to the buffer:
  636. #+STARTUP: odd
  637. #+STARTUP: oddeven"
  638. :group 'org-edit-structure
  639. :group 'org-font-lock
  640. :type 'boolean)
  641. (defcustom org-adapt-indentation t
  642. "Non-nil means, adapt indentation to outline node level.
  643. When this variable is set, Org assumes that you write outlines by
  644. indenting text in each node to align with the headline (after the stars).
  645. The following issues are influenced by this variable:
  646. - When this is set and the *entire* text in an entry is indented, the
  647. indentation is increased by one space in a demotion command, and
  648. decreased by one in a promotion command. If any line in the entry
  649. body starts with text at column 0, indentation is not changed at all.
  650. - Property drawers and planning information is inserted indented when
  651. this variable s set. When nil, they will not be indented.
  652. - TAB indents a line relative to context. The lines below a headline
  653. will be indented when this variable is set.
  654. Note that this is all about true indentation, by adding and removing
  655. space characters. See also `org-indent.el' which does level-dependent
  656. indentation in a virtual way, i.e. at display time in Emacs."
  657. :group 'org-edit-structure
  658. :type 'boolean)
  659. (defcustom org-special-ctrl-a/e nil
  660. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  661. When t, `C-a' will bring back the cursor to the beginning of the
  662. headline text, i.e. after the stars and after a possible TODO keyword.
  663. In an item, this will be the position after the bullet.
  664. When the cursor is already at that position, another `C-a' will bring
  665. it to the beginning of the line.
  666. `C-e' will jump to the end of the headline, ignoring the presence of tags
  667. in the headline. A second `C-e' will then jump to the true end of the
  668. line, after any tags.
  669. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  670. going to the true line boundary first. Only a directly following, identical
  671. keypress will bring the cursor to the special positions.
  672. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  673. set separately."
  674. :group 'org-edit-structure
  675. :type '(choice
  676. (const :tag "off" nil)
  677. (const :tag "after stars/bullet and before tags first" t)
  678. (const :tag "true line boundary first" reversed)
  679. (cons :tag "Set C-a and C-e separately"
  680. (choice :tag "Special C-a"
  681. (const :tag "off" nil)
  682. (const :tag "after stars/bullet first" t)
  683. (const :tag "before stars/bullet first" reversed))
  684. (choice :tag "Special C-e"
  685. (const :tag "off" nil)
  686. (const :tag "before tags first" t)
  687. (const :tag "after tags first" reversed)))))
  688. (if (fboundp 'defvaralias)
  689. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  690. (defcustom org-special-ctrl-k nil
  691. "Non-nil means `C-k' will behave specially in headlines.
  692. When nil, `C-k' will call the default `kill-line' command.
  693. When t, the following will happen while the cursor is in the headline:
  694. - When the cursor is at the beginning of a headline, kill the entire
  695. line and possible the folded subtree below the line.
  696. - When in the middle of the headline text, kill the headline up to the tags.
  697. - When after the headline text, kill the tags."
  698. :group 'org-edit-structure
  699. :type 'boolean)
  700. (defcustom org-yank-folded-subtrees t
  701. "Non-nil means, when yanking subtrees, fold them.
  702. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  703. it starts with a heading and all other headings in it are either children
  704. or siblings, then fold all the subtrees. However, do this only if no
  705. text after the yank would be swallowed into a folded tree by this action."
  706. :group 'org-edit-structure
  707. :type 'boolean)
  708. (defcustom org-yank-adjusted-subtrees nil
  709. "Non-nil means, when yanking subtrees, adjust the level.
  710. With this setting, `org-paste-subtree' is used to insert the subtree, see
  711. this function for details."
  712. :group 'org-edit-structure
  713. :type 'boolean)
  714. (defcustom org-M-RET-may-split-line '((default . t))
  715. "Non-nil means, M-RET will split the line at the cursor position.
  716. When nil, it will go to the end of the line before making a
  717. new line.
  718. You may also set this option in a different way for different
  719. contexts. Valid contexts are:
  720. headline when creating a new headline
  721. item when creating a new item
  722. table in a table field
  723. default the value to be used for all contexts not explicitly
  724. customized"
  725. :group 'org-structure
  726. :group 'org-table
  727. :type '(choice
  728. (const :tag "Always" t)
  729. (const :tag "Never" nil)
  730. (repeat :greedy t :tag "Individual contexts"
  731. (cons
  732. (choice :tag "Context"
  733. (const headline)
  734. (const item)
  735. (const table)
  736. (const default))
  737. (boolean)))))
  738. (defcustom org-insert-heading-respect-content nil
  739. "Non-nil means, insert new headings after the current subtree.
  740. When nil, the new heading is created directly after the current line.
  741. The commands \\[org-insert-heading-respect-content] and
  742. \\[org-insert-todo-heading-respect-content] turn this variable on
  743. for the duration of the command."
  744. :group 'org-structure
  745. :type 'boolean)
  746. (defcustom org-blank-before-new-entry '((heading . auto)
  747. (plain-list-item . auto))
  748. "Should `org-insert-heading' leave a blank line before new heading/item?
  749. The value is an alist, with `heading' and `plain-list-item' as car,
  750. and a boolean flag as cdr. For plain lists, if the variable
  751. `org-empty-line-terminates-plain-lists' is set, the setting here
  752. is ignored and no empty line is inserted, to keep the list in tact."
  753. :group 'org-edit-structure
  754. :type '(list
  755. (cons (const heading)
  756. (choice (const :tag "Never" nil)
  757. (const :tag "Always" t)
  758. (const :tag "Auto" auto)))
  759. (cons (const plain-list-item)
  760. (choice (const :tag "Never" nil)
  761. (const :tag "Always" t)
  762. (const :tag "Auto" auto)))))
  763. (defcustom org-insert-heading-hook nil
  764. "Hook being run after inserting a new heading."
  765. :group 'org-edit-structure
  766. :type 'hook)
  767. (defcustom org-enable-fixed-width-editor t
  768. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  769. This currently only means, they are never auto-wrapped.
  770. When nil, such lines will be treated like ordinary lines.
  771. See also the QUOTE keyword."
  772. :group 'org-edit-structure
  773. :type 'boolean)
  774. (defcustom org-goto-auto-isearch t
  775. "Non-nil means, typing characters in org-goto starts incremental search."
  776. :group 'org-edit-structure
  777. :type 'boolean)
  778. (defgroup org-sparse-trees nil
  779. "Options concerning sparse trees in Org-mode."
  780. :tag "Org Sparse Trees"
  781. :group 'org-structure)
  782. (defcustom org-highlight-sparse-tree-matches t
  783. "Non-nil means, highlight all matches that define a sparse tree.
  784. The highlights will automatically disappear the next time the buffer is
  785. changed by an edit command."
  786. :group 'org-sparse-trees
  787. :type 'boolean)
  788. (defcustom org-remove-highlights-with-change t
  789. "Non-nil means, any change to the buffer will remove temporary highlights.
  790. Such highlights are created by `org-occur' and `org-clock-display'.
  791. When nil, `C-c C-c needs to be used to get rid of the highlights.
  792. The highlights created by `org-preview-latex-fragment' always need
  793. `C-c C-c' to be removed."
  794. :group 'org-sparse-trees
  795. :group 'org-time
  796. :type 'boolean)
  797. (defcustom org-occur-hook '(org-first-headline-recenter)
  798. "Hook that is run after `org-occur' has constructed a sparse tree.
  799. This can be used to recenter the window to show as much of the structure
  800. as possible."
  801. :group 'org-sparse-trees
  802. :type 'hook)
  803. (defgroup org-imenu-and-speedbar nil
  804. "Options concerning imenu and speedbar in Org-mode."
  805. :tag "Org Imenu and Speedbar"
  806. :group 'org-structure)
  807. (defcustom org-imenu-depth 2
  808. "The maximum level for Imenu access to Org-mode headlines.
  809. This also applied for speedbar access."
  810. :group 'org-imenu-and-speedbar
  811. :type 'integer)
  812. (defgroup org-table nil
  813. "Options concerning tables in Org-mode."
  814. :tag "Org Table"
  815. :group 'org)
  816. (defcustom org-enable-table-editor 'optimized
  817. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  818. When nil, such lines will be treated like ordinary lines.
  819. When equal to the symbol `optimized', the table editor will be optimized to
  820. do the following:
  821. - Automatic overwrite mode in front of whitespace in table fields.
  822. This makes the structure of the table stay in tact as long as the edited
  823. field does not exceed the column width.
  824. - Minimize the number of realigns. Normally, the table is aligned each time
  825. TAB or RET are pressed to move to another field. With optimization this
  826. happens only if changes to a field might have changed the column width.
  827. Optimization requires replacing the functions `self-insert-command',
  828. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  829. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  830. very good at guessing when a re-align will be necessary, but you can always
  831. force one with \\[org-ctrl-c-ctrl-c].
  832. If you would like to use the optimized version in Org-mode, but the
  833. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  834. This variable can be used to turn on and off the table editor during a session,
  835. but in order to toggle optimization, a restart is required.
  836. See also the variable `org-table-auto-blank-field'."
  837. :group 'org-table
  838. :type '(choice
  839. (const :tag "off" nil)
  840. (const :tag "on" t)
  841. (const :tag "on, optimized" optimized)))
  842. (defcustom org-self-insert-cluster-for-undo t
  843. "Non-nil means cluster self-insert commands for undo when possible.
  844. If this is set, then, like in the Emacs command loop, 20 consequtive
  845. characters will be undone together.
  846. This is configurable, because there is some impact on typing performance."
  847. :group 'org-table
  848. :type 'boolean)
  849. (defcustom org-table-tab-recognizes-table.el t
  850. "Non-nil means, TAB will automatically notice a table.el table.
  851. When it sees such a table, it moves point into it and - if necessary -
  852. calls `table-recognize-table'."
  853. :group 'org-table-editing
  854. :type 'boolean)
  855. (defgroup org-link nil
  856. "Options concerning links in Org-mode."
  857. :tag "Org Link"
  858. :group 'org)
  859. (defvar org-link-abbrev-alist-local nil
  860. "Buffer-local version of `org-link-abbrev-alist', which see.
  861. The value of this is taken from the #+LINK lines.")
  862. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  863. (defcustom org-link-abbrev-alist nil
  864. "Alist of link abbreviations.
  865. The car of each element is a string, to be replaced at the start of a link.
  866. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  867. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  868. [[linkkey:tag][description]]
  869. The 'linkkey' must be a word word, starting with a letter, followed
  870. by letters, numbers, '-' or '_'.
  871. If REPLACE is a string, the tag will simply be appended to create the link.
  872. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  873. the placeholder \"%h\" will cause a url-encoded version of the tag to
  874. be inserted at that point (see the function `url-hexify-string').
  875. REPLACE may also be a function that will be called with the tag as the
  876. only argument to create the link, which should be returned as a string.
  877. See the manual for examples."
  878. :group 'org-link
  879. :type '(repeat
  880. (cons
  881. (string :tag "Protocol")
  882. (choice
  883. (string :tag "Format")
  884. (function)))))
  885. (defcustom org-descriptive-links t
  886. "Non-nil means, hide link part and only show description of bracket links.
  887. Bracket links are like [[link][description]]. This variable sets the initial
  888. state in new org-mode buffers. The setting can then be toggled on a
  889. per-buffer basis from the Org->Hyperlinks menu."
  890. :group 'org-link
  891. :type 'boolean)
  892. (defcustom org-link-file-path-type 'adaptive
  893. "How the path name in file links should be stored.
  894. Valid values are:
  895. relative Relative to the current directory, i.e. the directory of the file
  896. into which the link is being inserted.
  897. absolute Absolute path, if possible with ~ for home directory.
  898. noabbrev Absolute path, no abbreviation of home directory.
  899. adaptive Use relative path for files in the current directory and sub-
  900. directories of it. For other files, use an absolute path."
  901. :group 'org-link
  902. :type '(choice
  903. (const relative)
  904. (const absolute)
  905. (const noabbrev)
  906. (const adaptive)))
  907. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  908. "Types of links that should be activated in Org-mode files.
  909. This is a list of symbols, each leading to the activation of a certain link
  910. type. In principle, it does not hurt to turn on most link types - there may
  911. be a small gain when turning off unused link types. The types are:
  912. bracket The recommended [[link][description]] or [[link]] links with hiding.
  913. angular Links in angular brackets that may contain whitespace like
  914. <bbdb:Carsten Dominik>.
  915. plain Plain links in normal text, no whitespace, like http://google.com.
  916. radio Text that is matched by a radio target, see manual for details.
  917. tag Tag settings in a headline (link to tag search).
  918. date Time stamps (link to calendar).
  919. footnote Footnote labels.
  920. Changing this variable requires a restart of Emacs to become effective."
  921. :group 'org-link
  922. :type '(set :greedy t
  923. (const :tag "Double bracket links (new style)" bracket)
  924. (const :tag "Angular bracket links (old style)" angular)
  925. (const :tag "Plain text links" plain)
  926. (const :tag "Radio target matches" radio)
  927. (const :tag "Tags" tag)
  928. (const :tag "Timestamps" date)
  929. (const :tag "Footnotes" footnote)))
  930. (defcustom org-make-link-description-function nil
  931. "Function to use to generate link descriptions from links. If
  932. nil the link location will be used. This function must take two
  933. parameters; the first is the link and the second the description
  934. org-insert-link has generated, and should return the description
  935. to use."
  936. :group 'org-link
  937. :type 'function)
  938. (defgroup org-link-store nil
  939. "Options concerning storing links in Org-mode."
  940. :tag "Org Store Link"
  941. :group 'org-link)
  942. (defcustom org-email-link-description-format "Email %c: %.30s"
  943. "Format of the description part of a link to an email or usenet message.
  944. The following %-escapes will be replaced by corresponding information:
  945. %F full \"From\" field
  946. %f name, taken from \"From\" field, address if no name
  947. %T full \"To\" field
  948. %t first name in \"To\" field, address if no name
  949. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  950. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  951. %s subject
  952. %m message-id.
  953. You may use normal field width specification between the % and the letter.
  954. This is for example useful to limit the length of the subject.
  955. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  956. :group 'org-link-store
  957. :type 'string)
  958. (defcustom org-from-is-user-regexp
  959. (let (r1 r2)
  960. (when (and user-mail-address (not (string= user-mail-address "")))
  961. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  962. (when (and user-full-name (not (string= user-full-name "")))
  963. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  964. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  965. "Regexp matched against the \"From:\" header of an email or usenet message.
  966. It should match if the message is from the user him/herself."
  967. :group 'org-link-store
  968. :type 'regexp)
  969. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  970. "Non-nil means, storing a link to an Org file will use entry IDs.
  971. Note that before this variable is even considered, org-id must be loaded,
  972. so please customize `org-modules' and turn it on.
  973. The variable can have the following values:
  974. t Create an ID if needed to make a link to the current entry.
  975. create-if-interactive
  976. If `org-store-link' is called directly (interactively, as a user
  977. command), do create an ID to support the link. But when doing the
  978. job for remember, only use the ID if it already exists. The
  979. purpose of this setting is to avoid proliferation of unwanted
  980. IDs, just because you happen to be in an Org file when you
  981. call `org-remember' that automatically and preemptively
  982. creates a link. If you do want to get an ID link in a remember
  983. template to an entry not having an ID, create it first by
  984. explicitly creating a link to it, using `C-c C-l' first.
  985. create-if-interactive-and-no-custom-id
  986. Like create-if-interactive, but do not create an ID if there is
  987. a CUSTOM_ID property defined in the entry. This is the default.
  988. use-existing
  989. Use existing ID, do not create one.
  990. nil Never use an ID to make a link, instead link using a text search for
  991. the headline text."
  992. :group 'org-link-store
  993. :type '(choice
  994. (const :tag "Create ID to make link" t)
  995. (const :tag "Create if storing link interactively"
  996. create-if-interactive)
  997. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  998. create-if-interactive-and-no-custom-id)
  999. (const :tag "Only use existing" use-existing)
  1000. (const :tag "Do not use ID to create link" nil)))
  1001. (defcustom org-context-in-file-links t
  1002. "Non-nil means, file links from `org-store-link' contain context.
  1003. A search string will be added to the file name with :: as separator and
  1004. used to find the context when the link is activated by the command
  1005. `org-open-at-point'.
  1006. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1007. negates this setting for the duration of the command."
  1008. :group 'org-link-store
  1009. :type 'boolean)
  1010. (defcustom org-keep-stored-link-after-insertion nil
  1011. "Non-nil means, keep link in list for entire session.
  1012. The command `org-store-link' adds a link pointing to the current
  1013. location to an internal list. These links accumulate during a session.
  1014. The command `org-insert-link' can be used to insert links into any
  1015. Org-mode file (offering completion for all stored links). When this
  1016. option is nil, every link which has been inserted once using \\[org-insert-link]
  1017. will be removed from the list, to make completing the unused links
  1018. more efficient."
  1019. :group 'org-link-store
  1020. :type 'boolean)
  1021. (defgroup org-link-follow nil
  1022. "Options concerning following links in Org-mode."
  1023. :tag "Org Follow Link"
  1024. :group 'org-link)
  1025. (defcustom org-link-translation-function nil
  1026. "Function to translate links with different syntax to Org syntax.
  1027. This can be used to translate links created for example by the Planner
  1028. or emacs-wiki packages to Org syntax.
  1029. The function must accept two parameters, a TYPE containing the link
  1030. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1031. which is everything after the link protocol. It should return a cons
  1032. with possibly modified values of type and path.
  1033. Org contains a function for this, so if you set this variable to
  1034. `org-translate-link-from-planner', you should be able follow many
  1035. links created by planner."
  1036. :group 'org-link-follow
  1037. :type 'function)
  1038. (defcustom org-follow-link-hook nil
  1039. "Hook that is run after a link has been followed."
  1040. :group 'org-link-follow
  1041. :type 'hook)
  1042. (defcustom org-tab-follows-link nil
  1043. "Non-nil means, on links TAB will follow the link.
  1044. Needs to be set before org.el is loaded.
  1045. This really should not be used, it does not make sense, and the
  1046. implementation is bad."
  1047. :group 'org-link-follow
  1048. :type 'boolean)
  1049. (defcustom org-return-follows-link nil
  1050. "Non-nil means, on links RET will follow the link.
  1051. Needs to be set before org.el is loaded."
  1052. :group 'org-link-follow
  1053. :type 'boolean)
  1054. (defcustom org-mouse-1-follows-link
  1055. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1056. "Non-nil means, mouse-1 on a link will follow the link.
  1057. A longer mouse click will still set point. Does not work on XEmacs.
  1058. Needs to be set before org.el is loaded."
  1059. :group 'org-link-follow
  1060. :type 'boolean)
  1061. (defcustom org-mark-ring-length 4
  1062. "Number of different positions to be recorded in the ring
  1063. Changing this requires a restart of Emacs to work correctly."
  1064. :group 'org-link-follow
  1065. :type 'integer)
  1066. (defcustom org-link-frame-setup
  1067. '((vm . vm-visit-folder-other-frame)
  1068. (gnus . gnus-other-frame)
  1069. (file . find-file-other-window))
  1070. "Setup the frame configuration for following links.
  1071. When following a link with Emacs, it may often be useful to display
  1072. this link in another window or frame. This variable can be used to
  1073. set this up for the different types of links.
  1074. For VM, use any of
  1075. `vm-visit-folder'
  1076. `vm-visit-folder-other-frame'
  1077. For Gnus, use any of
  1078. `gnus'
  1079. `gnus-other-frame'
  1080. `org-gnus-no-new-news'
  1081. For FILE, use any of
  1082. `find-file'
  1083. `find-file-other-window'
  1084. `find-file-other-frame'
  1085. For the calendar, use the variable `calendar-setup'.
  1086. For BBDB, it is currently only possible to display the matches in
  1087. another window."
  1088. :group 'org-link-follow
  1089. :type '(list
  1090. (cons (const vm)
  1091. (choice
  1092. (const vm-visit-folder)
  1093. (const vm-visit-folder-other-window)
  1094. (const vm-visit-folder-other-frame)))
  1095. (cons (const gnus)
  1096. (choice
  1097. (const gnus)
  1098. (const gnus-other-frame)
  1099. (const org-gnus-no-new-news)))
  1100. (cons (const file)
  1101. (choice
  1102. (const find-file)
  1103. (const find-file-other-window)
  1104. (const find-file-other-frame)))))
  1105. (defcustom org-display-internal-link-with-indirect-buffer nil
  1106. "Non-nil means, use indirect buffer to display infile links.
  1107. Activating internal links (from one location in a file to another location
  1108. in the same file) normally just jumps to the location. When the link is
  1109. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1110. another window. When this option is set, the other window actually displays
  1111. an indirect buffer clone of the current buffer, to avoid any visibility
  1112. changes to the current buffer."
  1113. :group 'org-link-follow
  1114. :type 'boolean)
  1115. (defcustom org-open-non-existing-files nil
  1116. "Non-nil means, `org-open-file' will open non-existing files.
  1117. When nil, an error will be generated.
  1118. This variable applies only to external applications because they
  1119. might choke on non-existing files. If the link is to a file that
  1120. will be openend in Emacs, the variable is ignored."
  1121. :group 'org-link-follow
  1122. :type 'boolean)
  1123. (defcustom org-open-directory-means-index-dot-org nil
  1124. "Non-nil means, a link to a directory really means to index.org.
  1125. When nil, following a directory link will run dired or open a finder/explorer
  1126. window on that directory."
  1127. :group 'org-link-follow
  1128. :type 'boolean)
  1129. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1130. "Function and arguments to call for following mailto links.
  1131. This is a list with the first element being a lisp function, and the
  1132. remaining elements being arguments to the function. In string arguments,
  1133. %a will be replaced by the address, and %s will be replaced by the subject
  1134. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1135. :group 'org-link-follow
  1136. :type '(choice
  1137. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1138. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1139. (const :tag "message-mail" (message-mail "%a" "%s"))
  1140. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1141. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1142. "Non-nil means, ask for confirmation before executing shell links.
  1143. Shell links can be dangerous: just think about a link
  1144. [[shell:rm -rf ~/*][Google Search]]
  1145. This link would show up in your Org-mode document as \"Google Search\",
  1146. but really it would remove your entire home directory.
  1147. Therefore we advise against setting this variable to nil.
  1148. Just change it to `y-or-n-p' if you want to confirm with a
  1149. single keystroke rather than having to type \"yes\"."
  1150. :group 'org-link-follow
  1151. :type '(choice
  1152. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1153. (const :tag "with y-or-n (faster)" y-or-n-p)
  1154. (const :tag "no confirmation (dangerous)" nil)))
  1155. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1156. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1157. Elisp links can be dangerous: just think about a link
  1158. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1159. This link would show up in your Org-mode document as \"Google Search\",
  1160. but really it would remove your entire home directory.
  1161. Therefore we advise against setting this variable to nil.
  1162. Just change it to `y-or-n-p' if you want to confirm with a
  1163. single keystroke rather than having to type \"yes\"."
  1164. :group 'org-link-follow
  1165. :type '(choice
  1166. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1167. (const :tag "with y-or-n (faster)" y-or-n-p)
  1168. (const :tag "no confirmation (dangerous)" nil)))
  1169. (defconst org-file-apps-defaults-gnu
  1170. '((remote . emacs)
  1171. (system . mailcap)
  1172. (t . mailcap))
  1173. "Default file applications on a UNIX or GNU/Linux system.
  1174. See `org-file-apps'.")
  1175. (defconst org-file-apps-defaults-macosx
  1176. '((remote . emacs)
  1177. (t . "open %s")
  1178. (system . "open %s")
  1179. ("ps.gz" . "gv %s")
  1180. ("eps.gz" . "gv %s")
  1181. ("dvi" . "xdvi %s")
  1182. ("fig" . "xfig %s"))
  1183. "Default file applications on a MacOS X system.
  1184. The system \"open\" is known as a default, but we use X11 applications
  1185. for some files for which the OS does not have a good default.
  1186. See `org-file-apps'.")
  1187. (defconst org-file-apps-defaults-windowsnt
  1188. (list
  1189. '(remote . emacs)
  1190. (cons t
  1191. (list (if (featurep 'xemacs)
  1192. 'mswindows-shell-execute
  1193. 'w32-shell-execute)
  1194. "open" 'file))
  1195. (cons 'system
  1196. (list (if (featurep 'xemacs)
  1197. 'mswindows-shell-execute
  1198. 'w32-shell-execute)
  1199. "open" 'file)))
  1200. "Default file applications on a Windows NT system.
  1201. The system \"open\" is used for most files.
  1202. See `org-file-apps'.")
  1203. (defcustom org-file-apps
  1204. '(
  1205. (auto-mode . emacs)
  1206. ("\\.x?html?\\'" . default)
  1207. ("\\.pdf\\'" . default)
  1208. )
  1209. "External applications for opening `file:path' items in a document.
  1210. Org-mode uses system defaults for different file types, but
  1211. you can use this variable to set the application for a given file
  1212. extension. The entries in this list are cons cells where the car identifies
  1213. files and the cdr the corresponding command. Possible values for the
  1214. file identifier are
  1215. \"regex\" Regular expression matched against the file name. For backward
  1216. compatibility, this can also be a string with only alphanumeric
  1217. characters, which is then interpreted as an extension.
  1218. `directory' Matches a directory
  1219. `remote' Matches a remote file, accessible through tramp or efs.
  1220. Remote files most likely should be visited through Emacs
  1221. because external applications cannot handle such paths.
  1222. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1223. so all files Emacs knows how to handle. Using this with
  1224. command `emacs' will open most files in Emacs. Beware that this
  1225. will also open html files inside Emacs, unless you add
  1226. (\"html\" . default) to the list as well.
  1227. t Default for files not matched by any of the other options.
  1228. `system' The system command to open files, like `open' on Windows
  1229. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1230. that will be selected if you call `C-c C-o' with a double
  1231. `C-u C-u' prefix.
  1232. Possible values for the command are:
  1233. `emacs' The file will be visited by the current Emacs process.
  1234. `default' Use the default application for this file type, which is the
  1235. association for t in the list, most likely in the system-specific
  1236. part.
  1237. This can be used to overrule an unwanted setting in the
  1238. system-specific variable.
  1239. `system' Use the system command for opening files, like \"open\".
  1240. This command is specified by the entry whose car is `system'.
  1241. Most likely, the system-specific version of this variable
  1242. does define this command, but you can overrule/replace it
  1243. here.
  1244. string A command to be executed by a shell; %s will be replaced
  1245. by the path to the file.
  1246. sexp A Lisp form which will be evaluated. The file path will
  1247. be available in the Lisp variable `file'.
  1248. For more examples, see the system specific constants
  1249. `org-file-apps-defaults-macosx'
  1250. `org-file-apps-defaults-windowsnt'
  1251. `org-file-apps-defaults-gnu'."
  1252. :group 'org-link-follow
  1253. :type '(repeat
  1254. (cons (choice :value ""
  1255. (string :tag "Extension")
  1256. (const :tag "System command to open files" system)
  1257. (const :tag "Default for unrecognized files" t)
  1258. (const :tag "Remote file" remote)
  1259. (const :tag "Links to a directory" directory)
  1260. (const :tag "Any files that have Emacs modes"
  1261. auto-mode))
  1262. (choice :value ""
  1263. (const :tag "Visit with Emacs" emacs)
  1264. (const :tag "Use default" default)
  1265. (const :tag "Use the system command" system)
  1266. (string :tag "Command")
  1267. (sexp :tag "Lisp form")))))
  1268. (defgroup org-refile nil
  1269. "Options concerning refiling entries in Org-mode."
  1270. :tag "Org Refile"
  1271. :group 'org)
  1272. (defcustom org-directory "~/org"
  1273. "Directory with org files.
  1274. This is just a default location to look for Org files. There is no need
  1275. at all to put your files into this directory. It is only used in the
  1276. following situations:
  1277. 1. When a remember template specifies a target file that is not an
  1278. absolute path. The path will then be interpreted relative to
  1279. `org-directory'
  1280. 2. When a remember note is filed away in an interactive way (when exiting the
  1281. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1282. with `org-directory' as the default path."
  1283. :group 'org-refile
  1284. :group 'org-remember
  1285. :type 'directory)
  1286. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1287. "Default target for storing notes.
  1288. Used by the hooks for remember.el. This can be a string, or nil to mean
  1289. the value of `remember-data-file'.
  1290. You can set this on a per-template basis with the variable
  1291. `org-remember-templates'."
  1292. :group 'org-refile
  1293. :group 'org-remember
  1294. :type '(choice
  1295. (const :tag "Default from remember-data-file" nil)
  1296. file))
  1297. (defcustom org-goto-interface 'outline
  1298. "The default interface to be used for `org-goto'.
  1299. Allowed values are:
  1300. outline The interface shows an outline of the relevant file
  1301. and the correct heading is found by moving through
  1302. the outline or by searching with incremental search.
  1303. outline-path-completion Headlines in the current buffer are offered via
  1304. completion. This is the interface also used by
  1305. the refile command."
  1306. :group 'org-refile
  1307. :type '(choice
  1308. (const :tag "Outline" outline)
  1309. (const :tag "Outline-path-completion" outline-path-completion)))
  1310. (defcustom org-goto-max-level 5
  1311. "Maximum level to be considered when running org-goto with refile interface."
  1312. :group 'org-refile
  1313. :type 'integer)
  1314. (defcustom org-reverse-note-order nil
  1315. "Non-nil means, store new notes at the beginning of a file or entry.
  1316. When nil, new notes will be filed to the end of a file or entry.
  1317. This can also be a list with cons cells of regular expressions that
  1318. are matched against file names, and values."
  1319. :group 'org-remember
  1320. :group 'org-refile
  1321. :type '(choice
  1322. (const :tag "Reverse always" t)
  1323. (const :tag "Reverse never" nil)
  1324. (repeat :tag "By file name regexp"
  1325. (cons regexp boolean))))
  1326. (defcustom org-refile-targets nil
  1327. "Targets for refiling entries with \\[org-refile].
  1328. This is list of cons cells. Each cell contains:
  1329. - a specification of the files to be considered, either a list of files,
  1330. or a symbol whose function or variable value will be used to retrieve
  1331. a file name or a list of file names. If you use `org-agenda-files' for
  1332. that, all agenda files will be scanned for targets. Nil means, consider
  1333. headings in the current buffer.
  1334. - A specification of how to find candidate refile targets. This may be
  1335. any of:
  1336. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1337. This tag has to be present in all target headlines, inheritance will
  1338. not be considered.
  1339. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1340. todo keyword.
  1341. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1342. headlines that are refiling targets.
  1343. - a cons cell (:level . N). Any headline of level N is considered a target.
  1344. Note that, when `org-odd-levels-only' is set, level corresponds to
  1345. order in hierarchy, not to the number of stars.
  1346. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1347. Note that, when `org-odd-levels-only' is set, level corresponds to
  1348. order in hierarchy, not to the number of stars.
  1349. You can set the variable `org-refile-target-verify-function' to a function
  1350. to verify each headline found by the simple critery above.
  1351. When this variable is nil, all top-level headlines in the current buffer
  1352. are used, equivalent to the value `((nil . (:level . 1))'."
  1353. :group 'org-refile
  1354. :type '(repeat
  1355. (cons
  1356. (choice :value org-agenda-files
  1357. (const :tag "All agenda files" org-agenda-files)
  1358. (const :tag "Current buffer" nil)
  1359. (function) (variable) (file))
  1360. (choice :tag "Identify target headline by"
  1361. (cons :tag "Specific tag" (const :value :tag) (string))
  1362. (cons :tag "TODO keyword" (const :value :todo) (string))
  1363. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1364. (cons :tag "Level number" (const :value :level) (integer))
  1365. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1366. (defcustom org-refile-target-verify-function nil
  1367. "Function to verify if the headline at point should be a refile target.
  1368. The function will be called without arguments, with point at the
  1369. beginning of the headline. It should return t and leave point
  1370. where it is if the headline is a valid target for refiling.
  1371. If the target should not be selected, the function must return nil.
  1372. In addition to this, it may move point to a place from where the search
  1373. should be continued. For example, the function may decide that the entire
  1374. subtree of the current entry should be excluded and move point to the end
  1375. of the subtree."
  1376. :group 'org-refile
  1377. :type 'function)
  1378. (defcustom org-refile-use-outline-path nil
  1379. "Non-nil means, provide refile targets as paths.
  1380. So a level 3 headline will be available as level1/level2/level3.
  1381. When the value is `file', also include the file name (without directory)
  1382. into the path. In this case, you can also stop the completion after
  1383. the file name, to get entries inserted as top level in the file.
  1384. When `full-file-path', include the full file path."
  1385. :group 'org-refile
  1386. :type '(choice
  1387. (const :tag "Not" nil)
  1388. (const :tag "Yes" t)
  1389. (const :tag "Start with file name" file)
  1390. (const :tag "Start with full file path" full-file-path)))
  1391. (defcustom org-outline-path-complete-in-steps t
  1392. "Non-nil means, complete the outline path in hierarchical steps.
  1393. When Org-mode uses the refile interface to select an outline path
  1394. \(see variable `org-refile-use-outline-path'), the completion of
  1395. the path can be done is a single go, or if can be done in steps down
  1396. the headline hierarchy. Going in steps is probably the best if you
  1397. do not use a special completion package like `ido' or `icicles'.
  1398. However, when using these packages, going in one step can be very
  1399. fast, while still showing the whole path to the entry."
  1400. :group 'org-refile
  1401. :type 'boolean)
  1402. (defcustom org-refile-allow-creating-parent-nodes nil
  1403. "Non-nil means, allow to create new nodes as refile targets.
  1404. New nodes are then created by adding \"/new node name\" to the completion
  1405. of an existing node. When the value of this variable is `confirm',
  1406. new node creation must be confirmed by the user (recommended)
  1407. When nil, the completion must match an existing entry.
  1408. Note that, if the new heading is not seen by the criteria
  1409. listed in `org-refile-targets', multiple instances of the same
  1410. heading would be created by trying again to file under the new
  1411. heading."
  1412. :group 'org-refile
  1413. :type '(choice
  1414. (const :tag "Never" nil)
  1415. (const :tag "Always" t)
  1416. (const :tag "Prompt for confirmation" confirm)))
  1417. (defgroup org-todo nil
  1418. "Options concerning TODO items in Org-mode."
  1419. :tag "Org TODO"
  1420. :group 'org)
  1421. (defgroup org-progress nil
  1422. "Options concerning Progress logging in Org-mode."
  1423. :tag "Org Progress"
  1424. :group 'org-time)
  1425. (defvar org-todo-interpretation-widgets
  1426. '(
  1427. (:tag "Sequence (cycling hits every state)" sequence)
  1428. (:tag "Type (cycling directly to DONE)" type))
  1429. "The available interpretation symbols for customizing
  1430. `org-todo-keywords'.
  1431. Interested libraries should add to this list.")
  1432. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1433. "List of TODO entry keyword sequences and their interpretation.
  1434. \\<org-mode-map>This is a list of sequences.
  1435. Each sequence starts with a symbol, either `sequence' or `type',
  1436. indicating if the keywords should be interpreted as a sequence of
  1437. action steps, or as different types of TODO items. The first
  1438. keywords are states requiring action - these states will select a headline
  1439. for inclusion into the global TODO list Org-mode produces. If one of
  1440. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1441. signify that no further action is necessary. If \"|\" is not found,
  1442. the last keyword is treated as the only DONE state of the sequence.
  1443. The command \\[org-todo] cycles an entry through these states, and one
  1444. additional state where no keyword is present. For details about this
  1445. cycling, see the manual.
  1446. TODO keywords and interpretation can also be set on a per-file basis with
  1447. the special #+SEQ_TODO and #+TYP_TODO lines.
  1448. Each keyword can optionally specify a character for fast state selection
  1449. \(in combination with the variable `org-use-fast-todo-selection')
  1450. and specifiers for state change logging, using the same syntax
  1451. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1452. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1453. indicates to record a time stamp each time this state is selected.
  1454. Each keyword may also specify if a timestamp or a note should be
  1455. recorded when entering or leaving the state, by adding additional
  1456. characters in the parenthesis after the keyword. This looks like this:
  1457. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1458. record only the time of the state change. With X and Y being either
  1459. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1460. Y when leaving the state if and only if the *target* state does not
  1461. define X. You may omit any of the fast-selection key or X or /Y,
  1462. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1463. For backward compatibility, this variable may also be just a list
  1464. of keywords - in this case the interpretation (sequence or type) will be
  1465. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1466. :group 'org-todo
  1467. :group 'org-keywords
  1468. :type '(choice
  1469. (repeat :tag "Old syntax, just keywords"
  1470. (string :tag "Keyword"))
  1471. (repeat :tag "New syntax"
  1472. (cons
  1473. (choice
  1474. :tag "Interpretation"
  1475. ;;Quick and dirty way to see
  1476. ;;`org-todo-interpretations'. This takes the
  1477. ;;place of item arguments
  1478. :convert-widget
  1479. (lambda (widget)
  1480. (widget-put widget
  1481. :args (mapcar
  1482. #'(lambda (x)
  1483. (widget-convert
  1484. (cons 'const x)))
  1485. org-todo-interpretation-widgets))
  1486. widget))
  1487. (repeat
  1488. (string :tag "Keyword"))))))
  1489. (defvar org-todo-keywords-1 nil
  1490. "All TODO and DONE keywords active in a buffer.")
  1491. (make-variable-buffer-local 'org-todo-keywords-1)
  1492. (defvar org-todo-keywords-for-agenda nil)
  1493. (defvar org-done-keywords-for-agenda nil)
  1494. (defvar org-todo-keyword-alist-for-agenda nil)
  1495. (defvar org-tag-alist-for-agenda nil)
  1496. (defvar org-agenda-contributing-files nil)
  1497. (defvar org-not-done-keywords nil)
  1498. (make-variable-buffer-local 'org-not-done-keywords)
  1499. (defvar org-done-keywords nil)
  1500. (make-variable-buffer-local 'org-done-keywords)
  1501. (defvar org-todo-heads nil)
  1502. (make-variable-buffer-local 'org-todo-heads)
  1503. (defvar org-todo-sets nil)
  1504. (make-variable-buffer-local 'org-todo-sets)
  1505. (defvar org-todo-log-states nil)
  1506. (make-variable-buffer-local 'org-todo-log-states)
  1507. (defvar org-todo-kwd-alist nil)
  1508. (make-variable-buffer-local 'org-todo-kwd-alist)
  1509. (defvar org-todo-key-alist nil)
  1510. (make-variable-buffer-local 'org-todo-key-alist)
  1511. (defvar org-todo-key-trigger nil)
  1512. (make-variable-buffer-local 'org-todo-key-trigger)
  1513. (defcustom org-todo-interpretation 'sequence
  1514. "Controls how TODO keywords are interpreted.
  1515. This variable is in principle obsolete and is only used for
  1516. backward compatibility, if the interpretation of todo keywords is
  1517. not given already in `org-todo-keywords'. See that variable for
  1518. more information."
  1519. :group 'org-todo
  1520. :group 'org-keywords
  1521. :type '(choice (const sequence)
  1522. (const type)))
  1523. (defcustom org-use-fast-todo-selection t
  1524. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1525. This variable describes if and under what circumstances the cycling
  1526. mechanism for TODO keywords will be replaced by a single-key, direct
  1527. selection scheme.
  1528. When nil, fast selection is never used.
  1529. When the symbol `prefix', it will be used when `org-todo' is called with
  1530. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1531. in an agenda buffer.
  1532. When t, fast selection is used by default. In this case, the prefix
  1533. argument forces cycling instead.
  1534. In all cases, the special interface is only used if access keys have actually
  1535. been assigned by the user, i.e. if keywords in the configuration are followed
  1536. by a letter in parenthesis, like TODO(t)."
  1537. :group 'org-todo
  1538. :type '(choice
  1539. (const :tag "Never" nil)
  1540. (const :tag "By default" t)
  1541. (const :tag "Only with C-u C-c C-t" prefix)))
  1542. (defcustom org-provide-todo-statistics t
  1543. "Non-nil means, update todo statistics after insert and toggle.
  1544. ALL-HEADLINES means update todo statistics by including headlines
  1545. with no TODO keyword as well, counting them as not done.
  1546. A list of TODO keywords means the same, but skip keywords that are
  1547. not in this list.
  1548. When this is set, todo statistics is updated in the parent of the
  1549. current entry each time a todo state is changed."
  1550. :group 'org-todo
  1551. :type '(choice
  1552. (const :tag "Yes, only for TODO entries" t)
  1553. (const :tag "Yes, including all entries" 'all-headlines)
  1554. (repeat :tag "Yes, for TODOs in this list"
  1555. (string :tag "TODO keyword"))
  1556. (other :tag "No TODO statistics" nil)))
  1557. (defcustom org-hierarchical-todo-statistics t
  1558. "Non-nil means, TODO statistics covers just direct children.
  1559. When nil, all entries in the subtree are considered.
  1560. This has only an effect if `org-provide-todo-statistics' is set.
  1561. To set this to nil for only a single subtree, use a COOKIE_DATA
  1562. property and include the word \"recursive\" into the value."
  1563. :group 'org-todo
  1564. :type 'boolean)
  1565. (defcustom org-after-todo-state-change-hook nil
  1566. "Hook which is run after the state of a TODO item was changed.
  1567. The new state (a string with a TODO keyword, or nil) is available in the
  1568. Lisp variable `state'."
  1569. :group 'org-todo
  1570. :type 'hook)
  1571. (defvar org-blocker-hook nil
  1572. "Hook for functions that are allowed to block a state change.
  1573. Each function gets as its single argument a property list, see
  1574. `org-trigger-hook' for more information about this list.
  1575. If any of the functions in this hook returns nil, the state change
  1576. is blocked.")
  1577. (defvar org-trigger-hook nil
  1578. "Hook for functions that are triggered by a state change.
  1579. Each function gets as its single argument a property list with at least
  1580. the following elements:
  1581. (:type type-of-change :position pos-at-entry-start
  1582. :from old-state :to new-state)
  1583. Depending on the type, more properties may be present.
  1584. This mechanism is currently implemented for:
  1585. TODO state changes
  1586. ------------------
  1587. :type todo-state-change
  1588. :from previous state (keyword as a string), or nil, or a symbol
  1589. 'todo' or 'done', to indicate the general type of state.
  1590. :to new state, like in :from")
  1591. (defcustom org-enforce-todo-dependencies nil
  1592. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1593. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1594. be blocked if any prior sibling is not yet done.
  1595. Finally, if the parent is blocked because of ordered siblings of its own,
  1596. the child will also be blocked.
  1597. This variable needs to be set before org.el is loaded, and you need to
  1598. restart Emacs after a change to make the change effective. The only way
  1599. to change is while Emacs is running is through the customize interface."
  1600. :set (lambda (var val)
  1601. (set var val)
  1602. (if val
  1603. (add-hook 'org-blocker-hook
  1604. 'org-block-todo-from-children-or-siblings-or-parent)
  1605. (remove-hook 'org-blocker-hook
  1606. 'org-block-todo-from-children-or-siblings-or-parent)))
  1607. :group 'org-todo
  1608. :type 'boolean)
  1609. (defcustom org-enforce-todo-checkbox-dependencies nil
  1610. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1611. When this is nil, checkboxes have no influence on switching TODO states.
  1612. When non-nil, you first need to check off all check boxes before the TODO
  1613. entry can be switched to DONE.
  1614. This variable needs to be set before org.el is loaded, and you need to
  1615. restart Emacs after a change to make the change effective. The only way
  1616. to change is while Emacs is running is through the customize interface."
  1617. :set (lambda (var val)
  1618. (set var val)
  1619. (if val
  1620. (add-hook 'org-blocker-hook
  1621. 'org-block-todo-from-checkboxes)
  1622. (remove-hook 'org-blocker-hook
  1623. 'org-block-todo-from-checkboxes)))
  1624. :group 'org-todo
  1625. :type 'boolean)
  1626. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1627. "Non-nil means, inserting a TODO heading is treated as state change.
  1628. So when the command \\[org-insert-todo-heading] is used, state change
  1629. logging will apply if appropriate. When nil, the new TODO item will
  1630. be inserted directly, and no logging will take place."
  1631. :group 'org-todo
  1632. :type 'boolean)
  1633. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1634. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1635. This is the default behavior. However, setting this to nil allows a
  1636. convenient way to select a TODO state and bypass any logging associated
  1637. with that."
  1638. :group 'org-todo
  1639. :type 'boolean)
  1640. (defcustom org-todo-state-tags-triggers nil
  1641. "Tag changes that should be triggered by TODO state changes.
  1642. This is a list. Each entry is
  1643. (state-change (tag . flag) .......)
  1644. State-change can be a string with a state, and empty string to indicate the
  1645. state that has no TODO keyword, or it can be one of the symbols `todo'
  1646. or `done', meaning any not-done or done state, respectively."
  1647. :group 'org-todo
  1648. :group 'org-tags
  1649. :type '(repeat
  1650. (cons (choice :tag "When changing to"
  1651. (const :tag "Not-done state" todo)
  1652. (const :tag "Done state" done)
  1653. (string :tag "State"))
  1654. (repeat
  1655. (cons :tag "Tag action"
  1656. (string :tag "Tag")
  1657. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1658. (defcustom org-log-done nil
  1659. "Information to record when a task moves to the DONE state.
  1660. Possible values are:
  1661. nil Don't add anything, just change the keyword
  1662. time Add a time stamp to the task
  1663. note Prompt a closing note and add it with template `org-log-note-headings'
  1664. This option can also be set with on a per-file-basis with
  1665. #+STARTUP: nologdone
  1666. #+STARTUP: logdone
  1667. #+STARTUP: lognotedone
  1668. You can have local logging settings for a subtree by setting the LOGGING
  1669. property to one or more of these keywords."
  1670. :group 'org-todo
  1671. :group 'org-progress
  1672. :type '(choice
  1673. (const :tag "No logging" nil)
  1674. (const :tag "Record CLOSED timestamp" time)
  1675. (const :tag "Record CLOSED timestamp with closing note." note)))
  1676. ;; Normalize old uses of org-log-done.
  1677. (cond
  1678. ((eq org-log-done t) (setq org-log-done 'time))
  1679. ((and (listp org-log-done) (memq 'done org-log-done))
  1680. (setq org-log-done 'note)))
  1681. (defcustom org-log-note-clock-out nil
  1682. "Non-nil means, record a note when clocking out of an item.
  1683. This can also be configured on a per-file basis by adding one of
  1684. the following lines anywhere in the buffer:
  1685. #+STARTUP: lognoteclock-out
  1686. #+STARTUP: nolognoteclock-out"
  1687. :group 'org-todo
  1688. :group 'org-progress
  1689. :type 'boolean)
  1690. (defcustom org-log-done-with-time t
  1691. "Non-nil means, the CLOSED time stamp will contain date and time.
  1692. When nil, only the date will be recorded."
  1693. :group 'org-progress
  1694. :type 'boolean)
  1695. (defcustom org-log-note-headings
  1696. '((done . "CLOSING NOTE %t")
  1697. (state . "State %-12s from %-12S %t")
  1698. (note . "Note taken on %t")
  1699. (clock-out . ""))
  1700. "Headings for notes added to entries.
  1701. The value is an alist, with the car being a symbol indicating the note
  1702. context, and the cdr is the heading to be used. The heading may also be the
  1703. empty string.
  1704. %t in the heading will be replaced by a time stamp.
  1705. %s will be replaced by the new TODO state, in double quotes.
  1706. %S will be replaced by the old TODO state, in double quotes.
  1707. %u will be replaced by the user name.
  1708. %U will be replaced by the full user name."
  1709. :group 'org-todo
  1710. :group 'org-progress
  1711. :type '(list :greedy t
  1712. (cons (const :tag "Heading when closing an item" done) string)
  1713. (cons (const :tag
  1714. "Heading when changing todo state (todo sequence only)"
  1715. state) string)
  1716. (cons (const :tag "Heading when just taking a note" note) string)
  1717. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1718. (unless (assq 'note org-log-note-headings)
  1719. (push '(note . "%t") org-log-note-headings))
  1720. (defcustom org-log-into-drawer nil
  1721. "Non-nil means, insert state change notes and time stamps into a drawer.
  1722. When nil, state changes notes will be inserted after the headline and
  1723. any scheduling and clock lines, but not inside a drawer.
  1724. The value of this variable should be the name of the drawer to use.
  1725. LOGBOOK is proposed at the default drawer for this purpose, you can
  1726. also set this to a string to define the drawer of your choice.
  1727. A value of t is also allowed, representing \"LOGBOOK\".
  1728. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1729. will be ignored.
  1730. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1731. a subtree."
  1732. :group 'org-todo
  1733. :group 'org-progress
  1734. :type '(choice
  1735. (const :tag "Not into a drawer" nil)
  1736. (const :tag "LOGBOOK" t)
  1737. (string :tag "Other")))
  1738. (if (fboundp 'defvaralias)
  1739. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1740. (defun org-log-into-drawer ()
  1741. "Return the value of `org-log-into-drawer', but let properties overrule.
  1742. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1743. used instead of the default value."
  1744. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1745. (cond
  1746. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1747. ((equal p "t") "LOGBOOK")
  1748. (t p))))
  1749. (defcustom org-log-state-notes-insert-after-drawers nil
  1750. "Non-nil means, insert state change notes after any drawers in entry.
  1751. Only the drawers that *immediately* follow the headline and the
  1752. deadline/scheduled line are skipped.
  1753. When nil, insert notes right after the heading and perhaps the line
  1754. with deadline/scheduling if present.
  1755. This variable will have no effect if `org-log-into-drawer' is
  1756. set."
  1757. :group 'org-todo
  1758. :group 'org-progress
  1759. :type 'boolean)
  1760. (defcustom org-log-states-order-reversed t
  1761. "Non-nil means, the latest state change note will be directly after heading.
  1762. When nil, the notes will be orderer according to time."
  1763. :group 'org-todo
  1764. :group 'org-progress
  1765. :type 'boolean)
  1766. (defcustom org-log-repeat 'time
  1767. "Non-nil means, record moving through the DONE state when triggering repeat.
  1768. An auto-repeating tasks is immediately switched back to TODO when marked
  1769. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1770. the TODO keyword definition, or recording a closing note by setting
  1771. `org-log-done', there will be no record of the task moving through DONE.
  1772. This variable forces taking a note anyway. Possible values are:
  1773. nil Don't force a record
  1774. time Record a time stamp
  1775. note Record a note
  1776. This option can also be set with on a per-file-basis with
  1777. #+STARTUP: logrepeat
  1778. #+STARTUP: lognoterepeat
  1779. #+STARTUP: nologrepeat
  1780. You can have local logging settings for a subtree by setting the LOGGING
  1781. property to one or more of these keywords."
  1782. :group 'org-todo
  1783. :group 'org-progress
  1784. :type '(choice
  1785. (const :tag "Don't force a record" nil)
  1786. (const :tag "Force recording the DONE state" time)
  1787. (const :tag "Force recording a note with the DONE state" note)))
  1788. (defgroup org-priorities nil
  1789. "Priorities in Org-mode."
  1790. :tag "Org Priorities"
  1791. :group 'org-todo)
  1792. (defcustom org-enable-priority-commands t
  1793. "Non-nil means, priority commands are active.
  1794. When nil, these commands will be disabled, so that you never accidentally
  1795. set a priority."
  1796. :group 'org-priorities
  1797. :type 'boolean)
  1798. (defcustom org-highest-priority ?A
  1799. "The highest priority of TODO items. A character like ?A, ?B etc.
  1800. Must have a smaller ASCII number than `org-lowest-priority'."
  1801. :group 'org-priorities
  1802. :type 'character)
  1803. (defcustom org-lowest-priority ?C
  1804. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1805. Must have a larger ASCII number than `org-highest-priority'."
  1806. :group 'org-priorities
  1807. :type 'character)
  1808. (defcustom org-default-priority ?B
  1809. "The default priority of TODO items.
  1810. This is the priority an item get if no explicit priority is given."
  1811. :group 'org-priorities
  1812. :type 'character)
  1813. (defcustom org-priority-start-cycle-with-default t
  1814. "Non-nil means, start with default priority when starting to cycle.
  1815. When this is nil, the first step in the cycle will be (depending on the
  1816. command used) one higher or lower that the default priority."
  1817. :group 'org-priorities
  1818. :type 'boolean)
  1819. (defgroup org-time nil
  1820. "Options concerning time stamps and deadlines in Org-mode."
  1821. :tag "Org Time"
  1822. :group 'org)
  1823. (defcustom org-insert-labeled-timestamps-at-point nil
  1824. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1825. When nil, these labeled time stamps are forces into the second line of an
  1826. entry, just after the headline. When scheduling from the global TODO list,
  1827. the time stamp will always be forced into the second line."
  1828. :group 'org-time
  1829. :type 'boolean)
  1830. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1831. "Formats for `format-time-string' which are used for time stamps.
  1832. It is not recommended to change this constant.")
  1833. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1834. "Number of minutes to round time stamps to.
  1835. These are two values, the first applies when first creating a time stamp.
  1836. The second applies when changing it with the commands `S-up' and `S-down'.
  1837. When changing the time stamp, this means that it will change in steps
  1838. of N minutes, as given by the second value.
  1839. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1840. numbers should be factors of 60, so for example 5, 10, 15.
  1841. When this is larger than 1, you can still force an exact time-stamp by using
  1842. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1843. and by using a prefix arg to `S-up/down' to specify the exact number
  1844. of minutes to shift."
  1845. :group 'org-time
  1846. :get '(lambda (var) ; Make sure all entries have 5 elements
  1847. (if (integerp (default-value var))
  1848. (list (default-value var) 5)
  1849. (default-value var)))
  1850. :type '(list
  1851. (integer :tag "when inserting times")
  1852. (integer :tag "when modifying times")))
  1853. ;; Normalize old customizations of this variable.
  1854. (when (integerp org-time-stamp-rounding-minutes)
  1855. (setq org-time-stamp-rounding-minutes
  1856. (list org-time-stamp-rounding-minutes
  1857. org-time-stamp-rounding-minutes)))
  1858. (defcustom org-display-custom-times nil
  1859. "Non-nil means, overlay custom formats over all time stamps.
  1860. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1861. To turn this on on a per-file basis, insert anywhere in the file:
  1862. #+STARTUP: customtime"
  1863. :group 'org-time
  1864. :set 'set-default
  1865. :type 'sexp)
  1866. (make-variable-buffer-local 'org-display-custom-times)
  1867. (defcustom org-time-stamp-custom-formats
  1868. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1869. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1870. These are overlayed over the default ISO format if the variable
  1871. `org-display-custom-times' is set. Time like %H:%M should be at the
  1872. end of the second format. The custom formats are also honored by export
  1873. commands, if custom time display is turned on at the time of export."
  1874. :group 'org-time
  1875. :type 'sexp)
  1876. (defun org-time-stamp-format (&optional long inactive)
  1877. "Get the right format for a time string."
  1878. (let ((f (if long (cdr org-time-stamp-formats)
  1879. (car org-time-stamp-formats))))
  1880. (if inactive
  1881. (concat "[" (substring f 1 -1) "]")
  1882. f)))
  1883. (defcustom org-time-clocksum-format "%d:%02d"
  1884. "The format string used when creating CLOCKSUM lines, or when
  1885. org-mode generates a time duration."
  1886. :group 'org-time
  1887. :type 'string)
  1888. (defcustom org-deadline-warning-days 14
  1889. "No. of days before expiration during which a deadline becomes active.
  1890. This variable governs the display in sparse trees and in the agenda.
  1891. When 0 or negative, it means use this number (the absolute value of it)
  1892. even if a deadline has a different individual lead time specified.
  1893. Custom commands can set this variable in the options section."
  1894. :group 'org-time
  1895. :group 'org-agenda-daily/weekly
  1896. :type 'integer)
  1897. (defcustom org-read-date-prefer-future t
  1898. "Non-nil means, assume future for incomplete date input from user.
  1899. This affects the following situations:
  1900. 1. The user gives a day, but no month.
  1901. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1902. read this as the third of *next* month. However, if you enter \"17\",
  1903. it will be considered as *this* month.
  1904. 2. The user gives a month but not a year.
  1905. For example, if it is april and you enter \"feb 2\", this will be read
  1906. as feb 2, *next* year. \"May 5\", however, will be this year.
  1907. Currently this does not work for ISO week specifications.
  1908. When this option is nil, the current month and year will always be used
  1909. as defaults."
  1910. :group 'org-time
  1911. :type 'boolean)
  1912. (defcustom org-read-date-display-live t
  1913. "Non-nil means, display current interpretation of date prompt live.
  1914. This display will be in an overlay, in the minibuffer."
  1915. :group 'org-time
  1916. :type 'boolean)
  1917. (defcustom org-read-date-popup-calendar t
  1918. "Non-nil means, pop up a calendar when prompting for a date.
  1919. In the calendar, the date can be selected with mouse-1. However, the
  1920. minibuffer will also be active, and you can simply enter the date as well.
  1921. When nil, only the minibuffer will be available."
  1922. :group 'org-time
  1923. :type 'boolean)
  1924. (if (fboundp 'defvaralias)
  1925. (defvaralias 'org-popup-calendar-for-date-prompt
  1926. 'org-read-date-popup-calendar))
  1927. (defcustom org-read-date-minibuffer-setup-hook nil
  1928. "Hook to be used to set up keys for the date/time interface.
  1929. Add key definitions to `minibuffer-local-map', which will be a temporary
  1930. copy."
  1931. :group 'org-time
  1932. :type 'hook)
  1933. (defcustom org-extend-today-until 0
  1934. "The hour when your day really ends. Must be an integer.
  1935. This has influence for the following applications:
  1936. - When switching the agenda to \"today\". It it is still earlier than
  1937. the time given here, the day recognized as TODAY is actually yesterday.
  1938. - When a date is read from the user and it is still before the time given
  1939. here, the current date and time will be assumed to be yesterday, 23:59.
  1940. Also, timestamps inserted in remember templates follow this rule.
  1941. IMPORTANT: This is a feature whose implementation is and likely will
  1942. remain incomplete. Really, it is only here because past midnight seems to
  1943. be the favorite working time of John Wiegley :-)"
  1944. :group 'org-time
  1945. :type 'integer)
  1946. (defcustom org-edit-timestamp-down-means-later nil
  1947. "Non-nil means, S-down will increase the time in a time stamp.
  1948. When nil, S-up will increase."
  1949. :group 'org-time
  1950. :type 'boolean)
  1951. (defcustom org-calendar-follow-timestamp-change t
  1952. "Non-nil means, make the calendar window follow timestamp changes.
  1953. When a timestamp is modified and the calendar window is visible, it will be
  1954. moved to the new date."
  1955. :group 'org-time
  1956. :type 'boolean)
  1957. (defgroup org-tags nil
  1958. "Options concerning tags in Org-mode."
  1959. :tag "Org Tags"
  1960. :group 'org)
  1961. (defcustom org-tag-alist nil
  1962. "List of tags allowed in Org-mode files.
  1963. When this list is nil, Org-mode will base TAG input on what is already in the
  1964. buffer.
  1965. The value of this variable is an alist, the car of each entry must be a
  1966. keyword as a string, the cdr may be a character that is used to select
  1967. that tag through the fast-tag-selection interface.
  1968. See the manual for details."
  1969. :group 'org-tags
  1970. :type '(repeat
  1971. (choice
  1972. (cons (string :tag "Tag name")
  1973. (character :tag "Access char"))
  1974. (const :tag "Start radio group" (:startgroup))
  1975. (const :tag "End radio group" (:endgroup))
  1976. (const :tag "New line" (:newline)))))
  1977. (defcustom org-tag-persistent-alist nil
  1978. "List of tags that will always appear in all Org-mode files.
  1979. This is in addition to any in buffer settings or customizations
  1980. of `org-tag-alist'.
  1981. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1982. The value of this variable is an alist, the car of each entry must be a
  1983. keyword as a string, the cdr may be a character that is used to select
  1984. that tag through the fast-tag-selection interface.
  1985. See the manual for details.
  1986. To disable these tags on a per-file basis, insert anywhere in the file:
  1987. #+STARTUP: noptag"
  1988. :group 'org-tags
  1989. :type '(repeat
  1990. (choice
  1991. (cons (string :tag "Tag name")
  1992. (character :tag "Access char"))
  1993. (const :tag "Start radio group" (:startgroup))
  1994. (const :tag "End radio group" (:endgroup))
  1995. (const :tag "New line" (:newline)))))
  1996. (defvar org-file-tags nil
  1997. "List of tags that can be inherited by all entries in the file.
  1998. The tags will be inherited if the variable `org-use-tag-inheritance'
  1999. says they should be.
  2000. This variable is populated from #+TAG lines.")
  2001. (defcustom org-use-fast-tag-selection 'auto
  2002. "Non-nil means, use fast tag selection scheme.
  2003. This is a special interface to select and deselect tags with single keys.
  2004. When nil, fast selection is never used.
  2005. When the symbol `auto', fast selection is used if and only if selection
  2006. characters for tags have been configured, either through the variable
  2007. `org-tag-alist' or through a #+TAGS line in the buffer.
  2008. When t, fast selection is always used and selection keys are assigned
  2009. automatically if necessary."
  2010. :group 'org-tags
  2011. :type '(choice
  2012. (const :tag "Always" t)
  2013. (const :tag "Never" nil)
  2014. (const :tag "When selection characters are configured" 'auto)))
  2015. (defcustom org-fast-tag-selection-single-key nil
  2016. "Non-nil means, fast tag selection exits after first change.
  2017. When nil, you have to press RET to exit it.
  2018. During fast tag selection, you can toggle this flag with `C-c'.
  2019. This variable can also have the value `expert'. In this case, the window
  2020. displaying the tags menu is not even shown, until you press C-c again."
  2021. :group 'org-tags
  2022. :type '(choice
  2023. (const :tag "No" nil)
  2024. (const :tag "Yes" t)
  2025. (const :tag "Expert" expert)))
  2026. (defvar org-fast-tag-selection-include-todo nil
  2027. "Non-nil means, fast tags selection interface will also offer TODO states.
  2028. This is an undocumented feature, you should not rely on it.")
  2029. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2030. "The column to which tags should be indented in a headline.
  2031. If this number is positive, it specifies the column. If it is negative,
  2032. it means that the tags should be flushright to that column. For example,
  2033. -80 works well for a normal 80 character screen."
  2034. :group 'org-tags
  2035. :type 'integer)
  2036. (defcustom org-auto-align-tags t
  2037. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2038. These operations change the length of a headline and therefore shift
  2039. the tags around. With this options turned on, after each such operation
  2040. the tags are again aligned to `org-tags-column'."
  2041. :group 'org-tags
  2042. :type 'boolean)
  2043. (defcustom org-use-tag-inheritance t
  2044. "Non-nil means, tags in levels apply also for sublevels.
  2045. When nil, only the tags directly given in a specific line apply there.
  2046. This may also be a list of tags that should be inherited, or a regexp that
  2047. matches tags that should be inherited. Additional control is possible
  2048. with the variable `org-tags-exclude-from-inheritance' which gives an
  2049. explicit list of tags to be excluded from inheritance., even if the value of
  2050. `org-use-tag-inheritance' would select it for inheritance.
  2051. If this option is t, a match early-on in a tree can lead to a large
  2052. number of matches in the subtree when constructing the agenda or creating
  2053. a sparse tree. If you only want to see the first match in a tree during
  2054. a search, check out the variable `org-tags-match-list-sublevels'."
  2055. :group 'org-tags
  2056. :type '(choice
  2057. (const :tag "Not" nil)
  2058. (const :tag "Always" t)
  2059. (repeat :tag "Specific tags" (string :tag "Tag"))
  2060. (regexp :tag "Tags matched by regexp")))
  2061. (defcustom org-tags-exclude-from-inheritance nil
  2062. "List of tags that should never be inherited.
  2063. This is a way to exclude a few tags from inheritance. For way to do
  2064. the opposite, to actively allow inheritance for selected tags,
  2065. see the variable `org-use-tag-inheritance'."
  2066. :group 'org-tags
  2067. :type '(repeat (string :tag "Tag")))
  2068. (defun org-tag-inherit-p (tag)
  2069. "Check if TAG is one that should be inherited."
  2070. (cond
  2071. ((member tag org-tags-exclude-from-inheritance) nil)
  2072. ((eq org-use-tag-inheritance t) t)
  2073. ((not org-use-tag-inheritance) nil)
  2074. ((stringp org-use-tag-inheritance)
  2075. (string-match org-use-tag-inheritance tag))
  2076. ((listp org-use-tag-inheritance)
  2077. (member tag org-use-tag-inheritance))
  2078. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2079. (defcustom org-tags-match-list-sublevels t
  2080. "Non-nil means list also sublevels of headlines matching a search.
  2081. This variable applies to tags/property searches, and also to stuck
  2082. projects because this search is based on a tags match as well.
  2083. When set to the symbol `indented', sublevels are indented with
  2084. leading dots.
  2085. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2086. the sublevels of a headline matching a tag search often also match
  2087. the same search. Listing all of them can create very long lists.
  2088. Setting this variable to nil causes subtrees of a match to be skipped.
  2089. This variable is semi-obsolete and probably should always be true. It
  2090. is better to limit inheritance to certain tags using the variables
  2091. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2092. :group 'org-tags
  2093. :type '(choice
  2094. (const :tag "No, don't list them" nil)
  2095. (const :tag "Yes, do list them" t)
  2096. (const :tag "List them, indented with leading dots" indented)))
  2097. (defcustom org-tags-sort-function nil
  2098. "When set, tags are sorted using this function as a comparator"
  2099. :group 'org-tags
  2100. :type '(choice
  2101. (const :tag "No sorting" nil)
  2102. (const :tag "Alphabetical" string<)
  2103. (const :tag "Reverse alphabetical" string>)
  2104. (function :tag "Custom function" nil)))
  2105. (defvar org-tags-history nil
  2106. "History of minibuffer reads for tags.")
  2107. (defvar org-last-tags-completion-table nil
  2108. "The last used completion table for tags.")
  2109. (defvar org-after-tags-change-hook nil
  2110. "Hook that is run after the tags in a line have changed.")
  2111. (defgroup org-properties nil
  2112. "Options concerning properties in Org-mode."
  2113. :tag "Org Properties"
  2114. :group 'org)
  2115. (defcustom org-property-format "%-10s %s"
  2116. "How property key/value pairs should be formatted by `indent-line'.
  2117. When `indent-line' hits a property definition, it will format the line
  2118. according to this format, mainly to make sure that the values are
  2119. lined-up with respect to each other."
  2120. :group 'org-properties
  2121. :type 'string)
  2122. (defcustom org-use-property-inheritance nil
  2123. "Non-nil means, properties apply also for sublevels.
  2124. This setting is chiefly used during property searches. Turning it on can
  2125. cause significant overhead when doing a search, which is why it is not
  2126. on by default.
  2127. When nil, only the properties directly given in the current entry count.
  2128. When t, every property is inherited. The value may also be a list of
  2129. properties that should have inheritance, or a regular expression matching
  2130. properties that should be inherited.
  2131. However, note that some special properties use inheritance under special
  2132. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2133. and the properties ending in \"_ALL\" when they are used as descriptor
  2134. for valid values of a property.
  2135. Note for programmers:
  2136. When querying an entry with `org-entry-get', you can control if inheritance
  2137. should be used. By default, `org-entry-get' looks only at the local
  2138. properties. You can request inheritance by setting the inherit argument
  2139. to t (to force inheritance) or to `selective' (to respect the setting
  2140. in this variable)."
  2141. :group 'org-properties
  2142. :type '(choice
  2143. (const :tag "Not" nil)
  2144. (const :tag "Always" t)
  2145. (repeat :tag "Specific properties" (string :tag "Property"))
  2146. (regexp :tag "Properties matched by regexp")))
  2147. (defun org-property-inherit-p (property)
  2148. "Check if PROPERTY is one that should be inherited."
  2149. (cond
  2150. ((eq org-use-property-inheritance t) t)
  2151. ((not org-use-property-inheritance) nil)
  2152. ((stringp org-use-property-inheritance)
  2153. (string-match org-use-property-inheritance property))
  2154. ((listp org-use-property-inheritance)
  2155. (member property org-use-property-inheritance))
  2156. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2157. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2158. "The default column format, if no other format has been defined.
  2159. This variable can be set on the per-file basis by inserting a line
  2160. #+COLUMNS: %25ITEM ....."
  2161. :group 'org-properties
  2162. :type 'string)
  2163. (defcustom org-columns-ellipses ".."
  2164. "The ellipses to be used when a field in column view is truncated.
  2165. When this is the empty string, as many characters as possible are shown,
  2166. but then there will be no visual indication that the field has been truncated.
  2167. When this is a string of length N, the last N characters of a truncated
  2168. field are replaced by this string. If the column is narrower than the
  2169. ellipses string, only part of the ellipses string will be shown."
  2170. :group 'org-properties
  2171. :type 'string)
  2172. (defcustom org-columns-modify-value-for-display-function nil
  2173. "Function that modifies values for display in column view.
  2174. For example, it can be used to cut out a certain part from a time stamp.
  2175. The function must take 2 arguments:
  2176. column-title The title of the column (*not* the property name)
  2177. value The value that should be modified.
  2178. The function should return the value that should be displayed,
  2179. or nil if the normal value should be used."
  2180. :group 'org-properties
  2181. :type 'function)
  2182. (defcustom org-effort-property "Effort"
  2183. "The property that is being used to keep track of effort estimates.
  2184. Effort estimates given in this property need to have the format H:MM."
  2185. :group 'org-properties
  2186. :group 'org-progress
  2187. :type '(string :tag "Property"))
  2188. (defconst org-global-properties-fixed
  2189. '(("VISIBILITY_ALL" . "folded children content all")
  2190. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2191. "List of property/value pairs that can be inherited by any entry.
  2192. These are fixed values, for the preset properties. The user variable
  2193. that can be used to add to this list is `org-global-properties'.
  2194. The entries in this list are cons cells where the car is a property
  2195. name and cdr is a string with the value. If the value represents
  2196. multiple items like an \"_ALL\" property, separate the items by
  2197. spaces.")
  2198. (defcustom org-global-properties nil
  2199. "List of property/value pairs that can be inherited by any entry.
  2200. This list will be combined with the constant `org-global-properties-fixed'.
  2201. The entries in this list are cons cells where the car is a property
  2202. name and cdr is a string with the value.
  2203. You can set buffer-local values for the same purpose in the variable
  2204. `org-file-properties' this by adding lines like
  2205. #+PROPERTY: NAME VALUE"
  2206. :group 'org-properties
  2207. :type '(repeat
  2208. (cons (string :tag "Property")
  2209. (string :tag "Value"))))
  2210. (defvar org-file-properties nil
  2211. "List of property/value pairs that can be inherited by any entry.
  2212. Valid for the current buffer.
  2213. This variable is populated from #+PROPERTY lines.")
  2214. (make-variable-buffer-local 'org-file-properties)
  2215. (defgroup org-agenda nil
  2216. "Options concerning agenda views in Org-mode."
  2217. :tag "Org Agenda"
  2218. :group 'org)
  2219. (defvar org-category nil
  2220. "Variable used by org files to set a category for agenda display.
  2221. Such files should use a file variable to set it, for example
  2222. # -*- mode: org; org-category: \"ELisp\"
  2223. or contain a special line
  2224. #+CATEGORY: ELisp
  2225. If the file does not specify a category, then file's base name
  2226. is used instead.")
  2227. (make-variable-buffer-local 'org-category)
  2228. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2229. (defcustom org-agenda-files nil
  2230. "The files to be used for agenda display.
  2231. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2232. \\[org-remove-file]. You can also use customize to edit the list.
  2233. If an entry is a directory, all files in that directory that are matched by
  2234. `org-agenda-file-regexp' will be part of the file list.
  2235. If the value of the variable is not a list but a single file name, then
  2236. the list of agenda files is actually stored and maintained in that file, one
  2237. agenda file per line."
  2238. :group 'org-agenda
  2239. :type '(choice
  2240. (repeat :tag "List of files and directories" file)
  2241. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2242. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2243. "Regular expression to match files for `org-agenda-files'.
  2244. If any element in the list in that variable contains a directory instead
  2245. of a normal file, all files in that directory that are matched by this
  2246. regular expression will be included."
  2247. :group 'org-agenda
  2248. :type 'regexp)
  2249. (defcustom org-agenda-text-search-extra-files nil
  2250. "List of extra files to be searched by text search commands.
  2251. These files will be search in addition to the agenda files by the
  2252. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2253. Note that these files will only be searched for text search commands,
  2254. not for the other agenda views like todo lists, tag searches or the weekly
  2255. agenda. This variable is intended to list notes and possibly archive files
  2256. that should also be searched by these two commands.
  2257. In fact, if the first element in the list is the symbol `agenda-archives',
  2258. than all archive files of all agenda files will be added to the search
  2259. scope."
  2260. :group 'org-agenda
  2261. :type '(set :greedy t
  2262. (const :tag "Agenda Archives" agenda-archives)
  2263. (repeat :inline t (file))))
  2264. (if (fboundp 'defvaralias)
  2265. (defvaralias 'org-agenda-multi-occur-extra-files
  2266. 'org-agenda-text-search-extra-files))
  2267. (defcustom org-agenda-skip-unavailable-files nil
  2268. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2269. A nil value means to remove them, after a query, from the list."
  2270. :group 'org-agenda
  2271. :type 'boolean)
  2272. (defcustom org-calendar-to-agenda-key [?c]
  2273. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2274. The command `org-calendar-goto-agenda' will be bound to this key. The
  2275. default is the character `c' because then `c' can be used to switch back and
  2276. forth between agenda and calendar."
  2277. :group 'org-agenda
  2278. :type 'sexp)
  2279. (defcustom org-calendar-agenda-action-key [?k]
  2280. "The key to be installed in `calendar-mode-map' for agenda-action.
  2281. The command `org-agenda-action' will be bound to this key. The
  2282. default is the character `k' because we use the same key in the agenda."
  2283. :group 'org-agenda
  2284. :type 'sexp)
  2285. (eval-after-load "calendar"
  2286. '(progn
  2287. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2288. 'org-calendar-goto-agenda)
  2289. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2290. 'org-agenda-action)))
  2291. (defgroup org-latex nil
  2292. "Options for embedding LaTeX code into Org-mode."
  2293. :tag "Org LaTeX"
  2294. :group 'org)
  2295. (defcustom org-format-latex-options
  2296. '(:foreground default :background default :scale 1.0
  2297. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2298. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2299. "Options for creating images from LaTeX fragments.
  2300. This is a property list with the following properties:
  2301. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2302. `default' means use the foreground of the default face.
  2303. :background the background color, or \"Transparent\".
  2304. `default' means use the background of the default face.
  2305. :scale a scaling factor for the size of the images.
  2306. :html-foreground, :html-background, :html-scale
  2307. the same numbers for HTML export.
  2308. :matchers a list indicating which matchers should be used to
  2309. find LaTeX fragments. Valid members of this list are:
  2310. \"begin\" find environments
  2311. \"$1\" find single characters surrounded by $.$
  2312. \"$\" find math expressions surrounded by $...$
  2313. \"$$\" find math expressions surrounded by $$....$$
  2314. \"\\(\" find math expressions surrounded by \\(...\\)
  2315. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2316. :group 'org-latex
  2317. :type 'plist)
  2318. (defcustom org-format-latex-header "\\documentclass{article}
  2319. \\usepackage{fullpage} % do not remove
  2320. \\usepackage{amssymb}
  2321. \\usepackage[usenames]{color}
  2322. \\usepackage{amsmath}
  2323. \\usepackage{latexsym}
  2324. \\usepackage[mathscr]{eucal}
  2325. \\pagestyle{empty} % do not remove"
  2326. "The document header used for processing LaTeX fragments."
  2327. :group 'org-latex
  2328. :type 'string)
  2329. (defgroup org-font-lock nil
  2330. "Font-lock settings for highlighting in Org-mode."
  2331. :tag "Org Font Lock"
  2332. :group 'org)
  2333. (defcustom org-level-color-stars-only nil
  2334. "Non-nil means fontify only the stars in each headline.
  2335. When nil, the entire headline is fontified.
  2336. Changing it requires restart of `font-lock-mode' to become effective
  2337. also in regions already fontified."
  2338. :group 'org-font-lock
  2339. :type 'boolean)
  2340. (defcustom org-hide-leading-stars nil
  2341. "Non-nil means, hide the first N-1 stars in a headline.
  2342. This works by using the face `org-hide' for these stars. This
  2343. face is white for a light background, and black for a dark
  2344. background. You may have to customize the face `org-hide' to
  2345. make this work.
  2346. Changing it requires restart of `font-lock-mode' to become effective
  2347. also in regions already fontified.
  2348. You may also set this on a per-file basis by adding one of the following
  2349. lines to the buffer:
  2350. #+STARTUP: hidestars
  2351. #+STARTUP: showstars"
  2352. :group 'org-font-lock
  2353. :type 'boolean)
  2354. (defcustom org-fontify-done-headline nil
  2355. "Non-nil means, change the face of a headline if it is marked DONE.
  2356. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2357. When this is non-nil, the headline after the keyword is set to the
  2358. `org-headline-done' as an additional indication."
  2359. :group 'org-font-lock
  2360. :type 'boolean)
  2361. (defcustom org-fontify-emphasized-text t
  2362. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2363. Changing this variable requires a restart of Emacs to take effect."
  2364. :group 'org-font-lock
  2365. :type 'boolean)
  2366. (defcustom org-fontify-whole-heading-line nil
  2367. "Non-nil means fontify the whole line for headings.
  2368. This is useful when setting a background color for the
  2369. org-leve-* faces."
  2370. :group 'org-font-lock
  2371. :type 'boolean)
  2372. (defcustom org-highlight-latex-fragments-and-specials nil
  2373. "Non-nil means, fontify what is treated specially by the exporters."
  2374. :group 'org-font-lock
  2375. :type 'boolean)
  2376. (defcustom org-hide-emphasis-markers nil
  2377. "Non-nil mean font-lock should hide the emphasis marker characters."
  2378. :group 'org-font-lock
  2379. :type 'boolean)
  2380. (defvar org-emph-re nil
  2381. "Regular expression for matching emphasis.")
  2382. (defvar org-verbatim-re nil
  2383. "Regular expression for matching verbatim text.")
  2384. (defvar org-emphasis-regexp-components) ; defined just below
  2385. (defvar org-emphasis-alist) ; defined just below
  2386. (defun org-set-emph-re (var val)
  2387. "Set variable and compute the emphasis regular expression."
  2388. (set var val)
  2389. (when (and (boundp 'org-emphasis-alist)
  2390. (boundp 'org-emphasis-regexp-components)
  2391. org-emphasis-alist org-emphasis-regexp-components)
  2392. (let* ((e org-emphasis-regexp-components)
  2393. (pre (car e))
  2394. (post (nth 1 e))
  2395. (border (nth 2 e))
  2396. (body (nth 3 e))
  2397. (nl (nth 4 e))
  2398. (body1 (concat body "*?"))
  2399. (markers (mapconcat 'car org-emphasis-alist ""))
  2400. (vmarkers (mapconcat
  2401. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2402. org-emphasis-alist "")))
  2403. ;; make sure special characters appear at the right position in the class
  2404. (if (string-match "\\^" markers)
  2405. (setq markers (concat (replace-match "" t t markers) "^")))
  2406. (if (string-match "-" markers)
  2407. (setq markers (concat (replace-match "" t t markers) "-")))
  2408. (if (string-match "\\^" vmarkers)
  2409. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2410. (if (string-match "-" vmarkers)
  2411. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2412. (if (> nl 0)
  2413. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2414. (int-to-string nl) "\\}")))
  2415. ;; Make the regexp
  2416. (setq org-emph-re
  2417. (concat "\\([" pre "]\\|^\\)"
  2418. "\\("
  2419. "\\([" markers "]\\)"
  2420. "\\("
  2421. "[^" border "]\\|"
  2422. "[^" border "]"
  2423. body1
  2424. "[^" border "]"
  2425. "\\)"
  2426. "\\3\\)"
  2427. "\\([" post "]\\|$\\)"))
  2428. (setq org-verbatim-re
  2429. (concat "\\([" pre "]\\|^\\)"
  2430. "\\("
  2431. "\\([" vmarkers "]\\)"
  2432. "\\("
  2433. "[^" border "]\\|"
  2434. "[^" border "]"
  2435. body1
  2436. "[^" border "]"
  2437. "\\)"
  2438. "\\3\\)"
  2439. "\\([" post "]\\|$\\)")))))
  2440. (defcustom org-emphasis-regexp-components
  2441. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2442. "Components used to build the regular expression for emphasis.
  2443. This is a list with 6 entries. Terminology: In an emphasis string
  2444. like \" *strong word* \", we call the initial space PREMATCH, the final
  2445. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2446. and \"trong wor\" is the body. The different components in this variable
  2447. specify what is allowed/forbidden in each part:
  2448. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2449. post Chars allowed as postmatch. End of line will be allowed too.
  2450. border The chars *forbidden* as border characters.
  2451. body-regexp A regexp like \".\" to match a body character. Don't use
  2452. non-shy groups here, and don't allow newline here.
  2453. newline The maximum number of newlines allowed in an emphasis exp.
  2454. Use customize to modify this, or restart Emacs after changing it."
  2455. :group 'org-font-lock
  2456. :set 'org-set-emph-re
  2457. :type '(list
  2458. (sexp :tag "Allowed chars in pre ")
  2459. (sexp :tag "Allowed chars in post ")
  2460. (sexp :tag "Forbidden chars in border ")
  2461. (sexp :tag "Regexp for body ")
  2462. (integer :tag "number of newlines allowed")
  2463. (option (boolean :tag "Please ignore this button"))))
  2464. (defcustom org-emphasis-alist
  2465. `(("*" bold "<b>" "</b>")
  2466. ("/" italic "<i>" "</i>")
  2467. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2468. ("=" org-code "<code>" "</code>" verbatim)
  2469. ("~" org-verbatim "<code>" "</code>" verbatim)
  2470. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2471. "<del>" "</del>")
  2472. )
  2473. "Special syntax for emphasized text.
  2474. Text starting and ending with a special character will be emphasized, for
  2475. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2476. characters, the face to be used by font-lock for highlighting in Org-mode
  2477. Emacs buffers, and the HTML tags to be used for this.
  2478. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2479. Use customize to modify this, or restart Emacs after changing it."
  2480. :group 'org-font-lock
  2481. :set 'org-set-emph-re
  2482. :type '(repeat
  2483. (list
  2484. (string :tag "Marker character")
  2485. (choice
  2486. (face :tag "Font-lock-face")
  2487. (plist :tag "Face property list"))
  2488. (string :tag "HTML start tag")
  2489. (string :tag "HTML end tag")
  2490. (option (const verbatim)))))
  2491. (defvar org-protecting-blocks
  2492. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2493. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2494. This is needed for font-lock setup.")
  2495. ;;; Miscellaneous options
  2496. (defgroup org-completion nil
  2497. "Completion in Org-mode."
  2498. :tag "Org Completion"
  2499. :group 'org)
  2500. (defcustom org-completion-use-ido nil
  2501. "Non-nil means, use ido completion wherever possible.
  2502. Note that `ido-mode' must be active for this variable to be relevant.
  2503. If you decide to turn this variable on, you might well want to turn off
  2504. `org-outline-path-complete-in-steps'.
  2505. See also `org-completion-use-iswitchb'."
  2506. :group 'org-completion
  2507. :type 'boolean)
  2508. (defcustom org-completion-use-iswitchb nil
  2509. "Non-nil means, use iswitchb completion wherever possible.
  2510. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2511. If you decide to turn this variable on, you might well want to turn off
  2512. `org-outline-path-complete-in-steps'.
  2513. Note that thi variable has only an effect if `org-completion-use-ido' is nil."
  2514. :group 'org-completion
  2515. :type 'boolean)
  2516. (defcustom org-completion-fallback-command 'hippie-expand
  2517. "The expansion command called by \\[org-complete] in normal context.
  2518. Normal means, no org-mode-specific context."
  2519. :group 'org-completion
  2520. :type 'function)
  2521. ;;; Functions and variables from ther packages
  2522. ;; Declared here to avoid compiler warnings
  2523. ;; XEmacs only
  2524. (defvar outline-mode-menu-heading)
  2525. (defvar outline-mode-menu-show)
  2526. (defvar outline-mode-menu-hide)
  2527. (defvar zmacs-regions) ; XEmacs regions
  2528. ;; Emacs only
  2529. (defvar mark-active)
  2530. ;; Various packages
  2531. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2532. (declare-function calendar-forward-day "cal-move" (arg))
  2533. (declare-function calendar-goto-date "cal-move" (date))
  2534. (declare-function calendar-goto-today "cal-move" ())
  2535. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2536. (defvar calc-embedded-close-formula)
  2537. (defvar calc-embedded-open-formula)
  2538. (declare-function cdlatex-tab "ext:cdlatex" ())
  2539. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2540. (defvar font-lock-unfontify-region-function)
  2541. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2542. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2543. (defvar iswitchb-temp-buflist)
  2544. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2545. (defvar org-agenda-tags-todo-honor-ignore-options)
  2546. (declare-function org-agenda-skip "org-agenda" ())
  2547. (declare-function org-format-agenda-item "org-agenda"
  2548. (extra txt &optional category tags dotime noprefix remove-re))
  2549. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2550. (declare-function org-agenda-change-all-lines "org-agenda"
  2551. (newhead hdmarker &optional fixface just-this))
  2552. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2553. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2554. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2555. (beg end))
  2556. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2557. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2558. "org-agenda" (&optional end))
  2559. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2560. (declare-function org-indent-mode "org-indent" (arg))
  2561. (declare-function parse-time-string "parse-time" (string))
  2562. (declare-function remember "remember" (&optional initial))
  2563. (declare-function remember-buffer-desc "remember" ())
  2564. (declare-function remember-finalize "remember" ())
  2565. (defvar remember-save-after-remembering)
  2566. (defvar remember-data-file)
  2567. (defvar remember-register)
  2568. (defvar remember-buffer)
  2569. (defvar remember-handler-functions)
  2570. (defvar remember-annotation-functions)
  2571. (defvar texmathp-why)
  2572. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2573. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2574. (defvar w3m-current-url)
  2575. (defvar w3m-current-title)
  2576. (defvar org-latex-regexps)
  2577. ;;; Autoload and prepare some org modules
  2578. ;; Some table stuff that needs to be defined here, because it is used
  2579. ;; by the functions setting up org-mode or checking for table context.
  2580. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2581. "Detects an org-type or table-type table.")
  2582. (defconst org-table-line-regexp "^[ \t]*|"
  2583. "Detects an org-type table line.")
  2584. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2585. "Detects an org-type table line.")
  2586. (defconst org-table-hline-regexp "^[ \t]*|-"
  2587. "Detects an org-type table hline.")
  2588. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2589. "Detects a table-type table hline.")
  2590. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2591. "Searching from within a table (any type) this finds the first line
  2592. outside the table.")
  2593. ;; Autoload the functions in org-table.el that are needed by functions here.
  2594. (eval-and-compile
  2595. (org-autoload "org-table"
  2596. '(org-table-align org-table-begin org-table-blank-field
  2597. org-table-convert org-table-convert-region org-table-copy-down
  2598. org-table-copy-region org-table-create
  2599. org-table-create-or-convert-from-region
  2600. org-table-create-with-table.el org-table-current-dline
  2601. org-table-cut-region org-table-delete-column org-table-edit-field
  2602. org-table-edit-formulas org-table-end org-table-eval-formula
  2603. org-table-export org-table-field-info
  2604. org-table-get-stored-formulas org-table-goto-column
  2605. org-table-hline-and-move org-table-import org-table-insert-column
  2606. org-table-insert-hline org-table-insert-row org-table-iterate
  2607. org-table-justify-field-maybe org-table-kill-row
  2608. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2609. org-table-move-column org-table-move-column-left
  2610. org-table-move-column-right org-table-move-row
  2611. org-table-move-row-down org-table-move-row-up
  2612. org-table-next-field org-table-next-row org-table-paste-rectangle
  2613. org-table-previous-field org-table-recalculate
  2614. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2615. org-table-toggle-coordinate-overlays
  2616. org-table-toggle-formula-debugger org-table-wrap-region
  2617. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2618. (defun org-at-table-p (&optional table-type)
  2619. "Return t if the cursor is inside an org-type table.
  2620. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2621. (if org-enable-table-editor
  2622. (save-excursion
  2623. (beginning-of-line 1)
  2624. (looking-at (if table-type org-table-any-line-regexp
  2625. org-table-line-regexp)))
  2626. nil))
  2627. (defsubst org-table-p () (org-at-table-p))
  2628. (defun org-at-table.el-p ()
  2629. "Return t if and only if we are at a table.el table."
  2630. (and (org-at-table-p 'any)
  2631. (save-excursion
  2632. (goto-char (org-table-begin 'any))
  2633. (looking-at org-table1-hline-regexp))))
  2634. (defun org-table-recognize-table.el ()
  2635. "If there is a table.el table nearby, recognize it and move into it."
  2636. (if org-table-tab-recognizes-table.el
  2637. (if (org-at-table.el-p)
  2638. (progn
  2639. (beginning-of-line 1)
  2640. (if (looking-at org-table-dataline-regexp)
  2641. nil
  2642. (if (looking-at org-table1-hline-regexp)
  2643. (progn
  2644. (beginning-of-line 2)
  2645. (if (looking-at org-table-any-border-regexp)
  2646. (beginning-of-line -1)))))
  2647. (if (re-search-forward "|" (org-table-end t) t)
  2648. (progn
  2649. (require 'table)
  2650. (if (table--at-cell-p (point))
  2651. t
  2652. (message "recognizing table.el table...")
  2653. (table-recognize-table)
  2654. (message "recognizing table.el table...done")))
  2655. (error "This should not happen..."))
  2656. t)
  2657. nil)
  2658. nil))
  2659. (defun org-at-table-hline-p ()
  2660. "Return t if the cursor is inside a hline in a table."
  2661. (if org-enable-table-editor
  2662. (save-excursion
  2663. (beginning-of-line 1)
  2664. (looking-at org-table-hline-regexp))
  2665. nil))
  2666. (defvar org-table-clean-did-remove-column nil)
  2667. (defun org-table-map-tables (function)
  2668. "Apply FUNCTION to the start of all tables in the buffer."
  2669. (save-excursion
  2670. (save-restriction
  2671. (widen)
  2672. (goto-char (point-min))
  2673. (while (re-search-forward org-table-any-line-regexp nil t)
  2674. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2675. (beginning-of-line 1)
  2676. (when (looking-at org-table-line-regexp)
  2677. (save-excursion (funcall function))
  2678. (or (looking-at org-table-line-regexp)
  2679. (forward-char 1)))
  2680. (re-search-forward org-table-any-border-regexp nil 1))))
  2681. (message "Mapping tables: done"))
  2682. ;; Declare and autoload functions from org-exp.el & Co
  2683. (declare-function org-default-export-plist "org-exp")
  2684. (declare-function org-infile-export-plist "org-exp")
  2685. (declare-function org-get-current-options "org-exp")
  2686. (eval-and-compile
  2687. (org-autoload "org-exp"
  2688. '(org-export org-export-visible
  2689. org-insert-export-options-template
  2690. org-table-clean-before-export))
  2691. (org-autoload "org-ascii"
  2692. '(org-export-as-ascii org-export-ascii-preprocess
  2693. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2694. org-export-region-as-ascii))
  2695. (org-autoload "org-html"
  2696. '(org-export-as-html-and-open
  2697. org-export-as-html-batch org-export-as-html-to-buffer
  2698. org-replace-region-by-html org-export-region-as-html
  2699. org-export-as-html))
  2700. (org-autoload "org-icalendar"
  2701. '(org-export-icalendar-this-file
  2702. org-export-icalendar-all-agenda-files
  2703. org-export-icalendar-combine-agenda-files))
  2704. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2705. ;; Declare and autoload functions from org-agenda.el
  2706. (eval-and-compile
  2707. (org-autoload "org-agenda"
  2708. '(org-agenda org-agenda-list org-search-view
  2709. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2710. org-diary org-agenda-to-appt
  2711. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2712. ;; Autoload org-remember
  2713. (eval-and-compile
  2714. (org-autoload "org-remember"
  2715. '(org-remember-insinuate org-remember-annotation
  2716. org-remember-apply-template org-remember org-remember-handler)))
  2717. ;; Autoload org-clock.el
  2718. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2719. (beg end))
  2720. (declare-function org-clock-update-mode-line "org-clock" ())
  2721. (defvar org-clock-start-time)
  2722. (defvar org-clock-marker (make-marker)
  2723. "Marker recording the last clock-in.")
  2724. (defvar org-clock-hd-marker (make-marker)
  2725. "Marker recording the last clock-in, but the headline position.")
  2726. (defun org-clock-is-active ()
  2727. "Return non-nil if clock is currently running.
  2728. The return value is actually the clock marker."
  2729. (marker-buffer org-clock-marker))
  2730. (eval-and-compile
  2731. (org-autoload
  2732. "org-clock"
  2733. '(org-clock-in org-clock-out org-clock-cancel
  2734. org-clock-goto org-clock-sum org-clock-display
  2735. org-clock-remove-overlays org-clock-report
  2736. org-clocktable-shift org-dblock-write:clocktable
  2737. org-get-clocktable)))
  2738. (defun org-clock-update-time-maybe ()
  2739. "If this is a CLOCK line, update it and return t.
  2740. Otherwise, return nil."
  2741. (interactive)
  2742. (save-excursion
  2743. (beginning-of-line 1)
  2744. (skip-chars-forward " \t")
  2745. (when (looking-at org-clock-string)
  2746. (let ((re (concat "[ \t]*" org-clock-string
  2747. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2748. "\\([ \t]*=>.*\\)?\\)?"))
  2749. ts te h m s neg)
  2750. (cond
  2751. ((not (looking-at re))
  2752. nil)
  2753. ((not (match-end 2))
  2754. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2755. (> org-clock-marker (point))
  2756. (<= org-clock-marker (point-at-eol)))
  2757. ;; The clock is running here
  2758. (setq org-clock-start-time
  2759. (apply 'encode-time
  2760. (org-parse-time-string (match-string 1))))
  2761. (org-clock-update-mode-line)))
  2762. (t
  2763. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2764. (end-of-line 1)
  2765. (setq ts (match-string 1)
  2766. te (match-string 3))
  2767. (setq s (- (time-to-seconds
  2768. (apply 'encode-time (org-parse-time-string te)))
  2769. (time-to-seconds
  2770. (apply 'encode-time (org-parse-time-string ts))))
  2771. neg (< s 0)
  2772. s (abs s)
  2773. h (floor (/ s 3600))
  2774. s (- s (* 3600 h))
  2775. m (floor (/ s 60))
  2776. s (- s (* 60 s)))
  2777. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2778. t))))))
  2779. (defun org-check-running-clock ()
  2780. "Check if the current buffer contains the running clock.
  2781. If yes, offer to stop it and to save the buffer with the changes."
  2782. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2783. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2784. (buffer-name))))
  2785. (org-clock-out)
  2786. (when (y-or-n-p "Save changed buffer?")
  2787. (save-buffer))))
  2788. (defun org-clocktable-try-shift (dir n)
  2789. "Check if this line starts a clock table, if yes, shift the time block."
  2790. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2791. (org-clocktable-shift dir n)))
  2792. ;; Autoload org-timer.el
  2793. (eval-and-compile
  2794. (org-autoload
  2795. "org-timer"
  2796. '(org-timer-start org-timer org-timer-item
  2797. org-timer-change-times-in-region
  2798. org-timer-set-timer
  2799. org-timer-reset-timers
  2800. org-timer-show-remaining-time)))
  2801. ;; Autoload org-feed.el
  2802. (eval-and-compile
  2803. (org-autoload
  2804. "org-feed"
  2805. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2806. ;; Autoload org-indent.el
  2807. (eval-and-compile
  2808. (org-autoload
  2809. "org-indent"
  2810. '(org-indent-mode)))
  2811. ;; Autoload archiving code
  2812. ;; The stuff that is needed for cycling and tags has to be defined here.
  2813. (defgroup org-archive nil
  2814. "Options concerning archiving in Org-mode."
  2815. :tag "Org Archive"
  2816. :group 'org-structure)
  2817. (defcustom org-archive-location "%s_archive::"
  2818. "The location where subtrees should be archived.
  2819. The value of this variable is a string, consisting of two parts,
  2820. separated by a double-colon. The first part is a filename and
  2821. the second part is a headline.
  2822. When the filename is omitted, archiving happens in the same file.
  2823. %s in the filename will be replaced by the current file
  2824. name (without the directory part). Archiving to a different file
  2825. is useful to keep archived entries from contributing to the
  2826. Org-mode Agenda.
  2827. The archived entries will be filed as subtrees of the specified
  2828. headline. When the headline is omitted, the subtrees are simply
  2829. filed away at the end of the file, as top-level entries. Also in
  2830. the heading you can use %s to represent the file name, this can be
  2831. useful when using the same archive for a number of different files.
  2832. Here are a few examples:
  2833. \"%s_archive::\"
  2834. If the current file is Projects.org, archive in file
  2835. Projects.org_archive, as top-level trees. This is the default.
  2836. \"::* Archived Tasks\"
  2837. Archive in the current file, under the top-level headline
  2838. \"* Archived Tasks\".
  2839. \"~/org/archive.org::\"
  2840. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2841. \"~/org/archive.org::From %s\"
  2842. Archive in file ~/org/archive.org (absolute path), und headlines
  2843. \"From FILENAME\" where file name is the current file name.
  2844. \"basement::** Finished Tasks\"
  2845. Archive in file ./basement (relative path), as level 3 trees
  2846. below the level 2 heading \"** Finished Tasks\".
  2847. You may set this option on a per-file basis by adding to the buffer a
  2848. line like
  2849. #+ARCHIVE: basement::** Finished Tasks
  2850. You may also define it locally for a subtree by setting an ARCHIVE property
  2851. in the entry. If such a property is found in an entry, or anywhere up
  2852. the hierarchy, it will be used."
  2853. :group 'org-archive
  2854. :type 'string)
  2855. (defcustom org-archive-tag "ARCHIVE"
  2856. "The tag that marks a subtree as archived.
  2857. An archived subtree does not open during visibility cycling, and does
  2858. not contribute to the agenda listings.
  2859. After changing this, font-lock must be restarted in the relevant buffers to
  2860. get the proper fontification."
  2861. :group 'org-archive
  2862. :group 'org-keywords
  2863. :type 'string)
  2864. (defcustom org-agenda-skip-archived-trees t
  2865. "Non-nil means, the agenda will skip any items located in archived trees.
  2866. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2867. variable is no longer recommended, you should leave it at the value t.
  2868. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2869. :group 'org-archive
  2870. :group 'org-agenda-skip
  2871. :type 'boolean)
  2872. (defcustom org-columns-skip-arrchived-trees t
  2873. "Non-nil means, irgnore archived trees when creating column view."
  2874. :group 'org-archive
  2875. :group 'org-properties
  2876. :type 'boolean)
  2877. (defcustom org-cycle-open-archived-trees nil
  2878. "Non-nil means, `org-cycle' will open archived trees.
  2879. An archived tree is a tree marked with the tag ARCHIVE.
  2880. When nil, archived trees will stay folded. You can still open them with
  2881. normal outline commands like `show-all', but not with the cycling commands."
  2882. :group 'org-archive
  2883. :group 'org-cycle
  2884. :type 'boolean)
  2885. (defcustom org-sparse-tree-open-archived-trees nil
  2886. "Non-nil means sparse tree construction shows matches in archived trees.
  2887. When nil, matches in these trees are highlighted, but the trees are kept in
  2888. collapsed state."
  2889. :group 'org-archive
  2890. :group 'org-sparse-trees
  2891. :type 'boolean)
  2892. (defun org-cycle-hide-archived-subtrees (state)
  2893. "Re-hide all archived subtrees after a visibility state change."
  2894. (when (and (not org-cycle-open-archived-trees)
  2895. (not (memq state '(overview folded))))
  2896. (save-excursion
  2897. (let* ((globalp (memq state '(contents all)))
  2898. (beg (if globalp (point-min) (point)))
  2899. (end (if globalp (point-max) (org-end-of-subtree t))))
  2900. (org-hide-archived-subtrees beg end)
  2901. (goto-char beg)
  2902. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2903. (message "%s" (substitute-command-keys
  2904. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2905. (defun org-force-cycle-archived ()
  2906. "Cycle subtree even if it is archived."
  2907. (interactive)
  2908. (setq this-command 'org-cycle)
  2909. (let ((org-cycle-open-archived-trees t))
  2910. (call-interactively 'org-cycle)))
  2911. (defun org-hide-archived-subtrees (beg end)
  2912. "Re-hide all archived subtrees after a visibility state change."
  2913. (save-excursion
  2914. (let* ((re (concat ":" org-archive-tag ":")))
  2915. (goto-char beg)
  2916. (while (re-search-forward re end t)
  2917. (and (org-on-heading-p) (hide-subtree))
  2918. (org-end-of-subtree t)))))
  2919. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2920. (eval-and-compile
  2921. (org-autoload "org-archive"
  2922. '(org-add-archive-files org-archive-subtree
  2923. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2924. ;; Autoload Column View Code
  2925. (declare-function org-columns-number-to-string "org-colview")
  2926. (declare-function org-columns-get-format-and-top-level "org-colview")
  2927. (declare-function org-columns-compute "org-colview")
  2928. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2929. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2930. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2931. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2932. ;; Autoload ID code
  2933. (declare-function org-id-store-link "org-id")
  2934. (declare-function org-id-locations-load "org-id")
  2935. (declare-function org-id-locations-save "org-id")
  2936. (defvar org-id-track-globally)
  2937. (org-autoload "org-id"
  2938. '(org-id-get-create org-id-new org-id-copy org-id-get
  2939. org-id-get-with-outline-path-completion
  2940. org-id-get-with-outline-drilling
  2941. org-id-goto org-id-find org-id-store-link))
  2942. ;; Autoload Plotting Code
  2943. (org-autoload "org-plot"
  2944. '(org-plot/gnuplot))
  2945. ;;; Variables for pre-computed regular expressions, all buffer local
  2946. (defvar org-drawer-regexp nil
  2947. "Matches first line of a hidden block.")
  2948. (make-variable-buffer-local 'org-drawer-regexp)
  2949. (defvar org-todo-regexp nil
  2950. "Matches any of the TODO state keywords.")
  2951. (make-variable-buffer-local 'org-todo-regexp)
  2952. (defvar org-not-done-regexp nil
  2953. "Matches any of the TODO state keywords except the last one.")
  2954. (make-variable-buffer-local 'org-not-done-regexp)
  2955. (defvar org-not-done-heading-regexp nil
  2956. "Matches a TODO headline that is not done.")
  2957. (make-variable-buffer-local 'org-not-done-regexp)
  2958. (defvar org-todo-line-regexp nil
  2959. "Matches a headline and puts TODO state into group 2 if present.")
  2960. (make-variable-buffer-local 'org-todo-line-regexp)
  2961. (defvar org-complex-heading-regexp nil
  2962. "Matches a headline and puts everything into groups:
  2963. group 1: the stars
  2964. group 2: The todo keyword, maybe
  2965. group 3: Priority cookie
  2966. group 4: True headline
  2967. group 5: Tags")
  2968. (make-variable-buffer-local 'org-complex-heading-regexp)
  2969. (defvar org-todo-line-tags-regexp nil
  2970. "Matches a headline and puts TODO state into group 2 if present.
  2971. Also put tags into group 4 if tags are present.")
  2972. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2973. (defvar org-nl-done-regexp nil
  2974. "Matches newline followed by a headline with the DONE keyword.")
  2975. (make-variable-buffer-local 'org-nl-done-regexp)
  2976. (defvar org-looking-at-done-regexp nil
  2977. "Matches the DONE keyword a point.")
  2978. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2979. (defvar org-ds-keyword-length 12
  2980. "Maximum length of the Deadline and SCHEDULED keywords.")
  2981. (make-variable-buffer-local 'org-ds-keyword-length)
  2982. (defvar org-deadline-regexp nil
  2983. "Matches the DEADLINE keyword.")
  2984. (make-variable-buffer-local 'org-deadline-regexp)
  2985. (defvar org-deadline-time-regexp nil
  2986. "Matches the DEADLINE keyword together with a time stamp.")
  2987. (make-variable-buffer-local 'org-deadline-time-regexp)
  2988. (defvar org-deadline-line-regexp nil
  2989. "Matches the DEADLINE keyword and the rest of the line.")
  2990. (make-variable-buffer-local 'org-deadline-line-regexp)
  2991. (defvar org-scheduled-regexp nil
  2992. "Matches the SCHEDULED keyword.")
  2993. (make-variable-buffer-local 'org-scheduled-regexp)
  2994. (defvar org-scheduled-time-regexp nil
  2995. "Matches the SCHEDULED keyword together with a time stamp.")
  2996. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2997. (defvar org-closed-time-regexp nil
  2998. "Matches the CLOSED keyword together with a time stamp.")
  2999. (make-variable-buffer-local 'org-closed-time-regexp)
  3000. (defvar org-keyword-time-regexp nil
  3001. "Matches any of the 4 keywords, together with the time stamp.")
  3002. (make-variable-buffer-local 'org-keyword-time-regexp)
  3003. (defvar org-keyword-time-not-clock-regexp nil
  3004. "Matches any of the 3 keywords, together with the time stamp.")
  3005. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3006. (defvar org-maybe-keyword-time-regexp nil
  3007. "Matches a timestamp, possibly preceeded by a keyword.")
  3008. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3009. (defvar org-planning-or-clock-line-re nil
  3010. "Matches a line with planning or clock info.")
  3011. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3012. (defconst org-plain-time-of-day-regexp
  3013. (concat
  3014. "\\(\\<[012]?[0-9]"
  3015. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3016. "\\(--?"
  3017. "\\(\\<[012]?[0-9]"
  3018. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3019. "\\)?")
  3020. "Regular expression to match a plain time or time range.
  3021. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3022. groups carry important information:
  3023. 0 the full match
  3024. 1 the first time, range or not
  3025. 8 the second time, if it is a range.")
  3026. (defconst org-plain-time-extension-regexp
  3027. (concat
  3028. "\\(\\<[012]?[0-9]"
  3029. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3030. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3031. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3032. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3033. groups carry important information:
  3034. 0 the full match
  3035. 7 hours of duration
  3036. 9 minutes of duration")
  3037. (defconst org-stamp-time-of-day-regexp
  3038. (concat
  3039. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3040. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3041. "\\(--?"
  3042. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3043. "Regular expression to match a timestamp time or time range.
  3044. After a match, the following groups carry important information:
  3045. 0 the full match
  3046. 1 date plus weekday, for backreferencing to make sure both times on same day
  3047. 2 the first time, range or not
  3048. 4 the second time, if it is a range.")
  3049. (defconst org-startup-options
  3050. '(("fold" org-startup-folded t)
  3051. ("overview" org-startup-folded t)
  3052. ("nofold" org-startup-folded nil)
  3053. ("showall" org-startup-folded nil)
  3054. ("content" org-startup-folded content)
  3055. ("indent" org-startup-indented t)
  3056. ("noindent" org-startup-indented nil)
  3057. ("hidestars" org-hide-leading-stars t)
  3058. ("showstars" org-hide-leading-stars nil)
  3059. ("odd" org-odd-levels-only t)
  3060. ("oddeven" org-odd-levels-only nil)
  3061. ("align" org-startup-align-all-tables t)
  3062. ("noalign" org-startup-align-all-tables nil)
  3063. ("customtime" org-display-custom-times t)
  3064. ("logdone" org-log-done time)
  3065. ("lognotedone" org-log-done note)
  3066. ("nologdone" org-log-done nil)
  3067. ("lognoteclock-out" org-log-note-clock-out t)
  3068. ("nolognoteclock-out" org-log-note-clock-out nil)
  3069. ("logrepeat" org-log-repeat state)
  3070. ("lognoterepeat" org-log-repeat note)
  3071. ("nologrepeat" org-log-repeat nil)
  3072. ("fninline" org-footnote-define-inline t)
  3073. ("nofninline" org-footnote-define-inline nil)
  3074. ("fnlocal" org-footnote-section nil)
  3075. ("fnauto" org-footnote-auto-label t)
  3076. ("fnprompt" org-footnote-auto-label nil)
  3077. ("fnconfirm" org-footnote-auto-label confirm)
  3078. ("fnplain" org-footnote-auto-label plain)
  3079. ("fnadjust" org-footnote-auto-adjust t)
  3080. ("nofnadjust" org-footnote-auto-adjust nil)
  3081. ("constcgs" constants-unit-system cgs)
  3082. ("constSI" constants-unit-system SI)
  3083. ("noptag" org-tag-persistent-alist nil)
  3084. ("hideblocks" org-hide-block-startup t)
  3085. ("nohideblocks" org-hide-block-startup nil))
  3086. "Variable associated with STARTUP options for org-mode.
  3087. Each element is a list of three items: The startup options as written
  3088. in the #+STARTUP line, the corresponding variable, and the value to
  3089. set this variable to if the option is found. An optional forth element PUSH
  3090. means to push this value onto the list in the variable.")
  3091. (defun org-set-regexps-and-options ()
  3092. "Precompute regular expressions for current buffer."
  3093. (when (org-mode-p)
  3094. (org-set-local 'org-todo-kwd-alist nil)
  3095. (org-set-local 'org-todo-key-alist nil)
  3096. (org-set-local 'org-todo-key-trigger nil)
  3097. (org-set-local 'org-todo-keywords-1 nil)
  3098. (org-set-local 'org-done-keywords nil)
  3099. (org-set-local 'org-todo-heads nil)
  3100. (org-set-local 'org-todo-sets nil)
  3101. (org-set-local 'org-todo-log-states nil)
  3102. (org-set-local 'org-file-properties nil)
  3103. (org-set-local 'org-file-tags nil)
  3104. (let ((re (org-make-options-regexp
  3105. '("CATEGORY" "TODO" "COLUMNS"
  3106. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3107. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3108. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3109. (splitre "[ \t]+")
  3110. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3111. tail sep kws1 prio props ftags drawers
  3112. ext-setup-or-nil setup-contents (start 0))
  3113. (save-excursion
  3114. (save-restriction
  3115. (widen)
  3116. (goto-char (point-min))
  3117. (while (or (and ext-setup-or-nil
  3118. (string-match re ext-setup-or-nil start)
  3119. (setq start (match-end 0)))
  3120. (and (setq ext-setup-or-nil nil start 0)
  3121. (re-search-forward re nil t)))
  3122. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3123. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3124. (cond
  3125. ((equal key "CATEGORY")
  3126. (if (string-match "[ \t]+$" value)
  3127. (setq value (replace-match "" t t value)))
  3128. (setq cat value))
  3129. ((member key '("SEQ_TODO" "TODO"))
  3130. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3131. ((equal key "TYP_TODO")
  3132. (push (cons 'type (org-split-string value splitre)) kwds))
  3133. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3134. ;; general TODO-like setup
  3135. (push (cons (intern (downcase (match-string 1 key)))
  3136. (org-split-string value splitre)) kwds))
  3137. ((equal key "TAGS")
  3138. (setq tags (append tags (if tags '("\\n") nil)
  3139. (org-split-string value splitre))))
  3140. ((equal key "COLUMNS")
  3141. (org-set-local 'org-columns-default-format value))
  3142. ((equal key "LINK")
  3143. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3144. (push (cons (match-string 1 value)
  3145. (org-trim (match-string 2 value)))
  3146. links)))
  3147. ((equal key "PRIORITIES")
  3148. (setq prio (org-split-string value " +")))
  3149. ((equal key "PROPERTY")
  3150. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3151. (push (cons (match-string 1 value) (match-string 2 value))
  3152. props)))
  3153. ((equal key "FILETAGS")
  3154. (when (string-match "\\S-" value)
  3155. (setq ftags
  3156. (append
  3157. ftags
  3158. (apply 'append
  3159. (mapcar (lambda (x) (org-split-string x ":"))
  3160. (org-split-string value)))))))
  3161. ((equal key "DRAWERS")
  3162. (setq drawers (org-split-string value splitre)))
  3163. ((equal key "CONSTANTS")
  3164. (setq const (append const (org-split-string value splitre))))
  3165. ((equal key "STARTUP")
  3166. (let ((opts (org-split-string value splitre))
  3167. l var val)
  3168. (while (setq l (pop opts))
  3169. (when (setq l (assoc l org-startup-options))
  3170. (setq var (nth 1 l) val (nth 2 l))
  3171. (if (not (nth 3 l))
  3172. (set (make-local-variable var) val)
  3173. (if (not (listp (symbol-value var)))
  3174. (set (make-local-variable var) nil))
  3175. (set (make-local-variable var) (symbol-value var))
  3176. (add-to-list var val))))))
  3177. ((equal key "ARCHIVE")
  3178. (string-match " *$" value)
  3179. (setq arch (replace-match "" t t value))
  3180. (remove-text-properties 0 (length arch)
  3181. '(face t fontified t) arch))
  3182. ((equal key "SETUPFILE")
  3183. (setq setup-contents (org-file-contents
  3184. (expand-file-name
  3185. (org-remove-double-quotes value))
  3186. 'noerror))
  3187. (if (not ext-setup-or-nil)
  3188. (setq ext-setup-or-nil setup-contents start 0)
  3189. (setq ext-setup-or-nil
  3190. (concat (substring ext-setup-or-nil 0 start)
  3191. "\n" setup-contents "\n"
  3192. (substring ext-setup-or-nil start)))))
  3193. ))))
  3194. (when cat
  3195. (org-set-local 'org-category (intern cat))
  3196. (push (cons "CATEGORY" cat) props))
  3197. (when prio
  3198. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3199. (setq prio (mapcar 'string-to-char prio))
  3200. (org-set-local 'org-highest-priority (nth 0 prio))
  3201. (org-set-local 'org-lowest-priority (nth 1 prio))
  3202. (org-set-local 'org-default-priority (nth 2 prio)))
  3203. (and props (org-set-local 'org-file-properties (nreverse props)))
  3204. (and ftags (org-set-local 'org-file-tags
  3205. (mapcar 'org-add-prop-inherited ftags)))
  3206. (and drawers (org-set-local 'org-drawers drawers))
  3207. (and arch (org-set-local 'org-archive-location arch))
  3208. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3209. ;; Process the TODO keywords
  3210. (unless kwds
  3211. ;; Use the global values as if they had been given locally.
  3212. (setq kwds (default-value 'org-todo-keywords))
  3213. (if (stringp (car kwds))
  3214. (setq kwds (list (cons org-todo-interpretation
  3215. (default-value 'org-todo-keywords)))))
  3216. (setq kwds (reverse kwds)))
  3217. (setq kwds (nreverse kwds))
  3218. (let (inter kws kw)
  3219. (while (setq kws (pop kwds))
  3220. (let ((kws (or
  3221. (run-hook-with-args-until-success
  3222. 'org-todo-setup-filter-hook kws)
  3223. kws)))
  3224. (setq inter (pop kws) sep (member "|" kws)
  3225. kws0 (delete "|" (copy-sequence kws))
  3226. kwsa nil
  3227. kws1 (mapcar
  3228. (lambda (x)
  3229. ;; 1 2
  3230. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3231. (progn
  3232. (setq kw (match-string 1 x)
  3233. key (and (match-end 2) (match-string 2 x))
  3234. log (org-extract-log-state-settings x))
  3235. (push (cons kw (and key (string-to-char key))) kwsa)
  3236. (and log (push log org-todo-log-states))
  3237. kw)
  3238. (error "Invalid TODO keyword %s" x)))
  3239. kws0)
  3240. kwsa (if kwsa (append '((:startgroup))
  3241. (nreverse kwsa)
  3242. '((:endgroup))))
  3243. hw (car kws1)
  3244. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3245. tail (list inter hw (car dws) (org-last dws))))
  3246. (add-to-list 'org-todo-heads hw 'append)
  3247. (push kws1 org-todo-sets)
  3248. (setq org-done-keywords (append org-done-keywords dws nil))
  3249. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3250. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3251. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3252. (setq org-todo-sets (nreverse org-todo-sets)
  3253. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3254. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3255. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3256. ;; Process the constants
  3257. (when const
  3258. (let (e cst)
  3259. (while (setq e (pop const))
  3260. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3261. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3262. (setq org-table-formula-constants-local cst)))
  3263. ;; Process the tags.
  3264. (when tags
  3265. (let (e tgs)
  3266. (while (setq e (pop tags))
  3267. (cond
  3268. ((equal e "{") (push '(:startgroup) tgs))
  3269. ((equal e "}") (push '(:endgroup) tgs))
  3270. ((equal e "\\n") (push '(:newline) tgs))
  3271. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3272. (push (cons (match-string 1 e)
  3273. (string-to-char (match-string 2 e)))
  3274. tgs))
  3275. (t (push (list e) tgs))))
  3276. (org-set-local 'org-tag-alist nil)
  3277. (while (setq e (pop tgs))
  3278. (or (and (stringp (car e))
  3279. (assoc (car e) org-tag-alist))
  3280. (push e org-tag-alist)))))
  3281. ;; Compute the regular expressions and other local variables
  3282. (if (not org-done-keywords)
  3283. (setq org-done-keywords (and org-todo-keywords-1
  3284. (list (org-last org-todo-keywords-1)))))
  3285. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3286. (length org-scheduled-string)
  3287. (length org-clock-string)
  3288. (length org-closed-string)))
  3289. org-drawer-regexp
  3290. (concat "^[ \t]*:\\("
  3291. (mapconcat 'regexp-quote org-drawers "\\|")
  3292. "\\):[ \t]*$")
  3293. org-not-done-keywords
  3294. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3295. org-todo-regexp
  3296. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3297. "\\|") "\\)\\>")
  3298. org-not-done-regexp
  3299. (concat "\\<\\("
  3300. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3301. "\\)\\>")
  3302. org-not-done-heading-regexp
  3303. (concat "^\\(\\*+\\)[ \t]+\\("
  3304. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3305. "\\)\\>")
  3306. org-todo-line-regexp
  3307. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3308. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3309. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3310. org-complex-heading-regexp
  3311. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3312. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3313. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3314. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3315. org-nl-done-regexp
  3316. (concat "\n\\*+[ \t]+"
  3317. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3318. "\\)" "\\>")
  3319. org-todo-line-tags-regexp
  3320. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3321. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3322. (org-re
  3323. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3324. org-looking-at-done-regexp
  3325. (concat "^" "\\(?:"
  3326. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3327. "\\>")
  3328. org-deadline-regexp (concat "\\<" org-deadline-string)
  3329. org-deadline-time-regexp
  3330. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3331. org-deadline-line-regexp
  3332. (concat "\\<\\(" org-deadline-string "\\).*")
  3333. org-scheduled-regexp
  3334. (concat "\\<" org-scheduled-string)
  3335. org-scheduled-time-regexp
  3336. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3337. org-closed-time-regexp
  3338. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3339. org-keyword-time-regexp
  3340. (concat "\\<\\(" org-scheduled-string
  3341. "\\|" org-deadline-string
  3342. "\\|" org-closed-string
  3343. "\\|" org-clock-string "\\)"
  3344. " *[[<]\\([^]>]+\\)[]>]")
  3345. org-keyword-time-not-clock-regexp
  3346. (concat "\\<\\(" org-scheduled-string
  3347. "\\|" org-deadline-string
  3348. "\\|" org-closed-string
  3349. "\\)"
  3350. " *[[<]\\([^]>]+\\)[]>]")
  3351. org-maybe-keyword-time-regexp
  3352. (concat "\\(\\<\\(" org-scheduled-string
  3353. "\\|" org-deadline-string
  3354. "\\|" org-closed-string
  3355. "\\|" org-clock-string "\\)\\)?"
  3356. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3357. org-planning-or-clock-line-re
  3358. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3359. "\\|" org-deadline-string
  3360. "\\|" org-closed-string "\\|" org-clock-string
  3361. "\\)\\>\\)")
  3362. )
  3363. (org-compute-latex-and-specials-regexp)
  3364. (org-set-font-lock-defaults))))
  3365. (defun org-file-contents (file &optional noerror)
  3366. "Return the contents of FILE, as a string."
  3367. (if (or (not file)
  3368. (not (file-readable-p file)))
  3369. (if noerror
  3370. (progn
  3371. (message "Cannot read file %s" file)
  3372. (ding) (sit-for 2)
  3373. "")
  3374. (error "Cannot read file %s" file))
  3375. (with-temp-buffer
  3376. (insert-file-contents file)
  3377. (buffer-string))))
  3378. (defun org-extract-log-state-settings (x)
  3379. "Extract the log state setting from a TODO keyword string.
  3380. This will extract info from a string like \"WAIT(w@/!)\"."
  3381. (let (kw key log1 log2)
  3382. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3383. (setq kw (match-string 1 x)
  3384. key (and (match-end 2) (match-string 2 x))
  3385. log1 (and (match-end 3) (match-string 3 x))
  3386. log2 (and (match-end 4) (match-string 4 x)))
  3387. (and (or log1 log2)
  3388. (list kw
  3389. (and log1 (if (equal log1 "!") 'time 'note))
  3390. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3391. (defun org-remove-keyword-keys (list)
  3392. "Remove a pair of parenthesis at the end of each string in LIST."
  3393. (mapcar (lambda (x)
  3394. (if (string-match "(.*)$" x)
  3395. (substring x 0 (match-beginning 0))
  3396. x))
  3397. list))
  3398. ;; FIXME: this could be done much better, using second characters etc.
  3399. (defun org-assign-fast-keys (alist)
  3400. "Assign fast keys to a keyword-key alist.
  3401. Respect keys that are already there."
  3402. (let (new e k c c1 c2 (char ?a))
  3403. (while (setq e (pop alist))
  3404. (cond
  3405. ((equal e '(:startgroup)) (push e new))
  3406. ((equal e '(:endgroup)) (push e new))
  3407. ((equal e '(:newline)) (push e new))
  3408. (t
  3409. (setq k (car e) c2 nil)
  3410. (if (cdr e)
  3411. (setq c (cdr e))
  3412. ;; automatically assign a character.
  3413. (setq c1 (string-to-char
  3414. (downcase (substring
  3415. k (if (= (string-to-char k) ?@) 1 0)))))
  3416. (if (or (rassoc c1 new) (rassoc c1 alist))
  3417. (while (or (rassoc char new) (rassoc char alist))
  3418. (setq char (1+ char)))
  3419. (setq c2 c1))
  3420. (setq c (or c2 char)))
  3421. (push (cons k c) new))))
  3422. (nreverse new)))
  3423. ;;; Some variables used in various places
  3424. (defvar org-window-configuration nil
  3425. "Used in various places to store a window configuration.")
  3426. (defvar org-finish-function nil
  3427. "Function to be called when `C-c C-c' is used.
  3428. This is for getting out of special buffers like remember.")
  3429. ;; FIXME: Occasionally check by commenting these, to make sure
  3430. ;; no other functions uses these, forgetting to let-bind them.
  3431. (defvar entry)
  3432. (defvar last-state)
  3433. (defvar date)
  3434. ;; Defined somewhere in this file, but used before definition.
  3435. (defvar org-html-entities)
  3436. (defvar org-struct-menu)
  3437. (defvar org-org-menu)
  3438. (defvar org-tbl-menu)
  3439. (defvar org-agenda-keymap)
  3440. ;;;; Define the Org-mode
  3441. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3442. (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."))
  3443. ;; We use a before-change function to check if a table might need
  3444. ;; an update.
  3445. (defvar org-table-may-need-update t
  3446. "Indicates that a table might need an update.
  3447. This variable is set by `org-before-change-function'.
  3448. `org-table-align' sets it back to nil.")
  3449. (defun org-before-change-function (beg end)
  3450. "Every change indicates that a table might need an update."
  3451. (setq org-table-may-need-update t))
  3452. (defvar org-mode-map)
  3453. (defvar org-mode-hook nil
  3454. "Mode hook for Org-mode, run after the mode was turned on.")
  3455. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3456. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3457. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3458. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3459. (defvar org-table-buffer-is-an nil)
  3460. (defconst org-outline-regexp "\\*+ ")
  3461. ;;;###autoload
  3462. (define-derived-mode org-mode outline-mode "Org"
  3463. "Outline-based notes management and organizer, alias
  3464. \"Carsten's outline-mode for keeping track of everything.\"
  3465. Org-mode develops organizational tasks around a NOTES file which
  3466. contains information about projects as plain text. Org-mode is
  3467. implemented on top of outline-mode, which is ideal to keep the content
  3468. of large files well structured. It supports ToDo items, deadlines and
  3469. time stamps, which magically appear in the diary listing of the Emacs
  3470. calendar. Tables are easily created with a built-in table editor.
  3471. Plain text URL-like links connect to websites, emails (VM), Usenet
  3472. messages (Gnus), BBDB entries, and any files related to the project.
  3473. For printing and sharing of notes, an Org-mode file (or a part of it)
  3474. can be exported as a structured ASCII or HTML file.
  3475. The following commands are available:
  3476. \\{org-mode-map}"
  3477. ;; Get rid of Outline menus, they are not needed
  3478. ;; Need to do this here because define-derived-mode sets up
  3479. ;; the keymap so late. Still, it is a waste to call this each time
  3480. ;; we switch another buffer into org-mode.
  3481. (if (featurep 'xemacs)
  3482. (when (boundp 'outline-mode-menu-heading)
  3483. ;; Assume this is Greg's port, it used easymenu
  3484. (easy-menu-remove outline-mode-menu-heading)
  3485. (easy-menu-remove outline-mode-menu-show)
  3486. (easy-menu-remove outline-mode-menu-hide))
  3487. (define-key org-mode-map [menu-bar headings] 'undefined)
  3488. (define-key org-mode-map [menu-bar hide] 'undefined)
  3489. (define-key org-mode-map [menu-bar show] 'undefined))
  3490. (org-load-modules-maybe)
  3491. (easy-menu-add org-org-menu)
  3492. (easy-menu-add org-tbl-menu)
  3493. (org-install-agenda-files-menu)
  3494. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3495. (org-add-to-invisibility-spec '(org-cwidth))
  3496. (org-add-to-invisibility-spec '(org-hide-block . t))
  3497. (when (featurep 'xemacs)
  3498. (org-set-local 'line-move-ignore-invisible t))
  3499. (org-set-local 'outline-regexp org-outline-regexp)
  3500. (org-set-local 'outline-level 'org-outline-level)
  3501. (when (and org-ellipsis
  3502. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3503. (fboundp 'make-glyph-code))
  3504. (unless org-display-table
  3505. (setq org-display-table (make-display-table)))
  3506. (set-display-table-slot
  3507. org-display-table 4
  3508. (vconcat (mapcar
  3509. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3510. org-ellipsis)))
  3511. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3512. (setq buffer-display-table org-display-table))
  3513. (org-set-regexps-and-options)
  3514. (when (and org-tag-faces (not org-tags-special-faces-re))
  3515. ;; tag faces set outside customize.... force initialization.
  3516. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3517. ;; Calc embedded
  3518. (org-set-local 'calc-embedded-open-mode "# ")
  3519. (modify-syntax-entry ?# "<")
  3520. (modify-syntax-entry ?@ "w")
  3521. (if org-startup-truncated (setq truncate-lines t))
  3522. (org-set-local 'font-lock-unfontify-region-function
  3523. 'org-unfontify-region)
  3524. ;; Activate before-change-function
  3525. (org-set-local 'org-table-may-need-update t)
  3526. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3527. 'local)
  3528. ;; Check for running clock before killing a buffer
  3529. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3530. ;; Paragraphs and auto-filling
  3531. (org-set-autofill-regexps)
  3532. (setq indent-line-function 'org-indent-line-function)
  3533. (org-update-radio-target-regexp)
  3534. ;; Make sure dependence stuff works reliably, even for users who set it
  3535. ;; too late :-(
  3536. (if org-enforce-todo-dependencies
  3537. (add-hook 'org-blocker-hook
  3538. 'org-block-todo-from-children-or-siblings-or-parent)
  3539. (remove-hook 'org-blocker-hook
  3540. 'org-block-todo-from-children-or-siblings-or-parent))
  3541. (if org-enforce-todo-checkbox-dependencies
  3542. (add-hook 'org-blocker-hook
  3543. 'org-block-todo-from-checkboxes)
  3544. (remove-hook 'org-blocker-hook
  3545. 'org-block-todo-from-checkboxes))
  3546. ;; Comment characters
  3547. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3548. (org-set-local 'comment-padding " ")
  3549. ;; Align options lines
  3550. (org-set-local
  3551. 'align-mode-rules-list
  3552. '((org-in-buffer-settings
  3553. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3554. (modes . '(org-mode)))))
  3555. ;; Imenu
  3556. (org-set-local 'imenu-create-index-function
  3557. 'org-imenu-get-tree)
  3558. ;; Make isearch reveal context
  3559. (if (or (featurep 'xemacs)
  3560. (not (boundp 'outline-isearch-open-invisible-function)))
  3561. ;; Emacs 21 and XEmacs make use of the hook
  3562. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3563. ;; Emacs 22 deals with this through a special variable
  3564. (org-set-local 'outline-isearch-open-invisible-function
  3565. (lambda (&rest ignore) (org-show-context 'isearch))))
  3566. ;; If empty file that did not turn on org-mode automatically, make it to.
  3567. (if (and org-insert-mode-line-in-empty-file
  3568. (interactive-p)
  3569. (= (point-min) (point-max)))
  3570. (insert "# -*- mode: org -*-\n\n"))
  3571. (unless org-inhibit-startup
  3572. (when org-startup-align-all-tables
  3573. (let ((bmp (buffer-modified-p)))
  3574. (org-table-map-tables 'org-table-align)
  3575. (set-buffer-modified-p bmp)))
  3576. (when org-startup-indented
  3577. (require 'org-indent)
  3578. (org-indent-mode 1))
  3579. (org-set-startup-visibility)))
  3580. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3581. (defun org-current-time ()
  3582. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3583. (if (> (car org-time-stamp-rounding-minutes) 1)
  3584. (let ((r (car org-time-stamp-rounding-minutes))
  3585. (time (decode-time)))
  3586. (apply 'encode-time
  3587. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3588. (nthcdr 2 time))))
  3589. (current-time)))
  3590. ;;;; Font-Lock stuff, including the activators
  3591. (defvar org-mouse-map (make-sparse-keymap))
  3592. (org-defkey org-mouse-map
  3593. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3594. (org-defkey org-mouse-map
  3595. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3596. (when org-mouse-1-follows-link
  3597. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3598. (when org-tab-follows-link
  3599. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3600. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3601. (require 'font-lock)
  3602. (defconst org-non-link-chars "]\t\n\r<>")
  3603. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3604. "shell" "elisp"))
  3605. (defvar org-link-types-re nil
  3606. "Matches a link that has a url-like prefix like \"http:\"")
  3607. (defvar org-link-re-with-space nil
  3608. "Matches a link with spaces, optional angular brackets around it.")
  3609. (defvar org-link-re-with-space2 nil
  3610. "Matches a link with spaces, optional angular brackets around it.")
  3611. (defvar org-link-re-with-space3 nil
  3612. "Matches a link with spaces, only for internal part in bracket links.")
  3613. (defvar org-angle-link-re nil
  3614. "Matches link with angular brackets, spaces are allowed.")
  3615. (defvar org-plain-link-re nil
  3616. "Matches plain link, without spaces.")
  3617. (defvar org-bracket-link-regexp nil
  3618. "Matches a link in double brackets.")
  3619. (defvar org-bracket-link-analytic-regexp nil
  3620. "Regular expression used to analyze links.
  3621. Here is what the match groups contain after a match:
  3622. 1: http:
  3623. 2: http
  3624. 3: path
  3625. 4: [desc]
  3626. 5: desc")
  3627. (defvar org-bracket-link-analytic-regexp++ nil
  3628. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3629. (defvar org-any-link-re nil
  3630. "Regular expression matching any link.")
  3631. (defun org-make-link-regexps ()
  3632. "Update the link regular expressions.
  3633. This should be called after the variable `org-link-types' has changed."
  3634. (setq org-link-types-re
  3635. (concat
  3636. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3637. org-link-re-with-space
  3638. (concat
  3639. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3640. "\\([^" org-non-link-chars " ]"
  3641. "[^" org-non-link-chars "]*"
  3642. "[^" org-non-link-chars " ]\\)>?")
  3643. org-link-re-with-space2
  3644. (concat
  3645. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3646. "\\([^" org-non-link-chars " ]"
  3647. "[^\t\n\r]*"
  3648. "[^" org-non-link-chars " ]\\)>?")
  3649. org-link-re-with-space3
  3650. (concat
  3651. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3652. "\\([^" org-non-link-chars " ]"
  3653. "[^\t\n\r]*\\)")
  3654. org-angle-link-re
  3655. (concat
  3656. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3657. "\\([^" org-non-link-chars " ]"
  3658. "[^" org-non-link-chars "]*"
  3659. "\\)>")
  3660. org-plain-link-re
  3661. (concat
  3662. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3663. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3664. org-bracket-link-regexp
  3665. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3666. org-bracket-link-analytic-regexp
  3667. (concat
  3668. "\\[\\["
  3669. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3670. "\\([^]]+\\)"
  3671. "\\]"
  3672. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3673. "\\]")
  3674. org-bracket-link-analytic-regexp++
  3675. (concat
  3676. "\\[\\["
  3677. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3678. "\\([^]]+\\)"
  3679. "\\]"
  3680. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3681. "\\]")
  3682. org-any-link-re
  3683. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3684. org-angle-link-re "\\)\\|\\("
  3685. org-plain-link-re "\\)")))
  3686. (org-make-link-regexps)
  3687. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3688. "Regular expression for fast time stamp matching.")
  3689. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3690. "Regular expression for fast time stamp matching.")
  3691. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3692. "Regular expression matching time strings for analysis.
  3693. This one does not require the space after the date, so it can be used
  3694. on a string that terminates immediately after the date.")
  3695. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3696. "Regular expression matching time strings for analysis.")
  3697. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3698. "Regular expression matching time stamps, with groups.")
  3699. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3700. "Regular expression matching time stamps (also [..]), with groups.")
  3701. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3702. "Regular expression matching a time stamp range.")
  3703. (defconst org-tr-regexp-both
  3704. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3705. "Regular expression matching a time stamp range.")
  3706. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3707. org-ts-regexp "\\)?")
  3708. "Regular expression matching a time stamp or time stamp range.")
  3709. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3710. org-ts-regexp-both "\\)?")
  3711. "Regular expression matching a time stamp or time stamp range.
  3712. The time stamps may be either active or inactive.")
  3713. (defvar org-emph-face nil)
  3714. (defun org-do-emphasis-faces (limit)
  3715. "Run through the buffer and add overlays to links."
  3716. (let (rtn a)
  3717. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3718. (if (not (= (char-after (match-beginning 3))
  3719. (char-after (match-beginning 4))))
  3720. (progn
  3721. (setq rtn t)
  3722. (setq a (assoc (match-string 3) org-emphasis-alist))
  3723. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3724. 'face
  3725. (nth 1 a))
  3726. (and (nth 4 a)
  3727. (org-remove-flyspell-overlays-in
  3728. (match-beginning 0) (match-end 0)))
  3729. (add-text-properties (match-beginning 2) (match-end 2)
  3730. '(font-lock-multiline t))
  3731. (when org-hide-emphasis-markers
  3732. (add-text-properties (match-end 4) (match-beginning 5)
  3733. '(invisible org-link))
  3734. (add-text-properties (match-beginning 3) (match-end 3)
  3735. '(invisible org-link)))))
  3736. (backward-char 1))
  3737. rtn))
  3738. (defun org-emphasize (&optional char)
  3739. "Insert or change an emphasis, i.e. a font like bold or italic.
  3740. If there is an active region, change that region to a new emphasis.
  3741. If there is no region, just insert the marker characters and position
  3742. the cursor between them.
  3743. CHAR should be either the marker character, or the first character of the
  3744. HTML tag associated with that emphasis. If CHAR is a space, the means
  3745. to remove the emphasis of the selected region.
  3746. If char is not given (for example in an interactive call) it
  3747. will be prompted for."
  3748. (interactive)
  3749. (let ((eal org-emphasis-alist) e det
  3750. (erc org-emphasis-regexp-components)
  3751. (prompt "")
  3752. (string "") beg end move tag c s)
  3753. (if (org-region-active-p)
  3754. (setq beg (region-beginning) end (region-end)
  3755. string (buffer-substring beg end))
  3756. (setq move t))
  3757. (while (setq e (pop eal))
  3758. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3759. c (aref tag 0))
  3760. (push (cons c (string-to-char (car e))) det)
  3761. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3762. (substring tag 1)))))
  3763. (setq det (nreverse det))
  3764. (unless char
  3765. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3766. (setq char (read-char-exclusive)))
  3767. (setq char (or (cdr (assoc char det)) char))
  3768. (if (equal char ?\ )
  3769. (setq s "" move nil)
  3770. (unless (assoc (char-to-string char) org-emphasis-alist)
  3771. (error "No such emphasis marker: \"%c\"" char))
  3772. (setq s (char-to-string char)))
  3773. (while (and (> (length string) 1)
  3774. (equal (substring string 0 1) (substring string -1))
  3775. (assoc (substring string 0 1) org-emphasis-alist))
  3776. (setq string (substring string 1 -1)))
  3777. (setq string (concat s string s))
  3778. (if beg (delete-region beg end))
  3779. (unless (or (bolp)
  3780. (string-match (concat "[" (nth 0 erc) "\n]")
  3781. (char-to-string (char-before (point)))))
  3782. (insert " "))
  3783. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3784. (char-to-string (char-after (point))))
  3785. (insert " ") (backward-char 1))
  3786. (insert string)
  3787. (and move (backward-char 1))))
  3788. (defconst org-nonsticky-props
  3789. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3790. (defsubst org-rear-nonsticky-at (pos)
  3791. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3792. (defun org-activate-plain-links (limit)
  3793. "Run through the buffer and add overlays to links."
  3794. (catch 'exit
  3795. (let (f)
  3796. (if (re-search-forward org-plain-link-re limit t)
  3797. (progn
  3798. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3799. (setq f (get-text-property (match-beginning 0) 'face))
  3800. (if (or (eq f 'org-tag)
  3801. (and (listp f) (memq 'org-tag f)))
  3802. nil
  3803. (add-text-properties (match-beginning 0) (match-end 0)
  3804. (list 'mouse-face 'highlight
  3805. 'keymap org-mouse-map))
  3806. (org-rear-nonsticky-at (match-end 0)))
  3807. t)))))
  3808. (defun org-activate-code (limit)
  3809. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3810. (progn
  3811. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3812. (remove-text-properties (match-beginning 0) (match-end 0)
  3813. '(display t invisible t intangible t))
  3814. t)))
  3815. (defun org-fontify-meta-lines-and-blocks (limit)
  3816. "Fontify #+ lines and blocks, in the correct ways."
  3817. (let ((case-fold-search t))
  3818. (if (re-search-forward
  3819. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3820. limit t)
  3821. (let ((beg (match-beginning 0))
  3822. (beg1 (line-beginning-position 2))
  3823. (dc1 (downcase (match-string 2)))
  3824. (dc3 (downcase (match-string 3)))
  3825. end end1 quoting)
  3826. (cond
  3827. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3828. ;; a single line of backend-specific content
  3829. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3830. (remove-text-properties (match-beginning 0) (match-end 0)
  3831. '(display t invisible t intangible t))
  3832. (add-text-properties (match-beginning 1) (match-end 3)
  3833. '(font-lock-fontified t face org-meta-line))
  3834. (add-text-properties (match-beginning 6) (match-end 6)
  3835. '(font-lock-fontified t face org-block))
  3836. t)
  3837. ((and (match-end 4) (equal dc3 "begin"))
  3838. ;; Truely a block
  3839. (setq quoting (member (downcase (match-string 5))
  3840. org-protecting-blocks))
  3841. (when (re-search-forward
  3842. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3843. nil t) ;; on purpose, we look further than LIMIT
  3844. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3845. (when quoting
  3846. (remove-text-properties beg end
  3847. '(display t invisible t intangible t)))
  3848. (add-text-properties
  3849. beg end
  3850. '(font-lock-fontified t font-lock-multiline t))
  3851. (add-text-properties beg beg1 '(face org-meta-line))
  3852. (add-text-properties end1 end '(face org-meta-line))
  3853. (when quoting
  3854. (add-text-properties beg1 end1 '(face org-block)))
  3855. t))
  3856. ((not (member (char-after beg) '(?\ ?\t)))
  3857. ;; just any other in-buffer setting, but not indented
  3858. (add-text-properties
  3859. beg (match-end 0)
  3860. '(font-lock-fontified t face org-meta-line))
  3861. t)
  3862. ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:" "tblname:"))
  3863. (and (match-end 4) (equal dc3 "attr")))
  3864. (add-text-properties
  3865. beg (match-end 0)
  3866. '(font-lock-fontified t face org-meta-line))
  3867. t)
  3868. (t nil))))))
  3869. (defun org-activate-angle-links (limit)
  3870. "Run through the buffer and add overlays to links."
  3871. (if (re-search-forward org-angle-link-re limit t)
  3872. (progn
  3873. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3874. (add-text-properties (match-beginning 0) (match-end 0)
  3875. (list 'mouse-face 'highlight
  3876. 'keymap org-mouse-map))
  3877. (org-rear-nonsticky-at (match-end 0))
  3878. t)))
  3879. (defun org-activate-footnote-links (limit)
  3880. "Run through the buffer and add overlays to links."
  3881. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3882. limit t)
  3883. (progn
  3884. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3885. (add-text-properties (match-beginning 2) (match-end 2)
  3886. (list 'mouse-face 'highlight
  3887. 'keymap org-mouse-map
  3888. 'help-echo
  3889. (if (= (point-at-bol) (match-beginning 2))
  3890. "Footnote definition"
  3891. "Footnote reference")
  3892. ))
  3893. (org-rear-nonsticky-at (match-end 2))
  3894. t)))
  3895. (defun org-activate-bracket-links (limit)
  3896. "Run through the buffer and add overlays to bracketed links."
  3897. (if (re-search-forward org-bracket-link-regexp limit t)
  3898. (let* ((help (concat "LINK: "
  3899. (org-match-string-no-properties 1)))
  3900. ;; FIXME: above we should remove the escapes.
  3901. ;; but that requires another match, protecting match data,
  3902. ;; a lot of overhead for font-lock.
  3903. (ip (org-maybe-intangible
  3904. (list 'invisible 'org-link
  3905. 'keymap org-mouse-map 'mouse-face 'highlight
  3906. 'font-lock-multiline t 'help-echo help)))
  3907. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3908. 'font-lock-multiline t 'help-echo help)))
  3909. ;; We need to remove the invisible property here. Table narrowing
  3910. ;; may have made some of this invisible.
  3911. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3912. (remove-text-properties (match-beginning 0) (match-end 0)
  3913. '(invisible nil))
  3914. (if (match-end 3)
  3915. (progn
  3916. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3917. (org-rear-nonsticky-at (match-beginning 3))
  3918. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3919. (org-rear-nonsticky-at (match-end 3))
  3920. (add-text-properties (match-end 3) (match-end 0) ip)
  3921. (org-rear-nonsticky-at (match-end 0)))
  3922. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3923. (org-rear-nonsticky-at (match-beginning 1))
  3924. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3925. (org-rear-nonsticky-at (match-end 1))
  3926. (add-text-properties (match-end 1) (match-end 0) ip)
  3927. (org-rear-nonsticky-at (match-end 0)))
  3928. t)))
  3929. (defun org-activate-dates (limit)
  3930. "Run through the buffer and add overlays to dates."
  3931. (if (re-search-forward org-tsr-regexp-both limit t)
  3932. (progn
  3933. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3934. (add-text-properties (match-beginning 0) (match-end 0)
  3935. (list 'mouse-face 'highlight
  3936. 'keymap org-mouse-map))
  3937. (org-rear-nonsticky-at (match-end 0))
  3938. (when org-display-custom-times
  3939. (if (match-end 3)
  3940. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3941. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3942. t)))
  3943. (defvar org-target-link-regexp nil
  3944. "Regular expression matching radio targets in plain text.")
  3945. (make-variable-buffer-local 'org-target-link-regexp)
  3946. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3947. "Regular expression matching a link target.")
  3948. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3949. "Regular expression matching a radio target.")
  3950. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3951. "Regular expression matching any target.")
  3952. (defun org-activate-target-links (limit)
  3953. "Run through the buffer and add overlays to target matches."
  3954. (when org-target-link-regexp
  3955. (let ((case-fold-search t))
  3956. (if (re-search-forward org-target-link-regexp limit t)
  3957. (progn
  3958. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3959. (add-text-properties (match-beginning 0) (match-end 0)
  3960. (list 'mouse-face 'highlight
  3961. 'keymap org-mouse-map
  3962. 'help-echo "Radio target link"
  3963. 'org-linked-text t))
  3964. (org-rear-nonsticky-at (match-end 0))
  3965. t)))))
  3966. (defun org-update-radio-target-regexp ()
  3967. "Find all radio targets in this file and update the regular expression."
  3968. (interactive)
  3969. (when (memq 'radio org-activate-links)
  3970. (setq org-target-link-regexp
  3971. (org-make-target-link-regexp (org-all-targets 'radio)))
  3972. (org-restart-font-lock)))
  3973. (defun org-hide-wide-columns (limit)
  3974. (let (s e)
  3975. (setq s (text-property-any (point) (or limit (point-max))
  3976. 'org-cwidth t))
  3977. (when s
  3978. (setq e (next-single-property-change s 'org-cwidth))
  3979. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3980. (goto-char e)
  3981. t)))
  3982. (defvar org-latex-and-specials-regexp nil
  3983. "Regular expression for highlighting export special stuff.")
  3984. (defvar org-match-substring-regexp)
  3985. (defvar org-match-substring-with-braces-regexp)
  3986. ;; This should be with the exporter code, but we also use if for font-locking
  3987. (defconst org-export-html-special-string-regexps
  3988. '(("\\\\-" . "&shy;")
  3989. ("---\\([^-]\\)" . "&mdash;\\1")
  3990. ("--\\([^-]\\)" . "&ndash;\\1")
  3991. ("\\.\\.\\." . "&hellip;"))
  3992. "Regular expressions for special string conversion.")
  3993. (defun org-compute-latex-and-specials-regexp ()
  3994. "Compute regular expression for stuff treated specially by exporters."
  3995. (if (not org-highlight-latex-fragments-and-specials)
  3996. (org-set-local 'org-latex-and-specials-regexp nil)
  3997. (require 'org-exp)
  3998. (let*
  3999. ((matchers (plist-get org-format-latex-options :matchers))
  4000. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4001. org-latex-regexps)))
  4002. (options (org-combine-plists (org-default-export-plist)
  4003. (org-infile-export-plist)))
  4004. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4005. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4006. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4007. (org-export-html-expand (plist-get options :expand-quoted-html))
  4008. (org-export-with-special-strings (plist-get options :special-strings))
  4009. (re-sub
  4010. (cond
  4011. ((equal org-export-with-sub-superscripts '{})
  4012. (list org-match-substring-with-braces-regexp))
  4013. (org-export-with-sub-superscripts
  4014. (list org-match-substring-regexp))
  4015. (t nil)))
  4016. (re-latex
  4017. (if org-export-with-LaTeX-fragments
  4018. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4019. (re-macros
  4020. (if org-export-with-TeX-macros
  4021. (list (concat "\\\\"
  4022. (regexp-opt
  4023. (append (mapcar 'car org-html-entities)
  4024. (if (boundp 'org-latex-entities)
  4025. (mapcar (lambda (x)
  4026. (or (car-safe x) x))
  4027. org-latex-entities)
  4028. nil))
  4029. 'words))) ; FIXME
  4030. ))
  4031. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4032. (re-special (if org-export-with-special-strings
  4033. (mapcar (lambda (x) (car x))
  4034. org-export-html-special-string-regexps)))
  4035. (re-rest
  4036. (delq nil
  4037. (list
  4038. (if org-export-html-expand "@<[^>\n]+>")
  4039. ))))
  4040. (org-set-local
  4041. 'org-latex-and-specials-regexp
  4042. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4043. re-rest) "\\|")))))
  4044. (defun org-do-latex-and-special-faces (limit)
  4045. "Run through the buffer and add overlays to links."
  4046. (when org-latex-and-specials-regexp
  4047. (let (rtn d)
  4048. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4049. limit t))
  4050. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4051. 'face))
  4052. '(org-code org-verbatim underline)))
  4053. (progn
  4054. (setq rtn t
  4055. d (cond ((member (char-after (1+ (match-beginning 0)))
  4056. '(?_ ?^)) 1)
  4057. (t 0)))
  4058. (font-lock-prepend-text-property
  4059. (+ d (match-beginning 0)) (match-end 0)
  4060. 'face 'org-latex-and-export-specials)
  4061. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4062. '(font-lock-multiline t)))))
  4063. rtn)))
  4064. (defun org-restart-font-lock ()
  4065. "Restart font-lock-mode, to force refontification."
  4066. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4067. (font-lock-mode -1)
  4068. (font-lock-mode 1)))
  4069. (defun org-all-targets (&optional radio)
  4070. "Return a list of all targets in this file.
  4071. With optional argument RADIO, only find radio targets."
  4072. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4073. rtn)
  4074. (save-excursion
  4075. (goto-char (point-min))
  4076. (while (re-search-forward re nil t)
  4077. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4078. rtn)))
  4079. (defun org-make-target-link-regexp (targets)
  4080. "Make regular expression matching all strings in TARGETS.
  4081. The regular expression finds the targets also if there is a line break
  4082. between words."
  4083. (and targets
  4084. (concat
  4085. "\\<\\("
  4086. (mapconcat
  4087. (lambda (x)
  4088. (while (string-match " +" x)
  4089. (setq x (replace-match "\\s-+" t t x)))
  4090. x)
  4091. targets
  4092. "\\|")
  4093. "\\)\\>")))
  4094. (defun org-activate-tags (limit)
  4095. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4096. (progn
  4097. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4098. (add-text-properties (match-beginning 1) (match-end 1)
  4099. (list 'mouse-face 'highlight
  4100. 'keymap org-mouse-map))
  4101. (org-rear-nonsticky-at (match-end 1))
  4102. t)))
  4103. (defun org-outline-level ()
  4104. (save-excursion
  4105. (looking-at outline-regexp)
  4106. (if (match-beginning 1)
  4107. (+ (org-get-string-indentation (match-string 1)) 1000)
  4108. (1- (- (match-end 0) (match-beginning 0))))))
  4109. (defvar org-font-lock-keywords nil)
  4110. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4111. "Regular expression matching a property line.")
  4112. (defvar org-font-lock-hook nil
  4113. "Functions to be called for special font lock stuff.")
  4114. (defun org-font-lock-hook (limit)
  4115. (run-hook-with-args 'org-font-lock-hook limit))
  4116. (defun org-set-font-lock-defaults ()
  4117. (let* ((em org-fontify-emphasized-text)
  4118. (lk org-activate-links)
  4119. (org-font-lock-extra-keywords
  4120. (list
  4121. ;; Call the hook
  4122. '(org-font-lock-hook)
  4123. ;; Headlines
  4124. `(,(if org-fontify-whole-heading-line
  4125. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4126. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4127. (1 (org-get-level-face 1))
  4128. (2 (org-get-level-face 2))
  4129. (3 (org-get-level-face 3)))
  4130. ;; Table lines
  4131. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4132. (1 'org-table t))
  4133. ;; Table internals
  4134. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4135. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4136. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4137. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4138. ;; Drawers
  4139. (list org-drawer-regexp '(0 'org-special-keyword t))
  4140. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4141. ;; Properties
  4142. (list org-property-re
  4143. '(1 'org-special-keyword t)
  4144. '(3 'org-property-value t))
  4145. ;; Links
  4146. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4147. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4148. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4149. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4150. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4151. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4152. (if (memq 'footnote lk) '(org-activate-footnote-links
  4153. (2 'org-footnote t)))
  4154. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4155. '(org-hide-wide-columns (0 nil append))
  4156. ;; TODO lines
  4157. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4158. '(1 (org-get-todo-face 1) t))
  4159. ;; DONE
  4160. (if org-fontify-done-headline
  4161. (list (concat "^[*]+ +\\<\\("
  4162. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4163. "\\)\\(.*\\)")
  4164. '(2 'org-headline-done t))
  4165. nil)
  4166. ;; Priorities
  4167. '(org-font-lock-add-priority-faces)
  4168. ;; Tags
  4169. '(org-font-lock-add-tag-faces)
  4170. ;; Special keywords
  4171. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4172. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4173. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4174. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4175. ;; Emphasis
  4176. (if em
  4177. (if (featurep 'xemacs)
  4178. '(org-do-emphasis-faces (0 nil append))
  4179. '(org-do-emphasis-faces)))
  4180. ;; Checkboxes
  4181. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4182. 2 'org-checkbox prepend)
  4183. (if org-provide-checkbox-statistics
  4184. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4185. (0 (org-get-checkbox-statistics-face) t)))
  4186. ;; Description list items
  4187. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4188. 2 'bold prepend)
  4189. ;; ARCHIVEd headings
  4190. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4191. '(1 'org-archived prepend))
  4192. ;; Specials
  4193. '(org-do-latex-and-special-faces)
  4194. ;; Code
  4195. '(org-activate-code (1 'org-code t))
  4196. ;; COMMENT
  4197. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4198. "\\|" org-quote-string "\\)\\>")
  4199. '(1 'org-special-keyword t))
  4200. '("^#.*" (0 'font-lock-comment-face t))
  4201. ;; Blocks and meta lines
  4202. '(org-fontify-meta-lines-and-blocks)
  4203. )))
  4204. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4205. ;; Now set the full font-lock-keywords
  4206. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4207. (org-set-local 'font-lock-defaults
  4208. '(org-font-lock-keywords t nil nil backward-paragraph))
  4209. (kill-local-variable 'font-lock-keywords) nil))
  4210. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4211. "Fontify string S like in Org-mode"
  4212. (with-temp-buffer
  4213. (insert s)
  4214. (let ((org-odd-levels-only odd-levels))
  4215. (org-mode)
  4216. (font-lock-fontify-buffer)
  4217. (buffer-string))))
  4218. (defvar org-m nil)
  4219. (defvar org-l nil)
  4220. (defvar org-f nil)
  4221. (defun org-get-level-face (n)
  4222. "Get the right face for match N in font-lock matching of headlines."
  4223. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4224. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4225. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4226. (cond
  4227. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4228. ((eq n 2) org-f)
  4229. (t (if org-level-color-stars-only nil org-f))))
  4230. (defun org-get-todo-face (kwd)
  4231. "Get the right face for a TODO keyword KWD.
  4232. If KWD is a number, get the corresponding match group."
  4233. (if (numberp kwd) (setq kwd (match-string kwd)))
  4234. (or (cdr (assoc kwd org-todo-keyword-faces))
  4235. (and (member kwd org-done-keywords) 'org-done)
  4236. 'org-todo))
  4237. (defun org-font-lock-add-tag-faces (limit)
  4238. "Add the special tag faces."
  4239. (when (and org-tag-faces org-tags-special-faces-re)
  4240. (while (re-search-forward org-tags-special-faces-re limit t)
  4241. (add-text-properties (match-beginning 1) (match-end 1)
  4242. (list 'face (org-get-tag-face 1)
  4243. 'font-lock-fontified t))
  4244. (backward-char 1))))
  4245. (defun org-font-lock-add-priority-faces (limit)
  4246. "Add the special priority faces."
  4247. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4248. (add-text-properties
  4249. (match-beginning 0) (match-end 0)
  4250. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4251. org-priority-faces))
  4252. 'org-special-keyword)
  4253. 'font-lock-fontified t))))
  4254. (defun org-get-tag-face (kwd)
  4255. "Get the right face for a TODO keyword KWD.
  4256. If KWD is a number, get the corresponding match group."
  4257. (if (numberp kwd) (setq kwd (match-string kwd)))
  4258. (or (cdr (assoc kwd org-tag-faces))
  4259. 'org-tag))
  4260. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4261. "Remove fontification and activation overlays from links."
  4262. (font-lock-default-unfontify-region beg end)
  4263. (let* ((buffer-undo-list t)
  4264. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4265. (inhibit-modification-hooks t)
  4266. deactivate-mark buffer-file-name buffer-file-truename)
  4267. (remove-text-properties beg end
  4268. '(mouse-face t keymap t org-linked-text t
  4269. invisible t intangible t
  4270. line-prefix t wrap-prefix t
  4271. org-no-flyspell t))))
  4272. ;;;; Visibility cycling, including org-goto and indirect buffer
  4273. ;;; Cycling
  4274. (defvar org-cycle-global-status nil)
  4275. (make-variable-buffer-local 'org-cycle-global-status)
  4276. (defvar org-cycle-subtree-status nil)
  4277. (make-variable-buffer-local 'org-cycle-subtree-status)
  4278. ;;;###autoload
  4279. (defvar org-inlinetask-min-level)
  4280. (defun org-cycle (&optional arg)
  4281. "TAB-action and visibility cycling for Org-mode.
  4282. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4283. is outine visibility cycling, but it also invokes other actions
  4284. in special contexts.
  4285. - When this function is called with a prefix argument, rotate the entire
  4286. buffer through 3 states (global cycling)
  4287. 1. OVERVIEW: Show only top-level headlines.
  4288. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4289. 3. SHOW ALL: Show everything.
  4290. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4291. determined by the variable `org-startup-folded', and by any VISIBILITY
  4292. properties in the buffer.
  4293. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4294. including any drawers.
  4295. - When inside a table, re-align the table and move to the next field.
  4296. - When point is at the beginning of a headline, rotate the subtree started
  4297. by this line through 3 different states (local cycling)
  4298. 1. FOLDED: Only the main headline is shown.
  4299. 2. CHILDREN: The main headline and the direct children are shown.
  4300. From this state, you can move to one of the children
  4301. and zoom in further.
  4302. 3. SUBTREE: Show the entire subtree, including body text.
  4303. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4304. - When there is a numeric prefix, go up to a heading with level ARG, do
  4305. a `show-subtree' and return to the previous cursor position. If ARG
  4306. is negative, go up that many levels.
  4307. - When point is not at the beginning of a headline, execute the global
  4308. binding for TAB, which is re-indenting the line. See the option
  4309. `org-cycle-emulate-tab' for details.
  4310. - Special case: if point is at the beginning of the buffer and there is
  4311. no headline in line 1, this function will act as if called with prefix arg.
  4312. But only if also the variable `org-cycle-global-at-bob' is t."
  4313. (interactive "P")
  4314. (org-load-modules-maybe)
  4315. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4316. (let* ((limit-level
  4317. (or org-cycle-max-level
  4318. (and (boundp 'org-inlinetask-min-level)
  4319. org-inlinetask-min-level
  4320. (1- org-inlinetask-min-level))))
  4321. (nstars (and limit-level
  4322. (if org-odd-levels-only
  4323. (and limit-level (1- (* limit-level 2)))
  4324. limit-level)))
  4325. (outline-regexp
  4326. (cond
  4327. ((not (org-mode-p)) outline-regexp)
  4328. ((or (eq org-cycle-include-plain-lists 'integrate)
  4329. (and org-cycle-include-plain-lists (org-at-item-p)))
  4330. (concat "\\(?:\\*"
  4331. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4332. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4333. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4334. (bob-special (and org-cycle-global-at-bob (bobp)
  4335. (not (looking-at outline-regexp))))
  4336. (org-cycle-hook
  4337. (if bob-special
  4338. (delq 'org-optimize-window-after-visibility-change
  4339. (copy-sequence org-cycle-hook))
  4340. org-cycle-hook))
  4341. (pos (point)))
  4342. (if (or bob-special (equal arg '(4)))
  4343. ;; special case: use global cycling
  4344. (setq arg t))
  4345. (cond
  4346. ((equal arg '(16))
  4347. (org-set-startup-visibility)
  4348. (message "Startup visibility, plus VISIBILITY properties"))
  4349. ((equal arg '(64))
  4350. (show-all)
  4351. (message "Entire buffer visible, including drawers"))
  4352. ((org-at-table-p 'any)
  4353. ;; Enter the table or move to the next field in the table
  4354. (or (org-table-recognize-table.el)
  4355. (progn
  4356. (if arg (org-table-edit-field t)
  4357. (org-table-justify-field-maybe)
  4358. (call-interactively 'org-table-next-field)))))
  4359. ((run-hook-with-args-until-success
  4360. 'org-tab-after-check-for-table-hook))
  4361. ((eq arg t) ;; Global cycling
  4362. (org-cycle-internal-global))
  4363. ((and org-drawers org-drawer-regexp
  4364. (save-excursion
  4365. (beginning-of-line 1)
  4366. (looking-at org-drawer-regexp)))
  4367. ;; Toggle block visibility
  4368. (org-flag-drawer
  4369. (not (get-char-property (match-end 0) 'invisible))))
  4370. ((integerp arg)
  4371. ;; Show-subtree, ARG levels up from here.
  4372. (save-excursion
  4373. (org-back-to-heading)
  4374. (outline-up-heading (if (< arg 0) (- arg)
  4375. (- (funcall outline-level) arg)))
  4376. (org-show-subtree)))
  4377. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4378. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4379. (org-cycle-internal-local))
  4380. ;; TAB emulation and template completion
  4381. (buffer-read-only (org-back-to-heading))
  4382. ((run-hook-with-args-until-success
  4383. 'org-tab-after-check-for-cycling-hook))
  4384. ((org-try-structure-completion))
  4385. ((org-try-cdlatex-tab))
  4386. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4387. (or (not (bolp))
  4388. (not (looking-at outline-regexp))))
  4389. (call-interactively (global-key-binding "\t")))
  4390. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4391. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4392. (or (and (eq org-cycle-emulate-tab 'white)
  4393. (= (match-end 0) (point-at-eol)))
  4394. (and (eq org-cycle-emulate-tab 'whitestart)
  4395. (>= (match-end 0) pos))))
  4396. t
  4397. (eq org-cycle-emulate-tab t))
  4398. (call-interactively (global-key-binding "\t")))
  4399. (t (save-excursion
  4400. (org-back-to-heading)
  4401. (org-cycle)))))))
  4402. (defun org-cycle-internal-global ()
  4403. "Do the global cycling action."
  4404. (cond
  4405. ((and (eq last-command this-command)
  4406. (eq org-cycle-global-status 'overview))
  4407. ;; We just created the overview - now do table of contents
  4408. ;; This can be slow in very large buffers, so indicate action
  4409. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4410. (message "CONTENTS...")
  4411. (org-content)
  4412. (message "CONTENTS...done")
  4413. (setq org-cycle-global-status 'contents)
  4414. (run-hook-with-args 'org-cycle-hook 'contents))
  4415. ((and (eq last-command this-command)
  4416. (eq org-cycle-global-status 'contents))
  4417. ;; We just showed the table of contents - now show everything
  4418. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4419. (show-all)
  4420. (message "SHOW ALL")
  4421. (setq org-cycle-global-status 'all)
  4422. (run-hook-with-args 'org-cycle-hook 'all))
  4423. (t
  4424. ;; Default action: go to overview
  4425. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4426. (org-overview)
  4427. (message "OVERVIEW")
  4428. (setq org-cycle-global-status 'overview)
  4429. (run-hook-with-args 'org-cycle-hook 'overview))))
  4430. (defun org-cycle-internal-local ()
  4431. "Do the local cycling action."
  4432. (org-back-to-heading)
  4433. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4434. ;; First, some boundaries
  4435. (save-excursion
  4436. (org-back-to-heading)
  4437. (setq level (funcall outline-level))
  4438. (save-excursion
  4439. (beginning-of-line 2)
  4440. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4441. ; XEmacs does not have `next-single-char-property-change'
  4442. ; I'm not sure about Emacs 21.
  4443. (while (and (not (eobp)) ;; this is like `next-line'
  4444. (get-char-property (1- (point)) 'invisible))
  4445. (beginning-of-line 2))
  4446. (while (and (not (eobp)) ;; this is like `next-line'
  4447. (get-char-property (1- (point)) 'invisible))
  4448. (goto-char (next-single-char-property-change (point) 'invisible))
  4449. ;;;??? (or (bolp) (beginning-of-line 2))))
  4450. (and (eolp) (beginning-of-line 2))))
  4451. (setq eol (point)))
  4452. (outline-end-of-heading) (setq eoh (point))
  4453. (save-excursion
  4454. (outline-next-heading)
  4455. (setq has-children (and (org-at-heading-p t)
  4456. (> (funcall outline-level) level))))
  4457. (org-end-of-subtree t)
  4458. (unless (eobp)
  4459. (skip-chars-forward " \t\n")
  4460. (beginning-of-line 1) ; in case this is an item
  4461. )
  4462. (setq eos (if (eobp) (point) (1- (point)))))
  4463. ;; Find out what to do next and set `this-command'
  4464. (cond
  4465. ((= eos eoh)
  4466. ;; Nothing is hidden behind this heading
  4467. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4468. (message "EMPTY ENTRY")
  4469. (setq org-cycle-subtree-status nil)
  4470. (save-excursion
  4471. (goto-char eos)
  4472. (outline-next-heading)
  4473. (if (org-invisible-p) (org-flag-heading nil))))
  4474. ((and (or (>= eol eos)
  4475. (not (string-match "\\S-" (buffer-substring eol eos))))
  4476. (or has-children
  4477. (not (setq children-skipped
  4478. org-cycle-skip-children-state-if-no-children))))
  4479. ;; Entire subtree is hidden in one line: children view
  4480. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4481. (org-show-entry)
  4482. (show-children)
  4483. (message "CHILDREN")
  4484. (save-excursion
  4485. (goto-char eos)
  4486. (outline-next-heading)
  4487. (if (org-invisible-p) (org-flag-heading nil)))
  4488. (setq org-cycle-subtree-status 'children)
  4489. (run-hook-with-args 'org-cycle-hook 'children))
  4490. ((or children-skipped
  4491. (and (eq last-command this-command)
  4492. (eq org-cycle-subtree-status 'children)))
  4493. ;; We just showed the children, or no children are there,
  4494. ;; now show everything.
  4495. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4496. (org-show-subtree)
  4497. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4498. (setq org-cycle-subtree-status 'subtree)
  4499. (run-hook-with-args 'org-cycle-hook 'subtree))
  4500. (t
  4501. ;; Default action: hide the subtree.
  4502. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4503. (hide-subtree)
  4504. (message "FOLDED")
  4505. (setq org-cycle-subtree-status 'folded)
  4506. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4507. ;;;###autoload
  4508. (defun org-global-cycle (&optional arg)
  4509. "Cycle the global visibility. For details see `org-cycle'.
  4510. With C-u prefix arg, switch to startup visibility.
  4511. With a numeric prefix, show all headlines up to that level."
  4512. (interactive "P")
  4513. (let ((org-cycle-include-plain-lists
  4514. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4515. (cond
  4516. ((integerp arg)
  4517. (show-all)
  4518. (hide-sublevels arg)
  4519. (setq org-cycle-global-status 'contents))
  4520. ((equal arg '(4))
  4521. (org-set-startup-visibility)
  4522. (message "Startup visibility, plus VISIBILITY properties."))
  4523. (t
  4524. (org-cycle '(4))))))
  4525. (defun org-set-startup-visibility ()
  4526. "Set the visibility required by startup options and properties."
  4527. (cond
  4528. ((eq org-startup-folded t)
  4529. (org-cycle '(4)))
  4530. ((eq org-startup-folded 'content)
  4531. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4532. (org-cycle '(4)) (org-cycle '(4)))))
  4533. (if org-hide-block-startup (org-hide-block-all))
  4534. (org-set-visibility-according-to-property 'no-cleanup)
  4535. (org-cycle-hide-archived-subtrees 'all)
  4536. (org-cycle-hide-drawers 'all)
  4537. (org-cycle-show-empty-lines 'all))
  4538. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4539. "Switch subtree visibilities according to :VISIBILITY: property."
  4540. (interactive)
  4541. (let (org-show-entry-below state)
  4542. (save-excursion
  4543. (goto-char (point-min))
  4544. (while (re-search-forward
  4545. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4546. nil t)
  4547. (setq state (match-string 1))
  4548. (save-excursion
  4549. (org-back-to-heading t)
  4550. (hide-subtree)
  4551. (org-reveal)
  4552. (cond
  4553. ((equal state '("fold" "folded"))
  4554. (hide-subtree))
  4555. ((equal state "children")
  4556. (org-show-hidden-entry)
  4557. (show-children))
  4558. ((equal state "content")
  4559. (save-excursion
  4560. (save-restriction
  4561. (org-narrow-to-subtree)
  4562. (org-content))))
  4563. ((member state '("all" "showall"))
  4564. (show-subtree)))))
  4565. (unless no-cleanup
  4566. (org-cycle-hide-archived-subtrees 'all)
  4567. (org-cycle-hide-drawers 'all)
  4568. (org-cycle-show-empty-lines 'all)))))
  4569. (defun org-overview ()
  4570. "Switch to overview mode, showing only top-level headlines.
  4571. Really, this shows all headlines with level equal or greater than the level
  4572. of the first headline in the buffer. This is important, because if the
  4573. first headline is not level one, then (hide-sublevels 1) gives confusing
  4574. results."
  4575. (interactive)
  4576. (let ((level (save-excursion
  4577. (goto-char (point-min))
  4578. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4579. (progn
  4580. (goto-char (match-beginning 0))
  4581. (funcall outline-level))))))
  4582. (and level (hide-sublevels level))))
  4583. (defun org-content (&optional arg)
  4584. "Show all headlines in the buffer, like a table of contents.
  4585. With numerical argument N, show content up to level N."
  4586. (interactive "P")
  4587. (save-excursion
  4588. ;; Visit all headings and show their offspring
  4589. (and (integerp arg) (org-overview))
  4590. (goto-char (point-max))
  4591. (catch 'exit
  4592. (while (and (progn (condition-case nil
  4593. (outline-previous-visible-heading 1)
  4594. (error (goto-char (point-min))))
  4595. t)
  4596. (looking-at outline-regexp))
  4597. (if (integerp arg)
  4598. (show-children (1- arg))
  4599. (show-branches))
  4600. (if (bobp) (throw 'exit nil))))))
  4601. (defun org-optimize-window-after-visibility-change (state)
  4602. "Adjust the window after a change in outline visibility.
  4603. This function is the default value of the hook `org-cycle-hook'."
  4604. (when (get-buffer-window (current-buffer))
  4605. (cond
  4606. ((eq state 'content) nil)
  4607. ((eq state 'all) nil)
  4608. ((eq state 'folded) nil)
  4609. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4610. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4611. ;; FIXME: no longer in use
  4612. (defun org-compact-display-after-subtree-move ()
  4613. "Show a compacter version of the tree of the entry's parent."
  4614. (save-excursion
  4615. (if (org-up-heading-safe)
  4616. (progn
  4617. (hide-subtree)
  4618. (show-entry)
  4619. (show-children)
  4620. (org-cycle-show-empty-lines 'children)
  4621. (org-cycle-hide-drawers 'children))
  4622. (org-overview))))
  4623. (defun org-remove-empty-overlays-at (pos)
  4624. "Remove outline overlays that do not contain non-white stuff."
  4625. (mapc
  4626. (lambda (o)
  4627. (and (eq 'outline (org-overlay-get o 'invisible))
  4628. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4629. (org-overlay-end o))))
  4630. (org-delete-overlay o)))
  4631. (org-overlays-at pos)))
  4632. (defun org-clean-visibility-after-subtree-move ()
  4633. "Fix visibility issues after moving a subtree."
  4634. ;; First, find a reasonable region to look at:
  4635. ;; Start two siblings above, end three below
  4636. (let* ((beg (save-excursion
  4637. (and (org-get-last-sibling)
  4638. (org-get-last-sibling))
  4639. (point)))
  4640. (end (save-excursion
  4641. (and (org-get-next-sibling)
  4642. (org-get-next-sibling)
  4643. (org-get-next-sibling))
  4644. (if (org-at-heading-p)
  4645. (point-at-eol)
  4646. (point))))
  4647. (level (looking-at "\\*+"))
  4648. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4649. (save-excursion
  4650. (save-restriction
  4651. (narrow-to-region beg end)
  4652. (when re
  4653. ;; Properly fold already folded siblings
  4654. (goto-char (point-min))
  4655. (while (re-search-forward re nil t)
  4656. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4657. (hide-entry))))
  4658. (org-cycle-show-empty-lines 'overview)
  4659. (org-cycle-hide-drawers 'overview)))))
  4660. (defun org-cycle-show-empty-lines (state)
  4661. "Show empty lines above all visible headlines.
  4662. The region to be covered depends on STATE when called through
  4663. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4664. entire buffer covered. Note that an empty line is only shown if there
  4665. are at least `org-cycle-separator-lines' empty lines before the headline."
  4666. (when (not (= org-cycle-separator-lines 0))
  4667. (save-excursion
  4668. (let* ((n (abs org-cycle-separator-lines))
  4669. (re (cond
  4670. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4671. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4672. (t (let ((ns (number-to-string (- n 2))))
  4673. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4674. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4675. beg end b e)
  4676. (cond
  4677. ((memq state '(overview contents t))
  4678. (setq beg (point-min) end (point-max)))
  4679. ((memq state '(children folded))
  4680. (setq beg (point) end (progn (org-end-of-subtree t t)
  4681. (beginning-of-line 2)
  4682. (point)))))
  4683. (when beg
  4684. (goto-char beg)
  4685. (while (re-search-forward re end t)
  4686. (unless (get-char-property (match-end 1) 'invisible)
  4687. (setq e (match-end 1))
  4688. (if (< org-cycle-separator-lines 0)
  4689. (setq b (save-excursion
  4690. (goto-char (match-beginning 0))
  4691. (org-back-over-empty-lines)
  4692. (point)))
  4693. (setq b (match-beginning 1)))
  4694. (outline-flag-region b e nil)))))))
  4695. ;; Never hide empty lines at the end of the file.
  4696. (save-excursion
  4697. (goto-char (point-max))
  4698. (outline-previous-heading)
  4699. (outline-end-of-heading)
  4700. (if (and (looking-at "[ \t\n]+")
  4701. (= (match-end 0) (point-max)))
  4702. (outline-flag-region (point) (match-end 0) nil))))
  4703. (defun org-show-empty-lines-in-parent ()
  4704. "Move to the parent and re-show empty lines before visible headlines."
  4705. (save-excursion
  4706. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4707. (org-cycle-show-empty-lines context))))
  4708. (defun org-cycle-hide-drawers (state)
  4709. "Re-hide all drawers after a visibility state change."
  4710. (when (and (org-mode-p)
  4711. (not (memq state '(overview folded contents))))
  4712. (save-excursion
  4713. (let* ((globalp (memq state '(contents all)))
  4714. (beg (if globalp (point-min) (point)))
  4715. (end (if globalp (point-max)
  4716. (if (eq state 'children)
  4717. (save-excursion (outline-next-heading) (point))
  4718. (org-end-of-subtree t)))))
  4719. (goto-char beg)
  4720. (while (re-search-forward org-drawer-regexp end t)
  4721. (org-flag-drawer t))))))
  4722. (defun org-flag-drawer (flag)
  4723. (save-excursion
  4724. (beginning-of-line 1)
  4725. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4726. (let ((b (match-end 0))
  4727. (outline-regexp org-outline-regexp))
  4728. (if (re-search-forward
  4729. "^[ \t]*:END:"
  4730. (save-excursion (outline-next-heading) (point)) t)
  4731. (outline-flag-region b (point-at-eol) flag)
  4732. (error ":END: line missing at position %s" b))))))
  4733. (defun org-subtree-end-visible-p ()
  4734. "Is the end of the current subtree visible?"
  4735. (pos-visible-in-window-p
  4736. (save-excursion (org-end-of-subtree t) (point))))
  4737. (defun org-first-headline-recenter (&optional N)
  4738. "Move cursor to the first headline and recenter the headline.
  4739. Optional argument N means, put the headline into the Nth line of the window."
  4740. (goto-char (point-min))
  4741. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4742. (beginning-of-line)
  4743. (recenter (prefix-numeric-value N))))
  4744. ;;; Folding of blocks
  4745. (defconst org-block-regexp
  4746. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4747. "Regular expression for hiding blocks.")
  4748. (defvar org-hide-block-overlays nil
  4749. "Overays hiding blocks.")
  4750. (make-variable-buffer-local 'org-hide-block-overlays)
  4751. (defun org-block-map (function &optional start end)
  4752. "Call func at the head of all source blocks in the current
  4753. buffer. Optional arguments START and END can be used to limit
  4754. the range."
  4755. (let ((start (or start (point-min)))
  4756. (end (or end (point-max))))
  4757. (save-excursion
  4758. (goto-char start)
  4759. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4760. (save-excursion
  4761. (save-match-data
  4762. (goto-char (match-beginning 0))
  4763. (funcall function)))))))
  4764. (defun org-hide-block-toggle-all ()
  4765. "Toggle the visibility of all blocks in the current buffer."
  4766. (org-block-map #'org-hide-block-toggle))
  4767. (defun org-hide-block-all ()
  4768. "Fold all blocks in the current buffer."
  4769. (interactive)
  4770. (org-show-block-all)
  4771. (org-block-map #'org-hide-block-toggle-maybe))
  4772. (defun org-show-block-all ()
  4773. "Unfold all blocks in the current buffer."
  4774. (mapc 'org-delete-overlay org-hide-block-overlays)
  4775. (setq org-hide-block-overlays nil))
  4776. (defun org-hide-block-toggle-maybe ()
  4777. "Toggle visibility of block at point."
  4778. (interactive)
  4779. (let ((case-fold-search t))
  4780. (if (save-excursion
  4781. (beginning-of-line 1)
  4782. (looking-at org-block-regexp))
  4783. (progn (org-hide-block-toggle)
  4784. t) ;; to signal that we took action
  4785. nil))) ;; to signal that we did not
  4786. (defun org-hide-block-toggle (&optional force)
  4787. "Toggle the visibility of the current block."
  4788. (interactive)
  4789. (save-excursion
  4790. (beginning-of-line)
  4791. (if (re-search-forward org-block-regexp nil t)
  4792. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4793. (end (match-end 0)) ;; end of entire body
  4794. ov)
  4795. (if (memq t (mapcar (lambda (overlay)
  4796. (eq (org-overlay-get overlay 'invisible)
  4797. 'org-hide-block))
  4798. (org-overlays-at start)))
  4799. (if (or (not force) (eq force 'off))
  4800. (mapc (lambda (ov)
  4801. (when (member ov org-hide-block-overlays)
  4802. (setq org-hide-block-overlays
  4803. (delq ov org-hide-block-overlays)))
  4804. (when (eq (org-overlay-get ov 'invisible)
  4805. 'org-hide-block)
  4806. (org-delete-overlay ov)))
  4807. (org-overlays-at start)))
  4808. (setq ov (org-make-overlay start end))
  4809. (org-overlay-put ov 'invisible 'org-hide-block)
  4810. ;; make the block accessible to isearch
  4811. (org-overlay-put
  4812. ov 'isearch-open-invisible
  4813. (lambda (ov)
  4814. (when (member ov org-hide-block-overlays)
  4815. (setq org-hide-block-overlays
  4816. (delq ov org-hide-block-overlays)))
  4817. (when (eq (org-overlay-get ov 'invisible)
  4818. 'org-hide-block)
  4819. (org-delete-overlay ov))))
  4820. (push ov org-hide-block-overlays)))
  4821. (error "Not looking at a source block"))))
  4822. ;; org-tab-after-check-for-cycling-hook
  4823. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4824. ;; Remove overlays when changing major mode
  4825. (add-hook 'org-mode-hook
  4826. (lambda () (org-add-hook 'change-major-mode-hook
  4827. 'org-show-block-all 'append 'local)))
  4828. ;;; Org-goto
  4829. (defvar org-goto-window-configuration nil)
  4830. (defvar org-goto-marker nil)
  4831. (defvar org-goto-map
  4832. (let ((map (make-sparse-keymap)))
  4833. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4834. (while (setq cmd (pop cmds))
  4835. (substitute-key-definition cmd cmd map global-map)))
  4836. (suppress-keymap map)
  4837. (org-defkey map "\C-m" 'org-goto-ret)
  4838. (org-defkey map [(return)] 'org-goto-ret)
  4839. (org-defkey map [(left)] 'org-goto-left)
  4840. (org-defkey map [(right)] 'org-goto-right)
  4841. (org-defkey map [(control ?g)] 'org-goto-quit)
  4842. (org-defkey map "\C-i" 'org-cycle)
  4843. (org-defkey map [(tab)] 'org-cycle)
  4844. (org-defkey map [(down)] 'outline-next-visible-heading)
  4845. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4846. (if org-goto-auto-isearch
  4847. (if (fboundp 'define-key-after)
  4848. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4849. nil)
  4850. (org-defkey map "q" 'org-goto-quit)
  4851. (org-defkey map "n" 'outline-next-visible-heading)
  4852. (org-defkey map "p" 'outline-previous-visible-heading)
  4853. (org-defkey map "f" 'outline-forward-same-level)
  4854. (org-defkey map "b" 'outline-backward-same-level)
  4855. (org-defkey map "u" 'outline-up-heading))
  4856. (org-defkey map "/" 'org-occur)
  4857. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4858. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4859. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4860. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4861. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4862. map))
  4863. (defconst org-goto-help
  4864. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4865. RET=jump to location [Q]uit and return to previous location
  4866. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4867. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4868. ;; FIXME: Docstring doe not mention both interfaces
  4869. (defun org-goto (&optional alternative-interface)
  4870. "Look up a different location in the current file, keeping current visibility.
  4871. When you want look-up or go to a different location in a document, the
  4872. fastest way is often to fold the entire buffer and then dive into the tree.
  4873. This method has the disadvantage, that the previous location will be folded,
  4874. which may not be what you want.
  4875. This command works around this by showing a copy of the current buffer
  4876. in an indirect buffer, in overview mode. You can dive into the tree in
  4877. that copy, use org-occur and incremental search to find a location.
  4878. When pressing RET or `Q', the command returns to the original buffer in
  4879. which the visibility is still unchanged. After RET is will also jump to
  4880. the location selected in the indirect buffer and expose the
  4881. the headline hierarchy above."
  4882. (interactive "P")
  4883. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4884. (org-refile-use-outline-path t)
  4885. (org-refile-target-verify-function nil)
  4886. (interface
  4887. (if (not alternative-interface)
  4888. org-goto-interface
  4889. (if (eq org-goto-interface 'outline)
  4890. 'outline-path-completion
  4891. 'outline)))
  4892. (org-goto-start-pos (point))
  4893. (selected-point
  4894. (if (eq interface 'outline)
  4895. (car (org-get-location (current-buffer) org-goto-help))
  4896. (nth 3 (org-refile-get-location "Goto: ")))))
  4897. (if selected-point
  4898. (progn
  4899. (org-mark-ring-push org-goto-start-pos)
  4900. (goto-char selected-point)
  4901. (if (or (org-invisible-p) (org-invisible-p2))
  4902. (org-show-context 'org-goto)))
  4903. (message "Quit"))))
  4904. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4905. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4906. (defvar org-goto-local-auto-isearch-map) ; defined below
  4907. (defun org-get-location (buf help)
  4908. "Let the user select a location in the Org-mode buffer BUF.
  4909. This function uses a recursive edit. It returns the selected position
  4910. or nil."
  4911. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4912. (isearch-hide-immediately nil)
  4913. (isearch-search-fun-function
  4914. (lambda () 'org-goto-local-search-headings))
  4915. (org-goto-selected-point org-goto-exit-command))
  4916. (save-excursion
  4917. (save-window-excursion
  4918. (delete-other-windows)
  4919. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4920. (switch-to-buffer
  4921. (condition-case nil
  4922. (make-indirect-buffer (current-buffer) "*org-goto*")
  4923. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4924. (with-output-to-temp-buffer "*Help*"
  4925. (princ help))
  4926. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4927. (setq buffer-read-only nil)
  4928. (let ((org-startup-truncated t)
  4929. (org-startup-folded nil)
  4930. (org-startup-align-all-tables nil))
  4931. (org-mode)
  4932. (org-overview))
  4933. (setq buffer-read-only t)
  4934. (if (and (boundp 'org-goto-start-pos)
  4935. (integer-or-marker-p org-goto-start-pos))
  4936. (let ((org-show-hierarchy-above t)
  4937. (org-show-siblings t)
  4938. (org-show-following-heading t))
  4939. (goto-char org-goto-start-pos)
  4940. (and (org-invisible-p) (org-show-context)))
  4941. (goto-char (point-min)))
  4942. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4943. (message "Select location and press RET")
  4944. (use-local-map org-goto-map)
  4945. (recursive-edit)
  4946. ))
  4947. (kill-buffer "*org-goto*")
  4948. (cons org-goto-selected-point org-goto-exit-command)))
  4949. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4950. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4951. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4952. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4953. (defun org-goto-local-search-headings (string bound noerror)
  4954. "Search and make sure that any matches are in headlines."
  4955. (catch 'return
  4956. (while (if isearch-forward
  4957. (search-forward string bound noerror)
  4958. (search-backward string bound noerror))
  4959. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4960. (and (member :headline context)
  4961. (not (member :tags context))))
  4962. (throw 'return (point))))))
  4963. (defun org-goto-local-auto-isearch ()
  4964. "Start isearch."
  4965. (interactive)
  4966. (goto-char (point-min))
  4967. (let ((keys (this-command-keys)))
  4968. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4969. (isearch-mode t)
  4970. (isearch-process-search-char (string-to-char keys)))))
  4971. (defun org-goto-ret (&optional arg)
  4972. "Finish `org-goto' by going to the new location."
  4973. (interactive "P")
  4974. (setq org-goto-selected-point (point)
  4975. org-goto-exit-command 'return)
  4976. (throw 'exit nil))
  4977. (defun org-goto-left ()
  4978. "Finish `org-goto' by going to the new location."
  4979. (interactive)
  4980. (if (org-on-heading-p)
  4981. (progn
  4982. (beginning-of-line 1)
  4983. (setq org-goto-selected-point (point)
  4984. org-goto-exit-command 'left)
  4985. (throw 'exit nil))
  4986. (error "Not on a heading")))
  4987. (defun org-goto-right ()
  4988. "Finish `org-goto' by going to the new location."
  4989. (interactive)
  4990. (if (org-on-heading-p)
  4991. (progn
  4992. (setq org-goto-selected-point (point)
  4993. org-goto-exit-command 'right)
  4994. (throw 'exit nil))
  4995. (error "Not on a heading")))
  4996. (defun org-goto-quit ()
  4997. "Finish `org-goto' without cursor motion."
  4998. (interactive)
  4999. (setq org-goto-selected-point nil)
  5000. (setq org-goto-exit-command 'quit)
  5001. (throw 'exit nil))
  5002. ;;; Indirect buffer display of subtrees
  5003. (defvar org-indirect-dedicated-frame nil
  5004. "This is the frame being used for indirect tree display.")
  5005. (defvar org-last-indirect-buffer nil)
  5006. (defun org-tree-to-indirect-buffer (&optional arg)
  5007. "Create indirect buffer and narrow it to current subtree.
  5008. With numerical prefix ARG, go up to this level and then take that tree.
  5009. If ARG is negative, go up that many levels.
  5010. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5011. indirect buffer previously made with this command, to avoid proliferation of
  5012. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5013. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5014. is kept so that you can work with several indirect buffers at the same time.
  5015. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5016. requests that a new frame be made for the new buffer, so that the dedicated
  5017. frame is not changed."
  5018. (interactive "P")
  5019. (let ((cbuf (current-buffer))
  5020. (cwin (selected-window))
  5021. (pos (point))
  5022. beg end level heading ibuf)
  5023. (save-excursion
  5024. (org-back-to-heading t)
  5025. (when (numberp arg)
  5026. (setq level (org-outline-level))
  5027. (if (< arg 0) (setq arg (+ level arg)))
  5028. (while (> (setq level (org-outline-level)) arg)
  5029. (outline-up-heading 1 t)))
  5030. (setq beg (point)
  5031. heading (org-get-heading))
  5032. (org-end-of-subtree t) (setq end (point)))
  5033. (if (and (buffer-live-p org-last-indirect-buffer)
  5034. (not (eq org-indirect-buffer-display 'new-frame))
  5035. (not arg))
  5036. (kill-buffer org-last-indirect-buffer))
  5037. (setq ibuf (org-get-indirect-buffer cbuf)
  5038. org-last-indirect-buffer ibuf)
  5039. (cond
  5040. ((or (eq org-indirect-buffer-display 'new-frame)
  5041. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5042. (select-frame (make-frame))
  5043. (delete-other-windows)
  5044. (switch-to-buffer ibuf)
  5045. (org-set-frame-title heading))
  5046. ((eq org-indirect-buffer-display 'dedicated-frame)
  5047. (raise-frame
  5048. (select-frame (or (and org-indirect-dedicated-frame
  5049. (frame-live-p org-indirect-dedicated-frame)
  5050. org-indirect-dedicated-frame)
  5051. (setq org-indirect-dedicated-frame (make-frame)))))
  5052. (delete-other-windows)
  5053. (switch-to-buffer ibuf)
  5054. (org-set-frame-title (concat "Indirect: " heading)))
  5055. ((eq org-indirect-buffer-display 'current-window)
  5056. (switch-to-buffer ibuf))
  5057. ((eq org-indirect-buffer-display 'other-window)
  5058. (pop-to-buffer ibuf))
  5059. (t (error "Invalid value.")))
  5060. (if (featurep 'xemacs)
  5061. (save-excursion (org-mode) (turn-on-font-lock)))
  5062. (narrow-to-region beg end)
  5063. (show-all)
  5064. (goto-char pos)
  5065. (and (window-live-p cwin) (select-window cwin))))
  5066. (defun org-get-indirect-buffer (&optional buffer)
  5067. (setq buffer (or buffer (current-buffer)))
  5068. (let ((n 1) (base (buffer-name buffer)) bname)
  5069. (while (buffer-live-p
  5070. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5071. (setq n (1+ n)))
  5072. (condition-case nil
  5073. (make-indirect-buffer buffer bname 'clone)
  5074. (error (make-indirect-buffer buffer bname)))))
  5075. (defun org-set-frame-title (title)
  5076. "Set the title of the current frame to the string TITLE."
  5077. ;; FIXME: how to name a single frame in XEmacs???
  5078. (unless (featurep 'xemacs)
  5079. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5080. ;;;; Structure editing
  5081. ;;; Inserting headlines
  5082. (defun org-previous-line-empty-p ()
  5083. (save-excursion
  5084. (and (not (bobp))
  5085. (or (beginning-of-line 0) t)
  5086. (save-match-data
  5087. (looking-at "[ \t]*$")))))
  5088. (defun org-insert-heading (&optional force-heading)
  5089. "Insert a new heading or item with same depth at point.
  5090. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5091. If point is at the beginning of a headline, insert a sibling before the
  5092. current headline. If point is not at the beginning, do not split the line,
  5093. but create the new headline after the current line."
  5094. (interactive "P")
  5095. (if (= (buffer-size) 0)
  5096. (insert "\n* ")
  5097. (when (or force-heading (not (org-insert-item)))
  5098. (let* ((empty-line-p nil)
  5099. (head (save-excursion
  5100. (condition-case nil
  5101. (progn
  5102. (org-back-to-heading)
  5103. (setq empty-line-p (org-previous-line-empty-p))
  5104. (match-string 0))
  5105. (error "*"))))
  5106. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5107. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5108. pos hide-previous previous-pos)
  5109. (cond
  5110. ((and (org-on-heading-p) (bolp)
  5111. (or (bobp)
  5112. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5113. ;; insert before the current line
  5114. (open-line (if blank 2 1)))
  5115. ((and (bolp)
  5116. (not org-insert-heading-respect-content)
  5117. (or (bobp)
  5118. (save-excursion
  5119. (backward-char 1) (not (org-invisible-p)))))
  5120. ;; insert right here
  5121. nil)
  5122. (t
  5123. ;; somewhere in the line
  5124. (save-excursion
  5125. (setq previous-pos (point-at-bol))
  5126. (end-of-line)
  5127. (setq hide-previous (org-invisible-p)))
  5128. (and org-insert-heading-respect-content (org-show-subtree))
  5129. (let ((split
  5130. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5131. (save-excursion
  5132. (let ((p (point)))
  5133. (goto-char (point-at-bol))
  5134. (and (looking-at org-complex-heading-regexp)
  5135. (> p (match-beginning 4)))))))
  5136. tags pos)
  5137. (cond
  5138. (org-insert-heading-respect-content
  5139. (org-end-of-subtree nil t)
  5140. (or (bolp) (newline))
  5141. (or (org-previous-line-empty-p)
  5142. (and blank (newline)))
  5143. (open-line 1))
  5144. ((org-on-heading-p)
  5145. (when hide-previous
  5146. (show-children)
  5147. (org-show-entry))
  5148. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5149. (setq tags (and (match-end 2) (match-string 2)))
  5150. (and (match-end 1)
  5151. (delete-region (match-beginning 1) (match-end 1)))
  5152. (setq pos (point-at-bol))
  5153. (or split (end-of-line 1))
  5154. (delete-horizontal-space)
  5155. (newline (if blank 2 1))
  5156. (when tags
  5157. (save-excursion
  5158. (goto-char pos)
  5159. (end-of-line 1)
  5160. (insert " " tags)
  5161. (org-set-tags nil 'align))))
  5162. (t
  5163. (or split (end-of-line 1))
  5164. (newline (if blank 2 1)))))))
  5165. (insert head) (just-one-space)
  5166. (setq pos (point))
  5167. (end-of-line 1)
  5168. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5169. (when (and org-insert-heading-respect-content hide-previous)
  5170. (save-excursion
  5171. (goto-char previous-pos)
  5172. (hide-subtree)))
  5173. (run-hooks 'org-insert-heading-hook)))))
  5174. (defun org-get-heading (&optional no-tags)
  5175. "Return the heading of the current entry, without the stars."
  5176. (save-excursion
  5177. (org-back-to-heading t)
  5178. (if (looking-at
  5179. (if no-tags
  5180. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5181. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5182. (match-string 1) "")))
  5183. (defun org-heading-components ()
  5184. "Return the components of the current heading.
  5185. This is a list with the following elements:
  5186. - the level as an integer
  5187. - the reduced level, different if `org-odd-levels-only' is set.
  5188. - the TODO keyword, or nil
  5189. - the priority character, like ?A, or nil if no priority is given
  5190. - the headline text itself, or the tags string if no headline text
  5191. - the tags string, or nil."
  5192. (save-excursion
  5193. (org-back-to-heading t)
  5194. (if (looking-at org-complex-heading-regexp)
  5195. (list (length (match-string 1))
  5196. (org-reduced-level (length (match-string 1)))
  5197. (org-match-string-no-properties 2)
  5198. (and (match-end 3) (aref (match-string 3) 2))
  5199. (org-match-string-no-properties 4)
  5200. (org-match-string-no-properties 5)))))
  5201. (defun org-get-entry ()
  5202. "Get the entry text, after heading, entire subtree."
  5203. (save-excursion
  5204. (org-back-to-heading t)
  5205. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5206. (defun org-insert-heading-after-current ()
  5207. "Insert a new heading with same level as current, after current subtree."
  5208. (interactive)
  5209. (org-back-to-heading)
  5210. (org-insert-heading)
  5211. (org-move-subtree-down)
  5212. (end-of-line 1))
  5213. (defun org-insert-heading-respect-content ()
  5214. (interactive)
  5215. (let ((org-insert-heading-respect-content t))
  5216. (org-insert-heading t)))
  5217. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5218. (interactive "P")
  5219. (let ((org-insert-heading-respect-content t))
  5220. (org-insert-todo-heading force-state t)))
  5221. (defun org-insert-todo-heading (arg &optional force-heading)
  5222. "Insert a new heading with the same level and TODO state as current heading.
  5223. If the heading has no TODO state, or if the state is DONE, use the first
  5224. state (TODO by default). Also with prefix arg, force first state."
  5225. (interactive "P")
  5226. (when (or force-heading (not (org-insert-item 'checkbox)))
  5227. (org-insert-heading force-heading)
  5228. (save-excursion
  5229. (org-back-to-heading)
  5230. (outline-previous-heading)
  5231. (looking-at org-todo-line-regexp))
  5232. (let*
  5233. ((new-mark-x
  5234. (if (or arg
  5235. (not (match-beginning 2))
  5236. (member (match-string 2) org-done-keywords))
  5237. (car org-todo-keywords-1)
  5238. (match-string 2)))
  5239. (new-mark
  5240. (or
  5241. (run-hook-with-args-until-success
  5242. 'org-todo-get-default-hook new-mark-x nil)
  5243. new-mark-x)))
  5244. (beginning-of-line 1)
  5245. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5246. (if org-treat-insert-todo-heading-as-state-change
  5247. (org-todo new-mark)
  5248. (insert new-mark " "))))
  5249. (when org-provide-todo-statistics
  5250. (org-update-parent-todo-statistics))))
  5251. (defun org-insert-subheading (arg)
  5252. "Insert a new subheading and demote it.
  5253. Works for outline headings and for plain lists alike."
  5254. (interactive "P")
  5255. (org-insert-heading arg)
  5256. (cond
  5257. ((org-on-heading-p) (org-do-demote))
  5258. ((org-at-item-p) (org-indent-item 1))))
  5259. (defun org-insert-todo-subheading (arg)
  5260. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5261. Works for outline headings and for plain lists alike."
  5262. (interactive "P")
  5263. (org-insert-todo-heading arg)
  5264. (cond
  5265. ((org-on-heading-p) (org-do-demote))
  5266. ((org-at-item-p) (org-indent-item 1))))
  5267. ;;; Promotion and Demotion
  5268. (defvar org-after-demote-entry-hook nil
  5269. "Hook run after an entry has been demoted.
  5270. The cursor will be at the beginning of the entry.
  5271. When a subtree is being demoted, the hook will be called for each node.")
  5272. (defvar org-after-promote-entry-hook nil
  5273. "Hook run after an entry has been promoted.
  5274. The cursor will be at the beginning of the entry.
  5275. When a subtree is being promoted, the hook will be called for each node.")
  5276. (defun org-promote-subtree ()
  5277. "Promote the entire subtree.
  5278. See also `org-promote'."
  5279. (interactive)
  5280. (save-excursion
  5281. (org-map-tree 'org-promote))
  5282. (org-fix-position-after-promote))
  5283. (defun org-demote-subtree ()
  5284. "Demote the entire subtree. See `org-demote'.
  5285. See also `org-promote'."
  5286. (interactive)
  5287. (save-excursion
  5288. (org-map-tree 'org-demote))
  5289. (org-fix-position-after-promote))
  5290. (defun org-do-promote ()
  5291. "Promote the current heading higher up the tree.
  5292. If the region is active in `transient-mark-mode', promote all headings
  5293. in the region."
  5294. (interactive)
  5295. (save-excursion
  5296. (if (org-region-active-p)
  5297. (org-map-region 'org-promote (region-beginning) (region-end))
  5298. (org-promote)))
  5299. (org-fix-position-after-promote))
  5300. (defun org-do-demote ()
  5301. "Demote the current heading lower down the tree.
  5302. If the region is active in `transient-mark-mode', demote all headings
  5303. in the region."
  5304. (interactive)
  5305. (save-excursion
  5306. (if (org-region-active-p)
  5307. (org-map-region 'org-demote (region-beginning) (region-end))
  5308. (org-demote)))
  5309. (org-fix-position-after-promote))
  5310. (defun org-fix-position-after-promote ()
  5311. "Make sure that after pro/demotion cursor position is right."
  5312. (let ((pos (point)))
  5313. (when (save-excursion
  5314. (beginning-of-line 1)
  5315. (looking-at org-todo-line-regexp)
  5316. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5317. (cond ((eobp) (insert " "))
  5318. ((eolp) (insert " "))
  5319. ((equal (char-after) ?\ ) (forward-char 1))))))
  5320. (defun org-reduced-level (l)
  5321. "Compute the effective level of a heading.
  5322. This takes into account the setting of `org-odd-levels-only'."
  5323. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5324. (defun org-get-valid-level (level &optional change)
  5325. "Rectify a level change under the influence of `org-odd-levels-only'
  5326. LEVEL is a current level, CHANGE is by how much the level should be
  5327. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5328. even level numbers will become the next higher odd number."
  5329. (if org-odd-levels-only
  5330. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5331. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5332. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5333. (max 1 (+ level (or change 0)))))
  5334. (if (boundp 'define-obsolete-function-alias)
  5335. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5336. (define-obsolete-function-alias 'org-get-legal-level
  5337. 'org-get-valid-level)
  5338. (define-obsolete-function-alias 'org-get-legal-level
  5339. 'org-get-valid-level "23.1")))
  5340. (defun org-promote ()
  5341. "Promote the current heading higher up the tree.
  5342. If the region is active in `transient-mark-mode', promote all headings
  5343. in the region."
  5344. (org-back-to-heading t)
  5345. (let* ((level (save-match-data (funcall outline-level)))
  5346. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5347. (diff (abs (- level (length up-head) -1))))
  5348. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5349. (replace-match up-head nil t)
  5350. ;; Fixup tag positioning
  5351. (and org-auto-align-tags (org-set-tags nil t))
  5352. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5353. (run-hooks 'org-after-promote-entry-hook)))
  5354. (defun org-demote ()
  5355. "Demote the current heading lower down the tree.
  5356. If the region is active in `transient-mark-mode', demote all headings
  5357. in the region."
  5358. (org-back-to-heading t)
  5359. (let* ((level (save-match-data (funcall outline-level)))
  5360. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5361. (diff (abs (- level (length down-head) -1))))
  5362. (replace-match down-head nil t)
  5363. ;; Fixup tag positioning
  5364. (and org-auto-align-tags (org-set-tags nil t))
  5365. (if org-adapt-indentation (org-fixup-indentation diff))
  5366. (run-hooks 'org-after-demote-entry-hook)))
  5367. (defun org-map-tree (fun)
  5368. "Call FUN for every heading underneath the current one."
  5369. (org-back-to-heading)
  5370. (let ((level (funcall outline-level)))
  5371. (save-excursion
  5372. (funcall fun)
  5373. (while (and (progn
  5374. (outline-next-heading)
  5375. (> (funcall outline-level) level))
  5376. (not (eobp)))
  5377. (funcall fun)))))
  5378. (defun org-map-region (fun beg end)
  5379. "Call FUN for every heading between BEG and END."
  5380. (let ((org-ignore-region t))
  5381. (save-excursion
  5382. (setq end (copy-marker end))
  5383. (goto-char beg)
  5384. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5385. (< (point) end))
  5386. (funcall fun))
  5387. (while (and (progn
  5388. (outline-next-heading)
  5389. (< (point) end))
  5390. (not (eobp)))
  5391. (funcall fun)))))
  5392. (defun org-fixup-indentation (diff)
  5393. "Change the indentation in the current entry by DIFF
  5394. However, if any line in the current entry has no indentation, or if it
  5395. would end up with no indentation after the change, nothing at all is done."
  5396. (save-excursion
  5397. (let ((end (save-excursion (outline-next-heading)
  5398. (point-marker)))
  5399. (prohibit (if (> diff 0)
  5400. "^\\S-"
  5401. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5402. col)
  5403. (unless (save-excursion (end-of-line 1)
  5404. (re-search-forward prohibit end t))
  5405. (while (and (< (point) end)
  5406. (re-search-forward "^[ \t]+" end t))
  5407. (goto-char (match-end 0))
  5408. (setq col (current-column))
  5409. (if (< diff 0) (replace-match ""))
  5410. (org-indent-to-column (+ diff col))))
  5411. (move-marker end nil))))
  5412. (defun org-convert-to-odd-levels ()
  5413. "Convert an org-mode file with all levels allowed to one with odd levels.
  5414. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5415. level 5 etc."
  5416. (interactive)
  5417. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5418. (let ((org-odd-levels-only nil) n)
  5419. (save-excursion
  5420. (goto-char (point-min))
  5421. (while (re-search-forward "^\\*\\*+ " nil t)
  5422. (setq n (- (length (match-string 0)) 2))
  5423. (while (>= (setq n (1- n)) 0)
  5424. (org-demote))
  5425. (end-of-line 1))))))
  5426. (defun org-convert-to-oddeven-levels ()
  5427. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5428. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5429. section with an even level, conversion would destroy the structure of the file. An error
  5430. is signaled in this case."
  5431. (interactive)
  5432. (goto-char (point-min))
  5433. ;; First check if there are no even levels
  5434. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5435. (org-show-context t)
  5436. (error "Not all levels are odd in this file. Conversion not possible."))
  5437. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5438. (let ((org-odd-levels-only nil) n)
  5439. (save-excursion
  5440. (goto-char (point-min))
  5441. (while (re-search-forward "^\\*\\*+ " nil t)
  5442. (setq n (/ (1- (length (match-string 0))) 2))
  5443. (while (>= (setq n (1- n)) 0)
  5444. (org-promote))
  5445. (end-of-line 1))))))
  5446. (defun org-tr-level (n)
  5447. "Make N odd if required."
  5448. (if org-odd-levels-only (1+ (/ n 2)) n))
  5449. ;;; Vertical tree motion, cutting and pasting of subtrees
  5450. (defun org-move-subtree-up (&optional arg)
  5451. "Move the current subtree up past ARG headlines of the same level."
  5452. (interactive "p")
  5453. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5454. (defun org-move-subtree-down (&optional arg)
  5455. "Move the current subtree down past ARG headlines of the same level."
  5456. (interactive "p")
  5457. (setq arg (prefix-numeric-value arg))
  5458. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  5459. 'org-get-last-sibling))
  5460. (ins-point (make-marker))
  5461. (cnt (abs arg))
  5462. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5463. ;; Select the tree
  5464. (org-back-to-heading)
  5465. (setq beg0 (point))
  5466. (save-excursion
  5467. (setq ne-beg (org-back-over-empty-lines))
  5468. (setq beg (point)))
  5469. (save-match-data
  5470. (save-excursion (outline-end-of-heading)
  5471. (setq folded (org-invisible-p)))
  5472. (outline-end-of-subtree))
  5473. (outline-next-heading)
  5474. (setq ne-end (org-back-over-empty-lines))
  5475. (setq end (point))
  5476. (goto-char beg0)
  5477. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5478. ;; include less whitespace
  5479. (save-excursion
  5480. (goto-char beg)
  5481. (forward-line (- ne-beg ne-end))
  5482. (setq beg (point))))
  5483. ;; Find insertion point, with error handling
  5484. (while (> cnt 0)
  5485. (or (and (funcall movfunc) (looking-at outline-regexp))
  5486. (progn (goto-char beg0)
  5487. (error "Cannot move past superior level or buffer limit")))
  5488. (setq cnt (1- cnt)))
  5489. (if (> arg 0)
  5490. ;; Moving forward - still need to move over subtree
  5491. (progn (org-end-of-subtree t t)
  5492. (save-excursion
  5493. (org-back-over-empty-lines)
  5494. (or (bolp) (newline)))))
  5495. (setq ne-ins (org-back-over-empty-lines))
  5496. (move-marker ins-point (point))
  5497. (setq txt (buffer-substring beg end))
  5498. (org-save-markers-in-region beg end)
  5499. (delete-region beg end)
  5500. (org-remove-empty-overlays-at beg)
  5501. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5502. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5503. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5504. (let ((bbb (point)))
  5505. (insert-before-markers txt)
  5506. (org-reinstall-markers-in-region bbb)
  5507. (move-marker ins-point bbb))
  5508. (or (bolp) (insert "\n"))
  5509. (setq ins-end (point))
  5510. (goto-char ins-point)
  5511. (org-skip-whitespace)
  5512. (when (and (< arg 0)
  5513. (org-first-sibling-p)
  5514. (> ne-ins ne-beg))
  5515. ;; Move whitespace back to beginning
  5516. (save-excursion
  5517. (goto-char ins-end)
  5518. (let ((kill-whole-line t))
  5519. (kill-line (- ne-ins ne-beg)) (point)))
  5520. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5521. (move-marker ins-point nil)
  5522. (if folded
  5523. (hide-subtree)
  5524. (org-show-entry)
  5525. (show-children)
  5526. (org-cycle-hide-drawers 'children))
  5527. (org-clean-visibility-after-subtree-move)))
  5528. (defvar org-subtree-clip ""
  5529. "Clipboard for cut and paste of subtrees.
  5530. This is actually only a copy of the kill, because we use the normal kill
  5531. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5532. (defvar org-subtree-clip-folded nil
  5533. "Was the last copied subtree folded?
  5534. This is used to fold the tree back after pasting.")
  5535. (defun org-cut-subtree (&optional n)
  5536. "Cut the current subtree into the clipboard.
  5537. With prefix arg N, cut this many sequential subtrees.
  5538. This is a short-hand for marking the subtree and then cutting it."
  5539. (interactive "p")
  5540. (org-copy-subtree n 'cut))
  5541. (defun org-copy-subtree (&optional n cut force-store-markers)
  5542. "Cut the current subtree into the clipboard.
  5543. With prefix arg N, cut this many sequential subtrees.
  5544. This is a short-hand for marking the subtree and then copying it.
  5545. If CUT is non-nil, actually cut the subtree.
  5546. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5547. of some markers in the region, even if CUT is non-nil. This is
  5548. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5549. (interactive "p")
  5550. (let (beg end folded (beg0 (point)))
  5551. (if (interactive-p)
  5552. (org-back-to-heading nil) ; take what looks like a subtree
  5553. (org-back-to-heading t)) ; take what is really there
  5554. (org-back-over-empty-lines)
  5555. (setq beg (point))
  5556. (skip-chars-forward " \t\r\n")
  5557. (save-match-data
  5558. (save-excursion (outline-end-of-heading)
  5559. (setq folded (org-invisible-p)))
  5560. (condition-case nil
  5561. (org-forward-same-level (1- n) t)
  5562. (error nil))
  5563. (org-end-of-subtree t t))
  5564. (org-back-over-empty-lines)
  5565. (setq end (point))
  5566. (goto-char beg0)
  5567. (when (> end beg)
  5568. (setq org-subtree-clip-folded folded)
  5569. (when (or cut force-store-markers)
  5570. (org-save-markers-in-region beg end))
  5571. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5572. (setq org-subtree-clip (current-kill 0))
  5573. (message "%s: Subtree(s) with %d characters"
  5574. (if cut "Cut" "Copied")
  5575. (length org-subtree-clip)))))
  5576. (defun org-paste-subtree (&optional level tree for-yank)
  5577. "Paste the clipboard as a subtree, with modification of headline level.
  5578. The entire subtree is promoted or demoted in order to match a new headline
  5579. level.
  5580. If the cursor is at the beginning of a headline, the same level as
  5581. that headline is used to paste the tree
  5582. If not, the new level is derived from the *visible* headings
  5583. before and after the insertion point, and taken to be the inferior headline
  5584. level of the two. So if the previous visible heading is level 3 and the
  5585. next is level 4 (or vice versa), level 4 will be used for insertion.
  5586. This makes sure that the subtree remains an independent subtree and does
  5587. not swallow low level entries.
  5588. You can also force a different level, either by using a numeric prefix
  5589. argument, or by inserting the heading marker by hand. For example, if the
  5590. cursor is after \"*****\", then the tree will be shifted to level 5.
  5591. If optional TREE is given, use this text instead of the kill ring.
  5592. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5593. move back over whitespace before inserting, and move point to the end of
  5594. the inserted text when done."
  5595. (interactive "P")
  5596. (setq tree (or tree (and kill-ring (current-kill 0))))
  5597. (unless (org-kill-is-subtree-p tree)
  5598. (error "%s"
  5599. (substitute-command-keys
  5600. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5601. (let* ((visp (not (org-invisible-p)))
  5602. (txt tree)
  5603. (^re (concat "^\\(" outline-regexp "\\)"))
  5604. (re (concat "\\(" outline-regexp "\\)"))
  5605. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5606. (old-level (if (string-match ^re txt)
  5607. (- (match-end 0) (match-beginning 0) 1)
  5608. -1))
  5609. (force-level (cond (level (prefix-numeric-value level))
  5610. ((and (looking-at "[ \t]*$")
  5611. (string-match
  5612. ^re_ (buffer-substring
  5613. (point-at-bol) (point))))
  5614. (- (match-end 1) (match-beginning 1)))
  5615. ((and (bolp)
  5616. (looking-at org-outline-regexp))
  5617. (- (match-end 0) (point) 1))
  5618. (t nil)))
  5619. (previous-level (save-excursion
  5620. (condition-case nil
  5621. (progn
  5622. (outline-previous-visible-heading 1)
  5623. (if (looking-at re)
  5624. (- (match-end 0) (match-beginning 0) 1)
  5625. 1))
  5626. (error 1))))
  5627. (next-level (save-excursion
  5628. (condition-case nil
  5629. (progn
  5630. (or (looking-at outline-regexp)
  5631. (outline-next-visible-heading 1))
  5632. (if (looking-at re)
  5633. (- (match-end 0) (match-beginning 0) 1)
  5634. 1))
  5635. (error 1))))
  5636. (new-level (or force-level (max previous-level next-level)))
  5637. (shift (if (or (= old-level -1)
  5638. (= new-level -1)
  5639. (= old-level new-level))
  5640. 0
  5641. (- new-level old-level)))
  5642. (delta (if (> shift 0) -1 1))
  5643. (func (if (> shift 0) 'org-demote 'org-promote))
  5644. (org-odd-levels-only nil)
  5645. beg end newend)
  5646. ;; Remove the forced level indicator
  5647. (if force-level
  5648. (delete-region (point-at-bol) (point)))
  5649. ;; Paste
  5650. (beginning-of-line 1)
  5651. (unless for-yank (org-back-over-empty-lines))
  5652. (setq beg (point))
  5653. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5654. (insert-before-markers txt)
  5655. (unless (string-match "\n\\'" txt) (insert "\n"))
  5656. (setq newend (point))
  5657. (org-reinstall-markers-in-region beg)
  5658. (setq end (point))
  5659. (goto-char beg)
  5660. (skip-chars-forward " \t\n\r")
  5661. (setq beg (point))
  5662. (if (and (org-invisible-p) visp)
  5663. (save-excursion (outline-show-heading)))
  5664. ;; Shift if necessary
  5665. (unless (= shift 0)
  5666. (save-restriction
  5667. (narrow-to-region beg end)
  5668. (while (not (= shift 0))
  5669. (org-map-region func (point-min) (point-max))
  5670. (setq shift (+ delta shift)))
  5671. (goto-char (point-min))
  5672. (setq newend (point-max))))
  5673. (when (or (interactive-p) for-yank)
  5674. (message "Clipboard pasted as level %d subtree" new-level))
  5675. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5676. kill-ring
  5677. (eq org-subtree-clip (current-kill 0))
  5678. org-subtree-clip-folded)
  5679. ;; The tree was folded before it was killed/copied
  5680. (hide-subtree))
  5681. (and for-yank (goto-char newend))))
  5682. (defun org-kill-is-subtree-p (&optional txt)
  5683. "Check if the current kill is an outline subtree, or a set of trees.
  5684. Returns nil if kill does not start with a headline, or if the first
  5685. headline level is not the largest headline level in the tree.
  5686. So this will actually accept several entries of equal levels as well,
  5687. which is OK for `org-paste-subtree'.
  5688. If optional TXT is given, check this string instead of the current kill."
  5689. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5690. (start-level (and kill
  5691. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5692. org-outline-regexp "\\)")
  5693. kill)
  5694. (- (match-end 2) (match-beginning 2) 1)))
  5695. (re (concat "^" org-outline-regexp))
  5696. (start (1+ (or (match-beginning 2) -1))))
  5697. (if (not start-level)
  5698. (progn
  5699. nil) ;; does not even start with a heading
  5700. (catch 'exit
  5701. (while (setq start (string-match re kill (1+ start)))
  5702. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5703. (throw 'exit nil)))
  5704. t))))
  5705. (defvar org-markers-to-move nil
  5706. "Markers that should be moved with a cut-and-paste operation.
  5707. Those markers are stored together with their positions relative to
  5708. the start of the region.")
  5709. (defun org-save-markers-in-region (beg end)
  5710. "Check markers in region.
  5711. If these markers are between BEG and END, record their position relative
  5712. to BEG, so that after moving the block of text, we can put the markers back
  5713. into place.
  5714. This function gets called just before an entry or tree gets cut from the
  5715. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5716. called immediately, to move the markers with the entries."
  5717. (setq org-markers-to-move nil)
  5718. (when (featurep 'org-clock)
  5719. (org-clock-save-markers-for-cut-and-paste beg end))
  5720. (when (featurep 'org-agenda)
  5721. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5722. (defun org-check-and-save-marker (marker beg end)
  5723. "Check if MARKER is between BEG and END.
  5724. If yes, remember the marker and the distance to BEG."
  5725. (when (and (marker-buffer marker)
  5726. (equal (marker-buffer marker) (current-buffer)))
  5727. (if (and (>= marker beg) (< marker end))
  5728. (push (cons marker (- marker beg)) org-markers-to-move))))
  5729. (defun org-reinstall-markers-in-region (beg)
  5730. "Move all remembered markers to their position relative to BEG."
  5731. (mapc (lambda (x)
  5732. (move-marker (car x) (+ beg (cdr x))))
  5733. org-markers-to-move)
  5734. (setq org-markers-to-move nil))
  5735. (defun org-narrow-to-subtree ()
  5736. "Narrow buffer to the current subtree."
  5737. (interactive)
  5738. (save-excursion
  5739. (save-match-data
  5740. (narrow-to-region
  5741. (progn (org-back-to-heading t) (point))
  5742. (progn (org-end-of-subtree t) (point))))))
  5743. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5744. "Clone the task (subtree) at point N times.
  5745. The clones will be inserted as siblings.
  5746. In interactive use, the user will be prompted for the number of clones
  5747. to be produced, and for a time SHIFT, which may be a repeater as used
  5748. in time stamps, for example `+3d'.
  5749. When a valid repeater is given and the entry contains any time stamps,
  5750. the clones will become a sequence in time, with time stamps in the
  5751. subtree shifted for each clone produced. If SHIFT is nil or the
  5752. empty string, time stamps will be left alone.
  5753. If the original subtree did contain time stamps with a repeater,
  5754. the following will happen:
  5755. - the repeater will be removed in each clone
  5756. - an additional clone will be produced, with the current, unshifted
  5757. date(s) in the entry.
  5758. - the original entry will be placed *after* all the clones, with
  5759. repeater intact.
  5760. - the start days in the repeater in the original entry will be shifted
  5761. to past the last clone.
  5762. I this way you can spell out a number of instances of a repeating task,
  5763. and still retain the repeater to cover future instances of the task."
  5764. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5765. (let (beg end template task
  5766. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5767. (if (not (and (integerp n) (> n 0)))
  5768. (error "Invalid number of replications %s" n))
  5769. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5770. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5771. shift)))
  5772. (error "Invalid shift specification %s" shift))
  5773. (when doshift
  5774. (setq shift-n (string-to-number (match-string 1 shift))
  5775. shift-what (cdr (assoc (match-string 2 shift)
  5776. '(("d" . day) ("w" . week)
  5777. ("m" . month) ("y" . year))))))
  5778. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5779. (setq nmin 1 nmax n)
  5780. (org-back-to-heading t)
  5781. (setq beg (point))
  5782. (org-end-of-subtree t t)
  5783. (setq end (point))
  5784. (setq template (buffer-substring beg end))
  5785. (when (and doshift
  5786. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5787. (delete-region beg end)
  5788. (setq end beg)
  5789. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5790. (goto-char end)
  5791. (loop for n from nmin to nmax do
  5792. (if (not doshift)
  5793. (setq task template)
  5794. (with-temp-buffer
  5795. (insert template)
  5796. (org-mode)
  5797. (goto-char (point-min))
  5798. (while (re-search-forward org-ts-regexp-both nil t)
  5799. (org-timestamp-change (* n shift-n) shift-what))
  5800. (unless (= n n-no-remove)
  5801. (goto-char (point-min))
  5802. (while (re-search-forward org-ts-regexp nil t)
  5803. (save-excursion
  5804. (goto-char (match-beginning 0))
  5805. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5806. (delete-region (match-beginning 1) (match-end 1))))))
  5807. (setq task (buffer-string))))
  5808. (insert task))
  5809. (goto-char beg)))
  5810. ;;; Outline Sorting
  5811. (defun org-sort (with-case)
  5812. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5813. Optional argument WITH-CASE means sort case-sensitively.
  5814. With a double prefix argument, also remove duplicate entries."
  5815. (interactive "P")
  5816. (if (org-at-table-p)
  5817. (org-call-with-arg 'org-table-sort-lines with-case)
  5818. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5819. (defun org-sort-remove-invisible (s)
  5820. (remove-text-properties 0 (length s) org-rm-props s)
  5821. (while (string-match org-bracket-link-regexp s)
  5822. (setq s (replace-match (if (match-end 2)
  5823. (match-string 3 s)
  5824. (match-string 1 s)) t t s)))
  5825. s)
  5826. (defvar org-priority-regexp) ; defined later in the file
  5827. (defvar org-after-sorting-entries-or-items-hook nil
  5828. "Hook that is run after a bunch of entries or items have been sorted.
  5829. When children are sorted, the cursor is in the parent line when this
  5830. hook gets called. When a region or a plain list is sorted, the cursor
  5831. will be in the first entry of the sorted region/list.")
  5832. (defun org-sort-entries-or-items
  5833. (&optional with-case sorting-type getkey-func compare-func property)
  5834. "Sort entries on a certain level of an outline tree, or plain list items.
  5835. If there is an active region, the entries in the region are sorted.
  5836. Else, if the cursor is before the first entry, sort the top-level items.
  5837. Else, the children of the entry at point are sorted.
  5838. If the cursor is at the first item in a plain list, the list items will be
  5839. sorted.
  5840. Sorting can be alphabetically, numerically, by date/time as given by
  5841. a time stamp, by a property or by priority.
  5842. The command prompts for the sorting type unless it has been given to the
  5843. function through the SORTING-TYPE argument, which needs to a character,
  5844. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5845. precise meaning of each character:
  5846. n Numerically, by converting the beginning of the entry/item to a number.
  5847. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5848. t By date/time, either the first active time stamp in the entry, or, if
  5849. none exist, by the first inactive one.
  5850. In items, only the first line will be chekced.
  5851. s By the scheduled date/time.
  5852. d By deadline date/time.
  5853. c By creation time, which is assumed to be the first inactive time stamp
  5854. at the beginning of a line.
  5855. p By priority according to the cookie.
  5856. r By the value of a property.
  5857. Capital letters will reverse the sort order.
  5858. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5859. called with point at the beginning of the record. It must return either
  5860. a string or a number that should serve as the sorting key for that record.
  5861. Comparing entries ignores case by default. However, with an optional argument
  5862. WITH-CASE, the sorting considers case as well."
  5863. (interactive "P")
  5864. (let ((case-func (if with-case 'identity 'downcase))
  5865. start beg end stars re re2
  5866. txt what tmp plain-list-p)
  5867. ;; Find beginning and end of region to sort
  5868. (cond
  5869. ((org-region-active-p)
  5870. ;; we will sort the region
  5871. (setq end (region-end)
  5872. what "region")
  5873. (goto-char (region-beginning))
  5874. (if (not (org-on-heading-p)) (outline-next-heading))
  5875. (setq start (point)))
  5876. ((org-at-item-p)
  5877. ;; we will sort this plain list
  5878. (org-beginning-of-item-list) (setq start (point))
  5879. (org-end-of-item-list) (setq end (point))
  5880. (goto-char start)
  5881. (setq plain-list-p t
  5882. what "plain list"))
  5883. ((or (org-on-heading-p)
  5884. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5885. ;; we will sort the children of the current headline
  5886. (org-back-to-heading)
  5887. (setq start (point)
  5888. end (progn (org-end-of-subtree t t)
  5889. (org-back-over-empty-lines)
  5890. (point))
  5891. what "children")
  5892. (goto-char start)
  5893. (show-subtree)
  5894. (outline-next-heading))
  5895. (t
  5896. ;; we will sort the top-level entries in this file
  5897. (goto-char (point-min))
  5898. (or (org-on-heading-p) (outline-next-heading))
  5899. (setq start (point) end (point-max) what "top-level")
  5900. (goto-char start)
  5901. (show-all)))
  5902. (setq beg (point))
  5903. (if (>= beg end) (error "Nothing to sort"))
  5904. (unless plain-list-p
  5905. (looking-at "\\(\\*+\\)")
  5906. (setq stars (match-string 1)
  5907. re (concat "^" (regexp-quote stars) " +")
  5908. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5909. txt (buffer-substring beg end))
  5910. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5911. (if (and (not (equal stars "*")) (string-match re2 txt))
  5912. (error "Region to sort contains a level above the first entry")))
  5913. (unless sorting-type
  5914. (message
  5915. (if plain-list-p
  5916. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5917. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5918. [t]ime [s]cheduled [d]eadline [c]reated
  5919. A/N/T/S/D/C/P/O/F means reversed:")
  5920. what)
  5921. (setq sorting-type (read-char-exclusive))
  5922. (and (= (downcase sorting-type) ?f)
  5923. (setq getkey-func
  5924. (org-icompleting-read "Sort using function: "
  5925. obarray 'fboundp t nil nil))
  5926. (setq getkey-func (intern getkey-func)))
  5927. (and (= (downcase sorting-type) ?r)
  5928. (setq property
  5929. (org-icompleting-read "Property: "
  5930. (mapcar 'list (org-buffer-property-keys t))
  5931. nil t))))
  5932. (message "Sorting entries...")
  5933. (save-restriction
  5934. (narrow-to-region start end)
  5935. (let ((dcst (downcase sorting-type))
  5936. (case-fold-search nil)
  5937. (now (current-time)))
  5938. (sort-subr
  5939. (/= dcst sorting-type)
  5940. ;; This function moves to the beginning character of the "record" to
  5941. ;; be sorted.
  5942. (if plain-list-p
  5943. (lambda nil
  5944. (if (org-at-item-p) t (goto-char (point-max))))
  5945. (lambda nil
  5946. (if (re-search-forward re nil t)
  5947. (goto-char (match-beginning 0))
  5948. (goto-char (point-max)))))
  5949. ;; This function moves to the last character of the "record" being
  5950. ;; sorted.
  5951. (if plain-list-p
  5952. 'org-end-of-item
  5953. (lambda nil
  5954. (save-match-data
  5955. (condition-case nil
  5956. (outline-forward-same-level 1)
  5957. (error
  5958. (goto-char (point-max)))))))
  5959. ;; This function returns the value that gets sorted against.
  5960. (if plain-list-p
  5961. (lambda nil
  5962. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5963. (cond
  5964. ((= dcst ?n)
  5965. (string-to-number (buffer-substring (match-end 0)
  5966. (point-at-eol))))
  5967. ((= dcst ?a)
  5968. (buffer-substring (match-end 0) (point-at-eol)))
  5969. ((= dcst ?t)
  5970. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5971. (re-search-forward org-ts-regexp-both
  5972. (point-at-eol) t))
  5973. (org-time-string-to-seconds (match-string 0))
  5974. (time-to-seconds now)))
  5975. ((= dcst ?f)
  5976. (if getkey-func
  5977. (progn
  5978. (setq tmp (funcall getkey-func))
  5979. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5980. tmp)
  5981. (error "Invalid key function `%s'" getkey-func)))
  5982. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5983. (lambda nil
  5984. (cond
  5985. ((= dcst ?n)
  5986. (if (looking-at org-complex-heading-regexp)
  5987. (string-to-number (match-string 4))
  5988. nil))
  5989. ((= dcst ?a)
  5990. (if (looking-at org-complex-heading-regexp)
  5991. (funcall case-func (match-string 4))
  5992. nil))
  5993. ((= dcst ?t)
  5994. (let ((end (save-excursion (outline-next-heading) (point))))
  5995. (if (or (re-search-forward org-ts-regexp end t)
  5996. (re-search-forward org-ts-regexp-both end t))
  5997. (org-time-string-to-seconds (match-string 0))
  5998. (time-to-seconds now))))
  5999. ((= dcst ?c)
  6000. (let ((end (save-excursion (outline-next-heading) (point))))
  6001. (if (re-search-forward
  6002. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6003. end t)
  6004. (org-time-string-to-seconds (match-string 0))
  6005. (time-to-seconds now))))
  6006. ((= dcst ?s)
  6007. (let ((end (save-excursion (outline-next-heading) (point))))
  6008. (if (re-search-forward org-scheduled-time-regexp end t)
  6009. (org-time-string-to-seconds (match-string 1))
  6010. (time-to-seconds now))))
  6011. ((= dcst ?d)
  6012. (let ((end (save-excursion (outline-next-heading) (point))))
  6013. (if (re-search-forward org-deadline-time-regexp end t)
  6014. (org-time-string-to-seconds (match-string 1))
  6015. (time-to-seconds now))))
  6016. ((= dcst ?p)
  6017. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6018. (string-to-char (match-string 2))
  6019. org-default-priority))
  6020. ((= dcst ?r)
  6021. (or (org-entry-get nil property) ""))
  6022. ((= dcst ?o)
  6023. (if (looking-at org-complex-heading-regexp)
  6024. (- 9999 (length (member (match-string 2)
  6025. org-todo-keywords-1)))))
  6026. ((= dcst ?f)
  6027. (if getkey-func
  6028. (progn
  6029. (setq tmp (funcall getkey-func))
  6030. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6031. tmp)
  6032. (error "Invalid key function `%s'" getkey-func)))
  6033. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6034. nil
  6035. (cond
  6036. ((= dcst ?a) 'string<)
  6037. ((= dcst ?f) compare-func)
  6038. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6039. (t nil)))))
  6040. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6041. (message "Sorting entries...done")))
  6042. (defun org-do-sort (table what &optional with-case sorting-type)
  6043. "Sort TABLE of WHAT according to SORTING-TYPE.
  6044. The user will be prompted for the SORTING-TYPE if the call to this
  6045. function does not specify it. WHAT is only for the prompt, to indicate
  6046. what is being sorted. The sorting key will be extracted from
  6047. the car of the elements of the table.
  6048. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6049. (unless sorting-type
  6050. (message
  6051. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6052. what)
  6053. (setq sorting-type (read-char-exclusive)))
  6054. (let ((dcst (downcase sorting-type))
  6055. extractfun comparefun)
  6056. ;; Define the appropriate functions
  6057. (cond
  6058. ((= dcst ?n)
  6059. (setq extractfun 'string-to-number
  6060. comparefun (if (= dcst sorting-type) '< '>)))
  6061. ((= dcst ?a)
  6062. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6063. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6064. comparefun (if (= dcst sorting-type)
  6065. 'string<
  6066. (lambda (a b) (and (not (string< a b))
  6067. (not (string= a b)))))))
  6068. ((= dcst ?t)
  6069. (setq extractfun
  6070. (lambda (x)
  6071. (if (or (string-match org-ts-regexp x)
  6072. (string-match org-ts-regexp-both x))
  6073. (time-to-seconds
  6074. (org-time-string-to-time (match-string 0 x)))
  6075. 0))
  6076. comparefun (if (= dcst sorting-type) '< '>)))
  6077. (t (error "Invalid sorting type `%c'" sorting-type)))
  6078. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6079. table)
  6080. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6081. ;;; The orgstruct minor mode
  6082. ;; Define a minor mode which can be used in other modes in order to
  6083. ;; integrate the org-mode structure editing commands.
  6084. ;; This is really a hack, because the org-mode structure commands use
  6085. ;; keys which normally belong to the major mode. Here is how it
  6086. ;; works: The minor mode defines all the keys necessary to operate the
  6087. ;; structure commands, but wraps the commands into a function which
  6088. ;; tests if the cursor is currently at a headline or a plain list
  6089. ;; item. If that is the case, the structure command is used,
  6090. ;; temporarily setting many Org-mode variables like regular
  6091. ;; expressions for filling etc. However, when any of those keys is
  6092. ;; used at a different location, function uses `key-binding' to look
  6093. ;; up if the key has an associated command in another currently active
  6094. ;; keymap (minor modes, major mode, global), and executes that
  6095. ;; command. There might be problems if any of the keys is otherwise
  6096. ;; used as a prefix key.
  6097. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6098. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6099. ;; addresses this by checking explicitly for both bindings.
  6100. (defvar orgstruct-mode-map (make-sparse-keymap)
  6101. "Keymap for the minor `orgstruct-mode'.")
  6102. (defvar org-local-vars nil
  6103. "List of local variables, for use by `orgstruct-mode'")
  6104. ;;;###autoload
  6105. (define-minor-mode orgstruct-mode
  6106. "Toggle the minor more `orgstruct-mode'.
  6107. This mode is for using Org-mode structure commands in other modes.
  6108. The following key behave as if Org-mode was active, if the cursor
  6109. is on a headline, or on a plain list item (both in the definition
  6110. of Org-mode).
  6111. M-up Move entry/item up
  6112. M-down Move entry/item down
  6113. M-left Promote
  6114. M-right Demote
  6115. M-S-up Move entry/item up
  6116. M-S-down Move entry/item down
  6117. M-S-left Promote subtree
  6118. M-S-right Demote subtree
  6119. M-q Fill paragraph and items like in Org-mode
  6120. C-c ^ Sort entries
  6121. C-c - Cycle list bullet
  6122. TAB Cycle item visibility
  6123. M-RET Insert new heading/item
  6124. S-M-RET Insert new TODO heading / Checkbox item
  6125. C-c C-c Set tags / toggle checkbox"
  6126. nil " OrgStruct" nil
  6127. (org-load-modules-maybe)
  6128. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6129. ;;;###autoload
  6130. (defun turn-on-orgstruct ()
  6131. "Unconditionally turn on `orgstruct-mode'."
  6132. (orgstruct-mode 1))
  6133. (defun orgstruct++-mode (&optional arg)
  6134. "Toggle `orgstruct-mode', the enhanced version of it.
  6135. In addition to setting orgstruct-mode, this also exports all indentation
  6136. and autofilling variables from org-mode into the buffer. It will also
  6137. recognize item context in multiline items.
  6138. Note that turning off orgstruct-mode will *not* remove the
  6139. indentation/paragraph settings. This can only be done by refreshing the
  6140. major mode, for example with \\[normal-mode]."
  6141. (interactive "P")
  6142. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6143. (if (< arg 1)
  6144. (orgstruct-mode -1)
  6145. (orgstruct-mode 1)
  6146. (let (var val)
  6147. (mapc
  6148. (lambda (x)
  6149. (when (string-match
  6150. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6151. (symbol-name (car x)))
  6152. (setq var (car x) val (nth 1 x))
  6153. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6154. org-local-vars)
  6155. (org-set-local 'orgstruct-is-++ t))))
  6156. (defvar orgstruct-is-++ nil
  6157. "Is orgstruct-mode in ++ version in the current-buffer?")
  6158. (make-variable-buffer-local 'orgstruct-is-++)
  6159. ;;;###autoload
  6160. (defun turn-on-orgstruct++ ()
  6161. "Unconditionally turn on `orgstruct++-mode'."
  6162. (orgstruct++-mode 1))
  6163. (defun orgstruct-error ()
  6164. "Error when there is no default binding for a structure key."
  6165. (interactive)
  6166. (error "This key has no function outside structure elements"))
  6167. (defun orgstruct-setup ()
  6168. "Setup orgstruct keymaps."
  6169. (let ((nfunc 0)
  6170. (bindings
  6171. (list
  6172. '([(meta up)] org-metaup)
  6173. '([(meta down)] org-metadown)
  6174. '([(meta left)] org-metaleft)
  6175. '([(meta right)] org-metaright)
  6176. '([(meta shift up)] org-shiftmetaup)
  6177. '([(meta shift down)] org-shiftmetadown)
  6178. '([(meta shift left)] org-shiftmetaleft)
  6179. '([(meta shift right)] org-shiftmetaright)
  6180. '([?\e (up)] org-metaup)
  6181. '([?\e (down)] org-metadown)
  6182. '([?\e (left)] org-metaleft)
  6183. '([?\e (right)] org-metaright)
  6184. '([?\e (shift up)] org-shiftmetaup)
  6185. '([?\e (shift down)] org-shiftmetadown)
  6186. '([?\e (shift left)] org-shiftmetaleft)
  6187. '([?\e (shift right)] org-shiftmetaright)
  6188. '([(shift up)] org-shiftup)
  6189. '([(shift down)] org-shiftdown)
  6190. '([(shift left)] org-shiftleft)
  6191. '([(shift right)] org-shiftright)
  6192. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6193. '("\M-q" fill-paragraph)
  6194. '("\C-c^" org-sort)
  6195. '("\C-c-" org-cycle-list-bullet)))
  6196. elt key fun cmd)
  6197. (while (setq elt (pop bindings))
  6198. (setq nfunc (1+ nfunc))
  6199. (setq key (org-key (car elt))
  6200. fun (nth 1 elt)
  6201. cmd (orgstruct-make-binding fun nfunc key))
  6202. (org-defkey orgstruct-mode-map key cmd))
  6203. ;; Special treatment needed for TAB and RET
  6204. (org-defkey orgstruct-mode-map [(tab)]
  6205. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6206. (org-defkey orgstruct-mode-map "\C-i"
  6207. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6208. (org-defkey orgstruct-mode-map "\M-\C-m"
  6209. (orgstruct-make-binding 'org-insert-heading 105
  6210. "\M-\C-m" [(meta return)]))
  6211. (org-defkey orgstruct-mode-map [(meta return)]
  6212. (orgstruct-make-binding 'org-insert-heading 106
  6213. [(meta return)] "\M-\C-m"))
  6214. (org-defkey orgstruct-mode-map [(shift meta return)]
  6215. (orgstruct-make-binding 'org-insert-todo-heading 107
  6216. [(meta return)] "\M-\C-m"))
  6217. (org-defkey orgstruct-mode-map "\e\C-m"
  6218. (orgstruct-make-binding 'org-insert-heading 108
  6219. "\e\C-m" [?\e (return)]))
  6220. (org-defkey orgstruct-mode-map [?\e (return)]
  6221. (orgstruct-make-binding 'org-insert-heading 109
  6222. [?\e (return)] "\e\C-m"))
  6223. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6224. (orgstruct-make-binding 'org-insert-todo-heading 110
  6225. [?\e (return)] "\e\C-m"))
  6226. (unless org-local-vars
  6227. (setq org-local-vars (org-get-local-variables)))
  6228. t))
  6229. (defun orgstruct-make-binding (fun n &rest keys)
  6230. "Create a function for binding in the structure minor mode.
  6231. FUN is the command to call inside a table. N is used to create a unique
  6232. command name. KEYS are keys that should be checked in for a command
  6233. to execute outside of tables."
  6234. (eval
  6235. (list 'defun
  6236. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6237. '(arg)
  6238. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6239. "Outside of structure, run the binding of `"
  6240. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6241. "'.")
  6242. '(interactive "p")
  6243. (list 'if
  6244. `(org-context-p 'headline 'item
  6245. (and orgstruct-is-++
  6246. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6247. 'item-body))
  6248. (list 'org-run-like-in-org-mode (list 'quote fun))
  6249. (list 'let '(orgstruct-mode)
  6250. (list 'call-interactively
  6251. (append '(or)
  6252. (mapcar (lambda (k)
  6253. (list 'key-binding k))
  6254. keys)
  6255. '('orgstruct-error))))))))
  6256. (defun org-context-p (&rest contexts)
  6257. "Check if local context is any of CONTEXTS.
  6258. Possible values in the list of contexts are `table', `headline', and `item'."
  6259. (let ((pos (point)))
  6260. (goto-char (point-at-bol))
  6261. (prog1 (or (and (memq 'table contexts)
  6262. (looking-at "[ \t]*|"))
  6263. (and (memq 'headline contexts)
  6264. ;;????????? (looking-at "\\*+"))
  6265. (looking-at outline-regexp))
  6266. (and (memq 'item contexts)
  6267. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6268. (and (memq 'item-body contexts)
  6269. (org-in-item-p)))
  6270. (goto-char pos))))
  6271. (defun org-get-local-variables ()
  6272. "Return a list of all local variables in an org-mode buffer."
  6273. (let (varlist)
  6274. (with-current-buffer (get-buffer-create "*Org tmp*")
  6275. (erase-buffer)
  6276. (org-mode)
  6277. (setq varlist (buffer-local-variables)))
  6278. (kill-buffer "*Org tmp*")
  6279. (delq nil
  6280. (mapcar
  6281. (lambda (x)
  6282. (setq x
  6283. (if (symbolp x)
  6284. (list x)
  6285. (list (car x) (list 'quote (cdr x)))))
  6286. (if (string-match
  6287. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6288. (symbol-name (car x)))
  6289. x nil))
  6290. varlist))))
  6291. ;;;###autoload
  6292. (defun org-run-like-in-org-mode (cmd)
  6293. "Run a command, pretending that the current buffer is in Org-mode.
  6294. This will temporarily bind local variables that are typically bound in
  6295. Org-mode to the values they have in Org-mode, and then interactively
  6296. call CMD."
  6297. (org-load-modules-maybe)
  6298. (unless org-local-vars
  6299. (setq org-local-vars (org-get-local-variables)))
  6300. (eval (list 'let org-local-vars
  6301. (list 'call-interactively (list 'quote cmd)))))
  6302. ;;;; Archiving
  6303. (defun org-get-category (&optional pos)
  6304. "Get the category applying to position POS."
  6305. (get-text-property (or pos (point)) 'org-category))
  6306. (defun org-refresh-category-properties ()
  6307. "Refresh category text properties in the buffer."
  6308. (let ((def-cat (cond
  6309. ((null org-category)
  6310. (if buffer-file-name
  6311. (file-name-sans-extension
  6312. (file-name-nondirectory buffer-file-name))
  6313. "???"))
  6314. ((symbolp org-category) (symbol-name org-category))
  6315. (t org-category)))
  6316. beg end cat pos optionp)
  6317. (org-unmodified
  6318. (save-excursion
  6319. (save-restriction
  6320. (widen)
  6321. (goto-char (point-min))
  6322. (put-text-property (point) (point-max) 'org-category def-cat)
  6323. (while (re-search-forward
  6324. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6325. (setq pos (match-end 0)
  6326. optionp (equal (char-after (match-beginning 0)) ?#)
  6327. cat (org-trim (match-string 2)))
  6328. (if optionp
  6329. (setq beg (point-at-bol) end (point-max))
  6330. (org-back-to-heading t)
  6331. (setq beg (point) end (org-end-of-subtree t t)))
  6332. (put-text-property beg end 'org-category cat)
  6333. (goto-char pos)))))))
  6334. ;;;; Link Stuff
  6335. ;;; Link abbreviations
  6336. (defun org-link-expand-abbrev (link)
  6337. "Apply replacements as defined in `org-link-abbrev-alist."
  6338. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6339. (let* ((key (match-string 1 link))
  6340. (as (or (assoc key org-link-abbrev-alist-local)
  6341. (assoc key org-link-abbrev-alist)))
  6342. (tag (and (match-end 2) (match-string 3 link)))
  6343. rpl)
  6344. (if (not as)
  6345. link
  6346. (setq rpl (cdr as))
  6347. (cond
  6348. ((symbolp rpl) (funcall rpl tag))
  6349. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6350. ((string-match "%h" rpl)
  6351. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6352. (t (concat rpl tag)))))
  6353. link))
  6354. ;;; Storing and inserting links
  6355. (defvar org-insert-link-history nil
  6356. "Minibuffer history for links inserted with `org-insert-link'.")
  6357. (defvar org-stored-links nil
  6358. "Contains the links stored with `org-store-link'.")
  6359. (defvar org-store-link-plist nil
  6360. "Plist with info about the most recently link created with `org-store-link'.")
  6361. (defvar org-link-protocols nil
  6362. "Link protocols added to Org-mode using `org-add-link-type'.")
  6363. (defvar org-store-link-functions nil
  6364. "List of functions that are called to create and store a link.
  6365. Each function will be called in turn until one returns a non-nil
  6366. value. Each function should check if it is responsible for creating
  6367. this link (for example by looking at the major mode).
  6368. If not, it must exit and return nil.
  6369. If yes, it should return a non-nil value after a calling
  6370. `org-store-link-props' with a list of properties and values.
  6371. Special properties are:
  6372. :type The link prefix. like \"http\". This must be given.
  6373. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6374. This is obligatory as well.
  6375. :description Optional default description for the second pair
  6376. of brackets in an Org-mode link. The user can still change
  6377. this when inserting this link into an Org-mode buffer.
  6378. In addition to these, any additional properties can be specified
  6379. and then used in remember templates.")
  6380. (defun org-add-link-type (type &optional follow export)
  6381. "Add TYPE to the list of `org-link-types'.
  6382. Re-compute all regular expressions depending on `org-link-types'
  6383. FOLLOW and EXPORT are two functions.
  6384. FOLLOW should take the link path as the single argument and do whatever
  6385. is necessary to follow the link, for example find a file or display
  6386. a mail message.
  6387. EXPORT should format the link path for export to one of the export formats.
  6388. It should be a function accepting three arguments:
  6389. path the path of the link, the text after the prefix (like \"http:\")
  6390. desc the description of the link, if any, nil if there was no description
  6391. format the export format, a symbol like `html' or `latex'.
  6392. The function may use the FORMAT information to return different values
  6393. depending on the format. The return value will be put literally into
  6394. the exported file.
  6395. Org-mode has a built-in default for exporting links. If you are happy with
  6396. this default, there is no need to define an export function for the link
  6397. type. For a simple example of an export function, see `org-bbdb.el'."
  6398. (add-to-list 'org-link-types type t)
  6399. (org-make-link-regexps)
  6400. (if (assoc type org-link-protocols)
  6401. (setcdr (assoc type org-link-protocols) (list follow export))
  6402. (push (list type follow export) org-link-protocols)))
  6403. ;;;###autoload
  6404. (defun org-store-link (arg)
  6405. "\\<org-mode-map>Store an org-link to the current location.
  6406. This link is added to `org-stored-links' and can later be inserted
  6407. into an org-buffer with \\[org-insert-link].
  6408. For some link types, a prefix arg is interpreted:
  6409. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6410. For file links, arg negates `org-context-in-file-links'."
  6411. (interactive "P")
  6412. (org-load-modules-maybe)
  6413. (setq org-store-link-plist nil) ; reset
  6414. (let ((outline-regexp (org-get-limited-outline-regexp))
  6415. link cpltxt desc description search txt custom-id)
  6416. (cond
  6417. ((run-hook-with-args-until-success 'org-store-link-functions)
  6418. (setq link (plist-get org-store-link-plist :link)
  6419. desc (or (plist-get org-store-link-plist :description) link)))
  6420. ((equal (buffer-name) "*Org Edit Src Example*")
  6421. (let (label gc)
  6422. (while (or (not label)
  6423. (save-excursion
  6424. (save-restriction
  6425. (widen)
  6426. (goto-char (point-min))
  6427. (re-search-forward
  6428. (regexp-quote (format org-coderef-label-format label))
  6429. nil t))))
  6430. (when label (message "Label exists already") (sit-for 2))
  6431. (setq label (read-string "Code line label: " label)))
  6432. (end-of-line 1)
  6433. (setq link (format org-coderef-label-format label))
  6434. (setq gc (- 79 (length link)))
  6435. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6436. (insert link)
  6437. (setq link (concat "(" label ")") desc nil)))
  6438. ((eq major-mode 'calendar-mode)
  6439. (let ((cd (calendar-cursor-to-date)))
  6440. (setq link
  6441. (format-time-string
  6442. (car org-time-stamp-formats)
  6443. (apply 'encode-time
  6444. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6445. nil nil nil))))
  6446. (org-store-link-props :type "calendar" :date cd)))
  6447. ((eq major-mode 'w3-mode)
  6448. (setq cpltxt (if (and (buffer-name)
  6449. (not (string-match "Untitled" (buffer-name))))
  6450. (buffer-name)
  6451. (url-view-url t))
  6452. link (org-make-link (url-view-url t)))
  6453. (org-store-link-props :type "w3" :url (url-view-url t)))
  6454. ((eq major-mode 'w3m-mode)
  6455. (setq cpltxt (or w3m-current-title w3m-current-url)
  6456. link (org-make-link w3m-current-url))
  6457. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6458. ((setq search (run-hook-with-args-until-success
  6459. 'org-create-file-search-functions))
  6460. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6461. "::" search))
  6462. (setq cpltxt (or description link)))
  6463. ((eq major-mode 'image-mode)
  6464. (setq cpltxt (concat "file:"
  6465. (abbreviate-file-name buffer-file-name))
  6466. link (org-make-link cpltxt))
  6467. (org-store-link-props :type "image" :file buffer-file-name))
  6468. ((eq major-mode 'dired-mode)
  6469. ;; link to the file in the current line
  6470. (setq cpltxt (concat "file:"
  6471. (abbreviate-file-name
  6472. (expand-file-name
  6473. (dired-get-filename nil t))))
  6474. link (org-make-link cpltxt)))
  6475. ((and buffer-file-name (org-mode-p))
  6476. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6477. (cond
  6478. ((org-in-regexp "<<\\(.*?\\)>>")
  6479. (setq cpltxt
  6480. (concat "file:"
  6481. (abbreviate-file-name buffer-file-name)
  6482. "::" (match-string 1))
  6483. link (org-make-link cpltxt)))
  6484. ((and (featurep 'org-id)
  6485. (or (eq org-link-to-org-use-id t)
  6486. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6487. (interactive-p))
  6488. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6489. (interactive-p)
  6490. (not custom-id))
  6491. (and org-link-to-org-use-id
  6492. (condition-case nil
  6493. (org-entry-get nil "ID")
  6494. (error nil)))))
  6495. ;; We can make a link using the ID.
  6496. (setq link (condition-case nil
  6497. (prog1 (org-id-store-link)
  6498. (setq desc (plist-get org-store-link-plist
  6499. :description)))
  6500. (error
  6501. ;; probably before first headline, link to file only
  6502. (concat "file:"
  6503. (abbreviate-file-name buffer-file-name))))))
  6504. (t
  6505. ;; Just link to current headline
  6506. (setq cpltxt (concat "file:"
  6507. (abbreviate-file-name buffer-file-name)))
  6508. ;; Add a context search string
  6509. (when (org-xor org-context-in-file-links arg)
  6510. (setq txt (cond
  6511. ((org-on-heading-p) nil)
  6512. ((org-region-active-p)
  6513. (buffer-substring (region-beginning) (region-end)))
  6514. (t nil)))
  6515. (when (or (null txt) (string-match "\\S-" txt))
  6516. (setq cpltxt
  6517. (concat cpltxt "::"
  6518. (condition-case nil
  6519. (org-make-org-heading-search-string txt)
  6520. (error "")))
  6521. desc (or (nth 4 (org-heading-components)) "NONE"))))
  6522. (if (string-match "::\\'" cpltxt)
  6523. (setq cpltxt (substring cpltxt 0 -2)))
  6524. (setq link (org-make-link cpltxt)))))
  6525. ((buffer-file-name (buffer-base-buffer))
  6526. ;; Just link to this file here.
  6527. (setq cpltxt (concat "file:"
  6528. (abbreviate-file-name
  6529. (buffer-file-name (buffer-base-buffer)))))
  6530. ;; Add a context string
  6531. (when (org-xor org-context-in-file-links arg)
  6532. (setq txt (if (org-region-active-p)
  6533. (buffer-substring (region-beginning) (region-end))
  6534. (buffer-substring (point-at-bol) (point-at-eol))))
  6535. ;; Only use search option if there is some text.
  6536. (when (string-match "\\S-" txt)
  6537. (setq cpltxt
  6538. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6539. desc "NONE")))
  6540. (setq link (org-make-link cpltxt)))
  6541. ((interactive-p)
  6542. (error "Cannot link to a buffer which is not visiting a file"))
  6543. (t (setq link nil)))
  6544. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6545. (setq link (or link cpltxt)
  6546. desc (or desc cpltxt))
  6547. (if (equal desc "NONE") (setq desc nil))
  6548. (if (and (or (interactive-p) executing-kbd-macro) link)
  6549. (progn
  6550. (setq org-stored-links
  6551. (cons (list link desc) org-stored-links))
  6552. (message "Stored: %s" (or desc link))
  6553. (when custom-id
  6554. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6555. "::#" custom-id))
  6556. (setq org-stored-links
  6557. (cons (list link desc) org-stored-links))))
  6558. (and link (org-make-link-string link desc)))))
  6559. (defun org-store-link-props (&rest plist)
  6560. "Store link properties, extract names and addresses."
  6561. (let (x adr)
  6562. (when (setq x (plist-get plist :from))
  6563. (setq adr (mail-extract-address-components x))
  6564. (setq plist (plist-put plist :fromname (car adr)))
  6565. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6566. (when (setq x (plist-get plist :to))
  6567. (setq adr (mail-extract-address-components x))
  6568. (setq plist (plist-put plist :toname (car adr)))
  6569. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6570. (let ((from (plist-get plist :from))
  6571. (to (plist-get plist :to)))
  6572. (when (and from to org-from-is-user-regexp)
  6573. (setq plist
  6574. (plist-put plist :fromto
  6575. (if (string-match org-from-is-user-regexp from)
  6576. (concat "to %t")
  6577. (concat "from %f"))))))
  6578. (setq org-store-link-plist plist))
  6579. (defun org-add-link-props (&rest plist)
  6580. "Add these properties to the link property list."
  6581. (let (key value)
  6582. (while plist
  6583. (setq key (pop plist) value (pop plist))
  6584. (setq org-store-link-plist
  6585. (plist-put org-store-link-plist key value)))))
  6586. (defun org-email-link-description (&optional fmt)
  6587. "Return the description part of an email link.
  6588. This takes information from `org-store-link-plist' and formats it
  6589. according to FMT (default from `org-email-link-description-format')."
  6590. (setq fmt (or fmt org-email-link-description-format))
  6591. (let* ((p org-store-link-plist)
  6592. (to (plist-get p :toaddress))
  6593. (from (plist-get p :fromaddress))
  6594. (table
  6595. (list
  6596. (cons "%c" (plist-get p :fromto))
  6597. (cons "%F" (plist-get p :from))
  6598. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6599. (cons "%T" (plist-get p :to))
  6600. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6601. (cons "%s" (plist-get p :subject))
  6602. (cons "%m" (plist-get p :message-id)))))
  6603. (when (string-match "%c" fmt)
  6604. ;; Check if the user wrote this message
  6605. (if (and org-from-is-user-regexp from to
  6606. (save-match-data (string-match org-from-is-user-regexp from)))
  6607. (setq fmt (replace-match "to %t" t t fmt))
  6608. (setq fmt (replace-match "from %f" t t fmt))))
  6609. (org-replace-escapes fmt table)))
  6610. (defun org-make-org-heading-search-string (&optional string heading)
  6611. "Make search string for STRING or current headline."
  6612. (interactive)
  6613. (let ((s (or string (org-get-heading))))
  6614. (unless (and string (not heading))
  6615. ;; We are using a headline, clean up garbage in there.
  6616. (if (string-match org-todo-regexp s)
  6617. (setq s (replace-match "" t t s)))
  6618. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6619. (setq s (replace-match "" t t s)))
  6620. (setq s (org-trim s))
  6621. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6622. org-comment-string "\\)") s)
  6623. (setq s (replace-match "" t t s)))
  6624. (while (string-match org-ts-regexp s)
  6625. (setq s (replace-match "" t t s))))
  6626. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6627. (setq s (replace-match " " t t s)))
  6628. (or string (setq s (concat "*" s))) ; Add * for headlines
  6629. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6630. (defun org-make-link (&rest strings)
  6631. "Concatenate STRINGS."
  6632. (apply 'concat strings))
  6633. (defun org-make-link-string (link &optional description)
  6634. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6635. (unless (string-match "\\S-" link)
  6636. (error "Empty link"))
  6637. (when (stringp description)
  6638. ;; Remove brackets from the description, they are fatal.
  6639. (while (string-match "\\[" description)
  6640. (setq description (replace-match "{" t t description)))
  6641. (while (string-match "\\]" description)
  6642. (setq description (replace-match "}" t t description))))
  6643. (when (equal (org-link-escape link) description)
  6644. ;; No description needed, it is identical
  6645. (setq description nil))
  6646. (when (and (not description)
  6647. (not (equal link (org-link-escape link))))
  6648. (setq description (org-extract-attributes link)))
  6649. (concat "[[" (org-link-escape link) "]"
  6650. (if description (concat "[" description "]") "")
  6651. "]"))
  6652. (defconst org-link-escape-chars
  6653. '((?\ . "%20")
  6654. (?\[ . "%5B")
  6655. (?\] . "%5D")
  6656. (?\340 . "%E0") ; `a
  6657. (?\342 . "%E2") ; ^a
  6658. (?\347 . "%E7") ; ,c
  6659. (?\350 . "%E8") ; `e
  6660. (?\351 . "%E9") ; 'e
  6661. (?\352 . "%EA") ; ^e
  6662. (?\356 . "%EE") ; ^i
  6663. (?\364 . "%F4") ; ^o
  6664. (?\371 . "%F9") ; `u
  6665. (?\373 . "%FB") ; ^u
  6666. (?\; . "%3B")
  6667. (?? . "%3F")
  6668. (?= . "%3D")
  6669. (?+ . "%2B")
  6670. )
  6671. "Association list of escapes for some characters problematic in links.
  6672. This is the list that is used for internal purposes.")
  6673. (defvar org-url-encoding-use-url-hexify nil)
  6674. (defconst org-link-escape-chars-browser
  6675. '((?\ . "%20")) ; 32 for the SPC char
  6676. "Association list of escapes for some characters problematic in links.
  6677. This is the list that is used before handing over to the browser.")
  6678. (defun org-link-escape (text &optional table)
  6679. "Escape characters in TEXT that are problematic for links."
  6680. (if org-url-encoding-use-url-hexify
  6681. (url-hexify-string text)
  6682. (setq table (or table org-link-escape-chars))
  6683. (when text
  6684. (let ((re (mapconcat (lambda (x) (regexp-quote
  6685. (char-to-string (car x))))
  6686. table "\\|")))
  6687. (while (string-match re text)
  6688. (setq text
  6689. (replace-match
  6690. (cdr (assoc (string-to-char (match-string 0 text))
  6691. table))
  6692. t t text)))
  6693. text))))
  6694. (defun org-link-unescape (text &optional table)
  6695. "Reverse the action of `org-link-escape'."
  6696. (if org-url-encoding-use-url-hexify
  6697. (url-unhex-string text)
  6698. (setq table (or table org-link-escape-chars))
  6699. (when text
  6700. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6701. table "\\|")))
  6702. (while (string-match re text)
  6703. (setq text
  6704. (replace-match
  6705. (char-to-string (car (rassoc (match-string 0 text) table)))
  6706. t t text)))
  6707. text))))
  6708. (defun org-xor (a b)
  6709. "Exclusive or."
  6710. (if a (not b) b))
  6711. (defun org-fixup-message-id-for-http (s)
  6712. "Replace special characters in a message id, so it can be used in an http query."
  6713. (while (string-match "<" s)
  6714. (setq s (replace-match "%3C" t t s)))
  6715. (while (string-match ">" s)
  6716. (setq s (replace-match "%3E" t t s)))
  6717. (while (string-match "@" s)
  6718. (setq s (replace-match "%40" t t s)))
  6719. s)
  6720. ;;;###autoload
  6721. (defun org-insert-link-global ()
  6722. "Insert a link like Org-mode does.
  6723. This command can be called in any mode to insert a link in Org-mode syntax."
  6724. (interactive)
  6725. (org-load-modules-maybe)
  6726. (org-run-like-in-org-mode 'org-insert-link))
  6727. (defun org-insert-link (&optional complete-file link-location)
  6728. "Insert a link. At the prompt, enter the link.
  6729. Completion can be used to insert any of the link protocol prefixes like
  6730. http or ftp in use.
  6731. The history can be used to select a link previously stored with
  6732. `org-store-link'. When the empty string is entered (i.e. if you just
  6733. press RET at the prompt), the link defaults to the most recently
  6734. stored link. As SPC triggers completion in the minibuffer, you need to
  6735. use M-SPC or C-q SPC to force the insertion of a space character.
  6736. You will also be prompted for a description, and if one is given, it will
  6737. be displayed in the buffer instead of the link.
  6738. If there is already a link at point, this command will allow you to edit link
  6739. and description parts.
  6740. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6741. be selected using completion. The path to the file will be relative to the
  6742. current directory if the file is in the current directory or a subdirectory.
  6743. Otherwise, the link will be the absolute path as completed in the minibuffer
  6744. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6745. option `org-link-file-path-type'.
  6746. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6747. the current directory or below.
  6748. With three \\[universal-argument] prefixes, negate the meaning of
  6749. `org-keep-stored-link-after-insertion'.
  6750. If `org-make-link-description-function' is non-nil, this function will be
  6751. called with the link target, and the result will be the default
  6752. link description.
  6753. If the LINK-LOCATION parameter is non-nil, this value will be
  6754. used as the link location instead of reading one interactively."
  6755. (interactive "P")
  6756. (let* ((wcf (current-window-configuration))
  6757. (region (if (org-region-active-p)
  6758. (buffer-substring (region-beginning) (region-end))))
  6759. (remove (and region (list (region-beginning) (region-end))))
  6760. (desc region)
  6761. tmphist ; byte-compile incorrectly complains about this
  6762. (link link-location)
  6763. entry file all-prefixes)
  6764. (cond
  6765. (link-location) ; specified by arg, just use it.
  6766. ((org-in-regexp org-bracket-link-regexp 1)
  6767. ;; We do have a link at point, and we are going to edit it.
  6768. (setq remove (list (match-beginning 0) (match-end 0)))
  6769. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6770. (setq link (read-string "Link: "
  6771. (org-link-unescape
  6772. (org-match-string-no-properties 1)))))
  6773. ((or (org-in-regexp org-angle-link-re)
  6774. (org-in-regexp org-plain-link-re))
  6775. ;; Convert to bracket link
  6776. (setq remove (list (match-beginning 0) (match-end 0))
  6777. link (read-string "Link: "
  6778. (org-remove-angle-brackets (match-string 0)))))
  6779. ((member complete-file '((4) (16)))
  6780. ;; Completing read for file names.
  6781. (setq link (org-file-complete-link complete-file)))
  6782. (t
  6783. ;; Read link, with completion for stored links.
  6784. (with-output-to-temp-buffer "*Org Links*"
  6785. (princ "Insert a link.
  6786. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6787. (when org-stored-links
  6788. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6789. (princ (mapconcat
  6790. (lambda (x)
  6791. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6792. (reverse org-stored-links) "\n"))))
  6793. (let ((cw (selected-window)))
  6794. (select-window (get-buffer-window "*Org Links*"))
  6795. (setq truncate-lines t)
  6796. (unless (pos-visible-in-window-p (point-max))
  6797. (org-fit-window-to-buffer))
  6798. (and (window-live-p cw) (select-window cw)))
  6799. ;; Fake a link history, containing the stored links.
  6800. (setq tmphist (append (mapcar 'car org-stored-links)
  6801. org-insert-link-history))
  6802. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6803. (mapcar 'car org-link-abbrev-alist)
  6804. org-link-types))
  6805. (unwind-protect
  6806. (progn
  6807. (setq link
  6808. (let ((org-completion-use-ido nil)
  6809. (org-completion-use-iswitchb nil))
  6810. (org-completing-read
  6811. "Link: "
  6812. (append
  6813. (mapcar (lambda (x) (list (concat x ":")))
  6814. all-prefixes)
  6815. (mapcar 'car org-stored-links))
  6816. nil nil nil
  6817. 'tmphist
  6818. (car (car org-stored-links)))))
  6819. (if (or (member link all-prefixes)
  6820. (and (equal ":" (substring link -1))
  6821. (member (substring link 0 -1) all-prefixes)
  6822. (setq link (substring link 0 -1))))
  6823. (setq link (org-link-try-special-completion link))))
  6824. (set-window-configuration wcf)
  6825. (kill-buffer "*Org Links*"))
  6826. (setq entry (assoc link org-stored-links))
  6827. (or entry (push link org-insert-link-history))
  6828. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6829. (not org-keep-stored-link-after-insertion))
  6830. (setq org-stored-links (delq (assoc link org-stored-links)
  6831. org-stored-links)))
  6832. (setq desc (or desc (nth 1 entry)))))
  6833. (if (string-match org-plain-link-re link)
  6834. ;; URL-like link, normalize the use of angular brackets.
  6835. (setq link (org-make-link (org-remove-angle-brackets link))))
  6836. ;; Check if we are linking to the current file with a search option
  6837. ;; If yes, simplify the link by using only the search option.
  6838. (when (and buffer-file-name
  6839. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6840. (let* ((path (match-string 1 link))
  6841. (case-fold-search nil)
  6842. (search (match-string 2 link)))
  6843. (save-match-data
  6844. (if (equal (file-truename buffer-file-name) (file-truename path))
  6845. ;; We are linking to this same file, with a search option
  6846. (setq link search)))))
  6847. ;; Check if we can/should use a relative path. If yes, simplify the link
  6848. (when (string-match "^file:\\(.*\\)" link)
  6849. (let* ((path (match-string 1 link))
  6850. (origpath path)
  6851. (case-fold-search nil))
  6852. (cond
  6853. ((or (eq org-link-file-path-type 'absolute)
  6854. (equal complete-file '(16)))
  6855. (setq path (abbreviate-file-name (expand-file-name path))))
  6856. ((eq org-link-file-path-type 'noabbrev)
  6857. (setq path (expand-file-name path)))
  6858. ((eq org-link-file-path-type 'relative)
  6859. (setq path (file-relative-name path)))
  6860. (t
  6861. (save-match-data
  6862. (if (string-match (concat "^" (regexp-quote
  6863. (file-name-as-directory
  6864. (expand-file-name "."))))
  6865. (expand-file-name path))
  6866. ;; We are linking a file with relative path name.
  6867. (setq path (substring (expand-file-name path)
  6868. (match-end 0)))
  6869. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6870. (setq link (concat "file:" path))
  6871. (if (equal desc origpath)
  6872. (setq desc path))))
  6873. (if org-make-link-description-function
  6874. (setq desc (funcall org-make-link-description-function link desc)))
  6875. (setq desc (read-string "Description: " desc))
  6876. (unless (string-match "\\S-" desc) (setq desc nil))
  6877. (if remove (apply 'delete-region remove))
  6878. (insert (org-make-link-string link desc))))
  6879. (defun org-link-try-special-completion (type)
  6880. "If there is completion support for link type TYPE, offer it."
  6881. (let ((fun (intern (concat "org-" type "-complete-link"))))
  6882. (if (functionp fun)
  6883. (funcall fun)
  6884. (read-string "Link (no completion support): " (concat type ":")))))
  6885. (defun org-file-complete-link (&optional arg)
  6886. "Create a file link using completion."
  6887. (let (file link)
  6888. (setq file (read-file-name "File: "))
  6889. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6890. (pwd1 (file-name-as-directory (abbreviate-file-name
  6891. (expand-file-name ".")))))
  6892. (cond
  6893. ((equal arg '(16))
  6894. (setq link (org-make-link
  6895. "file:"
  6896. (abbreviate-file-name (expand-file-name file)))))
  6897. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6898. (setq link (org-make-link "file:" (match-string 1 file))))
  6899. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6900. (expand-file-name file))
  6901. (setq link (org-make-link
  6902. "file:" (match-string 1 (expand-file-name file)))))
  6903. (t (setq link (org-make-link "file:" file)))))
  6904. link))
  6905. (defun org-completing-read (&rest args)
  6906. "Completing-read with SPACE being a normal character."
  6907. (let ((minibuffer-local-completion-map
  6908. (copy-keymap minibuffer-local-completion-map)))
  6909. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6910. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6911. (apply 'org-icompleting-read args)))
  6912. (defun org-completing-read-no-i (&rest args)
  6913. (let (org-completion-use-ido org-completion-use-iswitchb)
  6914. (apply 'org-completing-read args)))
  6915. (defun org-iswitchb-completing-read (prompt choices &rest args)
  6916. "Use iswitch as a completing-read replacement to choose from choices.
  6917. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  6918. from."
  6919. (let ((iswitchb-make-buflist-hook
  6920. (lambda ()
  6921. (setq iswitchb-temp-buflist choices))))
  6922. (iswitchb-read-buffer prompt)))
  6923. (defun org-icompleting-read (&rest args)
  6924. "Completing-read using `ido-mode' or `iswitchb' speedups if available"
  6925. (if (and org-completion-use-ido
  6926. (fboundp 'ido-completing-read)
  6927. (boundp 'ido-mode) ido-mode
  6928. (listp (second args)))
  6929. (let ((ido-enter-matching-directory nil))
  6930. (apply 'ido-completing-read (concat (car args))
  6931. (if (consp (car (nth 1 args)))
  6932. (mapcar (lambda (x) (car x)) (nth 1 args))
  6933. (nth 1 args))
  6934. (cddr args)))
  6935. (if (and org-completion-use-iswitchb
  6936. (boundp 'iswitchb-mode) iswitchb-mode
  6937. (listp (second args)))
  6938. (apply 'org-iswitchb-completing-read (concat (car args))
  6939. (mapcar (lambda (x) (car x)) (nth 1 args))
  6940. (cddr args))
  6941. (apply 'completing-read args))))
  6942. (defun org-extract-attributes (s)
  6943. "Extract the attributes cookie from a string and set as text property."
  6944. (let (a attr (start 0) key value)
  6945. (save-match-data
  6946. (when (string-match "{{\\([^}]+\\)}}$" s)
  6947. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6948. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6949. (setq key (match-string 1 a) value (match-string 2 a)
  6950. start (match-end 0)
  6951. attr (plist-put attr (intern key) value))))
  6952. (org-add-props s nil 'org-attr attr))
  6953. s))
  6954. (defun org-extract-attributes-from-string (tag)
  6955. (let (key value attr)
  6956. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6957. (setq key (match-string 1 tag) value (match-string 2 tag)
  6958. tag (replace-match "" t t tag)
  6959. attr (plist-put attr (intern key) value)))
  6960. (cons tag attr)))
  6961. (defun org-attributes-to-string (plist)
  6962. "Format a property list into an HTML attribute list."
  6963. (let ((s "") key value)
  6964. (while plist
  6965. (setq key (pop plist) value (pop plist))
  6966. (and value
  6967. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6968. s))
  6969. ;;; Opening/following a link
  6970. (defvar org-link-search-failed nil)
  6971. (defun org-next-link ()
  6972. "Move forward to the next link.
  6973. If the link is in hidden text, expose it."
  6974. (interactive)
  6975. (when (and org-link-search-failed (eq this-command last-command))
  6976. (goto-char (point-min))
  6977. (message "Link search wrapped back to beginning of buffer"))
  6978. (setq org-link-search-failed nil)
  6979. (let* ((pos (point))
  6980. (ct (org-context))
  6981. (a (assoc :link ct)))
  6982. (if a (goto-char (nth 2 a)))
  6983. (if (re-search-forward org-any-link-re nil t)
  6984. (progn
  6985. (goto-char (match-beginning 0))
  6986. (if (org-invisible-p) (org-show-context)))
  6987. (goto-char pos)
  6988. (setq org-link-search-failed t)
  6989. (error "No further link found"))))
  6990. (defun org-previous-link ()
  6991. "Move backward to the previous link.
  6992. If the link is in hidden text, expose it."
  6993. (interactive)
  6994. (when (and org-link-search-failed (eq this-command last-command))
  6995. (goto-char (point-max))
  6996. (message "Link search wrapped back to end of buffer"))
  6997. (setq org-link-search-failed nil)
  6998. (let* ((pos (point))
  6999. (ct (org-context))
  7000. (a (assoc :link ct)))
  7001. (if a (goto-char (nth 1 a)))
  7002. (if (re-search-backward org-any-link-re nil t)
  7003. (progn
  7004. (goto-char (match-beginning 0))
  7005. (if (org-invisible-p) (org-show-context)))
  7006. (goto-char pos)
  7007. (setq org-link-search-failed t)
  7008. (error "No further link found"))))
  7009. (defun org-translate-link (s)
  7010. "Translate a link string if a translation function has been defined."
  7011. (if (and org-link-translation-function
  7012. (fboundp org-link-translation-function)
  7013. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7014. (progn
  7015. (setq s (funcall org-link-translation-function
  7016. (match-string 1) (match-string 2)))
  7017. (concat (car s) ":" (cdr s)))
  7018. s))
  7019. (defun org-translate-link-from-planner (type path)
  7020. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7021. This is still an experimental function, your mileage may vary."
  7022. (cond
  7023. ((member type '("http" "https" "news" "ftp"))
  7024. ;; standard Internet links are the same.
  7025. nil)
  7026. ((and (equal type "irc") (string-match "^//" path))
  7027. ;; Planner has two / at the beginning of an irc link, we have 1.
  7028. ;; We should have zero, actually....
  7029. (setq path (substring path 1)))
  7030. ((and (equal type "lisp") (string-match "^/" path))
  7031. ;; Planner has a slash, we do not.
  7032. (setq type "elisp" path (substring path 1)))
  7033. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7034. ;; A typical message link. Planner has the id after the fina slash,
  7035. ;; we separate it with a hash mark
  7036. (setq path (concat (match-string 1 path) "#"
  7037. (org-remove-angle-brackets (match-string 2 path)))))
  7038. )
  7039. (cons type path))
  7040. (defun org-find-file-at-mouse (ev)
  7041. "Open file link or URL at mouse."
  7042. (interactive "e")
  7043. (mouse-set-point ev)
  7044. (org-open-at-point 'in-emacs))
  7045. (defun org-open-at-mouse (ev)
  7046. "Open file link or URL at mouse."
  7047. (interactive "e")
  7048. (mouse-set-point ev)
  7049. (if (eq major-mode 'org-agenda-mode)
  7050. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7051. (org-open-at-point))
  7052. (defvar org-window-config-before-follow-link nil
  7053. "The window configuration before following a link.
  7054. This is saved in case the need arises to restore it.")
  7055. (defvar org-open-link-marker (make-marker)
  7056. "Marker pointing to the location where `org-open-at-point; was called.")
  7057. ;;;###autoload
  7058. (defun org-open-at-point-global ()
  7059. "Follow a link like Org-mode does.
  7060. This command can be called in any mode to follow a link that has
  7061. Org-mode syntax."
  7062. (interactive)
  7063. (org-run-like-in-org-mode 'org-open-at-point))
  7064. ;;;###autoload
  7065. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7066. "Open a link in the string S, as if it was in Org-mode."
  7067. (interactive "sLink: \nP")
  7068. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7069. (with-temp-buffer
  7070. (let ((org-inhibit-startup t))
  7071. (org-mode)
  7072. (insert s)
  7073. (goto-char (point-min))
  7074. (org-open-at-point arg reference-buffer)))))
  7075. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7076. "Open link at or after point.
  7077. If there is no link at point, this function will search forward up to
  7078. the end of the current line.
  7079. Normally, files will be opened by an appropriate application. If the
  7080. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7081. With a double prefix argument, try to open outside of Emacs, in the
  7082. application the system uses for this file type."
  7083. (interactive "P")
  7084. (org-load-modules-maybe)
  7085. (move-marker org-open-link-marker (point))
  7086. (setq org-window-config-before-follow-link (current-window-configuration))
  7087. (org-remove-occur-highlights nil nil t)
  7088. (cond
  7089. ((and (org-on-heading-p)
  7090. (not (org-in-regexp
  7091. (concat org-plain-link-re "\\|"
  7092. org-bracket-link-regexp "\\|"
  7093. org-angle-link-re))))
  7094. (org-offer-links-in-entry in-emacs))
  7095. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7096. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7097. (org-footnote-action))
  7098. (t
  7099. (let (type path link line search (pos (point)))
  7100. (catch 'match
  7101. (save-excursion
  7102. (skip-chars-forward "^]\n\r")
  7103. (when (org-in-regexp org-bracket-link-regexp)
  7104. (setq link (org-extract-attributes
  7105. (org-link-unescape (org-match-string-no-properties 1))))
  7106. (while (string-match " *\n *" link)
  7107. (setq link (replace-match " " t t link)))
  7108. (setq link (org-link-expand-abbrev link))
  7109. (cond
  7110. ((or (file-name-absolute-p link)
  7111. (string-match "^\\.\\.?/" link))
  7112. (setq type "file" path link))
  7113. ((string-match org-link-re-with-space3 link)
  7114. (setq type (match-string 1 link) path (match-string 2 link)))
  7115. (t (setq type "thisfile" path link)))
  7116. (throw 'match t)))
  7117. (when (get-text-property (point) 'org-linked-text)
  7118. (setq type "thisfile"
  7119. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7120. (1+ (point)) (point))
  7121. path (buffer-substring
  7122. (previous-single-property-change pos 'org-linked-text)
  7123. (next-single-property-change pos 'org-linked-text)))
  7124. (throw 'match t))
  7125. (save-excursion
  7126. (when (or (org-in-regexp org-angle-link-re)
  7127. (org-in-regexp org-plain-link-re))
  7128. (setq type (match-string 1) path (match-string 2))
  7129. (throw 'match t)))
  7130. (save-excursion
  7131. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7132. (setq type "tags"
  7133. path (match-string 1))
  7134. (while (string-match ":" path)
  7135. (setq path (replace-match "+" t t path)))
  7136. (throw 'match t)))
  7137. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7138. (setq type "tree-match"
  7139. path (match-string 1))
  7140. (throw 'match t)))
  7141. (unless path
  7142. (error "No link found"))
  7143. ;; switch back to reference buffer
  7144. ;; needed when if called in a temporary buffer through
  7145. ;; org-open-link-from-string
  7146. (with-current-buffer (or reference-buffer (current-buffer))
  7147. ;; Remove any trailing spaces in path
  7148. (if (string-match " +\\'" path)
  7149. (setq path (replace-match "" t t path)))
  7150. (if (and org-link-translation-function
  7151. (fboundp org-link-translation-function))
  7152. ;; Check if we need to translate the link
  7153. (let ((tmp (funcall org-link-translation-function type path)))
  7154. (setq type (car tmp) path (cdr tmp))))
  7155. (cond
  7156. ((assoc type org-link-protocols)
  7157. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7158. ((equal type "mailto")
  7159. (let ((cmd (car org-link-mailto-program))
  7160. (args (cdr org-link-mailto-program)) args1
  7161. (address path) (subject "") a)
  7162. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7163. (setq address (match-string 1 path)
  7164. subject (org-link-escape (match-string 2 path))))
  7165. (while args
  7166. (cond
  7167. ((not (stringp (car args))) (push (pop args) args1))
  7168. (t (setq a (pop args))
  7169. (if (string-match "%a" a)
  7170. (setq a (replace-match address t t a)))
  7171. (if (string-match "%s" a)
  7172. (setq a (replace-match subject t t a)))
  7173. (push a args1))))
  7174. (apply cmd (nreverse args1))))
  7175. ((member type '("http" "https" "ftp" "news"))
  7176. (browse-url (concat type ":" (org-link-escape
  7177. path org-link-escape-chars-browser))))
  7178. ((member type '("message"))
  7179. (browse-url (concat type ":" path)))
  7180. ((string= type "tags")
  7181. (org-tags-view in-emacs path))
  7182. ((string= type "thisfile")
  7183. (if in-emacs
  7184. (switch-to-buffer-other-window
  7185. (org-get-buffer-for-internal-link (current-buffer)))
  7186. (org-mark-ring-push))
  7187. (let ((cmd `(org-link-search
  7188. ,path
  7189. ,(cond ((equal in-emacs '(4)) 'occur)
  7190. ((equal in-emacs '(16)) 'org-occur)
  7191. (t nil))
  7192. ,pos)))
  7193. (condition-case nil (eval cmd)
  7194. (error (progn (widen) (eval cmd))))))
  7195. ((string= type "tree-match")
  7196. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7197. ((string= type "file")
  7198. (if (string-match "::\\([0-9]+\\)\\'" path)
  7199. (setq line (string-to-number (match-string 1 path))
  7200. path (substring path 0 (match-beginning 0)))
  7201. (if (string-match "::\\(.+\\)\\'" path)
  7202. (setq search (match-string 1 path)
  7203. path (substring path 0 (match-beginning 0)))))
  7204. (if (string-match "[*?{]" (file-name-nondirectory path))
  7205. (dired path)
  7206. (org-open-file path in-emacs line search)))
  7207. ((string= type "news")
  7208. (require 'org-gnus)
  7209. (org-gnus-follow-link path))
  7210. ((string= type "shell")
  7211. (let ((cmd path))
  7212. (if (or (not org-confirm-shell-link-function)
  7213. (funcall org-confirm-shell-link-function
  7214. (format "Execute \"%s\" in shell? "
  7215. (org-add-props cmd nil
  7216. 'face 'org-warning))))
  7217. (progn
  7218. (message "Executing %s" cmd)
  7219. (shell-command cmd))
  7220. (error "Abort"))))
  7221. ((string= type "elisp")
  7222. (let ((cmd path))
  7223. (if (or (not org-confirm-elisp-link-function)
  7224. (funcall org-confirm-elisp-link-function
  7225. (format "Execute \"%s\" as elisp? "
  7226. (org-add-props cmd nil
  7227. 'face 'org-warning))))
  7228. (message "%s => %s" cmd
  7229. (if (equal (string-to-char cmd) ?\()
  7230. (eval (read cmd))
  7231. (call-interactively (read cmd))))
  7232. (error "Abort"))))
  7233. (t
  7234. (browse-url-at-point))))))
  7235. (move-marker org-open-link-marker nil)
  7236. (run-hook-with-args 'org-follow-link-hook)))
  7237. (defun org-offer-links-in-entry (&optional nth)
  7238. "Offer links in the curren entry and follow the selected link.
  7239. If there is only one link, follow it immediately as well.
  7240. If NTH is an integer immediately pick the NTH link found."
  7241. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7242. "\\(" org-angle-link-re "\\)\\|"
  7243. "\\(" org-plain-link-re "\\)"))
  7244. (cnt 0)
  7245. (in-emacs (if (integerp nth) nil nth))
  7246. end
  7247. links link c)
  7248. (save-excursion
  7249. (org-back-to-heading t)
  7250. (setq end (save-excursion (outline-next-heading) (point)))
  7251. (while (re-search-forward re end t)
  7252. (push (match-string 0) links))
  7253. (setq links (reverse links)))
  7254. (cond
  7255. ((null links) (error "No links"))
  7256. ((equal (length links) 1)
  7257. (setq link (car links)))
  7258. ((and (integerp nth) (>= (length links) nth))
  7259. (setq link (nth (1- nth) links)))
  7260. (t ; we have to select a link
  7261. (save-excursion
  7262. (save-window-excursion
  7263. (delete-other-windows)
  7264. (with-output-to-temp-buffer "*Select Link*"
  7265. (princ "Select link\n\n")
  7266. (mapc (lambda (l) (princ (format "[%d] %s\n" (incf cnt) l)))
  7267. links))
  7268. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7269. (message "Select link to open:")
  7270. (setq c (read-char-exclusive))
  7271. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7272. (setq nth (- c ?0))
  7273. (unless (and (integerp nth) (>= (length links) nth))
  7274. (error "Invalid link selection"))
  7275. (setq link (nth (1- nth) links))))
  7276. (org-open-link-from-string link in-emacs (current-buffer))))
  7277. ;;;; Time estimates
  7278. (defun org-get-effort (&optional pom)
  7279. "Get the effort estimate for the current entry."
  7280. (org-entry-get pom org-effort-property))
  7281. ;;; File search
  7282. (defvar org-create-file-search-functions nil
  7283. "List of functions to construct the right search string for a file link.
  7284. These functions are called in turn with point at the location to
  7285. which the link should point.
  7286. A function in the hook should first test if it would like to
  7287. handle this file type, for example by checking the major-mode or
  7288. the file extension. If it decides not to handle this file, it
  7289. should just return nil to give other functions a chance. If it
  7290. does handle the file, it must return the search string to be used
  7291. when following the link. The search string will be part of the
  7292. file link, given after a double colon, and `org-open-at-point'
  7293. will automatically search for it. If special measures must be
  7294. taken to make the search successful, another function should be
  7295. added to the companion hook `org-execute-file-search-functions',
  7296. which see.
  7297. A function in this hook may also use `setq' to set the variable
  7298. `description' to provide a suggestion for the descriptive text to
  7299. be used for this link when it gets inserted into an Org-mode
  7300. buffer with \\[org-insert-link].")
  7301. (defvar org-execute-file-search-functions nil
  7302. "List of functions to execute a file search triggered by a link.
  7303. Functions added to this hook must accept a single argument, the
  7304. search string that was part of the file link, the part after the
  7305. double colon. The function must first check if it would like to
  7306. handle this search, for example by checking the major-mode or the
  7307. file extension. If it decides not to handle this search, it
  7308. should just return nil to give other functions a chance. If it
  7309. does handle the search, it must return a non-nil value to keep
  7310. other functions from trying.
  7311. Each function can access the current prefix argument through the
  7312. variable `current-prefix-argument'. Note that a single prefix is
  7313. used to force opening a link in Emacs, so it may be good to only
  7314. use a numeric or double prefix to guide the search function.
  7315. In case this is needed, a function in this hook can also restore
  7316. the window configuration before `org-open-at-point' was called using:
  7317. (set-window-configuration org-window-config-before-follow-link)")
  7318. (defun org-link-search (s &optional type avoid-pos)
  7319. "Search for a link search option.
  7320. If S is surrounded by forward slashes, it is interpreted as a
  7321. regular expression. In org-mode files, this will create an `org-occur'
  7322. sparse tree. In ordinary files, `occur' will be used to list matches.
  7323. If the current buffer is in `dired-mode', grep will be used to search
  7324. in all files. If AVOID-POS is given, ignore matches near that position."
  7325. (let ((case-fold-search t)
  7326. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7327. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7328. (append '(("") (" ") ("\t") ("\n"))
  7329. org-emphasis-alist)
  7330. "\\|") "\\)"))
  7331. (pos (point))
  7332. (pre nil) (post nil)
  7333. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7334. (cond
  7335. ;; First check if there are any special
  7336. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7337. ;; Now try the builtin stuff
  7338. ((and (equal (string-to-char s0) ?#)
  7339. (> (length s0) 1)
  7340. (save-excursion
  7341. (goto-char (point-min))
  7342. (and
  7343. (re-search-forward
  7344. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7345. (setq type 'dedicated
  7346. pos (match-beginning 0))))
  7347. ;; There is an exact target for this
  7348. (goto-char pos)
  7349. (org-back-to-heading t)))
  7350. ((save-excursion
  7351. (goto-char (point-min))
  7352. (and
  7353. (re-search-forward
  7354. (concat "<<" (regexp-quote s0) ">>") nil t)
  7355. (setq type 'dedicated
  7356. pos (match-beginning 0))))
  7357. ;; There is an exact target for this
  7358. (goto-char pos))
  7359. ((and (string-match "^(\\(.*\\))$" s0)
  7360. (save-excursion
  7361. (goto-char (point-min))
  7362. (and
  7363. (re-search-forward
  7364. (concat "[^[]" (regexp-quote
  7365. (format org-coderef-label-format
  7366. (match-string 1 s0))))
  7367. nil t)
  7368. (setq type 'dedicated
  7369. pos (1+ (match-beginning 0))))))
  7370. ;; There is a coderef target for this
  7371. (goto-char pos))
  7372. ((string-match "^/\\(.*\\)/$" s)
  7373. ;; A regular expression
  7374. (cond
  7375. ((org-mode-p)
  7376. (org-occur (match-string 1 s)))
  7377. ;;((eq major-mode 'dired-mode)
  7378. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7379. (t (org-do-occur (match-string 1 s)))))
  7380. (t
  7381. ;; A normal search strings
  7382. (when (equal (string-to-char s) ?*)
  7383. ;; Anchor on headlines, post may include tags.
  7384. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7385. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7386. s (substring s 1)))
  7387. (remove-text-properties
  7388. 0 (length s)
  7389. '(face nil mouse-face nil keymap nil fontified nil) s)
  7390. ;; Make a series of regular expressions to find a match
  7391. (setq words (org-split-string s "[ \n\r\t]+")
  7392. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7393. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7394. "\\)" markers)
  7395. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7396. re2a (concat "[ \t\r\n]" re2a_)
  7397. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7398. re4 (concat "[^a-zA-Z_]" re4_)
  7399. re1 (concat pre re2 post)
  7400. re3 (concat pre (if pre re4_ re4) post)
  7401. re5 (concat pre ".*" re4)
  7402. re2 (concat pre re2)
  7403. re2a (concat pre (if pre re2a_ re2a))
  7404. re4 (concat pre (if pre re4_ re4))
  7405. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7406. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7407. re5 "\\)"
  7408. ))
  7409. (cond
  7410. ((eq type 'org-occur) (org-occur reall))
  7411. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7412. (t (goto-char (point-min))
  7413. (setq type 'fuzzy)
  7414. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7415. (org-search-not-self 1 re1 nil t)
  7416. (org-search-not-self 1 re2 nil t)
  7417. (org-search-not-self 1 re2a nil t)
  7418. (org-search-not-self 1 re3 nil t)
  7419. (org-search-not-self 1 re4 nil t)
  7420. (org-search-not-self 1 re5 nil t)
  7421. )
  7422. (goto-char (match-beginning 1))
  7423. (goto-char pos)
  7424. (error "No match")))))
  7425. (t
  7426. ;; Normal string-search
  7427. (goto-char (point-min))
  7428. (if (search-forward s nil t)
  7429. (goto-char (match-beginning 0))
  7430. (error "No match"))))
  7431. (and (org-mode-p) (org-show-context 'link-search))
  7432. type))
  7433. (defun org-search-not-self (group &rest args)
  7434. "Execute `re-search-forward', but only accept matches that do not
  7435. enclose the position of `org-open-link-marker'."
  7436. (let ((m org-open-link-marker))
  7437. (catch 'exit
  7438. (while (apply 're-search-forward args)
  7439. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7440. (goto-char (match-end group))
  7441. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7442. (> (match-beginning 0) (marker-position m))
  7443. (< (match-end 0) (marker-position m)))
  7444. (save-match-data
  7445. (or (not (org-in-regexp
  7446. org-bracket-link-analytic-regexp 1))
  7447. (not (match-end 4)) ; no description
  7448. (and (<= (match-beginning 4) (point))
  7449. (>= (match-end 4) (point))))))
  7450. (throw 'exit (point))))))))
  7451. (defun org-get-buffer-for-internal-link (buffer)
  7452. "Return a buffer to be used for displaying the link target of internal links."
  7453. (cond
  7454. ((not org-display-internal-link-with-indirect-buffer)
  7455. buffer)
  7456. ((string-match "(Clone)$" (buffer-name buffer))
  7457. (message "Buffer is already a clone, not making another one")
  7458. ;; we also do not modify visibility in this case
  7459. buffer)
  7460. (t ; make a new indirect buffer for displaying the link
  7461. (let* ((bn (buffer-name buffer))
  7462. (ibn (concat bn "(Clone)"))
  7463. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7464. (with-current-buffer ib (org-overview))
  7465. ib))))
  7466. (defun org-do-occur (regexp &optional cleanup)
  7467. "Call the Emacs command `occur'.
  7468. If CLEANUP is non-nil, remove the printout of the regular expression
  7469. in the *Occur* buffer. This is useful if the regex is long and not useful
  7470. to read."
  7471. (occur regexp)
  7472. (when cleanup
  7473. (let ((cwin (selected-window)) win beg end)
  7474. (when (setq win (get-buffer-window "*Occur*"))
  7475. (select-window win))
  7476. (goto-char (point-min))
  7477. (when (re-search-forward "match[a-z]+" nil t)
  7478. (setq beg (match-end 0))
  7479. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7480. (setq end (1- (match-beginning 0)))))
  7481. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7482. (goto-char (point-min))
  7483. (select-window cwin))))
  7484. ;;; The mark ring for links jumps
  7485. (defvar org-mark-ring nil
  7486. "Mark ring for positions before jumps in Org-mode.")
  7487. (defvar org-mark-ring-last-goto nil
  7488. "Last position in the mark ring used to go back.")
  7489. ;; Fill and close the ring
  7490. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7491. (loop for i from 1 to org-mark-ring-length do
  7492. (push (make-marker) org-mark-ring))
  7493. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7494. org-mark-ring)
  7495. (defun org-mark-ring-push (&optional pos buffer)
  7496. "Put the current position or POS into the mark ring and rotate it."
  7497. (interactive)
  7498. (setq pos (or pos (point)))
  7499. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7500. (move-marker (car org-mark-ring)
  7501. (or pos (point))
  7502. (or buffer (current-buffer)))
  7503. (message "%s"
  7504. (substitute-command-keys
  7505. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7506. (defun org-mark-ring-goto (&optional n)
  7507. "Jump to the previous position in the mark ring.
  7508. With prefix arg N, jump back that many stored positions. When
  7509. called several times in succession, walk through the entire ring.
  7510. Org-mode commands jumping to a different position in the current file,
  7511. or to another Org-mode file, automatically push the old position
  7512. onto the ring."
  7513. (interactive "p")
  7514. (let (p m)
  7515. (if (eq last-command this-command)
  7516. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7517. (setq p org-mark-ring))
  7518. (setq org-mark-ring-last-goto p)
  7519. (setq m (car p))
  7520. (switch-to-buffer (marker-buffer m))
  7521. (goto-char m)
  7522. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7523. (defun org-remove-angle-brackets (s)
  7524. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7525. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7526. s)
  7527. (defun org-add-angle-brackets (s)
  7528. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7529. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7530. s)
  7531. (defun org-remove-double-quotes (s)
  7532. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7533. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7534. s)
  7535. ;;; Following specific links
  7536. (defun org-follow-timestamp-link ()
  7537. (cond
  7538. ((org-at-date-range-p t)
  7539. (let ((org-agenda-start-on-weekday)
  7540. (t1 (match-string 1))
  7541. (t2 (match-string 2)))
  7542. (setq t1 (time-to-days (org-time-string-to-time t1))
  7543. t2 (time-to-days (org-time-string-to-time t2)))
  7544. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7545. ((org-at-timestamp-p t)
  7546. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7547. (substring (match-string 1) 0 10)))
  7548. 1))
  7549. (t (error "This should not happen"))))
  7550. ;;; Following file links
  7551. (defvar org-wait nil)
  7552. (defun org-open-file (path &optional in-emacs line search)
  7553. "Open the file at PATH.
  7554. First, this expands any special file name abbreviations. Then the
  7555. configuration variable `org-file-apps' is checked if it contains an
  7556. entry for this file type, and if yes, the corresponding command is launched.
  7557. If no application is found, Emacs simply visits the file.
  7558. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7559. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7560. and o use an external application to visit the file.
  7561. Optional LINE specifies a line to go to, optional SEARCH a string to
  7562. search for. If LINE or SEARCH is given, the file will always be
  7563. opened in Emacs.
  7564. If the file does not exist, an error is thrown."
  7565. (setq in-emacs (or in-emacs line search))
  7566. (let* ((file (if (equal path "")
  7567. buffer-file-name
  7568. (substitute-in-file-name (expand-file-name path))))
  7569. (apps (append org-file-apps (org-default-apps)))
  7570. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7571. (dirp (if remp nil (file-directory-p file)))
  7572. (file (if (and dirp org-open-directory-means-index-dot-org)
  7573. (concat (file-name-as-directory file) "index.org")
  7574. file))
  7575. (a-m-a-p (assq 'auto-mode apps))
  7576. (dfile (downcase file))
  7577. (old-buffer (current-buffer))
  7578. (old-pos (point))
  7579. (old-mode major-mode)
  7580. ext cmd)
  7581. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7582. (setq ext (match-string 1 dfile))
  7583. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7584. (setq ext (match-string 1 dfile))))
  7585. (cond
  7586. ((equal in-emacs '(16))
  7587. (setq cmd (cdr (assoc 'system apps))))
  7588. (in-emacs (setq cmd 'emacs))
  7589. (t
  7590. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7591. (and dirp (cdr (assoc 'directory apps)))
  7592. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7593. 'string-match)
  7594. (cdr (assoc ext apps))
  7595. (cdr (assoc t apps))))))
  7596. (when (eq cmd 'system)
  7597. (setq cmd (cdr (assoc 'system apps))))
  7598. (when (eq cmd 'default)
  7599. (setq cmd (cdr (assoc t apps))))
  7600. (when (eq cmd 'mailcap)
  7601. (require 'mailcap)
  7602. (mailcap-parse-mailcaps)
  7603. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7604. (command (mailcap-mime-info mime-type)))
  7605. (if (stringp command)
  7606. (setq cmd command)
  7607. (setq cmd 'emacs))))
  7608. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7609. (not (file-exists-p file))
  7610. (not org-open-non-existing-files))
  7611. (error "No such file: %s" file))
  7612. (cond
  7613. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7614. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7615. (while (string-match "['\"]%s['\"]" cmd)
  7616. (setq cmd (replace-match "%s" t t cmd)))
  7617. (while (string-match "%s" cmd)
  7618. (setq cmd (replace-match
  7619. (save-match-data
  7620. (shell-quote-argument
  7621. (convert-standard-filename file)))
  7622. t t cmd)))
  7623. (save-window-excursion
  7624. (start-process-shell-command cmd nil cmd)
  7625. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7626. ))
  7627. ((or (stringp cmd)
  7628. (eq cmd 'emacs))
  7629. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7630. (widen)
  7631. (if line (org-goto-line line)
  7632. (if search (org-link-search search))))
  7633. ((consp cmd)
  7634. (let ((file (convert-standard-filename file)))
  7635. (eval cmd)))
  7636. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7637. (and (org-mode-p) (eq old-mode 'org-mode)
  7638. (or (not (equal old-buffer (current-buffer)))
  7639. (not (equal old-pos (point))))
  7640. (org-mark-ring-push old-pos old-buffer))))
  7641. (defun org-default-apps ()
  7642. "Return the default applications for this operating system."
  7643. (cond
  7644. ((eq system-type 'darwin)
  7645. org-file-apps-defaults-macosx)
  7646. ((eq system-type 'windows-nt)
  7647. org-file-apps-defaults-windowsnt)
  7648. (t org-file-apps-defaults-gnu)))
  7649. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7650. "Convert extensions to regular expressions in the cars of LIST.
  7651. Also, weed out any non-string entries, because the return value is used
  7652. only for regexp matching.
  7653. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7654. point to the symbol `emacs', indicating that the file should
  7655. be opened in Emacs."
  7656. (append
  7657. (delq nil
  7658. (mapcar (lambda (x)
  7659. (if (not (stringp (car x)))
  7660. nil
  7661. (if (string-match "\\W" (car x))
  7662. x
  7663. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7664. list))
  7665. (if add-auto-mode
  7666. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7667. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7668. (defun org-file-remote-p (file)
  7669. "Test whether FILE specifies a location on a remote system.
  7670. Return non-nil if the location is indeed remote.
  7671. For example, the filename \"/user@host:/foo\" specifies a location
  7672. on the system \"/user@host:\"."
  7673. (cond ((fboundp 'file-remote-p)
  7674. (file-remote-p file))
  7675. ((fboundp 'tramp-handle-file-remote-p)
  7676. (tramp-handle-file-remote-p file))
  7677. ((and (boundp 'ange-ftp-name-format)
  7678. (string-match (car ange-ftp-name-format) file))
  7679. t)
  7680. (t nil)))
  7681. ;;;; Refiling
  7682. (defun org-get-org-file ()
  7683. "Read a filename, with default directory `org-directory'."
  7684. (let ((default (or org-default-notes-file remember-data-file)))
  7685. (read-file-name (format "File name [%s]: " default)
  7686. (file-name-as-directory org-directory)
  7687. default)))
  7688. (defun org-notes-order-reversed-p ()
  7689. "Check if the current file should receive notes in reversed order."
  7690. (cond
  7691. ((not org-reverse-note-order) nil)
  7692. ((eq t org-reverse-note-order) t)
  7693. ((not (listp org-reverse-note-order)) nil)
  7694. (t (catch 'exit
  7695. (let ((all org-reverse-note-order)
  7696. entry)
  7697. (while (setq entry (pop all))
  7698. (if (string-match (car entry) buffer-file-name)
  7699. (throw 'exit (cdr entry))))
  7700. nil)))))
  7701. (defvar org-refile-target-table nil
  7702. "The list of refile targets, created by `org-refile'.")
  7703. (defvar org-agenda-new-buffers nil
  7704. "Buffers created to visit agenda files.")
  7705. (defun org-get-refile-targets (&optional default-buffer)
  7706. "Produce a table with refile targets."
  7707. (let ((case-fold-search nil)
  7708. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7709. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7710. targets txt re files f desc descre fast-path-p level pos0)
  7711. (message "Getting targets...")
  7712. (with-current-buffer (or default-buffer (current-buffer))
  7713. (while (setq entry (pop entries))
  7714. (setq files (car entry) desc (cdr entry))
  7715. (setq fast-path-p nil)
  7716. (cond
  7717. ((null files) (setq files (list (current-buffer))))
  7718. ((eq files 'org-agenda-files)
  7719. (setq files (org-agenda-files 'unrestricted)))
  7720. ((and (symbolp files) (fboundp files))
  7721. (setq files (funcall files)))
  7722. ((and (symbolp files) (boundp files))
  7723. (setq files (symbol-value files))))
  7724. (if (stringp files) (setq files (list files)))
  7725. (cond
  7726. ((eq (car desc) :tag)
  7727. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7728. ((eq (car desc) :todo)
  7729. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7730. ((eq (car desc) :regexp)
  7731. (setq descre (cdr desc)))
  7732. ((eq (car desc) :level)
  7733. (setq descre (concat "^\\*\\{" (number-to-string
  7734. (if org-odd-levels-only
  7735. (1- (* 2 (cdr desc)))
  7736. (cdr desc)))
  7737. "\\}[ \t]")))
  7738. ((eq (car desc) :maxlevel)
  7739. (setq fast-path-p t)
  7740. (setq descre (concat "^\\*\\{1," (number-to-string
  7741. (if org-odd-levels-only
  7742. (1- (* 2 (cdr desc)))
  7743. (cdr desc)))
  7744. "\\}[ \t]")))
  7745. (t (error "Bad refiling target description %s" desc)))
  7746. (while (setq f (pop files))
  7747. (save-excursion
  7748. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7749. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7750. (setq f (expand-file-name f))
  7751. (if (eq org-refile-use-outline-path 'file)
  7752. (push (list (file-name-nondirectory f) f nil nil) targets))
  7753. (save-excursion
  7754. (save-restriction
  7755. (widen)
  7756. (goto-char (point-min))
  7757. (while (re-search-forward descre nil t)
  7758. (goto-char (setq pos0 (point-at-bol)))
  7759. (catch 'next
  7760. (when org-refile-target-verify-function
  7761. (save-match-data
  7762. (or (funcall org-refile-target-verify-function)
  7763. (throw 'next t))))
  7764. (when (looking-at org-complex-heading-regexp)
  7765. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7766. txt (org-link-display-format (match-string 4))
  7767. re (concat "^" (regexp-quote
  7768. (buffer-substring (match-beginning 1)
  7769. (match-end 4)))))
  7770. (if (match-end 5) (setq re (concat re "[ \t]+"
  7771. (regexp-quote
  7772. (match-string 5)))))
  7773. (setq re (concat re "[ \t]*$"))
  7774. (when org-refile-use-outline-path
  7775. (setq txt (mapconcat 'org-protect-slash
  7776. (append
  7777. (if (eq org-refile-use-outline-path 'file)
  7778. (list (file-name-nondirectory
  7779. (buffer-file-name (buffer-base-buffer))))
  7780. (if (eq org-refile-use-outline-path 'full-file-path)
  7781. (list (buffer-file-name (buffer-base-buffer)))))
  7782. (org-get-outline-path fast-path-p level txt)
  7783. (list txt))
  7784. "/")))
  7785. (push (list txt f re (point)) targets)))
  7786. (when (= (point) pos0)
  7787. ;; verification function has not moved point
  7788. (goto-char (point-at-eol))))))))))
  7789. (message "Getting targets...done")
  7790. (nreverse targets)))
  7791. (defun org-protect-slash (s)
  7792. (while (string-match "/" s)
  7793. (setq s (replace-match "\\" t t s)))
  7794. s)
  7795. (defvar org-olpa (make-vector 20 nil))
  7796. (defun org-get-outline-path (&optional fastp level heading)
  7797. "Return the outline path to the current entry, as a list."
  7798. (if fastp
  7799. (progn
  7800. (if (> level 19)
  7801. (error "Outline path failure, more than 19 levels."))
  7802. (loop for i from level upto 19 do
  7803. (aset org-olpa i nil))
  7804. (prog1
  7805. (delq nil (append org-olpa nil))
  7806. (aset org-olpa level heading)))
  7807. (let (rtn)
  7808. (save-excursion
  7809. (while (org-up-heading-safe)
  7810. (when (looking-at org-complex-heading-regexp)
  7811. (push (org-match-string-no-properties 4) rtn)))
  7812. rtn))))
  7813. (defvar org-refile-history nil
  7814. "History for refiling operations.")
  7815. (defvar org-after-refile-insert-hook nil
  7816. "Hook run after `org-refile' has inserted its stuff at the new location.
  7817. Note that this is still *before* the stuff will be removed from
  7818. the *old* location.")
  7819. (defun org-refile (&optional goto default-buffer rfloc)
  7820. "Move the entry at point to another heading.
  7821. The list of target headings is compiled using the information in
  7822. `org-refile-targets', which see. This list is created before each use
  7823. and will therefore always be up-to-date.
  7824. At the target location, the entry is filed as a subitem of the target heading.
  7825. Depending on `org-reverse-note-order', the new subitem will either be the
  7826. first or the last subitem.
  7827. If there is an active region, all entries in that region will be moved.
  7828. However, the region must fulfil the requirement that the first heading
  7829. is the first one sets the top-level of the moved text - at most siblings
  7830. below it are allowed.
  7831. With prefix arg GOTO, the command will only visit the target location,
  7832. not actually move anything.
  7833. With a double prefix `C-u C-u', go to the location where the last refiling
  7834. operation has put the subtree.
  7835. RFLOC can be a refile location obtained in a different way.
  7836. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7837. (interactive "P")
  7838. (let* ((cbuf (current-buffer))
  7839. (regionp (org-region-active-p))
  7840. (region-start (and regionp (region-beginning)))
  7841. (region-end (and regionp (region-end)))
  7842. (region-length (and regionp (- region-end region-start)))
  7843. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7844. pos it nbuf file re level reversed)
  7845. (when regionp
  7846. (goto-char region-start)
  7847. (or (bolp) (goto-char (point-at-bol)))
  7848. (setq region-start (point))
  7849. (unless (org-kill-is-subtree-p
  7850. (buffer-substring region-start region-end))
  7851. (error "The region is not a (sequence of) subtree(s)")))
  7852. (if (equal goto '(16))
  7853. (org-refile-goto-last-stored)
  7854. (when (setq it (or rfloc
  7855. (save-excursion
  7856. (org-refile-get-location
  7857. (if goto "Goto: " "Refile to: ") default-buffer
  7858. org-refile-allow-creating-parent-nodes))))
  7859. (setq file (nth 1 it)
  7860. re (nth 2 it)
  7861. pos (nth 3 it))
  7862. (if (and (not goto)
  7863. pos
  7864. (equal (buffer-file-name) file)
  7865. (if regionp
  7866. (and (>= pos region-start)
  7867. (<= pos region-end))
  7868. (and (>= pos (point))
  7869. (< pos (save-excursion
  7870. (org-end-of-subtree t t))))))
  7871. (error "Cannot refile to position inside the tree or region"))
  7872. (setq nbuf (or (find-buffer-visiting file)
  7873. (find-file-noselect file)))
  7874. (if goto
  7875. (progn
  7876. (switch-to-buffer nbuf)
  7877. (goto-char pos)
  7878. (org-show-context 'org-goto))
  7879. (if regionp
  7880. (progn
  7881. (org-kill-new (buffer-substring region-start region-end))
  7882. (org-save-markers-in-region region-start region-end))
  7883. (org-copy-subtree 1 nil t))
  7884. (save-excursion
  7885. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7886. (find-file-noselect file))))
  7887. (setq reversed (org-notes-order-reversed-p))
  7888. (save-excursion
  7889. (save-restriction
  7890. (widen)
  7891. (if pos
  7892. (progn
  7893. (goto-char pos)
  7894. (looking-at outline-regexp)
  7895. (setq level (org-get-valid-level (funcall outline-level) 1))
  7896. (goto-char
  7897. (if reversed
  7898. (or (outline-next-heading) (point-max))
  7899. (or (save-excursion (org-get-next-sibling))
  7900. (org-end-of-subtree t t)
  7901. (point-max)))))
  7902. (setq level 1)
  7903. (if (not reversed)
  7904. (goto-char (point-max))
  7905. (goto-char (point-min))
  7906. (or (outline-next-heading) (goto-char (point-max)))))
  7907. (if (not (bolp)) (newline))
  7908. (bookmark-set "org-refile-last-stored")
  7909. (org-paste-subtree level)
  7910. (if (fboundp 'deactivate-mark) (deactivate-mark))
  7911. (run-hooks 'org-after-refile-insert-hook))))
  7912. (if regionp
  7913. (delete-region (point) (+ (point) region-length))
  7914. (org-cut-subtree))
  7915. (when (featurep 'org-inlinetask)
  7916. (org-inlinetask-remove-END-maybe))
  7917. (setq org-markers-to-move nil)
  7918. (message "Refiled to \"%s\"" (car it))))))
  7919. (org-reveal))
  7920. (defun org-refile-goto-last-stored ()
  7921. "Go to the location where the last refile was stored."
  7922. (interactive)
  7923. (bookmark-jump "org-refile-last-stored")
  7924. (message "This is the location of the last refile"))
  7925. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7926. "Prompt the user for a refile location, using PROMPT."
  7927. (let ((org-refile-targets org-refile-targets)
  7928. (org-refile-use-outline-path org-refile-use-outline-path))
  7929. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7930. (unless org-refile-target-table
  7931. (error "No refile targets"))
  7932. (let* ((cbuf (current-buffer))
  7933. (partial-completion-mode nil)
  7934. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7935. (cfunc (if (and org-refile-use-outline-path
  7936. org-outline-path-complete-in-steps)
  7937. 'org-olpath-completing-read
  7938. 'org-icompleting-read))
  7939. (extra (if org-refile-use-outline-path "/" ""))
  7940. (filename (and cfn (expand-file-name cfn)))
  7941. (tbl (mapcar
  7942. (lambda (x)
  7943. (if (and (not (member org-refile-use-outline-path
  7944. '(file full-file-path)))
  7945. (not (equal filename (nth 1 x))))
  7946. (cons (concat (car x) extra " ("
  7947. (file-name-nondirectory (nth 1 x)) ")")
  7948. (cdr x))
  7949. (cons (concat (car x) extra) (cdr x))))
  7950. org-refile-target-table))
  7951. (completion-ignore-case t)
  7952. pa answ parent-target child parent old-hist)
  7953. (setq old-hist org-refile-history)
  7954. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7955. nil 'org-refile-history))
  7956. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7957. (if pa
  7958. (progn
  7959. (when (or (not org-refile-history)
  7960. (not (eq old-hist org-refile-history))
  7961. (not (equal (car pa) (car org-refile-history))))
  7962. (setq org-refile-history
  7963. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  7964. org-refile-history
  7965. (cdr org-refile-history))))
  7966. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  7967. (pop org-refile-history)))
  7968. pa)
  7969. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  7970. (setq parent (match-string 1 answ)
  7971. child (match-string 2 answ))
  7972. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  7973. (when (and parent-target
  7974. (or (eq new-nodes t)
  7975. (and (eq new-nodes 'confirm)
  7976. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  7977. (org-refile-new-child parent-target child))))))
  7978. (defun org-refile-new-child (parent-target child)
  7979. "Use refile target PARENT-TARGET to add new CHILD below it."
  7980. (unless parent-target
  7981. (error "Cannot find parent for new node"))
  7982. (let ((file (nth 1 parent-target))
  7983. (pos (nth 3 parent-target))
  7984. level)
  7985. (with-current-buffer (or (find-buffer-visiting file)
  7986. (find-file-noselect file))
  7987. (save-excursion
  7988. (save-restriction
  7989. (widen)
  7990. (if pos
  7991. (goto-char pos)
  7992. (goto-char (point-max))
  7993. (if (not (bolp)) (newline)))
  7994. (when (looking-at outline-regexp)
  7995. (setq level (funcall outline-level))
  7996. (org-end-of-subtree t t))
  7997. (org-back-over-empty-lines)
  7998. (insert "\n" (make-string
  7999. (if pos (org-get-valid-level level 1) 1) ?*)
  8000. " " child "\n")
  8001. (beginning-of-line 0)
  8002. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8003. (defun org-olpath-completing-read (prompt collection &rest args)
  8004. "Read an outline path like a file name."
  8005. (let ((thetable collection)
  8006. (org-completion-use-ido nil) ; does not work with ido.
  8007. (org-completion-use-iswitchb nil)) ; or iswitchb
  8008. (apply
  8009. 'org-icompleting-read prompt
  8010. (lambda (string predicate &optional flag)
  8011. (let (rtn r f (l (length string)))
  8012. (cond
  8013. ((eq flag nil)
  8014. ;; try completion
  8015. (try-completion string thetable))
  8016. ((eq flag t)
  8017. ;; all-completions
  8018. (setq rtn (all-completions string thetable predicate))
  8019. (mapcar
  8020. (lambda (x)
  8021. (setq r (substring x l))
  8022. (if (string-match " ([^)]*)$" x)
  8023. (setq f (match-string 0 x))
  8024. (setq f ""))
  8025. (if (string-match "/" r)
  8026. (concat string (substring r 0 (match-end 0)) f)
  8027. x))
  8028. rtn))
  8029. ((eq flag 'lambda)
  8030. ;; exact match?
  8031. (assoc string thetable)))
  8032. ))
  8033. args)))
  8034. ;;;; Dynamic blocks
  8035. (defun org-find-dblock (name)
  8036. "Find the first dynamic block with name NAME in the buffer.
  8037. If not found, stay at current position and return nil."
  8038. (let (pos)
  8039. (save-excursion
  8040. (goto-char (point-min))
  8041. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8042. nil t)
  8043. (match-beginning 0))))
  8044. (if pos (goto-char pos))
  8045. pos))
  8046. (defconst org-dblock-start-re
  8047. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8048. "Matches the startline of a dynamic block, with parameters.")
  8049. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  8050. "Matches the end of a dynamic block.")
  8051. (defun org-create-dblock (plist)
  8052. "Create a dynamic block section, with parameters taken from PLIST.
  8053. PLIST must contain a :name entry which is used as name of the block."
  8054. (unless (bolp) (newline))
  8055. (let ((name (plist-get plist :name)))
  8056. (insert "#+BEGIN: " name)
  8057. (while plist
  8058. (if (eq (car plist) :name)
  8059. (setq plist (cddr plist))
  8060. (insert " " (prin1-to-string (pop plist)))))
  8061. (insert "\n\n#+END:\n")
  8062. (beginning-of-line -2)))
  8063. (defun org-prepare-dblock ()
  8064. "Prepare dynamic block for refresh.
  8065. This empties the block, puts the cursor at the insert position and returns
  8066. the property list including an extra property :name with the block name."
  8067. (unless (looking-at org-dblock-start-re)
  8068. (error "Not at a dynamic block"))
  8069. (let* ((begdel (1+ (match-end 0)))
  8070. (name (org-no-properties (match-string 1)))
  8071. (params (append (list :name name)
  8072. (read (concat "(" (match-string 3) ")")))))
  8073. (unless (re-search-forward org-dblock-end-re nil t)
  8074. (error "Dynamic block not terminated"))
  8075. (setq params
  8076. (append params
  8077. (list :content (buffer-substring
  8078. begdel (match-beginning 0)))))
  8079. (delete-region begdel (match-beginning 0))
  8080. (goto-char begdel)
  8081. (open-line 1)
  8082. params))
  8083. (defun org-map-dblocks (&optional command)
  8084. "Apply COMMAND to all dynamic blocks in the current buffer.
  8085. If COMMAND is not given, use `org-update-dblock'."
  8086. (let ((cmd (or command 'org-update-dblock))
  8087. pos)
  8088. (save-excursion
  8089. (goto-char (point-min))
  8090. (while (re-search-forward org-dblock-start-re nil t)
  8091. (goto-char (setq pos (match-beginning 0)))
  8092. (condition-case nil
  8093. (funcall cmd)
  8094. (error (message "Error during update of dynamic block")))
  8095. (goto-char pos)
  8096. (unless (re-search-forward org-dblock-end-re nil t)
  8097. (error "Dynamic block not terminated"))))))
  8098. (defun org-dblock-update (&optional arg)
  8099. "User command for updating dynamic blocks.
  8100. Update the dynamic block at point. With prefix ARG, update all dynamic
  8101. blocks in the buffer."
  8102. (interactive "P")
  8103. (if arg
  8104. (org-update-all-dblocks)
  8105. (or (looking-at org-dblock-start-re)
  8106. (org-beginning-of-dblock))
  8107. (org-update-dblock)))
  8108. (defun org-update-dblock ()
  8109. "Update the dynamic block at point
  8110. This means to empty the block, parse for parameters and then call
  8111. the correct writing function."
  8112. (save-window-excursion
  8113. (let* ((pos (point))
  8114. (line (org-current-line))
  8115. (params (org-prepare-dblock))
  8116. (name (plist-get params :name))
  8117. (cmd (intern (concat "org-dblock-write:" name))))
  8118. (message "Updating dynamic block `%s' at line %d..." name line)
  8119. (funcall cmd params)
  8120. (message "Updating dynamic block `%s' at line %d...done" name line)
  8121. (goto-char pos))))
  8122. (defun org-beginning-of-dblock ()
  8123. "Find the beginning of the dynamic block at point.
  8124. Error if there is no such block at point."
  8125. (let ((pos (point))
  8126. beg)
  8127. (end-of-line 1)
  8128. (if (and (re-search-backward org-dblock-start-re nil t)
  8129. (setq beg (match-beginning 0))
  8130. (re-search-forward org-dblock-end-re nil t)
  8131. (> (match-end 0) pos))
  8132. (goto-char beg)
  8133. (goto-char pos)
  8134. (error "Not in a dynamic block"))))
  8135. (defun org-update-all-dblocks ()
  8136. "Update all dynamic blocks in the buffer.
  8137. This function can be used in a hook."
  8138. (when (org-mode-p)
  8139. (org-map-dblocks 'org-update-dblock)))
  8140. ;;;; Completion
  8141. (defconst org-additional-option-like-keywords
  8142. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8143. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8144. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "ATTR_LaTeX"
  8145. "BEGIN:" "END:"
  8146. "ORGTBL" "TBLFM:" "TBLNAME:"
  8147. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8148. "BEGIN_QUOTE" "END_QUOTE"
  8149. "BEGIN_VERSE" "END_VERSE"
  8150. "BEGIN_CENTER" "END_CENTER"
  8151. "BEGIN_SRC" "END_SRC"
  8152. "CATEGORY" "COLUMNS"
  8153. "CAPTION" "LABEL"
  8154. "BIND"
  8155. "MACRO"))
  8156. (defcustom org-structure-template-alist
  8157. '(
  8158. ("s" "#+begin_src ?\n\n#+end_src"
  8159. "<src lang=\"?\">\n\n</src>")
  8160. ("e" "#+begin_example\n?\n#+end_example"
  8161. "<example>\n?\n</example>")
  8162. ("q" "#+begin_quote\n?\n#+end_quote"
  8163. "<quote>\n?\n</quote>")
  8164. ("v" "#+begin_verse\n?\n#+end_verse"
  8165. "<verse>\n?\n/verse>")
  8166. ("c" "#+begin_center\n?\n#+end_center"
  8167. "<center>\n?\n/center>")
  8168. ("l" "#+begin_latex\n?\n#+end_latex"
  8169. "<literal style=\"latex\">\n?\n</literal>")
  8170. ("L" "#+latex: "
  8171. "<literal style=\"latex\">?</literal>")
  8172. ("h" "#+begin_html\n?\n#+end_html"
  8173. "<literal style=\"html\">\n?\n</literal>")
  8174. ("H" "#+html: "
  8175. "<literal style=\"html\">?</literal>")
  8176. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8177. ("A" "#+ascii: ")
  8178. ("i" "#+include %file ?"
  8179. "<include file=%file markup=\"?\">")
  8180. )
  8181. "Structure completion elements.
  8182. This is a list of abbreviation keys and values. The value gets inserted
  8183. it you type @samp{.} followed by the key and then the completion key,
  8184. usually `M-TAB'. %file will be replaced by a file name after prompting
  8185. for the file using completion.
  8186. There are two templates for each key, the first uses the original Org syntax,
  8187. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8188. the default when the /org-mtags.el/ module has been loaded. See also the
  8189. variable `org-mtags-prefer-muse-templates'.
  8190. This is an experimental feature, it is undecided if it is going to stay in."
  8191. :group 'org-completion
  8192. :type '(repeat
  8193. (string :tag "Key")
  8194. (string :tag "Template")
  8195. (string :tag "Muse Template")))
  8196. (defun org-try-structure-completion ()
  8197. "Try to complete a structure template before point.
  8198. This looks for strings like \"<e\" on an otherwise empty line and
  8199. expands them."
  8200. (let ((l (buffer-substring (point-at-bol) (point)))
  8201. a)
  8202. (when (and (looking-at "[ \t]*$")
  8203. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8204. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8205. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8206. (match-beginning 1)) a)
  8207. t)))
  8208. (defun org-complete-expand-structure-template (start cell)
  8209. "Expand a structure template."
  8210. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8211. (rpl (nth (if musep 2 1) cell))
  8212. (ind ""))
  8213. (delete-region start (point))
  8214. (when (string-match "\\`#\\+" rpl)
  8215. (cond
  8216. ((bolp))
  8217. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8218. (setq ind (buffer-substring (point-at-bol) (point))))
  8219. (t (newline))))
  8220. (setq start (point))
  8221. (if (string-match "%file" rpl)
  8222. (setq rpl (replace-match
  8223. (concat
  8224. "\""
  8225. (save-match-data
  8226. (abbreviate-file-name (read-file-name "Include file: ")))
  8227. "\"")
  8228. t t rpl)))
  8229. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8230. (concat "\n" ind)))
  8231. (insert rpl)
  8232. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8233. (defun org-complete (&optional arg)
  8234. "Perform completion on word at point.
  8235. At the beginning of a headline, this completes TODO keywords as given in
  8236. `org-todo-keywords'.
  8237. If the current word is preceded by a backslash, completes the TeX symbols
  8238. that are supported for HTML support.
  8239. If the current word is preceded by \"#+\", completes special words for
  8240. setting file options.
  8241. In the line after \"#+STARTUP:, complete valid keywords.\"
  8242. At all other locations, this simply calls the value of
  8243. `org-completion-fallback-command'."
  8244. (interactive "P")
  8245. (org-without-partial-completion
  8246. (catch 'exit
  8247. (let* ((a nil)
  8248. (end (point))
  8249. (beg1 (save-excursion
  8250. (skip-chars-backward (org-re "[:alnum:]_@"))
  8251. (point)))
  8252. (beg (save-excursion
  8253. (skip-chars-backward "a-zA-Z0-9_:$")
  8254. (point)))
  8255. (confirm (lambda (x) (stringp (car x))))
  8256. (searchhead (equal (char-before beg) ?*))
  8257. (struct
  8258. (when (and (member (char-before beg1) '(?. ?<))
  8259. (setq a (assoc (buffer-substring beg1 (point))
  8260. org-structure-template-alist)))
  8261. (org-complete-expand-structure-template (1- beg1) a)
  8262. (throw 'exit t)))
  8263. (tag (and (equal (char-before beg1) ?:)
  8264. (equal (char-after (point-at-bol)) ?*)))
  8265. (prop (and (equal (char-before beg1) ?:)
  8266. (not (equal (char-after (point-at-bol)) ?*))))
  8267. (texp (equal (char-before beg) ?\\))
  8268. (link (equal (char-before beg) ?\[))
  8269. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8270. beg)
  8271. "#+"))
  8272. (startup (string-match "^#\\+STARTUP:.*"
  8273. (buffer-substring (point-at-bol) (point))))
  8274. (completion-ignore-case opt)
  8275. (type nil)
  8276. (tbl nil)
  8277. (table (cond
  8278. (opt
  8279. (setq type :opt)
  8280. (require 'org-exp)
  8281. (append
  8282. (delq nil
  8283. (mapcar
  8284. (lambda (x)
  8285. (if (string-match
  8286. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8287. (cons (match-string 2 x)
  8288. (match-string 1 x))))
  8289. (org-split-string (org-get-current-options) "\n")))
  8290. (mapcar 'list org-additional-option-like-keywords)))
  8291. (startup
  8292. (setq type :startup)
  8293. org-startup-options)
  8294. (link (append org-link-abbrev-alist-local
  8295. org-link-abbrev-alist))
  8296. (texp
  8297. (setq type :tex)
  8298. org-html-entities)
  8299. ((string-match "\\`\\*+[ \t]+\\'"
  8300. (buffer-substring (point-at-bol) beg))
  8301. (setq type :todo)
  8302. (mapcar 'list org-todo-keywords-1))
  8303. (searchhead
  8304. (setq type :searchhead)
  8305. (save-excursion
  8306. (goto-char (point-min))
  8307. (while (re-search-forward org-todo-line-regexp nil t)
  8308. (push (list
  8309. (org-make-org-heading-search-string
  8310. (match-string 3) t))
  8311. tbl)))
  8312. tbl)
  8313. (tag (setq type :tag beg beg1)
  8314. (or org-tag-alist (org-get-buffer-tags)))
  8315. (prop (setq type :prop beg beg1)
  8316. (mapcar 'list (org-buffer-property-keys nil t t)))
  8317. (t (progn
  8318. (call-interactively org-completion-fallback-command)
  8319. (throw 'exit nil)))))
  8320. (pattern (buffer-substring-no-properties beg end))
  8321. (completion (try-completion pattern table confirm)))
  8322. (cond ((eq completion t)
  8323. (if (not (assoc (upcase pattern) table))
  8324. (message "Already complete")
  8325. (if (and (equal type :opt)
  8326. (not (member (car (assoc (upcase pattern) table))
  8327. org-additional-option-like-keywords)))
  8328. (insert (substring (cdr (assoc (upcase pattern) table))
  8329. (length pattern)))
  8330. (if (memq type '(:tag :prop)) (insert ":")))))
  8331. ((null completion)
  8332. (message "Can't find completion for \"%s\"" pattern)
  8333. (ding))
  8334. ((not (string= pattern completion))
  8335. (delete-region beg end)
  8336. (if (string-match " +$" completion)
  8337. (setq completion (replace-match "" t t completion)))
  8338. (insert completion)
  8339. (if (get-buffer-window "*Completions*")
  8340. (delete-window (get-buffer-window "*Completions*")))
  8341. (if (assoc completion table)
  8342. (if (eq type :todo) (insert " ")
  8343. (if (memq type '(:tag :prop)) (insert ":"))))
  8344. (if (and (equal type :opt) (assoc completion table))
  8345. (message "%s" (substitute-command-keys
  8346. "Press \\[org-complete] again to insert example settings"))))
  8347. (t
  8348. (message "Making completion list...")
  8349. (let ((list (sort (all-completions pattern table confirm)
  8350. 'string<)))
  8351. (with-output-to-temp-buffer "*Completions*"
  8352. (condition-case nil
  8353. ;; Protection needed for XEmacs and emacs 21
  8354. (display-completion-list list pattern)
  8355. (error (display-completion-list list)))))
  8356. (message "Making completion list...%s" "done")))))))
  8357. ;;;; TODO, DEADLINE, Comments
  8358. (defun org-toggle-comment ()
  8359. "Change the COMMENT state of an entry."
  8360. (interactive)
  8361. (save-excursion
  8362. (org-back-to-heading)
  8363. (let (case-fold-search)
  8364. (if (looking-at (concat outline-regexp
  8365. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8366. (replace-match "" t t nil 1)
  8367. (if (looking-at outline-regexp)
  8368. (progn
  8369. (goto-char (match-end 0))
  8370. (insert org-comment-string " ")))))))
  8371. (defvar org-last-todo-state-is-todo nil
  8372. "This is non-nil when the last TODO state change led to a TODO state.
  8373. If the last change removed the TODO tag or switched to DONE, then
  8374. this is nil.")
  8375. (defvar org-setting-tags nil) ; dynamically skipped
  8376. (defun org-parse-local-options (string var)
  8377. "Parse STRING for startup setting relevant for variable VAR."
  8378. (let ((rtn (symbol-value var))
  8379. e opts)
  8380. (save-match-data
  8381. (if (or (not string) (not (string-match "\\S-" string)))
  8382. rtn
  8383. (setq opts (delq nil (mapcar (lambda (x)
  8384. (setq e (assoc x org-startup-options))
  8385. (if (eq (nth 1 e) var) e nil))
  8386. (org-split-string string "[ \t]+"))))
  8387. (if (not opts)
  8388. rtn
  8389. (setq rtn nil)
  8390. (while (setq e (pop opts))
  8391. (if (not (nth 3 e))
  8392. (setq rtn (nth 2 e))
  8393. (if (not (listp rtn)) (setq rtn nil))
  8394. (push (nth 2 e) rtn)))
  8395. rtn)))))
  8396. (defvar org-todo-setup-filter-hook nil
  8397. "Hook for functions that pre-filter todo specs.
  8398. Each function takes a todo spec and returns either `nil' or the spec
  8399. transformed into canonical form." )
  8400. (defvar org-todo-get-default-hook nil
  8401. "Hook for functions that get a default item for todo.
  8402. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8403. `nil' or a string to be used for the todo mark." )
  8404. (defvar org-agenda-headline-snapshot-before-repeat)
  8405. (defun org-todo (&optional arg)
  8406. "Change the TODO state of an item.
  8407. The state of an item is given by a keyword at the start of the heading,
  8408. like
  8409. *** TODO Write paper
  8410. *** DONE Call mom
  8411. The different keywords are specified in the variable `org-todo-keywords'.
  8412. By default the available states are \"TODO\" and \"DONE\".
  8413. So for this example: when the item starts with TODO, it is changed to DONE.
  8414. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8415. DONE are present, add TODO at the beginning of the heading.
  8416. With C-u prefix arg, use completion to determine the new state.
  8417. With numeric prefix arg, switch to that state.
  8418. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8419. With a tripple C-u prefix, circumvent any state blocking.
  8420. For calling through lisp, arg is also interpreted in the following way:
  8421. 'none -> empty state
  8422. \"\"(empty string) -> switch to empty state
  8423. 'done -> switch to DONE
  8424. 'nextset -> switch to the next set of keywords
  8425. 'previousset -> switch to the previous set of keywords
  8426. \"WAITING\" -> switch to the specified keyword, but only if it
  8427. really is a member of `org-todo-keywords'."
  8428. (interactive "P")
  8429. (if (equal arg '(16)) (setq arg 'nextset))
  8430. (let ((org-blocker-hook org-blocker-hook)
  8431. (case-fold-search nil))
  8432. (when (equal arg '(64))
  8433. (setq arg nil org-blocker-hook nil))
  8434. (when (and org-blocker-hook
  8435. (or org-inhibit-blocking
  8436. (org-entry-get nil "NOBLOCKING")))
  8437. (setq org-blocker-hook nil))
  8438. (save-excursion
  8439. (catch 'exit
  8440. (org-back-to-heading)
  8441. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8442. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8443. (looking-at " *"))
  8444. (let* ((match-data (match-data))
  8445. (startpos (point-at-bol))
  8446. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8447. (org-log-done org-log-done)
  8448. (org-log-repeat org-log-repeat)
  8449. (org-todo-log-states org-todo-log-states)
  8450. (this (match-string 1))
  8451. (hl-pos (match-beginning 0))
  8452. (head (org-get-todo-sequence-head this))
  8453. (ass (assoc head org-todo-kwd-alist))
  8454. (interpret (nth 1 ass))
  8455. (done-word (nth 3 ass))
  8456. (final-done-word (nth 4 ass))
  8457. (last-state (or this ""))
  8458. (completion-ignore-case t)
  8459. (member (member this org-todo-keywords-1))
  8460. (tail (cdr member))
  8461. (state (cond
  8462. ((and org-todo-key-trigger
  8463. (or (and (equal arg '(4))
  8464. (eq org-use-fast-todo-selection 'prefix))
  8465. (and (not arg) org-use-fast-todo-selection
  8466. (not (eq org-use-fast-todo-selection
  8467. 'prefix)))))
  8468. ;; Use fast selection
  8469. (org-fast-todo-selection))
  8470. ((and (equal arg '(4))
  8471. (or (not org-use-fast-todo-selection)
  8472. (not org-todo-key-trigger)))
  8473. ;; Read a state with completion
  8474. (org-icompleting-read
  8475. "State: " (mapcar (lambda(x) (list x))
  8476. org-todo-keywords-1)
  8477. nil t))
  8478. ((eq arg 'right)
  8479. (if this
  8480. (if tail (car tail) nil)
  8481. (car org-todo-keywords-1)))
  8482. ((eq arg 'left)
  8483. (if (equal member org-todo-keywords-1)
  8484. nil
  8485. (if this
  8486. (nth (- (length org-todo-keywords-1)
  8487. (length tail) 2)
  8488. org-todo-keywords-1)
  8489. (org-last org-todo-keywords-1))))
  8490. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8491. (setq arg nil))) ; hack to fall back to cycling
  8492. (arg
  8493. ;; user or caller requests a specific state
  8494. (cond
  8495. ((equal arg "") nil)
  8496. ((eq arg 'none) nil)
  8497. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8498. ((eq arg 'nextset)
  8499. (or (car (cdr (member head org-todo-heads)))
  8500. (car org-todo-heads)))
  8501. ((eq arg 'previousset)
  8502. (let ((org-todo-heads (reverse org-todo-heads)))
  8503. (or (car (cdr (member head org-todo-heads)))
  8504. (car org-todo-heads))))
  8505. ((car (member arg org-todo-keywords-1)))
  8506. ((nth (1- (prefix-numeric-value arg))
  8507. org-todo-keywords-1))))
  8508. ((null member) (or head (car org-todo-keywords-1)))
  8509. ((equal this final-done-word) nil) ;; -> make empty
  8510. ((null tail) nil) ;; -> first entry
  8511. ((memq interpret '(type priority))
  8512. (if (eq this-command last-command)
  8513. (car tail)
  8514. (if (> (length tail) 0)
  8515. (or done-word (car org-done-keywords))
  8516. nil)))
  8517. (t
  8518. (car tail))))
  8519. (state (or
  8520. (run-hook-with-args-until-success
  8521. 'org-todo-get-default-hook state last-state)
  8522. state))
  8523. (next (if state (concat " " state " ") " "))
  8524. (change-plist (list :type 'todo-state-change :from this :to state
  8525. :position startpos))
  8526. dolog now-done-p)
  8527. (when org-blocker-hook
  8528. (setq org-last-todo-state-is-todo
  8529. (not (member this org-done-keywords)))
  8530. (unless (save-excursion
  8531. (save-match-data
  8532. (run-hook-with-args-until-failure
  8533. 'org-blocker-hook change-plist)))
  8534. (if (interactive-p)
  8535. (error "TODO state change from %s to %s blocked" this state)
  8536. ;; fail silently
  8537. (message "TODO state change from %s to %s blocked" this state)
  8538. (throw 'exit nil))))
  8539. (store-match-data match-data)
  8540. (replace-match next t t)
  8541. (unless (pos-visible-in-window-p hl-pos)
  8542. (message "TODO state changed to %s" (org-trim next)))
  8543. (unless head
  8544. (setq head (org-get-todo-sequence-head state)
  8545. ass (assoc head org-todo-kwd-alist)
  8546. interpret (nth 1 ass)
  8547. done-word (nth 3 ass)
  8548. final-done-word (nth 4 ass)))
  8549. (when (memq arg '(nextset previousset))
  8550. (message "Keyword-Set %d/%d: %s"
  8551. (- (length org-todo-sets) -1
  8552. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8553. (length org-todo-sets)
  8554. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8555. (setq org-last-todo-state-is-todo
  8556. (not (member state org-done-keywords)))
  8557. (setq now-done-p (and (member state org-done-keywords)
  8558. (not (member this org-done-keywords))))
  8559. (and logging (org-local-logging logging))
  8560. (when (and (or org-todo-log-states org-log-done)
  8561. (not (eq org-inhibit-logging t))
  8562. (not (memq arg '(nextset previousset))))
  8563. ;; we need to look at recording a time and note
  8564. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8565. (nth 2 (assoc this org-todo-log-states))))
  8566. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8567. (setq dolog 'time))
  8568. (when (and state
  8569. (member state org-not-done-keywords)
  8570. (not (member this org-not-done-keywords)))
  8571. ;; This is now a todo state and was not one before
  8572. ;; If there was a CLOSED time stamp, get rid of it.
  8573. (org-add-planning-info nil nil 'closed))
  8574. (when (and now-done-p org-log-done)
  8575. ;; It is now done, and it was not done before
  8576. (org-add-planning-info 'closed (org-current-time))
  8577. (if (and (not dolog) (eq 'note org-log-done))
  8578. (org-add-log-setup 'done state this 'findpos 'note)))
  8579. (when (and state dolog)
  8580. ;; This is a non-nil state, and we need to log it
  8581. (org-add-log-setup 'state state this 'findpos dolog)))
  8582. ;; Fixup tag positioning
  8583. (org-todo-trigger-tag-changes state)
  8584. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8585. (when org-provide-todo-statistics
  8586. (org-update-parent-todo-statistics))
  8587. (run-hooks 'org-after-todo-state-change-hook)
  8588. (if (and arg (not (member state org-done-keywords)))
  8589. (setq head (org-get-todo-sequence-head state)))
  8590. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8591. ;; Do we need to trigger a repeat?
  8592. (when now-done-p
  8593. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8594. ;; This is for the agenda, take a snapshot of the headline.
  8595. (save-match-data
  8596. (setq org-agenda-headline-snapshot-before-repeat
  8597. (org-get-heading))))
  8598. (org-auto-repeat-maybe state))
  8599. ;; Fixup cursor location if close to the keyword
  8600. (if (and (outline-on-heading-p)
  8601. (not (bolp))
  8602. (save-excursion (beginning-of-line 1)
  8603. (looking-at org-todo-line-regexp))
  8604. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8605. (progn
  8606. (goto-char (or (match-end 2) (match-end 1)))
  8607. (and (looking-at " ") (just-one-space))))
  8608. (when org-trigger-hook
  8609. (save-excursion
  8610. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8611. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8612. "Block turning an entry into a TODO, using the hierarchy.
  8613. This checks whether the current task should be blocked from state
  8614. changes. Such blocking occurs when:
  8615. 1. The task has children which are not all in a completed state.
  8616. 2. A task has a parent with the property :ORDERED:, and there
  8617. are siblings prior to the current task with incomplete
  8618. status.
  8619. 3. The parent of the task is blocked because it has siblings that should
  8620. be done first, or is child of a block grandparent TODO entry."
  8621. (catch 'dont-block
  8622. ;; If this is not a todo state change, or if this entry is already DONE,
  8623. ;; do not block
  8624. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8625. (member (plist-get change-plist :from)
  8626. (cons 'done org-done-keywords))
  8627. (member (plist-get change-plist :to)
  8628. (cons 'todo org-not-done-keywords)))
  8629. (throw 'dont-block t))
  8630. ;; If this task has children, and any are undone, it's blocked
  8631. (save-excursion
  8632. (org-back-to-heading t)
  8633. (let ((this-level (funcall outline-level)))
  8634. (outline-next-heading)
  8635. (let ((child-level (funcall outline-level)))
  8636. (while (and (not (eobp))
  8637. (> child-level this-level))
  8638. ;; this todo has children, check whether they are all
  8639. ;; completed
  8640. (if (and (not (org-entry-is-done-p))
  8641. (org-entry-is-todo-p))
  8642. (throw 'dont-block nil))
  8643. (outline-next-heading)
  8644. (setq child-level (funcall outline-level))))))
  8645. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8646. ;; any previous siblings are undone, it's blocked
  8647. (save-excursion
  8648. (org-back-to-heading t)
  8649. (let* ((pos (point))
  8650. (parent-pos (and (org-up-heading-safe) (point))))
  8651. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8652. (when (and (org-entry-get (point) "ORDERED")
  8653. (forward-line 1)
  8654. (re-search-forward org-not-done-heading-regexp pos t))
  8655. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8656. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8657. (while t
  8658. (goto-char parent-pos)
  8659. (if (not (looking-at org-not-done-heading-regexp))
  8660. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8661. (setq pos (point))
  8662. (setq parent-pos (and (org-up-heading-safe) (point)))
  8663. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8664. (when (and (org-entry-get (point) "ORDERED")
  8665. (forward-line 1)
  8666. (re-search-forward org-not-done-heading-regexp pos t))
  8667. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8668. (defcustom org-track-ordered-property-with-tag nil
  8669. "Should the ORDERED property also be shown as a tag?
  8670. The ORDERED property decides if an entry should require subtasks to be
  8671. completed in sequence. Since a property is not very visible, setting
  8672. this option means that toggling the ORDERED property with the command
  8673. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8674. not relevant for the behavior, but it makes things more visible.
  8675. Note that toggling the tag with tags commands will not change the property
  8676. and therefore not influence behavior!
  8677. This can be t, meaning the tag ORDERED should be used, It can also be a
  8678. string to select a different tag for this task."
  8679. :group 'org-todo
  8680. :type '(choice
  8681. (const :tag "No tracking" nil)
  8682. (const :tag "Track with ORDERED tag" t)
  8683. (string :tag "Use other tag")))
  8684. (defun org-toggle-ordered-property ()
  8685. "Toggle the ORDERED property of the current entry.
  8686. For better visibility, you can track the value of this property with a tag.
  8687. See variable `org-track-ordered-property-with-tag'."
  8688. (interactive)
  8689. (let* ((t1 org-track-ordered-property-with-tag)
  8690. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8691. (save-excursion
  8692. (org-back-to-heading)
  8693. (if (org-entry-get nil "ORDERED")
  8694. (progn
  8695. (org-delete-property "ORDERED")
  8696. (and tag (org-toggle-tag tag 'off))
  8697. (message "Subtasks can be completed in arbitrary order"))
  8698. (org-entry-put nil "ORDERED" "t")
  8699. (and tag (org-toggle-tag tag 'on))
  8700. (message "Subtasks must be completed in sequence")))))
  8701. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8702. (defun org-block-todo-from-checkboxes (change-plist)
  8703. "Block turning an entry into a TODO, using checkboxes.
  8704. This checks whether the current task should be blocked from state
  8705. changes because there are uncheckd boxes in this entry."
  8706. (catch 'dont-block
  8707. ;; If this is not a todo state change, or if this entry is already DONE,
  8708. ;; do not block
  8709. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8710. (member (plist-get change-plist :from)
  8711. (cons 'done org-done-keywords))
  8712. (member (plist-get change-plist :to)
  8713. (cons 'todo org-not-done-keywords)))
  8714. (throw 'dont-block t))
  8715. ;; If this task has checkboxes that are not checked, it's blocked
  8716. (save-excursion
  8717. (org-back-to-heading t)
  8718. (let ((beg (point)) end)
  8719. (outline-next-heading)
  8720. (setq end (point))
  8721. (goto-char beg)
  8722. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8723. end t)
  8724. (progn
  8725. (if (boundp 'org-blocked-by-checkboxes)
  8726. (setq org-blocked-by-checkboxes t))
  8727. (throw 'dont-block nil)))))
  8728. t)) ; do not block
  8729. (defvar org-entry-property-inherited-from) ;; defined below
  8730. (defun org-update-parent-todo-statistics ()
  8731. "Update any statistics cookie in the parent of the current headline.
  8732. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8733. the entire subtree and this will travel up the hierarchy and update
  8734. statistics everywhere."
  8735. (interactive)
  8736. (let* ((lim 0) prop
  8737. (recursive (or (not org-hierarchical-todo-statistics)
  8738. (string-match
  8739. "\\<recursive\\>"
  8740. (or (setq prop (org-entry-get
  8741. nil "COOKIE_DATA" 'inherit)) ""))))
  8742. (lim (or (and prop (marker-position
  8743. org-entry-property-inherited-from))
  8744. lim))
  8745. (first t)
  8746. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8747. level ltoggle l1
  8748. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8749. (catch 'exit
  8750. (save-excursion
  8751. (beginning-of-line 1)
  8752. (if (org-at-heading-p)
  8753. (setq ltoggle (funcall outline-level))
  8754. (error "This should not happen"))
  8755. (while (and (setq level (org-up-heading-safe))
  8756. (or recursive first)
  8757. (>= (point) lim))
  8758. (setq first nil)
  8759. (unless (and level
  8760. (not (string-match
  8761. "\\<checkbox\\>"
  8762. (downcase
  8763. (or (org-entry-get
  8764. nil "COOKIE_DATA")
  8765. "")))))
  8766. (throw 'exit nil))
  8767. (while (re-search-forward box-re (point-at-eol) t)
  8768. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8769. (setq is-percent (match-end 2))
  8770. (save-match-data
  8771. (unless (outline-next-heading) (throw 'exit nil))
  8772. (while (and (looking-at org-complex-heading-regexp)
  8773. (> (setq l1 (length (match-string 1))) level))
  8774. (setq kwd (and (or recursive (= l1 ltoggle))
  8775. (match-string 2)))
  8776. (if (or (eq org-provide-todo-statistics 'all-headlines)
  8777. (and (listp org-provide-todo-statistics)
  8778. (or (member kwd org-provide-todo-statistics)
  8779. (member kwd org-done-keywords))))
  8780. (setq cnt-all (1+ cnt-all))
  8781. (if (eq org-provide-todo-statistics t)
  8782. (and kwd (setq cnt-all (1+ cnt-all)))))
  8783. (and (member kwd org-done-keywords)
  8784. (setq cnt-done (1+ cnt-done)))
  8785. (outline-next-heading)))
  8786. (replace-match
  8787. (if is-percent
  8788. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8789. (format "[%d/%d]" cnt-done cnt-all)))))
  8790. (when cookie-present
  8791. (run-hook-with-args 'org-after-todo-statistics-hook
  8792. cnt-done (- cnt-all cnt-done)))))
  8793. (run-hooks 'org-todo-statistics-hook)))
  8794. (defvar org-after-todo-statistics-hook nil
  8795. "Hook that is called after a TODO statistics cookie has been updated.
  8796. Each function is called with two arguments: the number of not-done entries
  8797. and the number of done entries.
  8798. For example, the following function, when added to this hook, will switch
  8799. an entry to DONE when all children are done, and back to TODO when new
  8800. entries are set to a TODO status. Note that this hook is only called
  8801. when there is a statistics cookie in the headline!
  8802. (defun org-summary-todo (n-done n-not-done)
  8803. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8804. (let (org-log-done org-log-states) ; turn off logging
  8805. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8806. ")
  8807. (defvar org-todo-statistics-hook nil
  8808. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8809. This hook runs even if there is no statisics cookie present, in which case
  8810. `org-after-todo-statistics-hook' would not run.")
  8811. (defun org-todo-trigger-tag-changes (state)
  8812. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8813. (let ((l org-todo-state-tags-triggers)
  8814. changes)
  8815. (when (or (not state) (equal state ""))
  8816. (setq changes (append changes (cdr (assoc "" l)))))
  8817. (when (and (stringp state) (> (length state) 0))
  8818. (setq changes (append changes (cdr (assoc state l)))))
  8819. (when (member state org-not-done-keywords)
  8820. (setq changes (append changes (cdr (assoc 'todo l)))))
  8821. (when (member state org-done-keywords)
  8822. (setq changes (append changes (cdr (assoc 'done l)))))
  8823. (dolist (c changes)
  8824. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8825. (defun org-local-logging (value)
  8826. "Get logging settings from a property VALUE."
  8827. (let* (words w a)
  8828. ;; directly set the variables, they are already local.
  8829. (setq org-log-done nil
  8830. org-log-repeat nil
  8831. org-todo-log-states nil)
  8832. (setq words (org-split-string value))
  8833. (while (setq w (pop words))
  8834. (cond
  8835. ((setq a (assoc w org-startup-options))
  8836. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8837. (set (nth 1 a) (nth 2 a))))
  8838. ((setq a (org-extract-log-state-settings w))
  8839. (and (member (car a) org-todo-keywords-1)
  8840. (push a org-todo-log-states)))))))
  8841. (defun org-get-todo-sequence-head (kwd)
  8842. "Return the head of the TODO sequence to which KWD belongs.
  8843. If KWD is not set, check if there is a text property remembering the
  8844. right sequence."
  8845. (let (p)
  8846. (cond
  8847. ((not kwd)
  8848. (or (get-text-property (point-at-bol) 'org-todo-head)
  8849. (progn
  8850. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8851. nil (point-at-eol)))
  8852. (get-text-property p 'org-todo-head))))
  8853. ((not (member kwd org-todo-keywords-1))
  8854. (car org-todo-keywords-1))
  8855. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8856. (defun org-fast-todo-selection ()
  8857. "Fast TODO keyword selection with single keys.
  8858. Returns the new TODO keyword, or nil if no state change should occur."
  8859. (let* ((fulltable org-todo-key-alist)
  8860. (done-keywords org-done-keywords) ;; needed for the faces.
  8861. (maxlen (apply 'max (mapcar
  8862. (lambda (x)
  8863. (if (stringp (car x)) (string-width (car x)) 0))
  8864. fulltable)))
  8865. (expert nil)
  8866. (fwidth (+ maxlen 3 1 3))
  8867. (ncol (/ (- (window-width) 4) fwidth))
  8868. tg cnt e c tbl
  8869. groups ingroup)
  8870. (save-excursion
  8871. (save-window-excursion
  8872. (if expert
  8873. (set-buffer (get-buffer-create " *Org todo*"))
  8874. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8875. (erase-buffer)
  8876. (org-set-local 'org-done-keywords done-keywords)
  8877. (setq tbl fulltable cnt 0)
  8878. (while (setq e (pop tbl))
  8879. (cond
  8880. ((equal e '(:startgroup))
  8881. (push '() groups) (setq ingroup t)
  8882. (when (not (= cnt 0))
  8883. (setq cnt 0)
  8884. (insert "\n"))
  8885. (insert "{ "))
  8886. ((equal e '(:endgroup))
  8887. (setq ingroup nil cnt 0)
  8888. (insert "}\n"))
  8889. ((equal e '(:newline))
  8890. (when (not (= cnt 0))
  8891. (setq cnt 0)
  8892. (insert "\n")
  8893. (setq e (car tbl))
  8894. (while (equal (car tbl) '(:newline))
  8895. (insert "\n")
  8896. (setq tbl (cdr tbl)))))
  8897. (t
  8898. (setq tg (car e) c (cdr e))
  8899. (if ingroup (push tg (car groups)))
  8900. (setq tg (org-add-props tg nil 'face
  8901. (org-get-todo-face tg)))
  8902. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8903. (insert "[" c "] " tg (make-string
  8904. (- fwidth 4 (length tg)) ?\ ))
  8905. (when (= (setq cnt (1+ cnt)) ncol)
  8906. (insert "\n")
  8907. (if ingroup (insert " "))
  8908. (setq cnt 0)))))
  8909. (insert "\n")
  8910. (goto-char (point-min))
  8911. (if (not expert) (org-fit-window-to-buffer))
  8912. (message "[a-z..]:Set [SPC]:clear")
  8913. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8914. (cond
  8915. ((or (= c ?\C-g)
  8916. (and (= c ?q) (not (rassoc c fulltable))))
  8917. (setq quit-flag t))
  8918. ((= c ?\ ) nil)
  8919. ((setq e (rassoc c fulltable) tg (car e))
  8920. tg)
  8921. (t (setq quit-flag t)))))))
  8922. (defun org-entry-is-todo-p ()
  8923. (member (org-get-todo-state) org-not-done-keywords))
  8924. (defun org-entry-is-done-p ()
  8925. (member (org-get-todo-state) org-done-keywords))
  8926. (defun org-get-todo-state ()
  8927. (save-excursion
  8928. (org-back-to-heading t)
  8929. (and (looking-at org-todo-line-regexp)
  8930. (match-end 2)
  8931. (match-string 2))))
  8932. (defun org-at-date-range-p (&optional inactive-ok)
  8933. "Is the cursor inside a date range?"
  8934. (interactive)
  8935. (save-excursion
  8936. (catch 'exit
  8937. (let ((pos (point)))
  8938. (skip-chars-backward "^[<\r\n")
  8939. (skip-chars-backward "<[")
  8940. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8941. (>= (match-end 0) pos)
  8942. (throw 'exit t))
  8943. (skip-chars-backward "^<[\r\n")
  8944. (skip-chars-backward "<[")
  8945. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8946. (>= (match-end 0) pos)
  8947. (throw 'exit t)))
  8948. nil)))
  8949. (defun org-get-repeat ()
  8950. "Check if there is a deadline/schedule with repeater in this entry."
  8951. (save-match-data
  8952. (save-excursion
  8953. (org-back-to-heading t)
  8954. (if (re-search-forward
  8955. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8956. (match-string 1)))))
  8957. (defvar org-last-changed-timestamp)
  8958. (defvar org-last-inserted-timestamp)
  8959. (defvar org-log-post-message)
  8960. (defvar org-log-note-purpose)
  8961. (defvar org-log-note-how)
  8962. (defvar org-log-note-extra)
  8963. (defun org-auto-repeat-maybe (done-word)
  8964. "Check if the current headline contains a repeated deadline/schedule.
  8965. If yes, set TODO state back to what it was and change the base date
  8966. of repeating deadline/scheduled time stamps to new date.
  8967. This function is run automatically after each state change to a DONE state."
  8968. ;; last-state is dynamically scoped into this function
  8969. (let* ((repeat (org-get-repeat))
  8970. (aa (assoc last-state org-todo-kwd-alist))
  8971. (interpret (nth 1 aa))
  8972. (head (nth 2 aa))
  8973. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8974. (msg "Entry repeats: ")
  8975. (org-log-done nil)
  8976. (org-todo-log-states nil)
  8977. (nshiftmax 10) (nshift 0)
  8978. re type n what ts time)
  8979. (when repeat
  8980. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8981. (org-todo (if (eq interpret 'type) last-state head))
  8982. (org-entry-put nil "LAST_REPEAT" (format-time-string
  8983. (org-time-stamp-format t t)))
  8984. (when org-log-repeat
  8985. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8986. (memq 'org-add-log-note post-command-hook))
  8987. ;; OK, we are already setup for some record
  8988. (if (eq org-log-repeat 'note)
  8989. ;; make sure we take a note, not only a time stamp
  8990. (setq org-log-note-how 'note))
  8991. ;; Set up for taking a record
  8992. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8993. last-state
  8994. 'findpos org-log-repeat)))
  8995. (org-back-to-heading t)
  8996. (org-add-planning-info nil nil 'closed)
  8997. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8998. org-deadline-time-regexp "\\)\\|\\("
  8999. org-ts-regexp "\\)"))
  9000. (while (re-search-forward
  9001. re (save-excursion (outline-next-heading) (point)) t)
  9002. (setq type (if (match-end 1) org-scheduled-string
  9003. (if (match-end 3) org-deadline-string "Plain:"))
  9004. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9005. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9006. (setq n (string-to-number (match-string 2 ts))
  9007. what (match-string 3 ts))
  9008. (if (equal what "w") (setq n (* n 7) what "d"))
  9009. ;; Preparation, see if we need to modify the start date for the change
  9010. (when (match-end 1)
  9011. (setq time (save-match-data (org-time-string-to-time ts)))
  9012. (cond
  9013. ((equal (match-string 1 ts) ".")
  9014. ;; Shift starting date to today
  9015. (org-timestamp-change
  9016. (- (time-to-days (current-time)) (time-to-days time))
  9017. 'day))
  9018. ((equal (match-string 1 ts) "+")
  9019. (while (or (= nshift 0)
  9020. (<= (time-to-days time) (time-to-days (current-time))))
  9021. (when (= (incf nshift) nshiftmax)
  9022. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9023. (error "Abort")))
  9024. (org-timestamp-change n (cdr (assoc what whata)))
  9025. (org-at-timestamp-p t)
  9026. (setq ts (match-string 1))
  9027. (setq time (save-match-data (org-time-string-to-time ts))))
  9028. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9029. ;; rematch, so that we have everything in place for the real shift
  9030. (org-at-timestamp-p t)
  9031. (setq ts (match-string 1))
  9032. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9033. (org-timestamp-change n (cdr (assoc what whata)))
  9034. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9035. (setq org-log-post-message msg)
  9036. (message "%s" msg))))
  9037. (defun org-show-todo-tree (arg)
  9038. "Make a compact tree which shows all headlines marked with TODO.
  9039. The tree will show the lines where the regexp matches, and all higher
  9040. headlines above the match.
  9041. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9042. With a numeric prefix N, construct a sparse tree for the Nth element
  9043. of `org-todo-keywords-1'."
  9044. (interactive "P")
  9045. (let ((case-fold-search nil)
  9046. (kwd-re
  9047. (cond ((null arg) org-not-done-regexp)
  9048. ((equal arg '(4))
  9049. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9050. (mapcar 'list org-todo-keywords-1))))
  9051. (concat "\\("
  9052. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9053. "\\)\\>")))
  9054. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9055. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9056. org-todo-keywords-1)))
  9057. (t (error "Invalid prefix argument: %s" arg)))))
  9058. (message "%d TODO entries found"
  9059. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9060. (defun org-deadline (&optional remove time)
  9061. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9062. With argument REMOVE, remove any deadline from the item.
  9063. When TIME is set, it should be an internal time specification, and the
  9064. scheduling will use the corresponding date."
  9065. (interactive "P")
  9066. (if remove
  9067. (progn
  9068. (org-remove-timestamp-with-keyword org-deadline-string)
  9069. (message "Item no longer has a deadline."))
  9070. (if (org-get-repeat)
  9071. (error "Cannot change deadline on task with repeater, please do that by hand")
  9072. (org-add-planning-info 'deadline time 'closed)
  9073. (message "Deadline on %s" org-last-inserted-timestamp))))
  9074. (defun org-schedule (&optional remove time)
  9075. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9076. With argument REMOVE, remove any scheduling date from the item.
  9077. When TIME is set, it should be an internal time specification, and the
  9078. scheduling will use the corresponding date."
  9079. (interactive "P")
  9080. (if remove
  9081. (progn
  9082. (org-remove-timestamp-with-keyword org-scheduled-string)
  9083. (message "Item is no longer scheduled."))
  9084. (if (org-get-repeat)
  9085. (error "Cannot reschedule task with repeater, please do that by hand")
  9086. (org-add-planning-info 'scheduled time 'closed)
  9087. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9088. (defun org-get-scheduled-time (pom &optional inherit)
  9089. "Get the scheduled time as a time tuple, of a format suitable
  9090. for calling org-schedule with, or if there is no scheduling,
  9091. returns nil."
  9092. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9093. (when time
  9094. (apply 'encode-time (org-parse-time-string time)))))
  9095. (defun org-get-deadline-time (pom &optional inherit)
  9096. "Get the deadine as a time tuple, of a format suitable for
  9097. calling org-deadlin with, or if there is no scheduling, returns
  9098. nil."
  9099. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9100. (when time
  9101. (apply 'encode-time (org-parse-time-string time)))))
  9102. (defun org-remove-timestamp-with-keyword (keyword)
  9103. "Remove all time stamps with KEYWORD in the current entry."
  9104. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9105. beg)
  9106. (save-excursion
  9107. (org-back-to-heading t)
  9108. (setq beg (point))
  9109. (outline-next-heading)
  9110. (while (re-search-backward re beg t)
  9111. (replace-match "")
  9112. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9113. (equal (char-before) ?\ ))
  9114. (backward-delete-char 1)
  9115. (if (string-match "^[ \t]*$" (buffer-substring
  9116. (point-at-bol) (point-at-eol)))
  9117. (delete-region (point-at-bol)
  9118. (min (point-max) (1+ (point-at-eol))))))))))
  9119. (defun org-add-planning-info (what &optional time &rest remove)
  9120. "Insert new timestamp with keyword in the line directly after the headline.
  9121. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9122. If non is given, the user is prompted for a date.
  9123. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9124. be removed."
  9125. (interactive)
  9126. (let (org-time-was-given org-end-time-was-given ts
  9127. end default-time default-input)
  9128. (catch 'exit
  9129. (when (and (not time) (memq what '(scheduled deadline)))
  9130. ;; Try to get a default date/time from existing timestamp
  9131. (save-excursion
  9132. (org-back-to-heading t)
  9133. (setq end (save-excursion (outline-next-heading) (point)))
  9134. (when (re-search-forward (if (eq what 'scheduled)
  9135. org-scheduled-time-regexp
  9136. org-deadline-time-regexp)
  9137. end t)
  9138. (setq ts (match-string 1)
  9139. default-time
  9140. (apply 'encode-time (org-parse-time-string ts))
  9141. default-input (and ts (org-get-compact-tod ts))))))
  9142. (when what
  9143. ;; If necessary, get the time from the user
  9144. (setq time (or time (org-read-date nil 'to-time nil nil
  9145. default-time default-input))))
  9146. (when (and org-insert-labeled-timestamps-at-point
  9147. (member what '(scheduled deadline)))
  9148. (insert
  9149. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9150. (org-insert-time-stamp time org-time-was-given
  9151. nil nil nil (list org-end-time-was-given))
  9152. (setq what nil))
  9153. (save-excursion
  9154. (save-restriction
  9155. (let (col list elt ts buffer-invisibility-spec)
  9156. (org-back-to-heading t)
  9157. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9158. (goto-char (match-end 1))
  9159. (setq col (current-column))
  9160. (goto-char (match-end 0))
  9161. (if (eobp) (insert "\n") (forward-char 1))
  9162. (when (and (not what)
  9163. (not (looking-at
  9164. (concat "[ \t]*"
  9165. org-keyword-time-not-clock-regexp))))
  9166. ;; Nothing to add, nothing to remove...... :-)
  9167. (throw 'exit nil))
  9168. (if (and (not (looking-at outline-regexp))
  9169. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9170. "[^\r\n]*"))
  9171. (not (equal (match-string 1) org-clock-string)))
  9172. (narrow-to-region (match-beginning 0) (match-end 0))
  9173. (insert-before-markers "\n")
  9174. (backward-char 1)
  9175. (narrow-to-region (point) (point))
  9176. (and org-adapt-indentation (org-indent-to-column col)))
  9177. ;; Check if we have to remove something.
  9178. (setq list (cons what remove))
  9179. (while list
  9180. (setq elt (pop list))
  9181. (goto-char (point-min))
  9182. (when (or (and (eq elt 'scheduled)
  9183. (re-search-forward org-scheduled-time-regexp nil t))
  9184. (and (eq elt 'deadline)
  9185. (re-search-forward org-deadline-time-regexp nil t))
  9186. (and (eq elt 'closed)
  9187. (re-search-forward org-closed-time-regexp nil t)))
  9188. (replace-match "")
  9189. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9190. (if (looking-at " +") (replace-match ""))))
  9191. (goto-char (point-max))
  9192. (when what
  9193. (insert
  9194. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9195. (cond ((eq what 'scheduled) org-scheduled-string)
  9196. ((eq what 'deadline) org-deadline-string)
  9197. ((eq what 'closed) org-closed-string))
  9198. " ")
  9199. (setq ts (org-insert-time-stamp
  9200. time
  9201. (or org-time-was-given
  9202. (and (eq what 'closed) org-log-done-with-time))
  9203. (eq what 'closed)
  9204. nil nil (list org-end-time-was-given)))
  9205. (end-of-line 1))
  9206. (goto-char (point-min))
  9207. (widen)
  9208. (if (and (looking-at "[ \t]+\n")
  9209. (equal (char-before) ?\n))
  9210. (delete-region (1- (point)) (point-at-eol)))
  9211. ts))))))
  9212. (defvar org-log-note-marker (make-marker))
  9213. (defvar org-log-note-purpose nil)
  9214. (defvar org-log-note-state nil)
  9215. (defvar org-log-note-previous-state nil)
  9216. (defvar org-log-note-how nil)
  9217. (defvar org-log-note-extra nil)
  9218. (defvar org-log-note-window-configuration nil)
  9219. (defvar org-log-note-return-to (make-marker))
  9220. (defvar org-log-post-message nil
  9221. "Message to be displayed after a log note has been stored.
  9222. The auto-repeater uses this.")
  9223. (defun org-add-note ()
  9224. "Add a note to the current entry.
  9225. This is done in the same way as adding a state change note."
  9226. (interactive)
  9227. (org-add-log-setup 'note nil nil 'findpos nil))
  9228. (defvar org-property-end-re)
  9229. (defun org-add-log-setup (&optional purpose state prev-state
  9230. findpos how &optional extra)
  9231. "Set up the post command hook to take a note.
  9232. If this is about to TODO state change, the new state is expected in STATE.
  9233. When FINDPOS is non-nil, find the correct position for the note in
  9234. the current entry. If not, assume that it can be inserted at point.
  9235. HOW is an indicator what kind of note should be created.
  9236. EXTRA is additional text that will be inserted into the notes buffer."
  9237. (let* ((org-log-into-drawer (org-log-into-drawer))
  9238. (drawer (cond ((stringp org-log-into-drawer)
  9239. org-log-into-drawer)
  9240. (org-log-into-drawer "LOGBOOK")
  9241. (t nil))))
  9242. (save-restriction
  9243. (save-excursion
  9244. (when findpos
  9245. (org-back-to-heading t)
  9246. (narrow-to-region (point) (save-excursion
  9247. (outline-next-heading) (point)))
  9248. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9249. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9250. "[^\r\n]*\\)?"))
  9251. (goto-char (match-end 0))
  9252. (cond
  9253. (drawer
  9254. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9255. nil t)
  9256. (progn
  9257. (goto-char (match-end 0))
  9258. (or org-log-states-order-reversed
  9259. (and (re-search-forward org-property-end-re nil t)
  9260. (goto-char (1- (match-beginning 0))))))
  9261. (insert "\n:" drawer ":\n:END:")
  9262. (beginning-of-line 0)
  9263. (org-indent-line-function)
  9264. (beginning-of-line 2)
  9265. (org-indent-line-function)
  9266. (end-of-line 0)))
  9267. ((and org-log-state-notes-insert-after-drawers
  9268. (save-excursion
  9269. (forward-line) (looking-at org-drawer-regexp)))
  9270. (forward-line)
  9271. (while (looking-at org-drawer-regexp)
  9272. (goto-char (match-end 0))
  9273. (re-search-forward org-property-end-re (point-max) t)
  9274. (forward-line))
  9275. (forward-line -1)))
  9276. (unless org-log-states-order-reversed
  9277. (and (= (char-after) ?\n) (forward-char 1))
  9278. (org-skip-over-state-notes)
  9279. (skip-chars-backward " \t\n\r")))
  9280. (move-marker org-log-note-marker (point))
  9281. (setq org-log-note-purpose purpose
  9282. org-log-note-state state
  9283. org-log-note-previous-state prev-state
  9284. org-log-note-how how
  9285. org-log-note-extra extra)
  9286. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9287. (defun org-skip-over-state-notes ()
  9288. "Skip past the list of State notes in an entry."
  9289. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9290. (while (looking-at "[ \t]*- State")
  9291. (condition-case nil
  9292. (org-next-item)
  9293. (error (org-end-of-item)))))
  9294. (defun org-add-log-note (&optional purpose)
  9295. "Pop up a window for taking a note, and add this note later at point."
  9296. (remove-hook 'post-command-hook 'org-add-log-note)
  9297. (setq org-log-note-window-configuration (current-window-configuration))
  9298. (delete-other-windows)
  9299. (move-marker org-log-note-return-to (point))
  9300. (switch-to-buffer (marker-buffer org-log-note-marker))
  9301. (goto-char org-log-note-marker)
  9302. (org-switch-to-buffer-other-window "*Org Note*")
  9303. (erase-buffer)
  9304. (if (memq org-log-note-how '(time state))
  9305. (let (current-prefix-arg) (org-store-log-note))
  9306. (let ((org-inhibit-startup t)) (org-mode))
  9307. (insert (format "# Insert note for %s.
  9308. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9309. (cond
  9310. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9311. ((eq org-log-note-purpose 'done) "closed todo item")
  9312. ((eq org-log-note-purpose 'state)
  9313. (format "state change from \"%s\" to \"%s\""
  9314. (or org-log-note-previous-state "")
  9315. (or org-log-note-state "")))
  9316. ((eq org-log-note-purpose 'note)
  9317. "this entry")
  9318. (t (error "This should not happen")))))
  9319. (if org-log-note-extra (insert org-log-note-extra))
  9320. (org-set-local 'org-finish-function 'org-store-log-note)))
  9321. (defvar org-note-abort nil) ; dynamically scoped
  9322. (defun org-store-log-note ()
  9323. "Finish taking a log note, and insert it to where it belongs."
  9324. (let ((txt (buffer-string))
  9325. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9326. lines ind)
  9327. (kill-buffer (current-buffer))
  9328. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9329. (setq txt (replace-match "" t t txt)))
  9330. (if (string-match "\\s-+\\'" txt)
  9331. (setq txt (replace-match "" t t txt)))
  9332. (setq lines (org-split-string txt "\n"))
  9333. (when (and note (string-match "\\S-" note))
  9334. (setq note
  9335. (org-replace-escapes
  9336. note
  9337. (list (cons "%u" (user-login-name))
  9338. (cons "%U" user-full-name)
  9339. (cons "%t" (format-time-string
  9340. (org-time-stamp-format 'long 'inactive)
  9341. (current-time)))
  9342. (cons "%s" (if org-log-note-state
  9343. (concat "\"" org-log-note-state "\"")
  9344. ""))
  9345. (cons "%S" (if org-log-note-previous-state
  9346. (concat "\"" org-log-note-previous-state "\"")
  9347. "\"\"")))))
  9348. (if lines (setq note (concat note " \\\\")))
  9349. (push note lines))
  9350. (when (or current-prefix-arg org-note-abort)
  9351. (when org-log-into-drawer
  9352. (org-remove-empty-drawer-at
  9353. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9354. org-log-note-marker))
  9355. (setq lines nil))
  9356. (when lines
  9357. (save-excursion
  9358. (set-buffer (marker-buffer org-log-note-marker))
  9359. (save-excursion
  9360. (goto-char org-log-note-marker)
  9361. (move-marker org-log-note-marker nil)
  9362. (end-of-line 1)
  9363. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9364. (insert "- " (pop lines))
  9365. (org-indent-line-function)
  9366. (beginning-of-line 1)
  9367. (looking-at "[ \t]*")
  9368. (setq ind (concat (match-string 0) " "))
  9369. (end-of-line 1)
  9370. (while lines (insert "\n" ind (pop lines)))
  9371. (message "Note stored")
  9372. (org-back-to-heading t)
  9373. (org-cycle-hide-drawers 'children)))))
  9374. (set-window-configuration org-log-note-window-configuration)
  9375. (with-current-buffer (marker-buffer org-log-note-return-to)
  9376. (goto-char org-log-note-return-to))
  9377. (move-marker org-log-note-return-to nil)
  9378. (and org-log-post-message (message "%s" org-log-post-message)))
  9379. (defun org-remove-empty-drawer-at (drawer pos)
  9380. "Remove an emptyr DARWER drawer at position POS.
  9381. POS may also be a marker."
  9382. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9383. (save-excursion
  9384. (save-restriction
  9385. (widen)
  9386. (goto-char pos)
  9387. (if (org-in-regexp
  9388. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9389. (replace-match ""))))))
  9390. (defun org-sparse-tree (&optional arg)
  9391. "Create a sparse tree, prompt for the details.
  9392. This command can create sparse trees. You first need to select the type
  9393. of match used to create the tree:
  9394. t Show entries with a specific TODO keyword.
  9395. m Show entries selected by a tags/property match.
  9396. p Enter a property name and its value (both with completion on existing
  9397. names/values) and show entries with that property.
  9398. r Show entries matching a regular expression.
  9399. d Show deadlines due within `org-deadline-warning-days'.
  9400. b Show deadlines and scheduled items before a date.
  9401. a Show deadlines and scheduled items after a date."
  9402. (interactive "P")
  9403. (let (ans kwd value)
  9404. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9405. (setq ans (read-char-exclusive))
  9406. (cond
  9407. ((equal ans ?d)
  9408. (call-interactively 'org-check-deadlines))
  9409. ((equal ans ?b)
  9410. (call-interactively 'org-check-before-date))
  9411. ((equal ans ?a)
  9412. (call-interactively 'org-check-after-date))
  9413. ((equal ans ?t)
  9414. (org-show-todo-tree '(4)))
  9415. ((member ans '(?T ?m))
  9416. (call-interactively 'org-match-sparse-tree))
  9417. ((member ans '(?p ?P))
  9418. (setq kwd (org-icompleting-read "Property: "
  9419. (mapcar 'list (org-buffer-property-keys))))
  9420. (setq value (org-icompleting-read "Value: "
  9421. (mapcar 'list (org-property-values kwd))))
  9422. (unless (string-match "\\`{.*}\\'" value)
  9423. (setq value (concat "\"" value "\"")))
  9424. (org-match-sparse-tree arg (concat kwd "=" value)))
  9425. ((member ans '(?r ?R ?/))
  9426. (call-interactively 'org-occur))
  9427. (t (error "No such sparse tree command \"%c\"" ans)))))
  9428. (defvar org-occur-highlights nil
  9429. "List of overlays used for occur matches.")
  9430. (make-variable-buffer-local 'org-occur-highlights)
  9431. (defvar org-occur-parameters nil
  9432. "Parameters of the active org-occur calls.
  9433. This is a list, each call to org-occur pushes as cons cell,
  9434. containing the regular expression and the callback, onto the list.
  9435. The list can contain several entries if `org-occur' has been called
  9436. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9437. will only contain one set of parameters. When the highlights are
  9438. removed (for example with `C-c C-c', or with the next edit (depending
  9439. on `org-remove-highlights-with-change'), this variable is emptied
  9440. as well.")
  9441. (make-variable-buffer-local 'org-occur-parameters)
  9442. (defun org-occur (regexp &optional keep-previous callback)
  9443. "Make a compact tree which shows all matches of REGEXP.
  9444. The tree will show the lines where the regexp matches, and all higher
  9445. headlines above the match. It will also show the heading after the match,
  9446. to make sure editing the matching entry is easy.
  9447. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9448. call to `org-occur' will be kept, to allow stacking of calls to this
  9449. command.
  9450. If CALLBACK is non-nil, it is a function which is called to confirm
  9451. that the match should indeed be shown."
  9452. (interactive "sRegexp: \nP")
  9453. (when (equal regexp "")
  9454. (error "Regexp cannot be empty"))
  9455. (unless keep-previous
  9456. (org-remove-occur-highlights nil nil t))
  9457. (push (cons regexp callback) org-occur-parameters)
  9458. (let ((cnt 0))
  9459. (save-excursion
  9460. (goto-char (point-min))
  9461. (if (or (not keep-previous) ; do not want to keep
  9462. (not org-occur-highlights)) ; no previous matches
  9463. ;; hide everything
  9464. (org-overview))
  9465. (while (re-search-forward regexp nil t)
  9466. (when (or (not callback)
  9467. (save-match-data (funcall callback)))
  9468. (setq cnt (1+ cnt))
  9469. (when org-highlight-sparse-tree-matches
  9470. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9471. (org-show-context 'occur-tree))))
  9472. (when org-remove-highlights-with-change
  9473. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9474. nil 'local))
  9475. (unless org-sparse-tree-open-archived-trees
  9476. (org-hide-archived-subtrees (point-min) (point-max)))
  9477. (run-hooks 'org-occur-hook)
  9478. (if (interactive-p)
  9479. (message "%d match(es) for regexp %s" cnt regexp))
  9480. cnt))
  9481. (defun org-show-context (&optional key)
  9482. "Make sure point and context and visible.
  9483. How much context is shown depends upon the variables
  9484. `org-show-hierarchy-above', `org-show-following-heading'. and
  9485. `org-show-siblings'."
  9486. (let ((heading-p (org-on-heading-p t))
  9487. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9488. (following-p (org-get-alist-option org-show-following-heading key))
  9489. (entry-p (org-get-alist-option org-show-entry-below key))
  9490. (siblings-p (org-get-alist-option org-show-siblings key)))
  9491. (catch 'exit
  9492. ;; Show heading or entry text
  9493. (if (and heading-p (not entry-p))
  9494. (org-flag-heading nil) ; only show the heading
  9495. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9496. (org-show-hidden-entry))) ; show entire entry
  9497. (when following-p
  9498. ;; Show next sibling, or heading below text
  9499. (save-excursion
  9500. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9501. (org-flag-heading nil))))
  9502. (when siblings-p (org-show-siblings))
  9503. (when hierarchy-p
  9504. ;; show all higher headings, possibly with siblings
  9505. (save-excursion
  9506. (while (and (condition-case nil
  9507. (progn (org-up-heading-all 1) t)
  9508. (error nil))
  9509. (not (bobp)))
  9510. (org-flag-heading nil)
  9511. (when siblings-p (org-show-siblings))))))))
  9512. (defun org-reveal (&optional siblings)
  9513. "Show current entry, hierarchy above it, and the following headline.
  9514. This can be used to show a consistent set of context around locations
  9515. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9516. not t for the search context.
  9517. With optional argument SIBLINGS, on each level of the hierarchy all
  9518. siblings are shown. This repairs the tree structure to what it would
  9519. look like when opened with hierarchical calls to `org-cycle'."
  9520. (interactive "P")
  9521. (let ((org-show-hierarchy-above t)
  9522. (org-show-following-heading t)
  9523. (org-show-siblings (if siblings t org-show-siblings)))
  9524. (org-show-context nil)))
  9525. (defun org-highlight-new-match (beg end)
  9526. "Highlight from BEG to END and mark the highlight is an occur headline."
  9527. (let ((ov (org-make-overlay beg end)))
  9528. (org-overlay-put ov 'face 'secondary-selection)
  9529. (push ov org-occur-highlights)))
  9530. (defun org-remove-occur-highlights (&optional beg end noremove)
  9531. "Remove the occur highlights from the buffer.
  9532. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9533. from the `before-change-functions' in the current buffer."
  9534. (interactive)
  9535. (unless org-inhibit-highlight-removal
  9536. (mapc 'org-delete-overlay org-occur-highlights)
  9537. (setq org-occur-highlights nil)
  9538. (setq org-occur-parameters nil)
  9539. (unless noremove
  9540. (remove-hook 'before-change-functions
  9541. 'org-remove-occur-highlights 'local))))
  9542. ;;;; Priorities
  9543. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9544. "Regular expression matching the priority indicator.")
  9545. (defvar org-remove-priority-next-time nil)
  9546. (defun org-priority-up ()
  9547. "Increase the priority of the current item."
  9548. (interactive)
  9549. (org-priority 'up))
  9550. (defun org-priority-down ()
  9551. "Decrease the priority of the current item."
  9552. (interactive)
  9553. (org-priority 'down))
  9554. (defun org-priority (&optional action)
  9555. "Change the priority of an item by ARG.
  9556. ACTION can be `set', `up', `down', or a character."
  9557. (interactive)
  9558. (unless org-enable-priority-commands
  9559. (error "Priority commands are disabled"))
  9560. (setq action (or action 'set))
  9561. (let (current new news have remove)
  9562. (save-excursion
  9563. (org-back-to-heading t)
  9564. (if (looking-at org-priority-regexp)
  9565. (setq current (string-to-char (match-string 2))
  9566. have t)
  9567. (setq current org-default-priority))
  9568. (cond
  9569. ((or (eq action 'set)
  9570. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9571. (if (not (eq action 'set))
  9572. (setq new action)
  9573. (message "Priority %c-%c, SPC to remove: "
  9574. org-highest-priority org-lowest-priority)
  9575. (setq new (read-char-exclusive)))
  9576. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9577. (= (upcase org-lowest-priority) org-lowest-priority))
  9578. (setq new (upcase new)))
  9579. (cond ((equal new ?\ ) (setq remove t))
  9580. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9581. (error "Priority must be between `%c' and `%c'"
  9582. org-highest-priority org-lowest-priority))))
  9583. ((eq action 'up)
  9584. (if (and (not have) (eq last-command this-command))
  9585. (setq new org-lowest-priority)
  9586. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9587. org-default-priority (1- current)))))
  9588. ((eq action 'down)
  9589. (if (and (not have) (eq last-command this-command))
  9590. (setq new org-highest-priority)
  9591. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9592. org-default-priority (1+ current)))))
  9593. (t (error "Invalid action")))
  9594. (if (or (< (upcase new) org-highest-priority)
  9595. (> (upcase new) org-lowest-priority))
  9596. (setq remove t))
  9597. (setq news (format "%c" new))
  9598. (if have
  9599. (if remove
  9600. (replace-match "" t t nil 1)
  9601. (replace-match news t t nil 2))
  9602. (if remove
  9603. (error "No priority cookie found in line")
  9604. (let ((case-fold-search nil))
  9605. (looking-at org-todo-line-regexp))
  9606. (if (match-end 2)
  9607. (progn
  9608. (goto-char (match-end 2))
  9609. (insert " [#" news "]"))
  9610. (goto-char (match-beginning 3))
  9611. (insert "[#" news "] "))))
  9612. (org-preserve-lc (org-set-tags nil 'align)))
  9613. (if remove
  9614. (message "Priority removed")
  9615. (message "Priority of current item set to %s" news))))
  9616. (defun org-get-priority (s)
  9617. "Find priority cookie and return priority."
  9618. (save-match-data
  9619. (if (not (string-match org-priority-regexp s))
  9620. (* 1000 (- org-lowest-priority org-default-priority))
  9621. (* 1000 (- org-lowest-priority
  9622. (string-to-char (match-string 2 s)))))))
  9623. ;;;; Tags
  9624. (defvar org-agenda-archives-mode)
  9625. (defvar org-map-continue-from nil
  9626. "Position from where mapping should continue.
  9627. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9628. (defvar org-scanner-tags nil
  9629. "The current tag list while the tags scanner is running.")
  9630. (defvar org-trust-scanner-tags nil
  9631. "Should `org-get-tags-at' use the tags fro the scanner.
  9632. This is for internal dynamical scoping only.
  9633. When this is non-nil, the function `org-get-tags-at' will return the value
  9634. of `org-scanner-tags' instead of building the list by itself. This
  9635. can lead to large speed-ups when the tags scanner is used in a file with
  9636. many entries, and when the list of tags is retrieved, for example to
  9637. obtain a list of properties. Building the tags list for each entry in such
  9638. a file becomes an N^2 operation - but with this variable set, it scales
  9639. as N.")
  9640. (defun org-scan-tags (action matcher &optional todo-only)
  9641. "Scan headline tags with inheritance and produce output ACTION.
  9642. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9643. or `agenda' to produce an entry list for an agenda view. It can also be
  9644. a Lisp form or a function that should be called at each matched headline, in
  9645. this case the return value is a list of all return values from these calls.
  9646. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9647. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9648. only lines with a TODO keyword are included in the output."
  9649. (require 'org-agenda)
  9650. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9651. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9652. (org-re
  9653. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9654. (props (list 'face 'default
  9655. 'done-face 'org-agenda-done
  9656. 'undone-face 'default
  9657. 'mouse-face 'highlight
  9658. 'org-not-done-regexp org-not-done-regexp
  9659. 'org-todo-regexp org-todo-regexp
  9660. 'keymap org-agenda-keymap
  9661. 'help-echo
  9662. (format "mouse-2 or RET jump to org file %s"
  9663. (abbreviate-file-name
  9664. (or (buffer-file-name (buffer-base-buffer))
  9665. (buffer-name (buffer-base-buffer)))))))
  9666. (case-fold-search nil)
  9667. (org-map-continue-from nil)
  9668. lspos tags tags-list
  9669. (tags-alist (list (cons 0 org-file-tags)))
  9670. (llast 0) rtn rtn1 level category i txt
  9671. todo marker entry priority)
  9672. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9673. (setq action (list 'lambda nil action)))
  9674. (save-excursion
  9675. (goto-char (point-min))
  9676. (when (eq action 'sparse-tree)
  9677. (org-overview)
  9678. (org-remove-occur-highlights))
  9679. (while (re-search-forward re nil t)
  9680. (catch :skip
  9681. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9682. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9683. (goto-char (setq lspos (match-beginning 0)))
  9684. (setq level (org-reduced-level (funcall outline-level))
  9685. category (org-get-category))
  9686. (setq i llast llast level)
  9687. ;; remove tag lists from same and sublevels
  9688. (while (>= i level)
  9689. (when (setq entry (assoc i tags-alist))
  9690. (setq tags-alist (delete entry tags-alist)))
  9691. (setq i (1- i)))
  9692. ;; add the next tags
  9693. (when tags
  9694. (setq tags (org-split-string tags ":")
  9695. tags-alist
  9696. (cons (cons level tags) tags-alist)))
  9697. ;; compile tags for current headline
  9698. (setq tags-list
  9699. (if org-use-tag-inheritance
  9700. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9701. tags)
  9702. org-scanner-tags tags-list)
  9703. (when org-use-tag-inheritance
  9704. (setcdr (car tags-alist)
  9705. (mapcar (lambda (x)
  9706. (setq x (copy-sequence x))
  9707. (org-add-prop-inherited x))
  9708. (cdar tags-alist))))
  9709. (when (and tags org-use-tag-inheritance
  9710. (or (not (eq t org-use-tag-inheritance))
  9711. org-tags-exclude-from-inheritance))
  9712. ;; selective inheritance, remove uninherited ones
  9713. (setcdr (car tags-alist)
  9714. (org-remove-uniherited-tags (cdar tags-alist))))
  9715. (when (and (or (not todo-only)
  9716. (and (member todo org-not-done-keywords)
  9717. (or (not org-agenda-tags-todo-honor-ignore-options)
  9718. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9719. (let ((case-fold-search t)) (eval matcher))
  9720. (or
  9721. (not (member org-archive-tag tags-list))
  9722. ;; we have an archive tag, should we use this anyway?
  9723. (or (not org-agenda-skip-archived-trees)
  9724. (and (eq action 'agenda) org-agenda-archives-mode))))
  9725. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9726. ;; select this headline
  9727. (cond
  9728. ((eq action 'sparse-tree)
  9729. (and org-highlight-sparse-tree-matches
  9730. (org-get-heading) (match-end 0)
  9731. (org-highlight-new-match
  9732. (match-beginning 0) (match-beginning 1)))
  9733. (org-show-context 'tags-tree))
  9734. ((eq action 'agenda)
  9735. (setq txt (org-format-agenda-item
  9736. ""
  9737. (concat
  9738. (if (eq org-tags-match-list-sublevels 'indented)
  9739. (make-string (1- level) ?.) "")
  9740. (org-get-heading))
  9741. category
  9742. tags-list
  9743. )
  9744. priority (org-get-priority txt))
  9745. (goto-char lspos)
  9746. (setq marker (org-agenda-new-marker))
  9747. (org-add-props txt props
  9748. 'org-marker marker 'org-hd-marker marker 'org-category category
  9749. 'todo-state todo
  9750. 'priority priority 'type "tagsmatch")
  9751. (push txt rtn))
  9752. ((functionp action)
  9753. (setq org-map-continue-from nil)
  9754. (save-excursion
  9755. (setq rtn1 (funcall action))
  9756. (push rtn1 rtn)))
  9757. (t (error "Invalid action")))
  9758. ;; if we are to skip sublevels, jump to end of subtree
  9759. (unless org-tags-match-list-sublevels
  9760. (org-end-of-subtree t)
  9761. (backward-char 1))))
  9762. ;; Get the correct position from where to continue
  9763. (if org-map-continue-from
  9764. (goto-char org-map-continue-from)
  9765. (and (= (point) lspos) (end-of-line 1)))))
  9766. (when (and (eq action 'sparse-tree)
  9767. (not org-sparse-tree-open-archived-trees))
  9768. (org-hide-archived-subtrees (point-min) (point-max)))
  9769. (nreverse rtn)))
  9770. (defun org-remove-uniherited-tags (tags)
  9771. "Remove all tags that are not inherited from the list TAGS."
  9772. (cond
  9773. ((eq org-use-tag-inheritance t)
  9774. (if org-tags-exclude-from-inheritance
  9775. (org-delete-all org-tags-exclude-from-inheritance tags)
  9776. tags))
  9777. ((not org-use-tag-inheritance) nil)
  9778. ((stringp org-use-tag-inheritance)
  9779. (delq nil (mapcar
  9780. (lambda (x)
  9781. (if (and (string-match org-use-tag-inheritance x)
  9782. (not (member x org-tags-exclude-from-inheritance)))
  9783. x nil))
  9784. tags)))
  9785. ((listp org-use-tag-inheritance)
  9786. (delq nil (mapcar
  9787. (lambda (x)
  9788. (if (member x org-use-tag-inheritance) x nil))
  9789. tags)))))
  9790. (defvar todo-only) ;; dynamically scoped
  9791. (defun org-match-sparse-tree (&optional todo-only match)
  9792. "Create a sparse tree according to tags string MATCH.
  9793. MATCH can contain positive and negative selection of tags, like
  9794. \"+WORK+URGENT-WITHBOSS\".
  9795. If optional argument TODO-ONLY is non-nil, only select lines that are
  9796. also TODO lines."
  9797. (interactive "P")
  9798. (org-prepare-agenda-buffers (list (current-buffer)))
  9799. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9800. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9801. (defvar org-cached-props nil)
  9802. (defun org-cached-entry-get (pom property)
  9803. (if (or (eq t org-use-property-inheritance)
  9804. (and (stringp org-use-property-inheritance)
  9805. (string-match org-use-property-inheritance property))
  9806. (and (listp org-use-property-inheritance)
  9807. (member property org-use-property-inheritance)))
  9808. ;; Caching is not possible, check it directly
  9809. (org-entry-get pom property 'inherit)
  9810. ;; Get all properties, so that we can do complicated checks easily
  9811. (cdr (assoc property (or org-cached-props
  9812. (setq org-cached-props
  9813. (org-entry-properties pom)))))))
  9814. (defun org-global-tags-completion-table (&optional files)
  9815. "Return the list of all tags in all agenda buffer/files."
  9816. (save-excursion
  9817. (org-uniquify
  9818. (delq nil
  9819. (apply 'append
  9820. (mapcar
  9821. (lambda (file)
  9822. (set-buffer (find-file-noselect file))
  9823. (append (org-get-buffer-tags)
  9824. (mapcar (lambda (x) (if (stringp (car-safe x))
  9825. (list (car-safe x)) nil))
  9826. org-tag-alist)))
  9827. (if (and files (car files))
  9828. files
  9829. (org-agenda-files))))))))
  9830. (defun org-make-tags-matcher (match)
  9831. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9832. ;; todo-only is scoped dynamically into this function, and the function
  9833. ;; may change it if the matcher asks for it.
  9834. (unless match
  9835. ;; Get a new match request, with completion
  9836. (let ((org-last-tags-completion-table
  9837. (org-global-tags-completion-table)))
  9838. (setq match (org-completing-read-no-i
  9839. "Match: " 'org-tags-completion-function nil nil nil
  9840. 'org-tags-history))))
  9841. ;; Parse the string and create a lisp form
  9842. (let ((match0 match)
  9843. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9844. minus tag mm
  9845. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9846. orterms term orlist re-p str-p level-p level-op time-p
  9847. prop-p pn pv po cat-p gv rest)
  9848. (if (string-match "/+" match)
  9849. ;; match contains also a todo-matching request
  9850. (progn
  9851. (setq tagsmatch (substring match 0 (match-beginning 0))
  9852. todomatch (substring match (match-end 0)))
  9853. (if (string-match "^!" todomatch)
  9854. (setq todo-only t todomatch (substring todomatch 1)))
  9855. (if (string-match "^\\s-*$" todomatch)
  9856. (setq todomatch nil)))
  9857. ;; only matching tags
  9858. (setq tagsmatch match todomatch nil))
  9859. ;; Make the tags matcher
  9860. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9861. (setq tagsmatcher t)
  9862. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9863. (while (setq term (pop orterms))
  9864. (while (and (equal (substring term -1) "\\") orterms)
  9865. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9866. (while (string-match re term)
  9867. (setq rest (substring term (match-end 0))
  9868. minus (and (match-end 1)
  9869. (equal (match-string 1 term) "-"))
  9870. tag (match-string 2 term)
  9871. re-p (equal (string-to-char tag) ?{)
  9872. level-p (match-end 4)
  9873. prop-p (match-end 5)
  9874. mm (cond
  9875. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9876. (level-p
  9877. (setq level-op (org-op-to-function (match-string 3 term)))
  9878. `(,level-op level ,(string-to-number
  9879. (match-string 4 term))))
  9880. (prop-p
  9881. (setq pn (match-string 5 term)
  9882. po (match-string 6 term)
  9883. pv (match-string 7 term)
  9884. cat-p (equal pn "CATEGORY")
  9885. re-p (equal (string-to-char pv) ?{)
  9886. str-p (equal (string-to-char pv) ?\")
  9887. time-p (save-match-data
  9888. (string-match "^\"[[<].*[]>]\"$" pv))
  9889. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9890. (if time-p (setq pv (org-matcher-time pv)))
  9891. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9892. (cond
  9893. ((equal pn "CATEGORY")
  9894. (setq gv '(get-text-property (point) 'org-category)))
  9895. ((equal pn "TODO")
  9896. (setq gv 'todo))
  9897. (t
  9898. (setq gv `(org-cached-entry-get nil ,pn))))
  9899. (if re-p
  9900. (if (eq po 'org<>)
  9901. `(not (string-match ,pv (or ,gv "")))
  9902. `(string-match ,pv (or ,gv "")))
  9903. (if str-p
  9904. `(,po (or ,gv "") ,pv)
  9905. `(,po (string-to-number (or ,gv ""))
  9906. ,(string-to-number pv) ))))
  9907. (t `(member ,tag tags-list)))
  9908. mm (if minus (list 'not mm) mm)
  9909. term rest)
  9910. (push mm tagsmatcher))
  9911. (push (if (> (length tagsmatcher) 1)
  9912. (cons 'and tagsmatcher)
  9913. (car tagsmatcher))
  9914. orlist)
  9915. (setq tagsmatcher nil))
  9916. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9917. (setq tagsmatcher
  9918. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9919. ;; Make the todo matcher
  9920. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9921. (setq todomatcher t)
  9922. (setq orterms (org-split-string todomatch "|") orlist nil)
  9923. (while (setq term (pop orterms))
  9924. (while (string-match re term)
  9925. (setq minus (and (match-end 1)
  9926. (equal (match-string 1 term) "-"))
  9927. kwd (match-string 2 term)
  9928. re-p (equal (string-to-char kwd) ?{)
  9929. term (substring term (match-end 0))
  9930. mm (if re-p
  9931. `(string-match ,(substring kwd 1 -1) todo)
  9932. (list 'equal 'todo kwd))
  9933. mm (if minus (list 'not mm) mm))
  9934. (push mm todomatcher))
  9935. (push (if (> (length todomatcher) 1)
  9936. (cons 'and todomatcher)
  9937. (car todomatcher))
  9938. orlist)
  9939. (setq todomatcher nil))
  9940. (setq todomatcher (if (> (length orlist) 1)
  9941. (cons 'or orlist) (car orlist))))
  9942. ;; Return the string and lisp forms of the matcher
  9943. (setq matcher (if todomatcher
  9944. (list 'and tagsmatcher todomatcher)
  9945. tagsmatcher))
  9946. (cons match0 matcher)))
  9947. (defun org-op-to-function (op &optional stringp)
  9948. "Turn an operator into the appropriate function."
  9949. (setq op
  9950. (cond
  9951. ((equal op "<" ) '(< string< org-time<))
  9952. ((equal op ">" ) '(> org-string> org-time>))
  9953. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9954. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9955. ((member op '("=" "==")) '(= string= org-time=))
  9956. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9957. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9958. (defun org<> (a b) (not (= a b)))
  9959. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9960. (defun org-string>= (a b) (not (string< a b)))
  9961. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9962. (defun org-string<> (a b) (not (string= a b)))
  9963. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9964. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9965. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9966. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9967. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9968. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9969. (defun org-2ft (s)
  9970. "Convert S to a floating point time.
  9971. If S is already a number, just return it. If it is a string, parse
  9972. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9973. (cond
  9974. ((numberp s) s)
  9975. ((stringp s)
  9976. (condition-case nil
  9977. (float-time (apply 'encode-time (org-parse-time-string s)))
  9978. (error 0.)))
  9979. (t 0.)))
  9980. (defun org-time-today ()
  9981. "Time in seconds today at 0:00.
  9982. Returns the float number of seconds since the beginning of the
  9983. epoch to the beginning of today (00:00)."
  9984. (float-time (apply 'encode-time
  9985. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9986. (defun org-matcher-time (s)
  9987. "Interpret a time comparison value."
  9988. (save-match-data
  9989. (cond
  9990. ((string= s "<now>") (float-time))
  9991. ((string= s "<today>") (org-time-today))
  9992. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9993. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9994. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9995. (+ (org-time-today)
  9996. (* (string-to-number (match-string 1 s))
  9997. (cdr (assoc (match-string 2 s)
  9998. '(("d" . 86400.0) ("w" . 604800.0)
  9999. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10000. (t (org-2ft s)))))
  10001. (defun org-match-any-p (re list)
  10002. "Does re match any element of list?"
  10003. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10004. (delq nil list))
  10005. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10006. (defvar org-tags-overlay (org-make-overlay 1 1))
  10007. (org-detach-overlay org-tags-overlay)
  10008. (defun org-get-local-tags-at (&optional pos)
  10009. "Get a list of tags defined in the current headline."
  10010. (org-get-tags-at pos 'local))
  10011. (defun org-get-local-tags ()
  10012. "Get a list of tags defined in the current headline."
  10013. (org-get-tags-at nil 'local))
  10014. (defun org-get-tags-at (&optional pos local)
  10015. "Get a list of all headline tags applicable at POS.
  10016. POS defaults to point. If tags are inherited, the list contains
  10017. the targets in the same sequence as the headlines appear, i.e.
  10018. the tags of the current headline come last.
  10019. When LOCAL is non-nil, only return tags from the current headline,
  10020. ignore inherited ones."
  10021. (interactive)
  10022. (if (and org-trust-scanner-tags
  10023. (or (not pos) (equal pos (point)))
  10024. (not local))
  10025. org-scanner-tags
  10026. (let (tags ltags lastpos parent)
  10027. (save-excursion
  10028. (save-restriction
  10029. (widen)
  10030. (goto-char (or pos (point)))
  10031. (save-match-data
  10032. (catch 'done
  10033. (condition-case nil
  10034. (progn
  10035. (org-back-to-heading t)
  10036. (while (not (equal lastpos (point)))
  10037. (setq lastpos (point))
  10038. (when (looking-at
  10039. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10040. (setq ltags (org-split-string
  10041. (org-match-string-no-properties 1) ":"))
  10042. (when parent
  10043. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10044. (setq tags (append
  10045. (if parent
  10046. (org-remove-uniherited-tags ltags)
  10047. ltags)
  10048. tags)))
  10049. (or org-use-tag-inheritance (throw 'done t))
  10050. (if local (throw 'done t))
  10051. (or (org-up-heading-safe) (error nil))
  10052. (setq parent t)))
  10053. (error nil)))))
  10054. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10055. (defun org-add-prop-inherited (s)
  10056. (add-text-properties 0 (length s) '(inherited t) s)
  10057. s)
  10058. (defun org-toggle-tag (tag &optional onoff)
  10059. "Toggle the tag TAG for the current line.
  10060. If ONOFF is `on' or `off', don't toggle but set to this state."
  10061. (let (res current)
  10062. (save-excursion
  10063. (org-back-to-heading t)
  10064. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10065. (point-at-eol) t)
  10066. (progn
  10067. (setq current (match-string 1))
  10068. (replace-match ""))
  10069. (setq current ""))
  10070. (setq current (nreverse (org-split-string current ":")))
  10071. (cond
  10072. ((eq onoff 'on)
  10073. (setq res t)
  10074. (or (member tag current) (push tag current)))
  10075. ((eq onoff 'off)
  10076. (or (not (member tag current)) (setq current (delete tag current))))
  10077. (t (if (member tag current)
  10078. (setq current (delete tag current))
  10079. (setq res t)
  10080. (push tag current))))
  10081. (end-of-line 1)
  10082. (if current
  10083. (progn
  10084. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10085. (org-set-tags nil t))
  10086. (delete-horizontal-space))
  10087. (run-hooks 'org-after-tags-change-hook))
  10088. res))
  10089. (defun org-align-tags-here (to-col)
  10090. ;; Assumes that this is a headline
  10091. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10092. (beginning-of-line 1)
  10093. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10094. (< pos (match-beginning 2)))
  10095. (progn
  10096. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10097. (goto-char (match-beginning 1))
  10098. (insert " ")
  10099. (delete-region (point) (1+ (match-beginning 2)))
  10100. (setq ncol (max (1+ (current-column))
  10101. (1+ col)
  10102. (if (> to-col 0)
  10103. to-col
  10104. (- (abs to-col) tags-l))))
  10105. (setq p (point))
  10106. (insert (make-string (- ncol (current-column)) ?\ ))
  10107. (setq ncol (current-column))
  10108. (when indent-tabs-mode (tabify p (point-at-eol)))
  10109. (org-move-to-column (min ncol col) t))
  10110. (goto-char pos))))
  10111. (defun org-set-tags-command (&optional arg just-align)
  10112. "Call the set-tags command for the current entry."
  10113. (interactive "P")
  10114. (if (org-on-heading-p)
  10115. (org-set-tags arg just-align)
  10116. (save-excursion
  10117. (org-back-to-heading t)
  10118. (org-set-tags arg just-align))))
  10119. (defun org-set-tags (&optional arg just-align)
  10120. "Set the tags for the current headline.
  10121. With prefix ARG, realign all tags in headings in the current buffer."
  10122. (interactive "P")
  10123. (let* ((re (concat "^" outline-regexp))
  10124. (current (org-get-tags-string))
  10125. (col (current-column))
  10126. (org-setting-tags t)
  10127. table current-tags inherited-tags ; computed below when needed
  10128. tags p0 c0 c1 rpl)
  10129. (if arg
  10130. (save-excursion
  10131. (goto-char (point-min))
  10132. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10133. (while (re-search-forward re nil t)
  10134. (org-set-tags nil t)
  10135. (end-of-line 1)))
  10136. (message "All tags realigned to column %d" org-tags-column))
  10137. (if just-align
  10138. (setq tags current)
  10139. ;; Get a new set of tags from the user
  10140. (save-excursion
  10141. (setq table (append org-tag-persistent-alist
  10142. (or org-tag-alist (org-get-buffer-tags)))
  10143. org-last-tags-completion-table table
  10144. current-tags (org-split-string current ":")
  10145. inherited-tags (nreverse
  10146. (nthcdr (length current-tags)
  10147. (nreverse (org-get-tags-at))))
  10148. tags
  10149. (if (or (eq t org-use-fast-tag-selection)
  10150. (and org-use-fast-tag-selection
  10151. (delq nil (mapcar 'cdr table))))
  10152. (org-fast-tag-selection
  10153. current-tags inherited-tags table
  10154. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10155. (let ((org-add-colon-after-tag-completion t))
  10156. (org-trim
  10157. (org-without-partial-completion
  10158. (org-icompleting-read "Tags: " 'org-tags-completion-function
  10159. nil nil current 'org-tags-history)))))))
  10160. (while (string-match "[-+&]+" tags)
  10161. ;; No boolean logic, just a list
  10162. (setq tags (replace-match ":" t t tags))))
  10163. (if org-tags-sort-function
  10164. (setq tags (mapconcat 'identity
  10165. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10166. org-tags-sort-function) ":")))
  10167. (if (string-match "\\`[\t ]*\\'" tags)
  10168. (setq tags "")
  10169. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10170. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10171. ;; Insert new tags at the correct column
  10172. (beginning-of-line 1)
  10173. (cond
  10174. ((and (equal current "") (equal tags "")))
  10175. ((re-search-forward
  10176. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10177. (point-at-eol) t)
  10178. (if (equal tags "")
  10179. (setq rpl "")
  10180. (goto-char (match-beginning 0))
  10181. (setq c0 (current-column) p0 (point)
  10182. c1 (max (1+ c0) (if (> org-tags-column 0)
  10183. org-tags-column
  10184. (- (- org-tags-column) (length tags))))
  10185. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10186. (replace-match rpl t t)
  10187. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10188. tags)
  10189. (t (error "Tags alignment failed")))
  10190. (org-move-to-column col)
  10191. (unless just-align
  10192. (run-hooks 'org-after-tags-change-hook)))))
  10193. (defun org-change-tag-in-region (beg end tag off)
  10194. "Add or remove TAG for each entry in the region.
  10195. This works in the agenda, and also in an org-mode buffer."
  10196. (interactive
  10197. (list (region-beginning) (region-end)
  10198. (let ((org-last-tags-completion-table
  10199. (if (org-mode-p)
  10200. (org-get-buffer-tags)
  10201. (org-global-tags-completion-table))))
  10202. (org-icompleting-read
  10203. "Tag: " 'org-tags-completion-function nil nil nil
  10204. 'org-tags-history))
  10205. (progn
  10206. (message "[s]et or [r]emove? ")
  10207. (equal (read-char-exclusive) ?r))))
  10208. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10209. (let ((agendap (equal major-mode 'org-agenda-mode))
  10210. l1 l2 m buf pos newhead (cnt 0))
  10211. (goto-char end)
  10212. (setq l2 (1- (org-current-line)))
  10213. (goto-char beg)
  10214. (setq l1 (org-current-line))
  10215. (loop for l from l1 to l2 do
  10216. (org-goto-line l)
  10217. (setq m (get-text-property (point) 'org-hd-marker))
  10218. (when (or (and (org-mode-p) (org-on-heading-p))
  10219. (and agendap m))
  10220. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10221. pos (if agendap m (point)))
  10222. (with-current-buffer buf
  10223. (save-excursion
  10224. (save-restriction
  10225. (goto-char pos)
  10226. (setq cnt (1+ cnt))
  10227. (org-toggle-tag tag (if off 'off 'on))
  10228. (setq newhead (org-get-heading)))))
  10229. (and agendap (org-agenda-change-all-lines newhead m))))
  10230. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10231. (defun org-tags-completion-function (string predicate &optional flag)
  10232. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10233. (confirm (lambda (x) (stringp (car x)))))
  10234. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10235. (setq s1 (match-string 1 string)
  10236. s2 (match-string 2 string))
  10237. (setq s1 "" s2 string))
  10238. (cond
  10239. ((eq flag nil)
  10240. ;; try completion
  10241. (setq rtn (try-completion s2 ctable confirm))
  10242. (if (stringp rtn)
  10243. (setq rtn
  10244. (concat s1 s2 (substring rtn (length s2))
  10245. (if (and org-add-colon-after-tag-completion
  10246. (assoc rtn ctable))
  10247. ":" ""))))
  10248. rtn)
  10249. ((eq flag t)
  10250. ;; all-completions
  10251. (all-completions s2 ctable confirm)
  10252. )
  10253. ((eq flag 'lambda)
  10254. ;; exact match?
  10255. (assoc s2 ctable)))
  10256. ))
  10257. (defun org-fast-tag-insert (kwd tags face &optional end)
  10258. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10259. (insert (format "%-12s" (concat kwd ":"))
  10260. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10261. (or end "")))
  10262. (defun org-fast-tag-show-exit (flag)
  10263. (save-excursion
  10264. (org-goto-line 3)
  10265. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10266. (replace-match ""))
  10267. (when flag
  10268. (end-of-line 1)
  10269. (org-move-to-column (- (window-width) 19) t)
  10270. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10271. (defun org-set-current-tags-overlay (current prefix)
  10272. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10273. (if (featurep 'xemacs)
  10274. (org-overlay-display org-tags-overlay (concat prefix s)
  10275. 'secondary-selection)
  10276. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10277. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10278. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10279. "Fast tag selection with single keys.
  10280. CURRENT is the current list of tags in the headline, INHERITED is the
  10281. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10282. possibly with grouping information. TODO-TABLE is a similar table with
  10283. TODO keywords, should these have keys assigned to them.
  10284. If the keys are nil, a-z are automatically assigned.
  10285. Returns the new tags string, or nil to not change the current settings."
  10286. (let* ((fulltable (append table todo-table))
  10287. (maxlen (apply 'max (mapcar
  10288. (lambda (x)
  10289. (if (stringp (car x)) (string-width (car x)) 0))
  10290. fulltable)))
  10291. (buf (current-buffer))
  10292. (expert (eq org-fast-tag-selection-single-key 'expert))
  10293. (buffer-tags nil)
  10294. (fwidth (+ maxlen 3 1 3))
  10295. (ncol (/ (- (window-width) 4) fwidth))
  10296. (i-face 'org-done)
  10297. (c-face 'org-todo)
  10298. tg cnt e c char c1 c2 ntable tbl rtn
  10299. ov-start ov-end ov-prefix
  10300. (exit-after-next org-fast-tag-selection-single-key)
  10301. (done-keywords org-done-keywords)
  10302. groups ingroup)
  10303. (save-excursion
  10304. (beginning-of-line 1)
  10305. (if (looking-at
  10306. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10307. (setq ov-start (match-beginning 1)
  10308. ov-end (match-end 1)
  10309. ov-prefix "")
  10310. (setq ov-start (1- (point-at-eol))
  10311. ov-end (1+ ov-start))
  10312. (skip-chars-forward "^\n\r")
  10313. (setq ov-prefix
  10314. (concat
  10315. (buffer-substring (1- (point)) (point))
  10316. (if (> (current-column) org-tags-column)
  10317. " "
  10318. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10319. (org-move-overlay org-tags-overlay ov-start ov-end)
  10320. (save-window-excursion
  10321. (if expert
  10322. (set-buffer (get-buffer-create " *Org tags*"))
  10323. (delete-other-windows)
  10324. (split-window-vertically)
  10325. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10326. (erase-buffer)
  10327. (org-set-local 'org-done-keywords done-keywords)
  10328. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10329. (org-fast-tag-insert "Current" current c-face "\n\n")
  10330. (org-fast-tag-show-exit exit-after-next)
  10331. (org-set-current-tags-overlay current ov-prefix)
  10332. (setq tbl fulltable char ?a cnt 0)
  10333. (while (setq e (pop tbl))
  10334. (cond
  10335. ((equal e '(:startgroup))
  10336. (push '() groups) (setq ingroup t)
  10337. (when (not (= cnt 0))
  10338. (setq cnt 0)
  10339. (insert "\n"))
  10340. (insert "{ "))
  10341. ((equal e '(:endgroup))
  10342. (setq ingroup nil cnt 0)
  10343. (insert "}\n"))
  10344. ((equal e '(:newline))
  10345. (when (not (= cnt 0))
  10346. (setq cnt 0)
  10347. (insert "\n")
  10348. (setq e (car tbl))
  10349. (while (equal (car tbl) '(:newline))
  10350. (insert "\n")
  10351. (setq tbl (cdr tbl)))))
  10352. (t
  10353. (setq tg (copy-sequence (car e)) c2 nil)
  10354. (if (cdr e)
  10355. (setq c (cdr e))
  10356. ;; automatically assign a character.
  10357. (setq c1 (string-to-char
  10358. (downcase (substring
  10359. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10360. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10361. (while (or (rassoc char ntable) (rassoc char table))
  10362. (setq char (1+ char)))
  10363. (setq c2 c1))
  10364. (setq c (or c2 char)))
  10365. (if ingroup (push tg (car groups)))
  10366. (setq tg (org-add-props tg nil 'face
  10367. (cond
  10368. ((not (assoc tg table))
  10369. (org-get-todo-face tg))
  10370. ((member tg current) c-face)
  10371. ((member tg inherited) i-face)
  10372. (t nil))))
  10373. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10374. (insert "[" c "] " tg (make-string
  10375. (- fwidth 4 (length tg)) ?\ ))
  10376. (push (cons tg c) ntable)
  10377. (when (= (setq cnt (1+ cnt)) ncol)
  10378. (insert "\n")
  10379. (if ingroup (insert " "))
  10380. (setq cnt 0)))))
  10381. (setq ntable (nreverse ntable))
  10382. (insert "\n")
  10383. (goto-char (point-min))
  10384. (if (not expert) (org-fit-window-to-buffer))
  10385. (setq rtn
  10386. (catch 'exit
  10387. (while t
  10388. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10389. (if groups " [!] no groups" " [!]groups")
  10390. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10391. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10392. (cond
  10393. ((= c ?\r) (throw 'exit t))
  10394. ((= c ?!)
  10395. (setq groups (not groups))
  10396. (goto-char (point-min))
  10397. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10398. ((= c ?\C-c)
  10399. (if (not expert)
  10400. (org-fast-tag-show-exit
  10401. (setq exit-after-next (not exit-after-next)))
  10402. (setq expert nil)
  10403. (delete-other-windows)
  10404. (split-window-vertically)
  10405. (org-switch-to-buffer-other-window " *Org tags*")
  10406. (org-fit-window-to-buffer)))
  10407. ((or (= c ?\C-g)
  10408. (and (= c ?q) (not (rassoc c ntable))))
  10409. (org-detach-overlay org-tags-overlay)
  10410. (setq quit-flag t))
  10411. ((= c ?\ )
  10412. (setq current nil)
  10413. (if exit-after-next (setq exit-after-next 'now)))
  10414. ((= c ?\t)
  10415. (condition-case nil
  10416. (setq tg (org-icompleting-read
  10417. "Tag: "
  10418. (or buffer-tags
  10419. (with-current-buffer buf
  10420. (org-get-buffer-tags)))))
  10421. (quit (setq tg "")))
  10422. (when (string-match "\\S-" tg)
  10423. (add-to-list 'buffer-tags (list tg))
  10424. (if (member tg current)
  10425. (setq current (delete tg current))
  10426. (push tg current)))
  10427. (if exit-after-next (setq exit-after-next 'now)))
  10428. ((setq e (rassoc c todo-table) tg (car e))
  10429. (with-current-buffer buf
  10430. (save-excursion (org-todo tg)))
  10431. (if exit-after-next (setq exit-after-next 'now)))
  10432. ((setq e (rassoc c ntable) tg (car e))
  10433. (if (member tg current)
  10434. (setq current (delete tg current))
  10435. (loop for g in groups do
  10436. (if (member tg g)
  10437. (mapc (lambda (x)
  10438. (setq current (delete x current)))
  10439. g)))
  10440. (push tg current))
  10441. (if exit-after-next (setq exit-after-next 'now))))
  10442. ;; Create a sorted list
  10443. (setq current
  10444. (sort current
  10445. (lambda (a b)
  10446. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10447. (if (eq exit-after-next 'now) (throw 'exit t))
  10448. (goto-char (point-min))
  10449. (beginning-of-line 2)
  10450. (delete-region (point) (point-at-eol))
  10451. (org-fast-tag-insert "Current" current c-face)
  10452. (org-set-current-tags-overlay current ov-prefix)
  10453. (while (re-search-forward
  10454. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10455. (setq tg (match-string 1))
  10456. (add-text-properties
  10457. (match-beginning 1) (match-end 1)
  10458. (list 'face
  10459. (cond
  10460. ((member tg current) c-face)
  10461. ((member tg inherited) i-face)
  10462. (t (get-text-property (match-beginning 1) 'face))))))
  10463. (goto-char (point-min)))))
  10464. (org-detach-overlay org-tags-overlay)
  10465. (if rtn
  10466. (mapconcat 'identity current ":")
  10467. nil))))
  10468. (defun org-get-tags-string ()
  10469. "Get the TAGS string in the current headline."
  10470. (unless (org-on-heading-p t)
  10471. (error "Not on a heading"))
  10472. (save-excursion
  10473. (beginning-of-line 1)
  10474. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10475. (org-match-string-no-properties 1)
  10476. "")))
  10477. (defun org-get-tags ()
  10478. "Get the list of tags specified in the current headline."
  10479. (org-split-string (org-get-tags-string) ":"))
  10480. (defun org-get-buffer-tags ()
  10481. "Get a table of all tags used in the buffer, for completion."
  10482. (let (tags)
  10483. (save-excursion
  10484. (goto-char (point-min))
  10485. (while (re-search-forward
  10486. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10487. (when (equal (char-after (point-at-bol 0)) ?*)
  10488. (mapc (lambda (x) (add-to-list 'tags x))
  10489. (org-split-string (org-match-string-no-properties 1) ":")))))
  10490. (mapcar 'list tags)))
  10491. ;;;; The mapping API
  10492. ;;;###autoload
  10493. (defun org-map-entries (func &optional match scope &rest skip)
  10494. "Call FUNC at each headline selected by MATCH in SCOPE.
  10495. FUNC is a function or a lisp form. The function will be called without
  10496. arguments, with the cursor positioned at the beginning of the headline.
  10497. The return values of all calls to the function will be collected and
  10498. returned as a list.
  10499. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10500. does not need to preserve point. After evaluation, the cursor will be
  10501. moved to the end of the line (presumably of the headline of the
  10502. processed entry) and search continues from there. Under some
  10503. circumstances, this may not produce the wanted results. For example,
  10504. if you have removed (e.g. archived) the current (sub)tree it could
  10505. mean that the next entry will be skipped entirely. In such cases, you
  10506. can specify the position from where search should continue by making
  10507. FUNC set the variable `org-map-continue-from' to the desired buffer
  10508. position.
  10509. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10510. Only headlines that are matched by this query will be considered during
  10511. the iteration. When MATCH is nil or t, all headlines will be
  10512. visited by the iteration.
  10513. SCOPE determines the scope of this command. It can be any of:
  10514. nil The current buffer, respecting the restriction if any
  10515. tree The subtree started with the entry at point
  10516. file The current buffer, without restriction
  10517. file-with-archives
  10518. The current buffer, and any archives associated with it
  10519. agenda All agenda files
  10520. agenda-with-archives
  10521. All agenda files with any archive files associated with them
  10522. \(file1 file2 ...)
  10523. If this is a list, all files in the list will be scanned
  10524. The remaining args are treated as settings for the skipping facilities of
  10525. the scanner. The following items can be given here:
  10526. archive skip trees with the archive tag.
  10527. comment skip trees with the COMMENT keyword
  10528. function or Emacs Lisp form:
  10529. will be used as value for `org-agenda-skip-function', so whenever
  10530. the the function returns t, FUNC will not be called for that
  10531. entry and search will continue from the point where the
  10532. function leaves it.
  10533. If your function needs to retrieve the tags including inherited tags
  10534. at the *current* entry, you can use the value of the variable
  10535. `org-scanner-tags' which will be much faster than getting the value
  10536. with `org-get-tags-at'. If your function gets properties with
  10537. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10538. to t around the call to `org-entry-properties' to get the same speedup.
  10539. Note that if your function moves around to retrieve tags and properties at
  10540. a *different* entry, you cannot use these techniques."
  10541. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10542. (org-agenda-skip-archived-trees (memq 'archive skip))
  10543. (org-agenda-skip-comment-trees (memq 'comment skip))
  10544. (org-agenda-skip-function
  10545. (car (org-delete-all '(comment archive) skip)))
  10546. (org-tags-match-list-sublevels t)
  10547. matcher file res
  10548. org-todo-keywords-for-agenda
  10549. org-done-keywords-for-agenda
  10550. org-todo-keyword-alist-for-agenda
  10551. org-tag-alist-for-agenda)
  10552. (cond
  10553. ((eq match t) (setq matcher t))
  10554. ((eq match nil) (setq matcher t))
  10555. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10556. (save-excursion
  10557. (save-restriction
  10558. (when (eq scope 'tree)
  10559. (org-back-to-heading t)
  10560. (org-narrow-to-subtree)
  10561. (setq scope nil))
  10562. (if (not scope)
  10563. (progn
  10564. (org-prepare-agenda-buffers
  10565. (list (buffer-file-name (current-buffer))))
  10566. (setq res (org-scan-tags func matcher)))
  10567. ;; Get the right scope
  10568. (cond
  10569. ((and scope (listp scope) (symbolp (car scope)))
  10570. (setq scope (eval scope)))
  10571. ((eq scope 'agenda)
  10572. (setq scope (org-agenda-files t)))
  10573. ((eq scope 'agenda-with-archives)
  10574. (setq scope (org-agenda-files t))
  10575. (setq scope (org-add-archive-files scope)))
  10576. ((eq scope 'file)
  10577. (setq scope (list (buffer-file-name))))
  10578. ((eq scope 'file-with-archives)
  10579. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10580. (org-prepare-agenda-buffers scope)
  10581. (while (setq file (pop scope))
  10582. (with-current-buffer (org-find-base-buffer-visiting file)
  10583. (save-excursion
  10584. (save-restriction
  10585. (widen)
  10586. (goto-char (point-min))
  10587. (setq res (append res (org-scan-tags func matcher))))))))))
  10588. res))
  10589. ;;;; Properties
  10590. ;;; Setting and retrieving properties
  10591. (defconst org-special-properties
  10592. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10593. "TIMESTAMP" "TIMESTAMP_IA")
  10594. "The special properties valid in Org-mode.
  10595. These are properties that are not defined in the property drawer,
  10596. but in some other way.")
  10597. (defconst org-default-properties
  10598. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10599. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10600. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10601. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10602. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10603. "CLOCK_MODELINE_TOTAL")
  10604. "Some properties that are used by Org-mode for various purposes.
  10605. Being in this list makes sure that they are offered for completion.")
  10606. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10607. "Regular expression matching the first line of a property drawer.")
  10608. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10609. "Regular expression matching the first line of a property drawer.")
  10610. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10611. "Regular expression matching the first line of a property drawer.")
  10612. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10613. "Regular expression matching the first line of a property drawer.")
  10614. (defconst org-property-drawer-re
  10615. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10616. org-property-end-re "\\)\n?")
  10617. "Matches an entire property drawer.")
  10618. (defconst org-clock-drawer-re
  10619. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10620. org-property-end-re "\\)\n?")
  10621. "Matches an entire clock drawer.")
  10622. (defun org-property-action ()
  10623. "Do an action on properties."
  10624. (interactive)
  10625. (let (c)
  10626. (org-at-property-p)
  10627. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10628. (setq c (read-char-exclusive))
  10629. (cond
  10630. ((equal c ?s)
  10631. (call-interactively 'org-set-property))
  10632. ((equal c ?d)
  10633. (call-interactively 'org-delete-property))
  10634. ((equal c ?D)
  10635. (call-interactively 'org-delete-property-globally))
  10636. ((equal c ?c)
  10637. (call-interactively 'org-compute-property-at-point))
  10638. (t (error "No such property action %c" c)))))
  10639. (defun org-set-effort (&optional value)
  10640. "Set the effort property of the current entry.
  10641. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  10642. allowed value."
  10643. (interactive "P")
  10644. (if (equal value 0) (setq value 10))
  10645. (let* ((completion-ignore-case t)
  10646. (prop org-effort-property)
  10647. (cur (org-entry-get nil prop))
  10648. (allowed (org-property-get-allowed-values nil prop 'table))
  10649. (existing (mapcar 'list (org-property-values prop)))
  10650. (val (cond
  10651. ((stringp value) value)
  10652. ((and allowed (integerp value))
  10653. (or (car (nth (1- value) allowed))
  10654. (car (org-last allowed))))
  10655. (allowed
  10656. (org-completing-read "Value: " allowed nil 'req-match))
  10657. (t
  10658. (let (org-completion-use-ido org-completion-use-iswitchb)
  10659. (org-completing-read
  10660. (concat "Value " (if (and cur (string-match "\\S-" cur))
  10661. (concat "[" cur "]") "")
  10662. ": ")
  10663. existing nil nil "" nil cur))))))
  10664. (unless (equal (org-entry-get nil prop) val)
  10665. (org-entry-put nil prop val))
  10666. (message "%s is now %s" prop val)))
  10667. (defun org-at-property-p ()
  10668. "Is the cursor in a property line?"
  10669. ;; FIXME: Does not check if we are actually in the drawer.
  10670. ;; FIXME: also returns true on any drawers.....
  10671. ;; This is used by C-c C-c for property action.
  10672. (save-excursion
  10673. (beginning-of-line 1)
  10674. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10675. (defun org-get-property-block (&optional beg end force)
  10676. "Return the (beg . end) range of the body of the property drawer.
  10677. BEG and END can be beginning and end of subtree, if not given
  10678. they will be found.
  10679. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10680. (catch 'exit
  10681. (save-excursion
  10682. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10683. (end (or end (progn (outline-next-heading) (point)))))
  10684. (goto-char beg)
  10685. (if (re-search-forward org-property-start-re end t)
  10686. (setq beg (1+ (match-end 0)))
  10687. (if force
  10688. (save-excursion
  10689. (org-insert-property-drawer)
  10690. (setq end (progn (outline-next-heading) (point))))
  10691. (throw 'exit nil))
  10692. (goto-char beg)
  10693. (if (re-search-forward org-property-start-re end t)
  10694. (setq beg (1+ (match-end 0)))))
  10695. (if (re-search-forward org-property-end-re end t)
  10696. (setq end (match-beginning 0))
  10697. (or force (throw 'exit nil))
  10698. (goto-char beg)
  10699. (setq end beg)
  10700. (org-indent-line-function)
  10701. (insert ":END:\n"))
  10702. (cons beg end)))))
  10703. (defun org-entry-properties (&optional pom which)
  10704. "Get all properties of the entry at point-or-marker POM.
  10705. This includes the TODO keyword, the tags, time strings for deadline,
  10706. scheduled, and clocking, and any additional properties defined in the
  10707. entry. The return value is an alist, keys may occur multiple times
  10708. if the property key was used several times.
  10709. POM may also be nil, in which case the current entry is used.
  10710. If WHICH is nil or `all', get all properties. If WHICH is
  10711. `special' or `standard', only get that subclass."
  10712. (setq which (or which 'all))
  10713. (org-with-point-at pom
  10714. (let ((clockstr (substring org-clock-string 0 -1))
  10715. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10716. beg end range props sum-props key value string clocksum)
  10717. (save-excursion
  10718. (when (condition-case nil
  10719. (and (org-mode-p) (org-back-to-heading t))
  10720. (error nil))
  10721. (setq beg (point))
  10722. (setq sum-props (get-text-property (point) 'org-summaries))
  10723. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10724. (outline-next-heading)
  10725. (setq end (point))
  10726. (when (memq which '(all special))
  10727. ;; Get the special properties, like TODO and tags
  10728. (goto-char beg)
  10729. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10730. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10731. (when (looking-at org-priority-regexp)
  10732. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10733. (when (and (setq value (org-get-tags-string))
  10734. (string-match "\\S-" value))
  10735. (push (cons "TAGS" value) props))
  10736. (when (setq value (org-get-tags-at))
  10737. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10738. props))
  10739. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10740. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10741. string (if (equal key clockstr)
  10742. (org-no-properties
  10743. (org-trim
  10744. (buffer-substring
  10745. (match-beginning 3) (goto-char (point-at-eol)))))
  10746. (substring (org-match-string-no-properties 3) 1 -1)))
  10747. (unless key
  10748. (if (= (char-after (match-beginning 3)) ?\[)
  10749. (setq key "TIMESTAMP_IA")
  10750. (setq key "TIMESTAMP")))
  10751. (when (or (equal key clockstr) (not (assoc key props)))
  10752. (push (cons key string) props)))
  10753. )
  10754. (when (memq which '(all standard))
  10755. ;; Get the standard properties, like :PROP: ...
  10756. (setq range (org-get-property-block beg end))
  10757. (when range
  10758. (goto-char (car range))
  10759. (while (re-search-forward
  10760. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10761. (cdr range) t)
  10762. (setq key (org-match-string-no-properties 1)
  10763. value (org-trim (or (org-match-string-no-properties 2) "")))
  10764. (unless (member key excluded)
  10765. (push (cons key (or value "")) props)))))
  10766. (if clocksum
  10767. (push (cons "CLOCKSUM"
  10768. (org-columns-number-to-string (/ (float clocksum) 60.)
  10769. 'add_times))
  10770. props))
  10771. (unless (assoc "CATEGORY" props)
  10772. (setq value (or (org-get-category)
  10773. (progn (org-refresh-category-properties)
  10774. (org-get-category))))
  10775. (push (cons "CATEGORY" value) props))
  10776. (append sum-props (nreverse props)))))))
  10777. (defun org-entry-get (pom property &optional inherit)
  10778. "Get value of PROPERTY for entry at point-or-marker POM.
  10779. If INHERIT is non-nil and the entry does not have the property,
  10780. then also check higher levels of the hierarchy.
  10781. If INHERIT is the symbol `selective', use inheritance only if the setting
  10782. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10783. If the property is present but empty, the return value is the empty string.
  10784. If the property is not present at all, nil is returned."
  10785. (org-with-point-at pom
  10786. (if (and inherit (if (eq inherit 'selective)
  10787. (org-property-inherit-p property)
  10788. t))
  10789. (org-entry-get-with-inheritance property)
  10790. (if (member property org-special-properties)
  10791. ;; We need a special property. Use brute force, get all properties.
  10792. (cdr (assoc property (org-entry-properties nil 'special)))
  10793. (let ((range (org-get-property-block)))
  10794. (if (and range
  10795. (goto-char (car range))
  10796. (re-search-forward
  10797. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10798. (cdr range) t))
  10799. ;; Found the property, return it.
  10800. (if (match-end 1)
  10801. (org-match-string-no-properties 1)
  10802. "")))))))
  10803. (defun org-property-or-variable-value (var &optional inherit)
  10804. "Check if there is a property fixing the value of VAR.
  10805. If yes, return this value. If not, return the current value of the variable."
  10806. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10807. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10808. (read prop)
  10809. (symbol-value var))))
  10810. (defun org-entry-delete (pom property)
  10811. "Delete the property PROPERTY from entry at point-or-marker POM."
  10812. (org-with-point-at pom
  10813. (if (member property org-special-properties)
  10814. nil ; cannot delete these properties.
  10815. (let ((range (org-get-property-block)))
  10816. (if (and range
  10817. (goto-char (car range))
  10818. (re-search-forward
  10819. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10820. (cdr range) t))
  10821. (progn
  10822. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10823. t)
  10824. nil)))))
  10825. ;; Multi-values properties are properties that contain multiple values
  10826. ;; These values are assumed to be single words, separated by whitespace.
  10827. (defun org-entry-add-to-multivalued-property (pom property value)
  10828. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10829. (let* ((old (org-entry-get pom property))
  10830. (values (and old (org-split-string old "[ \t]"))))
  10831. (setq value (org-entry-protect-space value))
  10832. (unless (member value values)
  10833. (setq values (cons value values))
  10834. (org-entry-put pom property
  10835. (mapconcat 'identity values " ")))))
  10836. (defun org-entry-remove-from-multivalued-property (pom property value)
  10837. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10838. (let* ((old (org-entry-get pom property))
  10839. (values (and old (org-split-string old "[ \t]"))))
  10840. (setq value (org-entry-protect-space value))
  10841. (when (member value values)
  10842. (setq values (delete value values))
  10843. (org-entry-put pom property
  10844. (mapconcat 'identity values " ")))))
  10845. (defun org-entry-member-in-multivalued-property (pom property value)
  10846. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10847. (let* ((old (org-entry-get pom property))
  10848. (values (and old (org-split-string old "[ \t]"))))
  10849. (setq value (org-entry-protect-space value))
  10850. (member value values)))
  10851. (defun org-entry-get-multivalued-property (pom property)
  10852. "Return a list of values in a multivalued property."
  10853. (let* ((value (org-entry-get pom property))
  10854. (values (and value (org-split-string value "[ \t]"))))
  10855. (mapcar 'org-entry-restore-space values)))
  10856. (defun org-entry-put-multivalued-property (pom property &rest values)
  10857. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10858. VALUES should be a list of strings. Spaces will be protected."
  10859. (org-entry-put pom property
  10860. (mapconcat 'org-entry-protect-space values " "))
  10861. (let* ((value (org-entry-get pom property))
  10862. (values (and value (org-split-string value "[ \t]"))))
  10863. (mapcar 'org-entry-restore-space values)))
  10864. (defun org-entry-protect-space (s)
  10865. "Protect spaces and newline in string S."
  10866. (while (string-match " " s)
  10867. (setq s (replace-match "%20" t t s)))
  10868. (while (string-match "\n" s)
  10869. (setq s (replace-match "%0A" t t s)))
  10870. s)
  10871. (defun org-entry-restore-space (s)
  10872. "Restore spaces and newline in string S."
  10873. (while (string-match "%20" s)
  10874. (setq s (replace-match " " t t s)))
  10875. (while (string-match "%0A" s)
  10876. (setq s (replace-match "\n" t t s)))
  10877. s)
  10878. (defvar org-entry-property-inherited-from (make-marker)
  10879. "Marker pointing to the entry from where a property was inherited.
  10880. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10881. location of the entry where the inheritance search matched. If there was
  10882. no match, the marker will point nowhere.
  10883. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10884. is set.")
  10885. (defun org-entry-get-with-inheritance (property)
  10886. "Get entry property, and search higher levels if not present."
  10887. (move-marker org-entry-property-inherited-from nil)
  10888. (let (tmp)
  10889. (save-excursion
  10890. (save-restriction
  10891. (widen)
  10892. (catch 'ex
  10893. (while t
  10894. (when (setq tmp (org-entry-get nil property))
  10895. (org-back-to-heading t)
  10896. (move-marker org-entry-property-inherited-from (point))
  10897. (throw 'ex tmp))
  10898. (or (org-up-heading-safe) (throw 'ex nil)))))
  10899. (or tmp
  10900. (cdr (assoc property org-file-properties))
  10901. (cdr (assoc property org-global-properties))
  10902. (cdr (assoc property org-global-properties-fixed))))))
  10903. (defun org-entry-put (pom property value)
  10904. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10905. (org-with-point-at pom
  10906. (org-back-to-heading t)
  10907. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10908. range)
  10909. (cond
  10910. ((equal property "TODO")
  10911. (when (and (stringp value) (string-match "\\S-" value)
  10912. (not (member value org-todo-keywords-1)))
  10913. (error "\"%s\" is not a valid TODO state" value))
  10914. (if (or (not value)
  10915. (not (string-match "\\S-" value)))
  10916. (setq value 'none))
  10917. (org-todo value)
  10918. (org-set-tags nil 'align))
  10919. ((equal property "PRIORITY")
  10920. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10921. (string-to-char value) ?\ ))
  10922. (org-set-tags nil 'align))
  10923. ((equal property "SCHEDULED")
  10924. (if (re-search-forward org-scheduled-time-regexp end t)
  10925. (cond
  10926. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10927. ((eq value 'later) (org-timestamp-change 1 'day))
  10928. (t (call-interactively 'org-schedule)))
  10929. (call-interactively 'org-schedule)))
  10930. ((equal property "DEADLINE")
  10931. (if (re-search-forward org-deadline-time-regexp end t)
  10932. (cond
  10933. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10934. ((eq value 'later) (org-timestamp-change 1 'day))
  10935. (t (call-interactively 'org-deadline)))
  10936. (call-interactively 'org-deadline)))
  10937. ((member property org-special-properties)
  10938. (error "The %s property can not yet be set with `org-entry-put'"
  10939. property))
  10940. (t ; a non-special property
  10941. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10942. (setq range (org-get-property-block beg end 'force))
  10943. (goto-char (car range))
  10944. (if (re-search-forward
  10945. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10946. (progn
  10947. (delete-region (match-beginning 1) (match-end 1))
  10948. (goto-char (match-beginning 1)))
  10949. (goto-char (cdr range))
  10950. (insert "\n")
  10951. (backward-char 1)
  10952. (org-indent-line-function)
  10953. (insert ":" property ":"))
  10954. (and value (insert " " value))
  10955. (org-indent-line-function)))))))
  10956. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10957. "Get all property keys in the current buffer.
  10958. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10959. like tags and TODO state.
  10960. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10961. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10962. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10963. formats in the current buffer."
  10964. (let (rtn range cfmt s p)
  10965. (save-excursion
  10966. (save-restriction
  10967. (widen)
  10968. (goto-char (point-min))
  10969. (while (re-search-forward org-property-start-re nil t)
  10970. (setq range (org-get-property-block))
  10971. (goto-char (car range))
  10972. (while (re-search-forward
  10973. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10974. (cdr range) t)
  10975. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10976. (outline-next-heading))))
  10977. (when include-specials
  10978. (setq rtn (append org-special-properties rtn)))
  10979. (when include-defaults
  10980. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  10981. (add-to-list 'rtn org-effort-property))
  10982. (when include-columns
  10983. (save-excursion
  10984. (save-restriction
  10985. (widen)
  10986. (goto-char (point-min))
  10987. (while (re-search-forward
  10988. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10989. nil t)
  10990. (setq cfmt (match-string 2) s 0)
  10991. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10992. cfmt s)
  10993. (setq s (match-end 0)
  10994. p (match-string 1 cfmt))
  10995. (unless (or (equal p "ITEM")
  10996. (member p org-special-properties))
  10997. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10998. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10999. (defun org-property-values (key)
  11000. "Return a list of all values of property KEY."
  11001. (save-excursion
  11002. (save-restriction
  11003. (widen)
  11004. (goto-char (point-min))
  11005. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11006. values)
  11007. (while (re-search-forward re nil t)
  11008. (add-to-list 'values (org-trim (match-string 1))))
  11009. (delete "" values)))))
  11010. (defun org-insert-property-drawer ()
  11011. "Insert a property drawer into the current entry."
  11012. (interactive)
  11013. (org-back-to-heading t)
  11014. (looking-at outline-regexp)
  11015. (let ((indent (if org-adapt-indentation
  11016. (- (match-end 0)(match-beginning 0))
  11017. 0))
  11018. (beg (point))
  11019. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11020. end hiddenp)
  11021. (outline-next-heading)
  11022. (setq end (point))
  11023. (goto-char beg)
  11024. (while (re-search-forward re end t))
  11025. (setq hiddenp (org-invisible-p))
  11026. (end-of-line 1)
  11027. (and (equal (char-after) ?\n) (forward-char 1))
  11028. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11029. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11030. ;; just skip this line
  11031. (beginning-of-line 2)
  11032. ;; Drawer start, find the end
  11033. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11034. (beginning-of-line 1)))
  11035. (org-skip-over-state-notes)
  11036. (skip-chars-backward " \t\n\r")
  11037. (if (eq (char-before) ?*) (forward-char 1))
  11038. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11039. (beginning-of-line 0)
  11040. (org-indent-to-column indent)
  11041. (beginning-of-line 2)
  11042. (org-indent-to-column indent)
  11043. (beginning-of-line 0)
  11044. (if hiddenp
  11045. (save-excursion
  11046. (org-back-to-heading t)
  11047. (hide-entry))
  11048. (org-flag-drawer t))))
  11049. (defun org-set-property (property value)
  11050. "In the current entry, set PROPERTY to VALUE.
  11051. When called interactively, this will prompt for a property name, offering
  11052. completion on existing and default properties. And then it will prompt
  11053. for a value, offering completion either on allowed values (via an inherited
  11054. xxx_ALL property) or on existing values in other instances of this property
  11055. in the current file."
  11056. (interactive
  11057. (let* ((completion-ignore-case t)
  11058. (keys (org-buffer-property-keys nil t t))
  11059. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  11060. (prop (if (member prop0 keys)
  11061. prop0
  11062. (or (cdr (assoc (downcase prop0)
  11063. (mapcar (lambda (x) (cons (downcase x) x))
  11064. keys)))
  11065. prop0)))
  11066. (cur (org-entry-get nil prop))
  11067. (allowed (org-property-get-allowed-values nil prop 'table))
  11068. (existing (mapcar 'list (org-property-values prop)))
  11069. (val (if allowed
  11070. (org-completing-read "Value: " allowed nil 'req-match)
  11071. (let (org-completion-use-ido org-completion-use-iswitchb)
  11072. (org-completing-read
  11073. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11074. (concat "[" cur "]") "")
  11075. ": ")
  11076. existing nil nil "" nil cur)))))
  11077. (list prop (if (equal val "") cur val))))
  11078. (unless (equal (org-entry-get nil property) value)
  11079. (org-entry-put nil property value)))
  11080. (defun org-delete-property (property)
  11081. "In the current entry, delete PROPERTY."
  11082. (interactive
  11083. (let* ((completion-ignore-case t)
  11084. (prop (org-icompleting-read
  11085. "Property: " (org-entry-properties nil 'standard))))
  11086. (list prop)))
  11087. (message "Property %s %s" property
  11088. (if (org-entry-delete nil property)
  11089. "deleted"
  11090. "was not present in the entry")))
  11091. (defun org-delete-property-globally (property)
  11092. "Remove PROPERTY globally, from all entries."
  11093. (interactive
  11094. (let* ((completion-ignore-case t)
  11095. (prop (org-icompleting-read
  11096. "Globally remove property: "
  11097. (mapcar 'list (org-buffer-property-keys)))))
  11098. (list prop)))
  11099. (save-excursion
  11100. (save-restriction
  11101. (widen)
  11102. (goto-char (point-min))
  11103. (let ((cnt 0))
  11104. (while (re-search-forward
  11105. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11106. nil t)
  11107. (setq cnt (1+ cnt))
  11108. (replace-match ""))
  11109. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11110. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11111. (defun org-compute-property-at-point ()
  11112. "Compute the property at point.
  11113. This looks for an enclosing column format, extracts the operator and
  11114. then applies it to the property in the column format's scope."
  11115. (interactive)
  11116. (unless (org-at-property-p)
  11117. (error "Not at a property"))
  11118. (let ((prop (org-match-string-no-properties 2)))
  11119. (org-columns-get-format-and-top-level)
  11120. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11121. (error "No operator defined for property %s" prop))
  11122. (org-columns-compute prop)))
  11123. (defun org-property-get-allowed-values (pom property &optional table)
  11124. "Get allowed values for the property PROPERTY.
  11125. When TABLE is non-nil, return an alist that can directly be used for
  11126. completion."
  11127. (let (vals)
  11128. (cond
  11129. ((equal property "TODO")
  11130. (setq vals (org-with-point-at pom
  11131. (append org-todo-keywords-1 '("")))))
  11132. ((equal property "PRIORITY")
  11133. (let ((n org-lowest-priority))
  11134. (while (>= n org-highest-priority)
  11135. (push (char-to-string n) vals)
  11136. (setq n (1- n)))))
  11137. ((member property org-special-properties))
  11138. (t
  11139. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11140. (when (and vals (string-match "\\S-" vals))
  11141. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11142. (setq vals (mapcar (lambda (x)
  11143. (cond ((stringp x) x)
  11144. ((numberp x) (number-to-string x))
  11145. ((symbolp x) (symbol-name x))
  11146. (t "???")))
  11147. vals)))))
  11148. (if table (mapcar 'list vals) vals)))
  11149. (defun org-property-previous-allowed-value (&optional previous)
  11150. "Switch to the next allowed value for this property."
  11151. (interactive)
  11152. (org-property-next-allowed-value t))
  11153. (defun org-property-next-allowed-value (&optional previous)
  11154. "Switch to the next allowed value for this property."
  11155. (interactive)
  11156. (unless (org-at-property-p)
  11157. (error "Not at a property"))
  11158. (let* ((key (match-string 2))
  11159. (value (match-string 3))
  11160. (allowed (or (org-property-get-allowed-values (point) key)
  11161. (and (member value '("[ ]" "[-]" "[X]"))
  11162. '("[ ]" "[X]"))))
  11163. nval)
  11164. (unless allowed
  11165. (error "Allowed values for this property have not been defined"))
  11166. (if previous (setq allowed (reverse allowed)))
  11167. (if (member value allowed)
  11168. (setq nval (car (cdr (member value allowed)))))
  11169. (setq nval (or nval (car allowed)))
  11170. (if (equal nval value)
  11171. (error "Only one allowed value for this property"))
  11172. (org-at-property-p)
  11173. (replace-match (concat " :" key ": " nval) t t)
  11174. (org-indent-line-function)
  11175. (beginning-of-line 1)
  11176. (skip-chars-forward " \t")))
  11177. (defun org-find-entry-with-id (ident)
  11178. "Locate the entry that contains the ID property with exact value IDENT.
  11179. IDENT can be a string, a symbol or a number, this function will search for
  11180. the string representation of it.
  11181. Return the position where this entry starts, or nil if there is no such entry."
  11182. (interactive "sID: ")
  11183. (let ((id (cond
  11184. ((stringp ident) ident)
  11185. ((symbol-name ident) (symbol-name ident))
  11186. ((numberp ident) (number-to-string ident))
  11187. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11188. (case-fold-search nil))
  11189. (save-excursion
  11190. (save-restriction
  11191. (widen)
  11192. (goto-char (point-min))
  11193. (when (re-search-forward
  11194. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11195. nil t)
  11196. (org-back-to-heading t)
  11197. (point))))))
  11198. ;;;; Timestamps
  11199. (defvar org-last-changed-timestamp nil)
  11200. (defvar org-last-inserted-timestamp nil
  11201. "The last time stamp inserted with `org-insert-time-stamp'.")
  11202. (defvar org-time-was-given) ; dynamically scoped parameter
  11203. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11204. (defvar org-ts-what) ; dynamically scoped parameter
  11205. (defun org-time-stamp (arg &optional inactive)
  11206. "Prompt for a date/time and insert a time stamp.
  11207. If the user specifies a time like HH:MM, or if this command is called
  11208. with a prefix argument, the time stamp will contain date and time.
  11209. Otherwise, only the date will be included. All parts of a date not
  11210. specified by the user will be filled in from the current date/time.
  11211. So if you press just return without typing anything, the time stamp
  11212. will represent the current date/time. If there is already a timestamp
  11213. at the cursor, it will be modified."
  11214. (interactive "P")
  11215. (let* ((ts nil)
  11216. (default-time
  11217. ;; Default time is either today, or, when entering a range,
  11218. ;; the range start.
  11219. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11220. (save-excursion
  11221. (re-search-backward
  11222. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11223. (- (point) 20) t)))
  11224. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11225. (current-time)))
  11226. (default-input (and ts (org-get-compact-tod ts)))
  11227. org-time-was-given org-end-time-was-given time)
  11228. (cond
  11229. ((and (org-at-timestamp-p t)
  11230. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11231. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11232. (insert "--")
  11233. (setq time (let ((this-command this-command))
  11234. (org-read-date arg 'totime nil nil
  11235. default-time default-input)))
  11236. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11237. ((org-at-timestamp-p t)
  11238. (setq time (let ((this-command this-command))
  11239. (org-read-date arg 'totime nil nil default-time default-input)))
  11240. (when (org-at-timestamp-p t) ; just to get the match data
  11241. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11242. (replace-match "")
  11243. (setq org-last-changed-timestamp
  11244. (org-insert-time-stamp
  11245. time (or org-time-was-given arg)
  11246. inactive nil nil (list org-end-time-was-given))))
  11247. (message "Timestamp updated"))
  11248. (t
  11249. (setq time (let ((this-command this-command))
  11250. (org-read-date arg 'totime nil nil default-time default-input)))
  11251. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11252. nil nil (list org-end-time-was-given))))))
  11253. ;; FIXME: can we use this for something else, like computing time differences?
  11254. (defun org-get-compact-tod (s)
  11255. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11256. (let* ((t1 (match-string 1 s))
  11257. (h1 (string-to-number (match-string 2 s)))
  11258. (m1 (string-to-number (match-string 3 s)))
  11259. (t2 (and (match-end 4) (match-string 5 s)))
  11260. (h2 (and t2 (string-to-number (match-string 6 s))))
  11261. (m2 (and t2 (string-to-number (match-string 7 s))))
  11262. dh dm)
  11263. (if (not t2)
  11264. t1
  11265. (setq dh (- h2 h1) dm (- m2 m1))
  11266. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11267. (concat t1 "+" (number-to-string dh)
  11268. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11269. (defun org-time-stamp-inactive (&optional arg)
  11270. "Insert an inactive time stamp.
  11271. An inactive time stamp is enclosed in square brackets instead of angle
  11272. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11273. does not link to the calendar and cannot be changed with the S-cursor keys.
  11274. So these are more for recording a certain time/date."
  11275. (interactive "P")
  11276. (org-time-stamp arg 'inactive))
  11277. (defvar org-date-ovl (org-make-overlay 1 1))
  11278. (org-overlay-put org-date-ovl 'face 'org-warning)
  11279. (org-detach-overlay org-date-ovl)
  11280. (defvar org-ans1) ; dynamically scoped parameter
  11281. (defvar org-ans2) ; dynamically scoped parameter
  11282. (defvar org-plain-time-of-day-regexp) ; defined below
  11283. (defvar org-overriding-default-time nil) ; dynamically scoped
  11284. (defvar org-read-date-overlay nil)
  11285. (defvar org-dcst nil) ; dynamically scoped
  11286. (defvar org-read-date-history nil)
  11287. (defvar org-read-date-final-answer nil)
  11288. (defun org-read-date (&optional with-time to-time from-string prompt
  11289. default-time default-input)
  11290. "Read a date, possibly a time, and make things smooth for the user.
  11291. The prompt will suggest to enter an ISO date, but you can also enter anything
  11292. which will at least partially be understood by `parse-time-string'.
  11293. Unrecognized parts of the date will default to the current day, month, year,
  11294. hour and minute. If this command is called to replace a timestamp at point,
  11295. of to enter the second timestamp of a range, the default time is taken from the
  11296. existing stamp. For example,
  11297. 3-2-5 --> 2003-02-05
  11298. feb 15 --> currentyear-02-15
  11299. sep 12 9 --> 2009-09-12
  11300. 12:45 --> today 12:45
  11301. 22 sept 0:34 --> currentyear-09-22 0:34
  11302. 12 --> currentyear-currentmonth-12
  11303. Fri --> nearest Friday (today or later)
  11304. etc.
  11305. Furthermore you can specify a relative date by giving, as the *first* thing
  11306. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11307. change in days weeks, months, years.
  11308. With a single plus or minus, the date is relative to today. With a double
  11309. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11310. +4d --> four days from today
  11311. +4 --> same as above
  11312. +2w --> two weeks from today
  11313. ++5 --> five days from default date
  11314. The function understands only English month and weekday abbreviations,
  11315. but this can be configured with the variables `parse-time-months' and
  11316. `parse-time-weekdays'.
  11317. While prompting, a calendar is popped up - you can also select the
  11318. date with the mouse (button 1). The calendar shows a period of three
  11319. months. To scroll it to other months, use the keys `>' and `<'.
  11320. If you don't like the calendar, turn it off with
  11321. \(setq org-read-date-popup-calendar nil)
  11322. With optional argument TO-TIME, the date will immediately be converted
  11323. to an internal time.
  11324. With an optional argument WITH-TIME, the prompt will suggest to also
  11325. insert a time. Note that when WITH-TIME is not set, you can still
  11326. enter a time, and this function will inform the calling routine about
  11327. this change. The calling routine may then choose to change the format
  11328. used to insert the time stamp into the buffer to include the time.
  11329. With optional argument FROM-STRING, read from this string instead from
  11330. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11331. the time/date that is used for everything that is not specified by the
  11332. user."
  11333. (require 'parse-time)
  11334. (let* ((org-time-stamp-rounding-minutes
  11335. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11336. (org-dcst org-display-custom-times)
  11337. (ct (org-current-time))
  11338. (def (or org-overriding-default-time default-time ct))
  11339. (defdecode (decode-time def))
  11340. (dummy (progn
  11341. (when (< (nth 2 defdecode) org-extend-today-until)
  11342. (setcar (nthcdr 2 defdecode) -1)
  11343. (setcar (nthcdr 1 defdecode) 59)
  11344. (setq def (apply 'encode-time defdecode)
  11345. defdecode (decode-time def)))))
  11346. (calendar-frame-setup nil)
  11347. (calendar-move-hook nil)
  11348. (calendar-view-diary-initially-flag nil)
  11349. (view-diary-entries-initially nil)
  11350. (calendar-view-holidays-initially-flag nil)
  11351. (view-calendar-holidays-initially nil)
  11352. (timestr (format-time-string
  11353. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11354. (prompt (concat (if prompt (concat prompt " ") "")
  11355. (format "Date+time [%s]: " timestr)))
  11356. ans (org-ans0 "") org-ans1 org-ans2 final)
  11357. (cond
  11358. (from-string (setq ans from-string))
  11359. (org-read-date-popup-calendar
  11360. (save-excursion
  11361. (save-window-excursion
  11362. (calendar)
  11363. (calendar-forward-day (- (time-to-days def)
  11364. (calendar-absolute-from-gregorian
  11365. (calendar-current-date))))
  11366. (org-eval-in-calendar nil t)
  11367. (let* ((old-map (current-local-map))
  11368. (map (copy-keymap calendar-mode-map))
  11369. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11370. (org-defkey map (kbd "RET") 'org-calendar-select)
  11371. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11372. 'org-calendar-select-mouse)
  11373. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11374. 'org-calendar-select-mouse)
  11375. (org-defkey minibuffer-local-map [(meta shift left)]
  11376. (lambda () (interactive)
  11377. (org-eval-in-calendar '(calendar-backward-month 1))))
  11378. (org-defkey minibuffer-local-map [(meta shift right)]
  11379. (lambda () (interactive)
  11380. (org-eval-in-calendar '(calendar-forward-month 1))))
  11381. (org-defkey minibuffer-local-map [(meta shift up)]
  11382. (lambda () (interactive)
  11383. (org-eval-in-calendar '(calendar-backward-year 1))))
  11384. (org-defkey minibuffer-local-map [(meta shift down)]
  11385. (lambda () (interactive)
  11386. (org-eval-in-calendar '(calendar-forward-year 1))))
  11387. (org-defkey minibuffer-local-map [?\e (shift left)]
  11388. (lambda () (interactive)
  11389. (org-eval-in-calendar '(calendar-backward-month 1))))
  11390. (org-defkey minibuffer-local-map [?\e (shift right)]
  11391. (lambda () (interactive)
  11392. (org-eval-in-calendar '(calendar-forward-month 1))))
  11393. (org-defkey minibuffer-local-map [?\e (shift up)]
  11394. (lambda () (interactive)
  11395. (org-eval-in-calendar '(calendar-backward-year 1))))
  11396. (org-defkey minibuffer-local-map [?\e (shift down)]
  11397. (lambda () (interactive)
  11398. (org-eval-in-calendar '(calendar-forward-year 1))))
  11399. (org-defkey minibuffer-local-map [(shift up)]
  11400. (lambda () (interactive)
  11401. (org-eval-in-calendar '(calendar-backward-week 1))))
  11402. (org-defkey minibuffer-local-map [(shift down)]
  11403. (lambda () (interactive)
  11404. (org-eval-in-calendar '(calendar-forward-week 1))))
  11405. (org-defkey minibuffer-local-map [(shift left)]
  11406. (lambda () (interactive)
  11407. (org-eval-in-calendar '(calendar-backward-day 1))))
  11408. (org-defkey minibuffer-local-map [(shift right)]
  11409. (lambda () (interactive)
  11410. (org-eval-in-calendar '(calendar-forward-day 1))))
  11411. (org-defkey minibuffer-local-map ">"
  11412. (lambda () (interactive)
  11413. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11414. (org-defkey minibuffer-local-map "<"
  11415. (lambda () (interactive)
  11416. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11417. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11418. (unwind-protect
  11419. (progn
  11420. (use-local-map map)
  11421. (add-hook 'post-command-hook 'org-read-date-display)
  11422. (setq org-ans0 (read-string prompt default-input
  11423. 'org-read-date-history nil))
  11424. ;; org-ans0: from prompt
  11425. ;; org-ans1: from mouse click
  11426. ;; org-ans2: from calendar motion
  11427. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11428. (remove-hook 'post-command-hook 'org-read-date-display)
  11429. (use-local-map old-map)
  11430. (when org-read-date-overlay
  11431. (org-delete-overlay org-read-date-overlay)
  11432. (setq org-read-date-overlay nil)))))))
  11433. (t ; Naked prompt only
  11434. (unwind-protect
  11435. (setq ans (read-string prompt default-input
  11436. 'org-read-date-history timestr))
  11437. (when org-read-date-overlay
  11438. (org-delete-overlay org-read-date-overlay)
  11439. (setq org-read-date-overlay nil)))))
  11440. (setq final (org-read-date-analyze ans def defdecode))
  11441. (setq org-read-date-final-answer ans)
  11442. (if to-time
  11443. (apply 'encode-time final)
  11444. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11445. (format "%04d-%02d-%02d %02d:%02d"
  11446. (nth 5 final) (nth 4 final) (nth 3 final)
  11447. (nth 2 final) (nth 1 final))
  11448. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11449. (defvar def)
  11450. (defvar defdecode)
  11451. (defvar with-time)
  11452. (defun org-read-date-display ()
  11453. "Display the current date prompt interpretation in the minibuffer."
  11454. (when org-read-date-display-live
  11455. (when org-read-date-overlay
  11456. (org-delete-overlay org-read-date-overlay))
  11457. (let ((p (point)))
  11458. (end-of-line 1)
  11459. (while (not (equal (buffer-substring
  11460. (max (point-min) (- (point) 4)) (point))
  11461. " "))
  11462. (insert " "))
  11463. (goto-char p))
  11464. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11465. " " (or org-ans1 org-ans2)))
  11466. (org-end-time-was-given nil)
  11467. (f (org-read-date-analyze ans def defdecode))
  11468. (fmts (if org-dcst
  11469. org-time-stamp-custom-formats
  11470. org-time-stamp-formats))
  11471. (fmt (if (or with-time
  11472. (and (boundp 'org-time-was-given) org-time-was-given))
  11473. (cdr fmts)
  11474. (car fmts)))
  11475. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11476. (when (and org-end-time-was-given
  11477. (string-match org-plain-time-of-day-regexp txt))
  11478. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11479. org-end-time-was-given
  11480. (substring txt (match-end 0)))))
  11481. (setq org-read-date-overlay
  11482. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11483. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11484. (defun org-read-date-analyze (ans def defdecode)
  11485. "Analyse the combined answer of the date prompt."
  11486. ;; FIXME: cleanup and comment
  11487. (let (delta deltan deltaw deltadef year month day
  11488. hour minute second wday pm h2 m2 tl wday1
  11489. iso-year iso-weekday iso-week iso-year iso-date)
  11490. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11491. (setq ans "+0"))
  11492. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11493. (setq ans (replace-match "" t t ans)
  11494. deltan (car delta)
  11495. deltaw (nth 1 delta)
  11496. deltadef (nth 2 delta)))
  11497. ;; Check if there is an iso week date in there
  11498. ;; If yes, sore the info and postpone interpreting it until the rest
  11499. ;; of the parsing is done
  11500. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11501. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11502. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11503. iso-week (string-to-number (match-string 2 ans)))
  11504. (setq ans (replace-match "" t t ans)))
  11505. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11506. (when (string-match
  11507. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11508. (setq year (if (match-end 2)
  11509. (string-to-number (match-string 2 ans))
  11510. (string-to-number (format-time-string "%Y")))
  11511. month (string-to-number (match-string 3 ans))
  11512. day (string-to-number (match-string 4 ans)))
  11513. (if (< year 100) (setq year (+ 2000 year)))
  11514. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11515. t nil ans)))
  11516. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11517. ;; If there is a time with am/pm, and *no* time without it, we convert
  11518. ;; so that matching will be successful.
  11519. (loop for i from 1 to 2 do ; twice, for end time as well
  11520. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11521. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11522. (setq hour (string-to-number (match-string 1 ans))
  11523. minute (if (match-end 3)
  11524. (string-to-number (match-string 3 ans))
  11525. 0)
  11526. pm (equal ?p
  11527. (string-to-char (downcase (match-string 4 ans)))))
  11528. (if (and (= hour 12) (not pm))
  11529. (setq hour 0)
  11530. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11531. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11532. t t ans))))
  11533. ;; Check if a time range is given as a duration
  11534. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11535. (setq hour (string-to-number (match-string 1 ans))
  11536. h2 (+ hour (string-to-number (match-string 3 ans)))
  11537. minute (string-to-number (match-string 2 ans))
  11538. m2 (+ minute (if (match-end 5) (string-to-number
  11539. (match-string 5 ans))0)))
  11540. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11541. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11542. t t ans)))
  11543. ;; Check if there is a time range
  11544. (when (boundp 'org-end-time-was-given)
  11545. (setq org-time-was-given nil)
  11546. (when (and (string-match org-plain-time-of-day-regexp ans)
  11547. (match-end 8))
  11548. (setq org-end-time-was-given (match-string 8 ans))
  11549. (setq ans (concat (substring ans 0 (match-beginning 7))
  11550. (substring ans (match-end 7))))))
  11551. (setq tl (parse-time-string ans)
  11552. day (or (nth 3 tl) (nth 3 defdecode))
  11553. month (or (nth 4 tl)
  11554. (if (and org-read-date-prefer-future
  11555. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11556. (1+ (nth 4 defdecode))
  11557. (nth 4 defdecode)))
  11558. year (or (nth 5 tl)
  11559. (if (and org-read-date-prefer-future
  11560. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11561. (1+ (nth 5 defdecode))
  11562. (nth 5 defdecode)))
  11563. hour (or (nth 2 tl) (nth 2 defdecode))
  11564. minute (or (nth 1 tl) (nth 1 defdecode))
  11565. second (or (nth 0 tl) 0)
  11566. wday (nth 6 tl))
  11567. ;; Special date definitions below
  11568. (cond
  11569. (iso-week
  11570. ;; There was an iso week
  11571. (setq year (or iso-year year)
  11572. day (or iso-weekday wday 1)
  11573. wday nil ; to make sure that the trigger below does not match
  11574. iso-date (calendar-gregorian-from-absolute
  11575. (calendar-absolute-from-iso
  11576. (list iso-week day year))))
  11577. ; FIXME: Should we also push ISO weeks into the future?
  11578. ; (when (and org-read-date-prefer-future
  11579. ; (not iso-year)
  11580. ; (< (calendar-absolute-from-gregorian iso-date)
  11581. ; (time-to-days (current-time))))
  11582. ; (setq year (1+ year)
  11583. ; iso-date (calendar-gregorian-from-absolute
  11584. ; (calendar-absolute-from-iso
  11585. ; (list iso-week day year)))))
  11586. (setq month (car iso-date)
  11587. year (nth 2 iso-date)
  11588. day (nth 1 iso-date)))
  11589. (deltan
  11590. (unless deltadef
  11591. (let ((now (decode-time (current-time))))
  11592. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11593. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11594. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11595. ((equal deltaw "m") (setq month (+ month deltan)))
  11596. ((equal deltaw "y") (setq year (+ year deltan)))))
  11597. ((and wday (not (nth 3 tl)))
  11598. ;; Weekday was given, but no day, so pick that day in the week
  11599. ;; on or after the derived date.
  11600. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11601. (unless (equal wday wday1)
  11602. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11603. (if (and (boundp 'org-time-was-given)
  11604. (nth 2 tl))
  11605. (setq org-time-was-given t))
  11606. (if (< year 100) (setq year (+ 2000 year)))
  11607. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11608. (list second minute hour day month year)))
  11609. (defvar parse-time-weekdays)
  11610. (defun org-read-date-get-relative (s today default)
  11611. "Check string S for special relative date string.
  11612. TODAY and DEFAULT are internal times, for today and for a default.
  11613. Return shift list (N what def-flag)
  11614. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11615. N is the number of WHATs to shift.
  11616. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11617. the DEFAULT date rather than TODAY."
  11618. (when (and
  11619. (string-match
  11620. (concat
  11621. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11622. "\\([0-9]+\\)?"
  11623. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11624. "\\([ \t]\\|$\\)") s)
  11625. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11626. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11627. (string-to-char (substring (match-string 1 s) -1))
  11628. ?+))
  11629. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11630. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11631. (what (if (match-end 3) (match-string 3 s) "d"))
  11632. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11633. (date (if rel default today))
  11634. (wday (nth 6 (decode-time date)))
  11635. delta)
  11636. (if wday1
  11637. (progn
  11638. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11639. (if (= dir ?-) (setq delta (- delta 7)))
  11640. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11641. (list delta "d" rel))
  11642. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11643. (defun org-eval-in-calendar (form &optional keepdate)
  11644. "Eval FORM in the calendar window and return to current window.
  11645. Also, store the cursor date in variable org-ans2."
  11646. (let ((sf (selected-frame))
  11647. (sw (selected-window)))
  11648. (select-window (get-buffer-window "*Calendar*" t))
  11649. (eval form)
  11650. (when (and (not keepdate) (calendar-cursor-to-date))
  11651. (let* ((date (calendar-cursor-to-date))
  11652. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11653. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11654. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11655. (select-window sw)
  11656. (select-frame-set-input-focus sf)))
  11657. (defun org-calendar-select ()
  11658. "Return to `org-read-date' with the date currently selected.
  11659. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11660. (interactive)
  11661. (when (calendar-cursor-to-date)
  11662. (let* ((date (calendar-cursor-to-date))
  11663. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11664. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11665. (if (active-minibuffer-window) (exit-minibuffer))))
  11666. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11667. "Insert a date stamp for the date given by the internal TIME.
  11668. WITH-HM means, use the stamp format that includes the time of the day.
  11669. INACTIVE means use square brackets instead of angular ones, so that the
  11670. stamp will not contribute to the agenda.
  11671. PRE and POST are optional strings to be inserted before and after the
  11672. stamp.
  11673. The command returns the inserted time stamp."
  11674. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11675. stamp)
  11676. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11677. (insert-before-markers (or pre ""))
  11678. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11679. (when (listp extra)
  11680. (setq extra (car extra))
  11681. (if (and (stringp extra)
  11682. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11683. (setq extra (format "-%02d:%02d"
  11684. (string-to-number (match-string 1 extra))
  11685. (string-to-number (match-string 2 extra))))
  11686. (setq extra nil)))
  11687. (when extra
  11688. (backward-char 1)
  11689. (insert-before-markers extra)
  11690. (forward-char 1))
  11691. (insert-before-markers (or post ""))
  11692. (setq org-last-inserted-timestamp stamp)))
  11693. (defun org-toggle-time-stamp-overlays ()
  11694. "Toggle the use of custom time stamp formats."
  11695. (interactive)
  11696. (setq org-display-custom-times (not org-display-custom-times))
  11697. (unless org-display-custom-times
  11698. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11699. (while (setq p (next-single-property-change p 'display))
  11700. (if (and (get-text-property p 'display)
  11701. (eq (get-text-property p 'face) 'org-date))
  11702. (remove-text-properties
  11703. p (setq p (next-single-property-change p 'display))
  11704. '(display t))))
  11705. (set-buffer-modified-p bmp)))
  11706. (if (featurep 'xemacs)
  11707. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11708. (org-restart-font-lock)
  11709. (setq org-table-may-need-update t)
  11710. (if org-display-custom-times
  11711. (message "Time stamps are overlayed with custom format")
  11712. (message "Time stamp overlays removed")))
  11713. (defun org-display-custom-time (beg end)
  11714. "Overlay modified time stamp format over timestamp between BEG and END."
  11715. (let* ((ts (buffer-substring beg end))
  11716. t1 w1 with-hm tf time str w2 (off 0))
  11717. (save-match-data
  11718. (setq t1 (org-parse-time-string ts t))
  11719. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11720. (setq off (- (match-end 0) (match-beginning 0)))))
  11721. (setq end (- end off))
  11722. (setq w1 (- end beg)
  11723. with-hm (and (nth 1 t1) (nth 2 t1))
  11724. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11725. time (org-fix-decoded-time t1)
  11726. str (org-add-props
  11727. (format-time-string
  11728. (substring tf 1 -1) (apply 'encode-time time))
  11729. nil 'mouse-face 'highlight)
  11730. w2 (length str))
  11731. (if (not (= w2 w1))
  11732. (add-text-properties (1+ beg) (+ 2 beg)
  11733. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11734. (if (featurep 'xemacs)
  11735. (progn
  11736. (put-text-property beg end 'invisible t)
  11737. (put-text-property beg end 'end-glyph (make-glyph str)))
  11738. (put-text-property beg end 'display str))))
  11739. (defun org-translate-time (string)
  11740. "Translate all timestamps in STRING to custom format.
  11741. But do this only if the variable `org-display-custom-times' is set."
  11742. (when org-display-custom-times
  11743. (save-match-data
  11744. (let* ((start 0)
  11745. (re org-ts-regexp-both)
  11746. t1 with-hm inactive tf time str beg end)
  11747. (while (setq start (string-match re string start))
  11748. (setq beg (match-beginning 0)
  11749. end (match-end 0)
  11750. t1 (save-match-data
  11751. (org-parse-time-string (substring string beg end) t))
  11752. with-hm (and (nth 1 t1) (nth 2 t1))
  11753. inactive (equal (substring string beg (1+ beg)) "[")
  11754. tf (funcall (if with-hm 'cdr 'car)
  11755. org-time-stamp-custom-formats)
  11756. time (org-fix-decoded-time t1)
  11757. str (format-time-string
  11758. (concat
  11759. (if inactive "[" "<") (substring tf 1 -1)
  11760. (if inactive "]" ">"))
  11761. (apply 'encode-time time))
  11762. string (replace-match str t t string)
  11763. start (+ start (length str)))))))
  11764. string)
  11765. (defun org-fix-decoded-time (time)
  11766. "Set 0 instead of nil for the first 6 elements of time.
  11767. Don't touch the rest."
  11768. (let ((n 0))
  11769. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11770. (defun org-days-to-time (timestamp-string)
  11771. "Difference between TIMESTAMP-STRING and now in days."
  11772. (- (time-to-days (org-time-string-to-time timestamp-string))
  11773. (time-to-days (current-time))))
  11774. (defun org-deadline-close (timestamp-string &optional ndays)
  11775. "Is the time in TIMESTAMP-STRING close to the current date?"
  11776. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11777. (and (< (org-days-to-time timestamp-string) ndays)
  11778. (not (org-entry-is-done-p))))
  11779. (defun org-get-wdays (ts)
  11780. "Get the deadline lead time appropriate for timestring TS."
  11781. (cond
  11782. ((<= org-deadline-warning-days 0)
  11783. ;; 0 or negative, enforce this value no matter what
  11784. (- org-deadline-warning-days))
  11785. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11786. ;; lead time is specified.
  11787. (floor (* (string-to-number (match-string 1 ts))
  11788. (cdr (assoc (match-string 2 ts)
  11789. '(("d" . 1) ("w" . 7)
  11790. ("m" . 30.4) ("y" . 365.25)))))))
  11791. ;; go for the default.
  11792. (t org-deadline-warning-days)))
  11793. (defun org-calendar-select-mouse (ev)
  11794. "Return to `org-read-date' with the date currently selected.
  11795. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11796. (interactive "e")
  11797. (mouse-set-point ev)
  11798. (when (calendar-cursor-to-date)
  11799. (let* ((date (calendar-cursor-to-date))
  11800. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11801. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11802. (if (active-minibuffer-window) (exit-minibuffer))))
  11803. (defun org-check-deadlines (ndays)
  11804. "Check if there are any deadlines due or past due.
  11805. A deadline is considered due if it happens within `org-deadline-warning-days'
  11806. days from today's date. If the deadline appears in an entry marked DONE,
  11807. it is not shown. The prefix arg NDAYS can be used to test that many
  11808. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11809. (interactive "P")
  11810. (let* ((org-warn-days
  11811. (cond
  11812. ((equal ndays '(4)) 100000)
  11813. (ndays (prefix-numeric-value ndays))
  11814. (t (abs org-deadline-warning-days))))
  11815. (case-fold-search nil)
  11816. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11817. (callback
  11818. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11819. (message "%d deadlines past-due or due within %d days"
  11820. (org-occur regexp nil callback)
  11821. org-warn-days)))
  11822. (defun org-check-before-date (date)
  11823. "Check if there are deadlines or scheduled entries before DATE."
  11824. (interactive (list (org-read-date)))
  11825. (let ((case-fold-search nil)
  11826. (regexp (concat "\\<\\(" org-deadline-string
  11827. "\\|" org-scheduled-string
  11828. "\\) *<\\([^>]+\\)>"))
  11829. (callback
  11830. (lambda () (time-less-p
  11831. (org-time-string-to-time (match-string 2))
  11832. (org-time-string-to-time date)))))
  11833. (message "%d entries before %s"
  11834. (org-occur regexp nil callback) date)))
  11835. (defun org-check-after-date (date)
  11836. "Check if there are deadlines or scheduled entries after DATE."
  11837. (interactive (list (org-read-date)))
  11838. (let ((case-fold-search nil)
  11839. (regexp (concat "\\<\\(" org-deadline-string
  11840. "\\|" org-scheduled-string
  11841. "\\) *<\\([^>]+\\)>"))
  11842. (callback
  11843. (lambda () (not
  11844. (time-less-p
  11845. (org-time-string-to-time (match-string 2))
  11846. (org-time-string-to-time date))))))
  11847. (message "%d entries after %s"
  11848. (org-occur regexp nil callback) date)))
  11849. (defun org-evaluate-time-range (&optional to-buffer)
  11850. "Evaluate a time range by computing the difference between start and end.
  11851. Normally the result is just printed in the echo area, but with prefix arg
  11852. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11853. If the time range is actually in a table, the result is inserted into the
  11854. next column.
  11855. For time difference computation, a year is assumed to be exactly 365
  11856. days in order to avoid rounding problems."
  11857. (interactive "P")
  11858. (or
  11859. (org-clock-update-time-maybe)
  11860. (save-excursion
  11861. (unless (org-at-date-range-p t)
  11862. (goto-char (point-at-bol))
  11863. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11864. (if (not (org-at-date-range-p t))
  11865. (error "Not at a time-stamp range, and none found in current line")))
  11866. (let* ((ts1 (match-string 1))
  11867. (ts2 (match-string 2))
  11868. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11869. (match-end (match-end 0))
  11870. (time1 (org-time-string-to-time ts1))
  11871. (time2 (org-time-string-to-time ts2))
  11872. (t1 (time-to-seconds time1))
  11873. (t2 (time-to-seconds time2))
  11874. (diff (abs (- t2 t1)))
  11875. (negative (< (- t2 t1) 0))
  11876. ;; (ys (floor (* 365 24 60 60)))
  11877. (ds (* 24 60 60))
  11878. (hs (* 60 60))
  11879. (fy "%dy %dd %02d:%02d")
  11880. (fy1 "%dy %dd")
  11881. (fd "%dd %02d:%02d")
  11882. (fd1 "%dd")
  11883. (fh "%02d:%02d")
  11884. y d h m align)
  11885. (if havetime
  11886. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11887. y 0
  11888. d (floor (/ diff ds)) diff (mod diff ds)
  11889. h (floor (/ diff hs)) diff (mod diff hs)
  11890. m (floor (/ diff 60)))
  11891. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11892. y 0
  11893. d (floor (+ (/ diff ds) 0.5))
  11894. h 0 m 0))
  11895. (if (not to-buffer)
  11896. (message "%s" (org-make-tdiff-string y d h m))
  11897. (if (org-at-table-p)
  11898. (progn
  11899. (goto-char match-end)
  11900. (setq align t)
  11901. (and (looking-at " *|") (goto-char (match-end 0))))
  11902. (goto-char match-end))
  11903. (if (looking-at
  11904. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11905. (replace-match ""))
  11906. (if negative (insert " -"))
  11907. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11908. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11909. (insert " " (format fh h m))))
  11910. (if align (org-table-align))
  11911. (message "Time difference inserted")))))
  11912. (defun org-make-tdiff-string (y d h m)
  11913. (let ((fmt "")
  11914. (l nil))
  11915. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11916. l (push y l)))
  11917. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11918. l (push d l)))
  11919. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11920. l (push h l)))
  11921. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11922. l (push m l)))
  11923. (apply 'format fmt (nreverse l))))
  11924. (defun org-time-string-to-time (s)
  11925. (apply 'encode-time (org-parse-time-string s)))
  11926. (defun org-time-string-to-seconds (s)
  11927. (time-to-seconds (org-time-string-to-time s)))
  11928. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11929. "Convert a time stamp to an absolute day number.
  11930. If there is a specifyer for a cyclic time stamp, get the closest date to
  11931. DAYNR.
  11932. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  11933. the variable date is bound by the calendar when this is called."
  11934. (cond
  11935. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  11936. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  11937. daynr
  11938. (+ daynr 1000)))
  11939. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  11940. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  11941. (time-to-days (current-time))) (match-string 0 s)
  11942. prefer show-all))
  11943. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  11944. (defun org-days-to-iso-week (days)
  11945. "Return the iso week number."
  11946. (require 'cal-iso)
  11947. (car (calendar-iso-from-absolute days)))
  11948. (defun org-small-year-to-year (year)
  11949. "Convert 2-digit years into 4-digit years.
  11950. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11951. The year 2000 cannot be abbreviated. Any year larger than 99
  11952. is returned unchanged."
  11953. (if (< year 38)
  11954. (setq year (+ 2000 year))
  11955. (if (< year 100)
  11956. (setq year (+ 1900 year))))
  11957. year)
  11958. (defun org-time-from-absolute (d)
  11959. "Return the time corresponding to date D.
  11960. D may be an absolute day number, or a calendar-type list (month day year)."
  11961. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11962. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11963. (defun org-calendar-holiday ()
  11964. "List of holidays, for Diary display in Org-mode."
  11965. (require 'holidays)
  11966. (let ((hl (funcall
  11967. (if (fboundp 'calendar-check-holidays)
  11968. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11969. (if hl (mapconcat 'identity hl "; "))))
  11970. (defun org-diary-sexp-entry (sexp entry date)
  11971. "Process a SEXP diary ENTRY for DATE."
  11972. (require 'diary-lib)
  11973. (let ((result (if calendar-debug-sexp
  11974. (let ((stack-trace-on-error t))
  11975. (eval (car (read-from-string sexp))))
  11976. (condition-case nil
  11977. (eval (car (read-from-string sexp)))
  11978. (error
  11979. (beep)
  11980. (message "Bad sexp at line %d in %s: %s"
  11981. (org-current-line)
  11982. (buffer-file-name) sexp)
  11983. (sleep-for 2))))))
  11984. (cond ((stringp result) result)
  11985. ((and (consp result)
  11986. (stringp (cdr result))) (cdr result))
  11987. (result entry)
  11988. (t nil))))
  11989. (defun org-diary-to-ical-string (frombuf)
  11990. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11991. This uses the icalendar.el library."
  11992. (let* ((tmpdir (if (featurep 'xemacs)
  11993. (temp-directory)
  11994. temporary-file-directory))
  11995. (tmpfile (make-temp-name
  11996. (expand-file-name "orgics" tmpdir)))
  11997. buf rtn b e)
  11998. (save-excursion
  11999. (set-buffer frombuf)
  12000. (icalendar-export-region (point-min) (point-max) tmpfile)
  12001. (setq buf (find-buffer-visiting tmpfile))
  12002. (set-buffer buf)
  12003. (goto-char (point-min))
  12004. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12005. (setq b (match-beginning 0)))
  12006. (goto-char (point-max))
  12007. (if (re-search-backward "^END:VEVENT" nil t)
  12008. (setq e (match-end 0)))
  12009. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12010. (kill-buffer buf)
  12011. (delete-file tmpfile)
  12012. rtn))
  12013. (defun org-closest-date (start current change prefer show-all)
  12014. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12015. When PREFER is `past' return a date that is either CURRENT or past.
  12016. When PREFER is `future', return a date that is either CURRENT or future.
  12017. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12018. ;; Make the proper lists from the dates
  12019. (catch 'exit
  12020. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12021. dn dw sday cday n1 n2 n0
  12022. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12023. (setq start (org-date-to-gregorian start)
  12024. current (org-date-to-gregorian
  12025. (if show-all
  12026. current
  12027. (time-to-days (current-time))))
  12028. sday (calendar-absolute-from-gregorian start)
  12029. cday (calendar-absolute-from-gregorian current))
  12030. (if (<= cday sday) (throw 'exit sday))
  12031. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12032. (setq dn (string-to-number (match-string 1 change))
  12033. dw (cdr (assoc (match-string 2 change) a1)))
  12034. (error "Invalid change specifyer: %s" change))
  12035. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12036. (cond
  12037. ((eq dw 'day)
  12038. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12039. n2 (+ n1 dn)))
  12040. ((eq dw 'year)
  12041. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12042. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12043. (setq date1 (list m d y1)
  12044. n1 (calendar-absolute-from-gregorian date1)
  12045. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12046. n2 (calendar-absolute-from-gregorian date2)))
  12047. ((eq dw 'month)
  12048. ;; approx number of month between the two dates
  12049. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12050. ;; How often does dn fit in there?
  12051. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12052. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12053. m (+ m nm)
  12054. ny (floor (/ m 12))
  12055. y (+ y ny)
  12056. m (- m (* ny 12)))
  12057. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12058. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12059. (setq m2 (+ m dn) y2 y)
  12060. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12061. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12062. (while (<= n2 cday)
  12063. (setq n1 n2 m m2 y y2)
  12064. (setq m2 (+ m dn) y2 y)
  12065. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12066. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12067. ;; Make sure n1 is the earlier date
  12068. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12069. (if show-all
  12070. (cond
  12071. ((eq prefer 'past) n1)
  12072. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12073. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12074. (cond
  12075. ((eq prefer 'past) n1)
  12076. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12077. (t (if (= cday n1) n1 n2)))))))
  12078. (defun org-date-to-gregorian (date)
  12079. "Turn any specification of DATE into a gregorian date for the calendar."
  12080. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12081. ((and (listp date) (= (length date) 3)) date)
  12082. ((stringp date)
  12083. (setq date (org-parse-time-string date))
  12084. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12085. ((listp date)
  12086. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12087. (defun org-parse-time-string (s &optional nodefault)
  12088. "Parse the standard Org-mode time string.
  12089. This should be a lot faster than the normal `parse-time-string'.
  12090. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12091. hour and minute fields will be nil if not given."
  12092. (if (string-match org-ts-regexp0 s)
  12093. (list 0
  12094. (if (or (match-beginning 8) (not nodefault))
  12095. (string-to-number (or (match-string 8 s) "0")))
  12096. (if (or (match-beginning 7) (not nodefault))
  12097. (string-to-number (or (match-string 7 s) "0")))
  12098. (string-to-number (match-string 4 s))
  12099. (string-to-number (match-string 3 s))
  12100. (string-to-number (match-string 2 s))
  12101. nil nil nil)
  12102. (error "Not a standard Org-mode time string: %s" s)))
  12103. (defun org-timestamp-up (&optional arg)
  12104. "Increase the date item at the cursor by one.
  12105. If the cursor is on the year, change the year. If it is on the month or
  12106. the day, change that.
  12107. With prefix ARG, change by that many units."
  12108. (interactive "p")
  12109. (org-timestamp-change (prefix-numeric-value arg)))
  12110. (defun org-timestamp-down (&optional arg)
  12111. "Decrease the date item at the cursor by one.
  12112. If the cursor is on the year, change the year. If it is on the month or
  12113. the day, change that.
  12114. With prefix ARG, change by that many units."
  12115. (interactive "p")
  12116. (org-timestamp-change (- (prefix-numeric-value arg))))
  12117. (defun org-timestamp-up-day (&optional arg)
  12118. "Increase the date in the time stamp by one day.
  12119. With prefix ARG, change that many days."
  12120. (interactive "p")
  12121. (if (and (not (org-at-timestamp-p t))
  12122. (org-on-heading-p))
  12123. (org-todo 'up)
  12124. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12125. (defun org-timestamp-down-day (&optional arg)
  12126. "Decrease the date in the time stamp by one day.
  12127. With prefix ARG, change that many days."
  12128. (interactive "p")
  12129. (if (and (not (org-at-timestamp-p t))
  12130. (org-on-heading-p))
  12131. (org-todo 'down)
  12132. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12133. (defun org-at-timestamp-p (&optional inactive-ok)
  12134. "Determine if the cursor is in or at a timestamp."
  12135. (interactive)
  12136. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12137. (pos (point))
  12138. (ans (or (looking-at tsr)
  12139. (save-excursion
  12140. (skip-chars-backward "^[<\n\r\t")
  12141. (if (> (point) (point-min)) (backward-char 1))
  12142. (and (looking-at tsr)
  12143. (> (- (match-end 0) pos) -1))))))
  12144. (and ans
  12145. (boundp 'org-ts-what)
  12146. (setq org-ts-what
  12147. (cond
  12148. ((= pos (match-beginning 0)) 'bracket)
  12149. ((= pos (1- (match-end 0))) 'bracket)
  12150. ((org-pos-in-match-range pos 2) 'year)
  12151. ((org-pos-in-match-range pos 3) 'month)
  12152. ((org-pos-in-match-range pos 7) 'hour)
  12153. ((org-pos-in-match-range pos 8) 'minute)
  12154. ((or (org-pos-in-match-range pos 4)
  12155. (org-pos-in-match-range pos 5)) 'day)
  12156. ((and (> pos (or (match-end 8) (match-end 5)))
  12157. (< pos (match-end 0)))
  12158. (- pos (or (match-end 8) (match-end 5))))
  12159. (t 'day))))
  12160. ans))
  12161. (defun org-toggle-timestamp-type ()
  12162. "Toggle the type (<active> or [inactive]) of a time stamp."
  12163. (interactive)
  12164. (when (org-at-timestamp-p t)
  12165. (let ((beg (match-beginning 0)) (end (match-end 0))
  12166. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12167. (save-excursion
  12168. (goto-char beg)
  12169. (while (re-search-forward "[][<>]" end t)
  12170. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12171. t t)))
  12172. (message "Timestamp is now %sactive"
  12173. (if (equal (char-after beg) ?<) "" "in")))))
  12174. (defun org-timestamp-change (n &optional what)
  12175. "Change the date in the time stamp at point.
  12176. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12177. `year', `minute', `second'. If WHAT is not given, the cursor position
  12178. in the timestamp determines what will be changed."
  12179. (let ((pos (point))
  12180. with-hm inactive
  12181. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12182. org-ts-what
  12183. extra rem
  12184. ts time time0)
  12185. (if (not (org-at-timestamp-p t))
  12186. (error "Not at a timestamp"))
  12187. (if (and (not what) (eq org-ts-what 'bracket))
  12188. (org-toggle-timestamp-type)
  12189. (if (and (not what) (not (eq org-ts-what 'day))
  12190. org-display-custom-times
  12191. (get-text-property (point) 'display)
  12192. (not (get-text-property (1- (point)) 'display)))
  12193. (setq org-ts-what 'day))
  12194. (setq org-ts-what (or what org-ts-what)
  12195. inactive (= (char-after (match-beginning 0)) ?\[)
  12196. ts (match-string 0))
  12197. (replace-match "")
  12198. (if (string-match
  12199. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  12200. ts)
  12201. (setq extra (match-string 1 ts)))
  12202. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12203. (setq with-hm t))
  12204. (setq time0 (org-parse-time-string ts))
  12205. (when (and (eq org-ts-what 'minute)
  12206. (eq current-prefix-arg nil))
  12207. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12208. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12209. (setcar (cdr time0) (+ (nth 1 time0)
  12210. (if (> n 0) (- rem) (- dm rem))))))
  12211. (setq time
  12212. (encode-time (or (car time0) 0)
  12213. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12214. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12215. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12216. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12217. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12218. (nthcdr 6 time0)))
  12219. (when (and (member org-ts-what '(hour minute))
  12220. extra
  12221. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12222. (setq extra (org-modify-ts-extra
  12223. extra
  12224. (if (eq org-ts-what 'hour) 2 5)
  12225. n dm)))
  12226. (when (integerp org-ts-what)
  12227. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12228. (if (eq what 'calendar)
  12229. (let ((cal-date (org-get-date-from-calendar)))
  12230. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12231. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12232. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12233. (setcar time0 (or (car time0) 0))
  12234. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12235. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12236. (setq time (apply 'encode-time time0))))
  12237. (setq org-last-changed-timestamp
  12238. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12239. (org-clock-update-time-maybe)
  12240. (goto-char pos)
  12241. ;; Try to recenter the calendar window, if any
  12242. (if (and org-calendar-follow-timestamp-change
  12243. (get-buffer-window "*Calendar*" t)
  12244. (memq org-ts-what '(day month year)))
  12245. (org-recenter-calendar (time-to-days time))))))
  12246. (defun org-modify-ts-extra (s pos n dm)
  12247. "Change the different parts of the lead-time and repeat fields in timestamp."
  12248. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12249. ng h m new rem)
  12250. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12251. (cond
  12252. ((or (org-pos-in-match-range pos 2)
  12253. (org-pos-in-match-range pos 3))
  12254. (setq m (string-to-number (match-string 3 s))
  12255. h (string-to-number (match-string 2 s)))
  12256. (if (org-pos-in-match-range pos 2)
  12257. (setq h (+ h n))
  12258. (setq n (* dm (org-no-warnings (signum n))))
  12259. (when (not (= 0 (setq rem (% m dm))))
  12260. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12261. (setq m (+ m n)))
  12262. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12263. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12264. (setq h (min 24 (max 0 h)))
  12265. (setq ng 1 new (format "-%02d:%02d" h m)))
  12266. ((org-pos-in-match-range pos 6)
  12267. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12268. ((org-pos-in-match-range pos 5)
  12269. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12270. ((org-pos-in-match-range pos 9)
  12271. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12272. ((org-pos-in-match-range pos 8)
  12273. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12274. (when ng
  12275. (setq s (concat
  12276. (substring s 0 (match-beginning ng))
  12277. new
  12278. (substring s (match-end ng))))))
  12279. s))
  12280. (defun org-recenter-calendar (date)
  12281. "If the calendar is visible, recenter it to DATE."
  12282. (let* ((win (selected-window))
  12283. (cwin (get-buffer-window "*Calendar*" t))
  12284. (calendar-move-hook nil))
  12285. (when cwin
  12286. (select-window cwin)
  12287. (calendar-goto-date (if (listp date) date
  12288. (calendar-gregorian-from-absolute date)))
  12289. (select-window win))))
  12290. (defun org-goto-calendar (&optional arg)
  12291. "Go to the Emacs calendar at the current date.
  12292. If there is a time stamp in the current line, go to that date.
  12293. A prefix ARG can be used to force the current date."
  12294. (interactive "P")
  12295. (let ((tsr org-ts-regexp) diff
  12296. (calendar-move-hook nil)
  12297. (calendar-view-holidays-initially-flag nil)
  12298. (view-calendar-holidays-initially nil)
  12299. (calendar-view-diary-initially-flag nil)
  12300. (view-diary-entries-initially nil))
  12301. (if (or (org-at-timestamp-p)
  12302. (save-excursion
  12303. (beginning-of-line 1)
  12304. (looking-at (concat ".*" tsr))))
  12305. (let ((d1 (time-to-days (current-time)))
  12306. (d2 (time-to-days
  12307. (org-time-string-to-time (match-string 1)))))
  12308. (setq diff (- d2 d1))))
  12309. (calendar)
  12310. (calendar-goto-today)
  12311. (if (and diff (not arg)) (calendar-forward-day diff))))
  12312. (defun org-get-date-from-calendar ()
  12313. "Return a list (month day year) of date at point in calendar."
  12314. (with-current-buffer "*Calendar*"
  12315. (save-match-data
  12316. (calendar-cursor-to-date))))
  12317. (defun org-date-from-calendar ()
  12318. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12319. If there is already a time stamp at the cursor position, update it."
  12320. (interactive)
  12321. (if (org-at-timestamp-p t)
  12322. (org-timestamp-change 0 'calendar)
  12323. (let ((cal-date (org-get-date-from-calendar)))
  12324. (org-insert-time-stamp
  12325. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12326. (defun org-minutes-to-hh:mm-string (m)
  12327. "Compute H:MM from a number of minutes."
  12328. (let ((h (/ m 60)))
  12329. (setq m (- m (* 60 h)))
  12330. (format org-time-clocksum-format h m)))
  12331. (defun org-hh:mm-string-to-minutes (s)
  12332. "Convert a string H:MM to a number of minutes.
  12333. If the string is just a number, interprete it as minutes.
  12334. In fact, the first hh:mm or number in the string will be taken,
  12335. there can be extra stuff in the string.
  12336. If no number is found, the return value is 0."
  12337. (cond
  12338. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12339. (+ (* (string-to-number (match-string 1 s)) 60)
  12340. (string-to-number (match-string 2 s))))
  12341. ((string-match "\\([0-9]+\\)" s)
  12342. (string-to-number (match-string 1 s)))
  12343. (t 0)))
  12344. ;;;; Files
  12345. (defun org-save-all-org-buffers ()
  12346. "Save all Org-mode buffers without user confirmation."
  12347. (interactive)
  12348. (message "Saving all Org-mode buffers...")
  12349. (save-some-buffers t 'org-mode-p)
  12350. (when (featurep 'org-id) (org-id-locations-save))
  12351. (message "Saving all Org-mode buffers... done"))
  12352. (defun org-revert-all-org-buffers ()
  12353. "Revert all Org-mode buffers.
  12354. Prompt for confirmation when there are unsaved changes.
  12355. Be sure you know what you are doing before letting this function
  12356. overwrite your changes.
  12357. This function is useful in a setup where one tracks org files
  12358. with a version control system, to revert on one machine after pulling
  12359. changes from another. I believe the procedure must be like this:
  12360. 1. M-x org-save-all-org-buffers
  12361. 2. Pull changes from the other machine, resolve conflicts
  12362. 3. M-x org-revert-all-org-buffers"
  12363. (interactive)
  12364. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12365. (error "Abort"))
  12366. (save-excursion
  12367. (save-window-excursion
  12368. (mapc
  12369. (lambda (b)
  12370. (when (and (with-current-buffer b (org-mode-p))
  12371. (with-current-buffer b buffer-file-name))
  12372. (switch-to-buffer b)
  12373. (revert-buffer t 'no-confirm)))
  12374. (buffer-list))
  12375. (when (and (featurep 'org-id) org-id-track-globally)
  12376. (org-id-locations-load)))))
  12377. ;;;; Agenda files
  12378. ;;;###autoload
  12379. (defun org-iswitchb (&optional arg)
  12380. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  12381. With a prefix argument, restrict available to files.
  12382. With two prefix arguments, restrict available buffers to agenda files."
  12383. (interactive "P")
  12384. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12385. ((equal arg '(16)) (org-buffer-list 'agenda))
  12386. (t (org-buffer-list)))))
  12387. (switch-to-buffer
  12388. (org-icompleting-read "Org buffer: "
  12389. (mapcar 'list (mapcar 'buffer-name blist))
  12390. nil t))))
  12391. ;;;###autoload
  12392. (defalias 'org-ido-switchb 'org-iswitchb)
  12393. (defun org-buffer-list (&optional predicate exclude-tmp)
  12394. "Return a list of Org buffers.
  12395. PREDICATE can be `export', `files' or `agenda'.
  12396. export restrict the list to Export buffers.
  12397. files restrict the list to buffers visiting Org files.
  12398. agenda restrict the list to buffers visiting agenda files.
  12399. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12400. (let* ((bfn nil)
  12401. (agenda-files (and (eq predicate 'agenda)
  12402. (mapcar 'file-truename (org-agenda-files t))))
  12403. (filter
  12404. (cond
  12405. ((eq predicate 'files)
  12406. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12407. ((eq predicate 'export)
  12408. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12409. ((eq predicate 'agenda)
  12410. (lambda (b)
  12411. (with-current-buffer b
  12412. (and (eq major-mode 'org-mode)
  12413. (setq bfn (buffer-file-name b))
  12414. (member (file-truename bfn) agenda-files)))))
  12415. (t (lambda (b) (with-current-buffer b
  12416. (or (eq major-mode 'org-mode)
  12417. (string-match "\*Org .*Export"
  12418. (buffer-name b)))))))))
  12419. (delq nil
  12420. (mapcar
  12421. (lambda(b)
  12422. (if (and (funcall filter b)
  12423. (or (not exclude-tmp)
  12424. (not (string-match "tmp" (buffer-name b)))))
  12425. b
  12426. nil))
  12427. (buffer-list)))))
  12428. (defun org-agenda-files (&optional unrestricted archives)
  12429. "Get the list of agenda files.
  12430. Optional UNRESTRICTED means return the full list even if a restriction
  12431. is currently in place.
  12432. When ARCHIVES is t, include all archive files hat are really being
  12433. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12434. `org-agenda-archives-mode' is t."
  12435. (let ((files
  12436. (cond
  12437. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12438. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12439. ((listp org-agenda-files) org-agenda-files)
  12440. (t (error "Invalid value of `org-agenda-files'")))))
  12441. (setq files (apply 'append
  12442. (mapcar (lambda (f)
  12443. (if (file-directory-p f)
  12444. (directory-files
  12445. f t org-agenda-file-regexp)
  12446. (list f)))
  12447. files)))
  12448. (when org-agenda-skip-unavailable-files
  12449. (setq files (delq nil
  12450. (mapcar (function
  12451. (lambda (file)
  12452. (and (file-readable-p file) file)))
  12453. files))))
  12454. (when (or (eq archives t)
  12455. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12456. (setq files (org-add-archive-files files)))
  12457. files))
  12458. (defun org-edit-agenda-file-list ()
  12459. "Edit the list of agenda files.
  12460. Depending on setup, this either uses customize to edit the variable
  12461. `org-agenda-files', or it visits the file that is holding the list. In the
  12462. latter case, the buffer is set up in a way that saving it automatically kills
  12463. the buffer and restores the previous window configuration."
  12464. (interactive)
  12465. (if (stringp org-agenda-files)
  12466. (let ((cw (current-window-configuration)))
  12467. (find-file org-agenda-files)
  12468. (org-set-local 'org-window-configuration cw)
  12469. (org-add-hook 'after-save-hook
  12470. (lambda ()
  12471. (set-window-configuration
  12472. (prog1 org-window-configuration
  12473. (kill-buffer (current-buffer))))
  12474. (org-install-agenda-files-menu)
  12475. (message "New agenda file list installed"))
  12476. nil 'local)
  12477. (message "%s" (substitute-command-keys
  12478. "Edit list and finish with \\[save-buffer]")))
  12479. (customize-variable 'org-agenda-files)))
  12480. (defun org-store-new-agenda-file-list (list)
  12481. "Set new value for the agenda file list and save it correctly."
  12482. (if (stringp org-agenda-files)
  12483. (let ((f org-agenda-files) b)
  12484. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12485. (with-temp-file f
  12486. (insert (mapconcat 'identity list "\n") "\n")))
  12487. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  12488. (setq org-agenda-files list)
  12489. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12490. (defun org-read-agenda-file-list ()
  12491. "Read the list of agenda files from a file."
  12492. (when (file-directory-p org-agenda-files)
  12493. (error "`org-agenda-files' cannot be a single directory"))
  12494. (when (stringp org-agenda-files)
  12495. (with-temp-buffer
  12496. (insert-file-contents org-agenda-files)
  12497. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12498. ;;;###autoload
  12499. (defun org-cycle-agenda-files ()
  12500. "Cycle through the files in `org-agenda-files'.
  12501. If the current buffer visits an agenda file, find the next one in the list.
  12502. If the current buffer does not, find the first agenda file."
  12503. (interactive)
  12504. (let* ((fs (org-agenda-files t))
  12505. (files (append fs (list (car fs))))
  12506. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12507. file)
  12508. (unless files (error "No agenda files"))
  12509. (catch 'exit
  12510. (while (setq file (pop files))
  12511. (if (equal (file-truename file) tcf)
  12512. (when (car files)
  12513. (find-file (car files))
  12514. (throw 'exit t))))
  12515. (find-file (car fs)))
  12516. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12517. (defun org-agenda-file-to-front (&optional to-end)
  12518. "Move/add the current file to the top of the agenda file list.
  12519. If the file is not present in the list, it is added to the front. If it is
  12520. present, it is moved there. With optional argument TO-END, add/move to the
  12521. end of the list."
  12522. (interactive "P")
  12523. (let ((org-agenda-skip-unavailable-files nil)
  12524. (file-alist (mapcar (lambda (x)
  12525. (cons (file-truename x) x))
  12526. (org-agenda-files t)))
  12527. (ctf (file-truename buffer-file-name))
  12528. x had)
  12529. (setq x (assoc ctf file-alist) had x)
  12530. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12531. (if to-end
  12532. (setq file-alist (append (delq x file-alist) (list x)))
  12533. (setq file-alist (cons x (delq x file-alist))))
  12534. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12535. (org-install-agenda-files-menu)
  12536. (message "File %s to %s of agenda file list"
  12537. (if had "moved" "added") (if to-end "end" "front"))))
  12538. (defun org-remove-file (&optional file)
  12539. "Remove current file from the list of files in variable `org-agenda-files'.
  12540. These are the files which are being checked for agenda entries.
  12541. Optional argument FILE means, use this file instead of the current."
  12542. (interactive)
  12543. (let* ((org-agenda-skip-unavailable-files nil)
  12544. (file (or file buffer-file-name))
  12545. (true-file (file-truename file))
  12546. (afile (abbreviate-file-name file))
  12547. (files (delq nil (mapcar
  12548. (lambda (x)
  12549. (if (equal true-file
  12550. (file-truename x))
  12551. nil x))
  12552. (org-agenda-files t)))))
  12553. (if (not (= (length files) (length (org-agenda-files t))))
  12554. (progn
  12555. (org-store-new-agenda-file-list files)
  12556. (org-install-agenda-files-menu)
  12557. (message "Removed file: %s" afile))
  12558. (message "File was not in list: %s (not removed)" afile))))
  12559. (defun org-file-menu-entry (file)
  12560. (vector file (list 'find-file file) t))
  12561. (defun org-check-agenda-file (file)
  12562. "Make sure FILE exists. If not, ask user what to do."
  12563. (when (not (file-exists-p file))
  12564. (message "non-existent file %s. [R]emove from list or [A]bort?"
  12565. (abbreviate-file-name file))
  12566. (let ((r (downcase (read-char-exclusive))))
  12567. (cond
  12568. ((equal r ?r)
  12569. (org-remove-file file)
  12570. (throw 'nextfile t))
  12571. (t (error "Abort"))))))
  12572. (defun org-get-agenda-file-buffer (file)
  12573. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12574. it to the list of buffers which might be released later."
  12575. (let ((buf (org-find-base-buffer-visiting file)))
  12576. (if buf
  12577. buf ; just return it
  12578. ;; Make a new buffer and remember it
  12579. (setq buf (find-file-noselect file))
  12580. (if buf (push buf org-agenda-new-buffers))
  12581. buf)))
  12582. (defun org-release-buffers (blist)
  12583. "Release all buffers in list, asking the user for confirmation when needed.
  12584. When a buffer is unmodified, it is just killed. When modified, it is saved
  12585. \(if the user agrees) and then killed."
  12586. (let (buf file)
  12587. (while (setq buf (pop blist))
  12588. (setq file (buffer-file-name buf))
  12589. (when (and (buffer-modified-p buf)
  12590. file
  12591. (y-or-n-p (format "Save file %s? " file)))
  12592. (with-current-buffer buf (save-buffer)))
  12593. (kill-buffer buf))))
  12594. (defun org-prepare-agenda-buffers (files)
  12595. "Create buffers for all agenda files, protect archived trees and comments."
  12596. (interactive)
  12597. (let ((pa '(:org-archived t))
  12598. (pc '(:org-comment t))
  12599. (pall '(:org-archived t :org-comment t))
  12600. (inhibit-read-only t)
  12601. (rea (concat ":" org-archive-tag ":"))
  12602. bmp file re)
  12603. (save-excursion
  12604. (save-restriction
  12605. (while (setq file (pop files))
  12606. (catch 'nextfile
  12607. (if (bufferp file)
  12608. (set-buffer file)
  12609. (org-check-agenda-file file)
  12610. (set-buffer (org-get-agenda-file-buffer file)))
  12611. (widen)
  12612. (setq bmp (buffer-modified-p))
  12613. (org-refresh-category-properties)
  12614. (setq org-todo-keywords-for-agenda
  12615. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12616. (setq org-done-keywords-for-agenda
  12617. (append org-done-keywords-for-agenda org-done-keywords))
  12618. (setq org-todo-keyword-alist-for-agenda
  12619. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12620. (setq org-tag-alist-for-agenda
  12621. (append org-tag-alist-for-agenda org-tag-alist))
  12622. (save-excursion
  12623. (remove-text-properties (point-min) (point-max) pall)
  12624. (when org-agenda-skip-archived-trees
  12625. (goto-char (point-min))
  12626. (while (re-search-forward rea nil t)
  12627. (if (org-on-heading-p t)
  12628. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12629. (goto-char (point-min))
  12630. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12631. (while (re-search-forward re nil t)
  12632. (add-text-properties
  12633. (match-beginning 0) (org-end-of-subtree t) pc)))
  12634. (set-buffer-modified-p bmp)))))
  12635. (setq org-todo-keyword-alist-for-agenda
  12636. (org-uniquify org-todo-keyword-alist-for-agenda)
  12637. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12638. ;;;; Embedded LaTeX
  12639. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12640. "Keymap for the minor `org-cdlatex-mode'.")
  12641. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12642. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12643. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12644. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12645. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12646. (defvar org-cdlatex-texmathp-advice-is-done nil
  12647. "Flag remembering if we have applied the advice to texmathp already.")
  12648. (define-minor-mode org-cdlatex-mode
  12649. "Toggle the minor `org-cdlatex-mode'.
  12650. This mode supports entering LaTeX environment and math in LaTeX fragments
  12651. in Org-mode.
  12652. \\{org-cdlatex-mode-map}"
  12653. nil " OCDL" nil
  12654. (when org-cdlatex-mode (require 'cdlatex))
  12655. (unless org-cdlatex-texmathp-advice-is-done
  12656. (setq org-cdlatex-texmathp-advice-is-done t)
  12657. (defadvice texmathp (around org-math-always-on activate)
  12658. "Always return t in org-mode buffers.
  12659. This is because we want to insert math symbols without dollars even outside
  12660. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12661. an embedded LaTeX fragment, let texmathp do its job.
  12662. \\[org-cdlatex-mode-map]"
  12663. (interactive)
  12664. (let (p)
  12665. (cond
  12666. ((not (org-mode-p)) ad-do-it)
  12667. ((eq this-command 'cdlatex-math-symbol)
  12668. (setq ad-return-value t
  12669. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12670. (t
  12671. (let ((p (org-inside-LaTeX-fragment-p)))
  12672. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12673. (setq ad-return-value t
  12674. texmathp-why '("Org-mode embedded math" . 0))
  12675. (if p ad-do-it)))))))))
  12676. (defun turn-on-org-cdlatex ()
  12677. "Unconditionally turn on `org-cdlatex-mode'."
  12678. (org-cdlatex-mode 1))
  12679. (defun org-inside-LaTeX-fragment-p ()
  12680. "Test if point is inside a LaTeX fragment.
  12681. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12682. sequence appearing also before point.
  12683. Even though the matchers for math are configurable, this function assumes
  12684. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12685. delimiters are skipped when they have been removed by customization.
  12686. The return value is nil, or a cons cell with the delimiter and
  12687. and the position of this delimiter.
  12688. This function does a reasonably good job, but can locally be fooled by
  12689. for example currency specifications. For example it will assume being in
  12690. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12691. fragments that are properly closed, but during editing, we have to live
  12692. with the uncertainty caused by missing closing delimiters. This function
  12693. looks only before point, not after."
  12694. (catch 'exit
  12695. (let ((pos (point))
  12696. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12697. (lim (progn
  12698. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12699. (point)))
  12700. dd-on str (start 0) m re)
  12701. (goto-char pos)
  12702. (when dodollar
  12703. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12704. re (nth 1 (assoc "$" org-latex-regexps)))
  12705. (while (string-match re str start)
  12706. (cond
  12707. ((= (match-end 0) (length str))
  12708. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12709. ((= (match-end 0) (- (length str) 5))
  12710. (throw 'exit nil))
  12711. (t (setq start (match-end 0))))))
  12712. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12713. (goto-char pos)
  12714. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12715. (and (match-beginning 2) (throw 'exit nil))
  12716. ;; count $$
  12717. (while (re-search-backward "\\$\\$" lim t)
  12718. (setq dd-on (not dd-on)))
  12719. (goto-char pos)
  12720. (if dd-on (cons "$$" m))))))
  12721. (defun org-try-cdlatex-tab ()
  12722. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12723. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12724. - inside a LaTeX fragment, or
  12725. - after the first word in a line, where an abbreviation expansion could
  12726. insert a LaTeX environment."
  12727. (when org-cdlatex-mode
  12728. (cond
  12729. ((save-excursion
  12730. (skip-chars-backward "a-zA-Z0-9*")
  12731. (skip-chars-backward " \t")
  12732. (bolp))
  12733. (cdlatex-tab) t)
  12734. ((org-inside-LaTeX-fragment-p)
  12735. (cdlatex-tab) t)
  12736. (t nil))))
  12737. (defun org-cdlatex-underscore-caret (&optional arg)
  12738. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12739. Revert to the normal definition outside of these fragments."
  12740. (interactive "P")
  12741. (if (org-inside-LaTeX-fragment-p)
  12742. (call-interactively 'cdlatex-sub-superscript)
  12743. (let (org-cdlatex-mode)
  12744. (call-interactively (key-binding (vector last-input-event))))))
  12745. (defun org-cdlatex-math-modify (&optional arg)
  12746. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12747. Revert to the normal definition outside of these fragments."
  12748. (interactive "P")
  12749. (if (org-inside-LaTeX-fragment-p)
  12750. (call-interactively 'cdlatex-math-modify)
  12751. (let (org-cdlatex-mode)
  12752. (call-interactively (key-binding (vector last-input-event))))))
  12753. (defvar org-latex-fragment-image-overlays nil
  12754. "List of overlays carrying the images of latex fragments.")
  12755. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12756. (defun org-remove-latex-fragment-image-overlays ()
  12757. "Remove all overlays with LaTeX fragment images in current buffer."
  12758. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12759. (setq org-latex-fragment-image-overlays nil))
  12760. (defun org-preview-latex-fragment (&optional subtree)
  12761. "Preview the LaTeX fragment at point, or all locally or globally.
  12762. If the cursor is in a LaTeX fragment, create the image and overlay
  12763. it over the source code. If there is no fragment at point, display
  12764. all fragments in the current text, from one headline to the next. With
  12765. prefix SUBTREE, display all fragments in the current subtree. With a
  12766. double prefix `C-u C-u', or when the cursor is before the first headline,
  12767. display all fragments in the buffer.
  12768. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12769. (interactive "P")
  12770. (org-remove-latex-fragment-image-overlays)
  12771. (save-excursion
  12772. (save-restriction
  12773. (let (beg end at msg)
  12774. (cond
  12775. ((or (equal subtree '(16))
  12776. (not (save-excursion
  12777. (re-search-backward (concat "^" outline-regexp) nil t))))
  12778. (setq beg (point-min) end (point-max)
  12779. msg "Creating images for buffer...%s"))
  12780. ((equal subtree '(4))
  12781. (org-back-to-heading)
  12782. (setq beg (point) end (org-end-of-subtree t)
  12783. msg "Creating images for subtree...%s"))
  12784. (t
  12785. (if (setq at (org-inside-LaTeX-fragment-p))
  12786. (goto-char (max (point-min) (- (cdr at) 2)))
  12787. (org-back-to-heading))
  12788. (setq beg (point) end (progn (outline-next-heading) (point))
  12789. msg (if at "Creating image...%s"
  12790. "Creating images for entry...%s"))))
  12791. (message msg "")
  12792. (narrow-to-region beg end)
  12793. (goto-char beg)
  12794. (org-format-latex
  12795. (concat "ltxpng/" (file-name-sans-extension
  12796. (file-name-nondirectory
  12797. buffer-file-name)))
  12798. default-directory 'overlays msg at 'forbuffer)
  12799. (message msg "done. Use `C-c C-c' to remove images.")))))
  12800. (defvar org-latex-regexps
  12801. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12802. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12803. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12804. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12805. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12806. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12807. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  12808. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  12809. "Regular expressions for matching embedded LaTeX.")
  12810. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12811. "Replace LaTeX fragments with links to an image, and produce images."
  12812. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12813. (let* ((prefixnodir (file-name-nondirectory prefix))
  12814. (absprefix (expand-file-name prefix dir))
  12815. (todir (file-name-directory absprefix))
  12816. (opt org-format-latex-options)
  12817. (matchers (plist-get opt :matchers))
  12818. (re-list org-latex-regexps)
  12819. (cnt 0) txt link beg end re e checkdir
  12820. executables-checked
  12821. m n block linkfile movefile ov)
  12822. ;; Check if there are old images files with this prefix, and remove them
  12823. (when (file-directory-p todir)
  12824. (mapc 'delete-file
  12825. (directory-files
  12826. todir 'full
  12827. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12828. ;; Check the different regular expressions
  12829. (while (setq e (pop re-list))
  12830. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12831. block (if (nth 3 e) "\n\n" ""))
  12832. (when (member m matchers)
  12833. (goto-char (point-min))
  12834. (while (re-search-forward re nil t)
  12835. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12836. (not (get-text-property (match-beginning n)
  12837. 'org-protected))
  12838. (or (not overlays)
  12839. (not (eq (get-char-property (match-beginning n)
  12840. 'org-overlay-type)
  12841. 'org-latex-overlay))))
  12842. (setq txt (match-string n)
  12843. beg (match-beginning n) end (match-end n)
  12844. cnt (1+ cnt)
  12845. linkfile (format "%s_%04d.png" prefix cnt)
  12846. movefile (format "%s_%04d.png" absprefix cnt)
  12847. link (concat block "[[file:" linkfile "]]" block))
  12848. (if msg (message msg cnt))
  12849. (goto-char beg)
  12850. (unless checkdir ; make sure the directory exists
  12851. (setq checkdir t)
  12852. (or (file-directory-p todir) (make-directory todir)))
  12853. (unless executables-checked
  12854. (org-check-external-command
  12855. "latex" "needed to convert LaTeX fragments to images")
  12856. (org-check-external-command
  12857. "dvipng" "needed to convert LaTeX fragments to images")
  12858. (setq executables-checked t))
  12859. (org-create-formula-image
  12860. txt movefile opt forbuffer)
  12861. (if overlays
  12862. (progn
  12863. (mapc (lambda (o)
  12864. (if (eq (org-overlay-get o 'org-overlay-type)
  12865. 'org-latex-overlay)
  12866. (org-delete-overlay o)))
  12867. (org-overlays-in beg end))
  12868. (setq ov (org-make-overlay beg end))
  12869. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  12870. (if (featurep 'xemacs)
  12871. (progn
  12872. (org-overlay-put ov 'invisible t)
  12873. (org-overlay-put
  12874. ov 'end-glyph
  12875. (make-glyph (vector 'png :file movefile))))
  12876. (org-overlay-put
  12877. ov 'display
  12878. (list 'image :type 'png :file movefile :ascent 'center)))
  12879. (push ov org-latex-fragment-image-overlays)
  12880. (goto-char end))
  12881. (delete-region beg end)
  12882. (insert link))))))))
  12883. (defvar org-export-latex-packages-alist) ;; defined in org-latex.el
  12884. ;; This function borrows from Ganesh Swami's latex2png.el
  12885. (defun org-create-formula-image (string tofile options buffer)
  12886. (require 'org-latex)
  12887. (let* ((tmpdir (if (featurep 'xemacs)
  12888. (temp-directory)
  12889. temporary-file-directory))
  12890. (texfilebase (make-temp-name
  12891. (expand-file-name "orgtex" tmpdir)))
  12892. (texfile (concat texfilebase ".tex"))
  12893. (dvifile (concat texfilebase ".dvi"))
  12894. (pngfile (concat texfilebase ".png"))
  12895. (fnh (if (featurep 'xemacs)
  12896. (font-height (get-face-font 'default))
  12897. (face-attribute 'default :height nil)))
  12898. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12899. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12900. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12901. "Black"))
  12902. (bg (or (plist-get options (if buffer :background :html-background))
  12903. "Transparent")))
  12904. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12905. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12906. (with-temp-file texfile
  12907. (insert org-format-latex-header
  12908. (if org-export-latex-packages-alist
  12909. (concat "\n"
  12910. (mapconcat (lambda(p)
  12911. (if (equal "" (car p))
  12912. (format "\\usepackage{%s}" (cadr p))
  12913. (format "\\usepackage[%s]{%s}"
  12914. (car p) (cadr p))))
  12915. org-export-latex-packages-alist "\n"))
  12916. "")
  12917. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12918. (let ((dir default-directory))
  12919. (condition-case nil
  12920. (progn
  12921. (cd tmpdir)
  12922. (call-process "latex" nil nil nil texfile))
  12923. (error nil))
  12924. (cd dir))
  12925. (if (not (file-exists-p dvifile))
  12926. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12927. (condition-case nil
  12928. (call-process "dvipng" nil nil nil
  12929. "-fg" fg "-bg" bg
  12930. "-D" dpi
  12931. ;;"-x" scale "-y" scale
  12932. "-T" "tight"
  12933. "-o" pngfile
  12934. dvifile)
  12935. (error nil))
  12936. (if (not (file-exists-p pngfile))
  12937. (progn (message "Failed to create png file from %s" texfile) nil)
  12938. ;; Use the requested file name and clean up
  12939. (copy-file pngfile tofile 'replace)
  12940. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  12941. (delete-file (concat texfilebase e)))
  12942. pngfile))))
  12943. (defun org-dvipng-color (attr)
  12944. "Return an rgb color specification for dvipng."
  12945. (apply 'format "rgb %s %s %s"
  12946. (mapcar 'org-normalize-color
  12947. (color-values (face-attribute 'default attr nil)))))
  12948. (defun org-normalize-color (value)
  12949. "Return string to be used as color value for an RGB component."
  12950. (format "%g" (/ value 65535.0)))
  12951. ;;;; Key bindings
  12952. ;; Make `C-c C-x' a prefix key
  12953. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12954. ;; TAB key with modifiers
  12955. (org-defkey org-mode-map "\C-i" 'org-cycle)
  12956. (org-defkey org-mode-map [(tab)] 'org-cycle)
  12957. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  12958. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  12959. (org-defkey org-mode-map "\M-\t" 'org-complete)
  12960. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  12961. ;; The following line is necessary under Suse GNU/Linux
  12962. (unless (featurep 'xemacs)
  12963. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  12964. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  12965. (define-key org-mode-map [backtab] 'org-shifttab)
  12966. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  12967. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12968. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  12969. ;; Cursor keys with modifiers
  12970. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12971. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12972. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12973. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12974. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12975. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12976. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12977. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12978. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12979. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12980. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12981. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12982. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12983. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12984. ;;; Extra keys for tty access.
  12985. ;; We only set them when really needed because otherwise the
  12986. ;; menus don't show the simple keys
  12987. (when (or org-use-extra-keys
  12988. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12989. (not window-system))
  12990. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12991. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12992. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12993. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12994. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12995. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12996. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12997. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12998. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12999. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  13000. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  13001. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  13002. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  13003. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  13004. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  13005. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  13006. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  13007. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  13008. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  13009. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  13010. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  13011. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  13012. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  13013. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  13014. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  13015. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  13016. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  13017. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  13018. ;; All the other keys
  13019. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13020. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  13021. (if (boundp 'narrow-map)
  13022. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  13023. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13024. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  13025. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  13026. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13027. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13028. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  13029. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13030. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13031. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13032. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13033. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13034. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13035. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13036. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13037. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13038. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13039. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13040. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13041. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13042. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13043. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13044. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13045. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13046. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13047. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13048. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13049. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13050. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13051. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13052. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13053. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13054. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13055. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13056. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13057. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13058. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13059. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13060. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13061. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13062. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13063. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13064. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13065. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13066. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13067. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13068. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13069. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13070. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13071. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  13072. (org-defkey org-mode-map "\C-m" 'org-return)
  13073. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13074. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13075. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13076. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13077. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13078. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13079. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13080. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13081. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13082. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13083. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13084. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13085. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13086. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13087. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13088. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13089. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13090. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  13091. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  13092. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13093. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13094. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13095. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13096. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13097. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13098. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13099. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13100. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13101. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13102. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13103. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13104. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13105. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13106. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13107. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  13108. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13109. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13110. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  13111. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13112. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13113. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13114. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13115. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13116. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13117. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13118. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13119. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13120. (when (featurep 'xemacs)
  13121. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13122. (defvar org-self-insert-command-undo-counter 0)
  13123. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13124. (defun org-self-insert-command (N)
  13125. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13126. If the cursor is in a table looking at whitespace, the whitespace is
  13127. overwritten, and the table is not marked as requiring realignment."
  13128. (interactive "p")
  13129. (if (and
  13130. (org-table-p)
  13131. (progn
  13132. ;; check if we blank the field, and if that triggers align
  13133. (and (featurep 'org-table) org-table-auto-blank-field
  13134. (member last-command
  13135. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  13136. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13137. ;; got extra space, this field does not determine column width
  13138. (let (org-table-may-need-update) (org-table-blank-field))
  13139. ;; no extra space, this field may determine column width
  13140. (org-table-blank-field)))
  13141. t)
  13142. (eq N 1)
  13143. (looking-at "[^|\n]* |"))
  13144. (let (org-table-may-need-update)
  13145. (goto-char (1- (match-end 0)))
  13146. (delete-backward-char 1)
  13147. (goto-char (match-beginning 0))
  13148. (self-insert-command N))
  13149. (setq org-table-may-need-update t)
  13150. (self-insert-command N)
  13151. (org-fix-tags-on-the-fly)
  13152. (if org-self-insert-cluster-for-undo
  13153. (if (not (eq last-command 'org-self-insert-command))
  13154. (setq org-self-insert-command-undo-counter 1)
  13155. (if (>= org-self-insert-command-undo-counter 20)
  13156. (setq org-self-insert-command-undo-counter 1)
  13157. (and (> org-self-insert-command-undo-counter 0)
  13158. buffer-undo-list
  13159. (not (cadr buffer-undo-list)) ; remove nil entry
  13160. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13161. (setq org-self-insert-command-undo-counter
  13162. (1+ org-self-insert-command-undo-counter)))))))
  13163. (defun org-fix-tags-on-the-fly ()
  13164. (when (and (equal (char-after (point-at-bol)) ?*)
  13165. (org-on-heading-p))
  13166. (org-align-tags-here org-tags-column)))
  13167. (defun org-delete-backward-char (N)
  13168. "Like `delete-backward-char', insert whitespace at field end in tables.
  13169. When deleting backwards, in tables this function will insert whitespace in
  13170. front of the next \"|\" separator, to keep the table aligned. The table will
  13171. still be marked for re-alignment if the field did fill the entire column,
  13172. because, in this case the deletion might narrow the column."
  13173. (interactive "p")
  13174. (if (and (org-table-p)
  13175. (eq N 1)
  13176. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13177. (looking-at ".*?|"))
  13178. (let ((pos (point))
  13179. (noalign (looking-at "[^|\n\r]* |"))
  13180. (c org-table-may-need-update))
  13181. (backward-delete-char N)
  13182. (skip-chars-forward "^|")
  13183. (insert " ")
  13184. (goto-char (1- pos))
  13185. ;; noalign: if there were two spaces at the end, this field
  13186. ;; does not determine the width of the column.
  13187. (if noalign (setq org-table-may-need-update c)))
  13188. (backward-delete-char N)
  13189. (org-fix-tags-on-the-fly)))
  13190. (defun org-delete-char (N)
  13191. "Like `delete-char', but insert whitespace at field end in tables.
  13192. When deleting characters, in tables this function will insert whitespace in
  13193. front of the next \"|\" separator, to keep the table aligned. The table will
  13194. still be marked for re-alignment if the field did fill the entire column,
  13195. because, in this case the deletion might narrow the column."
  13196. (interactive "p")
  13197. (if (and (org-table-p)
  13198. (not (bolp))
  13199. (not (= (char-after) ?|))
  13200. (eq N 1))
  13201. (if (looking-at ".*?|")
  13202. (let ((pos (point))
  13203. (noalign (looking-at "[^|\n\r]* |"))
  13204. (c org-table-may-need-update))
  13205. (replace-match (concat
  13206. (substring (match-string 0) 1 -1)
  13207. " |"))
  13208. (goto-char pos)
  13209. ;; noalign: if there were two spaces at the end, this field
  13210. ;; does not determine the width of the column.
  13211. (if noalign (setq org-table-may-need-update c)))
  13212. (delete-char N))
  13213. (delete-char N)
  13214. (org-fix-tags-on-the-fly)))
  13215. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13216. (put 'org-self-insert-command 'delete-selection t)
  13217. (put 'orgtbl-self-insert-command 'delete-selection t)
  13218. (put 'org-delete-char 'delete-selection 'supersede)
  13219. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13220. (put 'org-yank 'delete-selection 'yank)
  13221. ;; Make `flyspell-mode' delay after some commands
  13222. (put 'org-self-insert-command 'flyspell-delayed t)
  13223. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13224. (put 'org-delete-char 'flyspell-delayed t)
  13225. (put 'org-delete-backward-char 'flyspell-delayed t)
  13226. ;; Make pabbrev-mode expand after org-mode commands
  13227. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13228. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13229. ;; How to do this: Measure non-white length of current string
  13230. ;; If equal to column width, we should realign.
  13231. (defun org-remap (map &rest commands)
  13232. "In MAP, remap the functions given in COMMANDS.
  13233. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13234. (let (new old)
  13235. (while commands
  13236. (setq old (pop commands) new (pop commands))
  13237. (if (fboundp 'command-remapping)
  13238. (org-defkey map (vector 'remap old) new)
  13239. (substitute-key-definition old new map global-map)))))
  13240. (when (eq org-enable-table-editor 'optimized)
  13241. ;; If the user wants maximum table support, we need to hijack
  13242. ;; some standard editing functions
  13243. (org-remap org-mode-map
  13244. 'self-insert-command 'org-self-insert-command
  13245. 'delete-char 'org-delete-char
  13246. 'delete-backward-char 'org-delete-backward-char)
  13247. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13248. (defvar org-ctrl-c-ctrl-c-hook nil
  13249. "Hook for functions attaching themselves to `C-c C-c'.
  13250. This can be used to add additional functionality to the C-c C-c key which
  13251. executes context-dependent commands.
  13252. Each function will be called with no arguments. The function must check
  13253. if the context is appropriate for it to act. If yes, it should do its
  13254. thing and then return a non-nil value. If the context is wrong,
  13255. just do nothing and return nil.")
  13256. (defvar org-tab-first-hook nil
  13257. "Hook for functions to attach themselves to TAB.
  13258. See `org-ctrl-c-ctrl-c-hook' for more information.
  13259. This hook runs as the first action when TAB is pressed, even before
  13260. `org-cycle' messes around with the `outline-regexp' to cater for
  13261. inline tasks and plain list item folding.
  13262. If any function in this hook returns t, not other actions like table
  13263. field motion visibility cycling will be done.")
  13264. (defvar org-tab-after-check-for-table-hook nil
  13265. "Hook for functions to attach themselves to TAB.
  13266. See `org-ctrl-c-ctrl-c-hook' for more information.
  13267. This hook runs after it has been established that the cursor is not in a
  13268. table, but before checking if the cursor is in a headline or if global cycling
  13269. should be done.
  13270. If any function in this hook returns t, not other actions like visibility
  13271. cycling will be done.")
  13272. (defvar org-tab-after-check-for-cycling-hook nil
  13273. "Hook for functions to attach themselves to TAB.
  13274. See `org-ctrl-c-ctrl-c-hook' for more information.
  13275. This hook runs after it has been established that not table field motion and
  13276. not visibility should be done because of current context. This is probably
  13277. the place where a package like yasnippets can hook in.")
  13278. (defvar org-metaleft-hook nil
  13279. "Hook for functions attaching themselves to `M-left'.
  13280. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13281. (defvar org-metaright-hook nil
  13282. "Hook for functions attaching themselves to `M-right'.
  13283. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13284. (defvar org-metaup-hook nil
  13285. "Hook for functions attaching themselves to `M-up'.
  13286. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13287. (defvar org-metadown-hook nil
  13288. "Hook for functions attaching themselves to `M-down'.
  13289. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13290. (defvar org-shiftmetaleft-hook nil
  13291. "Hook for functions attaching themselves to `M-S-left'.
  13292. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13293. (defvar org-shiftmetaright-hook nil
  13294. "Hook for functions attaching themselves to `M-S-right'.
  13295. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13296. (defvar org-shiftmetaup-hook nil
  13297. "Hook for functions attaching themselves to `M-S-up'.
  13298. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13299. (defvar org-shiftmetadown-hook nil
  13300. "Hook for functions attaching themselves to `M-S-down'.
  13301. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13302. (defvar org-metareturn-hook nil
  13303. "Hook for functions attaching themselves to `M-RET'.
  13304. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13305. (defun org-modifier-cursor-error ()
  13306. "Throw an error, a modified cursor command was applied in wrong context."
  13307. (error "This command is active in special context like tables, headlines or items"))
  13308. (defun org-shiftselect-error ()
  13309. "Throw an error because Shift-Cursor command was applied in wrong context."
  13310. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13311. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  13312. (error "This command works only in special context like headlines or timestamps.")))
  13313. (defun org-call-for-shift-select (cmd)
  13314. (let ((this-command-keys-shift-translated t))
  13315. (call-interactively cmd)))
  13316. (defun org-shifttab (&optional arg)
  13317. "Global visibility cycling or move to previous table field.
  13318. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13319. on context.
  13320. See the individual commands for more information."
  13321. (interactive "P")
  13322. (cond
  13323. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13324. ((integerp arg)
  13325. (message "Content view to level: %d" arg)
  13326. (org-content (prefix-numeric-value arg))
  13327. (setq org-cycle-global-status 'overview))
  13328. (t (call-interactively 'org-global-cycle))))
  13329. (defun org-shiftmetaleft ()
  13330. "Promote subtree or delete table column.
  13331. Calls `org-promote-subtree', `org-outdent-item',
  13332. or `org-table-delete-column', depending on context.
  13333. See the individual commands for more information."
  13334. (interactive)
  13335. (cond
  13336. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13337. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13338. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13339. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13340. (t (org-modifier-cursor-error))))
  13341. (defun org-shiftmetaright ()
  13342. "Demote subtree or insert table column.
  13343. Calls `org-demote-subtree', `org-indent-item',
  13344. or `org-table-insert-column', depending on context.
  13345. See the individual commands for more information."
  13346. (interactive)
  13347. (cond
  13348. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13349. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13350. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13351. ((org-at-item-p) (call-interactively 'org-indent-item))
  13352. (t (org-modifier-cursor-error))))
  13353. (defun org-shiftmetaup (&optional arg)
  13354. "Move subtree up or kill table row.
  13355. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13356. `org-move-item-up' depending on context. See the individual commands
  13357. for more information."
  13358. (interactive "P")
  13359. (cond
  13360. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13361. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13362. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13363. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13364. (t (org-modifier-cursor-error))))
  13365. (defun org-shiftmetadown (&optional arg)
  13366. "Move subtree down or insert table row.
  13367. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13368. `org-move-item-down', depending on context. See the individual
  13369. commands for more information."
  13370. (interactive "P")
  13371. (cond
  13372. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13373. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13374. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13375. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13376. (t (org-modifier-cursor-error))))
  13377. (defun org-metaleft (&optional arg)
  13378. "Promote heading or move table column to left.
  13379. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13380. With no specific context, calls the Emacs default `backward-word'.
  13381. See the individual commands for more information."
  13382. (interactive "P")
  13383. (cond
  13384. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13385. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13386. ((or (org-on-heading-p)
  13387. (and (org-region-active-p)
  13388. (save-excursion
  13389. (goto-char (region-beginning))
  13390. (org-on-heading-p))))
  13391. (call-interactively 'org-do-promote))
  13392. ((or (org-at-item-p)
  13393. (and (org-region-active-p)
  13394. (save-excursion
  13395. (goto-char (region-beginning))
  13396. (org-at-item-p))))
  13397. (call-interactively 'org-outdent-item))
  13398. (t (call-interactively 'backward-word))))
  13399. (defun org-metaright (&optional arg)
  13400. "Demote subtree or move table column to right.
  13401. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13402. With no specific context, calls the Emacs default `forward-word'.
  13403. See the individual commands for more information."
  13404. (interactive "P")
  13405. (cond
  13406. ((run-hook-with-args-until-success 'org-metaright-hook))
  13407. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13408. ((or (org-on-heading-p)
  13409. (and (org-region-active-p)
  13410. (save-excursion
  13411. (goto-char (region-beginning))
  13412. (org-on-heading-p))))
  13413. (call-interactively 'org-do-demote))
  13414. ((or (org-at-item-p)
  13415. (and (org-region-active-p)
  13416. (save-excursion
  13417. (goto-char (region-beginning))
  13418. (org-at-item-p))))
  13419. (call-interactively 'org-indent-item))
  13420. (t (call-interactively 'forward-word))))
  13421. (defun org-metaup (&optional arg)
  13422. "Move subtree up or move table row up.
  13423. Calls `org-move-subtree-up' or `org-table-move-row' or
  13424. `org-move-item-up', depending on context. See the individual commands
  13425. for more information."
  13426. (interactive "P")
  13427. (cond
  13428. ((run-hook-with-args-until-success 'org-metaup-hook))
  13429. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13430. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13431. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13432. (t (transpose-lines 1) (beginning-of-line -1))))
  13433. (defun org-metadown (&optional arg)
  13434. "Move subtree down or move table row down.
  13435. Calls `org-move-subtree-down' or `org-table-move-row' or
  13436. `org-move-item-down', depending on context. See the individual
  13437. commands for more information."
  13438. (interactive "P")
  13439. (cond
  13440. ((run-hook-with-args-until-success 'org-metadown-hook))
  13441. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13442. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13443. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13444. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13445. (defun org-shiftup (&optional arg)
  13446. "Increase item in timestamp or increase priority of current headline.
  13447. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13448. depending on context. See the individual commands for more information."
  13449. (interactive "P")
  13450. (cond
  13451. ((and org-support-shift-select (org-region-active-p))
  13452. (org-call-for-shift-select 'previous-line))
  13453. ((org-at-timestamp-p t)
  13454. (call-interactively (if org-edit-timestamp-down-means-later
  13455. 'org-timestamp-down 'org-timestamp-up)))
  13456. ((and (not (eq org-support-shift-select 'always))
  13457. org-enable-priority-commands
  13458. (org-on-heading-p))
  13459. (call-interactively 'org-priority-up))
  13460. ((and (not org-support-shift-select) (org-at-item-p))
  13461. (call-interactively 'org-previous-item))
  13462. ((org-clocktable-try-shift 'up arg))
  13463. (org-support-shift-select
  13464. (org-call-for-shift-select 'previous-line))
  13465. (t (org-shiftselect-error))))
  13466. (defun org-shiftdown (&optional arg)
  13467. "Decrease item in timestamp or decrease priority of current headline.
  13468. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13469. depending on context. See the individual commands for more information."
  13470. (interactive "P")
  13471. (cond
  13472. ((and org-support-shift-select (org-region-active-p))
  13473. (org-call-for-shift-select 'next-line))
  13474. ((org-at-timestamp-p t)
  13475. (call-interactively (if org-edit-timestamp-down-means-later
  13476. 'org-timestamp-up 'org-timestamp-down)))
  13477. ((and (not (eq org-support-shift-select 'always))
  13478. org-enable-priority-commands
  13479. (org-on-heading-p))
  13480. (call-interactively 'org-priority-down))
  13481. ((and (not org-support-shift-select) (org-at-item-p))
  13482. (call-interactively 'org-next-item))
  13483. ((org-clocktable-try-shift 'down arg))
  13484. (org-support-shift-select
  13485. (org-call-for-shift-select 'next-line))
  13486. (t (org-shiftselect-error))))
  13487. (defun org-shiftright (&optional arg)
  13488. "Cycle the thing at point or in the current line, depending on context.
  13489. Depending on context, this does one of the following:
  13490. - switch a timestamp at point one day into the future
  13491. - on a headline, switch to the next TODO keyword.
  13492. - on an item, switch entire list to the next bullet type
  13493. - on a property line, switch to the next allowed value
  13494. - on a clocktable definition line, move time block into the future"
  13495. (interactive "P")
  13496. (cond
  13497. ((and org-support-shift-select (org-region-active-p))
  13498. (org-call-for-shift-select 'forward-char))
  13499. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13500. ((and (not (eq org-support-shift-select 'always))
  13501. (org-on-heading-p))
  13502. (let ((org-inhibit-logging
  13503. (not org-treat-S-cursor-todo-selection-as-state-change))
  13504. (org-inhibit-blocking
  13505. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13506. (org-call-with-arg 'org-todo 'right)))
  13507. ((or (and org-support-shift-select
  13508. (not (eq org-support-shift-select 'always))
  13509. (org-at-item-bullet-p))
  13510. (and (not org-support-shift-select) (org-at-item-p)))
  13511. (org-call-with-arg 'org-cycle-list-bullet nil))
  13512. ((and (not (eq org-support-shift-select 'always))
  13513. (org-at-property-p))
  13514. (call-interactively 'org-property-next-allowed-value))
  13515. ((org-clocktable-try-shift 'right arg))
  13516. (org-support-shift-select
  13517. (org-call-for-shift-select 'forward-char))
  13518. (t (org-shiftselect-error))))
  13519. (defun org-shiftleft (&optional arg)
  13520. "Cycle the thing at point or in the current line, depending on context.
  13521. Depending on context, this does one of the following:
  13522. - switch a timestamp at point one day into the past
  13523. - on a headline, switch to the previous TODO keyword.
  13524. - on an item, switch entire list to the previous bullet type
  13525. - on a property line, switch to the previous allowed value
  13526. - on a clocktable definition line, move time block into the past"
  13527. (interactive "P")
  13528. (cond
  13529. ((and org-support-shift-select (org-region-active-p))
  13530. (org-call-for-shift-select 'backward-char))
  13531. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13532. ((and (not (eq org-support-shift-select 'always))
  13533. (org-on-heading-p))
  13534. (let ((org-inhibit-logging
  13535. (not org-treat-S-cursor-todo-selection-as-state-change))
  13536. (org-inhibit-blocking
  13537. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13538. (org-call-with-arg 'org-todo 'left)))
  13539. ((or (and org-support-shift-select
  13540. (not (eq org-support-shift-select 'always))
  13541. (org-at-item-bullet-p))
  13542. (and (not org-support-shift-select) (org-at-item-p)))
  13543. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13544. ((and (not (eq org-support-shift-select 'always))
  13545. (org-at-property-p))
  13546. (call-interactively 'org-property-previous-allowed-value))
  13547. ((org-clocktable-try-shift 'left arg))
  13548. (org-support-shift-select
  13549. (org-call-for-shift-select 'backward-char))
  13550. (t (org-shiftselect-error))))
  13551. (defun org-shiftcontrolright ()
  13552. "Switch to next TODO set."
  13553. (interactive)
  13554. (cond
  13555. ((and org-support-shift-select (org-region-active-p))
  13556. (org-call-for-shift-select 'forward-word))
  13557. ((and (not (eq org-support-shift-select 'always))
  13558. (org-on-heading-p))
  13559. (org-call-with-arg 'org-todo 'nextset))
  13560. (org-support-shift-select
  13561. (org-call-for-shift-select 'forward-word))
  13562. (t (org-shiftselect-error))))
  13563. (defun org-shiftcontrolleft ()
  13564. "Switch to previous TODO set."
  13565. (interactive)
  13566. (cond
  13567. ((and org-support-shift-select (org-region-active-p))
  13568. (org-call-for-shift-select 'backward-word))
  13569. ((and (not (eq org-support-shift-select 'always))
  13570. (org-on-heading-p))
  13571. (org-call-with-arg 'org-todo 'previousset))
  13572. (org-support-shift-select
  13573. (org-call-for-shift-select 'backward-word))
  13574. (t (org-shiftselect-error))))
  13575. (defun org-ctrl-c-ret ()
  13576. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13577. (interactive)
  13578. (cond
  13579. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13580. (t (call-interactively 'org-insert-heading))))
  13581. (defun org-copy-special ()
  13582. "Copy region in table or copy current subtree.
  13583. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13584. See the individual commands for more information."
  13585. (interactive)
  13586. (call-interactively
  13587. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13588. (defun org-cut-special ()
  13589. "Cut region in table or cut current subtree.
  13590. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13591. See the individual commands for more information."
  13592. (interactive)
  13593. (call-interactively
  13594. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13595. (defun org-paste-special (arg)
  13596. "Paste rectangular region into table, or past subtree relative to level.
  13597. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13598. See the individual commands for more information."
  13599. (interactive "P")
  13600. (if (org-at-table-p)
  13601. (org-table-paste-rectangle)
  13602. (org-paste-subtree arg)))
  13603. (defun org-edit-special ()
  13604. "Call a special editor for the stuff at point.
  13605. When at a table, call the formula editor with `org-table-edit-formulas'.
  13606. When at the first line of an src example, call `org-edit-src-code'.
  13607. When in an #+include line, visit the include file. Otherwise call
  13608. `ffap' to visit the file at point."
  13609. (interactive)
  13610. (cond
  13611. ((org-at-table-p)
  13612. (call-interactively 'org-table-edit-formulas))
  13613. ((save-excursion
  13614. (beginning-of-line 1)
  13615. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13616. (find-file (org-trim (match-string 1))))
  13617. ((org-edit-src-code))
  13618. ((org-edit-fixed-width-region))
  13619. (t (call-interactively 'ffap))))
  13620. (defun org-ctrl-c-ctrl-c (&optional arg)
  13621. "Set tags in headline, or update according to changed information at point.
  13622. This command does many different things, depending on context:
  13623. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13624. this is what we do.
  13625. - If the cursor is in a headline, prompt for tags and insert them
  13626. into the current line, aligned to `org-tags-column'. When called
  13627. with prefix arg, realign all tags in the current buffer.
  13628. - If the cursor is in one of the special #+KEYWORD lines, this
  13629. triggers scanning the buffer for these lines and updating the
  13630. information.
  13631. - If the cursor is inside a table, realign the table. This command
  13632. works even if the automatic table editor has been turned off.
  13633. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13634. the entire table.
  13635. - If the cursor is at a footnote reference or definition, jump to
  13636. the corresponding definition or references, respectively.
  13637. - If the cursor is a the beginning of a dynamic block, update it.
  13638. - If the cursor is inside a table created by the table.el package,
  13639. activate that table.
  13640. - If the current buffer is a remember buffer, close note and file
  13641. it. A prefix argument of 1 files to the default location
  13642. without further interaction. A prefix argument of 2 files to
  13643. the currently clocking task.
  13644. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13645. links in this buffer.
  13646. - If the cursor is on a numbered item in a plain list, renumber the
  13647. ordered list.
  13648. - If the cursor is on a checkbox, toggle it."
  13649. (interactive "P")
  13650. (let ((org-enable-table-editor t))
  13651. (cond
  13652. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13653. org-occur-highlights
  13654. org-latex-fragment-image-overlays)
  13655. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13656. (org-remove-occur-highlights)
  13657. (org-remove-latex-fragment-image-overlays)
  13658. (message "Temporary highlights/overlays removed from current buffer"))
  13659. ((and (local-variable-p 'org-finish-function (current-buffer))
  13660. (fboundp org-finish-function))
  13661. (funcall org-finish-function))
  13662. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13663. ((org-at-property-p)
  13664. (call-interactively 'org-property-action))
  13665. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13666. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13667. ((org-at-table.el-p)
  13668. (require 'table)
  13669. (beginning-of-line 1)
  13670. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13671. (call-interactively 'table-recognize-table))
  13672. ((org-at-table-p)
  13673. (org-table-maybe-eval-formula)
  13674. (if arg
  13675. (call-interactively 'org-table-recalculate)
  13676. (org-table-maybe-recalculate-line))
  13677. (call-interactively 'org-table-align))
  13678. ((or (org-footnote-at-reference-p)
  13679. (org-footnote-at-definition-p))
  13680. (call-interactively 'org-footnote-action))
  13681. ((org-at-item-checkbox-p)
  13682. (call-interactively 'org-toggle-checkbox))
  13683. ((org-at-item-p)
  13684. (if arg
  13685. (call-interactively 'org-toggle-checkbox)
  13686. (call-interactively 'org-maybe-renumber-ordered-list)))
  13687. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13688. ;; Dynamic block
  13689. (beginning-of-line 1)
  13690. (save-excursion (org-update-dblock)))
  13691. ((save-excursion
  13692. (beginning-of-line 1)
  13693. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13694. (cond
  13695. ((equal (match-string 1) "TBLFM")
  13696. ;; Recalculate the table before this line
  13697. (save-excursion
  13698. (beginning-of-line 1)
  13699. (skip-chars-backward " \r\n\t")
  13700. (if (org-at-table-p)
  13701. (org-call-with-arg 'org-table-recalculate t))))
  13702. (t
  13703. ; (org-set-regexps-and-options)
  13704. ; (org-restart-font-lock)
  13705. (let ((org-inhibit-startup t)) (org-mode-restart))
  13706. (message "Local setup has been refreshed"))))
  13707. ((org-clock-update-time-maybe))
  13708. (t (error "C-c C-c can do nothing useful at this location.")))))
  13709. (defun org-mode-restart ()
  13710. "Restart Org-mode, to scan again for special lines.
  13711. Also updates the keyword regular expressions."
  13712. (interactive)
  13713. (org-mode)
  13714. (message "Org-mode restarted"))
  13715. (defun org-kill-note-or-show-branches ()
  13716. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13717. (interactive)
  13718. (if (not org-finish-function)
  13719. (call-interactively 'show-branches)
  13720. (let ((org-note-abort t))
  13721. (funcall org-finish-function))))
  13722. (defun org-return (&optional indent)
  13723. "Goto next table row or insert a newline.
  13724. Calls `org-table-next-row' or `newline', depending on context.
  13725. See the individual commands for more information."
  13726. (interactive)
  13727. (cond
  13728. ((bobp) (if indent (newline-and-indent) (newline)))
  13729. ((org-at-table-p)
  13730. (org-table-justify-field-maybe)
  13731. (call-interactively 'org-table-next-row))
  13732. ((and org-return-follows-link
  13733. (eq (get-text-property (point) 'face) 'org-link))
  13734. (call-interactively 'org-open-at-point))
  13735. ((and (org-at-heading-p)
  13736. (looking-at
  13737. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13738. (org-show-entry)
  13739. (end-of-line 1)
  13740. (newline))
  13741. (t (if indent (newline-and-indent) (newline)))))
  13742. (defun org-return-indent ()
  13743. "Goto next table row or insert a newline and indent.
  13744. Calls `org-table-next-row' or `newline-and-indent', depending on
  13745. context. See the individual commands for more information."
  13746. (interactive)
  13747. (org-return t))
  13748. (defun org-ctrl-c-star ()
  13749. "Compute table, or change heading status of lines.
  13750. Calls `org-table-recalculate' or `org-toggle-heading',
  13751. depending on context."
  13752. (interactive)
  13753. (cond
  13754. ((org-at-table-p)
  13755. (call-interactively 'org-table-recalculate))
  13756. (t
  13757. ;; Convert all lines in region to list items
  13758. (call-interactively 'org-toggle-heading))))
  13759. (defun org-ctrl-c-minus ()
  13760. "Insert separator line in table or modify bullet status of line.
  13761. Also turns a plain line or a region of lines into list items.
  13762. Calls `org-table-insert-hline', `org-toggle-item', or
  13763. `org-cycle-list-bullet', depending on context."
  13764. (interactive)
  13765. (cond
  13766. ((org-at-table-p)
  13767. (call-interactively 'org-table-insert-hline))
  13768. ((org-region-active-p)
  13769. (call-interactively 'org-toggle-item))
  13770. ((org-in-item-p)
  13771. (call-interactively 'org-cycle-list-bullet))
  13772. (t
  13773. (call-interactively 'org-toggle-item))))
  13774. (defun org-toggle-item ()
  13775. "Convert headings or normal lines to items, items to normal lines.
  13776. If there is no active region, only the current line is considered.
  13777. If the first line in the region is a headline, convert all headlines to items.
  13778. If the first line in the region is an item, convert all items to normal lines.
  13779. If the first line is normal text, add an item bullet to each line."
  13780. (interactive)
  13781. (let (l2 l beg end)
  13782. (if (org-region-active-p)
  13783. (setq beg (region-beginning) end (region-end))
  13784. (setq beg (point-at-bol)
  13785. end (min (1+ (point-at-eol)) (point-max))))
  13786. (save-excursion
  13787. (goto-char end)
  13788. (setq l2 (org-current-line))
  13789. (goto-char beg)
  13790. (beginning-of-line 1)
  13791. (setq l (1- (org-current-line)))
  13792. (if (org-at-item-p)
  13793. ;; We already have items, de-itemize
  13794. (while (< (setq l (1+ l)) l2)
  13795. (when (org-at-item-p)
  13796. (goto-char (match-beginning 2))
  13797. (delete-region (match-beginning 2) (match-end 2))
  13798. (and (looking-at "[ \t]+") (replace-match "")))
  13799. (beginning-of-line 2))
  13800. (if (org-on-heading-p)
  13801. ;; Headings, convert to items
  13802. (while (< (setq l (1+ l)) l2)
  13803. (if (looking-at org-outline-regexp)
  13804. (replace-match "- " t t))
  13805. (beginning-of-line 2))
  13806. ;; normal lines, turn them into items
  13807. (while (< (setq l (1+ l)) l2)
  13808. (unless (org-at-item-p)
  13809. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13810. (replace-match "\\1- \\2")))
  13811. (beginning-of-line 2)))))))
  13812. (defun org-toggle-heading (&optional nstars)
  13813. "Convert headings to normal text, or items or text to headings.
  13814. If there is no active region, only the current line is considered.
  13815. If the first line is a heading, remove the stars from all headlines
  13816. in the region.
  13817. If the first line is a plain list item, turn all plain list items
  13818. into headings.
  13819. If the first line is a normal line, turn each and every line in the
  13820. region into a heading.
  13821. When converting a line into a heading, the number of stars is chosen
  13822. such that the lines become children of the current entry. However,
  13823. when a prefix argument is given, its value determines the number of
  13824. stars to add."
  13825. (interactive "P")
  13826. (let (l2 l itemp beg end)
  13827. (if (org-region-active-p)
  13828. (setq beg (region-beginning) end (region-end))
  13829. (setq beg (point-at-bol)
  13830. end (min (1+ (point-at-eol)) (point-max))))
  13831. (save-excursion
  13832. (goto-char end)
  13833. (setq l2 (org-current-line))
  13834. (goto-char beg)
  13835. (beginning-of-line 1)
  13836. (setq l (1- (org-current-line)))
  13837. (if (org-on-heading-p)
  13838. ;; We already have headlines, de-star them
  13839. (while (< (setq l (1+ l)) l2)
  13840. (when (org-on-heading-p t)
  13841. (and (looking-at outline-regexp) (replace-match "")))
  13842. (beginning-of-line 2))
  13843. (setq itemp (org-at-item-p))
  13844. (let* ((stars
  13845. (if nstars
  13846. (make-string (prefix-numeric-value current-prefix-arg)
  13847. ?*)
  13848. (save-excursion
  13849. (if (re-search-backward org-complex-heading-regexp nil t)
  13850. (match-string 1) ""))))
  13851. (add-stars (cond (nstars "")
  13852. ((equal stars "") "*")
  13853. (org-odd-levels-only "**")
  13854. (t "*")))
  13855. (rpl (concat stars add-stars " ")))
  13856. (while (< (setq l (1+ l)) l2)
  13857. (if itemp
  13858. (and (org-at-item-p) (replace-match rpl t t))
  13859. (unless (org-on-heading-p)
  13860. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13861. (replace-match (concat rpl (match-string 2))))))
  13862. (beginning-of-line 2)))))))
  13863. (defun org-meta-return (&optional arg)
  13864. "Insert a new heading or wrap a region in a table.
  13865. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13866. See the individual commands for more information."
  13867. (interactive "P")
  13868. (cond
  13869. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13870. ((org-at-table-p)
  13871. (call-interactively 'org-table-wrap-region))
  13872. (t (call-interactively 'org-insert-heading))))
  13873. ;;; Menu entries
  13874. ;; Define the Org-mode menus
  13875. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13876. '("Tbl"
  13877. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13878. ["Next Field" org-cycle (org-at-table-p)]
  13879. ["Previous Field" org-shifttab (org-at-table-p)]
  13880. ["Next Row" org-return (org-at-table-p)]
  13881. "--"
  13882. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13883. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13884. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13885. "--"
  13886. ("Column"
  13887. ["Move Column Left" org-metaleft (org-at-table-p)]
  13888. ["Move Column Right" org-metaright (org-at-table-p)]
  13889. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13890. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13891. ("Row"
  13892. ["Move Row Up" org-metaup (org-at-table-p)]
  13893. ["Move Row Down" org-metadown (org-at-table-p)]
  13894. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13895. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13896. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13897. "--"
  13898. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13899. ("Rectangle"
  13900. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13901. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13902. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13903. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13904. "--"
  13905. ("Calculate"
  13906. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13907. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13908. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13909. "--"
  13910. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13911. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13912. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13913. "--"
  13914. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13915. "--"
  13916. ["Sum Column/Rectangle" org-table-sum
  13917. (or (org-at-table-p) (org-region-active-p))]
  13918. ["Which Column?" org-table-current-column (org-at-table-p)])
  13919. ["Debug Formulas"
  13920. org-table-toggle-formula-debugger
  13921. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13922. ["Show Col/Row Numbers"
  13923. org-table-toggle-coordinate-overlays
  13924. :style toggle
  13925. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  13926. "--"
  13927. ["Create" org-table-create (and (not (org-at-table-p))
  13928. org-enable-table-editor)]
  13929. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13930. ["Import from File" org-table-import (not (org-at-table-p))]
  13931. ["Export to File" org-table-export (org-at-table-p)]
  13932. "--"
  13933. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13934. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13935. '("Org"
  13936. ("Show/Hide"
  13937. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  13938. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  13939. ["Sparse Tree..." org-sparse-tree t]
  13940. ["Reveal Context" org-reveal t]
  13941. ["Show All" show-all t]
  13942. "--"
  13943. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  13944. "--"
  13945. ["New Heading" org-insert-heading t]
  13946. ("Navigate Headings"
  13947. ["Up" outline-up-heading t]
  13948. ["Next" outline-next-visible-heading t]
  13949. ["Previous" outline-previous-visible-heading t]
  13950. ["Next Same Level" outline-forward-same-level t]
  13951. ["Previous Same Level" outline-backward-same-level t]
  13952. "--"
  13953. ["Jump" org-goto t])
  13954. ("Edit Structure"
  13955. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13956. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13957. "--"
  13958. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13959. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13960. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13961. "--"
  13962. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  13963. "--"
  13964. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13965. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13966. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13967. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13968. "--"
  13969. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  13970. "--"
  13971. ["Convert to odd levels" org-convert-to-odd-levels t]
  13972. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13973. ("Editing"
  13974. ["Emphasis..." org-emphasize t]
  13975. ["Edit Source Example" org-edit-special t]
  13976. "--"
  13977. ["Footnote new/jump" org-footnote-action t]
  13978. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  13979. ("Archive"
  13980. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  13981. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  13982. ; :active t :keys "C-u C-c C-x C-a"]
  13983. ["Sparse trees open ARCHIVE trees"
  13984. (setq org-sparse-tree-open-archived-trees
  13985. (not org-sparse-tree-open-archived-trees))
  13986. :style toggle :selected org-sparse-tree-open-archived-trees]
  13987. ["Cycling opens ARCHIVE trees"
  13988. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  13989. :style toggle :selected org-cycle-open-archived-trees]
  13990. "--"
  13991. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  13992. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  13993. ; ["Check and Move Children" (org-archive-subtree '(4))
  13994. ; :active t :keys "C-u C-c C-x C-s"]
  13995. )
  13996. "--"
  13997. ("Hyperlinks"
  13998. ["Store Link (Global)" org-store-link t]
  13999. ["Find existing link to here" org-occur-link-in-agenda-files t]
  14000. ["Insert Link" org-insert-link t]
  14001. ["Follow Link" org-open-at-point t]
  14002. "--"
  14003. ["Next link" org-next-link t]
  14004. ["Previous link" org-previous-link t]
  14005. "--"
  14006. ["Descriptive Links"
  14007. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  14008. :style radio
  14009. :selected (member '(org-link) buffer-invisibility-spec)]
  14010. ["Literal Links"
  14011. (progn
  14012. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  14013. :style radio
  14014. :selected (not (member '(org-link) buffer-invisibility-spec))])
  14015. "--"
  14016. ("TODO Lists"
  14017. ["TODO/DONE/-" org-todo t]
  14018. ("Select keyword"
  14019. ["Next keyword" org-shiftright (org-on-heading-p)]
  14020. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14021. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  14022. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  14023. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  14024. ["Show TODO Tree" org-show-todo-tree t]
  14025. ["Global TODO list" org-todo-list t]
  14026. "--"
  14027. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  14028. :selected org-enforce-todo-dependencies :style toggle :active t]
  14029. "Settings for tree at point"
  14030. ["Do Children sequentially" org-toggle-ordered-property :style radio
  14031. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  14032. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14033. ["Do Children parallel" org-toggle-ordered-property :style radio
  14034. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  14035. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14036. "--"
  14037. ["Set Priority" org-priority t]
  14038. ["Priority Up" org-shiftup t]
  14039. ["Priority Down" org-shiftdown t]
  14040. "--"
  14041. ["Get news from all feeds" org-feed-update-all t]
  14042. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14043. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14044. ("TAGS and Properties"
  14045. ["Set Tags" org-set-tags-command t]
  14046. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14047. "--"
  14048. ["Set property" org-set-property t]
  14049. ["Column view of properties" org-columns t]
  14050. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14051. ("Dates and Scheduling"
  14052. ["Timestamp" org-time-stamp t]
  14053. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14054. ("Change Date"
  14055. ["1 Day Later" org-shiftright t]
  14056. ["1 Day Earlier" org-shiftleft t]
  14057. ["1 ... Later" org-shiftup t]
  14058. ["1 ... Earlier" org-shiftdown t])
  14059. ["Compute Time Range" org-evaluate-time-range t]
  14060. ["Schedule Item" org-schedule t]
  14061. ["Deadline" org-deadline t]
  14062. "--"
  14063. ["Custom time format" org-toggle-time-stamp-overlays
  14064. :style radio :selected org-display-custom-times]
  14065. "--"
  14066. ["Goto Calendar" org-goto-calendar t]
  14067. ["Date from Calendar" org-date-from-calendar t]
  14068. "--"
  14069. ["Start/Restart Timer" org-timer-start t]
  14070. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14071. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14072. ["Insert Timer String" org-timer t]
  14073. ["Insert Timer Item" org-timer-item t])
  14074. ("Logging work"
  14075. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  14076. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  14077. ["Clock out" org-clock-out t]
  14078. ["Clock cancel" org-clock-cancel t]
  14079. "--"
  14080. ["Mark as default task" org-clock-mark-default-task t]
  14081. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  14082. ["Goto running clock" org-clock-goto t]
  14083. "--"
  14084. ["Display times" org-clock-display t]
  14085. ["Create clock table" org-clock-report t]
  14086. "--"
  14087. ["Record DONE time"
  14088. (progn (setq org-log-done (not org-log-done))
  14089. (message "Switching to %s will %s record a timestamp"
  14090. (car org-done-keywords)
  14091. (if org-log-done "automatically" "not")))
  14092. :style toggle :selected org-log-done])
  14093. "--"
  14094. ["Agenda Command..." org-agenda t]
  14095. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14096. ("File List for Agenda")
  14097. ("Special views current file"
  14098. ["TODO Tree" org-show-todo-tree t]
  14099. ["Check Deadlines" org-check-deadlines t]
  14100. ["Timeline" org-timeline t]
  14101. ["Tags/Property tree" org-match-sparse-tree t])
  14102. "--"
  14103. ["Export/Publish..." org-export t]
  14104. ("LaTeX"
  14105. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14106. :selected org-cdlatex-mode]
  14107. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14108. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14109. ["Modify math symbol" org-cdlatex-math-modify
  14110. (org-inside-LaTeX-fragment-p)]
  14111. ["Insert citation" org-reftex-citation t]
  14112. "--"
  14113. ["Export LaTeX fragments as images"
  14114. (if (featurep 'org-exp)
  14115. (setq org-export-with-LaTeX-fragments
  14116. (not org-export-with-LaTeX-fragments))
  14117. (require 'org-exp))
  14118. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14119. org-export-with-LaTeX-fragments)])
  14120. "--"
  14121. ("Documentation"
  14122. ["Show Version" org-version t]
  14123. ["Info Documentation" org-info t])
  14124. ("Customize"
  14125. ["Browse Org Group" org-customize t]
  14126. "--"
  14127. ["Expand This Menu" org-create-customize-menu
  14128. (fboundp 'customize-menu-create)])
  14129. ["Send bug report" org-submit-bug-report t]
  14130. "--"
  14131. ("Refresh/Reload"
  14132. ["Refresh setup current buffer" org-mode-restart t]
  14133. ["Reload Org (after update)" org-reload t]
  14134. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14135. ))
  14136. (defun org-info (&optional node)
  14137. "Read documentation for Org-mode in the info system.
  14138. With optional NODE, go directly to that node."
  14139. (interactive)
  14140. (info (format "(org)%s" (or node ""))))
  14141. ;;;###autoload
  14142. (defun org-submit-bug-report ()
  14143. "Submit a bug report on Org-mode via mail.
  14144. Don't hesitate to report any problems or inaccurate documentation.
  14145. If you don't have setup sending mail from (X)Emacs, please copy the
  14146. output buffer into your mail program, as it gives us important
  14147. information about your Org-mode version and configuration."
  14148. (interactive)
  14149. (require 'reporter)
  14150. (org-load-modules-maybe)
  14151. (org-require-autoloaded-modules)
  14152. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  14153. (reporter-submit-bug-report
  14154. "emacs-orgmode@gnu.org"
  14155. (org-version)
  14156. (let (list)
  14157. (save-window-excursion
  14158. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  14159. (delete-other-windows)
  14160. (erase-buffer)
  14161. (insert "You are about to submit a bug report to the Org-mode mailing list.
  14162. We would like to add your full Org-mode and Outline configuration to the
  14163. bug report. This greatly simplifies the work of the maintainer and
  14164. other experts on the mailing list.
  14165. HOWEVER, some variables you have customized may contain private
  14166. information. The names of customers, colleagues, or friends, might
  14167. appear in the form of file names, tags, todo states, or search strings.
  14168. If you answer yes to the prompt, you might want to check and remove
  14169. such private information before sending the email.")
  14170. (add-text-properties (point-min) (point-max) '(face org-warning))
  14171. (when (yes-or-no-p "Include your Org-mode configuration ")
  14172. (mapatoms
  14173. (lambda (v)
  14174. (and (boundp v)
  14175. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  14176. (or (and (symbol-value v)
  14177. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  14178. (and
  14179. (get v 'custom-type) (get v 'standard-value)
  14180. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  14181. (push v list)))))
  14182. (kill-buffer (get-buffer "*Warn about privacy*"))
  14183. list))
  14184. nil nil
  14185. "Remember to cover the basics, that is, what you expected to happen and
  14186. what in fact did happen. You don't know hoe to make a good report? See
  14187. http://orgmode.org/manual/Feedback.html#Feedback
  14188. Your bug report will be posted to the Org-mode mailing list.
  14189. ------------------------------------------------------------------------")))
  14190. (defun org-install-agenda-files-menu ()
  14191. (let ((bl (buffer-list)))
  14192. (save-excursion
  14193. (while bl
  14194. (set-buffer (pop bl))
  14195. (if (org-mode-p) (setq bl nil)))
  14196. (when (org-mode-p)
  14197. (easy-menu-change
  14198. '("Org") "File List for Agenda"
  14199. (append
  14200. (list
  14201. ["Edit File List" (org-edit-agenda-file-list) t]
  14202. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14203. ["Remove Current File from List" org-remove-file t]
  14204. ["Cycle through agenda files" org-cycle-agenda-files t]
  14205. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14206. "--")
  14207. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14208. ;;;; Documentation
  14209. ;;;###autoload
  14210. (defun org-require-autoloaded-modules ()
  14211. (interactive)
  14212. (mapc 'require
  14213. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14214. org-docbook org-exp org-html org-icalendar
  14215. org-id org-latex
  14216. org-publish org-remember org-table
  14217. org-timer org-xoxo)))
  14218. ;;;###autoload
  14219. (defun org-reload (&optional uncompiled)
  14220. "Reload all org lisp files.
  14221. With prefix arg UNCOMPILED, load the uncompiled versions."
  14222. (interactive "P")
  14223. (require 'find-func)
  14224. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14225. (dir-org (file-name-directory (org-find-library-name "org")))
  14226. (dir-org-contrib (ignore-errors
  14227. (file-name-directory
  14228. (org-find-library-name "org-contribdir"))))
  14229. (files
  14230. (append (directory-files dir-org t file-re)
  14231. (and dir-org-contrib
  14232. (directory-files dir-org-contrib t file-re))))
  14233. (remove-re (concat (if (featurep 'xemacs)
  14234. "org-colview" "org-colview-xemacs")
  14235. "\\'")))
  14236. (setq files (mapcar 'file-name-sans-extension files))
  14237. (setq files (mapcar
  14238. (lambda (x) (if (string-match remove-re x) nil x))
  14239. files))
  14240. (setq files (delq nil files))
  14241. (mapc
  14242. (lambda (f)
  14243. (when (featurep (intern (file-name-nondirectory f)))
  14244. (if (and (not uncompiled)
  14245. (file-exists-p (concat f ".elc")))
  14246. (load (concat f ".elc") nil nil t)
  14247. (load (concat f ".el") nil nil t))))
  14248. files))
  14249. (org-version))
  14250. ;;;###autoload
  14251. (defun org-customize ()
  14252. "Call the customize function with org as argument."
  14253. (interactive)
  14254. (org-load-modules-maybe)
  14255. (org-require-autoloaded-modules)
  14256. (customize-browse 'org))
  14257. (defun org-create-customize-menu ()
  14258. "Create a full customization menu for Org-mode, insert it into the menu."
  14259. (interactive)
  14260. (org-load-modules-maybe)
  14261. (org-require-autoloaded-modules)
  14262. (if (fboundp 'customize-menu-create)
  14263. (progn
  14264. (easy-menu-change
  14265. '("Org") "Customize"
  14266. `(["Browse Org group" org-customize t]
  14267. "--"
  14268. ,(customize-menu-create 'org)
  14269. ["Set" Custom-set t]
  14270. ["Save" Custom-save t]
  14271. ["Reset to Current" Custom-reset-current t]
  14272. ["Reset to Saved" Custom-reset-saved t]
  14273. ["Reset to Standard Settings" Custom-reset-standard t]))
  14274. (message "\"Org\"-menu now contains full customization menu"))
  14275. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14276. ;;;; Miscellaneous stuff
  14277. ;;; Generally useful functions
  14278. (defun org-find-text-property-in-string (prop s)
  14279. "Return the first non-nil value of property PROP in string S."
  14280. (or (get-text-property 0 prop s)
  14281. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14282. prop s)))
  14283. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14284. "Display the given MESSAGE as a warning."
  14285. (if (fboundp 'display-warning)
  14286. (display-warning 'org message
  14287. (if (featurep 'xemacs)
  14288. 'warning
  14289. :warning))
  14290. (let ((buf (get-buffer-create "*Org warnings*")))
  14291. (with-current-buffer buf
  14292. (goto-char (point-max))
  14293. (insert "Warning (Org): " message)
  14294. (unless (bolp)
  14295. (newline)))
  14296. (display-buffer buf)
  14297. (sit-for 0))))
  14298. (defun org-in-commented-line ()
  14299. "Is point in a line starting with `#'?"
  14300. (equal (char-after (point-at-bol)) ?#))
  14301. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14302. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14303. (if (and marker (marker-buffer marker)
  14304. (buffer-live-p (marker-buffer marker)))
  14305. (progn
  14306. (switch-to-buffer (marker-buffer marker))
  14307. (if (or (> marker (point-max)) (< marker (point-min)))
  14308. (widen))
  14309. (goto-char marker)
  14310. (org-show-context 'org-goto))
  14311. (if bookmark
  14312. (bookmark-jump bookmark)
  14313. (error "Cannot find location"))))
  14314. (defun org-quote-csv-field (s)
  14315. "Quote field for inclusion in CSV material."
  14316. (if (string-match "[\",]" s)
  14317. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14318. s))
  14319. (defun org-plist-delete (plist property)
  14320. "Delete PROPERTY from PLIST.
  14321. This is in contrast to merely setting it to 0."
  14322. (let (p)
  14323. (while plist
  14324. (if (not (eq property (car plist)))
  14325. (setq p (plist-put p (car plist) (nth 1 plist))))
  14326. (setq plist (cddr plist)))
  14327. p))
  14328. (defun org-force-self-insert (N)
  14329. "Needed to enforce self-insert under remapping."
  14330. (interactive "p")
  14331. (self-insert-command N))
  14332. (defun org-string-width (s)
  14333. "Compute width of string, ignoring invisible characters.
  14334. This ignores character with invisibility property `org-link', and also
  14335. characters with property `org-cwidth', because these will become invisible
  14336. upon the next fontification round."
  14337. (let (b l)
  14338. (when (or (eq t buffer-invisibility-spec)
  14339. (assq 'org-link buffer-invisibility-spec))
  14340. (while (setq b (text-property-any 0 (length s)
  14341. 'invisible 'org-link s))
  14342. (setq s (concat (substring s 0 b)
  14343. (substring s (or (next-single-property-change
  14344. b 'invisible s) (length s)))))))
  14345. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14346. (setq s (concat (substring s 0 b)
  14347. (substring s (or (next-single-property-change
  14348. b 'org-cwidth s) (length s))))))
  14349. (setq l (string-width s) b -1)
  14350. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14351. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14352. l))
  14353. (defun org-get-indentation (&optional line)
  14354. "Get the indentation of the current line, interpreting tabs.
  14355. When LINE is given, assume it represents a line and compute its indentation."
  14356. (if line
  14357. (if (string-match "^ *" (org-remove-tabs line))
  14358. (match-end 0))
  14359. (save-excursion
  14360. (beginning-of-line 1)
  14361. (skip-chars-forward " \t")
  14362. (current-column))))
  14363. (defun org-remove-tabs (s &optional width)
  14364. "Replace tabulators in S with spaces.
  14365. Assumes that s is a single line, starting in column 0."
  14366. (setq width (or width tab-width))
  14367. (while (string-match "\t" s)
  14368. (setq s (replace-match
  14369. (make-string
  14370. (- (* width (/ (+ (match-beginning 0) width) width))
  14371. (match-beginning 0)) ?\ )
  14372. t t s)))
  14373. s)
  14374. (defun org-fix-indentation (line ind)
  14375. "Fix indentation in LINE.
  14376. IND is a cons cell with target and minimum indentation.
  14377. If the current indentation in LINE is smaller than the minimum,
  14378. leave it alone. If it is larger than ind, set it to the target."
  14379. (let* ((l (org-remove-tabs line))
  14380. (i (org-get-indentation l))
  14381. (i1 (car ind)) (i2 (cdr ind)))
  14382. (if (>= i i2) (setq l (substring line i2)))
  14383. (if (> i1 0)
  14384. (concat (make-string i1 ?\ ) l)
  14385. l)))
  14386. (defun org-remove-indentation (code &optional n)
  14387. "Remove the maximum common indentation from the lines in CODE.
  14388. N may optionally be the number of spaces to remove."
  14389. (with-temp-buffer
  14390. (insert code)
  14391. (org-do-remove-indentation n)
  14392. (buffer-string)))
  14393. (defun org-do-remove-indentation (&optional n)
  14394. "Remove the maximum common indentation from the buffer."
  14395. (untabify (point-min) (point-max))
  14396. (let ((min 10000) re)
  14397. (if n
  14398. (setq min n)
  14399. (goto-char (point-min))
  14400. (while (re-search-forward "^ *[^ \n]" nil t)
  14401. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14402. (unless (or (= min 0) (= min 10000))
  14403. (setq re (format "^ \\{%d\\}" min))
  14404. (goto-char (point-min))
  14405. (while (re-search-forward re nil t)
  14406. (replace-match "")
  14407. (end-of-line 1))
  14408. min)))
  14409. (defun org-base-buffer (buffer)
  14410. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14411. (if (not buffer)
  14412. buffer
  14413. (or (buffer-base-buffer buffer)
  14414. buffer)))
  14415. (defun org-trim (s)
  14416. "Remove whitespace at beginning and end of string."
  14417. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14418. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14419. s)
  14420. (defun org-wrap (string &optional width lines)
  14421. "Wrap string to either a number of lines, or a width in characters.
  14422. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14423. that costs. If there is a word longer than WIDTH, the text is actually
  14424. wrapped to the length of that word.
  14425. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14426. many lines, whatever width that takes.
  14427. The return value is a list of lines, without newlines at the end."
  14428. (let* ((words (org-split-string string "[ \t\n]+"))
  14429. (maxword (apply 'max (mapcar 'org-string-width words)))
  14430. w ll)
  14431. (cond (width
  14432. (org-do-wrap words (max maxword width)))
  14433. (lines
  14434. (setq w maxword)
  14435. (setq ll (org-do-wrap words maxword))
  14436. (if (<= (length ll) lines)
  14437. ll
  14438. (setq ll words)
  14439. (while (> (length ll) lines)
  14440. (setq w (1+ w))
  14441. (setq ll (org-do-wrap words w)))
  14442. ll))
  14443. (t (error "Cannot wrap this")))))
  14444. (defun org-do-wrap (words width)
  14445. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14446. (let (lines line)
  14447. (while words
  14448. (setq line (pop words))
  14449. (while (and words (< (+ (length line) (length (car words))) width))
  14450. (setq line (concat line " " (pop words))))
  14451. (setq lines (push line lines)))
  14452. (nreverse lines)))
  14453. (defun org-split-string (string &optional separators)
  14454. "Splits STRING into substrings at SEPARATORS.
  14455. No empty strings are returned if there are matches at the beginning
  14456. and end of string."
  14457. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14458. (start 0)
  14459. notfirst
  14460. (list nil))
  14461. (while (and (string-match rexp string
  14462. (if (and notfirst
  14463. (= start (match-beginning 0))
  14464. (< start (length string)))
  14465. (1+ start) start))
  14466. (< (match-beginning 0) (length string)))
  14467. (setq notfirst t)
  14468. (or (eq (match-beginning 0) 0)
  14469. (and (eq (match-beginning 0) (match-end 0))
  14470. (eq (match-beginning 0) start))
  14471. (setq list
  14472. (cons (substring string start (match-beginning 0))
  14473. list)))
  14474. (setq start (match-end 0)))
  14475. (or (eq start (length string))
  14476. (setq list
  14477. (cons (substring string start)
  14478. list)))
  14479. (nreverse list)))
  14480. (defun org-quote-vert (s)
  14481. "Replace \"|\" with \"\\vert\"."
  14482. (while (string-match "|" s)
  14483. (setq s (replace-match "\\vert" t t s)))
  14484. s)
  14485. (defun org-uuidgen-p (s)
  14486. "Is S an ID created by UUIDGEN?"
  14487. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14488. (defun org-context ()
  14489. "Return a list of contexts of the current cursor position.
  14490. If several contexts apply, all are returned.
  14491. Each context entry is a list with a symbol naming the context, and
  14492. two positions indicating start and end of the context. Possible
  14493. contexts are:
  14494. :headline anywhere in a headline
  14495. :headline-stars on the leading stars in a headline
  14496. :todo-keyword on a TODO keyword (including DONE) in a headline
  14497. :tags on the TAGS in a headline
  14498. :priority on the priority cookie in a headline
  14499. :item on the first line of a plain list item
  14500. :item-bullet on the bullet/number of a plain list item
  14501. :checkbox on the checkbox in a plain list item
  14502. :table in an org-mode table
  14503. :table-special on a special filed in a table
  14504. :table-table in a table.el table
  14505. :link on a hyperlink
  14506. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14507. :target on a <<target>>
  14508. :radio-target on a <<<radio-target>>>
  14509. :latex-fragment on a LaTeX fragment
  14510. :latex-preview on a LaTeX fragment with overlayed preview image
  14511. This function expects the position to be visible because it uses font-lock
  14512. faces as a help to recognize the following contexts: :table-special, :link,
  14513. and :keyword."
  14514. (let* ((f (get-text-property (point) 'face))
  14515. (faces (if (listp f) f (list f)))
  14516. (p (point)) clist o)
  14517. ;; First the large context
  14518. (cond
  14519. ((org-on-heading-p t)
  14520. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14521. (when (progn
  14522. (beginning-of-line 1)
  14523. (looking-at org-todo-line-tags-regexp))
  14524. (push (org-point-in-group p 1 :headline-stars) clist)
  14525. (push (org-point-in-group p 2 :todo-keyword) clist)
  14526. (push (org-point-in-group p 4 :tags) clist))
  14527. (goto-char p)
  14528. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14529. (if (looking-at "\\[#[A-Z0-9]\\]")
  14530. (push (org-point-in-group p 0 :priority) clist)))
  14531. ((org-at-item-p)
  14532. (push (org-point-in-group p 2 :item-bullet) clist)
  14533. (push (list :item (point-at-bol)
  14534. (save-excursion (org-end-of-item) (point)))
  14535. clist)
  14536. (and (org-at-item-checkbox-p)
  14537. (push (org-point-in-group p 0 :checkbox) clist)))
  14538. ((org-at-table-p)
  14539. (push (list :table (org-table-begin) (org-table-end)) clist)
  14540. (if (memq 'org-formula faces)
  14541. (push (list :table-special
  14542. (previous-single-property-change p 'face)
  14543. (next-single-property-change p 'face)) clist)))
  14544. ((org-at-table-p 'any)
  14545. (push (list :table-table) clist)))
  14546. (goto-char p)
  14547. ;; Now the small context
  14548. (cond
  14549. ((org-at-timestamp-p)
  14550. (push (org-point-in-group p 0 :timestamp) clist))
  14551. ((memq 'org-link faces)
  14552. (push (list :link
  14553. (previous-single-property-change p 'face)
  14554. (next-single-property-change p 'face)) clist))
  14555. ((memq 'org-special-keyword faces)
  14556. (push (list :keyword
  14557. (previous-single-property-change p 'face)
  14558. (next-single-property-change p 'face)) clist))
  14559. ((org-on-target-p)
  14560. (push (org-point-in-group p 0 :target) clist)
  14561. (goto-char (1- (match-beginning 0)))
  14562. (if (looking-at org-radio-target-regexp)
  14563. (push (org-point-in-group p 0 :radio-target) clist))
  14564. (goto-char p))
  14565. ((setq o (car (delq nil
  14566. (mapcar
  14567. (lambda (x)
  14568. (if (memq x org-latex-fragment-image-overlays) x))
  14569. (org-overlays-at (point))))))
  14570. (push (list :latex-fragment
  14571. (org-overlay-start o) (org-overlay-end o)) clist)
  14572. (push (list :latex-preview
  14573. (org-overlay-start o) (org-overlay-end o)) clist))
  14574. ((org-inside-LaTeX-fragment-p)
  14575. ;; FIXME: positions wrong.
  14576. (push (list :latex-fragment (point) (point)) clist)))
  14577. (setq clist (nreverse (delq nil clist)))
  14578. clist))
  14579. ;; FIXME: Compare with at-regexp-p Do we need both?
  14580. (defun org-in-regexp (re &optional nlines visually)
  14581. "Check if point is inside a match of regexp.
  14582. Normally only the current line is checked, but you can include NLINES extra
  14583. lines both before and after point into the search.
  14584. If VISUALLY is set, require that the cursor is not after the match but
  14585. really on, so that the block visually is on the match."
  14586. (catch 'exit
  14587. (let ((pos (point))
  14588. (eol (point-at-eol (+ 1 (or nlines 0))))
  14589. (inc (if visually 1 0)))
  14590. (save-excursion
  14591. (beginning-of-line (- 1 (or nlines 0)))
  14592. (while (re-search-forward re eol t)
  14593. (if (and (<= (match-beginning 0) pos)
  14594. (>= (+ inc (match-end 0)) pos))
  14595. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14596. (defun org-at-regexp-p (regexp)
  14597. "Is point inside a match of REGEXP in the current line?"
  14598. (catch 'exit
  14599. (save-excursion
  14600. (let ((pos (point)) (end (point-at-eol)))
  14601. (beginning-of-line 1)
  14602. (while (re-search-forward regexp end t)
  14603. (if (and (<= (match-beginning 0) pos)
  14604. (>= (match-end 0) pos))
  14605. (throw 'exit t)))
  14606. nil))))
  14607. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14608. "Call `multi-occur' with buffers for all agenda files."
  14609. (interactive "sOrg-files matching: \np")
  14610. (let* ((files (org-agenda-files))
  14611. (tnames (mapcar 'file-truename files))
  14612. (extra org-agenda-text-search-extra-files)
  14613. f)
  14614. (when (eq (car extra) 'agenda-archives)
  14615. (setq extra (cdr extra))
  14616. (setq files (org-add-archive-files files)))
  14617. (while (setq f (pop extra))
  14618. (unless (member (file-truename f) tnames)
  14619. (add-to-list 'files f 'append)
  14620. (add-to-list 'tnames (file-truename f) 'append)))
  14621. (multi-occur
  14622. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14623. regexp)))
  14624. (if (boundp 'occur-mode-find-occurrence-hook)
  14625. ;; Emacs 23
  14626. (add-hook 'occur-mode-find-occurrence-hook
  14627. (lambda ()
  14628. (when (org-mode-p)
  14629. (org-reveal))))
  14630. ;; Emacs 22
  14631. (defadvice occur-mode-goto-occurrence
  14632. (after org-occur-reveal activate)
  14633. (and (org-mode-p) (org-reveal)))
  14634. (defadvice occur-mode-goto-occurrence-other-window
  14635. (after org-occur-reveal activate)
  14636. (and (org-mode-p) (org-reveal)))
  14637. (defadvice occur-mode-display-occurrence
  14638. (after org-occur-reveal activate)
  14639. (when (org-mode-p)
  14640. (let ((pos (occur-mode-find-occurrence)))
  14641. (with-current-buffer (marker-buffer pos)
  14642. (save-excursion
  14643. (goto-char pos)
  14644. (org-reveal)))))))
  14645. (defun org-occur-link-in-agenda-files ()
  14646. "Create a link and search for it in the agendas.
  14647. The link is not stored in `org-stored-links', it is just created
  14648. for the search purpose."
  14649. (interactive)
  14650. (let ((link (condition-case nil
  14651. (org-store-link nil)
  14652. (error "Unable to create a link to here"))))
  14653. (org-occur-in-agenda-files (regexp-quote link))))
  14654. (defun org-uniquify (list)
  14655. "Remove duplicate elements from LIST."
  14656. (let (res)
  14657. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14658. res))
  14659. (defun org-delete-all (elts list)
  14660. "Remove all elements in ELTS from LIST."
  14661. (while elts
  14662. (setq list (delete (pop elts) list)))
  14663. list)
  14664. (defun org-back-over-empty-lines ()
  14665. "Move backwards over whitespace, to the beginning of the first empty line.
  14666. Returns the number of empty lines passed."
  14667. (let ((pos (point)))
  14668. (skip-chars-backward " \t\n\r")
  14669. (beginning-of-line 2)
  14670. (goto-char (min (point) pos))
  14671. (count-lines (point) pos)))
  14672. (defun org-skip-whitespace ()
  14673. (skip-chars-forward " \t\n\r"))
  14674. (defun org-point-in-group (point group &optional context)
  14675. "Check if POINT is in match-group GROUP.
  14676. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14677. match. If the match group does ot exist or point is not inside it,
  14678. return nil."
  14679. (and (match-beginning group)
  14680. (>= point (match-beginning group))
  14681. (<= point (match-end group))
  14682. (if context
  14683. (list context (match-beginning group) (match-end group))
  14684. t)))
  14685. (defun org-switch-to-buffer-other-window (&rest args)
  14686. "Switch to buffer in a second window on the current frame.
  14687. In particular, do not allow pop-up frames."
  14688. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14689. special-display-function)
  14690. (apply 'switch-to-buffer-other-window args)))
  14691. (defun org-combine-plists (&rest plists)
  14692. "Create a single property list from all plists in PLISTS.
  14693. The process starts by copying the first list, and then setting properties
  14694. from the other lists. Settings in the last list are the most significant
  14695. ones and overrule settings in the other lists."
  14696. (let ((rtn (copy-sequence (pop plists)))
  14697. p v ls)
  14698. (while plists
  14699. (setq ls (pop plists))
  14700. (while ls
  14701. (setq p (pop ls) v (pop ls))
  14702. (setq rtn (plist-put rtn p v))))
  14703. rtn))
  14704. (defun org-move-line-down (arg)
  14705. "Move the current line down. With prefix argument, move it past ARG lines."
  14706. (interactive "p")
  14707. (let ((col (current-column))
  14708. beg end pos)
  14709. (beginning-of-line 1) (setq beg (point))
  14710. (beginning-of-line 2) (setq end (point))
  14711. (beginning-of-line (+ 1 arg))
  14712. (setq pos (move-marker (make-marker) (point)))
  14713. (insert (delete-and-extract-region beg end))
  14714. (goto-char pos)
  14715. (org-move-to-column col)))
  14716. (defun org-move-line-up (arg)
  14717. "Move the current line up. With prefix argument, move it past ARG lines."
  14718. (interactive "p")
  14719. (let ((col (current-column))
  14720. beg end pos)
  14721. (beginning-of-line 1) (setq beg (point))
  14722. (beginning-of-line 2) (setq end (point))
  14723. (beginning-of-line (- arg))
  14724. (setq pos (move-marker (make-marker) (point)))
  14725. (insert (delete-and-extract-region beg end))
  14726. (goto-char pos)
  14727. (org-move-to-column col)))
  14728. (defun org-replace-escapes (string table)
  14729. "Replace %-escapes in STRING with values in TABLE.
  14730. TABLE is an association list with keys like \"%a\" and string values.
  14731. The sequences in STRING may contain normal field width and padding information,
  14732. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14733. so values can contain further %-escapes if they are define later in TABLE."
  14734. (let ((case-fold-search nil)
  14735. e re rpl)
  14736. (while (setq e (pop table))
  14737. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14738. (while (string-match re string)
  14739. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14740. (cdr e)))
  14741. (setq string (replace-match rpl t t string))))
  14742. string))
  14743. (defun org-sublist (list start end)
  14744. "Return a section of LIST, from START to END.
  14745. Counting starts at 1."
  14746. (let (rtn (c start))
  14747. (setq list (nthcdr (1- start) list))
  14748. (while (and list (<= c end))
  14749. (push (pop list) rtn)
  14750. (setq c (1+ c)))
  14751. (nreverse rtn)))
  14752. (defun org-find-base-buffer-visiting (file)
  14753. "Like `find-buffer-visiting' but always return the base buffer and
  14754. not an indirect buffer."
  14755. (let ((buf (or (get-file-buffer file)
  14756. (find-buffer-visiting file))))
  14757. (if buf
  14758. (or (buffer-base-buffer buf) buf)
  14759. nil)))
  14760. (defun org-image-file-name-regexp (&optional extensions)
  14761. "Return regexp matching the file names of images.
  14762. If EXTENSIONS is given, only match these."
  14763. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14764. (image-file-name-regexp)
  14765. (let ((image-file-name-extensions
  14766. (or extensions
  14767. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14768. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14769. (concat "\\."
  14770. (regexp-opt (nconc (mapcar 'upcase
  14771. image-file-name-extensions)
  14772. image-file-name-extensions)
  14773. t)
  14774. "\\'"))))
  14775. (defun org-file-image-p (file &optional extensions)
  14776. "Return non-nil if FILE is an image."
  14777. (save-match-data
  14778. (string-match (org-image-file-name-regexp extensions) file)))
  14779. (defun org-get-cursor-date ()
  14780. "Return the date at cursor in as a time.
  14781. This works in the calendar and in the agenda, anywhere else it just
  14782. returns the current time."
  14783. (let (date day defd)
  14784. (cond
  14785. ((eq major-mode 'calendar-mode)
  14786. (setq date (calendar-cursor-to-date)
  14787. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14788. ((eq major-mode 'org-agenda-mode)
  14789. (setq day (get-text-property (point) 'day))
  14790. (if day
  14791. (setq date (calendar-gregorian-from-absolute day)
  14792. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14793. (nth 2 date))))))
  14794. (or defd (current-time))))
  14795. (defvar org-agenda-action-marker (make-marker)
  14796. "Marker pointing to the entry for the next agenda action.")
  14797. (defun org-mark-entry-for-agenda-action ()
  14798. "Mark the current entry as target of an agenda action.
  14799. Agenda actions are actions executed from the agenda with the key `k',
  14800. which make use of the date at the cursor."
  14801. (interactive)
  14802. (move-marker org-agenda-action-marker
  14803. (save-excursion (org-back-to-heading t) (point))
  14804. (current-buffer))
  14805. (message
  14806. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14807. ;;; Paragraph filling stuff.
  14808. ;; We want this to be just right, so use the full arsenal.
  14809. (defun org-indent-line-function ()
  14810. "Indent line like previous, but further if previous was headline or item."
  14811. (interactive)
  14812. (let* ((pos (point))
  14813. (itemp (org-at-item-p))
  14814. (case-fold-search t)
  14815. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14816. column bpos bcol tpos tcol bullet btype bullet-type)
  14817. ;; Find the previous relevant line
  14818. (beginning-of-line 1)
  14819. (cond
  14820. ((looking-at "#") (setq column 0))
  14821. ((looking-at "\\*+ ") (setq column 0))
  14822. ((and (looking-at "[ \t]*:END:")
  14823. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14824. (save-excursion
  14825. (goto-char (1- (match-beginning 1)))
  14826. (setq column (current-column))))
  14827. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  14828. (save-excursion
  14829. (re-search-backward
  14830. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  14831. (setq column (org-get-indentation (match-string 0))))
  14832. (t
  14833. (beginning-of-line 0)
  14834. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14835. (not (looking-at "[ \t]*:END:"))
  14836. (not (looking-at org-drawer-regexp)))
  14837. (beginning-of-line 0))
  14838. (cond
  14839. ((looking-at "\\*+[ \t]+")
  14840. (if (not org-adapt-indentation)
  14841. (setq column 0)
  14842. (goto-char (match-end 0))
  14843. (setq column (current-column))))
  14844. ((looking-at org-drawer-regexp)
  14845. (goto-char (1- (match-beginning 1)))
  14846. (setq column (current-column)))
  14847. ((looking-at "\\([ \t]*\\):END:")
  14848. (goto-char (match-end 1))
  14849. (setq column (current-column)))
  14850. ((org-in-item-p)
  14851. (org-beginning-of-item)
  14852. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14853. (setq bpos (match-beginning 1) tpos (match-end 0)
  14854. bcol (progn (goto-char bpos) (current-column))
  14855. tcol (progn (goto-char tpos) (current-column))
  14856. bullet (match-string 1)
  14857. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14858. (if (> tcol (+ bcol org-description-max-indent))
  14859. (setq tcol (+ bcol 5)))
  14860. (if (not itemp)
  14861. (setq column tcol)
  14862. (goto-char pos)
  14863. (beginning-of-line 1)
  14864. (if (looking-at "\\S-")
  14865. (progn
  14866. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14867. (setq bullet (match-string 1)
  14868. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14869. (setq column (if (equal btype bullet-type) bcol tcol)))
  14870. (setq column (org-get-indentation)))))
  14871. (t (setq column (org-get-indentation))))))
  14872. (goto-char pos)
  14873. (if (<= (current-column) (current-indentation))
  14874. (org-indent-line-to column)
  14875. (save-excursion (org-indent-line-to column)))
  14876. (setq column (current-column))
  14877. (beginning-of-line 1)
  14878. (if (looking-at
  14879. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14880. (replace-match (concat "\\1" (format org-property-format
  14881. (match-string 2) (match-string 3)))
  14882. t nil))
  14883. (org-move-to-column column)))
  14884. (defun org-set-autofill-regexps ()
  14885. (interactive)
  14886. ;; In the paragraph separator we include headlines, because filling
  14887. ;; text in a line directly attached to a headline would otherwise
  14888. ;; fill the headline as well.
  14889. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14890. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  14891. ;; The paragraph starter includes hand-formatted lists.
  14892. (org-set-local
  14893. 'paragraph-start
  14894. (concat
  14895. "\f" "\\|"
  14896. "[ ]*$" "\\|"
  14897. "\\*+ " "\\|"
  14898. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  14899. "[ \t]*[:|]" "\\|"
  14900. "\\$\\$" "\\|"
  14901. "\\\\\\(begin\\|end\\|[][]\\)"))
  14902. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14903. ;; But only if the user has not turned off tables or fixed-width regions
  14904. (org-set-local
  14905. 'auto-fill-inhibit-regexp
  14906. (concat "\\*+ \\|#\\+"
  14907. "\\|[ \t]*" org-keyword-time-regexp
  14908. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14909. (concat
  14910. "\\|[ \t]*["
  14911. (if org-enable-table-editor "|" "")
  14912. (if org-enable-fixed-width-editor ":" "")
  14913. "]"))))
  14914. ;; We use our own fill-paragraph function, to make sure that tables
  14915. ;; and fixed-width regions are not wrapped. That function will pass
  14916. ;; through to `fill-paragraph' when appropriate.
  14917. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14918. ; Adaptive filling: To get full control, first make sure that
  14919. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14920. (org-set-local 'adaptive-fill-regexp "\000")
  14921. (org-set-local 'adaptive-fill-function
  14922. 'org-adaptive-fill-function)
  14923. (org-set-local
  14924. 'align-mode-rules-list
  14925. '((org-in-buffer-settings
  14926. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  14927. (modes . '(org-mode))))))
  14928. (defun org-fill-paragraph (&optional justify)
  14929. "Re-align a table, pass through to fill-paragraph if no table."
  14930. (let ((table-p (org-at-table-p))
  14931. (table.el-p (org-at-table.el-p)))
  14932. (cond ((and (equal (char-after (point-at-bol)) ?*)
  14933. (save-excursion (goto-char (point-at-bol))
  14934. (looking-at outline-regexp)))
  14935. t) ; skip headlines
  14936. (table.el-p t) ; skip table.el tables
  14937. (table-p (org-table-align) t) ; align org-mode tables
  14938. (t nil)))) ; call paragraph-fill
  14939. ;; For reference, this is the default value of adaptive-fill-regexp
  14940. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  14941. (defun org-adaptive-fill-function ()
  14942. "Return a fill prefix for org-mode files.
  14943. In particular, this makes sure hanging paragraphs for hand-formatted lists
  14944. work correctly."
  14945. (cond ((looking-at "#[ \t]+")
  14946. (match-string 0))
  14947. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  14948. (save-excursion
  14949. (if (> (match-end 1) (+ (match-beginning 1)
  14950. org-description-max-indent))
  14951. (goto-char (+ (match-beginning 1) 5))
  14952. (goto-char (match-end 0)))
  14953. (make-string (current-column) ?\ )))
  14954. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  14955. (save-excursion
  14956. (goto-char (match-end 0))
  14957. (make-string (current-column) ?\ )))
  14958. (t nil)))
  14959. ;;; Other stuff.
  14960. (defun org-toggle-fixed-width-section (arg)
  14961. "Toggle the fixed-width export.
  14962. If there is no active region, the QUOTE keyword at the current headline is
  14963. inserted or removed. When present, it causes the text between this headline
  14964. and the next to be exported as fixed-width text, and unmodified.
  14965. If there is an active region, this command adds or removes a colon as the
  14966. first character of this line. If the first character of a line is a colon,
  14967. this line is also exported in fixed-width font."
  14968. (interactive "P")
  14969. (let* ((cc 0)
  14970. (regionp (org-region-active-p))
  14971. (beg (if regionp (region-beginning) (point)))
  14972. (end (if regionp (region-end)))
  14973. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  14974. (case-fold-search nil)
  14975. (re "[ \t]*\\(: \\)")
  14976. off)
  14977. (if regionp
  14978. (save-excursion
  14979. (goto-char beg)
  14980. (setq cc (current-column))
  14981. (beginning-of-line 1)
  14982. (setq off (looking-at re))
  14983. (while (> nlines 0)
  14984. (setq nlines (1- nlines))
  14985. (beginning-of-line 1)
  14986. (cond
  14987. (arg
  14988. (org-move-to-column cc t)
  14989. (insert ": \n")
  14990. (forward-line -1))
  14991. ((and off (looking-at re))
  14992. (replace-match "" t t nil 1))
  14993. ((not off) (org-move-to-column cc t) (insert ": ")))
  14994. (forward-line 1)))
  14995. (save-excursion
  14996. (org-back-to-heading)
  14997. (if (looking-at (concat outline-regexp
  14998. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  14999. (replace-match "" t t nil 1)
  15000. (if (looking-at outline-regexp)
  15001. (progn
  15002. (goto-char (match-end 0))
  15003. (insert org-quote-string " "))))))))
  15004. (defun org-reftex-citation ()
  15005. "Use reftex-citation to insert a citation into the buffer.
  15006. This looks for a line like
  15007. #+BIBLIOGRAPHY: foo plain option:-d
  15008. and derives from it that foo.bib is the bbliography file relevant
  15009. for this document. It then installs the necessary environment for RefTeX
  15010. to work in this buffer and calls `reftex-citation' to insert a citation
  15011. into the buffer.
  15012. Export of such citations to both LaTeX and HTML is handled by the contributed
  15013. package org-exp-bibtex by Taru Karttunen."
  15014. (interactive)
  15015. (let ((reftex-docstruct-symbol 'rds)
  15016. (reftex-cite-format "\\cite{%l}")
  15017. rds bib)
  15018. (save-excursion
  15019. (save-restriction
  15020. (widen)
  15021. (let ((case-fold-search t)
  15022. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  15023. (if (not (save-excursion
  15024. (or (re-search-forward re nil t)
  15025. (re-search-backward re nil t))))
  15026. (error "No bibliography defined in file")
  15027. (setq bib (concat (match-string 1) ".bib")
  15028. rds (list (list 'bib bib)))))))
  15029. (call-interactively 'reftex-citation)))
  15030. ;;;; Functions extending outline functionality
  15031. (defun org-beginning-of-line (&optional arg)
  15032. "Go to the beginning of the current line. If that is invisible, continue
  15033. to a visible line beginning. This makes the function of C-a more intuitive.
  15034. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15035. first attempt, and only move to after the tags when the cursor is already
  15036. beyond the end of the headline."
  15037. (interactive "P")
  15038. (let ((pos (point))
  15039. (special (if (consp org-special-ctrl-a/e)
  15040. (car org-special-ctrl-a/e)
  15041. org-special-ctrl-a/e))
  15042. refpos)
  15043. (if (org-bound-and-true-p line-move-visual)
  15044. (beginning-of-visual-line 1)
  15045. (beginning-of-line 1))
  15046. (if (and arg (fboundp 'move-beginning-of-line))
  15047. (call-interactively 'move-beginning-of-line)
  15048. (if (bobp)
  15049. nil
  15050. (backward-char 1)
  15051. (if (org-invisible-p)
  15052. (while (and (not (bobp)) (org-invisible-p))
  15053. (backward-char 1)
  15054. (beginning-of-line 1))
  15055. (forward-char 1))))
  15056. (when special
  15057. (cond
  15058. ((and (looking-at org-complex-heading-regexp)
  15059. (= (char-after (match-end 1)) ?\ ))
  15060. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  15061. (point-at-eol)))
  15062. (goto-char
  15063. (if (eq special t)
  15064. (cond ((> pos refpos) refpos)
  15065. ((= pos (point)) refpos)
  15066. (t (point)))
  15067. (cond ((> pos (point)) (point))
  15068. ((not (eq last-command this-command)) (point))
  15069. (t refpos)))))
  15070. ((org-at-item-p)
  15071. (goto-char
  15072. (if (eq special t)
  15073. (cond ((> pos (match-end 4)) (match-end 4))
  15074. ((= pos (point)) (match-end 4))
  15075. (t (point)))
  15076. (cond ((> pos (point)) (point))
  15077. ((not (eq last-command this-command)) (point))
  15078. (t (match-end 4))))))))
  15079. (org-no-warnings
  15080. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15081. (defun org-end-of-line (&optional arg)
  15082. "Go to the end of the line.
  15083. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15084. first attempt, and only move to after the tags when the cursor is already
  15085. beyond the end of the headline."
  15086. (interactive "P")
  15087. (let ((special (if (consp org-special-ctrl-a/e)
  15088. (cdr org-special-ctrl-a/e)
  15089. org-special-ctrl-a/e)))
  15090. (if (or (not special)
  15091. (not (org-on-heading-p))
  15092. arg)
  15093. (call-interactively
  15094. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  15095. ((fboundp 'move-end-of-line) 'move-end-of-line)
  15096. (t 'end-of-line)))
  15097. (let ((pos (point)))
  15098. (beginning-of-line 1)
  15099. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  15100. (if (eq special t)
  15101. (if (or (< pos (match-beginning 1))
  15102. (= pos (match-end 0)))
  15103. (goto-char (match-beginning 1))
  15104. (goto-char (match-end 0)))
  15105. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  15106. (goto-char (match-end 0))
  15107. (goto-char (match-beginning 1))))
  15108. (call-interactively (if (fboundp 'move-end-of-line)
  15109. 'move-end-of-line
  15110. 'end-of-line)))))
  15111. (org-no-warnings
  15112. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15113. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15114. (define-key org-mode-map "\C-e" 'org-end-of-line)
  15115. (defun org-backward-sentence (&optional arg)
  15116. "Go to beginning of sentence, or beginning of table field.
  15117. This will call `backward-sentence' or `org-table-beginning-of-field',
  15118. depending on context."
  15119. (interactive "P")
  15120. (cond
  15121. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  15122. (t (call-interactively 'backward-sentence))))
  15123. (defun org-forward-sentence (&optional arg)
  15124. "Go to end of sentence, or end of table field.
  15125. This will call `forward-sentence' or `org-table-end-of-field',
  15126. depending on context."
  15127. (interactive "P")
  15128. (cond
  15129. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15130. (t (call-interactively 'forward-sentence))))
  15131. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15132. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15133. (defun org-kill-line (&optional arg)
  15134. "Kill line, to tags or end of line."
  15135. (interactive "P")
  15136. (cond
  15137. ((or (not org-special-ctrl-k)
  15138. (bolp)
  15139. (not (org-on-heading-p)))
  15140. (call-interactively 'kill-line))
  15141. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15142. (kill-region (point) (match-beginning 1))
  15143. (org-set-tags nil t))
  15144. (t (kill-region (point) (point-at-eol)))))
  15145. (define-key org-mode-map "\C-k" 'org-kill-line)
  15146. (defun org-yank (&optional arg)
  15147. "Yank. If the kill is a subtree, treat it specially.
  15148. This command will look at the current kill and check if is a single
  15149. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15150. cursor is at the beginning of a line or after the stars of a currently
  15151. empty headline, then the yank is handled specially. How exactly depends
  15152. on the value of the following variables, both set by default.
  15153. org-yank-folded-subtrees
  15154. When set, the subtree(s) will be folded after insertion, but only
  15155. if doing so would now swallow text after the yanked text.
  15156. org-yank-adjusted-subtrees
  15157. When set, the subtree will be promoted or demoted in order to
  15158. fit into the local outline tree structure, which means that the level
  15159. will be adjusted so that it becomes the smaller one of the two
  15160. *visible* surrounding headings.
  15161. Any prefix to this command will cause `yank' to be called directly with
  15162. no special treatment. In particular, a simple `C-u' prefix will just
  15163. plainly yank the text as it is.
  15164. \[1] The test checks if the first non-white line is a heading
  15165. and if there are no other headings with fewer stars."
  15166. (interactive "P")
  15167. (org-yank-generic 'yank arg))
  15168. (defun org-yank-generic (command arg)
  15169. "Perform some yank-like command.
  15170. This function implements the behavior described in the `org-yank'
  15171. documentation. However, it has been generalized to work for any
  15172. interactive command with similar behavior."
  15173. ;; pretend to be command COMMAND
  15174. (setq this-command command)
  15175. (if arg
  15176. (call-interactively command)
  15177. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15178. (and (org-kill-is-subtree-p)
  15179. (or (bolp)
  15180. (and (looking-at "[ \t]*$")
  15181. (string-match
  15182. "\\`\\*+\\'"
  15183. (buffer-substring (point-at-bol) (point)))))))
  15184. swallowp)
  15185. (cond
  15186. ((and subtreep org-yank-folded-subtrees)
  15187. (let ((beg (point))
  15188. end)
  15189. (if (and subtreep org-yank-adjusted-subtrees)
  15190. (org-paste-subtree nil nil 'for-yank)
  15191. (call-interactively command))
  15192. (setq end (point))
  15193. (goto-char beg)
  15194. (when (and (bolp) subtreep
  15195. (not (setq swallowp
  15196. (org-yank-folding-would-swallow-text beg end))))
  15197. (or (looking-at outline-regexp)
  15198. (re-search-forward (concat "^" outline-regexp) end t))
  15199. (while (and (< (point) end) (looking-at outline-regexp))
  15200. (hide-subtree)
  15201. (org-cycle-show-empty-lines 'folded)
  15202. (condition-case nil
  15203. (outline-forward-same-level 1)
  15204. (error (goto-char end)))))
  15205. (when swallowp
  15206. (message
  15207. "Inserted text not folded because that would swallow text"))
  15208. (goto-char end)
  15209. (skip-chars-forward " \t\n\r")
  15210. (beginning-of-line 1)
  15211. (push-mark beg 'nomsg)))
  15212. ((and subtreep org-yank-adjusted-subtrees)
  15213. (let ((beg (point-at-bol)))
  15214. (org-paste-subtree nil nil 'for-yank)
  15215. (push-mark beg 'nomsg)))
  15216. (t
  15217. (call-interactively command))))))
  15218. (defun org-yank-folding-would-swallow-text (beg end)
  15219. "Would hide-subtree at BEG swallow any text after END?"
  15220. (let (level)
  15221. (save-excursion
  15222. (goto-char beg)
  15223. (when (or (looking-at outline-regexp)
  15224. (re-search-forward (concat "^" outline-regexp) end t))
  15225. (setq level (org-outline-level)))
  15226. (goto-char end)
  15227. (skip-chars-forward " \t\r\n\v\f")
  15228. (if (or (eobp)
  15229. (and (bolp) (looking-at org-outline-regexp)
  15230. (<= (org-outline-level) level)))
  15231. nil ; Nothing would be swallowed
  15232. t)))) ; something would swallow
  15233. (define-key org-mode-map "\C-y" 'org-yank)
  15234. (defun org-invisible-p ()
  15235. "Check if point is at a character currently not visible."
  15236. ;; Early versions of noutline don't have `outline-invisible-p'.
  15237. (if (fboundp 'outline-invisible-p)
  15238. (outline-invisible-p)
  15239. (get-char-property (point) 'invisible)))
  15240. (defun org-invisible-p2 ()
  15241. "Check if point is at a character currently not visible."
  15242. (save-excursion
  15243. (if (and (eolp) (not (bobp))) (backward-char 1))
  15244. ;; Early versions of noutline don't have `outline-invisible-p'.
  15245. (if (fboundp 'outline-invisible-p)
  15246. (outline-invisible-p)
  15247. (get-char-property (point) 'invisible))))
  15248. (defun org-back-to-heading (&optional invisible-ok)
  15249. "Call `outline-back-to-heading', but provide a better error message."
  15250. (condition-case nil
  15251. (outline-back-to-heading invisible-ok)
  15252. (error (error "Before first headline at position %d in buffer %s"
  15253. (point) (current-buffer)))))
  15254. (defun org-before-first-heading-p ()
  15255. "Before first heading?"
  15256. (save-excursion
  15257. (null (re-search-backward "^\\*+ " nil t))))
  15258. (defalias 'org-on-heading-p 'outline-on-heading-p)
  15259. (defalias 'org-at-heading-p 'outline-on-heading-p)
  15260. (defun org-at-heading-or-item-p ()
  15261. (or (org-on-heading-p) (org-at-item-p)))
  15262. (defun org-on-target-p ()
  15263. (or (org-in-regexp org-radio-target-regexp)
  15264. (org-in-regexp org-target-regexp)))
  15265. (defun org-up-heading-all (arg)
  15266. "Move to the heading line of which the present line is a subheading.
  15267. This function considers both visible and invisible heading lines.
  15268. With argument, move up ARG levels."
  15269. (if (fboundp 'outline-up-heading-all)
  15270. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15271. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15272. (defun org-up-heading-safe ()
  15273. "Move to the heading line of which the present line is a subheading.
  15274. This version will not throw an error. It will return the level of the
  15275. headline found, or nil if no higher level is found.
  15276. Also, this function will be a lot faster than `outline-up-heading',
  15277. because it relies on stars being the outline starters. This can really
  15278. make a significant difference in outlines with very many siblings."
  15279. (let (start-level re)
  15280. (org-back-to-heading t)
  15281. (setq start-level (funcall outline-level))
  15282. (if (equal start-level 1)
  15283. nil
  15284. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15285. (if (re-search-backward re nil t)
  15286. (funcall outline-level)))))
  15287. (defun org-first-sibling-p ()
  15288. "Is this heading the first child of its parents?"
  15289. (interactive)
  15290. (let ((re (concat "^" outline-regexp))
  15291. level l)
  15292. (unless (org-at-heading-p t)
  15293. (error "Not at a heading"))
  15294. (setq level (funcall outline-level))
  15295. (save-excursion
  15296. (if (not (re-search-backward re nil t))
  15297. t
  15298. (setq l (funcall outline-level))
  15299. (< l level)))))
  15300. (defun org-goto-sibling (&optional previous)
  15301. "Goto the next sibling, even if it is invisible.
  15302. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15303. when a sibling was found. When none is found, return nil and don't
  15304. move point."
  15305. (let ((fun (if previous 're-search-backward 're-search-forward))
  15306. (pos (point))
  15307. (re (concat "^" outline-regexp))
  15308. level l)
  15309. (when (condition-case nil (org-back-to-heading t) (error nil))
  15310. (setq level (funcall outline-level))
  15311. (catch 'exit
  15312. (or previous (forward-char 1))
  15313. (while (funcall fun re nil t)
  15314. (setq l (funcall outline-level))
  15315. (when (< l level) (goto-char pos) (throw 'exit nil))
  15316. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15317. (goto-char pos)
  15318. nil))))
  15319. (defun org-show-siblings ()
  15320. "Show all siblings of the current headline."
  15321. (save-excursion
  15322. (while (org-goto-sibling) (org-flag-heading nil)))
  15323. (save-excursion
  15324. (while (org-goto-sibling 'previous)
  15325. (org-flag-heading nil))))
  15326. (defun org-show-hidden-entry ()
  15327. "Show an entry where even the heading is hidden."
  15328. (save-excursion
  15329. (org-show-entry)))
  15330. (defun org-flag-heading (flag &optional entry)
  15331. "Flag the current heading. FLAG non-nil means make invisible.
  15332. When ENTRY is non-nil, show the entire entry."
  15333. (save-excursion
  15334. (org-back-to-heading t)
  15335. ;; Check if we should show the entire entry
  15336. (if entry
  15337. (progn
  15338. (org-show-entry)
  15339. (save-excursion
  15340. (and (outline-next-heading)
  15341. (org-flag-heading nil))))
  15342. (outline-flag-region (max (point-min) (1- (point)))
  15343. (save-excursion (outline-end-of-heading) (point))
  15344. flag))))
  15345. (defun org-get-next-sibling ()
  15346. "Move to next heading of the same level, and return point.
  15347. If there is no such heading, return nil.
  15348. This is like outline-next-sibling, but invisible headings are ok."
  15349. (let ((level (funcall outline-level)))
  15350. (outline-next-heading)
  15351. (while (and (not (eobp)) (> (funcall outline-level) level))
  15352. (outline-next-heading))
  15353. (if (or (eobp) (< (funcall outline-level) level))
  15354. nil
  15355. (point))))
  15356. (defun org-get-last-sibling ()
  15357. "Move to previous heading of the same level, and return point.
  15358. If there is no such heading, return nil."
  15359. (let ((opoint (point))
  15360. (level (funcall outline-level)))
  15361. (outline-previous-heading)
  15362. (when (and (/= (point) opoint) (outline-on-heading-p t))
  15363. (while (and (> (funcall outline-level) level)
  15364. (not (bobp)))
  15365. (outline-previous-heading))
  15366. (if (< (funcall outline-level) level)
  15367. nil
  15368. (point)))))
  15369. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15370. ;; This contains an exact copy of the original function, but it uses
  15371. ;; `org-back-to-heading', to make it work also in invisible
  15372. ;; trees. And is uses an invisible-OK argument.
  15373. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15374. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15375. ;; with many children and grandchildren etc, this can be much faster
  15376. ;; than the outline version.
  15377. (org-back-to-heading invisible-OK)
  15378. (let ((first t)
  15379. (level (funcall outline-level)))
  15380. (if (and (org-mode-p) (< level 1000))
  15381. ;; A true heading (not a plain list item), in Org-mode
  15382. ;; This means we can easily find the end by looking
  15383. ;; only for the right number of stars. Using a regexp to do
  15384. ;; this is so much faster than using a Lisp loop.
  15385. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15386. (forward-char 1)
  15387. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15388. ;; something else, do it the slow way
  15389. (while (and (not (eobp))
  15390. (or first (> (funcall outline-level) level)))
  15391. (setq first nil)
  15392. (outline-next-heading)))
  15393. (unless to-heading
  15394. (if (memq (preceding-char) '(?\n ?\^M))
  15395. (progn
  15396. ;; Go to end of line before heading
  15397. (forward-char -1)
  15398. (if (memq (preceding-char) '(?\n ?\^M))
  15399. ;; leave blank line before heading
  15400. (forward-char -1))))))
  15401. (point))
  15402. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  15403. "Use Org version in org-mode, for dramatic speed-up."
  15404. (if (eq major-mode 'org-mode)
  15405. (progn
  15406. (org-end-of-subtree nil t)
  15407. (backward-char 1))
  15408. ad-do-it))
  15409. (defun org-forward-same-level (arg &optional invisible-ok)
  15410. "Move forward to the arg'th subheading at same level as this one.
  15411. Stop at the first and last subheadings of a superior heading."
  15412. (interactive "p")
  15413. (org-back-to-heading invisible-ok)
  15414. (org-on-heading-p)
  15415. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15416. (re (format "^\\*\\{1,%d\\} " level))
  15417. l)
  15418. (forward-char 1)
  15419. (while (> arg 0)
  15420. (while (and (re-search-forward re nil 'move)
  15421. (setq l (- (match-end 0) (match-beginning 0) 1))
  15422. (= l level)
  15423. (not invisible-ok)
  15424. (org-invisible-p))
  15425. (if (< l level) (setq arg 1)))
  15426. (setq arg (1- arg)))
  15427. (beginning-of-line 1)))
  15428. (defun org-backward-same-level (arg &optional invisible-ok)
  15429. "Move backward to the arg'th subheading at same level as this one.
  15430. Stop at the first and last subheadings of a superior heading."
  15431. (interactive "p")
  15432. (org-back-to-heading)
  15433. (org-on-heading-p)
  15434. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15435. (re (format "^\\*\\{1,%d\\} " level))
  15436. l)
  15437. (while (> arg 0)
  15438. (while (and (re-search-backward re nil 'move)
  15439. (setq l (- (match-end 0) (match-beginning 0) 1))
  15440. (= l level)
  15441. (not invisible-ok)
  15442. (org-invisible-p))
  15443. (if (< l level) (setq arg 1)))
  15444. (setq arg (1- arg)))))
  15445. (defun org-show-subtree ()
  15446. "Show everything after this heading at deeper levels."
  15447. (outline-flag-region
  15448. (point)
  15449. (save-excursion
  15450. (org-end-of-subtree t t))
  15451. nil))
  15452. (defun org-show-entry ()
  15453. "Show the body directly following this heading.
  15454. Show the heading too, if it is currently invisible."
  15455. (interactive)
  15456. (save-excursion
  15457. (condition-case nil
  15458. (progn
  15459. (org-back-to-heading t)
  15460. (outline-flag-region
  15461. (max (point-min) (1- (point)))
  15462. (save-excursion
  15463. (if (re-search-forward
  15464. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15465. (match-beginning 1)
  15466. (point-max)))
  15467. nil)
  15468. (org-cycle-hide-drawers 'children))
  15469. (error nil))))
  15470. (defun org-make-options-regexp (kwds &optional extra)
  15471. "Make a regular expression for keyword lines."
  15472. (concat
  15473. "^"
  15474. "#?[ \t]*\\+\\("
  15475. (mapconcat 'regexp-quote kwds "\\|")
  15476. (if extra (concat "\\|" extra))
  15477. "\\):[ \t]*"
  15478. "\\(.*\\)"))
  15479. ;; Make isearch reveal the necessary context
  15480. (defun org-isearch-end ()
  15481. "Reveal context after isearch exits."
  15482. (when isearch-success ; only if search was successful
  15483. (if (featurep 'xemacs)
  15484. ;; Under XEmacs, the hook is run in the correct place,
  15485. ;; we directly show the context.
  15486. (org-show-context 'isearch)
  15487. ;; In Emacs the hook runs *before* restoring the overlays.
  15488. ;; So we have to use a one-time post-command-hook to do this.
  15489. ;; (Emacs 22 has a special variable, see function `org-mode')
  15490. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15491. isearch-mode-end-hook-quit)
  15492. ;; Only when the isearch was not quitted.
  15493. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15494. 'append 'local)))))
  15495. (defun org-isearch-post-command ()
  15496. "Remove self from hook, and show context."
  15497. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15498. (org-show-context 'isearch))
  15499. ;;;; Integration with and fixes for other packages
  15500. ;;; Imenu support
  15501. (defvar org-imenu-markers nil
  15502. "All markers currently used by Imenu.")
  15503. (make-variable-buffer-local 'org-imenu-markers)
  15504. (defun org-imenu-new-marker (&optional pos)
  15505. "Return a new marker for use by Imenu, and remember the marker."
  15506. (let ((m (make-marker)))
  15507. (move-marker m (or pos (point)))
  15508. (push m org-imenu-markers)
  15509. m))
  15510. (defun org-imenu-get-tree ()
  15511. "Produce the index for Imenu."
  15512. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15513. (setq org-imenu-markers nil)
  15514. (let* ((n org-imenu-depth)
  15515. (re (concat "^" outline-regexp))
  15516. (subs (make-vector (1+ n) nil))
  15517. (last-level 0)
  15518. m level head)
  15519. (save-excursion
  15520. (save-restriction
  15521. (widen)
  15522. (goto-char (point-max))
  15523. (while (re-search-backward re nil t)
  15524. (setq level (org-reduced-level (funcall outline-level)))
  15525. (when (<= level n)
  15526. (looking-at org-complex-heading-regexp)
  15527. (setq head (org-link-display-format
  15528. (org-match-string-no-properties 4))
  15529. m (org-imenu-new-marker))
  15530. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15531. (if (>= level last-level)
  15532. (push (cons head m) (aref subs level))
  15533. (push (cons head (aref subs (1+ level))) (aref subs level))
  15534. (loop for i from (1+ level) to n do (aset subs i nil)))
  15535. (setq last-level level)))))
  15536. (aref subs 1)))
  15537. (eval-after-load "imenu"
  15538. '(progn
  15539. (add-hook 'imenu-after-jump-hook
  15540. (lambda ()
  15541. (if (eq major-mode 'org-mode)
  15542. (org-show-context 'org-goto))))))
  15543. (defun org-link-display-format (link)
  15544. "Replace a link with either the description, or the link target
  15545. if no description is present"
  15546. (save-match-data
  15547. (if (string-match org-bracket-link-analytic-regexp link)
  15548. (replace-match (or (match-string 5 link)
  15549. (concat (match-string 1 link)
  15550. (match-string 3 link)))
  15551. nil nil link)
  15552. link)))
  15553. ;; Speedbar support
  15554. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15555. "Overlay marking the agenda restriction line in speedbar.")
  15556. (org-overlay-put org-speedbar-restriction-lock-overlay
  15557. 'face 'org-agenda-restriction-lock)
  15558. (org-overlay-put org-speedbar-restriction-lock-overlay
  15559. 'help-echo "Agendas are currently limited to this item.")
  15560. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15561. (defun org-speedbar-set-agenda-restriction ()
  15562. "Restrict future agenda commands to the location at point in speedbar.
  15563. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15564. (interactive)
  15565. (require 'org-agenda)
  15566. (let (p m tp np dir txt)
  15567. (cond
  15568. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15569. 'org-imenu t))
  15570. (setq m (get-text-property p 'org-imenu-marker))
  15571. (save-excursion
  15572. (save-restriction
  15573. (set-buffer (marker-buffer m))
  15574. (goto-char m)
  15575. (org-agenda-set-restriction-lock 'subtree))))
  15576. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15577. 'speedbar-function 'speedbar-find-file))
  15578. (setq tp (previous-single-property-change
  15579. (1+ p) 'speedbar-function)
  15580. np (next-single-property-change
  15581. tp 'speedbar-function)
  15582. dir (speedbar-line-directory)
  15583. txt (buffer-substring-no-properties (or tp (point-min))
  15584. (or np (point-max))))
  15585. (save-excursion
  15586. (save-restriction
  15587. (set-buffer (find-file-noselect
  15588. (let ((default-directory dir))
  15589. (expand-file-name txt))))
  15590. (unless (org-mode-p)
  15591. (error "Cannot restrict to non-Org-mode file"))
  15592. (org-agenda-set-restriction-lock 'file))))
  15593. (t (error "Don't know how to restrict Org-mode's agenda")))
  15594. (org-move-overlay org-speedbar-restriction-lock-overlay
  15595. (point-at-bol) (point-at-eol))
  15596. (setq current-prefix-arg nil)
  15597. (org-agenda-maybe-redo)))
  15598. (eval-after-load "speedbar"
  15599. '(progn
  15600. (speedbar-add-supported-extension ".org")
  15601. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15602. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15603. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15604. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15605. (add-hook 'speedbar-visiting-tag-hook
  15606. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15607. ;;; Fixes and Hacks for problems with other packages
  15608. ;; Make flyspell not check words in links, to not mess up our keymap
  15609. (defun org-mode-flyspell-verify ()
  15610. "Don't let flyspell put overlays at active buttons."
  15611. (and (not (get-text-property (point) 'keymap))
  15612. (not (get-text-property (point) 'org-no-flyspell))))
  15613. (defun org-remove-flyspell-overlays-in (beg end)
  15614. "Remove flyspell overlays in region."
  15615. (and (org-bound-and-true-p flyspell-mode)
  15616. (fboundp 'flyspell-delete-region-overlays)
  15617. (flyspell-delete-region-overlays beg end))
  15618. (add-text-properties beg end '(org-no-flyspell t)))
  15619. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15620. (eval-after-load "bookmark"
  15621. '(if (boundp 'bookmark-after-jump-hook)
  15622. ;; We can use the hook
  15623. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15624. ;; Hook not available, use advice
  15625. (defadvice bookmark-jump (after org-make-visible activate)
  15626. "Make the position visible."
  15627. (org-bookmark-jump-unhide))))
  15628. ;; Make sure saveplace show the location if it was hidden
  15629. (eval-after-load "saveplace"
  15630. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15631. "Make the position visible."
  15632. (org-bookmark-jump-unhide)))
  15633. (defun org-bookmark-jump-unhide ()
  15634. "Unhide the current position, to show the bookmark location."
  15635. (and (org-mode-p)
  15636. (or (org-invisible-p)
  15637. (save-excursion (goto-char (max (point-min) (1- (point))))
  15638. (org-invisible-p)))
  15639. (org-show-context 'bookmark-jump)))
  15640. ;; Make session.el ignore our circular variable
  15641. (eval-after-load "session"
  15642. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15643. ;;;; Experimental code
  15644. (defun org-closed-in-range ()
  15645. "Sparse tree of items closed in a certain time range.
  15646. Still experimental, may disappear in the future."
  15647. (interactive)
  15648. ;; Get the time interval from the user.
  15649. (let* ((time1 (time-to-seconds
  15650. (org-read-date nil 'to-time nil "Starting date: ")))
  15651. (time2 (time-to-seconds
  15652. (org-read-date nil 'to-time nil "End date:")))
  15653. ;; callback function
  15654. (callback (lambda ()
  15655. (let ((time
  15656. (time-to-seconds
  15657. (apply 'encode-time
  15658. (org-parse-time-string
  15659. (match-string 1))))))
  15660. ;; check if time in interval
  15661. (and (>= time time1) (<= time time2))))))
  15662. ;; make tree, check each match with the callback
  15663. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15664. ;;;; Finish up
  15665. (provide 'org)
  15666. (run-hooks 'org-load-hook)
  15667. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15668. ;;; org.el ends here