org.el 641 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196
  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.30trans
  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.30trans"
  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, nor 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 " crypt: Encryption of subtrees" org-crypt)
  166. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  167. (const :tag " id: Global IDs for identifying entries" org-id)
  168. (const :tag " info: Links to Info nodes" org-info)
  169. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  170. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  171. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  172. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  173. (const :tag " mew Links to Mew folders/messages" org-mew)
  174. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  175. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  176. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  177. (const :tag " vm: Links to VM folders/messages" org-vm)
  178. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  179. (const :tag " w3m: Special cut/paste from w3m to Org." org-w3m)
  180. (const :tag " mouse: Additional mouse support" org-mouse)
  181. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  182. (const :tag "C annotation-helper: Call Remember directly from Browser\n\t\t\t(OBSOLETE, use org-protocol)" org-annotation-helper)
  183. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  184. (const :tag "C browser-url: Store link, directly from Browser\n\t\t\t(OBSOLETE, use org-protocol)" org-browser-url)
  185. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  186. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  187. (const :tag "C collector: Collect properties into tables" org-collector)
  188. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  189. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  190. (const :tag "C eval: Include command output as text" org-eval)
  191. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  192. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  193. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  194. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  195. (const :tag "C invoice Help manage client invoices in OrgMode" org-invoice)
  196. (const :tag "C jira Add a jira:ticket protocol to Org" org-jira)
  197. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  198. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  199. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  200. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  201. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  202. (const :tag "C R: Computation using the R language" org-R)
  203. (const :tag "C registry: A registry for Org links" org-registry)
  204. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  205. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  206. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  207. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  208. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  209. (const :tag "C track: Keep up with Org development" org-track)
  210. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  211. (defcustom org-support-shift-select nil
  212. "Non-nil means, make shift-cursor commands select text when possible.
  213. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  214. selecting a region, or enlarge thusly regions started in this way.
  215. In Org-mode, in special contexts, these same keys are used for other
  216. purposes, important enough to compete with shift selection. Org tries
  217. to balance these needs by supporting `shift-select-mode' outside these
  218. special contexts, under control of this variable.
  219. The default of this variable is nil, to avoid confusing behavior. Shifted
  220. cursor keys will then execute Org commands in the following contexts:
  221. - on a headline, changing TODO state (left/right) and priority (up/down)
  222. - on a time stamp, changing the time
  223. - in a plain list item, changing the bullet type
  224. - in a property definition line, switching between allowed values
  225. - in the BEGIN line of a clock table (changing the time block).
  226. Outside these contexts, the commands will throw an error.
  227. When this variable is t and the cursor is not in a special context,
  228. Org-mode will support shift-selection for making and enlarging regions.
  229. To make this more effective, the bullet cycling will no longer happen
  230. anywhere in an item line, but only if the cursor is exactly on the bullet.
  231. If you set this variable to the symbol `always', then the keys
  232. will not be special in headlines, property lines, and item lines, to make
  233. shift selection work there as well. If this is what you want, you can
  234. use the following alternative commands: `C-c C-t' and `C-c ,' to
  235. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  236. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  237. edited by hand or in column view.
  238. However, when the cursor is on a timestamp, shift-cursor commands
  239. will still edit the time stamp - this is just too good to give up.
  240. XEmacs user should have this variable set to nil, because shift-select-mode
  241. is Emacs 23 only."
  242. :group 'org
  243. :type '(choice
  244. (const :tag "Never" nil)
  245. (const :tag "When outside special context" t)
  246. (const :tag "Everywhere except timestamps" always)))
  247. (defgroup org-startup nil
  248. "Options concerning startup of Org-mode."
  249. :tag "Org Startup"
  250. :group 'org)
  251. (defcustom org-startup-folded t
  252. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  253. This can also be configured on a per-file basis by adding one of
  254. the following lines anywhere in the buffer:
  255. #+STARTUP: fold
  256. #+STARTUP: nofold
  257. #+STARTUP: content"
  258. :group 'org-startup
  259. :type '(choice
  260. (const :tag "nofold: show all" nil)
  261. (const :tag "fold: overview" t)
  262. (const :tag "content: all headlines" content)))
  263. (defcustom org-startup-truncated t
  264. "Non-nil means, entering Org-mode will set `truncate-lines'.
  265. This is useful since some lines containing links can be very long and
  266. uninteresting. Also tables look terrible when wrapped."
  267. :group 'org-startup
  268. :type 'boolean)
  269. (defcustom org-startup-indented nil
  270. "Non-nil means, turn on `org-indent-mode' on startup.
  271. This can also be configured on a per-file basis by adding one of
  272. the following lines anywhere in the buffer:
  273. #+STARTUP: indent
  274. #+STARTUP: noindent"
  275. :group 'org-structure
  276. :type '(choice
  277. (const :tag "Not" nil)
  278. (const :tag "Globally (slow on startup in large files)" t)))
  279. (defcustom org-startup-align-all-tables nil
  280. "Non-nil means, align all tables when visiting a file.
  281. This is useful when the column width in tables is forced with <N> cookies
  282. in table fields. Such tables will look correct only after the first re-align.
  283. This can also be configured on a per-file basis by adding one of
  284. the following lines anywhere in the buffer:
  285. #+STARTUP: align
  286. #+STARTUP: noalign"
  287. :group 'org-startup
  288. :type 'boolean)
  289. (defcustom org-insert-mode-line-in-empty-file nil
  290. "Non-nil means insert the first line setting Org-mode in empty files.
  291. When the function `org-mode' is called interactively in an empty file, this
  292. normally means that the file name does not automatically trigger Org-mode.
  293. To ensure that the file will always be in Org-mode in the future, a
  294. line enforcing Org-mode will be inserted into the buffer, if this option
  295. has been set."
  296. :group 'org-startup
  297. :type 'boolean)
  298. (defcustom org-replace-disputed-keys nil
  299. "Non-nil means use alternative key bindings for some keys.
  300. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  301. These keys are also used by other packages like shift-selection-mode'
  302. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  303. If you want to use Org-mode together with one of these other modes,
  304. or more generally if you would like to move some Org-mode commands to
  305. other keys, set this variable and configure the keys with the variable
  306. `org-disputed-keys'.
  307. This option is only relevant at load-time of Org-mode, and must be set
  308. *before* org.el is loaded. Changing it requires a restart of Emacs to
  309. become effective."
  310. :group 'org-startup
  311. :type 'boolean)
  312. (defcustom org-use-extra-keys nil
  313. "Non-nil means use extra key sequence definitions for certain
  314. commands. This happens automatically if you run XEmacs or if
  315. window-system is nil. This variable lets you do the same
  316. manually. You must set it before loading org.
  317. Example: on Carbon Emacs 22 running graphically, with an external
  318. keyboard on a Powerbook, the default way of setting M-left might
  319. not work for either Alt or ESC. Setting this variable will make
  320. it work for ESC."
  321. :group 'org-startup
  322. :type 'boolean)
  323. (if (fboundp 'defvaralias)
  324. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  325. (defcustom org-disputed-keys
  326. '(([(shift up)] . [(meta p)])
  327. ([(shift down)] . [(meta n)])
  328. ([(shift left)] . [(meta -)])
  329. ([(shift right)] . [(meta +)])
  330. ([(control shift right)] . [(meta shift +)])
  331. ([(control shift left)] . [(meta shift -)]))
  332. "Keys for which Org-mode and other modes compete.
  333. This is an alist, cars are the default keys, second element specifies
  334. the alternative to use when `org-replace-disputed-keys' is t.
  335. Keys can be specified in any syntax supported by `define-key'.
  336. The value of this option takes effect only at Org-mode's startup,
  337. therefore you'll have to restart Emacs to apply it after changing."
  338. :group 'org-startup
  339. :type 'alist)
  340. (defun org-key (key)
  341. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  342. Or return the original if not disputed."
  343. (if org-replace-disputed-keys
  344. (let* ((nkey (key-description key))
  345. (x (org-find-if (lambda (x)
  346. (equal (key-description (car x)) nkey))
  347. org-disputed-keys)))
  348. (if x (cdr x) key))
  349. key))
  350. (defun org-find-if (predicate seq)
  351. (catch 'exit
  352. (while seq
  353. (if (funcall predicate (car seq))
  354. (throw 'exit (car seq))
  355. (pop seq)))))
  356. (defun org-defkey (keymap key def)
  357. "Define a key, possibly translated, as returned by `org-key'."
  358. (define-key keymap (org-key key) def))
  359. (defcustom org-ellipsis nil
  360. "The ellipsis to use in the Org-mode outline.
  361. When nil, just use the standard three dots. When a string, use that instead,
  362. When a face, use the standard 3 dots, but with the specified face.
  363. The change affects only Org-mode (which will then use its own display table).
  364. Changing this requires executing `M-x org-mode' in a buffer to become
  365. effective."
  366. :group 'org-startup
  367. :type '(choice (const :tag "Default" nil)
  368. (face :tag "Face" :value org-warning)
  369. (string :tag "String" :value "...#")))
  370. (defvar org-display-table nil
  371. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  372. (defgroup org-keywords nil
  373. "Keywords in Org-mode."
  374. :tag "Org Keywords"
  375. :group 'org)
  376. (defcustom org-deadline-string "DEADLINE:"
  377. "String to mark deadline entries.
  378. A deadline is this string, followed by a time stamp. Should be a word,
  379. terminated by a colon. You can insert a schedule keyword and
  380. a timestamp with \\[org-deadline].
  381. Changes become only effective after restarting Emacs."
  382. :group 'org-keywords
  383. :type 'string)
  384. (defcustom org-scheduled-string "SCHEDULED:"
  385. "String to mark scheduled TODO entries.
  386. A schedule is this string, followed by a time stamp. Should be a word,
  387. terminated by a colon. You can insert a schedule keyword and
  388. a timestamp with \\[org-schedule].
  389. Changes become only effective after restarting Emacs."
  390. :group 'org-keywords
  391. :type 'string)
  392. (defcustom org-closed-string "CLOSED:"
  393. "String used as the prefix for timestamps logging closing a TODO entry."
  394. :group 'org-keywords
  395. :type 'string)
  396. (defcustom org-clock-string "CLOCK:"
  397. "String used as prefix for timestamps clocking work hours on an item."
  398. :group 'org-keywords
  399. :type 'string)
  400. (defcustom org-comment-string "COMMENT"
  401. "Entries starting with this keyword will never be exported.
  402. An entry can be toggled between COMMENT and normal with
  403. \\[org-toggle-comment].
  404. Changes become only effective after restarting Emacs."
  405. :group 'org-keywords
  406. :type 'string)
  407. (defcustom org-quote-string "QUOTE"
  408. "Entries starting with this keyword will be exported in fixed-width font.
  409. Quoting applies only to the text in the entry following the headline, and does
  410. not extend beyond the next headline, even if that is lower level.
  411. An entry can be toggled between QUOTE and normal with
  412. \\[org-toggle-fixed-width-section]."
  413. :group 'org-keywords
  414. :type 'string)
  415. (defconst org-repeat-re
  416. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  417. "Regular expression for specifying repeated events.
  418. After a match, group 1 contains the repeat expression.")
  419. (defgroup org-structure nil
  420. "Options concerning the general structure of Org-mode files."
  421. :tag "Org Structure"
  422. :group 'org)
  423. (defgroup org-reveal-location nil
  424. "Options about how to make context of a location visible."
  425. :tag "Org Reveal Location"
  426. :group 'org-structure)
  427. (defconst org-context-choice
  428. '(choice
  429. (const :tag "Always" t)
  430. (const :tag "Never" nil)
  431. (repeat :greedy t :tag "Individual contexts"
  432. (cons
  433. (choice :tag "Context"
  434. (const agenda)
  435. (const org-goto)
  436. (const occur-tree)
  437. (const tags-tree)
  438. (const link-search)
  439. (const mark-goto)
  440. (const bookmark-jump)
  441. (const isearch)
  442. (const default))
  443. (boolean))))
  444. "Contexts for the reveal options.")
  445. (defcustom org-show-hierarchy-above '((default . t))
  446. "Non-nil means, show full hierarchy when revealing a location.
  447. Org-mode often shows locations in an org-mode file which might have
  448. been invisible before. When this is set, the hierarchy of headings
  449. above the exposed location is shown.
  450. Turning this off for example for sparse trees makes them very compact.
  451. Instead of t, this can also be an alist specifying this option for different
  452. contexts. Valid contexts are
  453. agenda when exposing an entry from the agenda
  454. org-goto when using the command `org-goto' on key C-c C-j
  455. occur-tree when using the command `org-occur' on key C-c /
  456. tags-tree when constructing a sparse tree based on tags matches
  457. link-search when exposing search matches associated with a link
  458. mark-goto when exposing the jump goal of a mark
  459. bookmark-jump when exposing a bookmark location
  460. isearch when exiting from an incremental search
  461. default default for all contexts not set explicitly"
  462. :group 'org-reveal-location
  463. :type org-context-choice)
  464. (defcustom org-show-following-heading '((default . nil))
  465. "Non-nil means, show following heading when revealing a location.
  466. Org-mode often shows locations in an org-mode file which might have
  467. been invisible before. When this is set, the heading following the
  468. match is shown.
  469. Turning this off for example for sparse trees makes them very compact,
  470. but makes it harder to edit the location of the match. In such a case,
  471. use the command \\[org-reveal] to show more context.
  472. Instead of t, this can also be an alist specifying this option for different
  473. contexts. See `org-show-hierarchy-above' for valid contexts."
  474. :group 'org-reveal-location
  475. :type org-context-choice)
  476. (defcustom org-show-siblings '((default . nil) (isearch t))
  477. "Non-nil means, show all sibling heading when revealing a location.
  478. Org-mode often shows locations in an org-mode file which might have
  479. been invisible before. When this is set, the sibling of the current entry
  480. heading are all made visible. If `org-show-hierarchy-above' is t,
  481. the same happens on each level of the hierarchy above the current entry.
  482. By default this is on for the isearch context, off for all other contexts.
  483. Turning this off for example for sparse trees makes them very compact,
  484. but makes it harder to edit the location of the match. In such a case,
  485. use the command \\[org-reveal] to show more context.
  486. Instead of t, this can also be an alist specifying this option for different
  487. contexts. See `org-show-hierarchy-above' for valid contexts."
  488. :group 'org-reveal-location
  489. :type org-context-choice)
  490. (defcustom org-show-entry-below '((default . nil))
  491. "Non-nil means, show the entry below a headline when revealing a location.
  492. Org-mode often shows locations in an org-mode file which might have
  493. been invisible before. When this is set, the text below the headline that is
  494. exposed is also shown.
  495. By default this is off for all contexts.
  496. Instead of t, this can also be an alist specifying this option for different
  497. contexts. See `org-show-hierarchy-above' for valid contexts."
  498. :group 'org-reveal-location
  499. :type org-context-choice)
  500. (defcustom org-indirect-buffer-display 'other-window
  501. "How should indirect tree buffers be displayed?
  502. This applies to indirect buffers created with the commands
  503. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  504. Valid values are:
  505. current-window Display in the current window
  506. other-window Just display in another window.
  507. dedicated-frame Create one new frame, and re-use it each time.
  508. new-frame Make a new frame each time. Note that in this case
  509. previously-made indirect buffers are kept, and you need to
  510. kill these buffers yourself."
  511. :group 'org-structure
  512. :group 'org-agenda-windows
  513. :type '(choice
  514. (const :tag "In current window" current-window)
  515. (const :tag "In current frame, other window" other-window)
  516. (const :tag "Each time a new frame" new-frame)
  517. (const :tag "One dedicated frame" dedicated-frame)))
  518. (defgroup org-cycle nil
  519. "Options concerning visibility cycling in Org-mode."
  520. :tag "Org Cycle"
  521. :group 'org-structure)
  522. (defcustom org-cycle-skip-children-state-if-no-children t
  523. "Non-nil means, skip CHILDREN state in entries that don't have any."
  524. :group 'org-cycle
  525. :type 'boolean)
  526. (defcustom org-cycle-max-level nil
  527. "Maximum level which should still be subject to visibility cycling.
  528. Levels higher than this will, for cycling, be treated as text, not a headline.
  529. When `org-odd-levels-only' is set, a value of N in this variable actually
  530. means 2N-1 stars as the limiting headline.
  531. When nil, cycle all levels.
  532. Note that the limiting level of cycling is also influenced by
  533. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  534. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  535. than its value."
  536. :group 'org-cycle
  537. :type '(choice
  538. (const :tag "No limit" nil)
  539. (integer :tag "Maximum level")))
  540. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  541. "Names of drawers. Drawers are not opened by cycling on the headline above.
  542. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  543. this:
  544. :DRAWERNAME:
  545. .....
  546. :END:
  547. The drawer \"PROPERTIES\" is special for capturing properties through
  548. the property API.
  549. Drawers can be defined on the per-file basis with a line like:
  550. #+DRAWERS: HIDDEN STATE PROPERTIES"
  551. :group 'org-structure
  552. :group 'org-cycle
  553. :type '(repeat (string :tag "Drawer Name")))
  554. (defcustom org-hide-block-startup nil
  555. "Non-nil means, , entering Org-mode will fold all blocks.
  556. This can also be set in on a per-file basis with
  557. #+STARTUP: hideblocks
  558. #+STARTUP: showblocks"
  559. :group 'org-startup
  560. :group 'org-cycle
  561. :type 'boolean)
  562. (defcustom org-cycle-global-at-bob nil
  563. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  564. This makes it possible to do global cycling without having to use S-TAB or
  565. C-u TAB. For this special case to work, the first line of the buffer
  566. must not be a headline - it may be empty or some other text. When used in
  567. this way, `org-cycle-hook' is disables temporarily, to make sure the
  568. cursor stays at the beginning of the buffer.
  569. When this option is nil, don't do anything special at the beginning
  570. of the buffer."
  571. :group 'org-cycle
  572. :type 'boolean)
  573. (defcustom org-cycle-emulate-tab t
  574. "Where should `org-cycle' emulate TAB.
  575. nil Never
  576. white Only in completely white lines
  577. whitestart Only at the beginning of lines, before the first non-white char
  578. t Everywhere except in headlines
  579. exc-hl-bol Everywhere except at the start of a headline
  580. If TAB is used in a place where it does not emulate TAB, the current subtree
  581. visibility is cycled."
  582. :group 'org-cycle
  583. :type '(choice (const :tag "Never" nil)
  584. (const :tag "Only in completely white lines" white)
  585. (const :tag "Before first char in a line" whitestart)
  586. (const :tag "Everywhere except in headlines" t)
  587. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  588. ))
  589. (defcustom org-cycle-separator-lines 2
  590. "Number of empty lines needed to keep an empty line between collapsed trees.
  591. If you leave an empty line between the end of a subtree and the following
  592. headline, this empty line is hidden when the subtree is folded.
  593. Org-mode will leave (exactly) one empty line visible if the number of
  594. empty lines is equal or larger to the number given in this variable.
  595. So the default 2 means, at least 2 empty lines after the end of a subtree
  596. are needed to produce free space between a collapsed subtree and the
  597. following headline.
  598. If the number is negative, and the number of empty lines is at least -N,
  599. all empty lines are shown.
  600. Special case: when 0, never leave empty lines in collapsed view."
  601. :group 'org-cycle
  602. :type 'integer)
  603. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  604. (defcustom org-pre-cycle-hook nil
  605. "Hook that is run before visibility cycling is happening.
  606. The function(s) in this hook must accept a single argument which indicates
  607. the new state that will be set right after running this hook. The
  608. argument is a symbol. Before a global state change, it can have the values
  609. `overview', `content', or `all'. Before a local state change, it can have
  610. the values `folded', `children', or `subtree'."
  611. :group 'org-cycle
  612. :type 'hook)
  613. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  614. org-cycle-hide-drawers
  615. org-cycle-show-empty-lines
  616. org-optimize-window-after-visibility-change)
  617. "Hook that is run after `org-cycle' has changed the buffer visibility.
  618. The function(s) in this hook must accept a single argument which indicates
  619. the new state that was set by the most recent `org-cycle' command. The
  620. argument is a symbol. After a global state change, it can have the values
  621. `overview', `content', or `all'. After a local state change, it can have
  622. the values `folded', `children', or `subtree'."
  623. :group 'org-cycle
  624. :type 'hook)
  625. (defgroup org-edit-structure nil
  626. "Options concerning structure editing in Org-mode."
  627. :tag "Org Edit Structure"
  628. :group 'org-structure)
  629. (defcustom org-odd-levels-only nil
  630. "Non-nil means, skip even levels and only use odd levels for the outline.
  631. This has the effect that two stars are being added/taken away in
  632. promotion/demotion commands. It also influences how levels are
  633. handled by the exporters.
  634. Changing it requires restart of `font-lock-mode' to become effective
  635. for fontification also in regions already fontified.
  636. You may also set this on a per-file basis by adding one of the following
  637. lines to the buffer:
  638. #+STARTUP: odd
  639. #+STARTUP: oddeven"
  640. :group 'org-edit-structure
  641. :group 'org-font-lock
  642. :type 'boolean)
  643. (defcustom org-adapt-indentation t
  644. "Non-nil means, adapt indentation to outline node level.
  645. When this variable is set, Org assumes that you write outlines by
  646. indenting text in each node to align with the headline (after the stars).
  647. The following issues are influenced by this variable:
  648. - When this is set and the *entire* text in an entry is indented, the
  649. indentation is increased by one space in a demotion command, and
  650. decreased by one in a promotion command. If any line in the entry
  651. body starts with text at column 0, indentation is not changed at all.
  652. - Property drawers and planning information is inserted indented when
  653. this variable s set. When nil, they will not be indented.
  654. - TAB indents a line relative to context. The lines below a headline
  655. will be indented when this variable is set.
  656. Note that this is all about true indentation, by adding and removing
  657. space characters. See also `org-indent.el' which does level-dependent
  658. indentation in a virtual way, i.e. at display time in Emacs."
  659. :group 'org-edit-structure
  660. :type 'boolean)
  661. (defcustom org-special-ctrl-a/e nil
  662. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  663. When t, `C-a' will bring back the cursor to the beginning of the
  664. headline text, i.e. after the stars and after a possible TODO keyword.
  665. In an item, this will be the position after the bullet.
  666. When the cursor is already at that position, another `C-a' will bring
  667. it to the beginning of the line.
  668. `C-e' will jump to the end of the headline, ignoring the presence of tags
  669. in the headline. A second `C-e' will then jump to the true end of the
  670. line, after any tags.
  671. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  672. going to the true line boundary first. Only a directly following, identical
  673. keypress will bring the cursor to the special positions.
  674. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  675. set separately."
  676. :group 'org-edit-structure
  677. :type '(choice
  678. (const :tag "off" nil)
  679. (const :tag "after stars/bullet and before tags first" t)
  680. (const :tag "true line boundary first" reversed)
  681. (cons :tag "Set C-a and C-e separately"
  682. (choice :tag "Special C-a"
  683. (const :tag "off" nil)
  684. (const :tag "after stars/bullet first" t)
  685. (const :tag "before stars/bullet first" reversed))
  686. (choice :tag "Special C-e"
  687. (const :tag "off" nil)
  688. (const :tag "before tags first" t)
  689. (const :tag "after tags first" reversed)))))
  690. (if (fboundp 'defvaralias)
  691. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  692. (defcustom org-special-ctrl-k nil
  693. "Non-nil means `C-k' will behave specially in headlines.
  694. When nil, `C-k' will call the default `kill-line' command.
  695. When t, the following will happen while the cursor is in the headline:
  696. - When the cursor is at the beginning of a headline, kill the entire
  697. line and possible the folded subtree below the line.
  698. - When in the middle of the headline text, kill the headline up to the tags.
  699. - When after the headline text, kill the tags."
  700. :group 'org-edit-structure
  701. :type 'boolean)
  702. (defcustom org-yank-folded-subtrees t
  703. "Non-nil means, when yanking subtrees, fold them.
  704. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  705. it starts with a heading and all other headings in it are either children
  706. or siblings, then fold all the subtrees. However, do this only if no
  707. text after the yank would be swallowed into a folded tree by this action."
  708. :group 'org-edit-structure
  709. :type 'boolean)
  710. (defcustom org-yank-adjusted-subtrees nil
  711. "Non-nil means, when yanking subtrees, adjust the level.
  712. With this setting, `org-paste-subtree' is used to insert the subtree, see
  713. this function for details."
  714. :group 'org-edit-structure
  715. :type 'boolean)
  716. (defcustom org-M-RET-may-split-line '((default . t))
  717. "Non-nil means, M-RET will split the line at the cursor position.
  718. When nil, it will go to the end of the line before making a
  719. new line.
  720. You may also set this option in a different way for different
  721. contexts. Valid contexts are:
  722. headline when creating a new headline
  723. item when creating a new item
  724. table in a table field
  725. default the value to be used for all contexts not explicitly
  726. customized"
  727. :group 'org-structure
  728. :group 'org-table
  729. :type '(choice
  730. (const :tag "Always" t)
  731. (const :tag "Never" nil)
  732. (repeat :greedy t :tag "Individual contexts"
  733. (cons
  734. (choice :tag "Context"
  735. (const headline)
  736. (const item)
  737. (const table)
  738. (const default))
  739. (boolean)))))
  740. (defcustom org-insert-heading-respect-content nil
  741. "Non-nil means, insert new headings after the current subtree.
  742. When nil, the new heading is created directly after the current line.
  743. The commands \\[org-insert-heading-respect-content] and
  744. \\[org-insert-todo-heading-respect-content] turn this variable on
  745. for the duration of the command."
  746. :group 'org-structure
  747. :type 'boolean)
  748. (defcustom org-blank-before-new-entry '((heading . auto)
  749. (plain-list-item . auto))
  750. "Should `org-insert-heading' leave a blank line before new heading/item?
  751. The value is an alist, with `heading' and `plain-list-item' as car,
  752. and a boolean flag as cdr. For plain lists, if the variable
  753. `org-empty-line-terminates-plain-lists' is set, the setting here
  754. is ignored and no empty line is inserted, to keep the list in tact."
  755. :group 'org-edit-structure
  756. :type '(list
  757. (cons (const heading)
  758. (choice (const :tag "Never" nil)
  759. (const :tag "Always" t)
  760. (const :tag "Auto" auto)))
  761. (cons (const plain-list-item)
  762. (choice (const :tag "Never" nil)
  763. (const :tag "Always" t)
  764. (const :tag "Auto" auto)))))
  765. (defcustom org-insert-heading-hook nil
  766. "Hook being run after inserting a new heading."
  767. :group 'org-edit-structure
  768. :type 'hook)
  769. (defcustom org-enable-fixed-width-editor t
  770. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  771. This currently only means, they are never auto-wrapped.
  772. When nil, such lines will be treated like ordinary lines.
  773. See also the QUOTE keyword."
  774. :group 'org-edit-structure
  775. :type 'boolean)
  776. (defcustom org-goto-auto-isearch t
  777. "Non-nil means, typing characters in org-goto starts incremental search."
  778. :group 'org-edit-structure
  779. :type 'boolean)
  780. (defgroup org-sparse-trees nil
  781. "Options concerning sparse trees in Org-mode."
  782. :tag "Org Sparse Trees"
  783. :group 'org-structure)
  784. (defcustom org-highlight-sparse-tree-matches t
  785. "Non-nil means, highlight all matches that define a sparse tree.
  786. The highlights will automatically disappear the next time the buffer is
  787. changed by an edit command."
  788. :group 'org-sparse-trees
  789. :type 'boolean)
  790. (defcustom org-remove-highlights-with-change t
  791. "Non-nil means, any change to the buffer will remove temporary highlights.
  792. Such highlights are created by `org-occur' and `org-clock-display'.
  793. When nil, `C-c C-c needs to be used to get rid of the highlights.
  794. The highlights created by `org-preview-latex-fragment' always need
  795. `C-c C-c' to be removed."
  796. :group 'org-sparse-trees
  797. :group 'org-time
  798. :type 'boolean)
  799. (defcustom org-occur-hook '(org-first-headline-recenter)
  800. "Hook that is run after `org-occur' has constructed a sparse tree.
  801. This can be used to recenter the window to show as much of the structure
  802. as possible."
  803. :group 'org-sparse-trees
  804. :type 'hook)
  805. (defgroup org-imenu-and-speedbar nil
  806. "Options concerning imenu and speedbar in Org-mode."
  807. :tag "Org Imenu and Speedbar"
  808. :group 'org-structure)
  809. (defcustom org-imenu-depth 2
  810. "The maximum level for Imenu access to Org-mode headlines.
  811. This also applied for speedbar access."
  812. :group 'org-imenu-and-speedbar
  813. :type 'integer)
  814. (defgroup org-table nil
  815. "Options concerning tables in Org-mode."
  816. :tag "Org Table"
  817. :group 'org)
  818. (defcustom org-enable-table-editor 'optimized
  819. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  820. When nil, such lines will be treated like ordinary lines.
  821. When equal to the symbol `optimized', the table editor will be optimized to
  822. do the following:
  823. - Automatic overwrite mode in front of whitespace in table fields.
  824. This makes the structure of the table stay in tact as long as the edited
  825. field does not exceed the column width.
  826. - Minimize the number of realigns. Normally, the table is aligned each time
  827. TAB or RET are pressed to move to another field. With optimization this
  828. happens only if changes to a field might have changed the column width.
  829. Optimization requires replacing the functions `self-insert-command',
  830. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  831. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  832. very good at guessing when a re-align will be necessary, but you can always
  833. force one with \\[org-ctrl-c-ctrl-c].
  834. If you would like to use the optimized version in Org-mode, but the
  835. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  836. This variable can be used to turn on and off the table editor during a session,
  837. but in order to toggle optimization, a restart is required.
  838. See also the variable `org-table-auto-blank-field'."
  839. :group 'org-table
  840. :type '(choice
  841. (const :tag "off" nil)
  842. (const :tag "on" t)
  843. (const :tag "on, optimized" optimized)))
  844. (defcustom org-self-insert-cluster-for-undo t
  845. "Non-nil means cluster self-insert commands for undo when possible.
  846. If this is set, then, like in the Emacs command loop, 20 consequtive
  847. characters will be undone together.
  848. This is configurable, because there is some impact on typing performance."
  849. :group 'org-table
  850. :type 'boolean)
  851. (defcustom org-table-tab-recognizes-table.el t
  852. "Non-nil means, TAB will automatically notice a table.el table.
  853. When it sees such a table, it moves point into it and - if necessary -
  854. calls `table-recognize-table'."
  855. :group 'org-table-editing
  856. :type 'boolean)
  857. (defgroup org-link nil
  858. "Options concerning links in Org-mode."
  859. :tag "Org Link"
  860. :group 'org)
  861. (defvar org-link-abbrev-alist-local nil
  862. "Buffer-local version of `org-link-abbrev-alist', which see.
  863. The value of this is taken from the #+LINK lines.")
  864. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  865. (defcustom org-link-abbrev-alist nil
  866. "Alist of link abbreviations.
  867. The car of each element is a string, to be replaced at the start of a link.
  868. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  869. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  870. [[linkkey:tag][description]]
  871. The 'linkkey' must be a word word, starting with a letter, followed
  872. by letters, numbers, '-' or '_'.
  873. If REPLACE is a string, the tag will simply be appended to create the link.
  874. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  875. the placeholder \"%h\" will cause a url-encoded version of the tag to
  876. be inserted at that point (see the function `url-hexify-string').
  877. REPLACE may also be a function that will be called with the tag as the
  878. only argument to create the link, which should be returned as a string.
  879. See the manual for examples."
  880. :group 'org-link
  881. :type '(repeat
  882. (cons
  883. (string :tag "Protocol")
  884. (choice
  885. (string :tag "Format")
  886. (function)))))
  887. (defcustom org-descriptive-links t
  888. "Non-nil means, hide link part and only show description of bracket links.
  889. Bracket links are like [[link][description]]. This variable sets the initial
  890. state in new org-mode buffers. The setting can then be toggled on a
  891. per-buffer basis from the Org->Hyperlinks menu."
  892. :group 'org-link
  893. :type 'boolean)
  894. (defcustom org-link-file-path-type 'adaptive
  895. "How the path name in file links should be stored.
  896. Valid values are:
  897. relative Relative to the current directory, i.e. the directory of the file
  898. into which the link is being inserted.
  899. absolute Absolute path, if possible with ~ for home directory.
  900. noabbrev Absolute path, no abbreviation of home directory.
  901. adaptive Use relative path for files in the current directory and sub-
  902. directories of it. For other files, use an absolute path."
  903. :group 'org-link
  904. :type '(choice
  905. (const relative)
  906. (const absolute)
  907. (const noabbrev)
  908. (const adaptive)))
  909. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  910. "Types of links that should be activated in Org-mode files.
  911. This is a list of symbols, each leading to the activation of a certain link
  912. type. In principle, it does not hurt to turn on most link types - there may
  913. be a small gain when turning off unused link types. The types are:
  914. bracket The recommended [[link][description]] or [[link]] links with hiding.
  915. angular Links in angular brackets that may contain whitespace like
  916. <bbdb:Carsten Dominik>.
  917. plain Plain links in normal text, no whitespace, like http://google.com.
  918. radio Text that is matched by a radio target, see manual for details.
  919. tag Tag settings in a headline (link to tag search).
  920. date Time stamps (link to calendar).
  921. footnote Footnote labels.
  922. Changing this variable requires a restart of Emacs to become effective."
  923. :group 'org-link
  924. :type '(set :greedy t
  925. (const :tag "Double bracket links (new style)" bracket)
  926. (const :tag "Angular bracket links (old style)" angular)
  927. (const :tag "Plain text links" plain)
  928. (const :tag "Radio target matches" radio)
  929. (const :tag "Tags" tag)
  930. (const :tag "Timestamps" date)
  931. (const :tag "Footnotes" footnote)))
  932. (defcustom org-make-link-description-function nil
  933. "Function to use to generate link descriptions from links. If
  934. nil the link location will be used. This function must take two
  935. parameters; the first is the link and the second the description
  936. org-insert-link has generated, and should return the description
  937. to use."
  938. :group 'org-link
  939. :type 'function)
  940. (defgroup org-link-store nil
  941. "Options concerning storing links in Org-mode."
  942. :tag "Org Store Link"
  943. :group 'org-link)
  944. (defcustom org-email-link-description-format "Email %c: %.30s"
  945. "Format of the description part of a link to an email or usenet message.
  946. The following %-escapes will be replaced by corresponding information:
  947. %F full \"From\" field
  948. %f name, taken from \"From\" field, address if no name
  949. %T full \"To\" field
  950. %t first name in \"To\" field, address if no name
  951. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  952. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  953. %s subject
  954. %m message-id.
  955. You may use normal field width specification between the % and the letter.
  956. This is for example useful to limit the length of the subject.
  957. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  958. :group 'org-link-store
  959. :type 'string)
  960. (defcustom org-from-is-user-regexp
  961. (let (r1 r2)
  962. (when (and user-mail-address (not (string= user-mail-address "")))
  963. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  964. (when (and user-full-name (not (string= user-full-name "")))
  965. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  966. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  967. "Regexp matched against the \"From:\" header of an email or usenet message.
  968. It should match if the message is from the user him/herself."
  969. :group 'org-link-store
  970. :type 'regexp)
  971. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  972. "Non-nil means, storing a link to an Org file will use entry IDs.
  973. Note that before this variable is even considered, org-id must be loaded,
  974. so please customize `org-modules' and turn it on.
  975. The variable can have the following values:
  976. t Create an ID if needed to make a link to the current entry.
  977. create-if-interactive
  978. If `org-store-link' is called directly (interactively, as a user
  979. command), do create an ID to support the link. But when doing the
  980. job for remember, only use the ID if it already exists. The
  981. purpose of this setting is to avoid proliferation of unwanted
  982. IDs, just because you happen to be in an Org file when you
  983. call `org-remember' that automatically and preemptively
  984. creates a link. If you do want to get an ID link in a remember
  985. template to an entry not having an ID, create it first by
  986. explicitly creating a link to it, using `C-c C-l' first.
  987. create-if-interactive-and-no-custom-id
  988. Like create-if-interactive, but do not create an ID if there is
  989. a CUSTOM_ID property defined in the entry. This is the default.
  990. use-existing
  991. Use existing ID, do not create one.
  992. nil Never use an ID to make a link, instead link using a text search for
  993. the headline text."
  994. :group 'org-link-store
  995. :type '(choice
  996. (const :tag "Create ID to make link" t)
  997. (const :tag "Create if storing link interactively"
  998. create-if-interactive)
  999. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1000. create-if-interactive-and-no-custom-id)
  1001. (const :tag "Only use existing" use-existing)
  1002. (const :tag "Do not use ID to create link" nil)))
  1003. (defcustom org-context-in-file-links t
  1004. "Non-nil means, file links from `org-store-link' contain context.
  1005. A search string will be added to the file name with :: as separator and
  1006. used to find the context when the link is activated by the command
  1007. `org-open-at-point'.
  1008. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1009. negates this setting for the duration of the command."
  1010. :group 'org-link-store
  1011. :type 'boolean)
  1012. (defcustom org-keep-stored-link-after-insertion nil
  1013. "Non-nil means, keep link in list for entire session.
  1014. The command `org-store-link' adds a link pointing to the current
  1015. location to an internal list. These links accumulate during a session.
  1016. The command `org-insert-link' can be used to insert links into any
  1017. Org-mode file (offering completion for all stored links). When this
  1018. option is nil, every link which has been inserted once using \\[org-insert-link]
  1019. will be removed from the list, to make completing the unused links
  1020. more efficient."
  1021. :group 'org-link-store
  1022. :type 'boolean)
  1023. (defgroup org-link-follow nil
  1024. "Options concerning following links in Org-mode."
  1025. :tag "Org Follow Link"
  1026. :group 'org-link)
  1027. (defcustom org-link-translation-function nil
  1028. "Function to translate links with different syntax to Org syntax.
  1029. This can be used to translate links created for example by the Planner
  1030. or emacs-wiki packages to Org syntax.
  1031. The function must accept two parameters, a TYPE containing the link
  1032. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1033. which is everything after the link protocol. It should return a cons
  1034. with possibly modified values of type and path.
  1035. Org contains a function for this, so if you set this variable to
  1036. `org-translate-link-from-planner', you should be able follow many
  1037. links created by planner."
  1038. :group 'org-link-follow
  1039. :type 'function)
  1040. (defcustom org-follow-link-hook nil
  1041. "Hook that is run after a link has been followed."
  1042. :group 'org-link-follow
  1043. :type 'hook)
  1044. (defcustom org-tab-follows-link nil
  1045. "Non-nil means, on links TAB will follow the link.
  1046. Needs to be set before org.el is loaded.
  1047. This really should not be used, it does not make sense, and the
  1048. implementation is bad."
  1049. :group 'org-link-follow
  1050. :type 'boolean)
  1051. (defcustom org-return-follows-link nil
  1052. "Non-nil means, on links RET will follow the link.
  1053. Needs to be set before org.el is loaded."
  1054. :group 'org-link-follow
  1055. :type 'boolean)
  1056. (defcustom org-mouse-1-follows-link
  1057. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1058. "Non-nil means, mouse-1 on a link will follow the link.
  1059. A longer mouse click will still set point. Does not work on XEmacs.
  1060. Needs to be set before org.el is loaded."
  1061. :group 'org-link-follow
  1062. :type 'boolean)
  1063. (defcustom org-mark-ring-length 4
  1064. "Number of different positions to be recorded in the ring
  1065. Changing this requires a restart of Emacs to work correctly."
  1066. :group 'org-link-follow
  1067. :type 'integer)
  1068. (defcustom org-link-frame-setup
  1069. '((vm . vm-visit-folder-other-frame)
  1070. (gnus . gnus-other-frame)
  1071. (file . find-file-other-window))
  1072. "Setup the frame configuration for following links.
  1073. When following a link with Emacs, it may often be useful to display
  1074. this link in another window or frame. This variable can be used to
  1075. set this up for the different types of links.
  1076. For VM, use any of
  1077. `vm-visit-folder'
  1078. `vm-visit-folder-other-frame'
  1079. For Gnus, use any of
  1080. `gnus'
  1081. `gnus-other-frame'
  1082. `org-gnus-no-new-news'
  1083. For FILE, use any of
  1084. `find-file'
  1085. `find-file-other-window'
  1086. `find-file-other-frame'
  1087. For the calendar, use the variable `calendar-setup'.
  1088. For BBDB, it is currently only possible to display the matches in
  1089. another window."
  1090. :group 'org-link-follow
  1091. :type '(list
  1092. (cons (const vm)
  1093. (choice
  1094. (const vm-visit-folder)
  1095. (const vm-visit-folder-other-window)
  1096. (const vm-visit-folder-other-frame)))
  1097. (cons (const gnus)
  1098. (choice
  1099. (const gnus)
  1100. (const gnus-other-frame)
  1101. (const org-gnus-no-new-news)))
  1102. (cons (const file)
  1103. (choice
  1104. (const find-file)
  1105. (const find-file-other-window)
  1106. (const find-file-other-frame)))))
  1107. (defcustom org-display-internal-link-with-indirect-buffer nil
  1108. "Non-nil means, use indirect buffer to display infile links.
  1109. Activating internal links (from one location in a file to another location
  1110. in the same file) normally just jumps to the location. When the link is
  1111. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1112. another window. When this option is set, the other window actually displays
  1113. an indirect buffer clone of the current buffer, to avoid any visibility
  1114. changes to the current buffer."
  1115. :group 'org-link-follow
  1116. :type 'boolean)
  1117. (defcustom org-open-non-existing-files nil
  1118. "Non-nil means, `org-open-file' will open non-existing files.
  1119. When nil, an error will be generated.
  1120. This variable applies only to external applications because they
  1121. might choke on non-existing files. If the link is to a file that
  1122. will be openend in Emacs, the variable is ignored."
  1123. :group 'org-link-follow
  1124. :type 'boolean)
  1125. (defcustom org-open-directory-means-index-dot-org nil
  1126. "Non-nil means, a link to a directory really means to index.org.
  1127. When nil, following a directory link will run dired or open a finder/explorer
  1128. window on that directory."
  1129. :group 'org-link-follow
  1130. :type 'boolean)
  1131. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1132. "Function and arguments to call for following mailto links.
  1133. This is a list with the first element being a lisp function, and the
  1134. remaining elements being arguments to the function. In string arguments,
  1135. %a will be replaced by the address, and %s will be replaced by the subject
  1136. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1137. :group 'org-link-follow
  1138. :type '(choice
  1139. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1140. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1141. (const :tag "message-mail" (message-mail "%a" "%s"))
  1142. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1143. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1144. "Non-nil means, ask for confirmation before executing shell links.
  1145. Shell links can be dangerous: just think about a link
  1146. [[shell:rm -rf ~/*][Google Search]]
  1147. This link would show up in your Org-mode document as \"Google Search\",
  1148. but really it would remove your entire home directory.
  1149. Therefore we advise against setting this variable to nil.
  1150. Just change it to `y-or-n-p' if you want to confirm with a
  1151. single keystroke rather than having to type \"yes\"."
  1152. :group 'org-link-follow
  1153. :type '(choice
  1154. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1155. (const :tag "with y-or-n (faster)" y-or-n-p)
  1156. (const :tag "no confirmation (dangerous)" nil)))
  1157. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1158. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1159. Elisp links can be dangerous: just think about a link
  1160. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1161. This link would show up in your Org-mode document as \"Google Search\",
  1162. but really it would remove your entire home directory.
  1163. Therefore we advise against setting this variable to nil.
  1164. Just change it to `y-or-n-p' if you want to confirm with a
  1165. single keystroke rather than having to type \"yes\"."
  1166. :group 'org-link-follow
  1167. :type '(choice
  1168. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1169. (const :tag "with y-or-n (faster)" y-or-n-p)
  1170. (const :tag "no confirmation (dangerous)" nil)))
  1171. (defconst org-file-apps-defaults-gnu
  1172. '((remote . emacs)
  1173. (system . mailcap)
  1174. (t . mailcap))
  1175. "Default file applications on a UNIX or GNU/Linux system.
  1176. See `org-file-apps'.")
  1177. (defconst org-file-apps-defaults-macosx
  1178. '((remote . emacs)
  1179. (t . "open %s")
  1180. (system . "open %s")
  1181. ("ps.gz" . "gv %s")
  1182. ("eps.gz" . "gv %s")
  1183. ("dvi" . "xdvi %s")
  1184. ("fig" . "xfig %s"))
  1185. "Default file applications on a MacOS X system.
  1186. The system \"open\" is known as a default, but we use X11 applications
  1187. for some files for which the OS does not have a good default.
  1188. See `org-file-apps'.")
  1189. (defconst org-file-apps-defaults-windowsnt
  1190. (list
  1191. '(remote . emacs)
  1192. (cons t
  1193. (list (if (featurep 'xemacs)
  1194. 'mswindows-shell-execute
  1195. 'w32-shell-execute)
  1196. "open" 'file))
  1197. (cons 'system
  1198. (list (if (featurep 'xemacs)
  1199. 'mswindows-shell-execute
  1200. 'w32-shell-execute)
  1201. "open" 'file)))
  1202. "Default file applications on a Windows NT system.
  1203. The system \"open\" is used for most files.
  1204. See `org-file-apps'.")
  1205. (defcustom org-file-apps
  1206. '(
  1207. (auto-mode . emacs)
  1208. ("\\.x?html?\\'" . default)
  1209. ("\\.pdf\\'" . default)
  1210. )
  1211. "External applications for opening `file:path' items in a document.
  1212. Org-mode uses system defaults for different file types, but
  1213. you can use this variable to set the application for a given file
  1214. extension. The entries in this list are cons cells where the car identifies
  1215. files and the cdr the corresponding command. Possible values for the
  1216. file identifier are
  1217. \"regex\" Regular expression matched against the file name. For backward
  1218. compatibility, this can also be a string with only alphanumeric
  1219. characters, which is then interpreted as an extension.
  1220. `directory' Matches a directory
  1221. `remote' Matches a remote file, accessible through tramp or efs.
  1222. Remote files most likely should be visited through Emacs
  1223. because external applications cannot handle such paths.
  1224. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1225. so all files Emacs knows how to handle. Using this with
  1226. command `emacs' will open most files in Emacs. Beware that this
  1227. will also open html files inside Emacs, unless you add
  1228. (\"html\" . default) to the list as well.
  1229. t Default for files not matched by any of the other options.
  1230. `system' The system command to open files, like `open' on Windows
  1231. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1232. that will be selected if you call `C-c C-o' with a double
  1233. `C-u C-u' prefix.
  1234. Possible values for the command are:
  1235. `emacs' The file will be visited by the current Emacs process.
  1236. `default' Use the default application for this file type, which is the
  1237. association for t in the list, most likely in the system-specific
  1238. part.
  1239. This can be used to overrule an unwanted setting in the
  1240. system-specific variable.
  1241. `system' Use the system command for opening files, like \"open\".
  1242. This command is specified by the entry whose car is `system'.
  1243. Most likely, the system-specific version of this variable
  1244. does define this command, but you can overrule/replace it
  1245. here.
  1246. string A command to be executed by a shell; %s will be replaced
  1247. by the path to the file.
  1248. sexp A Lisp form which will be evaluated. The file path will
  1249. be available in the Lisp variable `file'.
  1250. For more examples, see the system specific constants
  1251. `org-file-apps-defaults-macosx'
  1252. `org-file-apps-defaults-windowsnt'
  1253. `org-file-apps-defaults-gnu'."
  1254. :group 'org-link-follow
  1255. :type '(repeat
  1256. (cons (choice :value ""
  1257. (string :tag "Extension")
  1258. (const :tag "System command to open files" system)
  1259. (const :tag "Default for unrecognized files" t)
  1260. (const :tag "Remote file" remote)
  1261. (const :tag "Links to a directory" directory)
  1262. (const :tag "Any files that have Emacs modes"
  1263. auto-mode))
  1264. (choice :value ""
  1265. (const :tag "Visit with Emacs" emacs)
  1266. (const :tag "Use default" default)
  1267. (const :tag "Use the system command" system)
  1268. (string :tag "Command")
  1269. (sexp :tag "Lisp form")))))
  1270. (defgroup org-refile nil
  1271. "Options concerning refiling entries in Org-mode."
  1272. :tag "Org Refile"
  1273. :group 'org)
  1274. (defcustom org-directory "~/org"
  1275. "Directory with org files.
  1276. This is just a default location to look for Org files. There is no need
  1277. at all to put your files into this directory. It is only used in the
  1278. following situations:
  1279. 1. When a remember template specifies a target file that is not an
  1280. absolute path. The path will then be interpreted relative to
  1281. `org-directory'
  1282. 2. When a remember note is filed away in an interactive way (when exiting the
  1283. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1284. with `org-directory' as the default path."
  1285. :group 'org-refile
  1286. :group 'org-remember
  1287. :type 'directory)
  1288. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1289. "Default target for storing notes.
  1290. Used by the hooks for remember.el. This can be a string, or nil to mean
  1291. the value of `remember-data-file'.
  1292. You can set this on a per-template basis with the variable
  1293. `org-remember-templates'."
  1294. :group 'org-refile
  1295. :group 'org-remember
  1296. :type '(choice
  1297. (const :tag "Default from remember-data-file" nil)
  1298. file))
  1299. (defcustom org-goto-interface 'outline
  1300. "The default interface to be used for `org-goto'.
  1301. Allowed values are:
  1302. outline The interface shows an outline of the relevant file
  1303. and the correct heading is found by moving through
  1304. the outline or by searching with incremental search.
  1305. outline-path-completion Headlines in the current buffer are offered via
  1306. completion. This is the interface also used by
  1307. the refile command."
  1308. :group 'org-refile
  1309. :type '(choice
  1310. (const :tag "Outline" outline)
  1311. (const :tag "Outline-path-completion" outline-path-completion)))
  1312. (defcustom org-goto-max-level 5
  1313. "Maximum level to be considered when running org-goto with refile interface."
  1314. :group 'org-refile
  1315. :type 'integer)
  1316. (defcustom org-reverse-note-order nil
  1317. "Non-nil means, store new notes at the beginning of a file or entry.
  1318. When nil, new notes will be filed to the end of a file or entry.
  1319. This can also be a list with cons cells of regular expressions that
  1320. are matched against file names, and values."
  1321. :group 'org-remember
  1322. :group 'org-refile
  1323. :type '(choice
  1324. (const :tag "Reverse always" t)
  1325. (const :tag "Reverse never" nil)
  1326. (repeat :tag "By file name regexp"
  1327. (cons regexp boolean))))
  1328. (defcustom org-refile-targets nil
  1329. "Targets for refiling entries with \\[org-refile].
  1330. This is list of cons cells. Each cell contains:
  1331. - a specification of the files to be considered, either a list of files,
  1332. or a symbol whose function or variable value will be used to retrieve
  1333. a file name or a list of file names. If you use `org-agenda-files' for
  1334. that, all agenda files will be scanned for targets. Nil means, consider
  1335. headings in the current buffer.
  1336. - A specification of how to find candidate refile targets. This may be
  1337. any of:
  1338. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1339. This tag has to be present in all target headlines, inheritance will
  1340. not be considered.
  1341. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1342. todo keyword.
  1343. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1344. headlines that are refiling targets.
  1345. - a cons cell (:level . N). Any headline of level N is considered a target.
  1346. Note that, when `org-odd-levels-only' is set, level corresponds to
  1347. order in hierarchy, not to the number of stars.
  1348. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1349. Note that, when `org-odd-levels-only' is set, level corresponds to
  1350. order in hierarchy, not to the number of stars.
  1351. You can set the variable `org-refile-target-verify-function' to a function
  1352. to verify each headline found by the simple critery above.
  1353. When this variable is nil, all top-level headlines in the current buffer
  1354. are used, equivalent to the value `((nil . (:level . 1))'."
  1355. :group 'org-refile
  1356. :type '(repeat
  1357. (cons
  1358. (choice :value org-agenda-files
  1359. (const :tag "All agenda files" org-agenda-files)
  1360. (const :tag "Current buffer" nil)
  1361. (function) (variable) (file))
  1362. (choice :tag "Identify target headline by"
  1363. (cons :tag "Specific tag" (const :value :tag) (string))
  1364. (cons :tag "TODO keyword" (const :value :todo) (string))
  1365. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1366. (cons :tag "Level number" (const :value :level) (integer))
  1367. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1368. (defcustom org-refile-target-verify-function nil
  1369. "Function to verify if the headline at point should be a refile target.
  1370. The function will be called without arguments, with point at the
  1371. beginning of the headline. It should return t and leave point
  1372. where it is if the headline is a valid target for refiling.
  1373. If the target should not be selected, the function must return nil.
  1374. In addition to this, it may move point to a place from where the search
  1375. should be continued. For example, the function may decide that the entire
  1376. subtree of the current entry should be excluded and move point to the end
  1377. of the subtree."
  1378. :group 'org-refile
  1379. :type 'function)
  1380. (defcustom org-refile-use-outline-path nil
  1381. "Non-nil means, provide refile targets as paths.
  1382. So a level 3 headline will be available as level1/level2/level3.
  1383. When the value is `file', also include the file name (without directory)
  1384. into the path. In this case, you can also stop the completion after
  1385. the file name, to get entries inserted as top level in the file.
  1386. When `full-file-path', include the full file path."
  1387. :group 'org-refile
  1388. :type '(choice
  1389. (const :tag "Not" nil)
  1390. (const :tag "Yes" t)
  1391. (const :tag "Start with file name" file)
  1392. (const :tag "Start with full file path" full-file-path)))
  1393. (defcustom org-outline-path-complete-in-steps t
  1394. "Non-nil means, complete the outline path in hierarchical steps.
  1395. When Org-mode uses the refile interface to select an outline path
  1396. \(see variable `org-refile-use-outline-path'), the completion of
  1397. the path can be done is a single go, or if can be done in steps down
  1398. the headline hierarchy. Going in steps is probably the best if you
  1399. do not use a special completion package like `ido' or `icicles'.
  1400. However, when using these packages, going in one step can be very
  1401. fast, while still showing the whole path to the entry."
  1402. :group 'org-refile
  1403. :type 'boolean)
  1404. (defcustom org-refile-allow-creating-parent-nodes nil
  1405. "Non-nil means, allow to create new nodes as refile targets.
  1406. New nodes are then created by adding \"/new node name\" to the completion
  1407. of an existing node. When the value of this variable is `confirm',
  1408. new node creation must be confirmed by the user (recommended)
  1409. When nil, the completion must match an existing entry.
  1410. Note that, if the new heading is not seen by the criteria
  1411. listed in `org-refile-targets', multiple instances of the same
  1412. heading would be created by trying again to file under the new
  1413. heading."
  1414. :group 'org-refile
  1415. :type '(choice
  1416. (const :tag "Never" nil)
  1417. (const :tag "Always" t)
  1418. (const :tag "Prompt for confirmation" confirm)))
  1419. (defgroup org-todo nil
  1420. "Options concerning TODO items in Org-mode."
  1421. :tag "Org TODO"
  1422. :group 'org)
  1423. (defgroup org-progress nil
  1424. "Options concerning Progress logging in Org-mode."
  1425. :tag "Org Progress"
  1426. :group 'org-time)
  1427. (defvar org-todo-interpretation-widgets
  1428. '(
  1429. (:tag "Sequence (cycling hits every state)" sequence)
  1430. (:tag "Type (cycling directly to DONE)" type))
  1431. "The available interpretation symbols for customizing
  1432. `org-todo-keywords'.
  1433. Interested libraries should add to this list.")
  1434. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1435. "List of TODO entry keyword sequences and their interpretation.
  1436. \\<org-mode-map>This is a list of sequences.
  1437. Each sequence starts with a symbol, either `sequence' or `type',
  1438. indicating if the keywords should be interpreted as a sequence of
  1439. action steps, or as different types of TODO items. The first
  1440. keywords are states requiring action - these states will select a headline
  1441. for inclusion into the global TODO list Org-mode produces. If one of
  1442. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1443. signify that no further action is necessary. If \"|\" is not found,
  1444. the last keyword is treated as the only DONE state of the sequence.
  1445. The command \\[org-todo] cycles an entry through these states, and one
  1446. additional state where no keyword is present. For details about this
  1447. cycling, see the manual.
  1448. TODO keywords and interpretation can also be set on a per-file basis with
  1449. the special #+SEQ_TODO and #+TYP_TODO lines.
  1450. Each keyword can optionally specify a character for fast state selection
  1451. \(in combination with the variable `org-use-fast-todo-selection')
  1452. and specifiers for state change logging, using the same syntax
  1453. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1454. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1455. indicates to record a time stamp each time this state is selected.
  1456. Each keyword may also specify if a timestamp or a note should be
  1457. recorded when entering or leaving the state, by adding additional
  1458. characters in the parenthesis after the keyword. This looks like this:
  1459. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1460. record only the time of the state change. With X and Y being either
  1461. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1462. Y when leaving the state if and only if the *target* state does not
  1463. define X. You may omit any of the fast-selection key or X or /Y,
  1464. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1465. For backward compatibility, this variable may also be just a list
  1466. of keywords - in this case the interpretation (sequence or type) will be
  1467. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1468. :group 'org-todo
  1469. :group 'org-keywords
  1470. :type '(choice
  1471. (repeat :tag "Old syntax, just keywords"
  1472. (string :tag "Keyword"))
  1473. (repeat :tag "New syntax"
  1474. (cons
  1475. (choice
  1476. :tag "Interpretation"
  1477. ;;Quick and dirty way to see
  1478. ;;`org-todo-interpretations'. This takes the
  1479. ;;place of item arguments
  1480. :convert-widget
  1481. (lambda (widget)
  1482. (widget-put widget
  1483. :args (mapcar
  1484. #'(lambda (x)
  1485. (widget-convert
  1486. (cons 'const x)))
  1487. org-todo-interpretation-widgets))
  1488. widget))
  1489. (repeat
  1490. (string :tag "Keyword"))))))
  1491. (defvar org-todo-keywords-1 nil
  1492. "All TODO and DONE keywords active in a buffer.")
  1493. (make-variable-buffer-local 'org-todo-keywords-1)
  1494. (defvar org-todo-keywords-for-agenda nil)
  1495. (defvar org-done-keywords-for-agenda nil)
  1496. (defvar org-todo-keyword-alist-for-agenda nil)
  1497. (defvar org-tag-alist-for-agenda nil)
  1498. (defvar org-agenda-contributing-files nil)
  1499. (defvar org-not-done-keywords nil)
  1500. (make-variable-buffer-local 'org-not-done-keywords)
  1501. (defvar org-done-keywords nil)
  1502. (make-variable-buffer-local 'org-done-keywords)
  1503. (defvar org-todo-heads nil)
  1504. (make-variable-buffer-local 'org-todo-heads)
  1505. (defvar org-todo-sets nil)
  1506. (make-variable-buffer-local 'org-todo-sets)
  1507. (defvar org-todo-log-states nil)
  1508. (make-variable-buffer-local 'org-todo-log-states)
  1509. (defvar org-todo-kwd-alist nil)
  1510. (make-variable-buffer-local 'org-todo-kwd-alist)
  1511. (defvar org-todo-key-alist nil)
  1512. (make-variable-buffer-local 'org-todo-key-alist)
  1513. (defvar org-todo-key-trigger nil)
  1514. (make-variable-buffer-local 'org-todo-key-trigger)
  1515. (defcustom org-todo-interpretation 'sequence
  1516. "Controls how TODO keywords are interpreted.
  1517. This variable is in principle obsolete and is only used for
  1518. backward compatibility, if the interpretation of todo keywords is
  1519. not given already in `org-todo-keywords'. See that variable for
  1520. more information."
  1521. :group 'org-todo
  1522. :group 'org-keywords
  1523. :type '(choice (const sequence)
  1524. (const type)))
  1525. (defcustom org-use-fast-todo-selection t
  1526. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1527. This variable describes if and under what circumstances the cycling
  1528. mechanism for TODO keywords will be replaced by a single-key, direct
  1529. selection scheme.
  1530. When nil, fast selection is never used.
  1531. When the symbol `prefix', it will be used when `org-todo' is called with
  1532. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1533. in an agenda buffer.
  1534. When t, fast selection is used by default. In this case, the prefix
  1535. argument forces cycling instead.
  1536. In all cases, the special interface is only used if access keys have actually
  1537. been assigned by the user, i.e. if keywords in the configuration are followed
  1538. by a letter in parenthesis, like TODO(t)."
  1539. :group 'org-todo
  1540. :type '(choice
  1541. (const :tag "Never" nil)
  1542. (const :tag "By default" t)
  1543. (const :tag "Only with C-u C-c C-t" prefix)))
  1544. (defcustom org-provide-todo-statistics t
  1545. "Non-nil means, update todo statistics after insert and toggle.
  1546. ALL-HEADLINES means update todo statistics by including headlines
  1547. with no TODO keyword as well, counting them as not done.
  1548. A list of TODO keywords means the same, but skip keywords that are
  1549. not in this list.
  1550. When this is set, todo statistics is updated in the parent of the
  1551. current entry each time a todo state is changed."
  1552. :group 'org-todo
  1553. :type '(choice
  1554. (const :tag "Yes, only for TODO entries" t)
  1555. (const :tag "Yes, including all entries" 'all-headlines)
  1556. (repeat :tag "Yes, for TODOs in this list"
  1557. (string :tag "TODO keyword"))
  1558. (other :tag "No TODO statistics" nil)))
  1559. (defcustom org-hierarchical-todo-statistics t
  1560. "Non-nil means, TODO statistics covers just direct children.
  1561. When nil, all entries in the subtree are considered.
  1562. This has only an effect if `org-provide-todo-statistics' is set.
  1563. To set this to nil for only a single subtree, use a COOKIE_DATA
  1564. property and include the word \"recursive\" into the value."
  1565. :group 'org-todo
  1566. :type 'boolean)
  1567. (defcustom org-after-todo-state-change-hook nil
  1568. "Hook which is run after the state of a TODO item was changed.
  1569. The new state (a string with a TODO keyword, or nil) is available in the
  1570. Lisp variable `state'."
  1571. :group 'org-todo
  1572. :type 'hook)
  1573. (defvar org-blocker-hook nil
  1574. "Hook for functions that are allowed to block a state change.
  1575. Each function gets as its single argument a property list, see
  1576. `org-trigger-hook' for more information about this list.
  1577. If any of the functions in this hook returns nil, the state change
  1578. is blocked.")
  1579. (defvar org-trigger-hook nil
  1580. "Hook for functions that are triggered by a state change.
  1581. Each function gets as its single argument a property list with at least
  1582. the following elements:
  1583. (:type type-of-change :position pos-at-entry-start
  1584. :from old-state :to new-state)
  1585. Depending on the type, more properties may be present.
  1586. This mechanism is currently implemented for:
  1587. TODO state changes
  1588. ------------------
  1589. :type todo-state-change
  1590. :from previous state (keyword as a string), or nil, or a symbol
  1591. 'todo' or 'done', to indicate the general type of state.
  1592. :to new state, like in :from")
  1593. (defcustom org-enforce-todo-dependencies nil
  1594. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1595. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1596. be blocked if any prior sibling is not yet done.
  1597. Finally, if the parent is blocked because of ordered siblings of its own,
  1598. the child will also be blocked.
  1599. This variable needs to be set before org.el is loaded, and you need to
  1600. restart Emacs after a change to make the change effective. The only way
  1601. to change is while Emacs is running is through the customize interface."
  1602. :set (lambda (var val)
  1603. (set var val)
  1604. (if val
  1605. (add-hook 'org-blocker-hook
  1606. 'org-block-todo-from-children-or-siblings-or-parent)
  1607. (remove-hook 'org-blocker-hook
  1608. 'org-block-todo-from-children-or-siblings-or-parent)))
  1609. :group 'org-todo
  1610. :type 'boolean)
  1611. (defcustom org-enforce-todo-checkbox-dependencies nil
  1612. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1613. When this is nil, checkboxes have no influence on switching TODO states.
  1614. When non-nil, you first need to check off all check boxes before the TODO
  1615. entry can be switched to DONE.
  1616. This variable needs to be set before org.el is loaded, and you need to
  1617. restart Emacs after a change to make the change effective. The only way
  1618. to change is while Emacs is running is through the customize interface."
  1619. :set (lambda (var val)
  1620. (set var val)
  1621. (if val
  1622. (add-hook 'org-blocker-hook
  1623. 'org-block-todo-from-checkboxes)
  1624. (remove-hook 'org-blocker-hook
  1625. 'org-block-todo-from-checkboxes)))
  1626. :group 'org-todo
  1627. :type 'boolean)
  1628. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1629. "Non-nil means, inserting a TODO heading is treated as state change.
  1630. So when the command \\[org-insert-todo-heading] is used, state change
  1631. logging will apply if appropriate. When nil, the new TODO item will
  1632. be inserted directly, and no logging will take place."
  1633. :group 'org-todo
  1634. :type 'boolean)
  1635. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1636. "Non-nil means, switching TODO states with S-cursor counts as state change.
  1637. This is the default behavior. However, setting this to nil allows a
  1638. convenient way to select a TODO state and bypass any logging associated
  1639. with that."
  1640. :group 'org-todo
  1641. :type 'boolean)
  1642. (defcustom org-todo-state-tags-triggers nil
  1643. "Tag changes that should be triggered by TODO state changes.
  1644. This is a list. Each entry is
  1645. (state-change (tag . flag) .......)
  1646. State-change can be a string with a state, and empty string to indicate the
  1647. state that has no TODO keyword, or it can be one of the symbols `todo'
  1648. or `done', meaning any not-done or done state, respectively."
  1649. :group 'org-todo
  1650. :group 'org-tags
  1651. :type '(repeat
  1652. (cons (choice :tag "When changing to"
  1653. (const :tag "Not-done state" todo)
  1654. (const :tag "Done state" done)
  1655. (string :tag "State"))
  1656. (repeat
  1657. (cons :tag "Tag action"
  1658. (string :tag "Tag")
  1659. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1660. (defcustom org-log-done nil
  1661. "Information to record when a task moves to the DONE state.
  1662. Possible values are:
  1663. nil Don't add anything, just change the keyword
  1664. time Add a time stamp to the task
  1665. note Prompt a closing note and add it with template `org-log-note-headings'
  1666. This option can also be set with on a per-file-basis with
  1667. #+STARTUP: nologdone
  1668. #+STARTUP: logdone
  1669. #+STARTUP: lognotedone
  1670. You can have local logging settings for a subtree by setting the LOGGING
  1671. property to one or more of these keywords."
  1672. :group 'org-todo
  1673. :group 'org-progress
  1674. :type '(choice
  1675. (const :tag "No logging" nil)
  1676. (const :tag "Record CLOSED timestamp" time)
  1677. (const :tag "Record CLOSED timestamp with closing note." note)))
  1678. ;; Normalize old uses of org-log-done.
  1679. (cond
  1680. ((eq org-log-done t) (setq org-log-done 'time))
  1681. ((and (listp org-log-done) (memq 'done org-log-done))
  1682. (setq org-log-done 'note)))
  1683. (defcustom org-log-note-clock-out nil
  1684. "Non-nil means, record a note when clocking out of an item.
  1685. This can also be configured on a per-file basis by adding one of
  1686. the following lines anywhere in the buffer:
  1687. #+STARTUP: lognoteclock-out
  1688. #+STARTUP: nolognoteclock-out"
  1689. :group 'org-todo
  1690. :group 'org-progress
  1691. :type 'boolean)
  1692. (defcustom org-log-done-with-time t
  1693. "Non-nil means, the CLOSED time stamp will contain date and time.
  1694. When nil, only the date will be recorded."
  1695. :group 'org-progress
  1696. :type 'boolean)
  1697. (defcustom org-log-note-headings
  1698. '((done . "CLOSING NOTE %t")
  1699. (state . "State %-12s from %-12S %t")
  1700. (note . "Note taken on %t")
  1701. (clock-out . ""))
  1702. "Headings for notes added to entries.
  1703. The value is an alist, with the car being a symbol indicating the note
  1704. context, and the cdr is the heading to be used. The heading may also be the
  1705. empty string.
  1706. %t in the heading will be replaced by a time stamp.
  1707. %s will be replaced by the new TODO state, in double quotes.
  1708. %S will be replaced by the old TODO state, in double quotes.
  1709. %u will be replaced by the user name.
  1710. %U will be replaced by the full user name."
  1711. :group 'org-todo
  1712. :group 'org-progress
  1713. :type '(list :greedy t
  1714. (cons (const :tag "Heading when closing an item" done) string)
  1715. (cons (const :tag
  1716. "Heading when changing todo state (todo sequence only)"
  1717. state) string)
  1718. (cons (const :tag "Heading when just taking a note" note) string)
  1719. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1720. (unless (assq 'note org-log-note-headings)
  1721. (push '(note . "%t") org-log-note-headings))
  1722. (defcustom org-log-into-drawer nil
  1723. "Non-nil means, insert state change notes and time stamps into a drawer.
  1724. When nil, state changes notes will be inserted after the headline and
  1725. any scheduling and clock lines, but not inside a drawer.
  1726. The value of this variable should be the name of the drawer to use.
  1727. LOGBOOK is proposed at the default drawer for this purpose, you can
  1728. also set this to a string to define the drawer of your choice.
  1729. A value of t is also allowed, representing \"LOGBOOK\".
  1730. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1731. will be ignored.
  1732. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1733. a subtree."
  1734. :group 'org-todo
  1735. :group 'org-progress
  1736. :type '(choice
  1737. (const :tag "Not into a drawer" nil)
  1738. (const :tag "LOGBOOK" t)
  1739. (string :tag "Other")))
  1740. (if (fboundp 'defvaralias)
  1741. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1742. (defun org-log-into-drawer ()
  1743. "Return the value of `org-log-into-drawer', but let properties overrule.
  1744. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1745. used instead of the default value."
  1746. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1747. (cond
  1748. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1749. ((equal p "t") "LOGBOOK")
  1750. (t p))))
  1751. (defcustom org-log-state-notes-insert-after-drawers nil
  1752. "Non-nil means, insert state change notes after any drawers in entry.
  1753. Only the drawers that *immediately* follow the headline and the
  1754. deadline/scheduled line are skipped.
  1755. When nil, insert notes right after the heading and perhaps the line
  1756. with deadline/scheduling if present.
  1757. This variable will have no effect if `org-log-into-drawer' is
  1758. set."
  1759. :group 'org-todo
  1760. :group 'org-progress
  1761. :type 'boolean)
  1762. (defcustom org-log-states-order-reversed t
  1763. "Non-nil means, the latest state change note will be directly after heading.
  1764. When nil, the notes will be orderer according to time."
  1765. :group 'org-todo
  1766. :group 'org-progress
  1767. :type 'boolean)
  1768. (defcustom org-log-repeat 'time
  1769. "Non-nil means, record moving through the DONE state when triggering repeat.
  1770. An auto-repeating task is immediately switched back to TODO when
  1771. marked DONE. If you are not logging state changes (by adding \"@\"
  1772. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1773. record a closing note, there will be no record of the task moving
  1774. through DONE. This variable forces taking a note anyway.
  1775. nil Don't force a record
  1776. time Record a time stamp
  1777. note Record a note
  1778. This option can also be set with on a per-file-basis with
  1779. #+STARTUP: logrepeat
  1780. #+STARTUP: lognoterepeat
  1781. #+STARTUP: nologrepeat
  1782. You can have local logging settings for a subtree by setting the LOGGING
  1783. property to one or more of these keywords."
  1784. :group 'org-todo
  1785. :group 'org-progress
  1786. :type '(choice
  1787. (const :tag "Don't force a record" nil)
  1788. (const :tag "Force recording the DONE state" time)
  1789. (const :tag "Force recording a note with the DONE state" note)))
  1790. (defgroup org-priorities nil
  1791. "Priorities in Org-mode."
  1792. :tag "Org Priorities"
  1793. :group 'org-todo)
  1794. (defcustom org-enable-priority-commands t
  1795. "Non-nil means, priority commands are active.
  1796. When nil, these commands will be disabled, so that you never accidentally
  1797. set a priority."
  1798. :group 'org-priorities
  1799. :type 'boolean)
  1800. (defcustom org-highest-priority ?A
  1801. "The highest priority of TODO items. A character like ?A, ?B etc.
  1802. Must have a smaller ASCII number than `org-lowest-priority'."
  1803. :group 'org-priorities
  1804. :type 'character)
  1805. (defcustom org-lowest-priority ?C
  1806. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1807. Must have a larger ASCII number than `org-highest-priority'."
  1808. :group 'org-priorities
  1809. :type 'character)
  1810. (defcustom org-default-priority ?B
  1811. "The default priority of TODO items.
  1812. This is the priority an item get if no explicit priority is given."
  1813. :group 'org-priorities
  1814. :type 'character)
  1815. (defcustom org-priority-start-cycle-with-default t
  1816. "Non-nil means, start with default priority when starting to cycle.
  1817. When this is nil, the first step in the cycle will be (depending on the
  1818. command used) one higher or lower that the default priority."
  1819. :group 'org-priorities
  1820. :type 'boolean)
  1821. (defgroup org-time nil
  1822. "Options concerning time stamps and deadlines in Org-mode."
  1823. :tag "Org Time"
  1824. :group 'org)
  1825. (defcustom org-insert-labeled-timestamps-at-point nil
  1826. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1827. When nil, these labeled time stamps are forces into the second line of an
  1828. entry, just after the headline. When scheduling from the global TODO list,
  1829. the time stamp will always be forced into the second line."
  1830. :group 'org-time
  1831. :type 'boolean)
  1832. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1833. "Formats for `format-time-string' which are used for time stamps.
  1834. It is not recommended to change this constant.")
  1835. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1836. "Number of minutes to round time stamps to.
  1837. These are two values, the first applies when first creating a time stamp.
  1838. The second applies when changing it with the commands `S-up' and `S-down'.
  1839. When changing the time stamp, this means that it will change in steps
  1840. of N minutes, as given by the second value.
  1841. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1842. numbers should be factors of 60, so for example 5, 10, 15.
  1843. When this is larger than 1, you can still force an exact time-stamp by using
  1844. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1845. and by using a prefix arg to `S-up/down' to specify the exact number
  1846. of minutes to shift."
  1847. :group 'org-time
  1848. :get '(lambda (var) ; Make sure all entries have 5 elements
  1849. (if (integerp (default-value var))
  1850. (list (default-value var) 5)
  1851. (default-value var)))
  1852. :type '(list
  1853. (integer :tag "when inserting times")
  1854. (integer :tag "when modifying times")))
  1855. ;; Normalize old customizations of this variable.
  1856. (when (integerp org-time-stamp-rounding-minutes)
  1857. (setq org-time-stamp-rounding-minutes
  1858. (list org-time-stamp-rounding-minutes
  1859. org-time-stamp-rounding-minutes)))
  1860. (defcustom org-display-custom-times nil
  1861. "Non-nil means, overlay custom formats over all time stamps.
  1862. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1863. To turn this on on a per-file basis, insert anywhere in the file:
  1864. #+STARTUP: customtime"
  1865. :group 'org-time
  1866. :set 'set-default
  1867. :type 'sexp)
  1868. (make-variable-buffer-local 'org-display-custom-times)
  1869. (defcustom org-time-stamp-custom-formats
  1870. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1871. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1872. These are overlayed over the default ISO format if the variable
  1873. `org-display-custom-times' is set. Time like %H:%M should be at the
  1874. end of the second format. The custom formats are also honored by export
  1875. commands, if custom time display is turned on at the time of export."
  1876. :group 'org-time
  1877. :type 'sexp)
  1878. (defun org-time-stamp-format (&optional long inactive)
  1879. "Get the right format for a time string."
  1880. (let ((f (if long (cdr org-time-stamp-formats)
  1881. (car org-time-stamp-formats))))
  1882. (if inactive
  1883. (concat "[" (substring f 1 -1) "]")
  1884. f)))
  1885. (defcustom org-time-clocksum-format "%d:%02d"
  1886. "The format string used when creating CLOCKSUM lines, or when
  1887. org-mode generates a time duration."
  1888. :group 'org-time
  1889. :type 'string)
  1890. (defcustom org-deadline-warning-days 14
  1891. "No. of days before expiration during which a deadline becomes active.
  1892. This variable governs the display in sparse trees and in the agenda.
  1893. When 0 or negative, it means use this number (the absolute value of it)
  1894. even if a deadline has a different individual lead time specified.
  1895. Custom commands can set this variable in the options section."
  1896. :group 'org-time
  1897. :group 'org-agenda-daily/weekly
  1898. :type 'integer)
  1899. (defcustom org-read-date-prefer-future t
  1900. "Non-nil means, assume future for incomplete date input from user.
  1901. This affects the following situations:
  1902. 1. The user gives a day, but no month.
  1903. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1904. read this as the third of *next* month. However, if you enter \"17\",
  1905. it will be considered as *this* month.
  1906. 2. The user gives a month but not a year.
  1907. For example, if it is april and you enter \"feb 2\", this will be read
  1908. as feb 2, *next* year. \"May 5\", however, will be this year.
  1909. Currently this does not work for ISO week specifications.
  1910. When this option is nil, the current month and year will always be used
  1911. as defaults."
  1912. :group 'org-time
  1913. :type 'boolean)
  1914. (defcustom org-read-date-display-live t
  1915. "Non-nil means, display current interpretation of date prompt live.
  1916. This display will be in an overlay, in the minibuffer."
  1917. :group 'org-time
  1918. :type 'boolean)
  1919. (defcustom org-read-date-popup-calendar t
  1920. "Non-nil means, pop up a calendar when prompting for a date.
  1921. In the calendar, the date can be selected with mouse-1. However, the
  1922. minibuffer will also be active, and you can simply enter the date as well.
  1923. When nil, only the minibuffer will be available."
  1924. :group 'org-time
  1925. :type 'boolean)
  1926. (if (fboundp 'defvaralias)
  1927. (defvaralias 'org-popup-calendar-for-date-prompt
  1928. 'org-read-date-popup-calendar))
  1929. (defcustom org-read-date-minibuffer-setup-hook nil
  1930. "Hook to be used to set up keys for the date/time interface.
  1931. Add key definitions to `minibuffer-local-map', which will be a temporary
  1932. copy."
  1933. :group 'org-time
  1934. :type 'hook)
  1935. (defcustom org-extend-today-until 0
  1936. "The hour when your day really ends. Must be an integer.
  1937. This has influence for the following applications:
  1938. - When switching the agenda to \"today\". It it is still earlier than
  1939. the time given here, the day recognized as TODAY is actually yesterday.
  1940. - When a date is read from the user and it is still before the time given
  1941. here, the current date and time will be assumed to be yesterday, 23:59.
  1942. Also, timestamps inserted in remember templates follow this rule.
  1943. IMPORTANT: This is a feature whose implementation is and likely will
  1944. remain incomplete. Really, it is only here because past midnight seems to
  1945. be the favorite working time of John Wiegley :-)"
  1946. :group 'org-time
  1947. :type 'integer)
  1948. (defcustom org-edit-timestamp-down-means-later nil
  1949. "Non-nil means, S-down will increase the time in a time stamp.
  1950. When nil, S-up will increase."
  1951. :group 'org-time
  1952. :type 'boolean)
  1953. (defcustom org-calendar-follow-timestamp-change t
  1954. "Non-nil means, make the calendar window follow timestamp changes.
  1955. When a timestamp is modified and the calendar window is visible, it will be
  1956. moved to the new date."
  1957. :group 'org-time
  1958. :type 'boolean)
  1959. (defgroup org-tags nil
  1960. "Options concerning tags in Org-mode."
  1961. :tag "Org Tags"
  1962. :group 'org)
  1963. (defcustom org-tag-alist nil
  1964. "List of tags allowed in Org-mode files.
  1965. When this list is nil, Org-mode will base TAG input on what is already in the
  1966. buffer.
  1967. The value of this variable is an alist, the car of each entry must be a
  1968. keyword as a string, the cdr may be a character that is used to select
  1969. that tag through the fast-tag-selection interface.
  1970. See the manual for details."
  1971. :group 'org-tags
  1972. :type '(repeat
  1973. (choice
  1974. (cons (string :tag "Tag name")
  1975. (character :tag "Access char"))
  1976. (const :tag "Start radio group" (:startgroup))
  1977. (const :tag "End radio group" (:endgroup))
  1978. (const :tag "New line" (:newline)))))
  1979. (defcustom org-tag-persistent-alist nil
  1980. "List of tags that will always appear in all Org-mode files.
  1981. This is in addition to any in buffer settings or customizations
  1982. of `org-tag-alist'.
  1983. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  1984. The value of this variable is an alist, the car of each entry must be a
  1985. keyword as a string, the cdr may be a character that is used to select
  1986. that tag through the fast-tag-selection interface.
  1987. See the manual for details.
  1988. To disable these tags on a per-file basis, insert anywhere in the file:
  1989. #+STARTUP: noptag"
  1990. :group 'org-tags
  1991. :type '(repeat
  1992. (choice
  1993. (cons (string :tag "Tag name")
  1994. (character :tag "Access char"))
  1995. (const :tag "Start radio group" (:startgroup))
  1996. (const :tag "End radio group" (:endgroup))
  1997. (const :tag "New line" (:newline)))))
  1998. (defvar org-file-tags nil
  1999. "List of tags that can be inherited by all entries in the file.
  2000. The tags will be inherited if the variable `org-use-tag-inheritance'
  2001. says they should be.
  2002. This variable is populated from #+TAG lines.")
  2003. (defcustom org-use-fast-tag-selection 'auto
  2004. "Non-nil means, use fast tag selection scheme.
  2005. This is a special interface to select and deselect tags with single keys.
  2006. When nil, fast selection is never used.
  2007. When the symbol `auto', fast selection is used if and only if selection
  2008. characters for tags have been configured, either through the variable
  2009. `org-tag-alist' or through a #+TAGS line in the buffer.
  2010. When t, fast selection is always used and selection keys are assigned
  2011. automatically if necessary."
  2012. :group 'org-tags
  2013. :type '(choice
  2014. (const :tag "Always" t)
  2015. (const :tag "Never" nil)
  2016. (const :tag "When selection characters are configured" 'auto)))
  2017. (defcustom org-fast-tag-selection-single-key nil
  2018. "Non-nil means, fast tag selection exits after first change.
  2019. When nil, you have to press RET to exit it.
  2020. During fast tag selection, you can toggle this flag with `C-c'.
  2021. This variable can also have the value `expert'. In this case, the window
  2022. displaying the tags menu is not even shown, until you press C-c again."
  2023. :group 'org-tags
  2024. :type '(choice
  2025. (const :tag "No" nil)
  2026. (const :tag "Yes" t)
  2027. (const :tag "Expert" expert)))
  2028. (defvar org-fast-tag-selection-include-todo nil
  2029. "Non-nil means, fast tags selection interface will also offer TODO states.
  2030. This is an undocumented feature, you should not rely on it.")
  2031. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2032. "The column to which tags should be indented in a headline.
  2033. If this number is positive, it specifies the column. If it is negative,
  2034. it means that the tags should be flushright to that column. For example,
  2035. -80 works well for a normal 80 character screen."
  2036. :group 'org-tags
  2037. :type 'integer)
  2038. (defcustom org-auto-align-tags t
  2039. "Non-nil means, realign tags after pro/demotion of TODO state change.
  2040. These operations change the length of a headline and therefore shift
  2041. the tags around. With this options turned on, after each such operation
  2042. the tags are again aligned to `org-tags-column'."
  2043. :group 'org-tags
  2044. :type 'boolean)
  2045. (defcustom org-use-tag-inheritance t
  2046. "Non-nil means, tags in levels apply also for sublevels.
  2047. When nil, only the tags directly given in a specific line apply there.
  2048. This may also be a list of tags that should be inherited, or a regexp that
  2049. matches tags that should be inherited. Additional control is possible
  2050. with the variable `org-tags-exclude-from-inheritance' which gives an
  2051. explicit list of tags to be excluded from inheritance., even if the value of
  2052. `org-use-tag-inheritance' would select it for inheritance.
  2053. If this option is t, a match early-on in a tree can lead to a large
  2054. number of matches in the subtree when constructing the agenda or creating
  2055. a sparse tree. If you only want to see the first match in a tree during
  2056. a search, check out the variable `org-tags-match-list-sublevels'."
  2057. :group 'org-tags
  2058. :type '(choice
  2059. (const :tag "Not" nil)
  2060. (const :tag "Always" t)
  2061. (repeat :tag "Specific tags" (string :tag "Tag"))
  2062. (regexp :tag "Tags matched by regexp")))
  2063. (defcustom org-tags-exclude-from-inheritance nil
  2064. "List of tags that should never be inherited.
  2065. This is a way to exclude a few tags from inheritance. For way to do
  2066. the opposite, to actively allow inheritance for selected tags,
  2067. see the variable `org-use-tag-inheritance'."
  2068. :group 'org-tags
  2069. :type '(repeat (string :tag "Tag")))
  2070. (defun org-tag-inherit-p (tag)
  2071. "Check if TAG is one that should be inherited."
  2072. (cond
  2073. ((member tag org-tags-exclude-from-inheritance) nil)
  2074. ((eq org-use-tag-inheritance t) t)
  2075. ((not org-use-tag-inheritance) nil)
  2076. ((stringp org-use-tag-inheritance)
  2077. (string-match org-use-tag-inheritance tag))
  2078. ((listp org-use-tag-inheritance)
  2079. (member tag org-use-tag-inheritance))
  2080. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2081. (defcustom org-tags-match-list-sublevels t
  2082. "Non-nil means list also sublevels of headlines matching a search.
  2083. This variable applies to tags/property searches, and also to stuck
  2084. projects because this search is based on a tags match as well.
  2085. When set to the symbol `indented', sublevels are indented with
  2086. leading dots.
  2087. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2088. the sublevels of a headline matching a tag search often also match
  2089. the same search. Listing all of them can create very long lists.
  2090. Setting this variable to nil causes subtrees of a match to be skipped.
  2091. This variable is semi-obsolete and probably should always be true. It
  2092. is better to limit inheritance to certain tags using the variables
  2093. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2094. :group 'org-tags
  2095. :type '(choice
  2096. (const :tag "No, don't list them" nil)
  2097. (const :tag "Yes, do list them" t)
  2098. (const :tag "List them, indented with leading dots" indented)))
  2099. (defcustom org-tags-sort-function nil
  2100. "When set, tags are sorted using this function as a comparator"
  2101. :group 'org-tags
  2102. :type '(choice
  2103. (const :tag "No sorting" nil)
  2104. (const :tag "Alphabetical" string<)
  2105. (const :tag "Reverse alphabetical" string>)
  2106. (function :tag "Custom function" nil)))
  2107. (defvar org-tags-history nil
  2108. "History of minibuffer reads for tags.")
  2109. (defvar org-last-tags-completion-table nil
  2110. "The last used completion table for tags.")
  2111. (defvar org-after-tags-change-hook nil
  2112. "Hook that is run after the tags in a line have changed.")
  2113. (defgroup org-properties nil
  2114. "Options concerning properties in Org-mode."
  2115. :tag "Org Properties"
  2116. :group 'org)
  2117. (defcustom org-property-format "%-10s %s"
  2118. "How property key/value pairs should be formatted by `indent-line'.
  2119. When `indent-line' hits a property definition, it will format the line
  2120. according to this format, mainly to make sure that the values are
  2121. lined-up with respect to each other."
  2122. :group 'org-properties
  2123. :type 'string)
  2124. (defcustom org-use-property-inheritance nil
  2125. "Non-nil means, properties apply also for sublevels.
  2126. This setting is chiefly used during property searches. Turning it on can
  2127. cause significant overhead when doing a search, which is why it is not
  2128. on by default.
  2129. When nil, only the properties directly given in the current entry count.
  2130. When t, every property is inherited. The value may also be a list of
  2131. properties that should have inheritance, or a regular expression matching
  2132. properties that should be inherited.
  2133. However, note that some special properties use inheritance under special
  2134. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2135. and the properties ending in \"_ALL\" when they are used as descriptor
  2136. for valid values of a property.
  2137. Note for programmers:
  2138. When querying an entry with `org-entry-get', you can control if inheritance
  2139. should be used. By default, `org-entry-get' looks only at the local
  2140. properties. You can request inheritance by setting the inherit argument
  2141. to t (to force inheritance) or to `selective' (to respect the setting
  2142. in this variable)."
  2143. :group 'org-properties
  2144. :type '(choice
  2145. (const :tag "Not" nil)
  2146. (const :tag "Always" t)
  2147. (repeat :tag "Specific properties" (string :tag "Property"))
  2148. (regexp :tag "Properties matched by regexp")))
  2149. (defun org-property-inherit-p (property)
  2150. "Check if PROPERTY is one that should be inherited."
  2151. (cond
  2152. ((eq org-use-property-inheritance t) t)
  2153. ((not org-use-property-inheritance) nil)
  2154. ((stringp org-use-property-inheritance)
  2155. (string-match org-use-property-inheritance property))
  2156. ((listp org-use-property-inheritance)
  2157. (member property org-use-property-inheritance))
  2158. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2159. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2160. "The default column format, if no other format has been defined.
  2161. This variable can be set on the per-file basis by inserting a line
  2162. #+COLUMNS: %25ITEM ....."
  2163. :group 'org-properties
  2164. :type 'string)
  2165. (defcustom org-columns-ellipses ".."
  2166. "The ellipses to be used when a field in column view is truncated.
  2167. When this is the empty string, as many characters as possible are shown,
  2168. but then there will be no visual indication that the field has been truncated.
  2169. When this is a string of length N, the last N characters of a truncated
  2170. field are replaced by this string. If the column is narrower than the
  2171. ellipses string, only part of the ellipses string will be shown."
  2172. :group 'org-properties
  2173. :type 'string)
  2174. (defcustom org-columns-modify-value-for-display-function nil
  2175. "Function that modifies values for display in column view.
  2176. For example, it can be used to cut out a certain part from a time stamp.
  2177. The function must take 2 arguments:
  2178. column-title The title of the column (*not* the property name)
  2179. value The value that should be modified.
  2180. The function should return the value that should be displayed,
  2181. or nil if the normal value should be used."
  2182. :group 'org-properties
  2183. :type 'function)
  2184. (defcustom org-effort-property "Effort"
  2185. "The property that is being used to keep track of effort estimates.
  2186. Effort estimates given in this property need to have the format H:MM."
  2187. :group 'org-properties
  2188. :group 'org-progress
  2189. :type '(string :tag "Property"))
  2190. (defconst org-global-properties-fixed
  2191. '(("VISIBILITY_ALL" . "folded children content all")
  2192. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2193. "List of property/value pairs that can be inherited by any entry.
  2194. These are fixed values, for the preset properties. The user variable
  2195. that can be used to add to this list is `org-global-properties'.
  2196. The entries in this list are cons cells where the car is a property
  2197. name and cdr is a string with the value. If the value represents
  2198. multiple items like an \"_ALL\" property, separate the items by
  2199. spaces.")
  2200. (defcustom org-global-properties nil
  2201. "List of property/value pairs that can be inherited by any entry.
  2202. This list will be combined with the constant `org-global-properties-fixed'.
  2203. The entries in this list are cons cells where the car is a property
  2204. name and cdr is a string with the value.
  2205. You can set buffer-local values for the same purpose in the variable
  2206. `org-file-properties' this by adding lines like
  2207. #+PROPERTY: NAME VALUE"
  2208. :group 'org-properties
  2209. :type '(repeat
  2210. (cons (string :tag "Property")
  2211. (string :tag "Value"))))
  2212. (defvar org-file-properties nil
  2213. "List of property/value pairs that can be inherited by any entry.
  2214. Valid for the current buffer.
  2215. This variable is populated from #+PROPERTY lines.")
  2216. (make-variable-buffer-local 'org-file-properties)
  2217. (defgroup org-agenda nil
  2218. "Options concerning agenda views in Org-mode."
  2219. :tag "Org Agenda"
  2220. :group 'org)
  2221. (defvar org-category nil
  2222. "Variable used by org files to set a category for agenda display.
  2223. Such files should use a file variable to set it, for example
  2224. # -*- mode: org; org-category: \"ELisp\"
  2225. or contain a special line
  2226. #+CATEGORY: ELisp
  2227. If the file does not specify a category, then file's base name
  2228. is used instead.")
  2229. (make-variable-buffer-local 'org-category)
  2230. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2231. (defcustom org-agenda-files nil
  2232. "The files to be used for agenda display.
  2233. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2234. \\[org-remove-file]. You can also use customize to edit the list.
  2235. If an entry is a directory, all files in that directory that are matched by
  2236. `org-agenda-file-regexp' will be part of the file list.
  2237. If the value of the variable is not a list but a single file name, then
  2238. the list of agenda files is actually stored and maintained in that file, one
  2239. agenda file per line."
  2240. :group 'org-agenda
  2241. :type '(choice
  2242. (repeat :tag "List of files and directories" file)
  2243. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2244. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2245. "Regular expression to match files for `org-agenda-files'.
  2246. If any element in the list in that variable contains a directory instead
  2247. of a normal file, all files in that directory that are matched by this
  2248. regular expression will be included."
  2249. :group 'org-agenda
  2250. :type 'regexp)
  2251. (defcustom org-agenda-text-search-extra-files nil
  2252. "List of extra files to be searched by text search commands.
  2253. These files will be search in addition to the agenda files by the
  2254. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2255. Note that these files will only be searched for text search commands,
  2256. not for the other agenda views like todo lists, tag searches or the weekly
  2257. agenda. This variable is intended to list notes and possibly archive files
  2258. that should also be searched by these two commands.
  2259. In fact, if the first element in the list is the symbol `agenda-archives',
  2260. than all archive files of all agenda files will be added to the search
  2261. scope."
  2262. :group 'org-agenda
  2263. :type '(set :greedy t
  2264. (const :tag "Agenda Archives" agenda-archives)
  2265. (repeat :inline t (file))))
  2266. (if (fboundp 'defvaralias)
  2267. (defvaralias 'org-agenda-multi-occur-extra-files
  2268. 'org-agenda-text-search-extra-files))
  2269. (defcustom org-agenda-skip-unavailable-files nil
  2270. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2271. A nil value means to remove them, after a query, from the list."
  2272. :group 'org-agenda
  2273. :type 'boolean)
  2274. (defcustom org-calendar-to-agenda-key [?c]
  2275. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2276. The command `org-calendar-goto-agenda' will be bound to this key. The
  2277. default is the character `c' because then `c' can be used to switch back and
  2278. forth between agenda and calendar."
  2279. :group 'org-agenda
  2280. :type 'sexp)
  2281. (defcustom org-calendar-agenda-action-key [?k]
  2282. "The key to be installed in `calendar-mode-map' for agenda-action.
  2283. The command `org-agenda-action' will be bound to this key. The
  2284. default is the character `k' because we use the same key in the agenda."
  2285. :group 'org-agenda
  2286. :type 'sexp)
  2287. (eval-after-load "calendar"
  2288. '(progn
  2289. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2290. 'org-calendar-goto-agenda)
  2291. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2292. 'org-agenda-action)))
  2293. (defgroup org-latex nil
  2294. "Options for embedding LaTeX code into Org-mode."
  2295. :tag "Org LaTeX"
  2296. :group 'org)
  2297. (defcustom org-format-latex-options
  2298. '(:foreground default :background default :scale 1.0
  2299. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2300. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2301. "Options for creating images from LaTeX fragments.
  2302. This is a property list with the following properties:
  2303. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2304. `default' means use the foreground of the default face.
  2305. :background the background color, or \"Transparent\".
  2306. `default' means use the background of the default face.
  2307. :scale a scaling factor for the size of the images.
  2308. :html-foreground, :html-background, :html-scale
  2309. the same numbers for HTML export.
  2310. :matchers a list indicating which matchers should be used to
  2311. find LaTeX fragments. Valid members of this list are:
  2312. \"begin\" find environments
  2313. \"$1\" find single characters surrounded by $.$
  2314. \"$\" find math expressions surrounded by $...$
  2315. \"$$\" find math expressions surrounded by $$....$$
  2316. \"\\(\" find math expressions surrounded by \\(...\\)
  2317. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2318. :group 'org-latex
  2319. :type 'plist)
  2320. (defcustom org-format-latex-header "\\documentclass{article}
  2321. \\usepackage{fullpage} % do not remove
  2322. \\usepackage{amssymb}
  2323. \\usepackage[usenames]{color}
  2324. \\usepackage{amsmath}
  2325. \\usepackage{latexsym}
  2326. \\usepackage[mathscr]{eucal}
  2327. \\pagestyle{empty} % do not remove"
  2328. "The document header used for processing LaTeX fragments."
  2329. :group 'org-latex
  2330. :type 'string)
  2331. (defgroup org-font-lock nil
  2332. "Font-lock settings for highlighting in Org-mode."
  2333. :tag "Org Font Lock"
  2334. :group 'org)
  2335. (defcustom org-level-color-stars-only nil
  2336. "Non-nil means fontify only the stars in each headline.
  2337. When nil, the entire headline is fontified.
  2338. Changing it requires restart of `font-lock-mode' to become effective
  2339. also in regions already fontified."
  2340. :group 'org-font-lock
  2341. :type 'boolean)
  2342. (defcustom org-hide-leading-stars nil
  2343. "Non-nil means, hide the first N-1 stars in a headline.
  2344. This works by using the face `org-hide' for these stars. This
  2345. face is white for a light background, and black for a dark
  2346. background. You may have to customize the face `org-hide' to
  2347. make this work.
  2348. Changing it requires restart of `font-lock-mode' to become effective
  2349. also in regions already fontified.
  2350. You may also set this on a per-file basis by adding one of the following
  2351. lines to the buffer:
  2352. #+STARTUP: hidestars
  2353. #+STARTUP: showstars"
  2354. :group 'org-font-lock
  2355. :type 'boolean)
  2356. (defcustom org-fontify-done-headline nil
  2357. "Non-nil means, change the face of a headline if it is marked DONE.
  2358. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2359. When this is non-nil, the headline after the keyword is set to the
  2360. `org-headline-done' as an additional indication."
  2361. :group 'org-font-lock
  2362. :type 'boolean)
  2363. (defcustom org-fontify-emphasized-text t
  2364. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2365. Changing this variable requires a restart of Emacs to take effect."
  2366. :group 'org-font-lock
  2367. :type 'boolean)
  2368. (defcustom org-fontify-whole-heading-line nil
  2369. "Non-nil means fontify the whole line for headings.
  2370. This is useful when setting a background color for the
  2371. org-leve-* faces."
  2372. :group 'org-font-lock
  2373. :type 'boolean)
  2374. (defcustom org-highlight-latex-fragments-and-specials nil
  2375. "Non-nil means, fontify what is treated specially by the exporters."
  2376. :group 'org-font-lock
  2377. :type 'boolean)
  2378. (defcustom org-hide-emphasis-markers nil
  2379. "Non-nil mean font-lock should hide the emphasis marker characters."
  2380. :group 'org-font-lock
  2381. :type 'boolean)
  2382. (defvar org-emph-re nil
  2383. "Regular expression for matching emphasis.")
  2384. (defvar org-verbatim-re nil
  2385. "Regular expression for matching verbatim text.")
  2386. (defvar org-emphasis-regexp-components) ; defined just below
  2387. (defvar org-emphasis-alist) ; defined just below
  2388. (defun org-set-emph-re (var val)
  2389. "Set variable and compute the emphasis regular expression."
  2390. (set var val)
  2391. (when (and (boundp 'org-emphasis-alist)
  2392. (boundp 'org-emphasis-regexp-components)
  2393. org-emphasis-alist org-emphasis-regexp-components)
  2394. (let* ((e org-emphasis-regexp-components)
  2395. (pre (car e))
  2396. (post (nth 1 e))
  2397. (border (nth 2 e))
  2398. (body (nth 3 e))
  2399. (nl (nth 4 e))
  2400. (body1 (concat body "*?"))
  2401. (markers (mapconcat 'car org-emphasis-alist ""))
  2402. (vmarkers (mapconcat
  2403. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2404. org-emphasis-alist "")))
  2405. ;; make sure special characters appear at the right position in the class
  2406. (if (string-match "\\^" markers)
  2407. (setq markers (concat (replace-match "" t t markers) "^")))
  2408. (if (string-match "-" markers)
  2409. (setq markers (concat (replace-match "" t t markers) "-")))
  2410. (if (string-match "\\^" vmarkers)
  2411. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2412. (if (string-match "-" vmarkers)
  2413. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2414. (if (> nl 0)
  2415. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2416. (int-to-string nl) "\\}")))
  2417. ;; Make the regexp
  2418. (setq org-emph-re
  2419. (concat "\\([" pre "]\\|^\\)"
  2420. "\\("
  2421. "\\([" markers "]\\)"
  2422. "\\("
  2423. "[^" border "]\\|"
  2424. "[^" border "]"
  2425. body1
  2426. "[^" border "]"
  2427. "\\)"
  2428. "\\3\\)"
  2429. "\\([" post "]\\|$\\)"))
  2430. (setq org-verbatim-re
  2431. (concat "\\([" pre "]\\|^\\)"
  2432. "\\("
  2433. "\\([" vmarkers "]\\)"
  2434. "\\("
  2435. "[^" border "]\\|"
  2436. "[^" border "]"
  2437. body1
  2438. "[^" border "]"
  2439. "\\)"
  2440. "\\3\\)"
  2441. "\\([" post "]\\|$\\)")))))
  2442. (defcustom org-emphasis-regexp-components
  2443. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2444. "Components used to build the regular expression for emphasis.
  2445. This is a list with 6 entries. Terminology: In an emphasis string
  2446. like \" *strong word* \", we call the initial space PREMATCH, the final
  2447. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2448. and \"trong wor\" is the body. The different components in this variable
  2449. specify what is allowed/forbidden in each part:
  2450. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2451. post Chars allowed as postmatch. End of line will be allowed too.
  2452. border The chars *forbidden* as border characters.
  2453. body-regexp A regexp like \".\" to match a body character. Don't use
  2454. non-shy groups here, and don't allow newline here.
  2455. newline The maximum number of newlines allowed in an emphasis exp.
  2456. Use customize to modify this, or restart Emacs after changing it."
  2457. :group 'org-font-lock
  2458. :set 'org-set-emph-re
  2459. :type '(list
  2460. (sexp :tag "Allowed chars in pre ")
  2461. (sexp :tag "Allowed chars in post ")
  2462. (sexp :tag "Forbidden chars in border ")
  2463. (sexp :tag "Regexp for body ")
  2464. (integer :tag "number of newlines allowed")
  2465. (option (boolean :tag "Please ignore this button"))))
  2466. (defcustom org-emphasis-alist
  2467. `(("*" bold "<b>" "</b>")
  2468. ("/" italic "<i>" "</i>")
  2469. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2470. ("=" org-code "<code>" "</code>" verbatim)
  2471. ("~" org-verbatim "<code>" "</code>" verbatim)
  2472. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2473. "<del>" "</del>")
  2474. )
  2475. "Special syntax for emphasized text.
  2476. Text starting and ending with a special character will be emphasized, for
  2477. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2478. characters, the face to be used by font-lock for highlighting in Org-mode
  2479. Emacs buffers, and the HTML tags to be used for this.
  2480. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2481. Use customize to modify this, or restart Emacs after changing it."
  2482. :group 'org-font-lock
  2483. :set 'org-set-emph-re
  2484. :type '(repeat
  2485. (list
  2486. (string :tag "Marker character")
  2487. (choice
  2488. (face :tag "Font-lock-face")
  2489. (plist :tag "Face property list"))
  2490. (string :tag "HTML start tag")
  2491. (string :tag "HTML end tag")
  2492. (option (const verbatim)))))
  2493. (defvar org-protecting-blocks
  2494. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2495. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2496. This is needed for font-lock setup.")
  2497. ;;; Miscellaneous options
  2498. (defgroup org-completion nil
  2499. "Completion in Org-mode."
  2500. :tag "Org Completion"
  2501. :group 'org)
  2502. (defcustom org-completion-use-ido nil
  2503. "Non-nil means, use ido completion wherever possible.
  2504. Note that `ido-mode' must be active for this variable to be relevant.
  2505. If you decide to turn this variable on, you might well want to turn off
  2506. `org-outline-path-complete-in-steps'.
  2507. See also `org-completion-use-iswitchb'."
  2508. :group 'org-completion
  2509. :type 'boolean)
  2510. (defcustom org-completion-use-iswitchb nil
  2511. "Non-nil means, use iswitchb completion wherever possible.
  2512. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2513. If you decide to turn this variable on, you might well want to turn off
  2514. `org-outline-path-complete-in-steps'.
  2515. Note that thi variable has only an effect if `org-completion-use-ido' is nil."
  2516. :group 'org-completion
  2517. :type 'boolean)
  2518. (defcustom org-completion-fallback-command 'hippie-expand
  2519. "The expansion command called by \\[org-complete] in normal context.
  2520. Normal means, no org-mode-specific context."
  2521. :group 'org-completion
  2522. :type 'function)
  2523. ;;; Functions and variables from ther packages
  2524. ;; Declared here to avoid compiler warnings
  2525. ;; XEmacs only
  2526. (defvar outline-mode-menu-heading)
  2527. (defvar outline-mode-menu-show)
  2528. (defvar outline-mode-menu-hide)
  2529. (defvar zmacs-regions) ; XEmacs regions
  2530. ;; Emacs only
  2531. (defvar mark-active)
  2532. ;; Various packages
  2533. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2534. (declare-function calendar-forward-day "cal-move" (arg))
  2535. (declare-function calendar-goto-date "cal-move" (date))
  2536. (declare-function calendar-goto-today "cal-move" ())
  2537. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2538. (defvar calc-embedded-close-formula)
  2539. (defvar calc-embedded-open-formula)
  2540. (declare-function cdlatex-tab "ext:cdlatex" ())
  2541. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2542. (defvar font-lock-unfontify-region-function)
  2543. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2544. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2545. (defvar iswitchb-temp-buflist)
  2546. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2547. (defvar org-agenda-tags-todo-honor-ignore-options)
  2548. (declare-function org-agenda-skip "org-agenda" ())
  2549. (declare-function org-format-agenda-item "org-agenda"
  2550. (extra txt &optional category tags dotime noprefix remove-re))
  2551. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2552. (declare-function org-agenda-change-all-lines "org-agenda"
  2553. (newhead hdmarker &optional fixface just-this))
  2554. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2555. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2556. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2557. (beg end))
  2558. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2559. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2560. "org-agenda" (&optional end))
  2561. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2562. (declare-function org-indent-mode "org-indent" (arg))
  2563. (declare-function parse-time-string "parse-time" (string))
  2564. (declare-function remember "remember" (&optional initial))
  2565. (declare-function remember-buffer-desc "remember" ())
  2566. (declare-function remember-finalize "remember" ())
  2567. (defvar remember-save-after-remembering)
  2568. (defvar remember-data-file)
  2569. (defvar remember-register)
  2570. (defvar remember-buffer)
  2571. (defvar remember-handler-functions)
  2572. (defvar remember-annotation-functions)
  2573. (defvar texmathp-why)
  2574. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2575. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2576. (defvar w3m-current-url)
  2577. (defvar w3m-current-title)
  2578. (defvar org-latex-regexps)
  2579. ;;; Autoload and prepare some org modules
  2580. ;; Some table stuff that needs to be defined here, because it is used
  2581. ;; by the functions setting up org-mode or checking for table context.
  2582. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2583. "Detects an org-type or table-type table.")
  2584. (defconst org-table-line-regexp "^[ \t]*|"
  2585. "Detects an org-type table line.")
  2586. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2587. "Detects an org-type table line.")
  2588. (defconst org-table-hline-regexp "^[ \t]*|-"
  2589. "Detects an org-type table hline.")
  2590. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2591. "Detects a table-type table hline.")
  2592. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2593. "Searching from within a table (any type) this finds the first line
  2594. outside the table.")
  2595. ;; Autoload the functions in org-table.el that are needed by functions here.
  2596. (eval-and-compile
  2597. (org-autoload "org-table"
  2598. '(org-table-align org-table-begin org-table-blank-field
  2599. org-table-convert org-table-convert-region org-table-copy-down
  2600. org-table-copy-region org-table-create
  2601. org-table-create-or-convert-from-region
  2602. org-table-create-with-table.el org-table-current-dline
  2603. org-table-cut-region org-table-delete-column org-table-edit-field
  2604. org-table-edit-formulas org-table-end org-table-eval-formula
  2605. org-table-export org-table-field-info
  2606. org-table-get-stored-formulas org-table-goto-column
  2607. org-table-hline-and-move org-table-import org-table-insert-column
  2608. org-table-insert-hline org-table-insert-row org-table-iterate
  2609. org-table-justify-field-maybe org-table-kill-row
  2610. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2611. org-table-move-column org-table-move-column-left
  2612. org-table-move-column-right org-table-move-row
  2613. org-table-move-row-down org-table-move-row-up
  2614. org-table-next-field org-table-next-row org-table-paste-rectangle
  2615. org-table-previous-field org-table-recalculate
  2616. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2617. org-table-toggle-coordinate-overlays
  2618. org-table-toggle-formula-debugger org-table-wrap-region
  2619. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2620. (defun org-at-table-p (&optional table-type)
  2621. "Return t if the cursor is inside an org-type table.
  2622. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2623. (if org-enable-table-editor
  2624. (save-excursion
  2625. (beginning-of-line 1)
  2626. (looking-at (if table-type org-table-any-line-regexp
  2627. org-table-line-regexp)))
  2628. nil))
  2629. (defsubst org-table-p () (org-at-table-p))
  2630. (defun org-at-table.el-p ()
  2631. "Return t if and only if we are at a table.el table."
  2632. (and (org-at-table-p 'any)
  2633. (save-excursion
  2634. (goto-char (org-table-begin 'any))
  2635. (looking-at org-table1-hline-regexp))))
  2636. (defun org-table-recognize-table.el ()
  2637. "If there is a table.el table nearby, recognize it and move into it."
  2638. (if org-table-tab-recognizes-table.el
  2639. (if (org-at-table.el-p)
  2640. (progn
  2641. (beginning-of-line 1)
  2642. (if (looking-at org-table-dataline-regexp)
  2643. nil
  2644. (if (looking-at org-table1-hline-regexp)
  2645. (progn
  2646. (beginning-of-line 2)
  2647. (if (looking-at org-table-any-border-regexp)
  2648. (beginning-of-line -1)))))
  2649. (if (re-search-forward "|" (org-table-end t) t)
  2650. (progn
  2651. (require 'table)
  2652. (if (table--at-cell-p (point))
  2653. t
  2654. (message "recognizing table.el table...")
  2655. (table-recognize-table)
  2656. (message "recognizing table.el table...done")))
  2657. (error "This should not happen..."))
  2658. t)
  2659. nil)
  2660. nil))
  2661. (defun org-at-table-hline-p ()
  2662. "Return t if the cursor is inside a hline in a table."
  2663. (if org-enable-table-editor
  2664. (save-excursion
  2665. (beginning-of-line 1)
  2666. (looking-at org-table-hline-regexp))
  2667. nil))
  2668. (defvar org-table-clean-did-remove-column nil)
  2669. (defun org-table-map-tables (function)
  2670. "Apply FUNCTION to the start of all tables in the buffer."
  2671. (save-excursion
  2672. (save-restriction
  2673. (widen)
  2674. (goto-char (point-min))
  2675. (while (re-search-forward org-table-any-line-regexp nil t)
  2676. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2677. (beginning-of-line 1)
  2678. (when (looking-at org-table-line-regexp)
  2679. (save-excursion (funcall function))
  2680. (or (looking-at org-table-line-regexp)
  2681. (forward-char 1)))
  2682. (re-search-forward org-table-any-border-regexp nil 1))))
  2683. (message "Mapping tables: done"))
  2684. ;; Declare and autoload functions from org-exp.el & Co
  2685. (declare-function org-default-export-plist "org-exp")
  2686. (declare-function org-infile-export-plist "org-exp")
  2687. (declare-function org-get-current-options "org-exp")
  2688. (eval-and-compile
  2689. (org-autoload "org-exp"
  2690. '(org-export org-export-visible
  2691. org-insert-export-options-template
  2692. org-table-clean-before-export))
  2693. (org-autoload "org-ascii"
  2694. '(org-export-as-ascii org-export-ascii-preprocess
  2695. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2696. org-export-region-as-ascii))
  2697. (org-autoload "org-html"
  2698. '(org-export-as-html-and-open
  2699. org-export-as-html-batch org-export-as-html-to-buffer
  2700. org-replace-region-by-html org-export-region-as-html
  2701. org-export-as-html))
  2702. (org-autoload "org-icalendar"
  2703. '(org-export-icalendar-this-file
  2704. org-export-icalendar-all-agenda-files
  2705. org-export-icalendar-combine-agenda-files))
  2706. (org-autoload "org-xoxo" '(org-export-as-xoxo)))
  2707. ;; Declare and autoload functions from org-agenda.el
  2708. (eval-and-compile
  2709. (org-autoload "org-agenda"
  2710. '(org-agenda org-agenda-list org-search-view
  2711. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2712. org-diary org-agenda-to-appt
  2713. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2714. ;; Autoload org-remember
  2715. (eval-and-compile
  2716. (org-autoload "org-remember"
  2717. '(org-remember-insinuate org-remember-annotation
  2718. org-remember-apply-template org-remember org-remember-handler)))
  2719. ;; Autoload org-clock.el
  2720. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2721. (beg end))
  2722. (declare-function org-clock-update-mode-line "org-clock" ())
  2723. (defvar org-clock-start-time)
  2724. (defvar org-clock-marker (make-marker)
  2725. "Marker recording the last clock-in.")
  2726. (defvar org-clock-hd-marker (make-marker)
  2727. "Marker recording the last clock-in, but the headline position.")
  2728. (defun org-clock-is-active ()
  2729. "Return non-nil if clock is currently running.
  2730. The return value is actually the clock marker."
  2731. (marker-buffer org-clock-marker))
  2732. (eval-and-compile
  2733. (org-autoload
  2734. "org-clock"
  2735. '(org-clock-in org-clock-out org-clock-cancel
  2736. org-clock-goto org-clock-sum org-clock-display
  2737. org-clock-remove-overlays org-clock-report
  2738. org-clocktable-shift org-dblock-write:clocktable
  2739. org-get-clocktable)))
  2740. (defun org-clock-update-time-maybe ()
  2741. "If this is a CLOCK line, update it and return t.
  2742. Otherwise, return nil."
  2743. (interactive)
  2744. (save-excursion
  2745. (beginning-of-line 1)
  2746. (skip-chars-forward " \t")
  2747. (when (looking-at org-clock-string)
  2748. (let ((re (concat "[ \t]*" org-clock-string
  2749. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2750. "\\([ \t]*=>.*\\)?\\)?"))
  2751. ts te h m s neg)
  2752. (cond
  2753. ((not (looking-at re))
  2754. nil)
  2755. ((not (match-end 2))
  2756. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2757. (> org-clock-marker (point))
  2758. (<= org-clock-marker (point-at-eol)))
  2759. ;; The clock is running here
  2760. (setq org-clock-start-time
  2761. (apply 'encode-time
  2762. (org-parse-time-string (match-string 1))))
  2763. (org-clock-update-mode-line)))
  2764. (t
  2765. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2766. (end-of-line 1)
  2767. (setq ts (match-string 1)
  2768. te (match-string 3))
  2769. (setq s (- (org-float-time
  2770. (apply 'encode-time (org-parse-time-string te)))
  2771. (org-float-time
  2772. (apply 'encode-time (org-parse-time-string ts))))
  2773. neg (< s 0)
  2774. s (abs s)
  2775. h (floor (/ s 3600))
  2776. s (- s (* 3600 h))
  2777. m (floor (/ s 60))
  2778. s (- s (* 60 s)))
  2779. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2780. t))))))
  2781. (defun org-check-running-clock ()
  2782. "Check if the current buffer contains the running clock.
  2783. If yes, offer to stop it and to save the buffer with the changes."
  2784. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2785. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2786. (buffer-name))))
  2787. (org-clock-out)
  2788. (when (y-or-n-p "Save changed buffer?")
  2789. (save-buffer))))
  2790. (defun org-clocktable-try-shift (dir n)
  2791. "Check if this line starts a clock table, if yes, shift the time block."
  2792. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2793. (org-clocktable-shift dir n)))
  2794. ;; Autoload org-timer.el
  2795. (eval-and-compile
  2796. (org-autoload
  2797. "org-timer"
  2798. '(org-timer-start org-timer org-timer-item
  2799. org-timer-change-times-in-region
  2800. org-timer-set-timer
  2801. org-timer-reset-timers
  2802. org-timer-show-remaining-time)))
  2803. ;; Autoload org-feed.el
  2804. (eval-and-compile
  2805. (org-autoload
  2806. "org-feed"
  2807. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  2808. ;; Autoload org-indent.el
  2809. (eval-and-compile
  2810. (org-autoload
  2811. "org-indent"
  2812. '(org-indent-mode)))
  2813. ;; Autoload archiving code
  2814. ;; The stuff that is needed for cycling and tags has to be defined here.
  2815. (defgroup org-archive nil
  2816. "Options concerning archiving in Org-mode."
  2817. :tag "Org Archive"
  2818. :group 'org-structure)
  2819. (defcustom org-archive-location "%s_archive::"
  2820. "The location where subtrees should be archived.
  2821. The value of this variable is a string, consisting of two parts,
  2822. separated by a double-colon. The first part is a filename and
  2823. the second part is a headline.
  2824. When the filename is omitted, archiving happens in the same file.
  2825. %s in the filename will be replaced by the current file
  2826. name (without the directory part). Archiving to a different file
  2827. is useful to keep archived entries from contributing to the
  2828. Org-mode Agenda.
  2829. The archived entries will be filed as subtrees of the specified
  2830. headline. When the headline is omitted, the subtrees are simply
  2831. filed away at the end of the file, as top-level entries. Also in
  2832. the heading you can use %s to represent the file name, this can be
  2833. useful when using the same archive for a number of different files.
  2834. Here are a few examples:
  2835. \"%s_archive::\"
  2836. If the current file is Projects.org, archive in file
  2837. Projects.org_archive, as top-level trees. This is the default.
  2838. \"::* Archived Tasks\"
  2839. Archive in the current file, under the top-level headline
  2840. \"* Archived Tasks\".
  2841. \"~/org/archive.org::\"
  2842. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2843. \"~/org/archive.org::From %s\"
  2844. Archive in file ~/org/archive.org (absolute path), und headlines
  2845. \"From FILENAME\" where file name is the current file name.
  2846. \"basement::** Finished Tasks\"
  2847. Archive in file ./basement (relative path), as level 3 trees
  2848. below the level 2 heading \"** Finished Tasks\".
  2849. You may set this option on a per-file basis by adding to the buffer a
  2850. line like
  2851. #+ARCHIVE: basement::** Finished Tasks
  2852. You may also define it locally for a subtree by setting an ARCHIVE property
  2853. in the entry. If such a property is found in an entry, or anywhere up
  2854. the hierarchy, it will be used."
  2855. :group 'org-archive
  2856. :type 'string)
  2857. (defcustom org-archive-tag "ARCHIVE"
  2858. "The tag that marks a subtree as archived.
  2859. An archived subtree does not open during visibility cycling, and does
  2860. not contribute to the agenda listings.
  2861. After changing this, font-lock must be restarted in the relevant buffers to
  2862. get the proper fontification."
  2863. :group 'org-archive
  2864. :group 'org-keywords
  2865. :type 'string)
  2866. (defcustom org-agenda-skip-archived-trees t
  2867. "Non-nil means, the agenda will skip any items located in archived trees.
  2868. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2869. variable is no longer recommended, you should leave it at the value t.
  2870. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2871. :group 'org-archive
  2872. :group 'org-agenda-skip
  2873. :type 'boolean)
  2874. (defcustom org-columns-skip-arrchived-trees t
  2875. "Non-nil means, irgnore archived trees when creating column view."
  2876. :group 'org-archive
  2877. :group 'org-properties
  2878. :type 'boolean)
  2879. (defcustom org-cycle-open-archived-trees nil
  2880. "Non-nil means, `org-cycle' will open archived trees.
  2881. An archived tree is a tree marked with the tag ARCHIVE.
  2882. When nil, archived trees will stay folded. You can still open them with
  2883. normal outline commands like `show-all', but not with the cycling commands."
  2884. :group 'org-archive
  2885. :group 'org-cycle
  2886. :type 'boolean)
  2887. (defcustom org-sparse-tree-open-archived-trees nil
  2888. "Non-nil means sparse tree construction shows matches in archived trees.
  2889. When nil, matches in these trees are highlighted, but the trees are kept in
  2890. collapsed state."
  2891. :group 'org-archive
  2892. :group 'org-sparse-trees
  2893. :type 'boolean)
  2894. (defun org-cycle-hide-archived-subtrees (state)
  2895. "Re-hide all archived subtrees after a visibility state change."
  2896. (when (and (not org-cycle-open-archived-trees)
  2897. (not (memq state '(overview folded))))
  2898. (save-excursion
  2899. (let* ((globalp (memq state '(contents all)))
  2900. (beg (if globalp (point-min) (point)))
  2901. (end (if globalp (point-max) (org-end-of-subtree t))))
  2902. (org-hide-archived-subtrees beg end)
  2903. (goto-char beg)
  2904. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2905. (message "%s" (substitute-command-keys
  2906. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2907. (defun org-force-cycle-archived ()
  2908. "Cycle subtree even if it is archived."
  2909. (interactive)
  2910. (setq this-command 'org-cycle)
  2911. (let ((org-cycle-open-archived-trees t))
  2912. (call-interactively 'org-cycle)))
  2913. (defun org-hide-archived-subtrees (beg end)
  2914. "Re-hide all archived subtrees after a visibility state change."
  2915. (save-excursion
  2916. (let* ((re (concat ":" org-archive-tag ":")))
  2917. (goto-char beg)
  2918. (while (re-search-forward re end t)
  2919. (and (org-on-heading-p) (hide-subtree))
  2920. (org-end-of-subtree t)))))
  2921. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2922. (eval-and-compile
  2923. (org-autoload "org-archive"
  2924. '(org-add-archive-files org-archive-subtree
  2925. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2926. ;; Autoload Column View Code
  2927. (declare-function org-columns-number-to-string "org-colview")
  2928. (declare-function org-columns-get-format-and-top-level "org-colview")
  2929. (declare-function org-columns-compute "org-colview")
  2930. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2931. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2932. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2933. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2934. ;; Autoload ID code
  2935. (declare-function org-id-store-link "org-id")
  2936. (declare-function org-id-locations-load "org-id")
  2937. (declare-function org-id-locations-save "org-id")
  2938. (defvar org-id-track-globally)
  2939. (org-autoload "org-id"
  2940. '(org-id-get-create org-id-new org-id-copy org-id-get
  2941. org-id-get-with-outline-path-completion
  2942. org-id-get-with-outline-drilling
  2943. org-id-goto org-id-find org-id-store-link))
  2944. ;; Autoload Plotting Code
  2945. (org-autoload "org-plot"
  2946. '(org-plot/gnuplot))
  2947. ;;; Variables for pre-computed regular expressions, all buffer local
  2948. (defvar org-drawer-regexp nil
  2949. "Matches first line of a hidden block.")
  2950. (make-variable-buffer-local 'org-drawer-regexp)
  2951. (defvar org-todo-regexp nil
  2952. "Matches any of the TODO state keywords.")
  2953. (make-variable-buffer-local 'org-todo-regexp)
  2954. (defvar org-not-done-regexp nil
  2955. "Matches any of the TODO state keywords except the last one.")
  2956. (make-variable-buffer-local 'org-not-done-regexp)
  2957. (defvar org-not-done-heading-regexp nil
  2958. "Matches a TODO headline that is not done.")
  2959. (make-variable-buffer-local 'org-not-done-regexp)
  2960. (defvar org-todo-line-regexp nil
  2961. "Matches a headline and puts TODO state into group 2 if present.")
  2962. (make-variable-buffer-local 'org-todo-line-regexp)
  2963. (defvar org-complex-heading-regexp nil
  2964. "Matches a headline and puts everything into groups:
  2965. group 1: the stars
  2966. group 2: The todo keyword, maybe
  2967. group 3: Priority cookie
  2968. group 4: True headline
  2969. group 5: Tags")
  2970. (make-variable-buffer-local 'org-complex-heading-regexp)
  2971. (defvar org-todo-line-tags-regexp nil
  2972. "Matches a headline and puts TODO state into group 2 if present.
  2973. Also put tags into group 4 if tags are present.")
  2974. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2975. (defvar org-nl-done-regexp nil
  2976. "Matches newline followed by a headline with the DONE keyword.")
  2977. (make-variable-buffer-local 'org-nl-done-regexp)
  2978. (defvar org-looking-at-done-regexp nil
  2979. "Matches the DONE keyword a point.")
  2980. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2981. (defvar org-ds-keyword-length 12
  2982. "Maximum length of the Deadline and SCHEDULED keywords.")
  2983. (make-variable-buffer-local 'org-ds-keyword-length)
  2984. (defvar org-deadline-regexp nil
  2985. "Matches the DEADLINE keyword.")
  2986. (make-variable-buffer-local 'org-deadline-regexp)
  2987. (defvar org-deadline-time-regexp nil
  2988. "Matches the DEADLINE keyword together with a time stamp.")
  2989. (make-variable-buffer-local 'org-deadline-time-regexp)
  2990. (defvar org-deadline-line-regexp nil
  2991. "Matches the DEADLINE keyword and the rest of the line.")
  2992. (make-variable-buffer-local 'org-deadline-line-regexp)
  2993. (defvar org-scheduled-regexp nil
  2994. "Matches the SCHEDULED keyword.")
  2995. (make-variable-buffer-local 'org-scheduled-regexp)
  2996. (defvar org-scheduled-time-regexp nil
  2997. "Matches the SCHEDULED keyword together with a time stamp.")
  2998. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2999. (defvar org-closed-time-regexp nil
  3000. "Matches the CLOSED keyword together with a time stamp.")
  3001. (make-variable-buffer-local 'org-closed-time-regexp)
  3002. (defvar org-keyword-time-regexp nil
  3003. "Matches any of the 4 keywords, together with the time stamp.")
  3004. (make-variable-buffer-local 'org-keyword-time-regexp)
  3005. (defvar org-keyword-time-not-clock-regexp nil
  3006. "Matches any of the 3 keywords, together with the time stamp.")
  3007. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3008. (defvar org-maybe-keyword-time-regexp nil
  3009. "Matches a timestamp, possibly preceeded by a keyword.")
  3010. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3011. (defvar org-planning-or-clock-line-re nil
  3012. "Matches a line with planning or clock info.")
  3013. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3014. (defconst org-plain-time-of-day-regexp
  3015. (concat
  3016. "\\(\\<[012]?[0-9]"
  3017. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3018. "\\(--?"
  3019. "\\(\\<[012]?[0-9]"
  3020. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3021. "\\)?")
  3022. "Regular expression to match a plain time or time range.
  3023. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3024. groups carry important information:
  3025. 0 the full match
  3026. 1 the first time, range or not
  3027. 8 the second time, if it is a range.")
  3028. (defconst org-plain-time-extension-regexp
  3029. (concat
  3030. "\\(\\<[012]?[0-9]"
  3031. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3032. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3033. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3034. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3035. groups carry important information:
  3036. 0 the full match
  3037. 7 hours of duration
  3038. 9 minutes of duration")
  3039. (defconst org-stamp-time-of-day-regexp
  3040. (concat
  3041. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3042. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3043. "\\(--?"
  3044. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3045. "Regular expression to match a timestamp time or time range.
  3046. After a match, the following groups carry important information:
  3047. 0 the full match
  3048. 1 date plus weekday, for backreferencing to make sure both times on same day
  3049. 2 the first time, range or not
  3050. 4 the second time, if it is a range.")
  3051. (defconst org-startup-options
  3052. '(("fold" org-startup-folded t)
  3053. ("overview" org-startup-folded t)
  3054. ("nofold" org-startup-folded nil)
  3055. ("showall" org-startup-folded nil)
  3056. ("content" org-startup-folded content)
  3057. ("indent" org-startup-indented t)
  3058. ("noindent" org-startup-indented nil)
  3059. ("hidestars" org-hide-leading-stars t)
  3060. ("showstars" org-hide-leading-stars nil)
  3061. ("odd" org-odd-levels-only t)
  3062. ("oddeven" org-odd-levels-only nil)
  3063. ("align" org-startup-align-all-tables t)
  3064. ("noalign" org-startup-align-all-tables nil)
  3065. ("customtime" org-display-custom-times t)
  3066. ("logdone" org-log-done time)
  3067. ("lognotedone" org-log-done note)
  3068. ("nologdone" org-log-done nil)
  3069. ("lognoteclock-out" org-log-note-clock-out t)
  3070. ("nolognoteclock-out" org-log-note-clock-out nil)
  3071. ("logrepeat" org-log-repeat state)
  3072. ("lognoterepeat" org-log-repeat note)
  3073. ("nologrepeat" org-log-repeat nil)
  3074. ("fninline" org-footnote-define-inline t)
  3075. ("nofninline" org-footnote-define-inline nil)
  3076. ("fnlocal" org-footnote-section nil)
  3077. ("fnauto" org-footnote-auto-label t)
  3078. ("fnprompt" org-footnote-auto-label nil)
  3079. ("fnconfirm" org-footnote-auto-label confirm)
  3080. ("fnplain" org-footnote-auto-label plain)
  3081. ("fnadjust" org-footnote-auto-adjust t)
  3082. ("nofnadjust" org-footnote-auto-adjust nil)
  3083. ("constcgs" constants-unit-system cgs)
  3084. ("constSI" constants-unit-system SI)
  3085. ("noptag" org-tag-persistent-alist nil)
  3086. ("hideblocks" org-hide-block-startup t)
  3087. ("nohideblocks" org-hide-block-startup nil))
  3088. "Variable associated with STARTUP options for org-mode.
  3089. Each element is a list of three items: The startup options as written
  3090. in the #+STARTUP line, the corresponding variable, and the value to
  3091. set this variable to if the option is found. An optional forth element PUSH
  3092. means to push this value onto the list in the variable.")
  3093. (defun org-set-regexps-and-options ()
  3094. "Precompute regular expressions for current buffer."
  3095. (when (org-mode-p)
  3096. (org-set-local 'org-todo-kwd-alist nil)
  3097. (org-set-local 'org-todo-key-alist nil)
  3098. (org-set-local 'org-todo-key-trigger nil)
  3099. (org-set-local 'org-todo-keywords-1 nil)
  3100. (org-set-local 'org-done-keywords nil)
  3101. (org-set-local 'org-todo-heads nil)
  3102. (org-set-local 'org-todo-sets nil)
  3103. (org-set-local 'org-todo-log-states nil)
  3104. (org-set-local 'org-file-properties nil)
  3105. (org-set-local 'org-file-tags nil)
  3106. (let ((re (org-make-options-regexp
  3107. '("CATEGORY" "TODO" "COLUMNS"
  3108. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3109. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  3110. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3111. (splitre "[ \t]+")
  3112. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3113. tail sep kws1 prio props ftags drawers
  3114. ext-setup-or-nil setup-contents (start 0))
  3115. (save-excursion
  3116. (save-restriction
  3117. (widen)
  3118. (goto-char (point-min))
  3119. (while (or (and ext-setup-or-nil
  3120. (string-match re ext-setup-or-nil start)
  3121. (setq start (match-end 0)))
  3122. (and (setq ext-setup-or-nil nil start 0)
  3123. (re-search-forward re nil t)))
  3124. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3125. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3126. (cond
  3127. ((equal key "CATEGORY")
  3128. (if (string-match "[ \t]+$" value)
  3129. (setq value (replace-match "" t t value)))
  3130. (setq cat value))
  3131. ((member key '("SEQ_TODO" "TODO"))
  3132. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3133. ((equal key "TYP_TODO")
  3134. (push (cons 'type (org-split-string value splitre)) kwds))
  3135. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3136. ;; general TODO-like setup
  3137. (push (cons (intern (downcase (match-string 1 key)))
  3138. (org-split-string value splitre)) kwds))
  3139. ((equal key "TAGS")
  3140. (setq tags (append tags (if tags '("\\n") nil)
  3141. (org-split-string value splitre))))
  3142. ((equal key "COLUMNS")
  3143. (org-set-local 'org-columns-default-format value))
  3144. ((equal key "LINK")
  3145. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3146. (push (cons (match-string 1 value)
  3147. (org-trim (match-string 2 value)))
  3148. links)))
  3149. ((equal key "PRIORITIES")
  3150. (setq prio (org-split-string value " +")))
  3151. ((equal key "PROPERTY")
  3152. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3153. (push (cons (match-string 1 value) (match-string 2 value))
  3154. props)))
  3155. ((equal key "FILETAGS")
  3156. (when (string-match "\\S-" value)
  3157. (setq ftags
  3158. (append
  3159. ftags
  3160. (apply 'append
  3161. (mapcar (lambda (x) (org-split-string x ":"))
  3162. (org-split-string value)))))))
  3163. ((equal key "DRAWERS")
  3164. (setq drawers (org-split-string value splitre)))
  3165. ((equal key "CONSTANTS")
  3166. (setq const (append const (org-split-string value splitre))))
  3167. ((equal key "STARTUP")
  3168. (let ((opts (org-split-string value splitre))
  3169. l var val)
  3170. (while (setq l (pop opts))
  3171. (when (setq l (assoc l org-startup-options))
  3172. (setq var (nth 1 l) val (nth 2 l))
  3173. (if (not (nth 3 l))
  3174. (set (make-local-variable var) val)
  3175. (if (not (listp (symbol-value var)))
  3176. (set (make-local-variable var) nil))
  3177. (set (make-local-variable var) (symbol-value var))
  3178. (add-to-list var val))))))
  3179. ((equal key "ARCHIVE")
  3180. (string-match " *$" value)
  3181. (setq arch (replace-match "" t t value))
  3182. (remove-text-properties 0 (length arch)
  3183. '(face t fontified t) arch))
  3184. ((equal key "SETUPFILE")
  3185. (setq setup-contents (org-file-contents
  3186. (expand-file-name
  3187. (org-remove-double-quotes value))
  3188. 'noerror))
  3189. (if (not ext-setup-or-nil)
  3190. (setq ext-setup-or-nil setup-contents start 0)
  3191. (setq ext-setup-or-nil
  3192. (concat (substring ext-setup-or-nil 0 start)
  3193. "\n" setup-contents "\n"
  3194. (substring ext-setup-or-nil start)))))
  3195. ))))
  3196. (when cat
  3197. (org-set-local 'org-category (intern cat))
  3198. (push (cons "CATEGORY" cat) props))
  3199. (when prio
  3200. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3201. (setq prio (mapcar 'string-to-char prio))
  3202. (org-set-local 'org-highest-priority (nth 0 prio))
  3203. (org-set-local 'org-lowest-priority (nth 1 prio))
  3204. (org-set-local 'org-default-priority (nth 2 prio)))
  3205. (and props (org-set-local 'org-file-properties (nreverse props)))
  3206. (and ftags (org-set-local 'org-file-tags
  3207. (mapcar 'org-add-prop-inherited ftags)))
  3208. (and drawers (org-set-local 'org-drawers drawers))
  3209. (and arch (org-set-local 'org-archive-location arch))
  3210. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3211. ;; Process the TODO keywords
  3212. (unless kwds
  3213. ;; Use the global values as if they had been given locally.
  3214. (setq kwds (default-value 'org-todo-keywords))
  3215. (if (stringp (car kwds))
  3216. (setq kwds (list (cons org-todo-interpretation
  3217. (default-value 'org-todo-keywords)))))
  3218. (setq kwds (reverse kwds)))
  3219. (setq kwds (nreverse kwds))
  3220. (let (inter kws kw)
  3221. (while (setq kws (pop kwds))
  3222. (let ((kws (or
  3223. (run-hook-with-args-until-success
  3224. 'org-todo-setup-filter-hook kws)
  3225. kws)))
  3226. (setq inter (pop kws) sep (member "|" kws)
  3227. kws0 (delete "|" (copy-sequence kws))
  3228. kwsa nil
  3229. kws1 (mapcar
  3230. (lambda (x)
  3231. ;; 1 2
  3232. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3233. (progn
  3234. (setq kw (match-string 1 x)
  3235. key (and (match-end 2) (match-string 2 x))
  3236. log (org-extract-log-state-settings x))
  3237. (push (cons kw (and key (string-to-char key))) kwsa)
  3238. (and log (push log org-todo-log-states))
  3239. kw)
  3240. (error "Invalid TODO keyword %s" x)))
  3241. kws0)
  3242. kwsa (if kwsa (append '((:startgroup))
  3243. (nreverse kwsa)
  3244. '((:endgroup))))
  3245. hw (car kws1)
  3246. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3247. tail (list inter hw (car dws) (org-last dws))))
  3248. (add-to-list 'org-todo-heads hw 'append)
  3249. (push kws1 org-todo-sets)
  3250. (setq org-done-keywords (append org-done-keywords dws nil))
  3251. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3252. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3253. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3254. (setq org-todo-sets (nreverse org-todo-sets)
  3255. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3256. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3257. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3258. ;; Process the constants
  3259. (when const
  3260. (let (e cst)
  3261. (while (setq e (pop const))
  3262. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3263. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3264. (setq org-table-formula-constants-local cst)))
  3265. ;; Process the tags.
  3266. (when tags
  3267. (let (e tgs)
  3268. (while (setq e (pop tags))
  3269. (cond
  3270. ((equal e "{") (push '(:startgroup) tgs))
  3271. ((equal e "}") (push '(:endgroup) tgs))
  3272. ((equal e "\\n") (push '(:newline) tgs))
  3273. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3274. (push (cons (match-string 1 e)
  3275. (string-to-char (match-string 2 e)))
  3276. tgs))
  3277. (t (push (list e) tgs))))
  3278. (org-set-local 'org-tag-alist nil)
  3279. (while (setq e (pop tgs))
  3280. (or (and (stringp (car e))
  3281. (assoc (car e) org-tag-alist))
  3282. (push e org-tag-alist)))))
  3283. ;; Compute the regular expressions and other local variables
  3284. (if (not org-done-keywords)
  3285. (setq org-done-keywords (and org-todo-keywords-1
  3286. (list (org-last org-todo-keywords-1)))))
  3287. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3288. (length org-scheduled-string)
  3289. (length org-clock-string)
  3290. (length org-closed-string)))
  3291. org-drawer-regexp
  3292. (concat "^[ \t]*:\\("
  3293. (mapconcat 'regexp-quote org-drawers "\\|")
  3294. "\\):[ \t]*$")
  3295. org-not-done-keywords
  3296. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3297. org-todo-regexp
  3298. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3299. "\\|") "\\)\\>")
  3300. org-not-done-regexp
  3301. (concat "\\<\\("
  3302. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3303. "\\)\\>")
  3304. org-not-done-heading-regexp
  3305. (concat "^\\(\\*+\\)[ \t]+\\("
  3306. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3307. "\\)\\>")
  3308. org-todo-line-regexp
  3309. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3310. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3311. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3312. org-complex-heading-regexp
  3313. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3314. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3315. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3316. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3317. org-nl-done-regexp
  3318. (concat "\n\\*+[ \t]+"
  3319. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3320. "\\)" "\\>")
  3321. org-todo-line-tags-regexp
  3322. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3323. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3324. (org-re
  3325. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3326. org-looking-at-done-regexp
  3327. (concat "^" "\\(?:"
  3328. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3329. "\\>")
  3330. org-deadline-regexp (concat "\\<" org-deadline-string)
  3331. org-deadline-time-regexp
  3332. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3333. org-deadline-line-regexp
  3334. (concat "\\<\\(" org-deadline-string "\\).*")
  3335. org-scheduled-regexp
  3336. (concat "\\<" org-scheduled-string)
  3337. org-scheduled-time-regexp
  3338. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3339. org-closed-time-regexp
  3340. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3341. org-keyword-time-regexp
  3342. (concat "\\<\\(" org-scheduled-string
  3343. "\\|" org-deadline-string
  3344. "\\|" org-closed-string
  3345. "\\|" org-clock-string "\\)"
  3346. " *[[<]\\([^]>]+\\)[]>]")
  3347. org-keyword-time-not-clock-regexp
  3348. (concat "\\<\\(" org-scheduled-string
  3349. "\\|" org-deadline-string
  3350. "\\|" org-closed-string
  3351. "\\)"
  3352. " *[[<]\\([^]>]+\\)[]>]")
  3353. org-maybe-keyword-time-regexp
  3354. (concat "\\(\\<\\(" org-scheduled-string
  3355. "\\|" org-deadline-string
  3356. "\\|" org-closed-string
  3357. "\\|" org-clock-string "\\)\\)?"
  3358. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3359. org-planning-or-clock-line-re
  3360. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3361. "\\|" org-deadline-string
  3362. "\\|" org-closed-string "\\|" org-clock-string
  3363. "\\)\\>\\)")
  3364. )
  3365. (org-compute-latex-and-specials-regexp)
  3366. (org-set-font-lock-defaults))))
  3367. (defun org-file-contents (file &optional noerror)
  3368. "Return the contents of FILE, as a string."
  3369. (if (or (not file)
  3370. (not (file-readable-p file)))
  3371. (if noerror
  3372. (progn
  3373. (message "Cannot read file %s" file)
  3374. (ding) (sit-for 2)
  3375. "")
  3376. (error "Cannot read file %s" file))
  3377. (with-temp-buffer
  3378. (insert-file-contents file)
  3379. (buffer-string))))
  3380. (defun org-extract-log-state-settings (x)
  3381. "Extract the log state setting from a TODO keyword string.
  3382. This will extract info from a string like \"WAIT(w@/!)\"."
  3383. (let (kw key log1 log2)
  3384. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3385. (setq kw (match-string 1 x)
  3386. key (and (match-end 2) (match-string 2 x))
  3387. log1 (and (match-end 3) (match-string 3 x))
  3388. log2 (and (match-end 4) (match-string 4 x)))
  3389. (and (or log1 log2)
  3390. (list kw
  3391. (and log1 (if (equal log1 "!") 'time 'note))
  3392. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3393. (defun org-remove-keyword-keys (list)
  3394. "Remove a pair of parenthesis at the end of each string in LIST."
  3395. (mapcar (lambda (x)
  3396. (if (string-match "(.*)$" x)
  3397. (substring x 0 (match-beginning 0))
  3398. x))
  3399. list))
  3400. ;; FIXME: this could be done much better, using second characters etc.
  3401. (defun org-assign-fast-keys (alist)
  3402. "Assign fast keys to a keyword-key alist.
  3403. Respect keys that are already there."
  3404. (let (new e k c c1 c2 (char ?a))
  3405. (while (setq e (pop alist))
  3406. (cond
  3407. ((equal e '(:startgroup)) (push e new))
  3408. ((equal e '(:endgroup)) (push e new))
  3409. ((equal e '(:newline)) (push e new))
  3410. (t
  3411. (setq k (car e) c2 nil)
  3412. (if (cdr e)
  3413. (setq c (cdr e))
  3414. ;; automatically assign a character.
  3415. (setq c1 (string-to-char
  3416. (downcase (substring
  3417. k (if (= (string-to-char k) ?@) 1 0)))))
  3418. (if (or (rassoc c1 new) (rassoc c1 alist))
  3419. (while (or (rassoc char new) (rassoc char alist))
  3420. (setq char (1+ char)))
  3421. (setq c2 c1))
  3422. (setq c (or c2 char)))
  3423. (push (cons k c) new))))
  3424. (nreverse new)))
  3425. ;;; Some variables used in various places
  3426. (defvar org-window-configuration nil
  3427. "Used in various places to store a window configuration.")
  3428. (defvar org-selected-window nil
  3429. "Used in various places to store a window configuration.")
  3430. (defvar org-finish-function nil
  3431. "Function to be called when `C-c C-c' is used.
  3432. This is for getting out of special buffers like remember.")
  3433. ;; FIXME: Occasionally check by commenting these, to make sure
  3434. ;; no other functions uses these, forgetting to let-bind them.
  3435. (defvar entry)
  3436. (defvar last-state)
  3437. (defvar date)
  3438. ;; Defined somewhere in this file, but used before definition.
  3439. (defvar org-html-entities)
  3440. (defvar org-struct-menu)
  3441. (defvar org-org-menu)
  3442. (defvar org-tbl-menu)
  3443. (defvar org-agenda-keymap)
  3444. ;;;; Define the Org-mode
  3445. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3446. (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."))
  3447. ;; We use a before-change function to check if a table might need
  3448. ;; an update.
  3449. (defvar org-table-may-need-update t
  3450. "Indicates that a table might need an update.
  3451. This variable is set by `org-before-change-function'.
  3452. `org-table-align' sets it back to nil.")
  3453. (defun org-before-change-function (beg end)
  3454. "Every change indicates that a table might need an update."
  3455. (setq org-table-may-need-update t))
  3456. (defvar org-mode-map)
  3457. (defvar org-mode-hook nil
  3458. "Mode hook for Org-mode, run after the mode was turned on.")
  3459. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3460. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3461. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3462. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3463. (defvar org-table-buffer-is-an nil)
  3464. (defconst org-outline-regexp "\\*+ ")
  3465. ;;;###autoload
  3466. (define-derived-mode org-mode outline-mode "Org"
  3467. "Outline-based notes management and organizer, alias
  3468. \"Carsten's outline-mode for keeping track of everything.\"
  3469. Org-mode develops organizational tasks around a NOTES file which
  3470. contains information about projects as plain text. Org-mode is
  3471. implemented on top of outline-mode, which is ideal to keep the content
  3472. of large files well structured. It supports ToDo items, deadlines and
  3473. time stamps, which magically appear in the diary listing of the Emacs
  3474. calendar. Tables are easily created with a built-in table editor.
  3475. Plain text URL-like links connect to websites, emails (VM), Usenet
  3476. messages (Gnus), BBDB entries, and any files related to the project.
  3477. For printing and sharing of notes, an Org-mode file (or a part of it)
  3478. can be exported as a structured ASCII or HTML file.
  3479. The following commands are available:
  3480. \\{org-mode-map}"
  3481. ;; Get rid of Outline menus, they are not needed
  3482. ;; Need to do this here because define-derived-mode sets up
  3483. ;; the keymap so late. Still, it is a waste to call this each time
  3484. ;; we switch another buffer into org-mode.
  3485. (if (featurep 'xemacs)
  3486. (when (boundp 'outline-mode-menu-heading)
  3487. ;; Assume this is Greg's port, it used easymenu
  3488. (easy-menu-remove outline-mode-menu-heading)
  3489. (easy-menu-remove outline-mode-menu-show)
  3490. (easy-menu-remove outline-mode-menu-hide))
  3491. (define-key org-mode-map [menu-bar headings] 'undefined)
  3492. (define-key org-mode-map [menu-bar hide] 'undefined)
  3493. (define-key org-mode-map [menu-bar show] 'undefined))
  3494. (org-load-modules-maybe)
  3495. (easy-menu-add org-org-menu)
  3496. (easy-menu-add org-tbl-menu)
  3497. (org-install-agenda-files-menu)
  3498. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3499. (org-add-to-invisibility-spec '(org-cwidth))
  3500. (org-add-to-invisibility-spec '(org-hide-block . t))
  3501. (when (featurep 'xemacs)
  3502. (org-set-local 'line-move-ignore-invisible t))
  3503. (org-set-local 'outline-regexp org-outline-regexp)
  3504. (org-set-local 'outline-level 'org-outline-level)
  3505. (when (and org-ellipsis
  3506. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3507. (fboundp 'make-glyph-code))
  3508. (unless org-display-table
  3509. (setq org-display-table (make-display-table)))
  3510. (set-display-table-slot
  3511. org-display-table 4
  3512. (vconcat (mapcar
  3513. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3514. org-ellipsis)))
  3515. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3516. (setq buffer-display-table org-display-table))
  3517. (org-set-regexps-and-options)
  3518. (when (and org-tag-faces (not org-tags-special-faces-re))
  3519. ;; tag faces set outside customize.... force initialization.
  3520. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3521. ;; Calc embedded
  3522. (org-set-local 'calc-embedded-open-mode "# ")
  3523. (modify-syntax-entry ?# "<")
  3524. (modify-syntax-entry ?@ "w")
  3525. (if org-startup-truncated (setq truncate-lines t))
  3526. (org-set-local 'font-lock-unfontify-region-function
  3527. 'org-unfontify-region)
  3528. ;; Activate before-change-function
  3529. (org-set-local 'org-table-may-need-update t)
  3530. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3531. 'local)
  3532. ;; Check for running clock before killing a buffer
  3533. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3534. ;; Paragraphs and auto-filling
  3535. (org-set-autofill-regexps)
  3536. (setq indent-line-function 'org-indent-line-function)
  3537. (org-update-radio-target-regexp)
  3538. ;; Make sure dependence stuff works reliably, even for users who set it
  3539. ;; too late :-(
  3540. (if org-enforce-todo-dependencies
  3541. (add-hook 'org-blocker-hook
  3542. 'org-block-todo-from-children-or-siblings-or-parent)
  3543. (remove-hook 'org-blocker-hook
  3544. 'org-block-todo-from-children-or-siblings-or-parent))
  3545. (if org-enforce-todo-checkbox-dependencies
  3546. (add-hook 'org-blocker-hook
  3547. 'org-block-todo-from-checkboxes)
  3548. (remove-hook 'org-blocker-hook
  3549. 'org-block-todo-from-checkboxes))
  3550. ;; Comment characters
  3551. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3552. (org-set-local 'comment-padding " ")
  3553. ;; Align options lines
  3554. (org-set-local
  3555. 'align-mode-rules-list
  3556. '((org-in-buffer-settings
  3557. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3558. (modes . '(org-mode)))))
  3559. ;; Imenu
  3560. (org-set-local 'imenu-create-index-function
  3561. 'org-imenu-get-tree)
  3562. ;; Make isearch reveal context
  3563. (if (or (featurep 'xemacs)
  3564. (not (boundp 'outline-isearch-open-invisible-function)))
  3565. ;; Emacs 21 and XEmacs make use of the hook
  3566. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3567. ;; Emacs 22 deals with this through a special variable
  3568. (org-set-local 'outline-isearch-open-invisible-function
  3569. (lambda (&rest ignore) (org-show-context 'isearch))))
  3570. ;; If empty file that did not turn on org-mode automatically, make it to.
  3571. (if (and org-insert-mode-line-in-empty-file
  3572. (interactive-p)
  3573. (= (point-min) (point-max)))
  3574. (insert "# -*- mode: org -*-\n\n"))
  3575. (unless org-inhibit-startup
  3576. (when org-startup-align-all-tables
  3577. (let ((bmp (buffer-modified-p)))
  3578. (org-table-map-tables 'org-table-align)
  3579. (set-buffer-modified-p bmp)))
  3580. (when org-startup-indented
  3581. (require 'org-indent)
  3582. (org-indent-mode 1))
  3583. (org-set-startup-visibility)))
  3584. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3585. (defun org-current-time ()
  3586. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3587. (if (> (car org-time-stamp-rounding-minutes) 1)
  3588. (let ((r (car org-time-stamp-rounding-minutes))
  3589. (time (decode-time)))
  3590. (apply 'encode-time
  3591. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3592. (nthcdr 2 time))))
  3593. (current-time)))
  3594. ;;;; Font-Lock stuff, including the activators
  3595. (defvar org-mouse-map (make-sparse-keymap))
  3596. (org-defkey org-mouse-map
  3597. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3598. (org-defkey org-mouse-map
  3599. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3600. (when org-mouse-1-follows-link
  3601. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3602. (when org-tab-follows-link
  3603. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3604. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3605. (require 'font-lock)
  3606. (defconst org-non-link-chars "]\t\n\r<>")
  3607. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3608. "shell" "elisp"))
  3609. (defvar org-link-types-re nil
  3610. "Matches a link that has a url-like prefix like \"http:\"")
  3611. (defvar org-link-re-with-space nil
  3612. "Matches a link with spaces, optional angular brackets around it.")
  3613. (defvar org-link-re-with-space2 nil
  3614. "Matches a link with spaces, optional angular brackets around it.")
  3615. (defvar org-link-re-with-space3 nil
  3616. "Matches a link with spaces, only for internal part in bracket links.")
  3617. (defvar org-angle-link-re nil
  3618. "Matches link with angular brackets, spaces are allowed.")
  3619. (defvar org-plain-link-re nil
  3620. "Matches plain link, without spaces.")
  3621. (defvar org-bracket-link-regexp nil
  3622. "Matches a link in double brackets.")
  3623. (defvar org-bracket-link-analytic-regexp nil
  3624. "Regular expression used to analyze links.
  3625. Here is what the match groups contain after a match:
  3626. 1: http:
  3627. 2: http
  3628. 3: path
  3629. 4: [desc]
  3630. 5: desc")
  3631. (defvar org-bracket-link-analytic-regexp++ nil
  3632. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3633. (defvar org-any-link-re nil
  3634. "Regular expression matching any link.")
  3635. (defun org-make-link-regexps ()
  3636. "Update the link regular expressions.
  3637. This should be called after the variable `org-link-types' has changed."
  3638. (setq org-link-types-re
  3639. (concat
  3640. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3641. org-link-re-with-space
  3642. (concat
  3643. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3644. "\\([^" org-non-link-chars " ]"
  3645. "[^" org-non-link-chars "]*"
  3646. "[^" org-non-link-chars " ]\\)>?")
  3647. org-link-re-with-space2
  3648. (concat
  3649. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3650. "\\([^" org-non-link-chars " ]"
  3651. "[^\t\n\r]*"
  3652. "[^" org-non-link-chars " ]\\)>?")
  3653. org-link-re-with-space3
  3654. (concat
  3655. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3656. "\\([^" org-non-link-chars " ]"
  3657. "[^\t\n\r]*\\)")
  3658. org-angle-link-re
  3659. (concat
  3660. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3661. "\\([^" org-non-link-chars " ]"
  3662. "[^" org-non-link-chars "]*"
  3663. "\\)>")
  3664. org-plain-link-re
  3665. (concat
  3666. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3667. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3668. org-bracket-link-regexp
  3669. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3670. org-bracket-link-analytic-regexp
  3671. (concat
  3672. "\\[\\["
  3673. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3674. "\\([^]]+\\)"
  3675. "\\]"
  3676. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3677. "\\]")
  3678. org-bracket-link-analytic-regexp++
  3679. (concat
  3680. "\\[\\["
  3681. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3682. "\\([^]]+\\)"
  3683. "\\]"
  3684. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3685. "\\]")
  3686. org-any-link-re
  3687. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3688. org-angle-link-re "\\)\\|\\("
  3689. org-plain-link-re "\\)")))
  3690. (org-make-link-regexps)
  3691. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3692. "Regular expression for fast time stamp matching.")
  3693. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3694. "Regular expression for fast time stamp matching.")
  3695. (defconst org-ts-regexp0 "\\(\\([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. This one does not require the space after the date, so it can be used
  3698. on a string that terminates immediately after the date.")
  3699. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3700. "Regular expression matching time strings for analysis.")
  3701. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3702. "Regular expression matching time stamps, with groups.")
  3703. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3704. "Regular expression matching time stamps (also [..]), with groups.")
  3705. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3706. "Regular expression matching a time stamp range.")
  3707. (defconst org-tr-regexp-both
  3708. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3709. "Regular expression matching a time stamp range.")
  3710. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3711. org-ts-regexp "\\)?")
  3712. "Regular expression matching a time stamp or time stamp range.")
  3713. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3714. org-ts-regexp-both "\\)?")
  3715. "Regular expression matching a time stamp or time stamp range.
  3716. The time stamps may be either active or inactive.")
  3717. (defvar org-emph-face nil)
  3718. (defun org-do-emphasis-faces (limit)
  3719. "Run through the buffer and add overlays to links."
  3720. (let (rtn a)
  3721. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3722. (if (not (= (char-after (match-beginning 3))
  3723. (char-after (match-beginning 4))))
  3724. (progn
  3725. (setq rtn t)
  3726. (setq a (assoc (match-string 3) org-emphasis-alist))
  3727. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3728. 'face
  3729. (nth 1 a))
  3730. (and (nth 4 a)
  3731. (org-remove-flyspell-overlays-in
  3732. (match-beginning 0) (match-end 0)))
  3733. (add-text-properties (match-beginning 2) (match-end 2)
  3734. '(font-lock-multiline t))
  3735. (when org-hide-emphasis-markers
  3736. (add-text-properties (match-end 4) (match-beginning 5)
  3737. '(invisible org-link))
  3738. (add-text-properties (match-beginning 3) (match-end 3)
  3739. '(invisible org-link)))))
  3740. (backward-char 1))
  3741. rtn))
  3742. (defun org-emphasize (&optional char)
  3743. "Insert or change an emphasis, i.e. a font like bold or italic.
  3744. If there is an active region, change that region to a new emphasis.
  3745. If there is no region, just insert the marker characters and position
  3746. the cursor between them.
  3747. CHAR should be either the marker character, or the first character of the
  3748. HTML tag associated with that emphasis. If CHAR is a space, the means
  3749. to remove the emphasis of the selected region.
  3750. If char is not given (for example in an interactive call) it
  3751. will be prompted for."
  3752. (interactive)
  3753. (let ((eal org-emphasis-alist) e det
  3754. (erc org-emphasis-regexp-components)
  3755. (prompt "")
  3756. (string "") beg end move tag c s)
  3757. (if (org-region-active-p)
  3758. (setq beg (region-beginning) end (region-end)
  3759. string (buffer-substring beg end))
  3760. (setq move t))
  3761. (while (setq e (pop eal))
  3762. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3763. c (aref tag 0))
  3764. (push (cons c (string-to-char (car e))) det)
  3765. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3766. (substring tag 1)))))
  3767. (setq det (nreverse det))
  3768. (unless char
  3769. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3770. (setq char (read-char-exclusive)))
  3771. (setq char (or (cdr (assoc char det)) char))
  3772. (if (equal char ?\ )
  3773. (setq s "" move nil)
  3774. (unless (assoc (char-to-string char) org-emphasis-alist)
  3775. (error "No such emphasis marker: \"%c\"" char))
  3776. (setq s (char-to-string char)))
  3777. (while (and (> (length string) 1)
  3778. (equal (substring string 0 1) (substring string -1))
  3779. (assoc (substring string 0 1) org-emphasis-alist))
  3780. (setq string (substring string 1 -1)))
  3781. (setq string (concat s string s))
  3782. (if beg (delete-region beg end))
  3783. (unless (or (bolp)
  3784. (string-match (concat "[" (nth 0 erc) "\n]")
  3785. (char-to-string (char-before (point)))))
  3786. (insert " "))
  3787. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3788. (char-to-string (char-after (point))))
  3789. (insert " ") (backward-char 1))
  3790. (insert string)
  3791. (and move (backward-char 1))))
  3792. (defconst org-nonsticky-props
  3793. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3794. (defsubst org-rear-nonsticky-at (pos)
  3795. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  3796. (defun org-activate-plain-links (limit)
  3797. "Run through the buffer and add overlays to links."
  3798. (catch 'exit
  3799. (let (f)
  3800. (if (re-search-forward org-plain-link-re limit t)
  3801. (progn
  3802. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3803. (setq f (get-text-property (match-beginning 0) 'face))
  3804. (if (or (eq f 'org-tag)
  3805. (and (listp f) (memq 'org-tag f)))
  3806. nil
  3807. (add-text-properties (match-beginning 0) (match-end 0)
  3808. (list 'mouse-face 'highlight
  3809. 'keymap org-mouse-map))
  3810. (org-rear-nonsticky-at (match-end 0)))
  3811. t)))))
  3812. (defun org-activate-code (limit)
  3813. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3814. (progn
  3815. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3816. (remove-text-properties (match-beginning 0) (match-end 0)
  3817. '(display t invisible t intangible t))
  3818. t)))
  3819. (defun org-fontify-meta-lines-and-blocks (limit)
  3820. "Fontify #+ lines and blocks, in the correct ways."
  3821. (let ((case-fold-search t))
  3822. (if (re-search-forward
  3823. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  3824. limit t)
  3825. (let ((beg (match-beginning 0))
  3826. (beg1 (line-beginning-position 2))
  3827. (dc1 (downcase (match-string 2)))
  3828. (dc3 (downcase (match-string 3)))
  3829. end end1 quoting)
  3830. (cond
  3831. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  3832. ;; a single line of backend-specific content
  3833. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3834. (remove-text-properties (match-beginning 0) (match-end 0)
  3835. '(display t invisible t intangible t))
  3836. (add-text-properties (match-beginning 1) (match-end 3)
  3837. '(font-lock-fontified t face org-meta-line))
  3838. (add-text-properties (match-beginning 6) (match-end 6)
  3839. '(font-lock-fontified t face org-block))
  3840. t)
  3841. ((and (match-end 4) (equal dc3 "begin"))
  3842. ;; Truely a block
  3843. (setq quoting (member (downcase (match-string 5))
  3844. org-protecting-blocks))
  3845. (when (re-search-forward
  3846. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  3847. nil t) ;; on purpose, we look further than LIMIT
  3848. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  3849. (when quoting
  3850. (remove-text-properties beg end
  3851. '(display t invisible t intangible t)))
  3852. (add-text-properties
  3853. beg end
  3854. '(font-lock-fontified t font-lock-multiline t))
  3855. (add-text-properties beg beg1 '(face org-meta-line))
  3856. (add-text-properties end1 end '(face org-meta-line))
  3857. (when quoting
  3858. (add-text-properties beg1 end1 '(face org-block)))
  3859. t))
  3860. ((not (member (char-after beg) '(?\ ?\t)))
  3861. ;; just any other in-buffer setting, but not indented
  3862. (add-text-properties
  3863. beg (match-end 0)
  3864. '(font-lock-fontified t face org-meta-line))
  3865. t)
  3866. ((or (member dc1 '("caption:" "label:" "orgtbl:" "tblfm:" "tblname:"))
  3867. (and (match-end 4) (equal dc3 "attr")))
  3868. (add-text-properties
  3869. beg (match-end 0)
  3870. '(font-lock-fontified t face org-meta-line))
  3871. t)
  3872. (t nil))))))
  3873. (defun org-activate-angle-links (limit)
  3874. "Run through the buffer and add overlays to links."
  3875. (if (re-search-forward org-angle-link-re limit t)
  3876. (progn
  3877. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3878. (add-text-properties (match-beginning 0) (match-end 0)
  3879. (list 'mouse-face 'highlight
  3880. 'keymap org-mouse-map))
  3881. (org-rear-nonsticky-at (match-end 0))
  3882. t)))
  3883. (defun org-activate-footnote-links (limit)
  3884. "Run through the buffer and add overlays to links."
  3885. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3886. limit t)
  3887. (progn
  3888. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3889. (add-text-properties (match-beginning 2) (match-end 2)
  3890. (list 'mouse-face 'highlight
  3891. 'keymap org-mouse-map
  3892. 'help-echo
  3893. (if (= (point-at-bol) (match-beginning 2))
  3894. "Footnote definition"
  3895. "Footnote reference")
  3896. ))
  3897. (org-rear-nonsticky-at (match-end 2))
  3898. t)))
  3899. (defun org-activate-bracket-links (limit)
  3900. "Run through the buffer and add overlays to bracketed links."
  3901. (if (re-search-forward org-bracket-link-regexp limit t)
  3902. (let* ((help (concat "LINK: "
  3903. (org-match-string-no-properties 1)))
  3904. ;; FIXME: above we should remove the escapes.
  3905. ;; but that requires another match, protecting match data,
  3906. ;; a lot of overhead for font-lock.
  3907. (ip (org-maybe-intangible
  3908. (list 'invisible 'org-link
  3909. 'keymap org-mouse-map 'mouse-face 'highlight
  3910. 'font-lock-multiline t 'help-echo help)))
  3911. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  3912. 'font-lock-multiline t 'help-echo help)))
  3913. ;; We need to remove the invisible property here. Table narrowing
  3914. ;; may have made some of this invisible.
  3915. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3916. (remove-text-properties (match-beginning 0) (match-end 0)
  3917. '(invisible nil))
  3918. (if (match-end 3)
  3919. (progn
  3920. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3921. (org-rear-nonsticky-at (match-beginning 3))
  3922. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3923. (org-rear-nonsticky-at (match-end 3))
  3924. (add-text-properties (match-end 3) (match-end 0) ip)
  3925. (org-rear-nonsticky-at (match-end 0)))
  3926. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3927. (org-rear-nonsticky-at (match-beginning 1))
  3928. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3929. (org-rear-nonsticky-at (match-end 1))
  3930. (add-text-properties (match-end 1) (match-end 0) ip)
  3931. (org-rear-nonsticky-at (match-end 0)))
  3932. t)))
  3933. (defun org-activate-dates (limit)
  3934. "Run through the buffer and add overlays to dates."
  3935. (if (re-search-forward org-tsr-regexp-both limit t)
  3936. (progn
  3937. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3938. (add-text-properties (match-beginning 0) (match-end 0)
  3939. (list 'mouse-face 'highlight
  3940. 'keymap org-mouse-map))
  3941. (org-rear-nonsticky-at (match-end 0))
  3942. (when org-display-custom-times
  3943. (if (match-end 3)
  3944. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3945. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3946. t)))
  3947. (defvar org-target-link-regexp nil
  3948. "Regular expression matching radio targets in plain text.")
  3949. (make-variable-buffer-local 'org-target-link-regexp)
  3950. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3951. "Regular expression matching a link target.")
  3952. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3953. "Regular expression matching a radio target.")
  3954. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3955. "Regular expression matching any target.")
  3956. (defun org-activate-target-links (limit)
  3957. "Run through the buffer and add overlays to target matches."
  3958. (when org-target-link-regexp
  3959. (let ((case-fold-search t))
  3960. (if (re-search-forward org-target-link-regexp limit t)
  3961. (progn
  3962. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  3963. (add-text-properties (match-beginning 0) (match-end 0)
  3964. (list 'mouse-face 'highlight
  3965. 'keymap org-mouse-map
  3966. 'help-echo "Radio target link"
  3967. 'org-linked-text t))
  3968. (org-rear-nonsticky-at (match-end 0))
  3969. t)))))
  3970. (defun org-update-radio-target-regexp ()
  3971. "Find all radio targets in this file and update the regular expression."
  3972. (interactive)
  3973. (when (memq 'radio org-activate-links)
  3974. (setq org-target-link-regexp
  3975. (org-make-target-link-regexp (org-all-targets 'radio)))
  3976. (org-restart-font-lock)))
  3977. (defun org-hide-wide-columns (limit)
  3978. (let (s e)
  3979. (setq s (text-property-any (point) (or limit (point-max))
  3980. 'org-cwidth t))
  3981. (when s
  3982. (setq e (next-single-property-change s 'org-cwidth))
  3983. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3984. (goto-char e)
  3985. t)))
  3986. (defvar org-latex-and-specials-regexp nil
  3987. "Regular expression for highlighting export special stuff.")
  3988. (defvar org-match-substring-regexp)
  3989. (defvar org-match-substring-with-braces-regexp)
  3990. ;; This should be with the exporter code, but we also use if for font-locking
  3991. (defconst org-export-html-special-string-regexps
  3992. '(("\\\\-" . "&shy;")
  3993. ("---\\([^-]\\)" . "&mdash;\\1")
  3994. ("--\\([^-]\\)" . "&ndash;\\1")
  3995. ("\\.\\.\\." . "&hellip;"))
  3996. "Regular expressions for special string conversion.")
  3997. (defun org-compute-latex-and-specials-regexp ()
  3998. "Compute regular expression for stuff treated specially by exporters."
  3999. (if (not org-highlight-latex-fragments-and-specials)
  4000. (org-set-local 'org-latex-and-specials-regexp nil)
  4001. (require 'org-exp)
  4002. (let*
  4003. ((matchers (plist-get org-format-latex-options :matchers))
  4004. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4005. org-latex-regexps)))
  4006. (options (org-combine-plists (org-default-export-plist)
  4007. (org-infile-export-plist)))
  4008. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4009. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4010. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4011. (org-export-html-expand (plist-get options :expand-quoted-html))
  4012. (org-export-with-special-strings (plist-get options :special-strings))
  4013. (re-sub
  4014. (cond
  4015. ((equal org-export-with-sub-superscripts '{})
  4016. (list org-match-substring-with-braces-regexp))
  4017. (org-export-with-sub-superscripts
  4018. (list org-match-substring-regexp))
  4019. (t nil)))
  4020. (re-latex
  4021. (if org-export-with-LaTeX-fragments
  4022. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4023. (re-macros
  4024. (if org-export-with-TeX-macros
  4025. (list (concat "\\\\"
  4026. (regexp-opt
  4027. (append (mapcar 'car org-html-entities)
  4028. (if (boundp 'org-latex-entities)
  4029. (mapcar (lambda (x)
  4030. (or (car-safe x) x))
  4031. org-latex-entities)
  4032. nil))
  4033. 'words))) ; FIXME
  4034. ))
  4035. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4036. (re-special (if org-export-with-special-strings
  4037. (mapcar (lambda (x) (car x))
  4038. org-export-html-special-string-regexps)))
  4039. (re-rest
  4040. (delq nil
  4041. (list
  4042. (if org-export-html-expand "@<[^>\n]+>")
  4043. ))))
  4044. (org-set-local
  4045. 'org-latex-and-specials-regexp
  4046. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4047. re-rest) "\\|")))))
  4048. (defun org-do-latex-and-special-faces (limit)
  4049. "Run through the buffer and add overlays to links."
  4050. (when org-latex-and-specials-regexp
  4051. (let (rtn d)
  4052. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4053. limit t))
  4054. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4055. 'face))
  4056. '(org-code org-verbatim underline)))
  4057. (progn
  4058. (setq rtn t
  4059. d (cond ((member (char-after (1+ (match-beginning 0)))
  4060. '(?_ ?^)) 1)
  4061. (t 0)))
  4062. (font-lock-prepend-text-property
  4063. (+ d (match-beginning 0)) (match-end 0)
  4064. 'face 'org-latex-and-export-specials)
  4065. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4066. '(font-lock-multiline t)))))
  4067. rtn)))
  4068. (defun org-restart-font-lock ()
  4069. "Restart font-lock-mode, to force refontification."
  4070. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4071. (font-lock-mode -1)
  4072. (font-lock-mode 1)))
  4073. (defun org-all-targets (&optional radio)
  4074. "Return a list of all targets in this file.
  4075. With optional argument RADIO, only find radio targets."
  4076. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4077. rtn)
  4078. (save-excursion
  4079. (goto-char (point-min))
  4080. (while (re-search-forward re nil t)
  4081. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4082. rtn)))
  4083. (defun org-make-target-link-regexp (targets)
  4084. "Make regular expression matching all strings in TARGETS.
  4085. The regular expression finds the targets also if there is a line break
  4086. between words."
  4087. (and targets
  4088. (concat
  4089. "\\<\\("
  4090. (mapconcat
  4091. (lambda (x)
  4092. (while (string-match " +" x)
  4093. (setq x (replace-match "\\s-+" t t x)))
  4094. x)
  4095. targets
  4096. "\\|")
  4097. "\\)\\>")))
  4098. (defun org-activate-tags (limit)
  4099. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4100. (progn
  4101. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4102. (add-text-properties (match-beginning 1) (match-end 1)
  4103. (list 'mouse-face 'highlight
  4104. 'keymap org-mouse-map))
  4105. (org-rear-nonsticky-at (match-end 1))
  4106. t)))
  4107. (defun org-outline-level ()
  4108. (save-excursion
  4109. (looking-at outline-regexp)
  4110. (if (match-beginning 1)
  4111. (+ (org-get-string-indentation (match-string 1)) 1000)
  4112. (1- (- (match-end 0) (match-beginning 0))))))
  4113. (defvar org-font-lock-keywords nil)
  4114. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4115. "Regular expression matching a property line.")
  4116. (defvar org-font-lock-hook nil
  4117. "Functions to be called for special font lock stuff.")
  4118. (defun org-font-lock-hook (limit)
  4119. (run-hook-with-args 'org-font-lock-hook limit))
  4120. (defun org-set-font-lock-defaults ()
  4121. (let* ((em org-fontify-emphasized-text)
  4122. (lk org-activate-links)
  4123. (org-font-lock-extra-keywords
  4124. (list
  4125. ;; Call the hook
  4126. '(org-font-lock-hook)
  4127. ;; Headlines
  4128. `(,(if org-fontify-whole-heading-line
  4129. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4130. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4131. (1 (org-get-level-face 1))
  4132. (2 (org-get-level-face 2))
  4133. (3 (org-get-level-face 3)))
  4134. ;; Table lines
  4135. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4136. (1 'org-table t))
  4137. ;; Table internals
  4138. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4139. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4140. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4141. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4142. ;; Drawers
  4143. (list org-drawer-regexp '(0 'org-special-keyword t))
  4144. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4145. ;; Properties
  4146. (list org-property-re
  4147. '(1 'org-special-keyword t)
  4148. '(3 'org-property-value t))
  4149. ;; Links
  4150. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4151. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4152. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  4153. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4154. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4155. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4156. (if (memq 'footnote lk) '(org-activate-footnote-links
  4157. (2 'org-footnote t)))
  4158. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4159. '(org-hide-wide-columns (0 nil append))
  4160. ;; TODO lines
  4161. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4162. '(1 (org-get-todo-face 1) t))
  4163. ;; DONE
  4164. (if org-fontify-done-headline
  4165. (list (concat "^[*]+ +\\<\\("
  4166. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4167. "\\)\\(.*\\)")
  4168. '(2 'org-headline-done t))
  4169. nil)
  4170. ;; Priorities
  4171. '(org-font-lock-add-priority-faces)
  4172. ;; Tags
  4173. '(org-font-lock-add-tag-faces)
  4174. ;; Special keywords
  4175. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4176. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4177. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4178. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4179. ;; Emphasis
  4180. (if em
  4181. (if (featurep 'xemacs)
  4182. '(org-do-emphasis-faces (0 nil append))
  4183. '(org-do-emphasis-faces)))
  4184. ;; Checkboxes
  4185. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4186. 2 'org-checkbox prepend)
  4187. (if org-provide-checkbox-statistics
  4188. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4189. (0 (org-get-checkbox-statistics-face) t)))
  4190. ;; Description list items
  4191. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4192. 2 'bold prepend)
  4193. ;; ARCHIVEd headings
  4194. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4195. '(1 'org-archived prepend))
  4196. ;; Specials
  4197. '(org-do-latex-and-special-faces)
  4198. ;; Code
  4199. '(org-activate-code (1 'org-code t))
  4200. ;; COMMENT
  4201. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4202. "\\|" org-quote-string "\\)\\>")
  4203. '(1 'org-special-keyword t))
  4204. '("^#.*" (0 'font-lock-comment-face t))
  4205. ;; Blocks and meta lines
  4206. '(org-fontify-meta-lines-and-blocks)
  4207. )))
  4208. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4209. ;; Now set the full font-lock-keywords
  4210. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4211. (org-set-local 'font-lock-defaults
  4212. '(org-font-lock-keywords t nil nil backward-paragraph))
  4213. (kill-local-variable 'font-lock-keywords) nil))
  4214. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4215. "Fontify string S like in Org-mode"
  4216. (with-temp-buffer
  4217. (insert s)
  4218. (let ((org-odd-levels-only odd-levels))
  4219. (org-mode)
  4220. (font-lock-fontify-buffer)
  4221. (buffer-string))))
  4222. (defvar org-m nil)
  4223. (defvar org-l nil)
  4224. (defvar org-f nil)
  4225. (defun org-get-level-face (n)
  4226. "Get the right face for match N in font-lock matching of headlines."
  4227. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4228. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4229. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4230. (cond
  4231. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4232. ((eq n 2) org-f)
  4233. (t (if org-level-color-stars-only nil org-f))))
  4234. (defun org-get-todo-face (kwd)
  4235. "Get the right face for a TODO keyword KWD.
  4236. If KWD is a number, get the corresponding match group."
  4237. (if (numberp kwd) (setq kwd (match-string kwd)))
  4238. (or (cdr (assoc kwd org-todo-keyword-faces))
  4239. (and (member kwd org-done-keywords) 'org-done)
  4240. 'org-todo))
  4241. (defun org-font-lock-add-tag-faces (limit)
  4242. "Add the special tag faces."
  4243. (when (and org-tag-faces org-tags-special-faces-re)
  4244. (while (re-search-forward org-tags-special-faces-re limit t)
  4245. (add-text-properties (match-beginning 1) (match-end 1)
  4246. (list 'face (org-get-tag-face 1)
  4247. 'font-lock-fontified t))
  4248. (backward-char 1))))
  4249. (defun org-font-lock-add-priority-faces (limit)
  4250. "Add the special priority faces."
  4251. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4252. (add-text-properties
  4253. (match-beginning 0) (match-end 0)
  4254. (list 'face (or (cdr (assoc (char-after (match-beginning 1))
  4255. org-priority-faces))
  4256. 'org-special-keyword)
  4257. 'font-lock-fontified t))))
  4258. (defun org-get-tag-face (kwd)
  4259. "Get the right face for a TODO keyword KWD.
  4260. If KWD is a number, get the corresponding match group."
  4261. (if (numberp kwd) (setq kwd (match-string kwd)))
  4262. (or (cdr (assoc kwd org-tag-faces))
  4263. 'org-tag))
  4264. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4265. "Remove fontification and activation overlays from links."
  4266. (font-lock-default-unfontify-region beg end)
  4267. (let* ((buffer-undo-list t)
  4268. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4269. (inhibit-modification-hooks t)
  4270. deactivate-mark buffer-file-name buffer-file-truename)
  4271. (remove-text-properties beg end
  4272. '(mouse-face t keymap t org-linked-text t
  4273. invisible t intangible t
  4274. line-prefix t wrap-prefix t
  4275. org-no-flyspell t))))
  4276. ;;;; Visibility cycling, including org-goto and indirect buffer
  4277. ;;; Cycling
  4278. (defvar org-cycle-global-status nil)
  4279. (make-variable-buffer-local 'org-cycle-global-status)
  4280. (defvar org-cycle-subtree-status nil)
  4281. (make-variable-buffer-local 'org-cycle-subtree-status)
  4282. ;;;###autoload
  4283. (defvar org-inlinetask-min-level)
  4284. (defun org-cycle (&optional arg)
  4285. "TAB-action and visibility cycling for Org-mode.
  4286. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4287. is outine visibility cycling, but it also invokes other actions
  4288. in special contexts.
  4289. - When this function is called with a prefix argument, rotate the entire
  4290. buffer through 3 states (global cycling)
  4291. 1. OVERVIEW: Show only top-level headlines.
  4292. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4293. 3. SHOW ALL: Show everything.
  4294. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4295. determined by the variable `org-startup-folded', and by any VISIBILITY
  4296. properties in the buffer.
  4297. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4298. including any drawers.
  4299. - When inside a table, re-align the table and move to the next field.
  4300. - When point is at the beginning of a headline, rotate the subtree started
  4301. by this line through 3 different states (local cycling)
  4302. 1. FOLDED: Only the main headline is shown.
  4303. 2. CHILDREN: The main headline and the direct children are shown.
  4304. From this state, you can move to one of the children
  4305. and zoom in further.
  4306. 3. SUBTREE: Show the entire subtree, including body text.
  4307. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4308. - When there is a numeric prefix, go up to a heading with level ARG, do
  4309. a `show-subtree' and return to the previous cursor position. If ARG
  4310. is negative, go up that many levels.
  4311. - When point is not at the beginning of a headline, execute the global
  4312. binding for TAB, which is re-indenting the line. See the option
  4313. `org-cycle-emulate-tab' for details.
  4314. - Special case: if point is at the beginning of the buffer and there is
  4315. no headline in line 1, this function will act as if called with prefix arg.
  4316. But only if also the variable `org-cycle-global-at-bob' is t."
  4317. (interactive "P")
  4318. (org-load-modules-maybe)
  4319. (unless (run-hook-with-args-until-success 'org-tab-first-hook)
  4320. (let* ((limit-level
  4321. (or org-cycle-max-level
  4322. (and (boundp 'org-inlinetask-min-level)
  4323. org-inlinetask-min-level
  4324. (1- org-inlinetask-min-level))))
  4325. (nstars (and limit-level
  4326. (if org-odd-levels-only
  4327. (and limit-level (1- (* limit-level 2)))
  4328. limit-level)))
  4329. (outline-regexp
  4330. (cond
  4331. ((not (org-mode-p)) outline-regexp)
  4332. ((or (eq org-cycle-include-plain-lists 'integrate)
  4333. (and org-cycle-include-plain-lists (org-at-item-p)))
  4334. (concat "\\(?:\\*"
  4335. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4336. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4337. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4338. (bob-special (and org-cycle-global-at-bob (bobp)
  4339. (not (looking-at outline-regexp))))
  4340. (org-cycle-hook
  4341. (if bob-special
  4342. (delq 'org-optimize-window-after-visibility-change
  4343. (copy-sequence org-cycle-hook))
  4344. org-cycle-hook))
  4345. (pos (point)))
  4346. (if (or bob-special (equal arg '(4)))
  4347. ;; special case: use global cycling
  4348. (setq arg t))
  4349. (cond
  4350. ((equal arg '(16))
  4351. (org-set-startup-visibility)
  4352. (message "Startup visibility, plus VISIBILITY properties"))
  4353. ((equal arg '(64))
  4354. (show-all)
  4355. (message "Entire buffer visible, including drawers"))
  4356. ((org-at-table-p 'any)
  4357. ;; Enter the table or move to the next field in the table
  4358. (or (org-table-recognize-table.el)
  4359. (progn
  4360. (if arg (org-table-edit-field t)
  4361. (org-table-justify-field-maybe)
  4362. (call-interactively 'org-table-next-field)))))
  4363. ((run-hook-with-args-until-success
  4364. 'org-tab-after-check-for-table-hook))
  4365. ((eq arg t) ;; Global cycling
  4366. (org-cycle-internal-global))
  4367. ((and org-drawers org-drawer-regexp
  4368. (save-excursion
  4369. (beginning-of-line 1)
  4370. (looking-at org-drawer-regexp)))
  4371. ;; Toggle block visibility
  4372. (org-flag-drawer
  4373. (not (get-char-property (match-end 0) 'invisible))))
  4374. ((integerp arg)
  4375. ;; Show-subtree, ARG levels up from here.
  4376. (save-excursion
  4377. (org-back-to-heading)
  4378. (outline-up-heading (if (< arg 0) (- arg)
  4379. (- (funcall outline-level) arg)))
  4380. (org-show-subtree)))
  4381. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4382. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4383. (org-cycle-internal-local))
  4384. ;; TAB emulation and template completion
  4385. (buffer-read-only (org-back-to-heading))
  4386. ((run-hook-with-args-until-success
  4387. 'org-tab-after-check-for-cycling-hook))
  4388. ((org-try-structure-completion))
  4389. ((org-try-cdlatex-tab))
  4390. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4391. (or (not (bolp))
  4392. (not (looking-at outline-regexp))))
  4393. (call-interactively (global-key-binding "\t")))
  4394. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4395. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4396. (or (and (eq org-cycle-emulate-tab 'white)
  4397. (= (match-end 0) (point-at-eol)))
  4398. (and (eq org-cycle-emulate-tab 'whitestart)
  4399. (>= (match-end 0) pos))))
  4400. t
  4401. (eq org-cycle-emulate-tab t))
  4402. (call-interactively (global-key-binding "\t")))
  4403. (t (save-excursion
  4404. (org-back-to-heading)
  4405. (org-cycle)))))))
  4406. (defun org-cycle-internal-global ()
  4407. "Do the global cycling action."
  4408. (cond
  4409. ((and (eq last-command this-command)
  4410. (eq org-cycle-global-status 'overview))
  4411. ;; We just created the overview - now do table of contents
  4412. ;; This can be slow in very large buffers, so indicate action
  4413. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4414. (message "CONTENTS...")
  4415. (org-content)
  4416. (message "CONTENTS...done")
  4417. (setq org-cycle-global-status 'contents)
  4418. (run-hook-with-args 'org-cycle-hook 'contents))
  4419. ((and (eq last-command this-command)
  4420. (eq org-cycle-global-status 'contents))
  4421. ;; We just showed the table of contents - now show everything
  4422. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4423. (show-all)
  4424. (message "SHOW ALL")
  4425. (setq org-cycle-global-status 'all)
  4426. (run-hook-with-args 'org-cycle-hook 'all))
  4427. (t
  4428. ;; Default action: go to overview
  4429. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4430. (org-overview)
  4431. (message "OVERVIEW")
  4432. (setq org-cycle-global-status 'overview)
  4433. (run-hook-with-args 'org-cycle-hook 'overview))))
  4434. (defun org-cycle-internal-local ()
  4435. "Do the local cycling action."
  4436. (org-back-to-heading)
  4437. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4438. ;; First, some boundaries
  4439. (save-excursion
  4440. (org-back-to-heading)
  4441. (setq level (funcall outline-level))
  4442. (save-excursion
  4443. (beginning-of-line 2)
  4444. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4445. ; XEmacs does not have `next-single-char-property-change'
  4446. ; I'm not sure about Emacs 21.
  4447. (while (and (not (eobp)) ;; this is like `next-line'
  4448. (get-char-property (1- (point)) 'invisible))
  4449. (beginning-of-line 2))
  4450. (while (and (not (eobp)) ;; this is like `next-line'
  4451. (get-char-property (1- (point)) 'invisible))
  4452. (goto-char (next-single-char-property-change (point) 'invisible))
  4453. ;;;??? (or (bolp) (beginning-of-line 2))))
  4454. (and (eolp) (beginning-of-line 2))))
  4455. (setq eol (point)))
  4456. (outline-end-of-heading) (setq eoh (point))
  4457. (save-excursion
  4458. (outline-next-heading)
  4459. (setq has-children (and (org-at-heading-p t)
  4460. (> (funcall outline-level) level))))
  4461. (org-end-of-subtree t)
  4462. (unless (eobp)
  4463. (skip-chars-forward " \t\n")
  4464. (beginning-of-line 1) ; in case this is an item
  4465. )
  4466. (setq eos (if (eobp) (point) (1- (point)))))
  4467. ;; Find out what to do next and set `this-command'
  4468. (cond
  4469. ((= eos eoh)
  4470. ;; Nothing is hidden behind this heading
  4471. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4472. (message "EMPTY ENTRY")
  4473. (setq org-cycle-subtree-status nil)
  4474. (save-excursion
  4475. (goto-char eos)
  4476. (outline-next-heading)
  4477. (if (org-invisible-p) (org-flag-heading nil))))
  4478. ((and (or (>= eol eos)
  4479. (not (string-match "\\S-" (buffer-substring eol eos))))
  4480. (or has-children
  4481. (not (setq children-skipped
  4482. org-cycle-skip-children-state-if-no-children))))
  4483. ;; Entire subtree is hidden in one line: children view
  4484. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4485. (org-show-entry)
  4486. (show-children)
  4487. (message "CHILDREN")
  4488. (save-excursion
  4489. (goto-char eos)
  4490. (outline-next-heading)
  4491. (if (org-invisible-p) (org-flag-heading nil)))
  4492. (setq org-cycle-subtree-status 'children)
  4493. (run-hook-with-args 'org-cycle-hook 'children))
  4494. ((or children-skipped
  4495. (and (eq last-command this-command)
  4496. (eq org-cycle-subtree-status 'children)))
  4497. ;; We just showed the children, or no children are there,
  4498. ;; now show everything.
  4499. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4500. (org-show-subtree)
  4501. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4502. (setq org-cycle-subtree-status 'subtree)
  4503. (run-hook-with-args 'org-cycle-hook 'subtree))
  4504. (t
  4505. ;; Default action: hide the subtree.
  4506. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4507. (hide-subtree)
  4508. (message "FOLDED")
  4509. (setq org-cycle-subtree-status 'folded)
  4510. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4511. ;;;###autoload
  4512. (defun org-global-cycle (&optional arg)
  4513. "Cycle the global visibility. For details see `org-cycle'.
  4514. With C-u prefix arg, switch to startup visibility.
  4515. With a numeric prefix, show all headlines up to that level."
  4516. (interactive "P")
  4517. (let ((org-cycle-include-plain-lists
  4518. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4519. (cond
  4520. ((integerp arg)
  4521. (show-all)
  4522. (hide-sublevels arg)
  4523. (setq org-cycle-global-status 'contents))
  4524. ((equal arg '(4))
  4525. (org-set-startup-visibility)
  4526. (message "Startup visibility, plus VISIBILITY properties."))
  4527. (t
  4528. (org-cycle '(4))))))
  4529. (defun org-set-startup-visibility ()
  4530. "Set the visibility required by startup options and properties."
  4531. (cond
  4532. ((eq org-startup-folded t)
  4533. (org-cycle '(4)))
  4534. ((eq org-startup-folded 'content)
  4535. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4536. (org-cycle '(4)) (org-cycle '(4)))))
  4537. (if org-hide-block-startup (org-hide-block-all))
  4538. (org-set-visibility-according-to-property 'no-cleanup)
  4539. (org-cycle-hide-archived-subtrees 'all)
  4540. (org-cycle-hide-drawers 'all)
  4541. (org-cycle-show-empty-lines 'all))
  4542. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4543. "Switch subtree visibilities according to :VISIBILITY: property."
  4544. (interactive)
  4545. (let (org-show-entry-below state)
  4546. (save-excursion
  4547. (goto-char (point-min))
  4548. (while (re-search-forward
  4549. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4550. nil t)
  4551. (setq state (match-string 1))
  4552. (save-excursion
  4553. (org-back-to-heading t)
  4554. (hide-subtree)
  4555. (org-reveal)
  4556. (cond
  4557. ((equal state '("fold" "folded"))
  4558. (hide-subtree))
  4559. ((equal state "children")
  4560. (org-show-hidden-entry)
  4561. (show-children))
  4562. ((equal state "content")
  4563. (save-excursion
  4564. (save-restriction
  4565. (org-narrow-to-subtree)
  4566. (org-content))))
  4567. ((member state '("all" "showall"))
  4568. (show-subtree)))))
  4569. (unless no-cleanup
  4570. (org-cycle-hide-archived-subtrees 'all)
  4571. (org-cycle-hide-drawers 'all)
  4572. (org-cycle-show-empty-lines 'all)))))
  4573. (defun org-overview ()
  4574. "Switch to overview mode, showing only top-level headlines.
  4575. Really, this shows all headlines with level equal or greater than the level
  4576. of the first headline in the buffer. This is important, because if the
  4577. first headline is not level one, then (hide-sublevels 1) gives confusing
  4578. results."
  4579. (interactive)
  4580. (let ((level (save-excursion
  4581. (goto-char (point-min))
  4582. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4583. (progn
  4584. (goto-char (match-beginning 0))
  4585. (funcall outline-level))))))
  4586. (and level (hide-sublevels level))))
  4587. (defun org-content (&optional arg)
  4588. "Show all headlines in the buffer, like a table of contents.
  4589. With numerical argument N, show content up to level N."
  4590. (interactive "P")
  4591. (save-excursion
  4592. ;; Visit all headings and show their offspring
  4593. (and (integerp arg) (org-overview))
  4594. (goto-char (point-max))
  4595. (catch 'exit
  4596. (while (and (progn (condition-case nil
  4597. (outline-previous-visible-heading 1)
  4598. (error (goto-char (point-min))))
  4599. t)
  4600. (looking-at outline-regexp))
  4601. (if (integerp arg)
  4602. (show-children (1- arg))
  4603. (show-branches))
  4604. (if (bobp) (throw 'exit nil))))))
  4605. (defun org-optimize-window-after-visibility-change (state)
  4606. "Adjust the window after a change in outline visibility.
  4607. This function is the default value of the hook `org-cycle-hook'."
  4608. (when (get-buffer-window (current-buffer))
  4609. (cond
  4610. ((eq state 'content) nil)
  4611. ((eq state 'all) nil)
  4612. ((eq state 'folded) nil)
  4613. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4614. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4615. ;; FIXME: no longer in use
  4616. (defun org-compact-display-after-subtree-move ()
  4617. "Show a compacter version of the tree of the entry's parent."
  4618. (save-excursion
  4619. (if (org-up-heading-safe)
  4620. (progn
  4621. (hide-subtree)
  4622. (show-entry)
  4623. (show-children)
  4624. (org-cycle-show-empty-lines 'children)
  4625. (org-cycle-hide-drawers 'children))
  4626. (org-overview))))
  4627. (defun org-remove-empty-overlays-at (pos)
  4628. "Remove outline overlays that do not contain non-white stuff."
  4629. (mapc
  4630. (lambda (o)
  4631. (and (eq 'outline (org-overlay-get o 'invisible))
  4632. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  4633. (org-overlay-end o))))
  4634. (org-delete-overlay o)))
  4635. (org-overlays-at pos)))
  4636. (defun org-clean-visibility-after-subtree-move ()
  4637. "Fix visibility issues after moving a subtree."
  4638. ;; First, find a reasonable region to look at:
  4639. ;; Start two siblings above, end three below
  4640. (let* ((beg (save-excursion
  4641. (and (org-get-last-sibling)
  4642. (org-get-last-sibling))
  4643. (point)))
  4644. (end (save-excursion
  4645. (and (org-get-next-sibling)
  4646. (org-get-next-sibling)
  4647. (org-get-next-sibling))
  4648. (if (org-at-heading-p)
  4649. (point-at-eol)
  4650. (point))))
  4651. (level (looking-at "\\*+"))
  4652. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  4653. (save-excursion
  4654. (save-restriction
  4655. (narrow-to-region beg end)
  4656. (when re
  4657. ;; Properly fold already folded siblings
  4658. (goto-char (point-min))
  4659. (while (re-search-forward re nil t)
  4660. (if (save-excursion (goto-char (point-at-eol)) (org-invisible-p))
  4661. (hide-entry))))
  4662. (org-cycle-show-empty-lines 'overview)
  4663. (org-cycle-hide-drawers 'overview)))))
  4664. (defun org-cycle-show-empty-lines (state)
  4665. "Show empty lines above all visible headlines.
  4666. The region to be covered depends on STATE when called through
  4667. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4668. entire buffer covered. Note that an empty line is only shown if there
  4669. are at least `org-cycle-separator-lines' empty lines before the headline."
  4670. (when (not (= org-cycle-separator-lines 0))
  4671. (save-excursion
  4672. (let* ((n (abs org-cycle-separator-lines))
  4673. (re (cond
  4674. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4675. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4676. (t (let ((ns (number-to-string (- n 2))))
  4677. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4678. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4679. beg end b e)
  4680. (cond
  4681. ((memq state '(overview contents t))
  4682. (setq beg (point-min) end (point-max)))
  4683. ((memq state '(children folded))
  4684. (setq beg (point) end (progn (org-end-of-subtree t t)
  4685. (beginning-of-line 2)
  4686. (point)))))
  4687. (when beg
  4688. (goto-char beg)
  4689. (while (re-search-forward re end t)
  4690. (unless (get-char-property (match-end 1) 'invisible)
  4691. (setq e (match-end 1))
  4692. (if (< org-cycle-separator-lines 0)
  4693. (setq b (save-excursion
  4694. (goto-char (match-beginning 0))
  4695. (org-back-over-empty-lines)
  4696. (if (save-excursion
  4697. (goto-char (max (point-min) (1- (point))))
  4698. (org-on-heading-p))
  4699. (1- (point))
  4700. (point))))
  4701. (setq b (match-beginning 1)))
  4702. (outline-flag-region b e nil)))))))
  4703. ;; Never hide empty lines at the end of the file.
  4704. (save-excursion
  4705. (goto-char (point-max))
  4706. (outline-previous-heading)
  4707. (outline-end-of-heading)
  4708. (if (and (looking-at "[ \t\n]+")
  4709. (= (match-end 0) (point-max)))
  4710. (outline-flag-region (point) (match-end 0) nil))))
  4711. (defun org-show-empty-lines-in-parent ()
  4712. "Move to the parent and re-show empty lines before visible headlines."
  4713. (save-excursion
  4714. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4715. (org-cycle-show-empty-lines context))))
  4716. (defun org-cycle-hide-drawers (state)
  4717. "Re-hide all drawers after a visibility state change."
  4718. (when (and (org-mode-p)
  4719. (not (memq state '(overview folded contents))))
  4720. (save-excursion
  4721. (let* ((globalp (memq state '(contents all)))
  4722. (beg (if globalp (point-min) (point)))
  4723. (end (if globalp (point-max)
  4724. (if (eq state 'children)
  4725. (save-excursion (outline-next-heading) (point))
  4726. (org-end-of-subtree t)))))
  4727. (goto-char beg)
  4728. (while (re-search-forward org-drawer-regexp end t)
  4729. (org-flag-drawer t))))))
  4730. (defun org-flag-drawer (flag)
  4731. (save-excursion
  4732. (beginning-of-line 1)
  4733. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4734. (let ((b (match-end 0))
  4735. (outline-regexp org-outline-regexp))
  4736. (if (re-search-forward
  4737. "^[ \t]*:END:"
  4738. (save-excursion (outline-next-heading) (point)) t)
  4739. (outline-flag-region b (point-at-eol) flag)
  4740. (error ":END: line missing at position %s" b))))))
  4741. (defun org-subtree-end-visible-p ()
  4742. "Is the end of the current subtree visible?"
  4743. (pos-visible-in-window-p
  4744. (save-excursion (org-end-of-subtree t) (point))))
  4745. (defun org-first-headline-recenter (&optional N)
  4746. "Move cursor to the first headline and recenter the headline.
  4747. Optional argument N means, put the headline into the Nth line of the window."
  4748. (goto-char (point-min))
  4749. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4750. (beginning-of-line)
  4751. (recenter (prefix-numeric-value N))))
  4752. ;;; Folding of blocks
  4753. (defconst org-block-regexp
  4754. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  4755. "Regular expression for hiding blocks.")
  4756. (defvar org-hide-block-overlays nil
  4757. "Overays hiding blocks.")
  4758. (make-variable-buffer-local 'org-hide-block-overlays)
  4759. (defun org-block-map (function &optional start end)
  4760. "Call func at the head of all source blocks in the current
  4761. buffer. Optional arguments START and END can be used to limit
  4762. the range."
  4763. (let ((start (or start (point-min)))
  4764. (end (or end (point-max))))
  4765. (save-excursion
  4766. (goto-char start)
  4767. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  4768. (save-excursion
  4769. (save-match-data
  4770. (goto-char (match-beginning 0))
  4771. (funcall function)))))))
  4772. (defun org-hide-block-toggle-all ()
  4773. "Toggle the visibility of all blocks in the current buffer."
  4774. (org-block-map #'org-hide-block-toggle))
  4775. (defun org-hide-block-all ()
  4776. "Fold all blocks in the current buffer."
  4777. (interactive)
  4778. (org-show-block-all)
  4779. (org-block-map #'org-hide-block-toggle-maybe))
  4780. (defun org-show-block-all ()
  4781. "Unfold all blocks in the current buffer."
  4782. (mapc 'org-delete-overlay org-hide-block-overlays)
  4783. (setq org-hide-block-overlays nil))
  4784. (defun org-hide-block-toggle-maybe ()
  4785. "Toggle visibility of block at point."
  4786. (interactive)
  4787. (let ((case-fold-search t))
  4788. (if (save-excursion
  4789. (beginning-of-line 1)
  4790. (looking-at org-block-regexp))
  4791. (progn (org-hide-block-toggle)
  4792. t) ;; to signal that we took action
  4793. nil))) ;; to signal that we did not
  4794. (defun org-hide-block-toggle (&optional force)
  4795. "Toggle the visibility of the current block."
  4796. (interactive)
  4797. (save-excursion
  4798. (beginning-of-line)
  4799. (if (re-search-forward org-block-regexp nil t)
  4800. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  4801. (end (match-end 0)) ;; end of entire body
  4802. ov)
  4803. (if (memq t (mapcar (lambda (overlay)
  4804. (eq (org-overlay-get overlay 'invisible)
  4805. 'org-hide-block))
  4806. (org-overlays-at start)))
  4807. (if (or (not force) (eq force 'off))
  4808. (mapc (lambda (ov)
  4809. (when (member ov org-hide-block-overlays)
  4810. (setq org-hide-block-overlays
  4811. (delq ov org-hide-block-overlays)))
  4812. (when (eq (org-overlay-get ov 'invisible)
  4813. 'org-hide-block)
  4814. (org-delete-overlay ov)))
  4815. (org-overlays-at start)))
  4816. (setq ov (org-make-overlay start end))
  4817. (org-overlay-put ov 'invisible 'org-hide-block)
  4818. ;; make the block accessible to isearch
  4819. (org-overlay-put
  4820. ov 'isearch-open-invisible
  4821. (lambda (ov)
  4822. (when (member ov org-hide-block-overlays)
  4823. (setq org-hide-block-overlays
  4824. (delq ov org-hide-block-overlays)))
  4825. (when (eq (org-overlay-get ov 'invisible)
  4826. 'org-hide-block)
  4827. (org-delete-overlay ov))))
  4828. (push ov org-hide-block-overlays)))
  4829. (error "Not looking at a source block"))))
  4830. ;; org-tab-after-check-for-cycling-hook
  4831. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  4832. ;; Remove overlays when changing major mode
  4833. (add-hook 'org-mode-hook
  4834. (lambda () (org-add-hook 'change-major-mode-hook
  4835. 'org-show-block-all 'append 'local)))
  4836. ;;; Org-goto
  4837. (defvar org-goto-window-configuration nil)
  4838. (defvar org-goto-marker nil)
  4839. (defvar org-goto-map
  4840. (let ((map (make-sparse-keymap)))
  4841. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4842. (while (setq cmd (pop cmds))
  4843. (substitute-key-definition cmd cmd map global-map)))
  4844. (suppress-keymap map)
  4845. (org-defkey map "\C-m" 'org-goto-ret)
  4846. (org-defkey map [(return)] 'org-goto-ret)
  4847. (org-defkey map [(left)] 'org-goto-left)
  4848. (org-defkey map [(right)] 'org-goto-right)
  4849. (org-defkey map [(control ?g)] 'org-goto-quit)
  4850. (org-defkey map "\C-i" 'org-cycle)
  4851. (org-defkey map [(tab)] 'org-cycle)
  4852. (org-defkey map [(down)] 'outline-next-visible-heading)
  4853. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4854. (if org-goto-auto-isearch
  4855. (if (fboundp 'define-key-after)
  4856. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4857. nil)
  4858. (org-defkey map "q" 'org-goto-quit)
  4859. (org-defkey map "n" 'outline-next-visible-heading)
  4860. (org-defkey map "p" 'outline-previous-visible-heading)
  4861. (org-defkey map "f" 'outline-forward-same-level)
  4862. (org-defkey map "b" 'outline-backward-same-level)
  4863. (org-defkey map "u" 'outline-up-heading))
  4864. (org-defkey map "/" 'org-occur)
  4865. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4866. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4867. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4868. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4869. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4870. map))
  4871. (defconst org-goto-help
  4872. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4873. RET=jump to location [Q]uit and return to previous location
  4874. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4875. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4876. ;; FIXME: Docstring doe not mention both interfaces
  4877. (defun org-goto (&optional alternative-interface)
  4878. "Look up a different location in the current file, keeping current visibility.
  4879. When you want look-up or go to a different location in a document, the
  4880. fastest way is often to fold the entire buffer and then dive into the tree.
  4881. This method has the disadvantage, that the previous location will be folded,
  4882. which may not be what you want.
  4883. This command works around this by showing a copy of the current buffer
  4884. in an indirect buffer, in overview mode. You can dive into the tree in
  4885. that copy, use org-occur and incremental search to find a location.
  4886. When pressing RET or `Q', the command returns to the original buffer in
  4887. which the visibility is still unchanged. After RET is will also jump to
  4888. the location selected in the indirect buffer and expose the
  4889. the headline hierarchy above."
  4890. (interactive "P")
  4891. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4892. (org-refile-use-outline-path t)
  4893. (org-refile-target-verify-function nil)
  4894. (interface
  4895. (if (not alternative-interface)
  4896. org-goto-interface
  4897. (if (eq org-goto-interface 'outline)
  4898. 'outline-path-completion
  4899. 'outline)))
  4900. (org-goto-start-pos (point))
  4901. (selected-point
  4902. (if (eq interface 'outline)
  4903. (car (org-get-location (current-buffer) org-goto-help))
  4904. (nth 3 (org-refile-get-location "Goto: ")))))
  4905. (if selected-point
  4906. (progn
  4907. (org-mark-ring-push org-goto-start-pos)
  4908. (goto-char selected-point)
  4909. (if (or (org-invisible-p) (org-invisible-p2))
  4910. (org-show-context 'org-goto)))
  4911. (message "Quit"))))
  4912. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4913. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4914. (defvar org-goto-local-auto-isearch-map) ; defined below
  4915. (defun org-get-location (buf help)
  4916. "Let the user select a location in the Org-mode buffer BUF.
  4917. This function uses a recursive edit. It returns the selected position
  4918. or nil."
  4919. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4920. (isearch-hide-immediately nil)
  4921. (isearch-search-fun-function
  4922. (lambda () 'org-goto-local-search-headings))
  4923. (org-goto-selected-point org-goto-exit-command))
  4924. (save-excursion
  4925. (save-window-excursion
  4926. (delete-other-windows)
  4927. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4928. (switch-to-buffer
  4929. (condition-case nil
  4930. (make-indirect-buffer (current-buffer) "*org-goto*")
  4931. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4932. (with-output-to-temp-buffer "*Help*"
  4933. (princ help))
  4934. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4935. (setq buffer-read-only nil)
  4936. (let ((org-startup-truncated t)
  4937. (org-startup-folded nil)
  4938. (org-startup-align-all-tables nil))
  4939. (org-mode)
  4940. (org-overview))
  4941. (setq buffer-read-only t)
  4942. (if (and (boundp 'org-goto-start-pos)
  4943. (integer-or-marker-p org-goto-start-pos))
  4944. (let ((org-show-hierarchy-above t)
  4945. (org-show-siblings t)
  4946. (org-show-following-heading t))
  4947. (goto-char org-goto-start-pos)
  4948. (and (org-invisible-p) (org-show-context)))
  4949. (goto-char (point-min)))
  4950. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4951. (message "Select location and press RET")
  4952. (use-local-map org-goto-map)
  4953. (recursive-edit)
  4954. ))
  4955. (kill-buffer "*org-goto*")
  4956. (cons org-goto-selected-point org-goto-exit-command)))
  4957. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4958. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4959. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4960. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4961. (defun org-goto-local-search-headings (string bound noerror)
  4962. "Search and make sure that any matches are in headlines."
  4963. (catch 'return
  4964. (while (if isearch-forward
  4965. (search-forward string bound noerror)
  4966. (search-backward string bound noerror))
  4967. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4968. (and (member :headline context)
  4969. (not (member :tags context))))
  4970. (throw 'return (point))))))
  4971. (defun org-goto-local-auto-isearch ()
  4972. "Start isearch."
  4973. (interactive)
  4974. (goto-char (point-min))
  4975. (let ((keys (this-command-keys)))
  4976. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4977. (isearch-mode t)
  4978. (isearch-process-search-char (string-to-char keys)))))
  4979. (defun org-goto-ret (&optional arg)
  4980. "Finish `org-goto' by going to the new location."
  4981. (interactive "P")
  4982. (setq org-goto-selected-point (point)
  4983. org-goto-exit-command 'return)
  4984. (throw 'exit nil))
  4985. (defun org-goto-left ()
  4986. "Finish `org-goto' by going to the new location."
  4987. (interactive)
  4988. (if (org-on-heading-p)
  4989. (progn
  4990. (beginning-of-line 1)
  4991. (setq org-goto-selected-point (point)
  4992. org-goto-exit-command 'left)
  4993. (throw 'exit nil))
  4994. (error "Not on a heading")))
  4995. (defun org-goto-right ()
  4996. "Finish `org-goto' by going to the new location."
  4997. (interactive)
  4998. (if (org-on-heading-p)
  4999. (progn
  5000. (setq org-goto-selected-point (point)
  5001. org-goto-exit-command 'right)
  5002. (throw 'exit nil))
  5003. (error "Not on a heading")))
  5004. (defun org-goto-quit ()
  5005. "Finish `org-goto' without cursor motion."
  5006. (interactive)
  5007. (setq org-goto-selected-point nil)
  5008. (setq org-goto-exit-command 'quit)
  5009. (throw 'exit nil))
  5010. ;;; Indirect buffer display of subtrees
  5011. (defvar org-indirect-dedicated-frame nil
  5012. "This is the frame being used for indirect tree display.")
  5013. (defvar org-last-indirect-buffer nil)
  5014. (defun org-tree-to-indirect-buffer (&optional arg)
  5015. "Create indirect buffer and narrow it to current subtree.
  5016. With numerical prefix ARG, go up to this level and then take that tree.
  5017. If ARG is negative, go up that many levels.
  5018. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5019. indirect buffer previously made with this command, to avoid proliferation of
  5020. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5021. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5022. is kept so that you can work with several indirect buffers at the same time.
  5023. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5024. requests that a new frame be made for the new buffer, so that the dedicated
  5025. frame is not changed."
  5026. (interactive "P")
  5027. (let ((cbuf (current-buffer))
  5028. (cwin (selected-window))
  5029. (pos (point))
  5030. beg end level heading ibuf)
  5031. (save-excursion
  5032. (org-back-to-heading t)
  5033. (when (numberp arg)
  5034. (setq level (org-outline-level))
  5035. (if (< arg 0) (setq arg (+ level arg)))
  5036. (while (> (setq level (org-outline-level)) arg)
  5037. (outline-up-heading 1 t)))
  5038. (setq beg (point)
  5039. heading (org-get-heading))
  5040. (org-end-of-subtree t) (setq end (point)))
  5041. (if (and (buffer-live-p org-last-indirect-buffer)
  5042. (not (eq org-indirect-buffer-display 'new-frame))
  5043. (not arg))
  5044. (kill-buffer org-last-indirect-buffer))
  5045. (setq ibuf (org-get-indirect-buffer cbuf)
  5046. org-last-indirect-buffer ibuf)
  5047. (cond
  5048. ((or (eq org-indirect-buffer-display 'new-frame)
  5049. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5050. (select-frame (make-frame))
  5051. (delete-other-windows)
  5052. (switch-to-buffer ibuf)
  5053. (org-set-frame-title heading))
  5054. ((eq org-indirect-buffer-display 'dedicated-frame)
  5055. (raise-frame
  5056. (select-frame (or (and org-indirect-dedicated-frame
  5057. (frame-live-p org-indirect-dedicated-frame)
  5058. org-indirect-dedicated-frame)
  5059. (setq org-indirect-dedicated-frame (make-frame)))))
  5060. (delete-other-windows)
  5061. (switch-to-buffer ibuf)
  5062. (org-set-frame-title (concat "Indirect: " heading)))
  5063. ((eq org-indirect-buffer-display 'current-window)
  5064. (switch-to-buffer ibuf))
  5065. ((eq org-indirect-buffer-display 'other-window)
  5066. (pop-to-buffer ibuf))
  5067. (t (error "Invalid value.")))
  5068. (if (featurep 'xemacs)
  5069. (save-excursion (org-mode) (turn-on-font-lock)))
  5070. (narrow-to-region beg end)
  5071. (show-all)
  5072. (goto-char pos)
  5073. (and (window-live-p cwin) (select-window cwin))))
  5074. (defun org-get-indirect-buffer (&optional buffer)
  5075. (setq buffer (or buffer (current-buffer)))
  5076. (let ((n 1) (base (buffer-name buffer)) bname)
  5077. (while (buffer-live-p
  5078. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5079. (setq n (1+ n)))
  5080. (condition-case nil
  5081. (make-indirect-buffer buffer bname 'clone)
  5082. (error (make-indirect-buffer buffer bname)))))
  5083. (defun org-set-frame-title (title)
  5084. "Set the title of the current frame to the string TITLE."
  5085. ;; FIXME: how to name a single frame in XEmacs???
  5086. (unless (featurep 'xemacs)
  5087. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5088. ;;;; Structure editing
  5089. ;;; Inserting headlines
  5090. (defun org-previous-line-empty-p ()
  5091. (save-excursion
  5092. (and (not (bobp))
  5093. (or (beginning-of-line 0) t)
  5094. (save-match-data
  5095. (looking-at "[ \t]*$")))))
  5096. (defun org-insert-heading (&optional force-heading)
  5097. "Insert a new heading or item with same depth at point.
  5098. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5099. If point is at the beginning of a headline, insert a sibling before the
  5100. current headline. If point is not at the beginning, do not split the line,
  5101. but create the new headline after the current line."
  5102. (interactive "P")
  5103. (if (= (buffer-size) 0)
  5104. (insert "\n* ")
  5105. (when (or force-heading (not (org-insert-item)))
  5106. (let* ((empty-line-p nil)
  5107. (head (save-excursion
  5108. (condition-case nil
  5109. (progn
  5110. (org-back-to-heading)
  5111. (setq empty-line-p (org-previous-line-empty-p))
  5112. (match-string 0))
  5113. (error "*"))))
  5114. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5115. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5116. pos hide-previous previous-pos)
  5117. (cond
  5118. ((and (org-on-heading-p) (bolp)
  5119. (or (bobp)
  5120. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5121. ;; insert before the current line
  5122. (open-line (if blank 2 1)))
  5123. ((and (bolp)
  5124. (not org-insert-heading-respect-content)
  5125. (or (bobp)
  5126. (save-excursion
  5127. (backward-char 1) (not (org-invisible-p)))))
  5128. ;; insert right here
  5129. nil)
  5130. (t
  5131. ;; somewhere in the line
  5132. (save-excursion
  5133. (setq previous-pos (point-at-bol))
  5134. (end-of-line)
  5135. (setq hide-previous (org-invisible-p)))
  5136. (and org-insert-heading-respect-content (org-show-subtree))
  5137. (let ((split
  5138. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5139. (save-excursion
  5140. (let ((p (point)))
  5141. (goto-char (point-at-bol))
  5142. (and (looking-at org-complex-heading-regexp)
  5143. (> p (match-beginning 4)))))))
  5144. tags pos)
  5145. (cond
  5146. (org-insert-heading-respect-content
  5147. (org-end-of-subtree nil t)
  5148. (or (bolp) (newline))
  5149. (or (org-previous-line-empty-p)
  5150. (and blank (newline)))
  5151. (open-line 1))
  5152. ((org-on-heading-p)
  5153. (when hide-previous
  5154. (show-children)
  5155. (org-show-entry))
  5156. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5157. (setq tags (and (match-end 2) (match-string 2)))
  5158. (and (match-end 1)
  5159. (delete-region (match-beginning 1) (match-end 1)))
  5160. (setq pos (point-at-bol))
  5161. (or split (end-of-line 1))
  5162. (delete-horizontal-space)
  5163. (newline (if blank 2 1))
  5164. (when tags
  5165. (save-excursion
  5166. (goto-char pos)
  5167. (end-of-line 1)
  5168. (insert " " tags)
  5169. (org-set-tags nil 'align))))
  5170. (t
  5171. (or split (end-of-line 1))
  5172. (newline (if blank 2 1)))))))
  5173. (insert head) (just-one-space)
  5174. (setq pos (point))
  5175. (end-of-line 1)
  5176. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5177. (when (and org-insert-heading-respect-content hide-previous)
  5178. (save-excursion
  5179. (goto-char previous-pos)
  5180. (hide-subtree)))
  5181. (run-hooks 'org-insert-heading-hook)))))
  5182. (defun org-get-heading (&optional no-tags)
  5183. "Return the heading of the current entry, without the stars."
  5184. (save-excursion
  5185. (org-back-to-heading t)
  5186. (if (looking-at
  5187. (if no-tags
  5188. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5189. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5190. (match-string 1) "")))
  5191. (defun org-heading-components ()
  5192. "Return the components of the current heading.
  5193. This is a list with the following elements:
  5194. - the level as an integer
  5195. - the reduced level, different if `org-odd-levels-only' is set.
  5196. - the TODO keyword, or nil
  5197. - the priority character, like ?A, or nil if no priority is given
  5198. - the headline text itself, or the tags string if no headline text
  5199. - the tags string, or nil."
  5200. (save-excursion
  5201. (org-back-to-heading t)
  5202. (if (looking-at org-complex-heading-regexp)
  5203. (list (length (match-string 1))
  5204. (org-reduced-level (length (match-string 1)))
  5205. (org-match-string-no-properties 2)
  5206. (and (match-end 3) (aref (match-string 3) 2))
  5207. (org-match-string-no-properties 4)
  5208. (org-match-string-no-properties 5)))))
  5209. (defun org-get-entry ()
  5210. "Get the entry text, after heading, entire subtree."
  5211. (save-excursion
  5212. (org-back-to-heading t)
  5213. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5214. (defun org-insert-heading-after-current ()
  5215. "Insert a new heading with same level as current, after current subtree."
  5216. (interactive)
  5217. (org-back-to-heading)
  5218. (org-insert-heading)
  5219. (org-move-subtree-down)
  5220. (end-of-line 1))
  5221. (defun org-insert-heading-respect-content ()
  5222. (interactive)
  5223. (let ((org-insert-heading-respect-content t))
  5224. (org-insert-heading t)))
  5225. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5226. (interactive "P")
  5227. (let ((org-insert-heading-respect-content t))
  5228. (org-insert-todo-heading force-state t)))
  5229. (defun org-insert-todo-heading (arg &optional force-heading)
  5230. "Insert a new heading with the same level and TODO state as current heading.
  5231. If the heading has no TODO state, or if the state is DONE, use the first
  5232. state (TODO by default). Also with prefix arg, force first state."
  5233. (interactive "P")
  5234. (when (or force-heading (not (org-insert-item 'checkbox)))
  5235. (org-insert-heading force-heading)
  5236. (save-excursion
  5237. (org-back-to-heading)
  5238. (outline-previous-heading)
  5239. (looking-at org-todo-line-regexp))
  5240. (let*
  5241. ((new-mark-x
  5242. (if (or arg
  5243. (not (match-beginning 2))
  5244. (member (match-string 2) org-done-keywords))
  5245. (car org-todo-keywords-1)
  5246. (match-string 2)))
  5247. (new-mark
  5248. (or
  5249. (run-hook-with-args-until-success
  5250. 'org-todo-get-default-hook new-mark-x nil)
  5251. new-mark-x)))
  5252. (beginning-of-line 1)
  5253. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5254. (if org-treat-insert-todo-heading-as-state-change
  5255. (org-todo new-mark)
  5256. (insert new-mark " "))))
  5257. (when org-provide-todo-statistics
  5258. (org-update-parent-todo-statistics))))
  5259. (defun org-insert-subheading (arg)
  5260. "Insert a new subheading and demote it.
  5261. Works for outline headings and for plain lists alike."
  5262. (interactive "P")
  5263. (org-insert-heading arg)
  5264. (cond
  5265. ((org-on-heading-p) (org-do-demote))
  5266. ((org-at-item-p) (org-indent-item 1))))
  5267. (defun org-insert-todo-subheading (arg)
  5268. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5269. Works for outline headings and for plain lists alike."
  5270. (interactive "P")
  5271. (org-insert-todo-heading arg)
  5272. (cond
  5273. ((org-on-heading-p) (org-do-demote))
  5274. ((org-at-item-p) (org-indent-item 1))))
  5275. ;;; Promotion and Demotion
  5276. (defvar org-after-demote-entry-hook nil
  5277. "Hook run after an entry has been demoted.
  5278. The cursor will be at the beginning of the entry.
  5279. When a subtree is being demoted, the hook will be called for each node.")
  5280. (defvar org-after-promote-entry-hook nil
  5281. "Hook run after an entry has been promoted.
  5282. The cursor will be at the beginning of the entry.
  5283. When a subtree is being promoted, the hook will be called for each node.")
  5284. (defun org-promote-subtree ()
  5285. "Promote the entire subtree.
  5286. See also `org-promote'."
  5287. (interactive)
  5288. (save-excursion
  5289. (org-map-tree 'org-promote))
  5290. (org-fix-position-after-promote))
  5291. (defun org-demote-subtree ()
  5292. "Demote the entire subtree. See `org-demote'.
  5293. See also `org-promote'."
  5294. (interactive)
  5295. (save-excursion
  5296. (org-map-tree 'org-demote))
  5297. (org-fix-position-after-promote))
  5298. (defun org-do-promote ()
  5299. "Promote the current heading higher up the tree.
  5300. If the region is active in `transient-mark-mode', promote all headings
  5301. in the region."
  5302. (interactive)
  5303. (save-excursion
  5304. (if (org-region-active-p)
  5305. (org-map-region 'org-promote (region-beginning) (region-end))
  5306. (org-promote)))
  5307. (org-fix-position-after-promote))
  5308. (defun org-do-demote ()
  5309. "Demote the current heading lower down the tree.
  5310. If the region is active in `transient-mark-mode', demote all headings
  5311. in the region."
  5312. (interactive)
  5313. (save-excursion
  5314. (if (org-region-active-p)
  5315. (org-map-region 'org-demote (region-beginning) (region-end))
  5316. (org-demote)))
  5317. (org-fix-position-after-promote))
  5318. (defun org-fix-position-after-promote ()
  5319. "Make sure that after pro/demotion cursor position is right."
  5320. (let ((pos (point)))
  5321. (when (save-excursion
  5322. (beginning-of-line 1)
  5323. (looking-at org-todo-line-regexp)
  5324. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5325. (cond ((eobp) (insert " "))
  5326. ((eolp) (insert " "))
  5327. ((equal (char-after) ?\ ) (forward-char 1))))))
  5328. (defun org-reduced-level (l)
  5329. "Compute the effective level of a heading.
  5330. This takes into account the setting of `org-odd-levels-only'."
  5331. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5332. (defun org-get-valid-level (level &optional change)
  5333. "Rectify a level change under the influence of `org-odd-levels-only'
  5334. LEVEL is a current level, CHANGE is by how much the level should be
  5335. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5336. even level numbers will become the next higher odd number."
  5337. (if org-odd-levels-only
  5338. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5339. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5340. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5341. (max 1 (+ level (or change 0)))))
  5342. (if (boundp 'define-obsolete-function-alias)
  5343. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5344. (define-obsolete-function-alias 'org-get-legal-level
  5345. 'org-get-valid-level)
  5346. (define-obsolete-function-alias 'org-get-legal-level
  5347. 'org-get-valid-level "23.1")))
  5348. (defun org-promote ()
  5349. "Promote the current heading higher up the tree.
  5350. If the region is active in `transient-mark-mode', promote all headings
  5351. in the region."
  5352. (org-back-to-heading t)
  5353. (let* ((level (save-match-data (funcall outline-level)))
  5354. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5355. (diff (abs (- level (length up-head) -1))))
  5356. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5357. (replace-match up-head nil t)
  5358. ;; Fixup tag positioning
  5359. (and org-auto-align-tags (org-set-tags nil t))
  5360. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5361. (run-hooks 'org-after-promote-entry-hook)))
  5362. (defun org-demote ()
  5363. "Demote the current heading lower down the tree.
  5364. If the region is active in `transient-mark-mode', demote all headings
  5365. in the region."
  5366. (org-back-to-heading t)
  5367. (let* ((level (save-match-data (funcall outline-level)))
  5368. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5369. (diff (abs (- level (length down-head) -1))))
  5370. (replace-match down-head nil t)
  5371. ;; Fixup tag positioning
  5372. (and org-auto-align-tags (org-set-tags nil t))
  5373. (if org-adapt-indentation (org-fixup-indentation diff))
  5374. (run-hooks 'org-after-demote-entry-hook)))
  5375. (defun org-map-tree (fun)
  5376. "Call FUN for every heading underneath the current one."
  5377. (org-back-to-heading)
  5378. (let ((level (funcall outline-level)))
  5379. (save-excursion
  5380. (funcall fun)
  5381. (while (and (progn
  5382. (outline-next-heading)
  5383. (> (funcall outline-level) level))
  5384. (not (eobp)))
  5385. (funcall fun)))))
  5386. (defun org-map-region (fun beg end)
  5387. "Call FUN for every heading between BEG and END."
  5388. (let ((org-ignore-region t))
  5389. (save-excursion
  5390. (setq end (copy-marker end))
  5391. (goto-char beg)
  5392. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5393. (< (point) end))
  5394. (funcall fun))
  5395. (while (and (progn
  5396. (outline-next-heading)
  5397. (< (point) end))
  5398. (not (eobp)))
  5399. (funcall fun)))))
  5400. (defun org-fixup-indentation (diff)
  5401. "Change the indentation in the current entry by DIFF
  5402. However, if any line in the current entry has no indentation, or if it
  5403. would end up with no indentation after the change, nothing at all is done."
  5404. (save-excursion
  5405. (let ((end (save-excursion (outline-next-heading)
  5406. (point-marker)))
  5407. (prohibit (if (> diff 0)
  5408. "^\\S-"
  5409. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5410. col)
  5411. (unless (save-excursion (end-of-line 1)
  5412. (re-search-forward prohibit end t))
  5413. (while (and (< (point) end)
  5414. (re-search-forward "^[ \t]+" end t))
  5415. (goto-char (match-end 0))
  5416. (setq col (current-column))
  5417. (if (< diff 0) (replace-match ""))
  5418. (org-indent-to-column (+ diff col))))
  5419. (move-marker end nil))))
  5420. (defun org-convert-to-odd-levels ()
  5421. "Convert an org-mode file with all levels allowed to one with odd levels.
  5422. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5423. level 5 etc."
  5424. (interactive)
  5425. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5426. (let ((outline-regexp org-outline-regexp)
  5427. (outline-level 'org-outline-level)
  5428. (org-odd-levels-only nil) n)
  5429. (save-excursion
  5430. (goto-char (point-min))
  5431. (while (re-search-forward "^\\*\\*+ " nil t)
  5432. (setq n (- (length (match-string 0)) 2))
  5433. (while (>= (setq n (1- n)) 0)
  5434. (org-demote))
  5435. (end-of-line 1))))))
  5436. (defun org-convert-to-oddeven-levels ()
  5437. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5438. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5439. section with an even level, conversion would destroy the structure of the file. An error
  5440. is signaled in this case."
  5441. (interactive)
  5442. (goto-char (point-min))
  5443. ;; First check if there are no even levels
  5444. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5445. (org-show-context t)
  5446. (error "Not all levels are odd in this file. Conversion not possible."))
  5447. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5448. (let ((outline-regexp org-outline-regexp)
  5449. (outline-level 'org-outline-level)
  5450. (org-odd-levels-only nil) n)
  5451. (save-excursion
  5452. (goto-char (point-min))
  5453. (while (re-search-forward "^\\*\\*+ " nil t)
  5454. (setq n (/ (1- (length (match-string 0))) 2))
  5455. (while (>= (setq n (1- n)) 0)
  5456. (org-promote))
  5457. (end-of-line 1))))))
  5458. (defun org-tr-level (n)
  5459. "Make N odd if required."
  5460. (if org-odd-levels-only (1+ (/ n 2)) n))
  5461. ;;; Vertical tree motion, cutting and pasting of subtrees
  5462. (defun org-move-subtree-up (&optional arg)
  5463. "Move the current subtree up past ARG headlines of the same level."
  5464. (interactive "p")
  5465. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5466. (defun org-move-subtree-down (&optional arg)
  5467. "Move the current subtree down past ARG headlines of the same level."
  5468. (interactive "p")
  5469. (setq arg (prefix-numeric-value arg))
  5470. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  5471. 'org-get-last-sibling))
  5472. (ins-point (make-marker))
  5473. (cnt (abs arg))
  5474. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  5475. ;; Select the tree
  5476. (org-back-to-heading)
  5477. (setq beg0 (point))
  5478. (save-excursion
  5479. (setq ne-beg (org-back-over-empty-lines))
  5480. (setq beg (point)))
  5481. (save-match-data
  5482. (save-excursion (outline-end-of-heading)
  5483. (setq folded (org-invisible-p)))
  5484. (outline-end-of-subtree))
  5485. (outline-next-heading)
  5486. (setq ne-end (org-back-over-empty-lines))
  5487. (setq end (point))
  5488. (goto-char beg0)
  5489. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  5490. ;; include less whitespace
  5491. (save-excursion
  5492. (goto-char beg)
  5493. (forward-line (- ne-beg ne-end))
  5494. (setq beg (point))))
  5495. ;; Find insertion point, with error handling
  5496. (while (> cnt 0)
  5497. (or (and (funcall movfunc) (looking-at outline-regexp))
  5498. (progn (goto-char beg0)
  5499. (error "Cannot move past superior level or buffer limit")))
  5500. (setq cnt (1- cnt)))
  5501. (if (> arg 0)
  5502. ;; Moving forward - still need to move over subtree
  5503. (progn (org-end-of-subtree t t)
  5504. (save-excursion
  5505. (org-back-over-empty-lines)
  5506. (or (bolp) (newline)))))
  5507. (setq ne-ins (org-back-over-empty-lines))
  5508. (move-marker ins-point (point))
  5509. (setq txt (buffer-substring beg end))
  5510. (org-save-markers-in-region beg end)
  5511. (delete-region beg end)
  5512. (org-remove-empty-overlays-at beg)
  5513. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  5514. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  5515. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  5516. (let ((bbb (point)))
  5517. (insert-before-markers txt)
  5518. (org-reinstall-markers-in-region bbb)
  5519. (move-marker ins-point bbb))
  5520. (or (bolp) (insert "\n"))
  5521. (setq ins-end (point))
  5522. (goto-char ins-point)
  5523. (org-skip-whitespace)
  5524. (when (and (< arg 0)
  5525. (org-first-sibling-p)
  5526. (> ne-ins ne-beg))
  5527. ;; Move whitespace back to beginning
  5528. (save-excursion
  5529. (goto-char ins-end)
  5530. (let ((kill-whole-line t))
  5531. (kill-line (- ne-ins ne-beg)) (point)))
  5532. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5533. (move-marker ins-point nil)
  5534. (if folded
  5535. (hide-subtree)
  5536. (org-show-entry)
  5537. (show-children)
  5538. (org-cycle-hide-drawers 'children))
  5539. (org-clean-visibility-after-subtree-move)))
  5540. (defvar org-subtree-clip ""
  5541. "Clipboard for cut and paste of subtrees.
  5542. This is actually only a copy of the kill, because we use the normal kill
  5543. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  5544. (defvar org-subtree-clip-folded nil
  5545. "Was the last copied subtree folded?
  5546. This is used to fold the tree back after pasting.")
  5547. (defun org-cut-subtree (&optional n)
  5548. "Cut the current subtree into the clipboard.
  5549. With prefix arg N, cut this many sequential subtrees.
  5550. This is a short-hand for marking the subtree and then cutting it."
  5551. (interactive "p")
  5552. (org-copy-subtree n 'cut))
  5553. (defun org-copy-subtree (&optional n cut force-store-markers)
  5554. "Cut the current subtree into the clipboard.
  5555. With prefix arg N, cut this many sequential subtrees.
  5556. This is a short-hand for marking the subtree and then copying it.
  5557. If CUT is non-nil, actually cut the subtree.
  5558. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  5559. of some markers in the region, even if CUT is non-nil. This is
  5560. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  5561. (interactive "p")
  5562. (let (beg end folded (beg0 (point)))
  5563. (if (interactive-p)
  5564. (org-back-to-heading nil) ; take what looks like a subtree
  5565. (org-back-to-heading t)) ; take what is really there
  5566. (org-back-over-empty-lines)
  5567. (setq beg (point))
  5568. (skip-chars-forward " \t\r\n")
  5569. (save-match-data
  5570. (save-excursion (outline-end-of-heading)
  5571. (setq folded (org-invisible-p)))
  5572. (condition-case nil
  5573. (org-forward-same-level (1- n) t)
  5574. (error nil))
  5575. (org-end-of-subtree t t))
  5576. (org-back-over-empty-lines)
  5577. (setq end (point))
  5578. (goto-char beg0)
  5579. (when (> end beg)
  5580. (setq org-subtree-clip-folded folded)
  5581. (when (or cut force-store-markers)
  5582. (org-save-markers-in-region beg end))
  5583. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  5584. (setq org-subtree-clip (current-kill 0))
  5585. (message "%s: Subtree(s) with %d characters"
  5586. (if cut "Cut" "Copied")
  5587. (length org-subtree-clip)))))
  5588. (defun org-paste-subtree (&optional level tree for-yank)
  5589. "Paste the clipboard as a subtree, with modification of headline level.
  5590. The entire subtree is promoted or demoted in order to match a new headline
  5591. level.
  5592. If the cursor is at the beginning of a headline, the same level as
  5593. that headline is used to paste the tree
  5594. If not, the new level is derived from the *visible* headings
  5595. before and after the insertion point, and taken to be the inferior headline
  5596. level of the two. So if the previous visible heading is level 3 and the
  5597. next is level 4 (or vice versa), level 4 will be used for insertion.
  5598. This makes sure that the subtree remains an independent subtree and does
  5599. not swallow low level entries.
  5600. You can also force a different level, either by using a numeric prefix
  5601. argument, or by inserting the heading marker by hand. For example, if the
  5602. cursor is after \"*****\", then the tree will be shifted to level 5.
  5603. If optional TREE is given, use this text instead of the kill ring.
  5604. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5605. move back over whitespace before inserting, and move point to the end of
  5606. the inserted text when done."
  5607. (interactive "P")
  5608. (setq tree (or tree (and kill-ring (current-kill 0))))
  5609. (unless (org-kill-is-subtree-p tree)
  5610. (error "%s"
  5611. (substitute-command-keys
  5612. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5613. (let* ((visp (not (org-invisible-p)))
  5614. (txt tree)
  5615. (^re (concat "^\\(" outline-regexp "\\)"))
  5616. (re (concat "\\(" outline-regexp "\\)"))
  5617. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5618. (old-level (if (string-match ^re txt)
  5619. (- (match-end 0) (match-beginning 0) 1)
  5620. -1))
  5621. (force-level (cond (level (prefix-numeric-value level))
  5622. ((and (looking-at "[ \t]*$")
  5623. (string-match
  5624. ^re_ (buffer-substring
  5625. (point-at-bol) (point))))
  5626. (- (match-end 1) (match-beginning 1)))
  5627. ((and (bolp)
  5628. (looking-at org-outline-regexp))
  5629. (- (match-end 0) (point) 1))
  5630. (t nil)))
  5631. (previous-level (save-excursion
  5632. (condition-case nil
  5633. (progn
  5634. (outline-previous-visible-heading 1)
  5635. (if (looking-at re)
  5636. (- (match-end 0) (match-beginning 0) 1)
  5637. 1))
  5638. (error 1))))
  5639. (next-level (save-excursion
  5640. (condition-case nil
  5641. (progn
  5642. (or (looking-at outline-regexp)
  5643. (outline-next-visible-heading 1))
  5644. (if (looking-at re)
  5645. (- (match-end 0) (match-beginning 0) 1)
  5646. 1))
  5647. (error 1))))
  5648. (new-level (or force-level (max previous-level next-level)))
  5649. (shift (if (or (= old-level -1)
  5650. (= new-level -1)
  5651. (= old-level new-level))
  5652. 0
  5653. (- new-level old-level)))
  5654. (delta (if (> shift 0) -1 1))
  5655. (func (if (> shift 0) 'org-demote 'org-promote))
  5656. (org-odd-levels-only nil)
  5657. beg end newend)
  5658. ;; Remove the forced level indicator
  5659. (if force-level
  5660. (delete-region (point-at-bol) (point)))
  5661. ;; Paste
  5662. (beginning-of-line 1)
  5663. (unless for-yank (org-back-over-empty-lines))
  5664. (setq beg (point))
  5665. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5666. (insert-before-markers txt)
  5667. (unless (string-match "\n\\'" txt) (insert "\n"))
  5668. (setq newend (point))
  5669. (org-reinstall-markers-in-region beg)
  5670. (setq end (point))
  5671. (goto-char beg)
  5672. (skip-chars-forward " \t\n\r")
  5673. (setq beg (point))
  5674. (if (and (org-invisible-p) visp)
  5675. (save-excursion (outline-show-heading)))
  5676. ;; Shift if necessary
  5677. (unless (= shift 0)
  5678. (save-restriction
  5679. (narrow-to-region beg end)
  5680. (while (not (= shift 0))
  5681. (org-map-region func (point-min) (point-max))
  5682. (setq shift (+ delta shift)))
  5683. (goto-char (point-min))
  5684. (setq newend (point-max))))
  5685. (when (or (interactive-p) for-yank)
  5686. (message "Clipboard pasted as level %d subtree" new-level))
  5687. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5688. kill-ring
  5689. (eq org-subtree-clip (current-kill 0))
  5690. org-subtree-clip-folded)
  5691. ;; The tree was folded before it was killed/copied
  5692. (hide-subtree))
  5693. (and for-yank (goto-char newend))))
  5694. (defun org-kill-is-subtree-p (&optional txt)
  5695. "Check if the current kill is an outline subtree, or a set of trees.
  5696. Returns nil if kill does not start with a headline, or if the first
  5697. headline level is not the largest headline level in the tree.
  5698. So this will actually accept several entries of equal levels as well,
  5699. which is OK for `org-paste-subtree'.
  5700. If optional TXT is given, check this string instead of the current kill."
  5701. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5702. (start-level (and kill
  5703. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5704. org-outline-regexp "\\)")
  5705. kill)
  5706. (- (match-end 2) (match-beginning 2) 1)))
  5707. (re (concat "^" org-outline-regexp))
  5708. (start (1+ (or (match-beginning 2) -1))))
  5709. (if (not start-level)
  5710. (progn
  5711. nil) ;; does not even start with a heading
  5712. (catch 'exit
  5713. (while (setq start (string-match re kill (1+ start)))
  5714. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5715. (throw 'exit nil)))
  5716. t))))
  5717. (defvar org-markers-to-move nil
  5718. "Markers that should be moved with a cut-and-paste operation.
  5719. Those markers are stored together with their positions relative to
  5720. the start of the region.")
  5721. (defun org-save-markers-in-region (beg end)
  5722. "Check markers in region.
  5723. If these markers are between BEG and END, record their position relative
  5724. to BEG, so that after moving the block of text, we can put the markers back
  5725. into place.
  5726. This function gets called just before an entry or tree gets cut from the
  5727. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5728. called immediately, to move the markers with the entries."
  5729. (setq org-markers-to-move nil)
  5730. (when (featurep 'org-clock)
  5731. (org-clock-save-markers-for-cut-and-paste beg end))
  5732. (when (featurep 'org-agenda)
  5733. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5734. (defun org-check-and-save-marker (marker beg end)
  5735. "Check if MARKER is between BEG and END.
  5736. If yes, remember the marker and the distance to BEG."
  5737. (when (and (marker-buffer marker)
  5738. (equal (marker-buffer marker) (current-buffer)))
  5739. (if (and (>= marker beg) (< marker end))
  5740. (push (cons marker (- marker beg)) org-markers-to-move))))
  5741. (defun org-reinstall-markers-in-region (beg)
  5742. "Move all remembered markers to their position relative to BEG."
  5743. (mapc (lambda (x)
  5744. (move-marker (car x) (+ beg (cdr x))))
  5745. org-markers-to-move)
  5746. (setq org-markers-to-move nil))
  5747. (defun org-narrow-to-subtree ()
  5748. "Narrow buffer to the current subtree."
  5749. (interactive)
  5750. (save-excursion
  5751. (save-match-data
  5752. (narrow-to-region
  5753. (progn (org-back-to-heading t) (point))
  5754. (progn (org-end-of-subtree t) (point))))))
  5755. (defun org-clone-subtree-with-time-shift (n &optional shift)
  5756. "Clone the task (subtree) at point N times.
  5757. The clones will be inserted as siblings.
  5758. In interactive use, the user will be prompted for the number of clones
  5759. to be produced, and for a time SHIFT, which may be a repeater as used
  5760. in time stamps, for example `+3d'.
  5761. When a valid repeater is given and the entry contains any time stamps,
  5762. the clones will become a sequence in time, with time stamps in the
  5763. subtree shifted for each clone produced. If SHIFT is nil or the
  5764. empty string, time stamps will be left alone.
  5765. If the original subtree did contain time stamps with a repeater,
  5766. the following will happen:
  5767. - the repeater will be removed in each clone
  5768. - an additional clone will be produced, with the current, unshifted
  5769. date(s) in the entry.
  5770. - the original entry will be placed *after* all the clones, with
  5771. repeater intact.
  5772. - the start days in the repeater in the original entry will be shifted
  5773. to past the last clone.
  5774. I this way you can spell out a number of instances of a repeating task,
  5775. and still retain the repeater to cover future instances of the task."
  5776. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  5777. (let (beg end template task
  5778. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  5779. (if (not (and (integerp n) (> n 0)))
  5780. (error "Invalid number of replications %s" n))
  5781. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  5782. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  5783. shift)))
  5784. (error "Invalid shift specification %s" shift))
  5785. (when doshift
  5786. (setq shift-n (string-to-number (match-string 1 shift))
  5787. shift-what (cdr (assoc (match-string 2 shift)
  5788. '(("d" . day) ("w" . week)
  5789. ("m" . month) ("y" . year))))))
  5790. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  5791. (setq nmin 1 nmax n)
  5792. (org-back-to-heading t)
  5793. (setq beg (point))
  5794. (org-end-of-subtree t t)
  5795. (setq end (point))
  5796. (setq template (buffer-substring beg end))
  5797. (when (and doshift
  5798. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  5799. (delete-region beg end)
  5800. (setq end beg)
  5801. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  5802. (goto-char end)
  5803. (loop for n from nmin to nmax do
  5804. (if (not doshift)
  5805. (setq task template)
  5806. (with-temp-buffer
  5807. (insert template)
  5808. (org-mode)
  5809. (goto-char (point-min))
  5810. (while (re-search-forward org-ts-regexp-both nil t)
  5811. (org-timestamp-change (* n shift-n) shift-what))
  5812. (unless (= n n-no-remove)
  5813. (goto-char (point-min))
  5814. (while (re-search-forward org-ts-regexp nil t)
  5815. (save-excursion
  5816. (goto-char (match-beginning 0))
  5817. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  5818. (delete-region (match-beginning 1) (match-end 1))))))
  5819. (setq task (buffer-string))))
  5820. (insert task))
  5821. (goto-char beg)))
  5822. ;;; Outline Sorting
  5823. (defun org-sort (with-case)
  5824. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5825. Optional argument WITH-CASE means sort case-sensitively.
  5826. With a double prefix argument, also remove duplicate entries."
  5827. (interactive "P")
  5828. (if (org-at-table-p)
  5829. (org-call-with-arg 'org-table-sort-lines with-case)
  5830. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5831. (defun org-sort-remove-invisible (s)
  5832. (remove-text-properties 0 (length s) org-rm-props s)
  5833. (while (string-match org-bracket-link-regexp s)
  5834. (setq s (replace-match (if (match-end 2)
  5835. (match-string 3 s)
  5836. (match-string 1 s)) t t s)))
  5837. s)
  5838. (defvar org-priority-regexp) ; defined later in the file
  5839. (defvar org-after-sorting-entries-or-items-hook nil
  5840. "Hook that is run after a bunch of entries or items have been sorted.
  5841. When children are sorted, the cursor is in the parent line when this
  5842. hook gets called. When a region or a plain list is sorted, the cursor
  5843. will be in the first entry of the sorted region/list.")
  5844. (defun org-sort-entries-or-items
  5845. (&optional with-case sorting-type getkey-func compare-func property)
  5846. "Sort entries on a certain level of an outline tree, or plain list items.
  5847. If there is an active region, the entries in the region are sorted.
  5848. Else, if the cursor is before the first entry, sort the top-level items.
  5849. Else, the children of the entry at point are sorted.
  5850. If the cursor is at the first item in a plain list, the list items will be
  5851. sorted.
  5852. Sorting can be alphabetically, numerically, by date/time as given by
  5853. a time stamp, by a property or by priority.
  5854. The command prompts for the sorting type unless it has been given to the
  5855. function through the SORTING-TYPE argument, which needs to a character,
  5856. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  5857. precise meaning of each character:
  5858. n Numerically, by converting the beginning of the entry/item to a number.
  5859. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  5860. t By date/time, either the first active time stamp in the entry, or, if
  5861. none exist, by the first inactive one.
  5862. In items, only the first line will be chekced.
  5863. s By the scheduled date/time.
  5864. d By deadline date/time.
  5865. c By creation time, which is assumed to be the first inactive time stamp
  5866. at the beginning of a line.
  5867. p By priority according to the cookie.
  5868. r By the value of a property.
  5869. Capital letters will reverse the sort order.
  5870. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5871. called with point at the beginning of the record. It must return either
  5872. a string or a number that should serve as the sorting key for that record.
  5873. Comparing entries ignores case by default. However, with an optional argument
  5874. WITH-CASE, the sorting considers case as well."
  5875. (interactive "P")
  5876. (let ((case-func (if with-case 'identity 'downcase))
  5877. start beg end stars re re2
  5878. txt what tmp plain-list-p)
  5879. ;; Find beginning and end of region to sort
  5880. (cond
  5881. ((org-region-active-p)
  5882. ;; we will sort the region
  5883. (setq end (region-end)
  5884. what "region")
  5885. (goto-char (region-beginning))
  5886. (if (not (org-on-heading-p)) (outline-next-heading))
  5887. (setq start (point)))
  5888. ((org-at-item-p)
  5889. ;; we will sort this plain list
  5890. (org-beginning-of-item-list) (setq start (point))
  5891. (org-end-of-item-list) (setq end (point))
  5892. (goto-char start)
  5893. (setq plain-list-p t
  5894. what "plain list"))
  5895. ((or (org-on-heading-p)
  5896. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5897. ;; we will sort the children of the current headline
  5898. (org-back-to-heading)
  5899. (setq start (point)
  5900. end (progn (org-end-of-subtree t t)
  5901. (org-back-over-empty-lines)
  5902. (point))
  5903. what "children")
  5904. (goto-char start)
  5905. (show-subtree)
  5906. (outline-next-heading))
  5907. (t
  5908. ;; we will sort the top-level entries in this file
  5909. (goto-char (point-min))
  5910. (or (org-on-heading-p) (outline-next-heading))
  5911. (setq start (point) end (point-max) what "top-level")
  5912. (goto-char start)
  5913. (show-all)))
  5914. (setq beg (point))
  5915. (if (>= beg end) (error "Nothing to sort"))
  5916. (unless plain-list-p
  5917. (looking-at "\\(\\*+\\)")
  5918. (setq stars (match-string 1)
  5919. re (concat "^" (regexp-quote stars) " +")
  5920. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5921. txt (buffer-substring beg end))
  5922. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5923. (if (and (not (equal stars "*")) (string-match re2 txt))
  5924. (error "Region to sort contains a level above the first entry")))
  5925. (unless sorting-type
  5926. (message
  5927. (if plain-list-p
  5928. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5929. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  5930. [t]ime [s]cheduled [d]eadline [c]reated
  5931. A/N/T/S/D/C/P/O/F means reversed:")
  5932. what)
  5933. (setq sorting-type (read-char-exclusive))
  5934. (and (= (downcase sorting-type) ?f)
  5935. (setq getkey-func
  5936. (org-icompleting-read "Sort using function: "
  5937. obarray 'fboundp t nil nil))
  5938. (setq getkey-func (intern getkey-func)))
  5939. (and (= (downcase sorting-type) ?r)
  5940. (setq property
  5941. (org-icompleting-read "Property: "
  5942. (mapcar 'list (org-buffer-property-keys t))
  5943. nil t))))
  5944. (message "Sorting entries...")
  5945. (save-restriction
  5946. (narrow-to-region start end)
  5947. (let ((dcst (downcase sorting-type))
  5948. (case-fold-search nil)
  5949. (now (current-time)))
  5950. (sort-subr
  5951. (/= dcst sorting-type)
  5952. ;; This function moves to the beginning character of the "record" to
  5953. ;; be sorted.
  5954. (if plain-list-p
  5955. (lambda nil
  5956. (if (org-at-item-p) t (goto-char (point-max))))
  5957. (lambda nil
  5958. (if (re-search-forward re nil t)
  5959. (goto-char (match-beginning 0))
  5960. (goto-char (point-max)))))
  5961. ;; This function moves to the last character of the "record" being
  5962. ;; sorted.
  5963. (if plain-list-p
  5964. 'org-end-of-item
  5965. (lambda nil
  5966. (save-match-data
  5967. (condition-case nil
  5968. (outline-forward-same-level 1)
  5969. (error
  5970. (goto-char (point-max)))))))
  5971. ;; This function returns the value that gets sorted against.
  5972. (if plain-list-p
  5973. (lambda nil
  5974. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5975. (cond
  5976. ((= dcst ?n)
  5977. (string-to-number (buffer-substring (match-end 0)
  5978. (point-at-eol))))
  5979. ((= dcst ?a)
  5980. (buffer-substring (match-end 0) (point-at-eol)))
  5981. ((= dcst ?t)
  5982. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  5983. (re-search-forward org-ts-regexp-both
  5984. (point-at-eol) t))
  5985. (org-time-string-to-seconds (match-string 0))
  5986. (org-float-time now)))
  5987. ((= dcst ?f)
  5988. (if getkey-func
  5989. (progn
  5990. (setq tmp (funcall getkey-func))
  5991. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5992. tmp)
  5993. (error "Invalid key function `%s'" getkey-func)))
  5994. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5995. (lambda nil
  5996. (cond
  5997. ((= dcst ?n)
  5998. (if (looking-at org-complex-heading-regexp)
  5999. (string-to-number (match-string 4))
  6000. nil))
  6001. ((= dcst ?a)
  6002. (if (looking-at org-complex-heading-regexp)
  6003. (funcall case-func (match-string 4))
  6004. nil))
  6005. ((= dcst ?t)
  6006. (let ((end (save-excursion (outline-next-heading) (point))))
  6007. (if (or (re-search-forward org-ts-regexp end t)
  6008. (re-search-forward org-ts-regexp-both end t))
  6009. (org-time-string-to-seconds (match-string 0))
  6010. (org-float-time now))))
  6011. ((= dcst ?c)
  6012. (let ((end (save-excursion (outline-next-heading) (point))))
  6013. (if (re-search-forward
  6014. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6015. end t)
  6016. (org-time-string-to-seconds (match-string 0))
  6017. (org-float-time now))))
  6018. ((= dcst ?s)
  6019. (let ((end (save-excursion (outline-next-heading) (point))))
  6020. (if (re-search-forward org-scheduled-time-regexp end t)
  6021. (org-time-string-to-seconds (match-string 1))
  6022. (org-float-time now))))
  6023. ((= dcst ?d)
  6024. (let ((end (save-excursion (outline-next-heading) (point))))
  6025. (if (re-search-forward org-deadline-time-regexp end t)
  6026. (org-time-string-to-seconds (match-string 1))
  6027. (org-float-time now))))
  6028. ((= dcst ?p)
  6029. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6030. (string-to-char (match-string 2))
  6031. org-default-priority))
  6032. ((= dcst ?r)
  6033. (or (org-entry-get nil property) ""))
  6034. ((= dcst ?o)
  6035. (if (looking-at org-complex-heading-regexp)
  6036. (- 9999 (length (member (match-string 2)
  6037. org-todo-keywords-1)))))
  6038. ((= dcst ?f)
  6039. (if getkey-func
  6040. (progn
  6041. (setq tmp (funcall getkey-func))
  6042. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6043. tmp)
  6044. (error "Invalid key function `%s'" getkey-func)))
  6045. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6046. nil
  6047. (cond
  6048. ((= dcst ?a) 'string<)
  6049. ((= dcst ?f) compare-func)
  6050. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6051. (t nil)))))
  6052. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6053. (message "Sorting entries...done")))
  6054. (defun org-do-sort (table what &optional with-case sorting-type)
  6055. "Sort TABLE of WHAT according to SORTING-TYPE.
  6056. The user will be prompted for the SORTING-TYPE if the call to this
  6057. function does not specify it. WHAT is only for the prompt, to indicate
  6058. what is being sorted. The sorting key will be extracted from
  6059. the car of the elements of the table.
  6060. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6061. (unless sorting-type
  6062. (message
  6063. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6064. what)
  6065. (setq sorting-type (read-char-exclusive)))
  6066. (let ((dcst (downcase sorting-type))
  6067. extractfun comparefun)
  6068. ;; Define the appropriate functions
  6069. (cond
  6070. ((= dcst ?n)
  6071. (setq extractfun 'string-to-number
  6072. comparefun (if (= dcst sorting-type) '< '>)))
  6073. ((= dcst ?a)
  6074. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6075. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6076. comparefun (if (= dcst sorting-type)
  6077. 'string<
  6078. (lambda (a b) (and (not (string< a b))
  6079. (not (string= a b)))))))
  6080. ((= dcst ?t)
  6081. (setq extractfun
  6082. (lambda (x)
  6083. (if (or (string-match org-ts-regexp x)
  6084. (string-match org-ts-regexp-both x))
  6085. (org-float-time
  6086. (org-time-string-to-time (match-string 0 x)))
  6087. 0))
  6088. comparefun (if (= dcst sorting-type) '< '>)))
  6089. (t (error "Invalid sorting type `%c'" sorting-type)))
  6090. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6091. table)
  6092. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6093. ;;; The orgstruct minor mode
  6094. ;; Define a minor mode which can be used in other modes in order to
  6095. ;; integrate the org-mode structure editing commands.
  6096. ;; This is really a hack, because the org-mode structure commands use
  6097. ;; keys which normally belong to the major mode. Here is how it
  6098. ;; works: The minor mode defines all the keys necessary to operate the
  6099. ;; structure commands, but wraps the commands into a function which
  6100. ;; tests if the cursor is currently at a headline or a plain list
  6101. ;; item. If that is the case, the structure command is used,
  6102. ;; temporarily setting many Org-mode variables like regular
  6103. ;; expressions for filling etc. However, when any of those keys is
  6104. ;; used at a different location, function uses `key-binding' to look
  6105. ;; up if the key has an associated command in another currently active
  6106. ;; keymap (minor modes, major mode, global), and executes that
  6107. ;; command. There might be problems if any of the keys is otherwise
  6108. ;; used as a prefix key.
  6109. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6110. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6111. ;; addresses this by checking explicitly for both bindings.
  6112. (defvar orgstruct-mode-map (make-sparse-keymap)
  6113. "Keymap for the minor `orgstruct-mode'.")
  6114. (defvar org-local-vars nil
  6115. "List of local variables, for use by `orgstruct-mode'")
  6116. ;;;###autoload
  6117. (define-minor-mode orgstruct-mode
  6118. "Toggle the minor more `orgstruct-mode'.
  6119. This mode is for using Org-mode structure commands in other modes.
  6120. The following key behave as if Org-mode was active, if the cursor
  6121. is on a headline, or on a plain list item (both in the definition
  6122. of Org-mode).
  6123. M-up Move entry/item up
  6124. M-down Move entry/item down
  6125. M-left Promote
  6126. M-right Demote
  6127. M-S-up Move entry/item up
  6128. M-S-down Move entry/item down
  6129. M-S-left Promote subtree
  6130. M-S-right Demote subtree
  6131. M-q Fill paragraph and items like in Org-mode
  6132. C-c ^ Sort entries
  6133. C-c - Cycle list bullet
  6134. TAB Cycle item visibility
  6135. M-RET Insert new heading/item
  6136. S-M-RET Insert new TODO heading / Checkbox item
  6137. C-c C-c Set tags / toggle checkbox"
  6138. nil " OrgStruct" nil
  6139. (org-load-modules-maybe)
  6140. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6141. ;;;###autoload
  6142. (defun turn-on-orgstruct ()
  6143. "Unconditionally turn on `orgstruct-mode'."
  6144. (orgstruct-mode 1))
  6145. (defun orgstruct++-mode (&optional arg)
  6146. "Toggle `orgstruct-mode', the enhanced version of it.
  6147. In addition to setting orgstruct-mode, this also exports all indentation
  6148. and autofilling variables from org-mode into the buffer. It will also
  6149. recognize item context in multiline items.
  6150. Note that turning off orgstruct-mode will *not* remove the
  6151. indentation/paragraph settings. This can only be done by refreshing the
  6152. major mode, for example with \\[normal-mode]."
  6153. (interactive "P")
  6154. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6155. (if (< arg 1)
  6156. (orgstruct-mode -1)
  6157. (orgstruct-mode 1)
  6158. (let (var val)
  6159. (mapc
  6160. (lambda (x)
  6161. (when (string-match
  6162. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6163. (symbol-name (car x)))
  6164. (setq var (car x) val (nth 1 x))
  6165. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6166. org-local-vars)
  6167. (org-set-local 'orgstruct-is-++ t))))
  6168. (defvar orgstruct-is-++ nil
  6169. "Is orgstruct-mode in ++ version in the current-buffer?")
  6170. (make-variable-buffer-local 'orgstruct-is-++)
  6171. ;;;###autoload
  6172. (defun turn-on-orgstruct++ ()
  6173. "Unconditionally turn on `orgstruct++-mode'."
  6174. (orgstruct++-mode 1))
  6175. (defun orgstruct-error ()
  6176. "Error when there is no default binding for a structure key."
  6177. (interactive)
  6178. (error "This key has no function outside structure elements"))
  6179. (defun orgstruct-setup ()
  6180. "Setup orgstruct keymaps."
  6181. (let ((nfunc 0)
  6182. (bindings
  6183. (list
  6184. '([(meta up)] org-metaup)
  6185. '([(meta down)] org-metadown)
  6186. '([(meta left)] org-metaleft)
  6187. '([(meta right)] org-metaright)
  6188. '([(meta shift up)] org-shiftmetaup)
  6189. '([(meta shift down)] org-shiftmetadown)
  6190. '([(meta shift left)] org-shiftmetaleft)
  6191. '([(meta shift right)] org-shiftmetaright)
  6192. '([?\e (up)] org-metaup)
  6193. '([?\e (down)] org-metadown)
  6194. '([?\e (left)] org-metaleft)
  6195. '([?\e (right)] org-metaright)
  6196. '([?\e (shift up)] org-shiftmetaup)
  6197. '([?\e (shift down)] org-shiftmetadown)
  6198. '([?\e (shift left)] org-shiftmetaleft)
  6199. '([?\e (shift right)] org-shiftmetaright)
  6200. '([(shift up)] org-shiftup)
  6201. '([(shift down)] org-shiftdown)
  6202. '([(shift left)] org-shiftleft)
  6203. '([(shift right)] org-shiftright)
  6204. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6205. '("\M-q" fill-paragraph)
  6206. '("\C-c^" org-sort)
  6207. '("\C-c-" org-cycle-list-bullet)))
  6208. elt key fun cmd)
  6209. (while (setq elt (pop bindings))
  6210. (setq nfunc (1+ nfunc))
  6211. (setq key (org-key (car elt))
  6212. fun (nth 1 elt)
  6213. cmd (orgstruct-make-binding fun nfunc key))
  6214. (org-defkey orgstruct-mode-map key cmd))
  6215. ;; Special treatment needed for TAB and RET
  6216. (org-defkey orgstruct-mode-map [(tab)]
  6217. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6218. (org-defkey orgstruct-mode-map "\C-i"
  6219. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6220. (org-defkey orgstruct-mode-map "\M-\C-m"
  6221. (orgstruct-make-binding 'org-insert-heading 105
  6222. "\M-\C-m" [(meta return)]))
  6223. (org-defkey orgstruct-mode-map [(meta return)]
  6224. (orgstruct-make-binding 'org-insert-heading 106
  6225. [(meta return)] "\M-\C-m"))
  6226. (org-defkey orgstruct-mode-map [(shift meta return)]
  6227. (orgstruct-make-binding 'org-insert-todo-heading 107
  6228. [(meta return)] "\M-\C-m"))
  6229. (org-defkey orgstruct-mode-map "\e\C-m"
  6230. (orgstruct-make-binding 'org-insert-heading 108
  6231. "\e\C-m" [?\e (return)]))
  6232. (org-defkey orgstruct-mode-map [?\e (return)]
  6233. (orgstruct-make-binding 'org-insert-heading 109
  6234. [?\e (return)] "\e\C-m"))
  6235. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6236. (orgstruct-make-binding 'org-insert-todo-heading 110
  6237. [?\e (return)] "\e\C-m"))
  6238. (unless org-local-vars
  6239. (setq org-local-vars (org-get-local-variables)))
  6240. t))
  6241. (defun orgstruct-make-binding (fun n &rest keys)
  6242. "Create a function for binding in the structure minor mode.
  6243. FUN is the command to call inside a table. N is used to create a unique
  6244. command name. KEYS are keys that should be checked in for a command
  6245. to execute outside of tables."
  6246. (eval
  6247. (list 'defun
  6248. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6249. '(arg)
  6250. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6251. "Outside of structure, run the binding of `"
  6252. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6253. "'.")
  6254. '(interactive "p")
  6255. (list 'if
  6256. `(org-context-p 'headline 'item
  6257. (and orgstruct-is-++
  6258. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6259. 'item-body))
  6260. (list 'org-run-like-in-org-mode (list 'quote fun))
  6261. (list 'let '(orgstruct-mode)
  6262. (list 'call-interactively
  6263. (append '(or)
  6264. (mapcar (lambda (k)
  6265. (list 'key-binding k))
  6266. keys)
  6267. '('orgstruct-error))))))))
  6268. (defun org-context-p (&rest contexts)
  6269. "Check if local context is any of CONTEXTS.
  6270. Possible values in the list of contexts are `table', `headline', and `item'."
  6271. (let ((pos (point)))
  6272. (goto-char (point-at-bol))
  6273. (prog1 (or (and (memq 'table contexts)
  6274. (looking-at "[ \t]*|"))
  6275. (and (memq 'headline contexts)
  6276. ;;????????? (looking-at "\\*+"))
  6277. (looking-at outline-regexp))
  6278. (and (memq 'item contexts)
  6279. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6280. (and (memq 'item-body contexts)
  6281. (org-in-item-p)))
  6282. (goto-char pos))))
  6283. (defun org-get-local-variables ()
  6284. "Return a list of all local variables in an org-mode buffer."
  6285. (let (varlist)
  6286. (with-current-buffer (get-buffer-create "*Org tmp*")
  6287. (erase-buffer)
  6288. (org-mode)
  6289. (setq varlist (buffer-local-variables)))
  6290. (kill-buffer "*Org tmp*")
  6291. (delq nil
  6292. (mapcar
  6293. (lambda (x)
  6294. (setq x
  6295. (if (symbolp x)
  6296. (list x)
  6297. (list (car x) (list 'quote (cdr x)))))
  6298. (if (string-match
  6299. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6300. (symbol-name (car x)))
  6301. x nil))
  6302. varlist))))
  6303. ;;;###autoload
  6304. (defun org-run-like-in-org-mode (cmd)
  6305. "Run a command, pretending that the current buffer is in Org-mode.
  6306. This will temporarily bind local variables that are typically bound in
  6307. Org-mode to the values they have in Org-mode, and then interactively
  6308. call CMD."
  6309. (org-load-modules-maybe)
  6310. (unless org-local-vars
  6311. (setq org-local-vars (org-get-local-variables)))
  6312. (eval (list 'let org-local-vars
  6313. (list 'call-interactively (list 'quote cmd)))))
  6314. ;;;; Archiving
  6315. (defun org-get-category (&optional pos)
  6316. "Get the category applying to position POS."
  6317. (get-text-property (or pos (point)) 'org-category))
  6318. (defun org-refresh-category-properties ()
  6319. "Refresh category text properties in the buffer."
  6320. (let ((def-cat (cond
  6321. ((null org-category)
  6322. (if buffer-file-name
  6323. (file-name-sans-extension
  6324. (file-name-nondirectory buffer-file-name))
  6325. "???"))
  6326. ((symbolp org-category) (symbol-name org-category))
  6327. (t org-category)))
  6328. beg end cat pos optionp)
  6329. (org-unmodified
  6330. (save-excursion
  6331. (save-restriction
  6332. (widen)
  6333. (goto-char (point-min))
  6334. (put-text-property (point) (point-max) 'org-category def-cat)
  6335. (while (re-search-forward
  6336. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6337. (setq pos (match-end 0)
  6338. optionp (equal (char-after (match-beginning 0)) ?#)
  6339. cat (org-trim (match-string 2)))
  6340. (if optionp
  6341. (setq beg (point-at-bol) end (point-max))
  6342. (org-back-to-heading t)
  6343. (setq beg (point) end (org-end-of-subtree t t)))
  6344. (put-text-property beg end 'org-category cat)
  6345. (goto-char pos)))))))
  6346. ;;;; Link Stuff
  6347. ;;; Link abbreviations
  6348. (defun org-link-expand-abbrev (link)
  6349. "Apply replacements as defined in `org-link-abbrev-alist."
  6350. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6351. (let* ((key (match-string 1 link))
  6352. (as (or (assoc key org-link-abbrev-alist-local)
  6353. (assoc key org-link-abbrev-alist)))
  6354. (tag (and (match-end 2) (match-string 3 link)))
  6355. rpl)
  6356. (if (not as)
  6357. link
  6358. (setq rpl (cdr as))
  6359. (cond
  6360. ((symbolp rpl) (funcall rpl tag))
  6361. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6362. ((string-match "%h" rpl)
  6363. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6364. (t (concat rpl tag)))))
  6365. link))
  6366. ;;; Storing and inserting links
  6367. (defvar org-insert-link-history nil
  6368. "Minibuffer history for links inserted with `org-insert-link'.")
  6369. (defvar org-stored-links nil
  6370. "Contains the links stored with `org-store-link'.")
  6371. (defvar org-store-link-plist nil
  6372. "Plist with info about the most recently link created with `org-store-link'.")
  6373. (defvar org-link-protocols nil
  6374. "Link protocols added to Org-mode using `org-add-link-type'.")
  6375. (defvar org-store-link-functions nil
  6376. "List of functions that are called to create and store a link.
  6377. Each function will be called in turn until one returns a non-nil
  6378. value. Each function should check if it is responsible for creating
  6379. this link (for example by looking at the major mode).
  6380. If not, it must exit and return nil.
  6381. If yes, it should return a non-nil value after a calling
  6382. `org-store-link-props' with a list of properties and values.
  6383. Special properties are:
  6384. :type The link prefix. like \"http\". This must be given.
  6385. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6386. This is obligatory as well.
  6387. :description Optional default description for the second pair
  6388. of brackets in an Org-mode link. The user can still change
  6389. this when inserting this link into an Org-mode buffer.
  6390. In addition to these, any additional properties can be specified
  6391. and then used in remember templates.")
  6392. (defun org-add-link-type (type &optional follow export)
  6393. "Add TYPE to the list of `org-link-types'.
  6394. Re-compute all regular expressions depending on `org-link-types'
  6395. FOLLOW and EXPORT are two functions.
  6396. FOLLOW should take the link path as the single argument and do whatever
  6397. is necessary to follow the link, for example find a file or display
  6398. a mail message.
  6399. EXPORT should format the link path for export to one of the export formats.
  6400. It should be a function accepting three arguments:
  6401. path the path of the link, the text after the prefix (like \"http:\")
  6402. desc the description of the link, if any, nil if there was no description
  6403. format the export format, a symbol like `html' or `latex'.
  6404. The function may use the FORMAT information to return different values
  6405. depending on the format. The return value will be put literally into
  6406. the exported file.
  6407. Org-mode has a built-in default for exporting links. If you are happy with
  6408. this default, there is no need to define an export function for the link
  6409. type. For a simple example of an export function, see `org-bbdb.el'."
  6410. (add-to-list 'org-link-types type t)
  6411. (org-make-link-regexps)
  6412. (if (assoc type org-link-protocols)
  6413. (setcdr (assoc type org-link-protocols) (list follow export))
  6414. (push (list type follow export) org-link-protocols)))
  6415. (defvar org-agenda-buffer-name)
  6416. ;;;###autoload
  6417. (defun org-store-link (arg)
  6418. "\\<org-mode-map>Store an org-link to the current location.
  6419. This link is added to `org-stored-links' and can later be inserted
  6420. into an org-buffer with \\[org-insert-link].
  6421. For some link types, a prefix arg is interpreted:
  6422. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6423. For file links, arg negates `org-context-in-file-links'."
  6424. (interactive "P")
  6425. (org-load-modules-maybe)
  6426. (setq org-store-link-plist nil) ; reset
  6427. (let ((outline-regexp (org-get-limited-outline-regexp))
  6428. link cpltxt desc description search txt custom-id)
  6429. (cond
  6430. ((run-hook-with-args-until-success 'org-store-link-functions)
  6431. (setq link (plist-get org-store-link-plist :link)
  6432. desc (or (plist-get org-store-link-plist :description) link)))
  6433. ((equal (buffer-name) "*Org Edit Src Example*")
  6434. (let (label gc)
  6435. (while (or (not label)
  6436. (save-excursion
  6437. (save-restriction
  6438. (widen)
  6439. (goto-char (point-min))
  6440. (re-search-forward
  6441. (regexp-quote (format org-coderef-label-format label))
  6442. nil t))))
  6443. (when label (message "Label exists already") (sit-for 2))
  6444. (setq label (read-string "Code line label: " label)))
  6445. (end-of-line 1)
  6446. (setq link (format org-coderef-label-format label))
  6447. (setq gc (- 79 (length link)))
  6448. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6449. (insert link)
  6450. (setq link (concat "(" label ")") desc nil)))
  6451. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  6452. ;; We are in the agenda, link to referenced location
  6453. (let ((m (or (get-text-property (point) 'org-hd-marker)
  6454. (get-text-property (point) 'org-marker))))
  6455. (unless m (error "Don't know what location to link to"))
  6456. (org-with-point-at m
  6457. (call-interactively 'org-store-link))))
  6458. ((eq major-mode 'calendar-mode)
  6459. (let ((cd (calendar-cursor-to-date)))
  6460. (setq link
  6461. (format-time-string
  6462. (car org-time-stamp-formats)
  6463. (apply 'encode-time
  6464. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6465. nil nil nil))))
  6466. (org-store-link-props :type "calendar" :date cd)))
  6467. ((eq major-mode 'w3-mode)
  6468. (setq cpltxt (if (and (buffer-name)
  6469. (not (string-match "Untitled" (buffer-name))))
  6470. (buffer-name)
  6471. (url-view-url t))
  6472. link (org-make-link (url-view-url t)))
  6473. (org-store-link-props :type "w3" :url (url-view-url t)))
  6474. ((eq major-mode 'w3m-mode)
  6475. (setq cpltxt (or w3m-current-title w3m-current-url)
  6476. link (org-make-link w3m-current-url))
  6477. (org-store-link-props :type "w3m" :url (url-view-url t)))
  6478. ((setq search (run-hook-with-args-until-success
  6479. 'org-create-file-search-functions))
  6480. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  6481. "::" search))
  6482. (setq cpltxt (or description link)))
  6483. ((eq major-mode 'image-mode)
  6484. (setq cpltxt (concat "file:"
  6485. (abbreviate-file-name buffer-file-name))
  6486. link (org-make-link cpltxt))
  6487. (org-store-link-props :type "image" :file buffer-file-name))
  6488. ((eq major-mode 'dired-mode)
  6489. ;; link to the file in the current line
  6490. (setq cpltxt (concat "file:"
  6491. (abbreviate-file-name
  6492. (expand-file-name
  6493. (dired-get-filename nil t))))
  6494. link (org-make-link cpltxt)))
  6495. ((and buffer-file-name (org-mode-p))
  6496. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  6497. (cond
  6498. ((org-in-regexp "<<\\(.*?\\)>>")
  6499. (setq cpltxt
  6500. (concat "file:"
  6501. (abbreviate-file-name buffer-file-name)
  6502. "::" (match-string 1))
  6503. link (org-make-link cpltxt)))
  6504. ((and (featurep 'org-id)
  6505. (or (eq org-link-to-org-use-id t)
  6506. (and (eq org-link-to-org-use-id 'create-if-interactive)
  6507. (interactive-p))
  6508. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  6509. (interactive-p)
  6510. (not custom-id))
  6511. (and org-link-to-org-use-id
  6512. (condition-case nil
  6513. (org-entry-get nil "ID")
  6514. (error nil)))))
  6515. ;; We can make a link using the ID.
  6516. (setq link (condition-case nil
  6517. (prog1 (org-id-store-link)
  6518. (setq desc (plist-get org-store-link-plist
  6519. :description)))
  6520. (error
  6521. ;; probably before first headline, link to file only
  6522. (concat "file:"
  6523. (abbreviate-file-name buffer-file-name))))))
  6524. (t
  6525. ;; Just link to current headline
  6526. (setq cpltxt (concat "file:"
  6527. (abbreviate-file-name buffer-file-name)))
  6528. ;; Add a context search string
  6529. (when (org-xor org-context-in-file-links arg)
  6530. (setq txt (cond
  6531. ((org-on-heading-p) nil)
  6532. ((org-region-active-p)
  6533. (buffer-substring (region-beginning) (region-end)))
  6534. (t nil)))
  6535. (when (or (null txt) (string-match "\\S-" txt))
  6536. (setq cpltxt
  6537. (concat cpltxt "::"
  6538. (condition-case nil
  6539. (org-make-org-heading-search-string txt)
  6540. (error "")))
  6541. desc (or (nth 4 (ignore-errors
  6542. (org-heading-components))) "NONE"))))
  6543. (if (string-match "::\\'" cpltxt)
  6544. (setq cpltxt (substring cpltxt 0 -2)))
  6545. (setq link (org-make-link cpltxt)))))
  6546. ((buffer-file-name (buffer-base-buffer))
  6547. ;; Just link to this file here.
  6548. (setq cpltxt (concat "file:"
  6549. (abbreviate-file-name
  6550. (buffer-file-name (buffer-base-buffer)))))
  6551. ;; Add a context string
  6552. (when (org-xor org-context-in-file-links arg)
  6553. (setq txt (if (org-region-active-p)
  6554. (buffer-substring (region-beginning) (region-end))
  6555. (buffer-substring (point-at-bol) (point-at-eol))))
  6556. ;; Only use search option if there is some text.
  6557. (when (string-match "\\S-" txt)
  6558. (setq cpltxt
  6559. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6560. desc "NONE")))
  6561. (setq link (org-make-link cpltxt)))
  6562. ((interactive-p)
  6563. (error "Cannot link to a buffer which is not visiting a file"))
  6564. (t (setq link nil)))
  6565. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6566. (setq link (or link cpltxt)
  6567. desc (or desc cpltxt))
  6568. (if (equal desc "NONE") (setq desc nil))
  6569. (if (and (or (interactive-p) executing-kbd-macro) link)
  6570. (progn
  6571. (setq org-stored-links
  6572. (cons (list link desc) org-stored-links))
  6573. (message "Stored: %s" (or desc link))
  6574. (when custom-id
  6575. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  6576. "::#" custom-id))
  6577. (setq org-stored-links
  6578. (cons (list link desc) org-stored-links))))
  6579. (and link (org-make-link-string link desc)))))
  6580. (defun org-store-link-props (&rest plist)
  6581. "Store link properties, extract names and addresses."
  6582. (let (x adr)
  6583. (when (setq x (plist-get plist :from))
  6584. (setq adr (mail-extract-address-components x))
  6585. (setq plist (plist-put plist :fromname (car adr)))
  6586. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6587. (when (setq x (plist-get plist :to))
  6588. (setq adr (mail-extract-address-components x))
  6589. (setq plist (plist-put plist :toname (car adr)))
  6590. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6591. (let ((from (plist-get plist :from))
  6592. (to (plist-get plist :to)))
  6593. (when (and from to org-from-is-user-regexp)
  6594. (setq plist
  6595. (plist-put plist :fromto
  6596. (if (string-match org-from-is-user-regexp from)
  6597. (concat "to %t")
  6598. (concat "from %f"))))))
  6599. (setq org-store-link-plist plist))
  6600. (defun org-add-link-props (&rest plist)
  6601. "Add these properties to the link property list."
  6602. (let (key value)
  6603. (while plist
  6604. (setq key (pop plist) value (pop plist))
  6605. (setq org-store-link-plist
  6606. (plist-put org-store-link-plist key value)))))
  6607. (defun org-email-link-description (&optional fmt)
  6608. "Return the description part of an email link.
  6609. This takes information from `org-store-link-plist' and formats it
  6610. according to FMT (default from `org-email-link-description-format')."
  6611. (setq fmt (or fmt org-email-link-description-format))
  6612. (let* ((p org-store-link-plist)
  6613. (to (plist-get p :toaddress))
  6614. (from (plist-get p :fromaddress))
  6615. (table
  6616. (list
  6617. (cons "%c" (plist-get p :fromto))
  6618. (cons "%F" (plist-get p :from))
  6619. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6620. (cons "%T" (plist-get p :to))
  6621. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6622. (cons "%s" (plist-get p :subject))
  6623. (cons "%m" (plist-get p :message-id)))))
  6624. (when (string-match "%c" fmt)
  6625. ;; Check if the user wrote this message
  6626. (if (and org-from-is-user-regexp from to
  6627. (save-match-data (string-match org-from-is-user-regexp from)))
  6628. (setq fmt (replace-match "to %t" t t fmt))
  6629. (setq fmt (replace-match "from %f" t t fmt))))
  6630. (org-replace-escapes fmt table)))
  6631. (defun org-make-org-heading-search-string (&optional string heading)
  6632. "Make search string for STRING or current headline."
  6633. (interactive)
  6634. (let ((s (or string (org-get-heading))))
  6635. (unless (and string (not heading))
  6636. ;; We are using a headline, clean up garbage in there.
  6637. (if (string-match org-todo-regexp s)
  6638. (setq s (replace-match "" t t s)))
  6639. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6640. (setq s (replace-match "" t t s)))
  6641. (setq s (org-trim s))
  6642. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6643. org-comment-string "\\)") s)
  6644. (setq s (replace-match "" t t s)))
  6645. (while (string-match org-ts-regexp s)
  6646. (setq s (replace-match "" t t s))))
  6647. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6648. (setq s (replace-match " " t t s)))
  6649. (or string (setq s (concat "*" s))) ; Add * for headlines
  6650. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6651. (defun org-make-link (&rest strings)
  6652. "Concatenate STRINGS."
  6653. (apply 'concat strings))
  6654. (defun org-make-link-string (link &optional description)
  6655. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6656. (unless (string-match "\\S-" link)
  6657. (error "Empty link"))
  6658. (when (stringp description)
  6659. ;; Remove brackets from the description, they are fatal.
  6660. (while (string-match "\\[" description)
  6661. (setq description (replace-match "{" t t description)))
  6662. (while (string-match "\\]" description)
  6663. (setq description (replace-match "}" t t description))))
  6664. (when (equal (org-link-escape link) description)
  6665. ;; No description needed, it is identical
  6666. (setq description nil))
  6667. (when (and (not description)
  6668. (not (equal link (org-link-escape link))))
  6669. (setq description (org-extract-attributes link)))
  6670. (concat "[[" (org-link-escape link) "]"
  6671. (if description (concat "[" description "]") "")
  6672. "]"))
  6673. (defconst org-link-escape-chars
  6674. '((?\ . "%20")
  6675. (?\[ . "%5B")
  6676. (?\] . "%5D")
  6677. (?\340 . "%E0") ; `a
  6678. (?\342 . "%E2") ; ^a
  6679. (?\347 . "%E7") ; ,c
  6680. (?\350 . "%E8") ; `e
  6681. (?\351 . "%E9") ; 'e
  6682. (?\352 . "%EA") ; ^e
  6683. (?\356 . "%EE") ; ^i
  6684. (?\364 . "%F4") ; ^o
  6685. (?\371 . "%F9") ; `u
  6686. (?\373 . "%FB") ; ^u
  6687. (?\; . "%3B")
  6688. (?? . "%3F")
  6689. (?= . "%3D")
  6690. (?+ . "%2B")
  6691. )
  6692. "Association list of escapes for some characters problematic in links.
  6693. This is the list that is used for internal purposes.")
  6694. (defvar org-url-encoding-use-url-hexify nil)
  6695. (defconst org-link-escape-chars-browser
  6696. '((?\ . "%20")) ; 32 for the SPC char
  6697. "Association list of escapes for some characters problematic in links.
  6698. This is the list that is used before handing over to the browser.")
  6699. (defun org-link-escape (text &optional table)
  6700. "Escape characters in TEXT that are problematic for links."
  6701. (if org-url-encoding-use-url-hexify
  6702. (url-hexify-string text)
  6703. (setq table (or table org-link-escape-chars))
  6704. (when text
  6705. (let ((re (mapconcat (lambda (x) (regexp-quote
  6706. (char-to-string (car x))))
  6707. table "\\|")))
  6708. (while (string-match re text)
  6709. (setq text
  6710. (replace-match
  6711. (cdr (assoc (string-to-char (match-string 0 text))
  6712. table))
  6713. t t text)))
  6714. text))))
  6715. (defun org-link-unescape (text &optional table)
  6716. "Reverse the action of `org-link-escape'."
  6717. (if org-url-encoding-use-url-hexify
  6718. (url-unhex-string text)
  6719. (setq table (or table org-link-escape-chars))
  6720. (when text
  6721. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6722. table "\\|")))
  6723. (while (string-match re text)
  6724. (setq text
  6725. (replace-match
  6726. (char-to-string (car (rassoc (match-string 0 text) table)))
  6727. t t text)))
  6728. text))))
  6729. (defun org-xor (a b)
  6730. "Exclusive or."
  6731. (if a (not b) b))
  6732. (defun org-fixup-message-id-for-http (s)
  6733. "Replace special characters in a message id, so it can be used in an http query."
  6734. (while (string-match "<" s)
  6735. (setq s (replace-match "%3C" t t s)))
  6736. (while (string-match ">" s)
  6737. (setq s (replace-match "%3E" t t s)))
  6738. (while (string-match "@" s)
  6739. (setq s (replace-match "%40" t t s)))
  6740. s)
  6741. ;;;###autoload
  6742. (defun org-insert-link-global ()
  6743. "Insert a link like Org-mode does.
  6744. This command can be called in any mode to insert a link in Org-mode syntax."
  6745. (interactive)
  6746. (org-load-modules-maybe)
  6747. (org-run-like-in-org-mode 'org-insert-link))
  6748. (defun org-insert-link (&optional complete-file link-location)
  6749. "Insert a link. At the prompt, enter the link.
  6750. Completion can be used to insert any of the link protocol prefixes like
  6751. http or ftp in use.
  6752. The history can be used to select a link previously stored with
  6753. `org-store-link'. When the empty string is entered (i.e. if you just
  6754. press RET at the prompt), the link defaults to the most recently
  6755. stored link. As SPC triggers completion in the minibuffer, you need to
  6756. use M-SPC or C-q SPC to force the insertion of a space character.
  6757. You will also be prompted for a description, and if one is given, it will
  6758. be displayed in the buffer instead of the link.
  6759. If there is already a link at point, this command will allow you to edit link
  6760. and description parts.
  6761. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6762. be selected using completion. The path to the file will be relative to the
  6763. current directory if the file is in the current directory or a subdirectory.
  6764. Otherwise, the link will be the absolute path as completed in the minibuffer
  6765. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6766. option `org-link-file-path-type'.
  6767. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6768. the current directory or below.
  6769. With three \\[universal-argument] prefixes, negate the meaning of
  6770. `org-keep-stored-link-after-insertion'.
  6771. If `org-make-link-description-function' is non-nil, this function will be
  6772. called with the link target, and the result will be the default
  6773. link description.
  6774. If the LINK-LOCATION parameter is non-nil, this value will be
  6775. used as the link location instead of reading one interactively."
  6776. (interactive "P")
  6777. (let* ((wcf (current-window-configuration))
  6778. (region (if (org-region-active-p)
  6779. (buffer-substring (region-beginning) (region-end))))
  6780. (remove (and region (list (region-beginning) (region-end))))
  6781. (desc region)
  6782. tmphist ; byte-compile incorrectly complains about this
  6783. (link link-location)
  6784. entry file all-prefixes)
  6785. (cond
  6786. (link-location) ; specified by arg, just use it.
  6787. ((org-in-regexp org-bracket-link-regexp 1)
  6788. ;; We do have a link at point, and we are going to edit it.
  6789. (setq remove (list (match-beginning 0) (match-end 0)))
  6790. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6791. (setq link (read-string "Link: "
  6792. (org-link-unescape
  6793. (org-match-string-no-properties 1)))))
  6794. ((or (org-in-regexp org-angle-link-re)
  6795. (org-in-regexp org-plain-link-re))
  6796. ;; Convert to bracket link
  6797. (setq remove (list (match-beginning 0) (match-end 0))
  6798. link (read-string "Link: "
  6799. (org-remove-angle-brackets (match-string 0)))))
  6800. ((member complete-file '((4) (16)))
  6801. ;; Completing read for file names.
  6802. (setq link (org-file-complete-link complete-file)))
  6803. (t
  6804. ;; Read link, with completion for stored links.
  6805. (with-output-to-temp-buffer "*Org Links*"
  6806. (princ "Insert a link.
  6807. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  6808. (when org-stored-links
  6809. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6810. (princ (mapconcat
  6811. (lambda (x)
  6812. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6813. (reverse org-stored-links) "\n"))))
  6814. (let ((cw (selected-window)))
  6815. (select-window (get-buffer-window "*Org Links*"))
  6816. (setq truncate-lines t)
  6817. (unless (pos-visible-in-window-p (point-max))
  6818. (org-fit-window-to-buffer))
  6819. (and (window-live-p cw) (select-window cw)))
  6820. ;; Fake a link history, containing the stored links.
  6821. (setq tmphist (append (mapcar 'car org-stored-links)
  6822. org-insert-link-history))
  6823. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  6824. (mapcar 'car org-link-abbrev-alist)
  6825. org-link-types))
  6826. (unwind-protect
  6827. (progn
  6828. (setq link
  6829. (let ((org-completion-use-ido nil)
  6830. (org-completion-use-iswitchb nil))
  6831. (org-completing-read
  6832. "Link: "
  6833. (append
  6834. (mapcar (lambda (x) (list (concat x ":")))
  6835. all-prefixes)
  6836. (mapcar 'car org-stored-links))
  6837. nil nil nil
  6838. 'tmphist
  6839. (car (car org-stored-links)))))
  6840. (if (or (member link all-prefixes)
  6841. (and (equal ":" (substring link -1))
  6842. (member (substring link 0 -1) all-prefixes)
  6843. (setq link (substring link 0 -1))))
  6844. (setq link (org-link-try-special-completion link))))
  6845. (set-window-configuration wcf)
  6846. (kill-buffer "*Org Links*"))
  6847. (setq entry (assoc link org-stored-links))
  6848. (or entry (push link org-insert-link-history))
  6849. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6850. (not org-keep-stored-link-after-insertion))
  6851. (setq org-stored-links (delq (assoc link org-stored-links)
  6852. org-stored-links)))
  6853. (setq desc (or desc (nth 1 entry)))))
  6854. (if (string-match org-plain-link-re link)
  6855. ;; URL-like link, normalize the use of angular brackets.
  6856. (setq link (org-make-link (org-remove-angle-brackets link))))
  6857. ;; Check if we are linking to the current file with a search option
  6858. ;; If yes, simplify the link by using only the search option.
  6859. (when (and buffer-file-name
  6860. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6861. (let* ((path (match-string 1 link))
  6862. (case-fold-search nil)
  6863. (search (match-string 2 link)))
  6864. (save-match-data
  6865. (if (equal (file-truename buffer-file-name) (file-truename path))
  6866. ;; We are linking to this same file, with a search option
  6867. (setq link search)))))
  6868. ;; Check if we can/should use a relative path. If yes, simplify the link
  6869. (when (string-match "^file:\\(.*\\)" link)
  6870. (let* ((path (match-string 1 link))
  6871. (origpath path)
  6872. (case-fold-search nil))
  6873. (cond
  6874. ((or (eq org-link-file-path-type 'absolute)
  6875. (equal complete-file '(16)))
  6876. (setq path (abbreviate-file-name (expand-file-name path))))
  6877. ((eq org-link-file-path-type 'noabbrev)
  6878. (setq path (expand-file-name path)))
  6879. ((eq org-link-file-path-type 'relative)
  6880. (setq path (file-relative-name path)))
  6881. (t
  6882. (save-match-data
  6883. (if (string-match (concat "^" (regexp-quote
  6884. (file-name-as-directory
  6885. (expand-file-name "."))))
  6886. (expand-file-name path))
  6887. ;; We are linking a file with relative path name.
  6888. (setq path (substring (expand-file-name path)
  6889. (match-end 0)))
  6890. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6891. (setq link (concat "file:" path))
  6892. (if (equal desc origpath)
  6893. (setq desc path))))
  6894. (if org-make-link-description-function
  6895. (setq desc (funcall org-make-link-description-function link desc)))
  6896. (setq desc (read-string "Description: " desc))
  6897. (unless (string-match "\\S-" desc) (setq desc nil))
  6898. (if remove (apply 'delete-region remove))
  6899. (insert (org-make-link-string link desc))))
  6900. (defun org-link-try-special-completion (type)
  6901. "If there is completion support for link type TYPE, offer it."
  6902. (let ((fun (intern (concat "org-" type "-complete-link"))))
  6903. (if (functionp fun)
  6904. (funcall fun)
  6905. (read-string "Link (no completion support): " (concat type ":")))))
  6906. (defun org-file-complete-link (&optional arg)
  6907. "Create a file link using completion."
  6908. (let (file link)
  6909. (setq file (read-file-name "File: "))
  6910. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6911. (pwd1 (file-name-as-directory (abbreviate-file-name
  6912. (expand-file-name ".")))))
  6913. (cond
  6914. ((equal arg '(16))
  6915. (setq link (org-make-link
  6916. "file:"
  6917. (abbreviate-file-name (expand-file-name file)))))
  6918. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6919. (setq link (org-make-link "file:" (match-string 1 file))))
  6920. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6921. (expand-file-name file))
  6922. (setq link (org-make-link
  6923. "file:" (match-string 1 (expand-file-name file)))))
  6924. (t (setq link (org-make-link "file:" file)))))
  6925. link))
  6926. (defun org-completing-read (&rest args)
  6927. "Completing-read with SPACE being a normal character."
  6928. (let ((minibuffer-local-completion-map
  6929. (copy-keymap minibuffer-local-completion-map)))
  6930. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6931. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6932. (apply 'org-icompleting-read args)))
  6933. (defun org-completing-read-no-i (&rest args)
  6934. (let (org-completion-use-ido org-completion-use-iswitchb)
  6935. (apply 'org-completing-read args)))
  6936. (defun org-iswitchb-completing-read (prompt choices &rest args)
  6937. "Use iswitch as a completing-read replacement to choose from choices.
  6938. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  6939. from."
  6940. (let* ((switchb-use-virtual-buffers nil)
  6941. (iswitchb-make-buflist-hook
  6942. (lambda ()
  6943. (setq iswitchb-temp-buflist choices))))
  6944. (iswitchb-read-buffer prompt)))
  6945. (defun org-icompleting-read (&rest args)
  6946. "Completing-read using `ido-mode' or `iswitchb' speedups if available"
  6947. (if (and org-completion-use-ido
  6948. (fboundp 'ido-completing-read)
  6949. (boundp 'ido-mode) ido-mode
  6950. (listp (second args)))
  6951. (let ((ido-enter-matching-directory nil))
  6952. (apply 'ido-completing-read (concat (car args))
  6953. (if (consp (car (nth 1 args)))
  6954. (mapcar (lambda (x) (car x)) (nth 1 args))
  6955. (nth 1 args))
  6956. (cddr args)))
  6957. (if (and org-completion-use-iswitchb
  6958. (boundp 'iswitchb-mode) iswitchb-mode
  6959. (listp (second args)))
  6960. (apply 'org-iswitchb-completing-read (concat (car args))
  6961. (mapcar (lambda (x) (car x)) (nth 1 args))
  6962. (cddr args))
  6963. (apply 'completing-read args))))
  6964. (defun org-extract-attributes (s)
  6965. "Extract the attributes cookie from a string and set as text property."
  6966. (let (a attr (start 0) key value)
  6967. (save-match-data
  6968. (when (string-match "{{\\([^}]+\\)}}$" s)
  6969. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6970. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6971. (setq key (match-string 1 a) value (match-string 2 a)
  6972. start (match-end 0)
  6973. attr (plist-put attr (intern key) value))))
  6974. (org-add-props s nil 'org-attr attr))
  6975. s))
  6976. (defun org-extract-attributes-from-string (tag)
  6977. (let (key value attr)
  6978. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  6979. (setq key (match-string 1 tag) value (match-string 2 tag)
  6980. tag (replace-match "" t t tag)
  6981. attr (plist-put attr (intern key) value)))
  6982. (cons tag attr)))
  6983. (defun org-attributes-to-string (plist)
  6984. "Format a property list into an HTML attribute list."
  6985. (let ((s "") key value)
  6986. (while plist
  6987. (setq key (pop plist) value (pop plist))
  6988. (and value
  6989. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6990. s))
  6991. ;;; Opening/following a link
  6992. (defvar org-link-search-failed nil)
  6993. (defun org-next-link ()
  6994. "Move forward to the next link.
  6995. If the link is in hidden text, expose it."
  6996. (interactive)
  6997. (when (and org-link-search-failed (eq this-command last-command))
  6998. (goto-char (point-min))
  6999. (message "Link search wrapped back to beginning of buffer"))
  7000. (setq org-link-search-failed nil)
  7001. (let* ((pos (point))
  7002. (ct (org-context))
  7003. (a (assoc :link ct)))
  7004. (if a (goto-char (nth 2 a)))
  7005. (if (re-search-forward org-any-link-re nil t)
  7006. (progn
  7007. (goto-char (match-beginning 0))
  7008. (if (org-invisible-p) (org-show-context)))
  7009. (goto-char pos)
  7010. (setq org-link-search-failed t)
  7011. (error "No further link found"))))
  7012. (defun org-previous-link ()
  7013. "Move backward to the previous link.
  7014. If the link is in hidden text, expose it."
  7015. (interactive)
  7016. (when (and org-link-search-failed (eq this-command last-command))
  7017. (goto-char (point-max))
  7018. (message "Link search wrapped back to end of buffer"))
  7019. (setq org-link-search-failed nil)
  7020. (let* ((pos (point))
  7021. (ct (org-context))
  7022. (a (assoc :link ct)))
  7023. (if a (goto-char (nth 1 a)))
  7024. (if (re-search-backward org-any-link-re nil t)
  7025. (progn
  7026. (goto-char (match-beginning 0))
  7027. (if (org-invisible-p) (org-show-context)))
  7028. (goto-char pos)
  7029. (setq org-link-search-failed t)
  7030. (error "No further link found"))))
  7031. (defun org-translate-link (s)
  7032. "Translate a link string if a translation function has been defined."
  7033. (if (and org-link-translation-function
  7034. (fboundp org-link-translation-function)
  7035. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7036. (progn
  7037. (setq s (funcall org-link-translation-function
  7038. (match-string 1) (match-string 2)))
  7039. (concat (car s) ":" (cdr s)))
  7040. s))
  7041. (defun org-translate-link-from-planner (type path)
  7042. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7043. This is still an experimental function, your mileage may vary."
  7044. (cond
  7045. ((member type '("http" "https" "news" "ftp"))
  7046. ;; standard Internet links are the same.
  7047. nil)
  7048. ((and (equal type "irc") (string-match "^//" path))
  7049. ;; Planner has two / at the beginning of an irc link, we have 1.
  7050. ;; We should have zero, actually....
  7051. (setq path (substring path 1)))
  7052. ((and (equal type "lisp") (string-match "^/" path))
  7053. ;; Planner has a slash, we do not.
  7054. (setq type "elisp" path (substring path 1)))
  7055. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7056. ;; A typical message link. Planner has the id after the fina slash,
  7057. ;; we separate it with a hash mark
  7058. (setq path (concat (match-string 1 path) "#"
  7059. (org-remove-angle-brackets (match-string 2 path)))))
  7060. )
  7061. (cons type path))
  7062. (defun org-find-file-at-mouse (ev)
  7063. "Open file link or URL at mouse."
  7064. (interactive "e")
  7065. (mouse-set-point ev)
  7066. (org-open-at-point 'in-emacs))
  7067. (defun org-open-at-mouse (ev)
  7068. "Open file link or URL at mouse."
  7069. (interactive "e")
  7070. (mouse-set-point ev)
  7071. (if (eq major-mode 'org-agenda-mode)
  7072. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7073. (org-open-at-point))
  7074. (defvar org-window-config-before-follow-link nil
  7075. "The window configuration before following a link.
  7076. This is saved in case the need arises to restore it.")
  7077. (defvar org-open-link-marker (make-marker)
  7078. "Marker pointing to the location where `org-open-at-point; was called.")
  7079. ;;;###autoload
  7080. (defun org-open-at-point-global ()
  7081. "Follow a link like Org-mode does.
  7082. This command can be called in any mode to follow a link that has
  7083. Org-mode syntax."
  7084. (interactive)
  7085. (org-run-like-in-org-mode 'org-open-at-point))
  7086. ;;;###autoload
  7087. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7088. "Open a link in the string S, as if it was in Org-mode."
  7089. (interactive "sLink: \nP")
  7090. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7091. (with-temp-buffer
  7092. (let ((org-inhibit-startup t))
  7093. (org-mode)
  7094. (insert s)
  7095. (goto-char (point-min))
  7096. (org-open-at-point arg reference-buffer)))))
  7097. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7098. "Open link at or after point.
  7099. If there is no link at point, this function will search forward up to
  7100. the end of the current line.
  7101. Normally, files will be opened by an appropriate application. If the
  7102. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7103. With a double prefix argument, try to open outside of Emacs, in the
  7104. application the system uses for this file type."
  7105. (interactive "P")
  7106. (org-load-modules-maybe)
  7107. (move-marker org-open-link-marker (point))
  7108. (setq org-window-config-before-follow-link (current-window-configuration))
  7109. (org-remove-occur-highlights nil nil t)
  7110. (cond
  7111. ((and (org-on-heading-p)
  7112. (not (org-in-regexp
  7113. (concat org-plain-link-re "\\|"
  7114. org-bracket-link-regexp "\\|"
  7115. org-angle-link-re "\\|"
  7116. "[ \t]:[^ \t\n]+:[ \t]*$"))))
  7117. (org-offer-links-in-entry in-emacs))
  7118. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7119. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7120. (org-footnote-action))
  7121. (t
  7122. (let (type path link line search (pos (point)))
  7123. (catch 'match
  7124. (save-excursion
  7125. (skip-chars-forward "^]\n\r")
  7126. (when (org-in-regexp org-bracket-link-regexp)
  7127. (setq link (org-extract-attributes
  7128. (org-link-unescape (org-match-string-no-properties 1))))
  7129. (while (string-match " *\n *" link)
  7130. (setq link (replace-match " " t t link)))
  7131. (setq link (org-link-expand-abbrev link))
  7132. (cond
  7133. ((or (file-name-absolute-p link)
  7134. (string-match "^\\.\\.?/" link))
  7135. (setq type "file" path link))
  7136. ((string-match org-link-re-with-space3 link)
  7137. (setq type (match-string 1 link) path (match-string 2 link)))
  7138. (t (setq type "thisfile" path link)))
  7139. (throw 'match t)))
  7140. (when (get-text-property (point) 'org-linked-text)
  7141. (setq type "thisfile"
  7142. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7143. (1+ (point)) (point))
  7144. path (buffer-substring
  7145. (previous-single-property-change pos 'org-linked-text)
  7146. (next-single-property-change pos 'org-linked-text)))
  7147. (throw 'match t))
  7148. (save-excursion
  7149. (when (or (org-in-regexp org-angle-link-re)
  7150. (org-in-regexp org-plain-link-re))
  7151. (setq type (match-string 1) path (match-string 2))
  7152. (throw 'match t)))
  7153. (save-excursion
  7154. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7155. (setq type "tags"
  7156. path (match-string 1))
  7157. (while (string-match ":" path)
  7158. (setq path (replace-match "+" t t path)))
  7159. (throw 'match t)))
  7160. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7161. (setq type "tree-match"
  7162. path (match-string 1))
  7163. (throw 'match t)))
  7164. (unless path
  7165. (error "No link found"))
  7166. ;; switch back to reference buffer
  7167. ;; needed when if called in a temporary buffer through
  7168. ;; org-open-link-from-string
  7169. (with-current-buffer (or reference-buffer (current-buffer))
  7170. ;; Remove any trailing spaces in path
  7171. (if (string-match " +\\'" path)
  7172. (setq path (replace-match "" t t path)))
  7173. (if (and org-link-translation-function
  7174. (fboundp org-link-translation-function))
  7175. ;; Check if we need to translate the link
  7176. (let ((tmp (funcall org-link-translation-function type path)))
  7177. (setq type (car tmp) path (cdr tmp))))
  7178. (cond
  7179. ((assoc type org-link-protocols)
  7180. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7181. ((equal type "mailto")
  7182. (let ((cmd (car org-link-mailto-program))
  7183. (args (cdr org-link-mailto-program)) args1
  7184. (address path) (subject "") a)
  7185. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7186. (setq address (match-string 1 path)
  7187. subject (org-link-escape (match-string 2 path))))
  7188. (while args
  7189. (cond
  7190. ((not (stringp (car args))) (push (pop args) args1))
  7191. (t (setq a (pop args))
  7192. (if (string-match "%a" a)
  7193. (setq a (replace-match address t t a)))
  7194. (if (string-match "%s" a)
  7195. (setq a (replace-match subject t t a)))
  7196. (push a args1))))
  7197. (apply cmd (nreverse args1))))
  7198. ((member type '("http" "https" "ftp" "news"))
  7199. (browse-url (concat type ":" (org-link-escape
  7200. path org-link-escape-chars-browser))))
  7201. ((member type '("message"))
  7202. (browse-url (concat type ":" path)))
  7203. ((string= type "tags")
  7204. (org-tags-view in-emacs path))
  7205. ((string= type "thisfile")
  7206. (if in-emacs
  7207. (switch-to-buffer-other-window
  7208. (org-get-buffer-for-internal-link (current-buffer)))
  7209. (org-mark-ring-push))
  7210. (let ((cmd `(org-link-search
  7211. ,path
  7212. ,(cond ((equal in-emacs '(4)) 'occur)
  7213. ((equal in-emacs '(16)) 'org-occur)
  7214. (t nil))
  7215. ,pos)))
  7216. (condition-case nil (eval cmd)
  7217. (error (progn (widen) (eval cmd))))))
  7218. ((string= type "tree-match")
  7219. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7220. ((string= type "file")
  7221. (if (string-match "::\\([0-9]+\\)\\'" path)
  7222. (setq line (string-to-number (match-string 1 path))
  7223. path (substring path 0 (match-beginning 0)))
  7224. (if (string-match "::\\(.+\\)\\'" path)
  7225. (setq search (match-string 1 path)
  7226. path (substring path 0 (match-beginning 0)))))
  7227. (if (string-match "[*?{]" (file-name-nondirectory path))
  7228. (dired path)
  7229. (org-open-file path in-emacs line search)))
  7230. ((string= type "news")
  7231. (require 'org-gnus)
  7232. (org-gnus-follow-link path))
  7233. ((string= type "shell")
  7234. (let ((cmd path))
  7235. (if (or (not org-confirm-shell-link-function)
  7236. (funcall org-confirm-shell-link-function
  7237. (format "Execute \"%s\" in shell? "
  7238. (org-add-props cmd nil
  7239. 'face 'org-warning))))
  7240. (progn
  7241. (message "Executing %s" cmd)
  7242. (shell-command cmd))
  7243. (error "Abort"))))
  7244. ((string= type "elisp")
  7245. (let ((cmd path))
  7246. (if (or (not org-confirm-elisp-link-function)
  7247. (funcall org-confirm-elisp-link-function
  7248. (format "Execute \"%s\" as elisp? "
  7249. (org-add-props cmd nil
  7250. 'face 'org-warning))))
  7251. (message "%s => %s" cmd
  7252. (if (equal (string-to-char cmd) ?\()
  7253. (eval (read cmd))
  7254. (call-interactively (read cmd))))
  7255. (error "Abort"))))
  7256. (t
  7257. (browse-url-at-point))))))
  7258. (move-marker org-open-link-marker nil)
  7259. (run-hook-with-args 'org-follow-link-hook)))
  7260. (defun org-offer-links-in-entry (&optional nth zero)
  7261. "Offer links in the curren entry and follow the selected link.
  7262. If there is only one link, follow it immediately as well.
  7263. If NTH is an integer, immediately pick the NTH link found.
  7264. If ZERO is a string, check also this string for a link, and if
  7265. there is one, offer it as link number zero."
  7266. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7267. "\\(" org-angle-link-re "\\)\\|"
  7268. "\\(" org-plain-link-re "\\)"))
  7269. (cnt ?0)
  7270. (in-emacs (if (integerp nth) nil nth))
  7271. have-zero end links link c)
  7272. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7273. (push (match-string 0 zero) links)
  7274. (setq cnt (1- cnt) have-zero t))
  7275. (save-excursion
  7276. (org-back-to-heading t)
  7277. (setq end (save-excursion (outline-next-heading) (point)))
  7278. (while (re-search-forward re end t)
  7279. (push (match-string 0) links))
  7280. (setq links (org-uniquify (reverse links))))
  7281. (cond
  7282. ((null links) (error "No links"))
  7283. ((equal (length links) 1)
  7284. (setq link (car links)))
  7285. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7286. (setq link (nth (if have-zero nth (1- nth)) links)))
  7287. (t ; we have to select a link
  7288. (save-excursion
  7289. (save-window-excursion
  7290. (delete-other-windows)
  7291. (with-output-to-temp-buffer "*Select Link*"
  7292. (mapc (lambda (l)
  7293. (if (not (string-match org-bracket-link-regexp l))
  7294. (princ (format "[%c] %s\n" (incf cnt)
  7295. (org-remove-angle-brackets l)))
  7296. (if (match-end 3)
  7297. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7298. (match-string 3 l) (match-string 1 l)))
  7299. (princ (format "[%c] %s\n" (incf cnt)
  7300. (match-string 1 l))))))
  7301. links))
  7302. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7303. (message "Select link to open:")
  7304. (setq c (read-char-exclusive))
  7305. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7306. (when (equal c ?q) (error "Abort"))
  7307. (setq nth (- c ?0))
  7308. (if have-zero (setq nth (1+ nth)))
  7309. (unless (and (integerp nth) (>= (length links) nth))
  7310. (error "Invalid link selection"))
  7311. (setq link (nth (1- nth) links))))
  7312. (org-open-link-from-string link in-emacs (current-buffer))))
  7313. ;;;; Time estimates
  7314. (defun org-get-effort (&optional pom)
  7315. "Get the effort estimate for the current entry."
  7316. (org-entry-get pom org-effort-property))
  7317. ;;; File search
  7318. (defvar org-create-file-search-functions nil
  7319. "List of functions to construct the right search string for a file link.
  7320. These functions are called in turn with point at the location to
  7321. which the link should point.
  7322. A function in the hook should first test if it would like to
  7323. handle this file type, for example by checking the major-mode or
  7324. the file extension. If it decides not to handle this file, it
  7325. should just return nil to give other functions a chance. If it
  7326. does handle the file, it must return the search string to be used
  7327. when following the link. The search string will be part of the
  7328. file link, given after a double colon, and `org-open-at-point'
  7329. will automatically search for it. If special measures must be
  7330. taken to make the search successful, another function should be
  7331. added to the companion hook `org-execute-file-search-functions',
  7332. which see.
  7333. A function in this hook may also use `setq' to set the variable
  7334. `description' to provide a suggestion for the descriptive text to
  7335. be used for this link when it gets inserted into an Org-mode
  7336. buffer with \\[org-insert-link].")
  7337. (defvar org-execute-file-search-functions nil
  7338. "List of functions to execute a file search triggered by a link.
  7339. Functions added to this hook must accept a single argument, the
  7340. search string that was part of the file link, the part after the
  7341. double colon. The function must first check if it would like to
  7342. handle this search, for example by checking the major-mode or the
  7343. file extension. If it decides not to handle this search, it
  7344. should just return nil to give other functions a chance. If it
  7345. does handle the search, it must return a non-nil value to keep
  7346. other functions from trying.
  7347. Each function can access the current prefix argument through the
  7348. variable `current-prefix-argument'. Note that a single prefix is
  7349. used to force opening a link in Emacs, so it may be good to only
  7350. use a numeric or double prefix to guide the search function.
  7351. In case this is needed, a function in this hook can also restore
  7352. the window configuration before `org-open-at-point' was called using:
  7353. (set-window-configuration org-window-config-before-follow-link)")
  7354. (defun org-link-search (s &optional type avoid-pos)
  7355. "Search for a link search option.
  7356. If S is surrounded by forward slashes, it is interpreted as a
  7357. regular expression. In org-mode files, this will create an `org-occur'
  7358. sparse tree. In ordinary files, `occur' will be used to list matches.
  7359. If the current buffer is in `dired-mode', grep will be used to search
  7360. in all files. If AVOID-POS is given, ignore matches near that position."
  7361. (let ((case-fold-search t)
  7362. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7363. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7364. (append '(("") (" ") ("\t") ("\n"))
  7365. org-emphasis-alist)
  7366. "\\|") "\\)"))
  7367. (pos (point))
  7368. (pre nil) (post nil)
  7369. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7370. (cond
  7371. ;; First check if there are any special
  7372. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7373. ;; Now try the builtin stuff
  7374. ((and (equal (string-to-char s0) ?#)
  7375. (> (length s0) 1)
  7376. (save-excursion
  7377. (goto-char (point-min))
  7378. (and
  7379. (re-search-forward
  7380. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7381. (setq type 'dedicated
  7382. pos (match-beginning 0))))
  7383. ;; There is an exact target for this
  7384. (goto-char pos)
  7385. (org-back-to-heading t)))
  7386. ((save-excursion
  7387. (goto-char (point-min))
  7388. (and
  7389. (re-search-forward
  7390. (concat "<<" (regexp-quote s0) ">>") nil t)
  7391. (setq type 'dedicated
  7392. pos (match-beginning 0))))
  7393. ;; There is an exact target for this
  7394. (goto-char pos))
  7395. ((and (string-match "^(\\(.*\\))$" s0)
  7396. (save-excursion
  7397. (goto-char (point-min))
  7398. (and
  7399. (re-search-forward
  7400. (concat "[^[]" (regexp-quote
  7401. (format org-coderef-label-format
  7402. (match-string 1 s0))))
  7403. nil t)
  7404. (setq type 'dedicated
  7405. pos (1+ (match-beginning 0))))))
  7406. ;; There is a coderef target for this
  7407. (goto-char pos))
  7408. ((string-match "^/\\(.*\\)/$" s)
  7409. ;; A regular expression
  7410. (cond
  7411. ((org-mode-p)
  7412. (org-occur (match-string 1 s)))
  7413. ;;((eq major-mode 'dired-mode)
  7414. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7415. (t (org-do-occur (match-string 1 s)))))
  7416. (t
  7417. ;; A normal search strings
  7418. (when (equal (string-to-char s) ?*)
  7419. ;; Anchor on headlines, post may include tags.
  7420. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  7421. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  7422. s (substring s 1)))
  7423. (remove-text-properties
  7424. 0 (length s)
  7425. '(face nil mouse-face nil keymap nil fontified nil) s)
  7426. ;; Make a series of regular expressions to find a match
  7427. (setq words (org-split-string s "[ \n\r\t]+")
  7428. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7429. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  7430. "\\)" markers)
  7431. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7432. re2a (concat "[ \t\r\n]" re2a_)
  7433. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7434. re4 (concat "[^a-zA-Z_]" re4_)
  7435. re1 (concat pre re2 post)
  7436. re3 (concat pre (if pre re4_ re4) post)
  7437. re5 (concat pre ".*" re4)
  7438. re2 (concat pre re2)
  7439. re2a (concat pre (if pre re2a_ re2a))
  7440. re4 (concat pre (if pre re4_ re4))
  7441. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7442. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7443. re5 "\\)"
  7444. ))
  7445. (cond
  7446. ((eq type 'org-occur) (org-occur reall))
  7447. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7448. (t (goto-char (point-min))
  7449. (setq type 'fuzzy)
  7450. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  7451. (org-search-not-self 1 re1 nil t)
  7452. (org-search-not-self 1 re2 nil t)
  7453. (org-search-not-self 1 re2a nil t)
  7454. (org-search-not-self 1 re3 nil t)
  7455. (org-search-not-self 1 re4 nil t)
  7456. (org-search-not-self 1 re5 nil t)
  7457. )
  7458. (goto-char (match-beginning 1))
  7459. (goto-char pos)
  7460. (error "No match")))))
  7461. (t
  7462. ;; Normal string-search
  7463. (goto-char (point-min))
  7464. (if (search-forward s nil t)
  7465. (goto-char (match-beginning 0))
  7466. (error "No match"))))
  7467. (and (org-mode-p) (org-show-context 'link-search))
  7468. type))
  7469. (defun org-search-not-self (group &rest args)
  7470. "Execute `re-search-forward', but only accept matches that do not
  7471. enclose the position of `org-open-link-marker'."
  7472. (let ((m org-open-link-marker))
  7473. (catch 'exit
  7474. (while (apply 're-search-forward args)
  7475. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  7476. (goto-char (match-end group))
  7477. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  7478. (> (match-beginning 0) (marker-position m))
  7479. (< (match-end 0) (marker-position m)))
  7480. (save-match-data
  7481. (or (not (org-in-regexp
  7482. org-bracket-link-analytic-regexp 1))
  7483. (not (match-end 4)) ; no description
  7484. (and (<= (match-beginning 4) (point))
  7485. (>= (match-end 4) (point))))))
  7486. (throw 'exit (point))))))))
  7487. (defun org-get-buffer-for-internal-link (buffer)
  7488. "Return a buffer to be used for displaying the link target of internal links."
  7489. (cond
  7490. ((not org-display-internal-link-with-indirect-buffer)
  7491. buffer)
  7492. ((string-match "(Clone)$" (buffer-name buffer))
  7493. (message "Buffer is already a clone, not making another one")
  7494. ;; we also do not modify visibility in this case
  7495. buffer)
  7496. (t ; make a new indirect buffer for displaying the link
  7497. (let* ((bn (buffer-name buffer))
  7498. (ibn (concat bn "(Clone)"))
  7499. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  7500. (with-current-buffer ib (org-overview))
  7501. ib))))
  7502. (defun org-do-occur (regexp &optional cleanup)
  7503. "Call the Emacs command `occur'.
  7504. If CLEANUP is non-nil, remove the printout of the regular expression
  7505. in the *Occur* buffer. This is useful if the regex is long and not useful
  7506. to read."
  7507. (occur regexp)
  7508. (when cleanup
  7509. (let ((cwin (selected-window)) win beg end)
  7510. (when (setq win (get-buffer-window "*Occur*"))
  7511. (select-window win))
  7512. (goto-char (point-min))
  7513. (when (re-search-forward "match[a-z]+" nil t)
  7514. (setq beg (match-end 0))
  7515. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7516. (setq end (1- (match-beginning 0)))))
  7517. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  7518. (goto-char (point-min))
  7519. (select-window cwin))))
  7520. ;;; The mark ring for links jumps
  7521. (defvar org-mark-ring nil
  7522. "Mark ring for positions before jumps in Org-mode.")
  7523. (defvar org-mark-ring-last-goto nil
  7524. "Last position in the mark ring used to go back.")
  7525. ;; Fill and close the ring
  7526. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7527. (loop for i from 1 to org-mark-ring-length do
  7528. (push (make-marker) org-mark-ring))
  7529. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7530. org-mark-ring)
  7531. (defun org-mark-ring-push (&optional pos buffer)
  7532. "Put the current position or POS into the mark ring and rotate it."
  7533. (interactive)
  7534. (setq pos (or pos (point)))
  7535. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7536. (move-marker (car org-mark-ring)
  7537. (or pos (point))
  7538. (or buffer (current-buffer)))
  7539. (message "%s"
  7540. (substitute-command-keys
  7541. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7542. (defun org-mark-ring-goto (&optional n)
  7543. "Jump to the previous position in the mark ring.
  7544. With prefix arg N, jump back that many stored positions. When
  7545. called several times in succession, walk through the entire ring.
  7546. Org-mode commands jumping to a different position in the current file,
  7547. or to another Org-mode file, automatically push the old position
  7548. onto the ring."
  7549. (interactive "p")
  7550. (let (p m)
  7551. (if (eq last-command this-command)
  7552. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7553. (setq p org-mark-ring))
  7554. (setq org-mark-ring-last-goto p)
  7555. (setq m (car p))
  7556. (switch-to-buffer (marker-buffer m))
  7557. (goto-char m)
  7558. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  7559. (defun org-remove-angle-brackets (s)
  7560. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7561. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7562. s)
  7563. (defun org-add-angle-brackets (s)
  7564. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7565. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7566. s)
  7567. (defun org-remove-double-quotes (s)
  7568. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  7569. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  7570. s)
  7571. ;;; Following specific links
  7572. (defun org-follow-timestamp-link ()
  7573. (cond
  7574. ((org-at-date-range-p t)
  7575. (let ((org-agenda-start-on-weekday)
  7576. (t1 (match-string 1))
  7577. (t2 (match-string 2)))
  7578. (setq t1 (time-to-days (org-time-string-to-time t1))
  7579. t2 (time-to-days (org-time-string-to-time t2)))
  7580. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  7581. ((org-at-timestamp-p t)
  7582. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7583. (substring (match-string 1) 0 10)))
  7584. 1))
  7585. (t (error "This should not happen"))))
  7586. ;;; Following file links
  7587. (defvar org-wait nil)
  7588. (defun org-open-file (path &optional in-emacs line search)
  7589. "Open the file at PATH.
  7590. First, this expands any special file name abbreviations. Then the
  7591. configuration variable `org-file-apps' is checked if it contains an
  7592. entry for this file type, and if yes, the corresponding command is launched.
  7593. If no application is found, Emacs simply visits the file.
  7594. With optional prefix argument IN-EMACS, Emacs will visit the file.
  7595. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  7596. and o use an external application to visit the file.
  7597. Optional LINE specifies a line to go to, optional SEARCH a string to
  7598. search for. If LINE or SEARCH is given, the file will always be
  7599. opened in Emacs.
  7600. If the file does not exist, an error is thrown."
  7601. (setq in-emacs (or in-emacs line search))
  7602. (let* ((file (if (equal path "")
  7603. buffer-file-name
  7604. (substitute-in-file-name (expand-file-name path))))
  7605. (apps (append org-file-apps (org-default-apps)))
  7606. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  7607. (dirp (if remp nil (file-directory-p file)))
  7608. (file (if (and dirp org-open-directory-means-index-dot-org)
  7609. (concat (file-name-as-directory file) "index.org")
  7610. file))
  7611. (a-m-a-p (assq 'auto-mode apps))
  7612. (dfile (downcase file))
  7613. (old-buffer (current-buffer))
  7614. (old-pos (point))
  7615. (old-mode major-mode)
  7616. ext cmd)
  7617. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7618. (setq ext (match-string 1 dfile))
  7619. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7620. (setq ext (match-string 1 dfile))))
  7621. (cond
  7622. ((equal in-emacs '(16))
  7623. (setq cmd (cdr (assoc 'system apps))))
  7624. (in-emacs (setq cmd 'emacs))
  7625. (t
  7626. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  7627. (and dirp (cdr (assoc 'directory apps)))
  7628. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  7629. 'string-match)
  7630. (cdr (assoc ext apps))
  7631. (cdr (assoc t apps))))))
  7632. (when (eq cmd 'system)
  7633. (setq cmd (cdr (assoc 'system apps))))
  7634. (when (eq cmd 'default)
  7635. (setq cmd (cdr (assoc t apps))))
  7636. (when (eq cmd 'mailcap)
  7637. (require 'mailcap)
  7638. (mailcap-parse-mailcaps)
  7639. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7640. (command (mailcap-mime-info mime-type)))
  7641. (if (stringp command)
  7642. (setq cmd command)
  7643. (setq cmd 'emacs))))
  7644. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  7645. (not (file-exists-p file))
  7646. (not org-open-non-existing-files))
  7647. (error "No such file: %s" file))
  7648. (cond
  7649. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7650. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  7651. (while (string-match "['\"]%s['\"]" cmd)
  7652. (setq cmd (replace-match "%s" t t cmd)))
  7653. (while (string-match "%s" cmd)
  7654. (setq cmd (replace-match
  7655. (save-match-data
  7656. (shell-quote-argument
  7657. (convert-standard-filename file)))
  7658. t t cmd)))
  7659. (save-window-excursion
  7660. (start-process-shell-command cmd nil cmd)
  7661. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7662. ))
  7663. ((or (stringp cmd)
  7664. (eq cmd 'emacs))
  7665. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7666. (widen)
  7667. (if line (org-goto-line line)
  7668. (if search (org-link-search search))))
  7669. ((consp cmd)
  7670. (let ((file (convert-standard-filename file)))
  7671. (eval cmd)))
  7672. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7673. (and (org-mode-p) (eq old-mode 'org-mode)
  7674. (or (not (equal old-buffer (current-buffer)))
  7675. (not (equal old-pos (point))))
  7676. (org-mark-ring-push old-pos old-buffer))))
  7677. (defun org-default-apps ()
  7678. "Return the default applications for this operating system."
  7679. (cond
  7680. ((eq system-type 'darwin)
  7681. org-file-apps-defaults-macosx)
  7682. ((eq system-type 'windows-nt)
  7683. org-file-apps-defaults-windowsnt)
  7684. (t org-file-apps-defaults-gnu)))
  7685. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7686. "Convert extensions to regular expressions in the cars of LIST.
  7687. Also, weed out any non-string entries, because the return value is used
  7688. only for regexp matching.
  7689. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7690. point to the symbol `emacs', indicating that the file should
  7691. be opened in Emacs."
  7692. (append
  7693. (delq nil
  7694. (mapcar (lambda (x)
  7695. (if (not (stringp (car x)))
  7696. nil
  7697. (if (string-match "\\W" (car x))
  7698. x
  7699. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7700. list))
  7701. (if add-auto-mode
  7702. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7703. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7704. (defun org-file-remote-p (file)
  7705. "Test whether FILE specifies a location on a remote system.
  7706. Return non-nil if the location is indeed remote.
  7707. For example, the filename \"/user@host:/foo\" specifies a location
  7708. on the system \"/user@host:\"."
  7709. (cond ((fboundp 'file-remote-p)
  7710. (file-remote-p file))
  7711. ((fboundp 'tramp-handle-file-remote-p)
  7712. (tramp-handle-file-remote-p file))
  7713. ((and (boundp 'ange-ftp-name-format)
  7714. (string-match (car ange-ftp-name-format) file))
  7715. t)
  7716. (t nil)))
  7717. ;;;; Refiling
  7718. (defun org-get-org-file ()
  7719. "Read a filename, with default directory `org-directory'."
  7720. (let ((default (or org-default-notes-file remember-data-file)))
  7721. (read-file-name (format "File name [%s]: " default)
  7722. (file-name-as-directory org-directory)
  7723. default)))
  7724. (defun org-notes-order-reversed-p ()
  7725. "Check if the current file should receive notes in reversed order."
  7726. (cond
  7727. ((not org-reverse-note-order) nil)
  7728. ((eq t org-reverse-note-order) t)
  7729. ((not (listp org-reverse-note-order)) nil)
  7730. (t (catch 'exit
  7731. (let ((all org-reverse-note-order)
  7732. entry)
  7733. (while (setq entry (pop all))
  7734. (if (string-match (car entry) buffer-file-name)
  7735. (throw 'exit (cdr entry))))
  7736. nil)))))
  7737. (defvar org-refile-target-table nil
  7738. "The list of refile targets, created by `org-refile'.")
  7739. (defvar org-agenda-new-buffers nil
  7740. "Buffers created to visit agenda files.")
  7741. (defun org-get-refile-targets (&optional default-buffer)
  7742. "Produce a table with refile targets."
  7743. (let ((case-fold-search nil)
  7744. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  7745. (entries (or org-refile-targets '((nil . (:level . 1)))))
  7746. targets txt re files f desc descre fast-path-p level pos0)
  7747. (message "Getting targets...")
  7748. (with-current-buffer (or default-buffer (current-buffer))
  7749. (while (setq entry (pop entries))
  7750. (setq files (car entry) desc (cdr entry))
  7751. (setq fast-path-p nil)
  7752. (cond
  7753. ((null files) (setq files (list (current-buffer))))
  7754. ((eq files 'org-agenda-files)
  7755. (setq files (org-agenda-files 'unrestricted)))
  7756. ((and (symbolp files) (fboundp files))
  7757. (setq files (funcall files)))
  7758. ((and (symbolp files) (boundp files))
  7759. (setq files (symbol-value files))))
  7760. (if (stringp files) (setq files (list files)))
  7761. (cond
  7762. ((eq (car desc) :tag)
  7763. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7764. ((eq (car desc) :todo)
  7765. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7766. ((eq (car desc) :regexp)
  7767. (setq descre (cdr desc)))
  7768. ((eq (car desc) :level)
  7769. (setq descre (concat "^\\*\\{" (number-to-string
  7770. (if org-odd-levels-only
  7771. (1- (* 2 (cdr desc)))
  7772. (cdr desc)))
  7773. "\\}[ \t]")))
  7774. ((eq (car desc) :maxlevel)
  7775. (setq fast-path-p t)
  7776. (setq descre (concat "^\\*\\{1," (number-to-string
  7777. (if org-odd-levels-only
  7778. (1- (* 2 (cdr desc)))
  7779. (cdr desc)))
  7780. "\\}[ \t]")))
  7781. (t (error "Bad refiling target description %s" desc)))
  7782. (while (setq f (pop files))
  7783. (save-excursion
  7784. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7785. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7786. (setq f (expand-file-name f))
  7787. (if (eq org-refile-use-outline-path 'file)
  7788. (push (list (file-name-nondirectory f) f nil nil) targets))
  7789. (save-excursion
  7790. (save-restriction
  7791. (widen)
  7792. (goto-char (point-min))
  7793. (while (re-search-forward descre nil t)
  7794. (goto-char (setq pos0 (point-at-bol)))
  7795. (catch 'next
  7796. (when org-refile-target-verify-function
  7797. (save-match-data
  7798. (or (funcall org-refile-target-verify-function)
  7799. (throw 'next t))))
  7800. (when (looking-at org-complex-heading-regexp)
  7801. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7802. txt (org-link-display-format (match-string 4))
  7803. re (concat "^" (regexp-quote
  7804. (buffer-substring (match-beginning 1)
  7805. (match-end 4)))))
  7806. (if (match-end 5) (setq re (concat re "[ \t]+"
  7807. (regexp-quote
  7808. (match-string 5)))))
  7809. (setq re (concat re "[ \t]*$"))
  7810. (when org-refile-use-outline-path
  7811. (setq txt (mapconcat 'org-protect-slash
  7812. (append
  7813. (if (eq org-refile-use-outline-path 'file)
  7814. (list (file-name-nondirectory
  7815. (buffer-file-name (buffer-base-buffer))))
  7816. (if (eq org-refile-use-outline-path 'full-file-path)
  7817. (list (buffer-file-name (buffer-base-buffer)))))
  7818. (org-get-outline-path fast-path-p level txt)
  7819. (list txt))
  7820. "/")))
  7821. (push (list txt f re (point)) targets)))
  7822. (when (= (point) pos0)
  7823. ;; verification function has not moved point
  7824. (goto-char (point-at-eol))))))))))
  7825. (message "Getting targets...done")
  7826. (nreverse targets)))
  7827. (defun org-protect-slash (s)
  7828. (while (string-match "/" s)
  7829. (setq s (replace-match "\\" t t s)))
  7830. s)
  7831. (defvar org-olpa (make-vector 20 nil))
  7832. (defun org-get-outline-path (&optional fastp level heading)
  7833. "Return the outline path to the current entry, as a list."
  7834. (if fastp
  7835. (progn
  7836. (if (> level 19)
  7837. (error "Outline path failure, more than 19 levels."))
  7838. (loop for i from level upto 19 do
  7839. (aset org-olpa i nil))
  7840. (prog1
  7841. (delq nil (append org-olpa nil))
  7842. (aset org-olpa level heading)))
  7843. (let (rtn)
  7844. (save-excursion
  7845. (while (org-up-heading-safe)
  7846. (when (looking-at org-complex-heading-regexp)
  7847. (push (org-match-string-no-properties 4) rtn)))
  7848. rtn))))
  7849. (defvar org-refile-history nil
  7850. "History for refiling operations.")
  7851. (defvar org-after-refile-insert-hook nil
  7852. "Hook run after `org-refile' has inserted its stuff at the new location.
  7853. Note that this is still *before* the stuff will be removed from
  7854. the *old* location.")
  7855. (defun org-refile (&optional goto default-buffer rfloc)
  7856. "Move the entry at point to another heading.
  7857. The list of target headings is compiled using the information in
  7858. `org-refile-targets', which see. This list is created before each use
  7859. and will therefore always be up-to-date.
  7860. At the target location, the entry is filed as a subitem of the target heading.
  7861. Depending on `org-reverse-note-order', the new subitem will either be the
  7862. first or the last subitem.
  7863. If there is an active region, all entries in that region will be moved.
  7864. However, the region must fulfil the requirement that the first heading
  7865. is the first one sets the top-level of the moved text - at most siblings
  7866. below it are allowed.
  7867. With prefix arg GOTO, the command will only visit the target location,
  7868. not actually move anything.
  7869. With a double prefix `C-u C-u', go to the location where the last refiling
  7870. operation has put the subtree.
  7871. RFLOC can be a refile location obtained in a different way.
  7872. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  7873. (interactive "P")
  7874. (let* ((cbuf (current-buffer))
  7875. (regionp (org-region-active-p))
  7876. (region-start (and regionp (region-beginning)))
  7877. (region-end (and regionp (region-end)))
  7878. (region-length (and regionp (- region-end region-start)))
  7879. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7880. pos it nbuf file re level reversed)
  7881. (when regionp
  7882. (goto-char region-start)
  7883. (or (bolp) (goto-char (point-at-bol)))
  7884. (setq region-start (point))
  7885. (unless (org-kill-is-subtree-p
  7886. (buffer-substring region-start region-end))
  7887. (error "The region is not a (sequence of) subtree(s)")))
  7888. (if (equal goto '(16))
  7889. (org-refile-goto-last-stored)
  7890. (when (setq it (or rfloc
  7891. (save-excursion
  7892. (org-refile-get-location
  7893. (if goto "Goto: " "Refile to: ") default-buffer
  7894. org-refile-allow-creating-parent-nodes))))
  7895. (setq file (nth 1 it)
  7896. re (nth 2 it)
  7897. pos (nth 3 it))
  7898. (if (and (not goto)
  7899. pos
  7900. (equal (buffer-file-name) file)
  7901. (if regionp
  7902. (and (>= pos region-start)
  7903. (<= pos region-end))
  7904. (and (>= pos (point))
  7905. (< pos (save-excursion
  7906. (org-end-of-subtree t t))))))
  7907. (error "Cannot refile to position inside the tree or region"))
  7908. (setq nbuf (or (find-buffer-visiting file)
  7909. (find-file-noselect file)))
  7910. (if goto
  7911. (progn
  7912. (switch-to-buffer nbuf)
  7913. (goto-char pos)
  7914. (org-show-context 'org-goto))
  7915. (if regionp
  7916. (progn
  7917. (org-kill-new (buffer-substring region-start region-end))
  7918. (org-save-markers-in-region region-start region-end))
  7919. (org-copy-subtree 1 nil t))
  7920. (save-excursion
  7921. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7922. (find-file-noselect file))))
  7923. (setq reversed (org-notes-order-reversed-p))
  7924. (save-excursion
  7925. (save-restriction
  7926. (widen)
  7927. (if pos
  7928. (progn
  7929. (goto-char pos)
  7930. (looking-at outline-regexp)
  7931. (setq level (org-get-valid-level (funcall outline-level) 1))
  7932. (goto-char
  7933. (if reversed
  7934. (or (outline-next-heading) (point-max))
  7935. (or (save-excursion (org-get-next-sibling))
  7936. (org-end-of-subtree t t)
  7937. (point-max)))))
  7938. (setq level 1)
  7939. (if (not reversed)
  7940. (goto-char (point-max))
  7941. (goto-char (point-min))
  7942. (or (outline-next-heading) (goto-char (point-max)))))
  7943. (if (not (bolp)) (newline))
  7944. (bookmark-set "org-refile-last-stored")
  7945. (org-paste-subtree level)
  7946. (if (fboundp 'deactivate-mark) (deactivate-mark))
  7947. (run-hooks 'org-after-refile-insert-hook))))
  7948. (if regionp
  7949. (delete-region (point) (+ (point) region-length))
  7950. (org-cut-subtree))
  7951. (when (featurep 'org-inlinetask)
  7952. (org-inlinetask-remove-END-maybe))
  7953. (setq org-markers-to-move nil)
  7954. (message "Refiled to \"%s\"" (car it))))))
  7955. (org-reveal))
  7956. (defun org-refile-goto-last-stored ()
  7957. "Go to the location where the last refile was stored."
  7958. (interactive)
  7959. (bookmark-jump "org-refile-last-stored")
  7960. (message "This is the location of the last refile"))
  7961. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  7962. "Prompt the user for a refile location, using PROMPT."
  7963. (let ((org-refile-targets org-refile-targets)
  7964. (org-refile-use-outline-path org-refile-use-outline-path))
  7965. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7966. (unless org-refile-target-table
  7967. (error "No refile targets"))
  7968. (let* ((cbuf (current-buffer))
  7969. (partial-completion-mode nil)
  7970. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7971. (cfunc (if (and org-refile-use-outline-path
  7972. org-outline-path-complete-in-steps)
  7973. 'org-olpath-completing-read
  7974. 'org-icompleting-read))
  7975. (extra (if org-refile-use-outline-path "/" ""))
  7976. (filename (and cfn (expand-file-name cfn)))
  7977. (tbl (mapcar
  7978. (lambda (x)
  7979. (if (and (not (member org-refile-use-outline-path
  7980. '(file full-file-path)))
  7981. (not (equal filename (nth 1 x))))
  7982. (cons (concat (car x) extra " ("
  7983. (file-name-nondirectory (nth 1 x)) ")")
  7984. (cdr x))
  7985. (cons (concat (car x) extra) (cdr x))))
  7986. org-refile-target-table))
  7987. (completion-ignore-case t)
  7988. pa answ parent-target child parent old-hist)
  7989. (setq old-hist org-refile-history)
  7990. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  7991. nil 'org-refile-history))
  7992. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  7993. (if pa
  7994. (progn
  7995. (when (or (not org-refile-history)
  7996. (not (eq old-hist org-refile-history))
  7997. (not (equal (car pa) (car org-refile-history))))
  7998. (setq org-refile-history
  7999. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8000. org-refile-history
  8001. (cdr org-refile-history))))
  8002. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8003. (pop org-refile-history)))
  8004. pa)
  8005. (when (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8006. (setq parent (match-string 1 answ)
  8007. child (match-string 2 answ))
  8008. (setq parent-target (or (assoc parent tbl) (assoc (concat parent "/") tbl)))
  8009. (when (and parent-target
  8010. (or (eq new-nodes t)
  8011. (and (eq new-nodes 'confirm)
  8012. (y-or-n-p (format "Create new node \"%s\"? " child)))))
  8013. (org-refile-new-child parent-target child))))))
  8014. (defun org-refile-new-child (parent-target child)
  8015. "Use refile target PARENT-TARGET to add new CHILD below it."
  8016. (unless parent-target
  8017. (error "Cannot find parent for new node"))
  8018. (let ((file (nth 1 parent-target))
  8019. (pos (nth 3 parent-target))
  8020. level)
  8021. (with-current-buffer (or (find-buffer-visiting file)
  8022. (find-file-noselect file))
  8023. (save-excursion
  8024. (save-restriction
  8025. (widen)
  8026. (if pos
  8027. (goto-char pos)
  8028. (goto-char (point-max))
  8029. (if (not (bolp)) (newline)))
  8030. (when (looking-at outline-regexp)
  8031. (setq level (funcall outline-level))
  8032. (org-end-of-subtree t t))
  8033. (org-back-over-empty-lines)
  8034. (insert "\n" (make-string
  8035. (if pos (org-get-valid-level level 1) 1) ?*)
  8036. " " child "\n")
  8037. (beginning-of-line 0)
  8038. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8039. (defun org-olpath-completing-read (prompt collection &rest args)
  8040. "Read an outline path like a file name."
  8041. (let ((thetable collection)
  8042. (org-completion-use-ido nil) ; does not work with ido.
  8043. (org-completion-use-iswitchb nil)) ; or iswitchb
  8044. (apply
  8045. 'org-icompleting-read prompt
  8046. (lambda (string predicate &optional flag)
  8047. (let (rtn r f (l (length string)))
  8048. (cond
  8049. ((eq flag nil)
  8050. ;; try completion
  8051. (try-completion string thetable))
  8052. ((eq flag t)
  8053. ;; all-completions
  8054. (setq rtn (all-completions string thetable predicate))
  8055. (mapcar
  8056. (lambda (x)
  8057. (setq r (substring x l))
  8058. (if (string-match " ([^)]*)$" x)
  8059. (setq f (match-string 0 x))
  8060. (setq f ""))
  8061. (if (string-match "/" r)
  8062. (concat string (substring r 0 (match-end 0)) f)
  8063. x))
  8064. rtn))
  8065. ((eq flag 'lambda)
  8066. ;; exact match?
  8067. (assoc string thetable)))
  8068. ))
  8069. args)))
  8070. ;;;; Dynamic blocks
  8071. (defun org-find-dblock (name)
  8072. "Find the first dynamic block with name NAME in the buffer.
  8073. If not found, stay at current position and return nil."
  8074. (let (pos)
  8075. (save-excursion
  8076. (goto-char (point-min))
  8077. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8078. nil t)
  8079. (match-beginning 0))))
  8080. (if pos (goto-char pos))
  8081. pos))
  8082. (defconst org-dblock-start-re
  8083. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8084. "Matches the startline of a dynamic block, with parameters.")
  8085. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  8086. "Matches the end of a dynamic block.")
  8087. (defun org-create-dblock (plist)
  8088. "Create a dynamic block section, with parameters taken from PLIST.
  8089. PLIST must contain a :name entry which is used as name of the block."
  8090. (unless (bolp) (newline))
  8091. (let ((name (plist-get plist :name)))
  8092. (insert "#+BEGIN: " name)
  8093. (while plist
  8094. (if (eq (car plist) :name)
  8095. (setq plist (cddr plist))
  8096. (insert " " (prin1-to-string (pop plist)))))
  8097. (insert "\n\n#+END:\n")
  8098. (beginning-of-line -2)))
  8099. (defun org-prepare-dblock ()
  8100. "Prepare dynamic block for refresh.
  8101. This empties the block, puts the cursor at the insert position and returns
  8102. the property list including an extra property :name with the block name."
  8103. (unless (looking-at org-dblock-start-re)
  8104. (error "Not at a dynamic block"))
  8105. (let* ((begdel (1+ (match-end 0)))
  8106. (name (org-no-properties (match-string 1)))
  8107. (params (append (list :name name)
  8108. (read (concat "(" (match-string 3) ")")))))
  8109. (unless (re-search-forward org-dblock-end-re nil t)
  8110. (error "Dynamic block not terminated"))
  8111. (setq params
  8112. (append params
  8113. (list :content (buffer-substring
  8114. begdel (match-beginning 0)))))
  8115. (delete-region begdel (match-beginning 0))
  8116. (goto-char begdel)
  8117. (open-line 1)
  8118. params))
  8119. (defun org-map-dblocks (&optional command)
  8120. "Apply COMMAND to all dynamic blocks in the current buffer.
  8121. If COMMAND is not given, use `org-update-dblock'."
  8122. (let ((cmd (or command 'org-update-dblock))
  8123. pos)
  8124. (save-excursion
  8125. (goto-char (point-min))
  8126. (while (re-search-forward org-dblock-start-re nil t)
  8127. (goto-char (setq pos (match-beginning 0)))
  8128. (condition-case nil
  8129. (funcall cmd)
  8130. (error (message "Error during update of dynamic block")))
  8131. (goto-char pos)
  8132. (unless (re-search-forward org-dblock-end-re nil t)
  8133. (error "Dynamic block not terminated"))))))
  8134. (defun org-dblock-update (&optional arg)
  8135. "User command for updating dynamic blocks.
  8136. Update the dynamic block at point. With prefix ARG, update all dynamic
  8137. blocks in the buffer."
  8138. (interactive "P")
  8139. (if arg
  8140. (org-update-all-dblocks)
  8141. (or (looking-at org-dblock-start-re)
  8142. (org-beginning-of-dblock))
  8143. (org-update-dblock)))
  8144. (defun org-update-dblock ()
  8145. "Update the dynamic block at point
  8146. This means to empty the block, parse for parameters and then call
  8147. the correct writing function."
  8148. (save-window-excursion
  8149. (let* ((pos (point))
  8150. (line (org-current-line))
  8151. (params (org-prepare-dblock))
  8152. (name (plist-get params :name))
  8153. (cmd (intern (concat "org-dblock-write:" name))))
  8154. (message "Updating dynamic block `%s' at line %d..." name line)
  8155. (funcall cmd params)
  8156. (message "Updating dynamic block `%s' at line %d...done" name line)
  8157. (goto-char pos))))
  8158. (defun org-beginning-of-dblock ()
  8159. "Find the beginning of the dynamic block at point.
  8160. Error if there is no such block at point."
  8161. (let ((pos (point))
  8162. beg)
  8163. (end-of-line 1)
  8164. (if (and (re-search-backward org-dblock-start-re nil t)
  8165. (setq beg (match-beginning 0))
  8166. (re-search-forward org-dblock-end-re nil t)
  8167. (> (match-end 0) pos))
  8168. (goto-char beg)
  8169. (goto-char pos)
  8170. (error "Not in a dynamic block"))))
  8171. (defun org-update-all-dblocks ()
  8172. "Update all dynamic blocks in the buffer.
  8173. This function can be used in a hook."
  8174. (when (org-mode-p)
  8175. (org-map-dblocks 'org-update-dblock)))
  8176. ;;;; Completion
  8177. (defconst org-additional-option-like-keywords
  8178. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8179. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8180. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:" "ATTR_LaTeX"
  8181. "BEGIN:" "END:"
  8182. "ORGTBL" "TBLFM:" "TBLNAME:"
  8183. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8184. "BEGIN_QUOTE" "END_QUOTE"
  8185. "BEGIN_VERSE" "END_VERSE"
  8186. "BEGIN_CENTER" "END_CENTER"
  8187. "BEGIN_SRC" "END_SRC"
  8188. "CATEGORY" "COLUMNS"
  8189. "CAPTION" "LABEL"
  8190. "BIND"
  8191. "MACRO"))
  8192. (defcustom org-structure-template-alist
  8193. '(
  8194. ("s" "#+begin_src ?\n\n#+end_src"
  8195. "<src lang=\"?\">\n\n</src>")
  8196. ("e" "#+begin_example\n?\n#+end_example"
  8197. "<example>\n?\n</example>")
  8198. ("q" "#+begin_quote\n?\n#+end_quote"
  8199. "<quote>\n?\n</quote>")
  8200. ("v" "#+begin_verse\n?\n#+end_verse"
  8201. "<verse>\n?\n/verse>")
  8202. ("c" "#+begin_center\n?\n#+end_center"
  8203. "<center>\n?\n/center>")
  8204. ("l" "#+begin_latex\n?\n#+end_latex"
  8205. "<literal style=\"latex\">\n?\n</literal>")
  8206. ("L" "#+latex: "
  8207. "<literal style=\"latex\">?</literal>")
  8208. ("h" "#+begin_html\n?\n#+end_html"
  8209. "<literal style=\"html\">\n?\n</literal>")
  8210. ("H" "#+html: "
  8211. "<literal style=\"html\">?</literal>")
  8212. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8213. ("A" "#+ascii: ")
  8214. ("i" "#+include %file ?"
  8215. "<include file=%file markup=\"?\">")
  8216. )
  8217. "Structure completion elements.
  8218. This is a list of abbreviation keys and values. The value gets inserted
  8219. it you type @samp{.} followed by the key and then the completion key,
  8220. usually `M-TAB'. %file will be replaced by a file name after prompting
  8221. for the file using completion.
  8222. There are two templates for each key, the first uses the original Org syntax,
  8223. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8224. the default when the /org-mtags.el/ module has been loaded. See also the
  8225. variable `org-mtags-prefer-muse-templates'.
  8226. This is an experimental feature, it is undecided if it is going to stay in."
  8227. :group 'org-completion
  8228. :type '(repeat
  8229. (string :tag "Key")
  8230. (string :tag "Template")
  8231. (string :tag "Muse Template")))
  8232. (defun org-try-structure-completion ()
  8233. "Try to complete a structure template before point.
  8234. This looks for strings like \"<e\" on an otherwise empty line and
  8235. expands them."
  8236. (let ((l (buffer-substring (point-at-bol) (point)))
  8237. a)
  8238. (when (and (looking-at "[ \t]*$")
  8239. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8240. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8241. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8242. (match-beginning 1)) a)
  8243. t)))
  8244. (defun org-complete-expand-structure-template (start cell)
  8245. "Expand a structure template."
  8246. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8247. (rpl (nth (if musep 2 1) cell))
  8248. (ind ""))
  8249. (delete-region start (point))
  8250. (when (string-match "\\`#\\+" rpl)
  8251. (cond
  8252. ((bolp))
  8253. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8254. (setq ind (buffer-substring (point-at-bol) (point))))
  8255. (t (newline))))
  8256. (setq start (point))
  8257. (if (string-match "%file" rpl)
  8258. (setq rpl (replace-match
  8259. (concat
  8260. "\""
  8261. (save-match-data
  8262. (abbreviate-file-name (read-file-name "Include file: ")))
  8263. "\"")
  8264. t t rpl)))
  8265. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8266. (concat "\n" ind)))
  8267. (insert rpl)
  8268. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8269. (defun org-complete (&optional arg)
  8270. "Perform completion on word at point.
  8271. At the beginning of a headline, this completes TODO keywords as given in
  8272. `org-todo-keywords'.
  8273. If the current word is preceded by a backslash, completes the TeX symbols
  8274. that are supported for HTML support.
  8275. If the current word is preceded by \"#+\", completes special words for
  8276. setting file options.
  8277. In the line after \"#+STARTUP:, complete valid keywords.\"
  8278. At all other locations, this simply calls the value of
  8279. `org-completion-fallback-command'."
  8280. (interactive "P")
  8281. (org-without-partial-completion
  8282. (catch 'exit
  8283. (let* ((a nil)
  8284. (end (point))
  8285. (beg1 (save-excursion
  8286. (skip-chars-backward (org-re "[:alnum:]_@"))
  8287. (point)))
  8288. (beg (save-excursion
  8289. (skip-chars-backward "a-zA-Z0-9_:$")
  8290. (point)))
  8291. (confirm (lambda (x) (stringp (car x))))
  8292. (searchhead (equal (char-before beg) ?*))
  8293. (struct
  8294. (when (and (member (char-before beg1) '(?. ?<))
  8295. (setq a (assoc (buffer-substring beg1 (point))
  8296. org-structure-template-alist)))
  8297. (org-complete-expand-structure-template (1- beg1) a)
  8298. (throw 'exit t)))
  8299. (tag (and (equal (char-before beg1) ?:)
  8300. (equal (char-after (point-at-bol)) ?*)))
  8301. (prop (and (equal (char-before beg1) ?:)
  8302. (not (equal (char-after (point-at-bol)) ?*))))
  8303. (texp (equal (char-before beg) ?\\))
  8304. (link (equal (char-before beg) ?\[))
  8305. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  8306. beg)
  8307. "#+"))
  8308. (startup (string-match "^#\\+STARTUP:.*"
  8309. (buffer-substring (point-at-bol) (point))))
  8310. (completion-ignore-case opt)
  8311. (type nil)
  8312. (tbl nil)
  8313. (table (cond
  8314. (opt
  8315. (setq type :opt)
  8316. (require 'org-exp)
  8317. (append
  8318. (delq nil
  8319. (mapcar
  8320. (lambda (x)
  8321. (if (string-match
  8322. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  8323. (cons (match-string 2 x)
  8324. (match-string 1 x))))
  8325. (org-split-string (org-get-current-options) "\n")))
  8326. (mapcar 'list org-additional-option-like-keywords)))
  8327. (startup
  8328. (setq type :startup)
  8329. org-startup-options)
  8330. (link (append org-link-abbrev-alist-local
  8331. org-link-abbrev-alist))
  8332. (texp
  8333. (setq type :tex)
  8334. org-html-entities)
  8335. ((string-match "\\`\\*+[ \t]+\\'"
  8336. (buffer-substring (point-at-bol) beg))
  8337. (setq type :todo)
  8338. (mapcar 'list org-todo-keywords-1))
  8339. (searchhead
  8340. (setq type :searchhead)
  8341. (save-excursion
  8342. (goto-char (point-min))
  8343. (while (re-search-forward org-todo-line-regexp nil t)
  8344. (push (list
  8345. (org-make-org-heading-search-string
  8346. (match-string 3) t))
  8347. tbl)))
  8348. tbl)
  8349. (tag (setq type :tag beg beg1)
  8350. (or org-tag-alist (org-get-buffer-tags)))
  8351. (prop (setq type :prop beg beg1)
  8352. (mapcar 'list (org-buffer-property-keys nil t t)))
  8353. (t (progn
  8354. (call-interactively org-completion-fallback-command)
  8355. (throw 'exit nil)))))
  8356. (pattern (buffer-substring-no-properties beg end))
  8357. (completion (try-completion pattern table confirm)))
  8358. (cond ((eq completion t)
  8359. (if (not (assoc (upcase pattern) table))
  8360. (message "Already complete")
  8361. (if (and (equal type :opt)
  8362. (not (member (car (assoc (upcase pattern) table))
  8363. org-additional-option-like-keywords)))
  8364. (insert (substring (cdr (assoc (upcase pattern) table))
  8365. (length pattern)))
  8366. (if (memq type '(:tag :prop)) (insert ":")))))
  8367. ((null completion)
  8368. (message "Can't find completion for \"%s\"" pattern)
  8369. (ding))
  8370. ((not (string= pattern completion))
  8371. (delete-region beg end)
  8372. (if (string-match " +$" completion)
  8373. (setq completion (replace-match "" t t completion)))
  8374. (insert completion)
  8375. (if (get-buffer-window "*Completions*")
  8376. (delete-window (get-buffer-window "*Completions*")))
  8377. (if (assoc completion table)
  8378. (if (eq type :todo) (insert " ")
  8379. (if (memq type '(:tag :prop)) (insert ":"))))
  8380. (if (and (equal type :opt) (assoc completion table))
  8381. (message "%s" (substitute-command-keys
  8382. "Press \\[org-complete] again to insert example settings"))))
  8383. (t
  8384. (message "Making completion list...")
  8385. (let ((list (sort (all-completions pattern table confirm)
  8386. 'string<)))
  8387. (with-output-to-temp-buffer "*Completions*"
  8388. (condition-case nil
  8389. ;; Protection needed for XEmacs and emacs 21
  8390. (display-completion-list list pattern)
  8391. (error (display-completion-list list)))))
  8392. (message "Making completion list...%s" "done")))))))
  8393. ;;;; TODO, DEADLINE, Comments
  8394. (defun org-toggle-comment ()
  8395. "Change the COMMENT state of an entry."
  8396. (interactive)
  8397. (save-excursion
  8398. (org-back-to-heading)
  8399. (let (case-fold-search)
  8400. (if (looking-at (concat outline-regexp
  8401. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  8402. (replace-match "" t t nil 1)
  8403. (if (looking-at outline-regexp)
  8404. (progn
  8405. (goto-char (match-end 0))
  8406. (insert org-comment-string " ")))))))
  8407. (defvar org-last-todo-state-is-todo nil
  8408. "This is non-nil when the last TODO state change led to a TODO state.
  8409. If the last change removed the TODO tag or switched to DONE, then
  8410. this is nil.")
  8411. (defvar org-setting-tags nil) ; dynamically skipped
  8412. (defun org-parse-local-options (string var)
  8413. "Parse STRING for startup setting relevant for variable VAR."
  8414. (let ((rtn (symbol-value var))
  8415. e opts)
  8416. (save-match-data
  8417. (if (or (not string) (not (string-match "\\S-" string)))
  8418. rtn
  8419. (setq opts (delq nil (mapcar (lambda (x)
  8420. (setq e (assoc x org-startup-options))
  8421. (if (eq (nth 1 e) var) e nil))
  8422. (org-split-string string "[ \t]+"))))
  8423. (if (not opts)
  8424. rtn
  8425. (setq rtn nil)
  8426. (while (setq e (pop opts))
  8427. (if (not (nth 3 e))
  8428. (setq rtn (nth 2 e))
  8429. (if (not (listp rtn)) (setq rtn nil))
  8430. (push (nth 2 e) rtn)))
  8431. rtn)))))
  8432. (defvar org-todo-setup-filter-hook nil
  8433. "Hook for functions that pre-filter todo specs.
  8434. Each function takes a todo spec and returns either `nil' or the spec
  8435. transformed into canonical form." )
  8436. (defvar org-todo-get-default-hook nil
  8437. "Hook for functions that get a default item for todo.
  8438. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  8439. `nil' or a string to be used for the todo mark." )
  8440. (defvar org-agenda-headline-snapshot-before-repeat)
  8441. (defun org-todo (&optional arg)
  8442. "Change the TODO state of an item.
  8443. The state of an item is given by a keyword at the start of the heading,
  8444. like
  8445. *** TODO Write paper
  8446. *** DONE Call mom
  8447. The different keywords are specified in the variable `org-todo-keywords'.
  8448. By default the available states are \"TODO\" and \"DONE\".
  8449. So for this example: when the item starts with TODO, it is changed to DONE.
  8450. When it starts with DONE, the DONE is removed. And when neither TODO nor
  8451. DONE are present, add TODO at the beginning of the heading.
  8452. With C-u prefix arg, use completion to determine the new state.
  8453. With numeric prefix arg, switch to that state.
  8454. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  8455. With a tripple C-u prefix, circumvent any state blocking.
  8456. For calling through lisp, arg is also interpreted in the following way:
  8457. 'none -> empty state
  8458. \"\"(empty string) -> switch to empty state
  8459. 'done -> switch to DONE
  8460. 'nextset -> switch to the next set of keywords
  8461. 'previousset -> switch to the previous set of keywords
  8462. \"WAITING\" -> switch to the specified keyword, but only if it
  8463. really is a member of `org-todo-keywords'."
  8464. (interactive "P")
  8465. (if (equal arg '(16)) (setq arg 'nextset))
  8466. (let ((org-blocker-hook org-blocker-hook)
  8467. (case-fold-search nil))
  8468. (when (equal arg '(64))
  8469. (setq arg nil org-blocker-hook nil))
  8470. (when (and org-blocker-hook
  8471. (or org-inhibit-blocking
  8472. (org-entry-get nil "NOBLOCKING")))
  8473. (setq org-blocker-hook nil))
  8474. (save-excursion
  8475. (catch 'exit
  8476. (org-back-to-heading)
  8477. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  8478. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  8479. (looking-at " *"))
  8480. (let* ((match-data (match-data))
  8481. (startpos (point-at-bol))
  8482. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  8483. (org-log-done org-log-done)
  8484. (org-log-repeat org-log-repeat)
  8485. (org-todo-log-states org-todo-log-states)
  8486. (this (match-string 1))
  8487. (hl-pos (match-beginning 0))
  8488. (head (org-get-todo-sequence-head this))
  8489. (ass (assoc head org-todo-kwd-alist))
  8490. (interpret (nth 1 ass))
  8491. (done-word (nth 3 ass))
  8492. (final-done-word (nth 4 ass))
  8493. (last-state (or this ""))
  8494. (completion-ignore-case t)
  8495. (member (member this org-todo-keywords-1))
  8496. (tail (cdr member))
  8497. (state (cond
  8498. ((and org-todo-key-trigger
  8499. (or (and (equal arg '(4))
  8500. (eq org-use-fast-todo-selection 'prefix))
  8501. (and (not arg) org-use-fast-todo-selection
  8502. (not (eq org-use-fast-todo-selection
  8503. 'prefix)))))
  8504. ;; Use fast selection
  8505. (org-fast-todo-selection))
  8506. ((and (equal arg '(4))
  8507. (or (not org-use-fast-todo-selection)
  8508. (not org-todo-key-trigger)))
  8509. ;; Read a state with completion
  8510. (org-icompleting-read
  8511. "State: " (mapcar (lambda(x) (list x))
  8512. org-todo-keywords-1)
  8513. nil t))
  8514. ((eq arg 'right)
  8515. (if this
  8516. (if tail (car tail) nil)
  8517. (car org-todo-keywords-1)))
  8518. ((eq arg 'left)
  8519. (if (equal member org-todo-keywords-1)
  8520. nil
  8521. (if this
  8522. (nth (- (length org-todo-keywords-1)
  8523. (length tail) 2)
  8524. org-todo-keywords-1)
  8525. (org-last org-todo-keywords-1))))
  8526. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  8527. (setq arg nil))) ; hack to fall back to cycling
  8528. (arg
  8529. ;; user or caller requests a specific state
  8530. (cond
  8531. ((equal arg "") nil)
  8532. ((eq arg 'none) nil)
  8533. ((eq arg 'done) (or done-word (car org-done-keywords)))
  8534. ((eq arg 'nextset)
  8535. (or (car (cdr (member head org-todo-heads)))
  8536. (car org-todo-heads)))
  8537. ((eq arg 'previousset)
  8538. (let ((org-todo-heads (reverse org-todo-heads)))
  8539. (or (car (cdr (member head org-todo-heads)))
  8540. (car org-todo-heads))))
  8541. ((car (member arg org-todo-keywords-1)))
  8542. ((nth (1- (prefix-numeric-value arg))
  8543. org-todo-keywords-1))))
  8544. ((null member) (or head (car org-todo-keywords-1)))
  8545. ((equal this final-done-word) nil) ;; -> make empty
  8546. ((null tail) nil) ;; -> first entry
  8547. ((memq interpret '(type priority))
  8548. (if (eq this-command last-command)
  8549. (car tail)
  8550. (if (> (length tail) 0)
  8551. (or done-word (car org-done-keywords))
  8552. nil)))
  8553. (t
  8554. (car tail))))
  8555. (state (or
  8556. (run-hook-with-args-until-success
  8557. 'org-todo-get-default-hook state last-state)
  8558. state))
  8559. (next (if state (concat " " state " ") " "))
  8560. (change-plist (list :type 'todo-state-change :from this :to state
  8561. :position startpos))
  8562. dolog now-done-p)
  8563. (when org-blocker-hook
  8564. (setq org-last-todo-state-is-todo
  8565. (not (member this org-done-keywords)))
  8566. (unless (save-excursion
  8567. (save-match-data
  8568. (run-hook-with-args-until-failure
  8569. 'org-blocker-hook change-plist)))
  8570. (if (interactive-p)
  8571. (error "TODO state change from %s to %s blocked" this state)
  8572. ;; fail silently
  8573. (message "TODO state change from %s to %s blocked" this state)
  8574. (throw 'exit nil))))
  8575. (store-match-data match-data)
  8576. (replace-match next t t)
  8577. (unless (pos-visible-in-window-p hl-pos)
  8578. (message "TODO state changed to %s" (org-trim next)))
  8579. (unless head
  8580. (setq head (org-get-todo-sequence-head state)
  8581. ass (assoc head org-todo-kwd-alist)
  8582. interpret (nth 1 ass)
  8583. done-word (nth 3 ass)
  8584. final-done-word (nth 4 ass)))
  8585. (when (memq arg '(nextset previousset))
  8586. (message "Keyword-Set %d/%d: %s"
  8587. (- (length org-todo-sets) -1
  8588. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  8589. (length org-todo-sets)
  8590. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  8591. (setq org-last-todo-state-is-todo
  8592. (not (member state org-done-keywords)))
  8593. (setq now-done-p (and (member state org-done-keywords)
  8594. (not (member this org-done-keywords))))
  8595. (and logging (org-local-logging logging))
  8596. (when (and (or org-todo-log-states org-log-done)
  8597. (not (eq org-inhibit-logging t))
  8598. (not (memq arg '(nextset previousset))))
  8599. ;; we need to look at recording a time and note
  8600. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  8601. (nth 2 (assoc this org-todo-log-states))))
  8602. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  8603. (setq dolog 'time))
  8604. (when (and state
  8605. (member state org-not-done-keywords)
  8606. (not (member this org-not-done-keywords)))
  8607. ;; This is now a todo state and was not one before
  8608. ;; If there was a CLOSED time stamp, get rid of it.
  8609. (org-add-planning-info nil nil 'closed))
  8610. (when (and now-done-p org-log-done)
  8611. ;; It is now done, and it was not done before
  8612. (org-add-planning-info 'closed (org-current-time))
  8613. (if (and (not dolog) (eq 'note org-log-done))
  8614. (org-add-log-setup 'done state this 'findpos 'note)))
  8615. (when (and state dolog)
  8616. ;; This is a non-nil state, and we need to log it
  8617. (org-add-log-setup 'state state this 'findpos dolog)))
  8618. ;; Fixup tag positioning
  8619. (org-todo-trigger-tag-changes state)
  8620. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  8621. (when org-provide-todo-statistics
  8622. (org-update-parent-todo-statistics))
  8623. (run-hooks 'org-after-todo-state-change-hook)
  8624. (if (and arg (not (member state org-done-keywords)))
  8625. (setq head (org-get-todo-sequence-head state)))
  8626. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  8627. ;; Do we need to trigger a repeat?
  8628. (when now-done-p
  8629. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  8630. ;; This is for the agenda, take a snapshot of the headline.
  8631. (save-match-data
  8632. (setq org-agenda-headline-snapshot-before-repeat
  8633. (org-get-heading))))
  8634. (org-auto-repeat-maybe state))
  8635. ;; Fixup cursor location if close to the keyword
  8636. (if (and (outline-on-heading-p)
  8637. (not (bolp))
  8638. (save-excursion (beginning-of-line 1)
  8639. (looking-at org-todo-line-regexp))
  8640. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  8641. (progn
  8642. (goto-char (or (match-end 2) (match-end 1)))
  8643. (and (looking-at " ") (just-one-space))))
  8644. (when org-trigger-hook
  8645. (save-excursion
  8646. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  8647. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  8648. "Block turning an entry into a TODO, using the hierarchy.
  8649. This checks whether the current task should be blocked from state
  8650. changes. Such blocking occurs when:
  8651. 1. The task has children which are not all in a completed state.
  8652. 2. A task has a parent with the property :ORDERED:, and there
  8653. are siblings prior to the current task with incomplete
  8654. status.
  8655. 3. The parent of the task is blocked because it has siblings that should
  8656. be done first, or is child of a block grandparent TODO entry."
  8657. (catch 'dont-block
  8658. ;; If this is not a todo state change, or if this entry is already DONE,
  8659. ;; do not block
  8660. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8661. (member (plist-get change-plist :from)
  8662. (cons 'done org-done-keywords))
  8663. (member (plist-get change-plist :to)
  8664. (cons 'todo org-not-done-keywords)))
  8665. (throw 'dont-block t))
  8666. ;; If this task has children, and any are undone, it's blocked
  8667. (save-excursion
  8668. (org-back-to-heading t)
  8669. (let ((this-level (funcall outline-level)))
  8670. (outline-next-heading)
  8671. (let ((child-level (funcall outline-level)))
  8672. (while (and (not (eobp))
  8673. (> child-level this-level))
  8674. ;; this todo has children, check whether they are all
  8675. ;; completed
  8676. (if (and (not (org-entry-is-done-p))
  8677. (org-entry-is-todo-p))
  8678. (throw 'dont-block nil))
  8679. (outline-next-heading)
  8680. (setq child-level (funcall outline-level))))))
  8681. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  8682. ;; any previous siblings are undone, it's blocked
  8683. (save-excursion
  8684. (org-back-to-heading t)
  8685. (let* ((pos (point))
  8686. (parent-pos (and (org-up-heading-safe) (point))))
  8687. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8688. (when (and (org-entry-get (point) "ORDERED")
  8689. (forward-line 1)
  8690. (re-search-forward org-not-done-heading-regexp pos t))
  8691. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  8692. ;; Search further up the hierarchy, to see if an anchestor is blocked
  8693. (while t
  8694. (goto-char parent-pos)
  8695. (if (not (looking-at org-not-done-heading-regexp))
  8696. (throw 'dont-block t)) ; do not block, parent is not a TODO
  8697. (setq pos (point))
  8698. (setq parent-pos (and (org-up-heading-safe) (point)))
  8699. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  8700. (when (and (org-entry-get (point) "ORDERED")
  8701. (forward-line 1)
  8702. (re-search-forward org-not-done-heading-regexp pos t))
  8703. (throw 'dont-block nil))))))) ; block, older sibling not done.
  8704. (defcustom org-track-ordered-property-with-tag nil
  8705. "Should the ORDERED property also be shown as a tag?
  8706. The ORDERED property decides if an entry should require subtasks to be
  8707. completed in sequence. Since a property is not very visible, setting
  8708. this option means that toggling the ORDERED property with the command
  8709. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  8710. not relevant for the behavior, but it makes things more visible.
  8711. Note that toggling the tag with tags commands will not change the property
  8712. and therefore not influence behavior!
  8713. This can be t, meaning the tag ORDERED should be used, It can also be a
  8714. string to select a different tag for this task."
  8715. :group 'org-todo
  8716. :type '(choice
  8717. (const :tag "No tracking" nil)
  8718. (const :tag "Track with ORDERED tag" t)
  8719. (string :tag "Use other tag")))
  8720. (defun org-toggle-ordered-property ()
  8721. "Toggle the ORDERED property of the current entry.
  8722. For better visibility, you can track the value of this property with a tag.
  8723. See variable `org-track-ordered-property-with-tag'."
  8724. (interactive)
  8725. (let* ((t1 org-track-ordered-property-with-tag)
  8726. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  8727. (save-excursion
  8728. (org-back-to-heading)
  8729. (if (org-entry-get nil "ORDERED")
  8730. (progn
  8731. (org-delete-property "ORDERED")
  8732. (and tag (org-toggle-tag tag 'off))
  8733. (message "Subtasks can be completed in arbitrary order"))
  8734. (org-entry-put nil "ORDERED" "t")
  8735. (and tag (org-toggle-tag tag 'on))
  8736. (message "Subtasks must be completed in sequence")))))
  8737. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  8738. (defun org-block-todo-from-checkboxes (change-plist)
  8739. "Block turning an entry into a TODO, using checkboxes.
  8740. This checks whether the current task should be blocked from state
  8741. changes because there are uncheckd boxes in this entry."
  8742. (catch 'dont-block
  8743. ;; If this is not a todo state change, or if this entry is already DONE,
  8744. ;; do not block
  8745. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  8746. (member (plist-get change-plist :from)
  8747. (cons 'done org-done-keywords))
  8748. (member (plist-get change-plist :to)
  8749. (cons 'todo org-not-done-keywords)))
  8750. (throw 'dont-block t))
  8751. ;; If this task has checkboxes that are not checked, it's blocked
  8752. (save-excursion
  8753. (org-back-to-heading t)
  8754. (let ((beg (point)) end)
  8755. (outline-next-heading)
  8756. (setq end (point))
  8757. (goto-char beg)
  8758. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  8759. end t)
  8760. (progn
  8761. (if (boundp 'org-blocked-by-checkboxes)
  8762. (setq org-blocked-by-checkboxes t))
  8763. (throw 'dont-block nil)))))
  8764. t)) ; do not block
  8765. (defun org-update-statistics-cookies (all)
  8766. "Update the statistics cookie, either from TODO or from checkboxes.
  8767. This should be called with the cursor in a line with a statistics cookie."
  8768. (interactive "P")
  8769. (if all
  8770. (progn
  8771. (org-update-checkbox-count 'all)
  8772. (org-map-entries 'org-update-parent-todo-statistics))
  8773. (if (not (org-on-heading-p))
  8774. (org-update-checkbox-count)
  8775. (let ((pos (move-marker (make-marker) (point)))
  8776. end l1 l2)
  8777. (ignore-errors (org-back-to-heading t))
  8778. (if (not (org-on-heading-p))
  8779. (org-update-checkbox-count)
  8780. (setq l1 (org-outline-level))
  8781. (setq end (save-excursion
  8782. (outline-next-heading)
  8783. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  8784. (point)))
  8785. (if (and (save-excursion (re-search-forward
  8786. "^[ \t]*[-+*] \\[[- X]\\]" end t))
  8787. (not (save-excursion (re-search-forward
  8788. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  8789. (org-update-checkbox-count)
  8790. (if (and l2 (> l2 l1))
  8791. (progn
  8792. (goto-char end)
  8793. (org-update-parent-todo-statistics))
  8794. (error "No data for statistics cookie"))))
  8795. (goto-char pos)
  8796. (move-marker pos nil)))))
  8797. (defvar org-entry-property-inherited-from) ;; defined below
  8798. (defun org-update-parent-todo-statistics ()
  8799. "Update any statistics cookie in the parent of the current headline.
  8800. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  8801. the entire subtree and this will travel up the hierarchy and update
  8802. statistics everywhere."
  8803. (interactive)
  8804. (let* ((lim 0) prop
  8805. (recursive (or (not org-hierarchical-todo-statistics)
  8806. (string-match
  8807. "\\<recursive\\>"
  8808. (or (setq prop (org-entry-get
  8809. nil "COOKIE_DATA" 'inherit)) ""))))
  8810. (lim (or (and prop (marker-position
  8811. org-entry-property-inherited-from))
  8812. lim))
  8813. (first t)
  8814. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  8815. level ltoggle l1
  8816. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  8817. (catch 'exit
  8818. (save-excursion
  8819. (beginning-of-line 1)
  8820. (if (org-at-heading-p)
  8821. (setq ltoggle (funcall outline-level))
  8822. (error "This should not happen"))
  8823. (while (and (setq level (org-up-heading-safe))
  8824. (or recursive first)
  8825. (>= (point) lim))
  8826. (setq first nil)
  8827. (unless (and level
  8828. (not (string-match
  8829. "\\<checkbox\\>"
  8830. (downcase
  8831. (or (org-entry-get
  8832. nil "COOKIE_DATA")
  8833. "")))))
  8834. (throw 'exit nil))
  8835. (while (re-search-forward box-re (point-at-eol) t)
  8836. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8837. (setq is-percent (match-end 2))
  8838. (save-match-data
  8839. (unless (outline-next-heading) (throw 'exit nil))
  8840. (while (and (looking-at org-complex-heading-regexp)
  8841. (> (setq l1 (length (match-string 1))) level))
  8842. (setq kwd (and (or recursive (= l1 ltoggle))
  8843. (match-string 2)))
  8844. (if (or (eq org-provide-todo-statistics 'all-headlines)
  8845. (and (listp org-provide-todo-statistics)
  8846. (or (member kwd org-provide-todo-statistics)
  8847. (member kwd org-done-keywords))))
  8848. (setq cnt-all (1+ cnt-all))
  8849. (if (eq org-provide-todo-statistics t)
  8850. (and kwd (setq cnt-all (1+ cnt-all)))))
  8851. (and (member kwd org-done-keywords)
  8852. (setq cnt-done (1+ cnt-done)))
  8853. (outline-next-heading)))
  8854. (replace-match
  8855. (if is-percent
  8856. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8857. (format "[%d/%d]" cnt-done cnt-all)))))
  8858. (when cookie-present
  8859. (run-hook-with-args 'org-after-todo-statistics-hook
  8860. cnt-done (- cnt-all cnt-done)))))
  8861. (run-hooks 'org-todo-statistics-hook)))
  8862. (defvar org-after-todo-statistics-hook nil
  8863. "Hook that is called after a TODO statistics cookie has been updated.
  8864. Each function is called with two arguments: the number of not-done entries
  8865. and the number of done entries.
  8866. For example, the following function, when added to this hook, will switch
  8867. an entry to DONE when all children are done, and back to TODO when new
  8868. entries are set to a TODO status. Note that this hook is only called
  8869. when there is a statistics cookie in the headline!
  8870. (defun org-summary-todo (n-done n-not-done)
  8871. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8872. (let (org-log-done org-log-states) ; turn off logging
  8873. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8874. ")
  8875. (defvar org-todo-statistics-hook nil
  8876. "Hook that is run whenever Org thinks TODO statistics should be updated.
  8877. This hook runs even if there is no statisics cookie present, in which case
  8878. `org-after-todo-statistics-hook' would not run.")
  8879. (defun org-todo-trigger-tag-changes (state)
  8880. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8881. (let ((l org-todo-state-tags-triggers)
  8882. changes)
  8883. (when (or (not state) (equal state ""))
  8884. (setq changes (append changes (cdr (assoc "" l)))))
  8885. (when (and (stringp state) (> (length state) 0))
  8886. (setq changes (append changes (cdr (assoc state l)))))
  8887. (when (member state org-not-done-keywords)
  8888. (setq changes (append changes (cdr (assoc 'todo l)))))
  8889. (when (member state org-done-keywords)
  8890. (setq changes (append changes (cdr (assoc 'done l)))))
  8891. (dolist (c changes)
  8892. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8893. (defun org-local-logging (value)
  8894. "Get logging settings from a property VALUE."
  8895. (let* (words w a)
  8896. ;; directly set the variables, they are already local.
  8897. (setq org-log-done nil
  8898. org-log-repeat nil
  8899. org-todo-log-states nil)
  8900. (setq words (org-split-string value))
  8901. (while (setq w (pop words))
  8902. (cond
  8903. ((setq a (assoc w org-startup-options))
  8904. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8905. (set (nth 1 a) (nth 2 a))))
  8906. ((setq a (org-extract-log-state-settings w))
  8907. (and (member (car a) org-todo-keywords-1)
  8908. (push a org-todo-log-states)))))))
  8909. (defun org-get-todo-sequence-head (kwd)
  8910. "Return the head of the TODO sequence to which KWD belongs.
  8911. If KWD is not set, check if there is a text property remembering the
  8912. right sequence."
  8913. (let (p)
  8914. (cond
  8915. ((not kwd)
  8916. (or (get-text-property (point-at-bol) 'org-todo-head)
  8917. (progn
  8918. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8919. nil (point-at-eol)))
  8920. (get-text-property p 'org-todo-head))))
  8921. ((not (member kwd org-todo-keywords-1))
  8922. (car org-todo-keywords-1))
  8923. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8924. (defun org-fast-todo-selection ()
  8925. "Fast TODO keyword selection with single keys.
  8926. Returns the new TODO keyword, or nil if no state change should occur."
  8927. (let* ((fulltable org-todo-key-alist)
  8928. (done-keywords org-done-keywords) ;; needed for the faces.
  8929. (maxlen (apply 'max (mapcar
  8930. (lambda (x)
  8931. (if (stringp (car x)) (string-width (car x)) 0))
  8932. fulltable)))
  8933. (expert nil)
  8934. (fwidth (+ maxlen 3 1 3))
  8935. (ncol (/ (- (window-width) 4) fwidth))
  8936. tg cnt e c tbl
  8937. groups ingroup)
  8938. (save-excursion
  8939. (save-window-excursion
  8940. (if expert
  8941. (set-buffer (get-buffer-create " *Org todo*"))
  8942. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8943. (erase-buffer)
  8944. (org-set-local 'org-done-keywords done-keywords)
  8945. (setq tbl fulltable cnt 0)
  8946. (while (setq e (pop tbl))
  8947. (cond
  8948. ((equal e '(:startgroup))
  8949. (push '() groups) (setq ingroup t)
  8950. (when (not (= cnt 0))
  8951. (setq cnt 0)
  8952. (insert "\n"))
  8953. (insert "{ "))
  8954. ((equal e '(:endgroup))
  8955. (setq ingroup nil cnt 0)
  8956. (insert "}\n"))
  8957. ((equal e '(:newline))
  8958. (when (not (= cnt 0))
  8959. (setq cnt 0)
  8960. (insert "\n")
  8961. (setq e (car tbl))
  8962. (while (equal (car tbl) '(:newline))
  8963. (insert "\n")
  8964. (setq tbl (cdr tbl)))))
  8965. (t
  8966. (setq tg (car e) c (cdr e))
  8967. (if ingroup (push tg (car groups)))
  8968. (setq tg (org-add-props tg nil 'face
  8969. (org-get-todo-face tg)))
  8970. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8971. (insert "[" c "] " tg (make-string
  8972. (- fwidth 4 (length tg)) ?\ ))
  8973. (when (= (setq cnt (1+ cnt)) ncol)
  8974. (insert "\n")
  8975. (if ingroup (insert " "))
  8976. (setq cnt 0)))))
  8977. (insert "\n")
  8978. (goto-char (point-min))
  8979. (if (not expert) (org-fit-window-to-buffer))
  8980. (message "[a-z..]:Set [SPC]:clear")
  8981. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8982. (cond
  8983. ((or (= c ?\C-g)
  8984. (and (= c ?q) (not (rassoc c fulltable))))
  8985. (setq quit-flag t))
  8986. ((= c ?\ ) nil)
  8987. ((setq e (rassoc c fulltable) tg (car e))
  8988. tg)
  8989. (t (setq quit-flag t)))))))
  8990. (defun org-entry-is-todo-p ()
  8991. (member (org-get-todo-state) org-not-done-keywords))
  8992. (defun org-entry-is-done-p ()
  8993. (member (org-get-todo-state) org-done-keywords))
  8994. (defun org-get-todo-state ()
  8995. (save-excursion
  8996. (org-back-to-heading t)
  8997. (and (looking-at org-todo-line-regexp)
  8998. (match-end 2)
  8999. (match-string 2))))
  9000. (defun org-at-date-range-p (&optional inactive-ok)
  9001. "Is the cursor inside a date range?"
  9002. (interactive)
  9003. (save-excursion
  9004. (catch 'exit
  9005. (let ((pos (point)))
  9006. (skip-chars-backward "^[<\r\n")
  9007. (skip-chars-backward "<[")
  9008. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9009. (>= (match-end 0) pos)
  9010. (throw 'exit t))
  9011. (skip-chars-backward "^<[\r\n")
  9012. (skip-chars-backward "<[")
  9013. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9014. (>= (match-end 0) pos)
  9015. (throw 'exit t)))
  9016. nil)))
  9017. (defun org-get-repeat ()
  9018. "Check if there is a deadline/schedule with repeater in this entry."
  9019. (save-match-data
  9020. (save-excursion
  9021. (org-back-to-heading t)
  9022. (if (re-search-forward
  9023. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  9024. (match-string 1)))))
  9025. (defvar org-last-changed-timestamp)
  9026. (defvar org-last-inserted-timestamp)
  9027. (defvar org-log-post-message)
  9028. (defvar org-log-note-purpose)
  9029. (defvar org-log-note-how)
  9030. (defvar org-log-note-extra)
  9031. (defun org-auto-repeat-maybe (done-word)
  9032. "Check if the current headline contains a repeated deadline/schedule.
  9033. If yes, set TODO state back to what it was and change the base date
  9034. of repeating deadline/scheduled time stamps to new date.
  9035. This function is run automatically after each state change to a DONE state."
  9036. ;; last-state is dynamically scoped into this function
  9037. (let* ((repeat (org-get-repeat))
  9038. (aa (assoc last-state org-todo-kwd-alist))
  9039. (interpret (nth 1 aa))
  9040. (head (nth 2 aa))
  9041. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9042. (msg "Entry repeats: ")
  9043. (org-log-done nil)
  9044. (org-todo-log-states nil)
  9045. (nshiftmax 10) (nshift 0)
  9046. re type n what ts time)
  9047. (when repeat
  9048. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9049. (org-todo (if (eq interpret 'type) last-state head))
  9050. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9051. (org-time-stamp-format t t)))
  9052. (when org-log-repeat
  9053. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9054. (memq 'org-add-log-note post-command-hook))
  9055. ;; OK, we are already setup for some record
  9056. (if (eq org-log-repeat 'note)
  9057. ;; make sure we take a note, not only a time stamp
  9058. (setq org-log-note-how 'note))
  9059. ;; Set up for taking a record
  9060. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9061. last-state
  9062. 'findpos org-log-repeat)))
  9063. (org-back-to-heading t)
  9064. (org-add-planning-info nil nil 'closed)
  9065. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9066. org-deadline-time-regexp "\\)\\|\\("
  9067. org-ts-regexp "\\)"))
  9068. (while (re-search-forward
  9069. re (save-excursion (outline-next-heading) (point)) t)
  9070. (setq type (if (match-end 1) org-scheduled-string
  9071. (if (match-end 3) org-deadline-string "Plain:"))
  9072. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9073. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9074. (setq n (string-to-number (match-string 2 ts))
  9075. what (match-string 3 ts))
  9076. (if (equal what "w") (setq n (* n 7) what "d"))
  9077. ;; Preparation, see if we need to modify the start date for the change
  9078. (when (match-end 1)
  9079. (setq time (save-match-data (org-time-string-to-time ts)))
  9080. (cond
  9081. ((equal (match-string 1 ts) ".")
  9082. ;; Shift starting date to today
  9083. (org-timestamp-change
  9084. (- (time-to-days (current-time)) (time-to-days time))
  9085. 'day))
  9086. ((equal (match-string 1 ts) "+")
  9087. (while (or (= nshift 0)
  9088. (<= (time-to-days time) (time-to-days (current-time))))
  9089. (when (= (incf nshift) nshiftmax)
  9090. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9091. (error "Abort")))
  9092. (org-timestamp-change n (cdr (assoc what whata)))
  9093. (org-at-timestamp-p t)
  9094. (setq ts (match-string 1))
  9095. (setq time (save-match-data (org-time-string-to-time ts))))
  9096. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9097. ;; rematch, so that we have everything in place for the real shift
  9098. (org-at-timestamp-p t)
  9099. (setq ts (match-string 1))
  9100. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9101. (org-timestamp-change n (cdr (assoc what whata)))
  9102. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9103. (setq org-log-post-message msg)
  9104. (message "%s" msg))))
  9105. (defun org-show-todo-tree (arg)
  9106. "Make a compact tree which shows all headlines marked with TODO.
  9107. The tree will show the lines where the regexp matches, and all higher
  9108. headlines above the match.
  9109. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9110. With a numeric prefix N, construct a sparse tree for the Nth element
  9111. of `org-todo-keywords-1'."
  9112. (interactive "P")
  9113. (let ((case-fold-search nil)
  9114. (kwd-re
  9115. (cond ((null arg) org-not-done-regexp)
  9116. ((equal arg '(4))
  9117. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9118. (mapcar 'list org-todo-keywords-1))))
  9119. (concat "\\("
  9120. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9121. "\\)\\>")))
  9122. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9123. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9124. org-todo-keywords-1)))
  9125. (t (error "Invalid prefix argument: %s" arg)))))
  9126. (message "%d TODO entries found"
  9127. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9128. (defun org-deadline (&optional remove time)
  9129. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9130. With argument REMOVE, remove any deadline from the item.
  9131. When TIME is set, it should be an internal time specification, and the
  9132. scheduling will use the corresponding date."
  9133. (interactive "P")
  9134. (if remove
  9135. (progn
  9136. (org-remove-timestamp-with-keyword org-deadline-string)
  9137. (message "Item no longer has a deadline."))
  9138. (if (org-get-repeat)
  9139. (error "Cannot change deadline on task with repeater, please do that by hand")
  9140. (org-add-planning-info 'deadline time 'closed)
  9141. (message "Deadline on %s" org-last-inserted-timestamp))))
  9142. (defun org-schedule (&optional remove time)
  9143. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9144. With argument REMOVE, remove any scheduling date from the item.
  9145. When TIME is set, it should be an internal time specification, and the
  9146. scheduling will use the corresponding date."
  9147. (interactive "P")
  9148. (if remove
  9149. (progn
  9150. (org-remove-timestamp-with-keyword org-scheduled-string)
  9151. (message "Item is no longer scheduled."))
  9152. (if (org-get-repeat)
  9153. (error "Cannot reschedule task with repeater, please do that by hand")
  9154. (org-add-planning-info 'scheduled time 'closed)
  9155. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9156. (defun org-get-scheduled-time (pom &optional inherit)
  9157. "Get the scheduled time as a time tuple, of a format suitable
  9158. for calling org-schedule with, or if there is no scheduling,
  9159. returns nil."
  9160. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9161. (when time
  9162. (apply 'encode-time (org-parse-time-string time)))))
  9163. (defun org-get-deadline-time (pom &optional inherit)
  9164. "Get the deadine as a time tuple, of a format suitable for
  9165. calling org-deadlin with, or if there is no scheduling, returns
  9166. nil."
  9167. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9168. (when time
  9169. (apply 'encode-time (org-parse-time-string time)))))
  9170. (defun org-remove-timestamp-with-keyword (keyword)
  9171. "Remove all time stamps with KEYWORD in the current entry."
  9172. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9173. beg)
  9174. (save-excursion
  9175. (org-back-to-heading t)
  9176. (setq beg (point))
  9177. (outline-next-heading)
  9178. (while (re-search-backward re beg t)
  9179. (replace-match "")
  9180. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9181. (equal (char-before) ?\ ))
  9182. (backward-delete-char 1)
  9183. (if (string-match "^[ \t]*$" (buffer-substring
  9184. (point-at-bol) (point-at-eol)))
  9185. (delete-region (point-at-bol)
  9186. (min (point-max) (1+ (point-at-eol))))))))))
  9187. (defun org-add-planning-info (what &optional time &rest remove)
  9188. "Insert new timestamp with keyword in the line directly after the headline.
  9189. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9190. If non is given, the user is prompted for a date.
  9191. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9192. be removed."
  9193. (interactive)
  9194. (let (org-time-was-given org-end-time-was-given ts
  9195. end default-time default-input)
  9196. (catch 'exit
  9197. (when (and (not time) (memq what '(scheduled deadline)))
  9198. ;; Try to get a default date/time from existing timestamp
  9199. (save-excursion
  9200. (org-back-to-heading t)
  9201. (setq end (save-excursion (outline-next-heading) (point)))
  9202. (when (re-search-forward (if (eq what 'scheduled)
  9203. org-scheduled-time-regexp
  9204. org-deadline-time-regexp)
  9205. end t)
  9206. (setq ts (match-string 1)
  9207. default-time
  9208. (apply 'encode-time (org-parse-time-string ts))
  9209. default-input (and ts (org-get-compact-tod ts))))))
  9210. (when what
  9211. ;; If necessary, get the time from the user
  9212. (setq time (or time (org-read-date nil 'to-time nil nil
  9213. default-time default-input))))
  9214. (when (and org-insert-labeled-timestamps-at-point
  9215. (member what '(scheduled deadline)))
  9216. (insert
  9217. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  9218. (org-insert-time-stamp time org-time-was-given
  9219. nil nil nil (list org-end-time-was-given))
  9220. (setq what nil))
  9221. (save-excursion
  9222. (save-restriction
  9223. (let (col list elt ts buffer-invisibility-spec)
  9224. (org-back-to-heading t)
  9225. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  9226. (goto-char (match-end 1))
  9227. (setq col (current-column))
  9228. (goto-char (match-end 0))
  9229. (if (eobp) (insert "\n") (forward-char 1))
  9230. (when (and (not what)
  9231. (not (looking-at
  9232. (concat "[ \t]*"
  9233. org-keyword-time-not-clock-regexp))))
  9234. ;; Nothing to add, nothing to remove...... :-)
  9235. (throw 'exit nil))
  9236. (if (and (not (looking-at outline-regexp))
  9237. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  9238. "[^\r\n]*"))
  9239. (not (equal (match-string 1) org-clock-string)))
  9240. (narrow-to-region (match-beginning 0) (match-end 0))
  9241. (insert-before-markers "\n")
  9242. (backward-char 1)
  9243. (narrow-to-region (point) (point))
  9244. (and org-adapt-indentation (org-indent-to-column col)))
  9245. ;; Check if we have to remove something.
  9246. (setq list (cons what remove))
  9247. (while list
  9248. (setq elt (pop list))
  9249. (goto-char (point-min))
  9250. (when (or (and (eq elt 'scheduled)
  9251. (re-search-forward org-scheduled-time-regexp nil t))
  9252. (and (eq elt 'deadline)
  9253. (re-search-forward org-deadline-time-regexp nil t))
  9254. (and (eq elt 'closed)
  9255. (re-search-forward org-closed-time-regexp nil t)))
  9256. (replace-match "")
  9257. (if (looking-at "--+<[^>]+>") (replace-match ""))
  9258. (skip-chars-backward " ")
  9259. (if (looking-at " +") (replace-match ""))))
  9260. (goto-char (point-max))
  9261. (when what
  9262. (insert
  9263. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  9264. (cond ((eq what 'scheduled) org-scheduled-string)
  9265. ((eq what 'deadline) org-deadline-string)
  9266. ((eq what 'closed) org-closed-string))
  9267. " ")
  9268. (setq ts (org-insert-time-stamp
  9269. time
  9270. (or org-time-was-given
  9271. (and (eq what 'closed) org-log-done-with-time))
  9272. (eq what 'closed)
  9273. nil nil (list org-end-time-was-given)))
  9274. (end-of-line 1))
  9275. (goto-char (point-min))
  9276. (widen)
  9277. (if (and (looking-at "[ \t]+\n")
  9278. (equal (char-before) ?\n))
  9279. (delete-region (1- (point)) (point-at-eol)))
  9280. ts))))))
  9281. (defvar org-log-note-marker (make-marker))
  9282. (defvar org-log-note-purpose nil)
  9283. (defvar org-log-note-state nil)
  9284. (defvar org-log-note-previous-state nil)
  9285. (defvar org-log-note-how nil)
  9286. (defvar org-log-note-extra nil)
  9287. (defvar org-log-note-window-configuration nil)
  9288. (defvar org-log-note-return-to (make-marker))
  9289. (defvar org-log-post-message nil
  9290. "Message to be displayed after a log note has been stored.
  9291. The auto-repeater uses this.")
  9292. (defun org-add-note ()
  9293. "Add a note to the current entry.
  9294. This is done in the same way as adding a state change note."
  9295. (interactive)
  9296. (org-add-log-setup 'note nil nil 'findpos nil))
  9297. (defvar org-property-end-re)
  9298. (defun org-add-log-setup (&optional purpose state prev-state
  9299. findpos how &optional extra)
  9300. "Set up the post command hook to take a note.
  9301. If this is about to TODO state change, the new state is expected in STATE.
  9302. When FINDPOS is non-nil, find the correct position for the note in
  9303. the current entry. If not, assume that it can be inserted at point.
  9304. HOW is an indicator what kind of note should be created.
  9305. EXTRA is additional text that will be inserted into the notes buffer."
  9306. (let* ((org-log-into-drawer (org-log-into-drawer))
  9307. (drawer (cond ((stringp org-log-into-drawer)
  9308. org-log-into-drawer)
  9309. (org-log-into-drawer "LOGBOOK")
  9310. (t nil))))
  9311. (save-restriction
  9312. (save-excursion
  9313. (when findpos
  9314. (org-back-to-heading t)
  9315. (narrow-to-region (point) (save-excursion
  9316. (outline-next-heading) (point)))
  9317. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  9318. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  9319. "[^\r\n]*\\)?"))
  9320. (goto-char (match-end 0))
  9321. (cond
  9322. (drawer
  9323. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  9324. nil t)
  9325. (progn
  9326. (goto-char (match-end 0))
  9327. (or org-log-states-order-reversed
  9328. (and (re-search-forward org-property-end-re nil t)
  9329. (goto-char (1- (match-beginning 0))))))
  9330. (insert "\n:" drawer ":\n:END:")
  9331. (beginning-of-line 0)
  9332. (org-indent-line-function)
  9333. (beginning-of-line 2)
  9334. (org-indent-line-function)
  9335. (end-of-line 0)))
  9336. ((and org-log-state-notes-insert-after-drawers
  9337. (save-excursion
  9338. (forward-line) (looking-at org-drawer-regexp)))
  9339. (forward-line)
  9340. (while (looking-at org-drawer-regexp)
  9341. (goto-char (match-end 0))
  9342. (re-search-forward org-property-end-re (point-max) t)
  9343. (forward-line))
  9344. (forward-line -1)))
  9345. (unless org-log-states-order-reversed
  9346. (and (= (char-after) ?\n) (forward-char 1))
  9347. (org-skip-over-state-notes)
  9348. (skip-chars-backward " \t\n\r")))
  9349. (move-marker org-log-note-marker (point))
  9350. (setq org-log-note-purpose purpose
  9351. org-log-note-state state
  9352. org-log-note-previous-state prev-state
  9353. org-log-note-how how
  9354. org-log-note-extra extra)
  9355. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  9356. (defun org-skip-over-state-notes ()
  9357. "Skip past the list of State notes in an entry."
  9358. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  9359. (while (looking-at "[ \t]*- State")
  9360. (condition-case nil
  9361. (org-next-item)
  9362. (error (org-end-of-item)))))
  9363. (defun org-add-log-note (&optional purpose)
  9364. "Pop up a window for taking a note, and add this note later at point."
  9365. (remove-hook 'post-command-hook 'org-add-log-note)
  9366. (setq org-log-note-window-configuration (current-window-configuration))
  9367. (delete-other-windows)
  9368. (move-marker org-log-note-return-to (point))
  9369. (switch-to-buffer (marker-buffer org-log-note-marker))
  9370. (goto-char org-log-note-marker)
  9371. (org-switch-to-buffer-other-window "*Org Note*")
  9372. (erase-buffer)
  9373. (if (memq org-log-note-how '(time state))
  9374. (let (current-prefix-arg) (org-store-log-note))
  9375. (let ((org-inhibit-startup t)) (org-mode))
  9376. (insert (format "# Insert note for %s.
  9377. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  9378. (cond
  9379. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  9380. ((eq org-log-note-purpose 'done) "closed todo item")
  9381. ((eq org-log-note-purpose 'state)
  9382. (format "state change from \"%s\" to \"%s\""
  9383. (or org-log-note-previous-state "")
  9384. (or org-log-note-state "")))
  9385. ((eq org-log-note-purpose 'note)
  9386. "this entry")
  9387. (t (error "This should not happen")))))
  9388. (if org-log-note-extra (insert org-log-note-extra))
  9389. (org-set-local 'org-finish-function 'org-store-log-note)))
  9390. (defvar org-note-abort nil) ; dynamically scoped
  9391. (defun org-store-log-note ()
  9392. "Finish taking a log note, and insert it to where it belongs."
  9393. (let ((txt (buffer-string))
  9394. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  9395. lines ind)
  9396. (kill-buffer (current-buffer))
  9397. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  9398. (setq txt (replace-match "" t t txt)))
  9399. (if (string-match "\\s-+\\'" txt)
  9400. (setq txt (replace-match "" t t txt)))
  9401. (setq lines (org-split-string txt "\n"))
  9402. (when (and note (string-match "\\S-" note))
  9403. (setq note
  9404. (org-replace-escapes
  9405. note
  9406. (list (cons "%u" (user-login-name))
  9407. (cons "%U" user-full-name)
  9408. (cons "%t" (format-time-string
  9409. (org-time-stamp-format 'long 'inactive)
  9410. (current-time)))
  9411. (cons "%s" (if org-log-note-state
  9412. (concat "\"" org-log-note-state "\"")
  9413. ""))
  9414. (cons "%S" (if org-log-note-previous-state
  9415. (concat "\"" org-log-note-previous-state "\"")
  9416. "\"\"")))))
  9417. (if lines (setq note (concat note " \\\\")))
  9418. (push note lines))
  9419. (when (or current-prefix-arg org-note-abort)
  9420. (when org-log-into-drawer
  9421. (org-remove-empty-drawer-at
  9422. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  9423. org-log-note-marker))
  9424. (setq lines nil))
  9425. (when lines
  9426. (save-excursion
  9427. (set-buffer (marker-buffer org-log-note-marker))
  9428. (save-excursion
  9429. (goto-char org-log-note-marker)
  9430. (move-marker org-log-note-marker nil)
  9431. (end-of-line 1)
  9432. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  9433. (insert "- " (pop lines))
  9434. (org-indent-line-function)
  9435. (beginning-of-line 1)
  9436. (looking-at "[ \t]*")
  9437. (setq ind (concat (match-string 0) " "))
  9438. (end-of-line 1)
  9439. (while lines (insert "\n" ind (pop lines)))
  9440. (message "Note stored")
  9441. (org-back-to-heading t)
  9442. (org-cycle-hide-drawers 'children)))))
  9443. (set-window-configuration org-log-note-window-configuration)
  9444. (with-current-buffer (marker-buffer org-log-note-return-to)
  9445. (goto-char org-log-note-return-to))
  9446. (move-marker org-log-note-return-to nil)
  9447. (and org-log-post-message (message "%s" org-log-post-message)))
  9448. (defun org-remove-empty-drawer-at (drawer pos)
  9449. "Remove an emptyr DARWER drawer at position POS.
  9450. POS may also be a marker."
  9451. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  9452. (save-excursion
  9453. (save-restriction
  9454. (widen)
  9455. (goto-char pos)
  9456. (if (org-in-regexp
  9457. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  9458. (replace-match ""))))))
  9459. (defun org-sparse-tree (&optional arg)
  9460. "Create a sparse tree, prompt for the details.
  9461. This command can create sparse trees. You first need to select the type
  9462. of match used to create the tree:
  9463. t Show entries with a specific TODO keyword.
  9464. m Show entries selected by a tags/property match.
  9465. p Enter a property name and its value (both with completion on existing
  9466. names/values) and show entries with that property.
  9467. r Show entries matching a regular expression.
  9468. d Show deadlines due within `org-deadline-warning-days'.
  9469. b Show deadlines and scheduled items before a date.
  9470. a Show deadlines and scheduled items after a date."
  9471. (interactive "P")
  9472. (let (ans kwd value)
  9473. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  9474. (setq ans (read-char-exclusive))
  9475. (cond
  9476. ((equal ans ?d)
  9477. (call-interactively 'org-check-deadlines))
  9478. ((equal ans ?b)
  9479. (call-interactively 'org-check-before-date))
  9480. ((equal ans ?a)
  9481. (call-interactively 'org-check-after-date))
  9482. ((equal ans ?t)
  9483. (org-show-todo-tree '(4)))
  9484. ((member ans '(?T ?m))
  9485. (call-interactively 'org-match-sparse-tree))
  9486. ((member ans '(?p ?P))
  9487. (setq kwd (org-icompleting-read "Property: "
  9488. (mapcar 'list (org-buffer-property-keys))))
  9489. (setq value (org-icompleting-read "Value: "
  9490. (mapcar 'list (org-property-values kwd))))
  9491. (unless (string-match "\\`{.*}\\'" value)
  9492. (setq value (concat "\"" value "\"")))
  9493. (org-match-sparse-tree arg (concat kwd "=" value)))
  9494. ((member ans '(?r ?R ?/))
  9495. (call-interactively 'org-occur))
  9496. (t (error "No such sparse tree command \"%c\"" ans)))))
  9497. (defvar org-occur-highlights nil
  9498. "List of overlays used for occur matches.")
  9499. (make-variable-buffer-local 'org-occur-highlights)
  9500. (defvar org-occur-parameters nil
  9501. "Parameters of the active org-occur calls.
  9502. This is a list, each call to org-occur pushes as cons cell,
  9503. containing the regular expression and the callback, onto the list.
  9504. The list can contain several entries if `org-occur' has been called
  9505. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  9506. will only contain one set of parameters. When the highlights are
  9507. removed (for example with `C-c C-c', or with the next edit (depending
  9508. on `org-remove-highlights-with-change'), this variable is emptied
  9509. as well.")
  9510. (make-variable-buffer-local 'org-occur-parameters)
  9511. (defun org-occur (regexp &optional keep-previous callback)
  9512. "Make a compact tree which shows all matches of REGEXP.
  9513. The tree will show the lines where the regexp matches, and all higher
  9514. headlines above the match. It will also show the heading after the match,
  9515. to make sure editing the matching entry is easy.
  9516. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  9517. call to `org-occur' will be kept, to allow stacking of calls to this
  9518. command.
  9519. If CALLBACK is non-nil, it is a function which is called to confirm
  9520. that the match should indeed be shown."
  9521. (interactive "sRegexp: \nP")
  9522. (when (equal regexp "")
  9523. (error "Regexp cannot be empty"))
  9524. (unless keep-previous
  9525. (org-remove-occur-highlights nil nil t))
  9526. (push (cons regexp callback) org-occur-parameters)
  9527. (let ((cnt 0))
  9528. (save-excursion
  9529. (goto-char (point-min))
  9530. (if (or (not keep-previous) ; do not want to keep
  9531. (not org-occur-highlights)) ; no previous matches
  9532. ;; hide everything
  9533. (org-overview))
  9534. (while (re-search-forward regexp nil t)
  9535. (when (or (not callback)
  9536. (save-match-data (funcall callback)))
  9537. (setq cnt (1+ cnt))
  9538. (when org-highlight-sparse-tree-matches
  9539. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  9540. (org-show-context 'occur-tree))))
  9541. (when org-remove-highlights-with-change
  9542. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  9543. nil 'local))
  9544. (unless org-sparse-tree-open-archived-trees
  9545. (org-hide-archived-subtrees (point-min) (point-max)))
  9546. (run-hooks 'org-occur-hook)
  9547. (if (interactive-p)
  9548. (message "%d match(es) for regexp %s" cnt regexp))
  9549. cnt))
  9550. (defun org-show-context (&optional key)
  9551. "Make sure point and context and visible.
  9552. How much context is shown depends upon the variables
  9553. `org-show-hierarchy-above', `org-show-following-heading'. and
  9554. `org-show-siblings'."
  9555. (let ((heading-p (org-on-heading-p t))
  9556. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  9557. (following-p (org-get-alist-option org-show-following-heading key))
  9558. (entry-p (org-get-alist-option org-show-entry-below key))
  9559. (siblings-p (org-get-alist-option org-show-siblings key)))
  9560. (catch 'exit
  9561. ;; Show heading or entry text
  9562. (if (and heading-p (not entry-p))
  9563. (org-flag-heading nil) ; only show the heading
  9564. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  9565. (org-show-hidden-entry))) ; show entire entry
  9566. (when following-p
  9567. ;; Show next sibling, or heading below text
  9568. (save-excursion
  9569. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  9570. (org-flag-heading nil))))
  9571. (when siblings-p (org-show-siblings))
  9572. (when hierarchy-p
  9573. ;; show all higher headings, possibly with siblings
  9574. (save-excursion
  9575. (while (and (condition-case nil
  9576. (progn (org-up-heading-all 1) t)
  9577. (error nil))
  9578. (not (bobp)))
  9579. (org-flag-heading nil)
  9580. (when siblings-p (org-show-siblings))))))))
  9581. (defun org-reveal (&optional siblings)
  9582. "Show current entry, hierarchy above it, and the following headline.
  9583. This can be used to show a consistent set of context around locations
  9584. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  9585. not t for the search context.
  9586. With optional argument SIBLINGS, on each level of the hierarchy all
  9587. siblings are shown. This repairs the tree structure to what it would
  9588. look like when opened with hierarchical calls to `org-cycle'."
  9589. (interactive "P")
  9590. (let ((org-show-hierarchy-above t)
  9591. (org-show-following-heading t)
  9592. (org-show-siblings (if siblings t org-show-siblings)))
  9593. (org-show-context nil)))
  9594. (defun org-highlight-new-match (beg end)
  9595. "Highlight from BEG to END and mark the highlight is an occur headline."
  9596. (let ((ov (org-make-overlay beg end)))
  9597. (org-overlay-put ov 'face 'secondary-selection)
  9598. (push ov org-occur-highlights)))
  9599. (defun org-remove-occur-highlights (&optional beg end noremove)
  9600. "Remove the occur highlights from the buffer.
  9601. BEG and END are ignored. If NOREMOVE is nil, remove this function
  9602. from the `before-change-functions' in the current buffer."
  9603. (interactive)
  9604. (unless org-inhibit-highlight-removal
  9605. (mapc 'org-delete-overlay org-occur-highlights)
  9606. (setq org-occur-highlights nil)
  9607. (setq org-occur-parameters nil)
  9608. (unless noremove
  9609. (remove-hook 'before-change-functions
  9610. 'org-remove-occur-highlights 'local))))
  9611. ;;;; Priorities
  9612. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  9613. "Regular expression matching the priority indicator.")
  9614. (defvar org-remove-priority-next-time nil)
  9615. (defun org-priority-up ()
  9616. "Increase the priority of the current item."
  9617. (interactive)
  9618. (org-priority 'up))
  9619. (defun org-priority-down ()
  9620. "Decrease the priority of the current item."
  9621. (interactive)
  9622. (org-priority 'down))
  9623. (defun org-priority (&optional action)
  9624. "Change the priority of an item by ARG.
  9625. ACTION can be `set', `up', `down', or a character."
  9626. (interactive)
  9627. (unless org-enable-priority-commands
  9628. (error "Priority commands are disabled"))
  9629. (setq action (or action 'set))
  9630. (let (current new news have remove)
  9631. (save-excursion
  9632. (org-back-to-heading t)
  9633. (if (looking-at org-priority-regexp)
  9634. (setq current (string-to-char (match-string 2))
  9635. have t)
  9636. (setq current org-default-priority))
  9637. (cond
  9638. ((or (eq action 'set)
  9639. (if (featurep 'xemacs) (characterp action) (integerp action)))
  9640. (if (not (eq action 'set))
  9641. (setq new action)
  9642. (message "Priority %c-%c, SPC to remove: "
  9643. org-highest-priority org-lowest-priority)
  9644. (setq new (read-char-exclusive)))
  9645. (if (and (= (upcase org-highest-priority) org-highest-priority)
  9646. (= (upcase org-lowest-priority) org-lowest-priority))
  9647. (setq new (upcase new)))
  9648. (cond ((equal new ?\ ) (setq remove t))
  9649. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  9650. (error "Priority must be between `%c' and `%c'"
  9651. org-highest-priority org-lowest-priority))))
  9652. ((eq action 'up)
  9653. (if (and (not have) (eq last-command this-command))
  9654. (setq new org-lowest-priority)
  9655. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9656. org-default-priority (1- current)))))
  9657. ((eq action 'down)
  9658. (if (and (not have) (eq last-command this-command))
  9659. (setq new org-highest-priority)
  9660. (setq new (if (and org-priority-start-cycle-with-default (not have))
  9661. org-default-priority (1+ current)))))
  9662. (t (error "Invalid action")))
  9663. (if (or (< (upcase new) org-highest-priority)
  9664. (> (upcase new) org-lowest-priority))
  9665. (setq remove t))
  9666. (setq news (format "%c" new))
  9667. (if have
  9668. (if remove
  9669. (replace-match "" t t nil 1)
  9670. (replace-match news t t nil 2))
  9671. (if remove
  9672. (error "No priority cookie found in line")
  9673. (let ((case-fold-search nil))
  9674. (looking-at org-todo-line-regexp))
  9675. (if (match-end 2)
  9676. (progn
  9677. (goto-char (match-end 2))
  9678. (insert " [#" news "]"))
  9679. (goto-char (match-beginning 3))
  9680. (insert "[#" news "] "))))
  9681. (org-preserve-lc (org-set-tags nil 'align)))
  9682. (if remove
  9683. (message "Priority removed")
  9684. (message "Priority of current item set to %s" news))))
  9685. (defun org-get-priority (s)
  9686. "Find priority cookie and return priority."
  9687. (save-match-data
  9688. (if (not (string-match org-priority-regexp s))
  9689. (* 1000 (- org-lowest-priority org-default-priority))
  9690. (* 1000 (- org-lowest-priority
  9691. (string-to-char (match-string 2 s)))))))
  9692. ;;;; Tags
  9693. (defvar org-agenda-archives-mode)
  9694. (defvar org-map-continue-from nil
  9695. "Position from where mapping should continue.
  9696. Can be set byt the action argument to `org-scan-tag's and `org-map-entries'.")
  9697. (defvar org-scanner-tags nil
  9698. "The current tag list while the tags scanner is running.")
  9699. (defvar org-trust-scanner-tags nil
  9700. "Should `org-get-tags-at' use the tags fro the scanner.
  9701. This is for internal dynamical scoping only.
  9702. When this is non-nil, the function `org-get-tags-at' will return the value
  9703. of `org-scanner-tags' instead of building the list by itself. This
  9704. can lead to large speed-ups when the tags scanner is used in a file with
  9705. many entries, and when the list of tags is retrieved, for example to
  9706. obtain a list of properties. Building the tags list for each entry in such
  9707. a file becomes an N^2 operation - but with this variable set, it scales
  9708. as N.")
  9709. (defun org-scan-tags (action matcher &optional todo-only)
  9710. "Scan headline tags with inheritance and produce output ACTION.
  9711. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  9712. or `agenda' to produce an entry list for an agenda view. It can also be
  9713. a Lisp form or a function that should be called at each matched headline, in
  9714. this case the return value is a list of all return values from these calls.
  9715. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  9716. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  9717. only lines with a TODO keyword are included in the output."
  9718. (require 'org-agenda)
  9719. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  9720. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  9721. (org-re
  9722. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  9723. (props (list 'face 'default
  9724. 'done-face 'org-agenda-done
  9725. 'undone-face 'default
  9726. 'mouse-face 'highlight
  9727. 'org-not-done-regexp org-not-done-regexp
  9728. 'org-todo-regexp org-todo-regexp
  9729. 'keymap org-agenda-keymap
  9730. 'help-echo
  9731. (format "mouse-2 or RET jump to org file %s"
  9732. (abbreviate-file-name
  9733. (or (buffer-file-name (buffer-base-buffer))
  9734. (buffer-name (buffer-base-buffer)))))))
  9735. (case-fold-search nil)
  9736. (org-map-continue-from nil)
  9737. lspos tags tags-list
  9738. (tags-alist (list (cons 0 org-file-tags)))
  9739. (llast 0) rtn rtn1 level category i txt
  9740. todo marker entry priority)
  9741. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  9742. (setq action (list 'lambda nil action)))
  9743. (save-excursion
  9744. (goto-char (point-min))
  9745. (when (eq action 'sparse-tree)
  9746. (org-overview)
  9747. (org-remove-occur-highlights))
  9748. (while (re-search-forward re nil t)
  9749. (catch :skip
  9750. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  9751. tags (if (match-end 4) (org-match-string-no-properties 4)))
  9752. (goto-char (setq lspos (match-beginning 0)))
  9753. (setq level (org-reduced-level (funcall outline-level))
  9754. category (org-get-category))
  9755. (setq i llast llast level)
  9756. ;; remove tag lists from same and sublevels
  9757. (while (>= i level)
  9758. (when (setq entry (assoc i tags-alist))
  9759. (setq tags-alist (delete entry tags-alist)))
  9760. (setq i (1- i)))
  9761. ;; add the next tags
  9762. (when tags
  9763. (setq tags (org-split-string tags ":")
  9764. tags-alist
  9765. (cons (cons level tags) tags-alist)))
  9766. ;; compile tags for current headline
  9767. (setq tags-list
  9768. (if org-use-tag-inheritance
  9769. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  9770. tags)
  9771. org-scanner-tags tags-list)
  9772. (when org-use-tag-inheritance
  9773. (setcdr (car tags-alist)
  9774. (mapcar (lambda (x)
  9775. (setq x (copy-sequence x))
  9776. (org-add-prop-inherited x))
  9777. (cdar tags-alist))))
  9778. (when (and tags org-use-tag-inheritance
  9779. (or (not (eq t org-use-tag-inheritance))
  9780. org-tags-exclude-from-inheritance))
  9781. ;; selective inheritance, remove uninherited ones
  9782. (setcdr (car tags-alist)
  9783. (org-remove-uniherited-tags (cdar tags-alist))))
  9784. (when (and (or (not todo-only)
  9785. (and (member todo org-not-done-keywords)
  9786. (or (not org-agenda-tags-todo-honor-ignore-options)
  9787. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  9788. (let ((case-fold-search t)) (eval matcher))
  9789. (or
  9790. (not (member org-archive-tag tags-list))
  9791. ;; we have an archive tag, should we use this anyway?
  9792. (or (not org-agenda-skip-archived-trees)
  9793. (and (eq action 'agenda) org-agenda-archives-mode))))
  9794. (unless (eq action 'sparse-tree) (org-agenda-skip))
  9795. ;; select this headline
  9796. (cond
  9797. ((eq action 'sparse-tree)
  9798. (and org-highlight-sparse-tree-matches
  9799. (org-get-heading) (match-end 0)
  9800. (org-highlight-new-match
  9801. (match-beginning 0) (match-beginning 1)))
  9802. (org-show-context 'tags-tree))
  9803. ((eq action 'agenda)
  9804. (setq txt (org-format-agenda-item
  9805. ""
  9806. (concat
  9807. (if (eq org-tags-match-list-sublevels 'indented)
  9808. (make-string (1- level) ?.) "")
  9809. (org-get-heading))
  9810. category
  9811. tags-list
  9812. )
  9813. priority (org-get-priority txt))
  9814. (goto-char lspos)
  9815. (setq marker (org-agenda-new-marker))
  9816. (org-add-props txt props
  9817. 'org-marker marker 'org-hd-marker marker 'org-category category
  9818. 'todo-state todo
  9819. 'priority priority 'type "tagsmatch")
  9820. (push txt rtn))
  9821. ((functionp action)
  9822. (setq org-map-continue-from nil)
  9823. (save-excursion
  9824. (setq rtn1 (funcall action))
  9825. (push rtn1 rtn)))
  9826. (t (error "Invalid action")))
  9827. ;; if we are to skip sublevels, jump to end of subtree
  9828. (unless org-tags-match-list-sublevels
  9829. (org-end-of-subtree t)
  9830. (backward-char 1))))
  9831. ;; Get the correct position from where to continue
  9832. (if org-map-continue-from
  9833. (goto-char org-map-continue-from)
  9834. (and (= (point) lspos) (end-of-line 1)))))
  9835. (when (and (eq action 'sparse-tree)
  9836. (not org-sparse-tree-open-archived-trees))
  9837. (org-hide-archived-subtrees (point-min) (point-max)))
  9838. (nreverse rtn)))
  9839. (defun org-remove-uniherited-tags (tags)
  9840. "Remove all tags that are not inherited from the list TAGS."
  9841. (cond
  9842. ((eq org-use-tag-inheritance t)
  9843. (if org-tags-exclude-from-inheritance
  9844. (org-delete-all org-tags-exclude-from-inheritance tags)
  9845. tags))
  9846. ((not org-use-tag-inheritance) nil)
  9847. ((stringp org-use-tag-inheritance)
  9848. (delq nil (mapcar
  9849. (lambda (x)
  9850. (if (and (string-match org-use-tag-inheritance x)
  9851. (not (member x org-tags-exclude-from-inheritance)))
  9852. x nil))
  9853. tags)))
  9854. ((listp org-use-tag-inheritance)
  9855. (delq nil (mapcar
  9856. (lambda (x)
  9857. (if (member x org-use-tag-inheritance) x nil))
  9858. tags)))))
  9859. (defvar todo-only) ;; dynamically scoped
  9860. (defun org-match-sparse-tree (&optional todo-only match)
  9861. "Create a sparse tree according to tags string MATCH.
  9862. MATCH can contain positive and negative selection of tags, like
  9863. \"+WORK+URGENT-WITHBOSS\".
  9864. If optional argument TODO-ONLY is non-nil, only select lines that are
  9865. also TODO lines."
  9866. (interactive "P")
  9867. (org-prepare-agenda-buffers (list (current-buffer)))
  9868. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  9869. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  9870. (defvar org-cached-props nil)
  9871. (defun org-cached-entry-get (pom property)
  9872. (if (or (eq t org-use-property-inheritance)
  9873. (and (stringp org-use-property-inheritance)
  9874. (string-match org-use-property-inheritance property))
  9875. (and (listp org-use-property-inheritance)
  9876. (member property org-use-property-inheritance)))
  9877. ;; Caching is not possible, check it directly
  9878. (org-entry-get pom property 'inherit)
  9879. ;; Get all properties, so that we can do complicated checks easily
  9880. (cdr (assoc property (or org-cached-props
  9881. (setq org-cached-props
  9882. (org-entry-properties pom)))))))
  9883. (defun org-global-tags-completion-table (&optional files)
  9884. "Return the list of all tags in all agenda buffer/files."
  9885. (save-excursion
  9886. (org-uniquify
  9887. (delq nil
  9888. (apply 'append
  9889. (mapcar
  9890. (lambda (file)
  9891. (set-buffer (find-file-noselect file))
  9892. (append (org-get-buffer-tags)
  9893. (mapcar (lambda (x) (if (stringp (car-safe x))
  9894. (list (car-safe x)) nil))
  9895. org-tag-alist)))
  9896. (if (and files (car files))
  9897. files
  9898. (org-agenda-files))))))))
  9899. (defun org-make-tags-matcher (match)
  9900. "Create the TAGS//TODO matcher form for the selection string MATCH."
  9901. ;; todo-only is scoped dynamically into this function, and the function
  9902. ;; may change it if the matcher asks for it.
  9903. (unless match
  9904. ;; Get a new match request, with completion
  9905. (let ((org-last-tags-completion-table
  9906. (org-global-tags-completion-table)))
  9907. (setq match (org-completing-read-no-i
  9908. "Match: " 'org-tags-completion-function nil nil nil
  9909. 'org-tags-history))))
  9910. ;; Parse the string and create a lisp form
  9911. (let ((match0 match)
  9912. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9913. minus tag mm
  9914. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9915. orterms term orlist re-p str-p level-p level-op time-p
  9916. prop-p pn pv po cat-p gv rest)
  9917. (if (string-match "/+" match)
  9918. ;; match contains also a todo-matching request
  9919. (progn
  9920. (setq tagsmatch (substring match 0 (match-beginning 0))
  9921. todomatch (substring match (match-end 0)))
  9922. (if (string-match "^!" todomatch)
  9923. (setq todo-only t todomatch (substring todomatch 1)))
  9924. (if (string-match "^\\s-*$" todomatch)
  9925. (setq todomatch nil)))
  9926. ;; only matching tags
  9927. (setq tagsmatch match todomatch nil))
  9928. ;; Make the tags matcher
  9929. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9930. (setq tagsmatcher t)
  9931. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9932. (while (setq term (pop orterms))
  9933. (while (and (equal (substring term -1) "\\") orterms)
  9934. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9935. (while (string-match re term)
  9936. (setq rest (substring term (match-end 0))
  9937. minus (and (match-end 1)
  9938. (equal (match-string 1 term) "-"))
  9939. tag (match-string 2 term)
  9940. re-p (equal (string-to-char tag) ?{)
  9941. level-p (match-end 4)
  9942. prop-p (match-end 5)
  9943. mm (cond
  9944. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9945. (level-p
  9946. (setq level-op (org-op-to-function (match-string 3 term)))
  9947. `(,level-op level ,(string-to-number
  9948. (match-string 4 term))))
  9949. (prop-p
  9950. (setq pn (match-string 5 term)
  9951. po (match-string 6 term)
  9952. pv (match-string 7 term)
  9953. cat-p (equal pn "CATEGORY")
  9954. re-p (equal (string-to-char pv) ?{)
  9955. str-p (equal (string-to-char pv) ?\")
  9956. time-p (save-match-data
  9957. (string-match "^\"[[<].*[]>]\"$" pv))
  9958. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9959. (if time-p (setq pv (org-matcher-time pv)))
  9960. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9961. (cond
  9962. ((equal pn "CATEGORY")
  9963. (setq gv '(get-text-property (point) 'org-category)))
  9964. ((equal pn "TODO")
  9965. (setq gv 'todo))
  9966. (t
  9967. (setq gv `(org-cached-entry-get nil ,pn))))
  9968. (if re-p
  9969. (if (eq po 'org<>)
  9970. `(not (string-match ,pv (or ,gv "")))
  9971. `(string-match ,pv (or ,gv "")))
  9972. (if str-p
  9973. `(,po (or ,gv "") ,pv)
  9974. `(,po (string-to-number (or ,gv ""))
  9975. ,(string-to-number pv) ))))
  9976. (t `(member ,tag tags-list)))
  9977. mm (if minus (list 'not mm) mm)
  9978. term rest)
  9979. (push mm tagsmatcher))
  9980. (push (if (> (length tagsmatcher) 1)
  9981. (cons 'and tagsmatcher)
  9982. (car tagsmatcher))
  9983. orlist)
  9984. (setq tagsmatcher nil))
  9985. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9986. (setq tagsmatcher
  9987. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9988. ;; Make the todo matcher
  9989. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9990. (setq todomatcher t)
  9991. (setq orterms (org-split-string todomatch "|") orlist nil)
  9992. (while (setq term (pop orterms))
  9993. (while (string-match re term)
  9994. (setq minus (and (match-end 1)
  9995. (equal (match-string 1 term) "-"))
  9996. kwd (match-string 2 term)
  9997. re-p (equal (string-to-char kwd) ?{)
  9998. term (substring term (match-end 0))
  9999. mm (if re-p
  10000. `(string-match ,(substring kwd 1 -1) todo)
  10001. (list 'equal 'todo kwd))
  10002. mm (if minus (list 'not mm) mm))
  10003. (push mm todomatcher))
  10004. (push (if (> (length todomatcher) 1)
  10005. (cons 'and todomatcher)
  10006. (car todomatcher))
  10007. orlist)
  10008. (setq todomatcher nil))
  10009. (setq todomatcher (if (> (length orlist) 1)
  10010. (cons 'or orlist) (car orlist))))
  10011. ;; Return the string and lisp forms of the matcher
  10012. (setq matcher (if todomatcher
  10013. (list 'and tagsmatcher todomatcher)
  10014. tagsmatcher))
  10015. (cons match0 matcher)))
  10016. (defun org-op-to-function (op &optional stringp)
  10017. "Turn an operator into the appropriate function."
  10018. (setq op
  10019. (cond
  10020. ((equal op "<" ) '(< string< org-time<))
  10021. ((equal op ">" ) '(> org-string> org-time>))
  10022. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10023. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10024. ((member op '("=" "==")) '(= string= org-time=))
  10025. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10026. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10027. (defun org<> (a b) (not (= a b)))
  10028. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10029. (defun org-string>= (a b) (not (string< a b)))
  10030. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10031. (defun org-string<> (a b) (not (string= a b)))
  10032. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10033. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10034. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10035. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10036. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10037. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10038. (defun org-2ft (s)
  10039. "Convert S to a floating point time.
  10040. If S is already a number, just return it. If it is a string, parse
  10041. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10042. (cond
  10043. ((numberp s) s)
  10044. ((stringp s)
  10045. (condition-case nil
  10046. (float-time (apply 'encode-time (org-parse-time-string s)))
  10047. (error 0.)))
  10048. (t 0.)))
  10049. (defun org-time-today ()
  10050. "Time in seconds today at 0:00.
  10051. Returns the float number of seconds since the beginning of the
  10052. epoch to the beginning of today (00:00)."
  10053. (float-time (apply 'encode-time
  10054. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10055. (defun org-matcher-time (s)
  10056. "Interpret a time comparison value."
  10057. (save-match-data
  10058. (cond
  10059. ((string= s "<now>") (float-time))
  10060. ((string= s "<today>") (org-time-today))
  10061. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10062. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10063. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10064. (+ (org-time-today)
  10065. (* (string-to-number (match-string 1 s))
  10066. (cdr (assoc (match-string 2 s)
  10067. '(("d" . 86400.0) ("w" . 604800.0)
  10068. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10069. (t (org-2ft s)))))
  10070. (defun org-match-any-p (re list)
  10071. "Does re match any element of list?"
  10072. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10073. (delq nil list))
  10074. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10075. (defvar org-tags-overlay (org-make-overlay 1 1))
  10076. (org-detach-overlay org-tags-overlay)
  10077. (defun org-get-local-tags-at (&optional pos)
  10078. "Get a list of tags defined in the current headline."
  10079. (org-get-tags-at pos 'local))
  10080. (defun org-get-local-tags ()
  10081. "Get a list of tags defined in the current headline."
  10082. (org-get-tags-at nil 'local))
  10083. (defun org-get-tags-at (&optional pos local)
  10084. "Get a list of all headline tags applicable at POS.
  10085. POS defaults to point. If tags are inherited, the list contains
  10086. the targets in the same sequence as the headlines appear, i.e.
  10087. the tags of the current headline come last.
  10088. When LOCAL is non-nil, only return tags from the current headline,
  10089. ignore inherited ones."
  10090. (interactive)
  10091. (if (and org-trust-scanner-tags
  10092. (or (not pos) (equal pos (point)))
  10093. (not local))
  10094. org-scanner-tags
  10095. (let (tags ltags lastpos parent)
  10096. (save-excursion
  10097. (save-restriction
  10098. (widen)
  10099. (goto-char (or pos (point)))
  10100. (save-match-data
  10101. (catch 'done
  10102. (condition-case nil
  10103. (progn
  10104. (org-back-to-heading t)
  10105. (while (not (equal lastpos (point)))
  10106. (setq lastpos (point))
  10107. (when (looking-at
  10108. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10109. (setq ltags (org-split-string
  10110. (org-match-string-no-properties 1) ":"))
  10111. (when parent
  10112. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10113. (setq tags (append
  10114. (if parent
  10115. (org-remove-uniherited-tags ltags)
  10116. ltags)
  10117. tags)))
  10118. (or org-use-tag-inheritance (throw 'done t))
  10119. (if local (throw 'done t))
  10120. (or (org-up-heading-safe) (error nil))
  10121. (setq parent t)))
  10122. (error nil)))))
  10123. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10124. (defun org-add-prop-inherited (s)
  10125. (add-text-properties 0 (length s) '(inherited t) s)
  10126. s)
  10127. (defun org-toggle-tag (tag &optional onoff)
  10128. "Toggle the tag TAG for the current line.
  10129. If ONOFF is `on' or `off', don't toggle but set to this state."
  10130. (let (res current)
  10131. (save-excursion
  10132. (org-back-to-heading t)
  10133. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10134. (point-at-eol) t)
  10135. (progn
  10136. (setq current (match-string 1))
  10137. (replace-match ""))
  10138. (setq current ""))
  10139. (setq current (nreverse (org-split-string current ":")))
  10140. (cond
  10141. ((eq onoff 'on)
  10142. (setq res t)
  10143. (or (member tag current) (push tag current)))
  10144. ((eq onoff 'off)
  10145. (or (not (member tag current)) (setq current (delete tag current))))
  10146. (t (if (member tag current)
  10147. (setq current (delete tag current))
  10148. (setq res t)
  10149. (push tag current))))
  10150. (end-of-line 1)
  10151. (if current
  10152. (progn
  10153. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10154. (org-set-tags nil t))
  10155. (delete-horizontal-space))
  10156. (run-hooks 'org-after-tags-change-hook))
  10157. res))
  10158. (defun org-align-tags-here (to-col)
  10159. ;; Assumes that this is a headline
  10160. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10161. (beginning-of-line 1)
  10162. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10163. (< pos (match-beginning 2)))
  10164. (progn
  10165. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10166. (goto-char (match-beginning 1))
  10167. (insert " ")
  10168. (delete-region (point) (1+ (match-beginning 2)))
  10169. (setq ncol (max (1+ (current-column))
  10170. (1+ col)
  10171. (if (> to-col 0)
  10172. to-col
  10173. (- (abs to-col) tags-l))))
  10174. (setq p (point))
  10175. (insert (make-string (- ncol (current-column)) ?\ ))
  10176. (setq ncol (current-column))
  10177. (when indent-tabs-mode (tabify p (point-at-eol)))
  10178. (org-move-to-column (min ncol col) t))
  10179. (goto-char pos))))
  10180. (defun org-set-tags-command (&optional arg just-align)
  10181. "Call the set-tags command for the current entry."
  10182. (interactive "P")
  10183. (if (org-on-heading-p)
  10184. (org-set-tags arg just-align)
  10185. (save-excursion
  10186. (org-back-to-heading t)
  10187. (org-set-tags arg just-align))))
  10188. (defun org-set-tags (&optional arg just-align)
  10189. "Set the tags for the current headline.
  10190. With prefix ARG, realign all tags in headings in the current buffer."
  10191. (interactive "P")
  10192. (let* ((re (concat "^" outline-regexp))
  10193. (current (org-get-tags-string))
  10194. (col (current-column))
  10195. (org-setting-tags t)
  10196. table current-tags inherited-tags ; computed below when needed
  10197. tags p0 c0 c1 rpl)
  10198. (if arg
  10199. (save-excursion
  10200. (goto-char (point-min))
  10201. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  10202. (while (re-search-forward re nil t)
  10203. (org-set-tags nil t)
  10204. (end-of-line 1)))
  10205. (message "All tags realigned to column %d" org-tags-column))
  10206. (if just-align
  10207. (setq tags current)
  10208. ;; Get a new set of tags from the user
  10209. (save-excursion
  10210. (setq table (append org-tag-persistent-alist
  10211. (or org-tag-alist (org-get-buffer-tags)))
  10212. org-last-tags-completion-table table
  10213. current-tags (org-split-string current ":")
  10214. inherited-tags (nreverse
  10215. (nthcdr (length current-tags)
  10216. (nreverse (org-get-tags-at))))
  10217. tags
  10218. (if (or (eq t org-use-fast-tag-selection)
  10219. (and org-use-fast-tag-selection
  10220. (delq nil (mapcar 'cdr table))))
  10221. (org-fast-tag-selection
  10222. current-tags inherited-tags table
  10223. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  10224. (let ((org-add-colon-after-tag-completion t))
  10225. (org-trim
  10226. (org-without-partial-completion
  10227. (org-icompleting-read "Tags: " 'org-tags-completion-function
  10228. nil nil current 'org-tags-history)))))))
  10229. (while (string-match "[-+&]+" tags)
  10230. ;; No boolean logic, just a list
  10231. (setq tags (replace-match ":" t t tags))))
  10232. (if org-tags-sort-function
  10233. (setq tags (mapconcat 'identity
  10234. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  10235. org-tags-sort-function) ":")))
  10236. (if (string-match "\\`[\t ]*\\'" tags)
  10237. (setq tags "")
  10238. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  10239. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  10240. ;; Insert new tags at the correct column
  10241. (beginning-of-line 1)
  10242. (cond
  10243. ((and (equal current "") (equal tags "")))
  10244. ((re-search-forward
  10245. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  10246. (point-at-eol) t)
  10247. (if (equal tags "")
  10248. (setq rpl "")
  10249. (goto-char (match-beginning 0))
  10250. (setq c0 (current-column) p0 (point)
  10251. c1 (max (1+ c0) (if (> org-tags-column 0)
  10252. org-tags-column
  10253. (- (- org-tags-column) (length tags))))
  10254. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  10255. (replace-match rpl t t)
  10256. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  10257. tags)
  10258. (t (error "Tags alignment failed")))
  10259. (org-move-to-column col)
  10260. (unless just-align
  10261. (run-hooks 'org-after-tags-change-hook)))))
  10262. (defun org-change-tag-in-region (beg end tag off)
  10263. "Add or remove TAG for each entry in the region.
  10264. This works in the agenda, and also in an org-mode buffer."
  10265. (interactive
  10266. (list (region-beginning) (region-end)
  10267. (let ((org-last-tags-completion-table
  10268. (if (org-mode-p)
  10269. (org-get-buffer-tags)
  10270. (org-global-tags-completion-table))))
  10271. (org-icompleting-read
  10272. "Tag: " 'org-tags-completion-function nil nil nil
  10273. 'org-tags-history))
  10274. (progn
  10275. (message "[s]et or [r]emove? ")
  10276. (equal (read-char-exclusive) ?r))))
  10277. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10278. (let ((agendap (equal major-mode 'org-agenda-mode))
  10279. l1 l2 m buf pos newhead (cnt 0))
  10280. (goto-char end)
  10281. (setq l2 (1- (org-current-line)))
  10282. (goto-char beg)
  10283. (setq l1 (org-current-line))
  10284. (loop for l from l1 to l2 do
  10285. (org-goto-line l)
  10286. (setq m (get-text-property (point) 'org-hd-marker))
  10287. (when (or (and (org-mode-p) (org-on-heading-p))
  10288. (and agendap m))
  10289. (setq buf (if agendap (marker-buffer m) (current-buffer))
  10290. pos (if agendap m (point)))
  10291. (with-current-buffer buf
  10292. (save-excursion
  10293. (save-restriction
  10294. (goto-char pos)
  10295. (setq cnt (1+ cnt))
  10296. (org-toggle-tag tag (if off 'off 'on))
  10297. (setq newhead (org-get-heading)))))
  10298. (and agendap (org-agenda-change-all-lines newhead m))))
  10299. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  10300. (defun org-tags-completion-function (string predicate &optional flag)
  10301. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  10302. (confirm (lambda (x) (stringp (car x)))))
  10303. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  10304. (setq s1 (match-string 1 string)
  10305. s2 (match-string 2 string))
  10306. (setq s1 "" s2 string))
  10307. (cond
  10308. ((eq flag nil)
  10309. ;; try completion
  10310. (setq rtn (try-completion s2 ctable confirm))
  10311. (if (stringp rtn)
  10312. (setq rtn
  10313. (concat s1 s2 (substring rtn (length s2))
  10314. (if (and org-add-colon-after-tag-completion
  10315. (assoc rtn ctable))
  10316. ":" ""))))
  10317. rtn)
  10318. ((eq flag t)
  10319. ;; all-completions
  10320. (all-completions s2 ctable confirm)
  10321. )
  10322. ((eq flag 'lambda)
  10323. ;; exact match?
  10324. (assoc s2 ctable)))
  10325. ))
  10326. (defun org-fast-tag-insert (kwd tags face &optional end)
  10327. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  10328. (insert (format "%-12s" (concat kwd ":"))
  10329. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  10330. (or end "")))
  10331. (defun org-fast-tag-show-exit (flag)
  10332. (save-excursion
  10333. (org-goto-line 3)
  10334. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  10335. (replace-match ""))
  10336. (when flag
  10337. (end-of-line 1)
  10338. (org-move-to-column (- (window-width) 19) t)
  10339. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  10340. (defun org-set-current-tags-overlay (current prefix)
  10341. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  10342. (if (featurep 'xemacs)
  10343. (org-overlay-display org-tags-overlay (concat prefix s)
  10344. 'secondary-selection)
  10345. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  10346. (org-overlay-display org-tags-overlay (concat prefix s)))))
  10347. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  10348. "Fast tag selection with single keys.
  10349. CURRENT is the current list of tags in the headline, INHERITED is the
  10350. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  10351. possibly with grouping information. TODO-TABLE is a similar table with
  10352. TODO keywords, should these have keys assigned to them.
  10353. If the keys are nil, a-z are automatically assigned.
  10354. Returns the new tags string, or nil to not change the current settings."
  10355. (let* ((fulltable (append table todo-table))
  10356. (maxlen (apply 'max (mapcar
  10357. (lambda (x)
  10358. (if (stringp (car x)) (string-width (car x)) 0))
  10359. fulltable)))
  10360. (buf (current-buffer))
  10361. (expert (eq org-fast-tag-selection-single-key 'expert))
  10362. (buffer-tags nil)
  10363. (fwidth (+ maxlen 3 1 3))
  10364. (ncol (/ (- (window-width) 4) fwidth))
  10365. (i-face 'org-done)
  10366. (c-face 'org-todo)
  10367. tg cnt e c char c1 c2 ntable tbl rtn
  10368. ov-start ov-end ov-prefix
  10369. (exit-after-next org-fast-tag-selection-single-key)
  10370. (done-keywords org-done-keywords)
  10371. groups ingroup)
  10372. (save-excursion
  10373. (beginning-of-line 1)
  10374. (if (looking-at
  10375. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10376. (setq ov-start (match-beginning 1)
  10377. ov-end (match-end 1)
  10378. ov-prefix "")
  10379. (setq ov-start (1- (point-at-eol))
  10380. ov-end (1+ ov-start))
  10381. (skip-chars-forward "^\n\r")
  10382. (setq ov-prefix
  10383. (concat
  10384. (buffer-substring (1- (point)) (point))
  10385. (if (> (current-column) org-tags-column)
  10386. " "
  10387. (make-string (- org-tags-column (current-column)) ?\ ))))))
  10388. (org-move-overlay org-tags-overlay ov-start ov-end)
  10389. (save-window-excursion
  10390. (if expert
  10391. (set-buffer (get-buffer-create " *Org tags*"))
  10392. (delete-other-windows)
  10393. (split-window-vertically)
  10394. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  10395. (erase-buffer)
  10396. (org-set-local 'org-done-keywords done-keywords)
  10397. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  10398. (org-fast-tag-insert "Current" current c-face "\n\n")
  10399. (org-fast-tag-show-exit exit-after-next)
  10400. (org-set-current-tags-overlay current ov-prefix)
  10401. (setq tbl fulltable char ?a cnt 0)
  10402. (while (setq e (pop tbl))
  10403. (cond
  10404. ((equal e '(:startgroup))
  10405. (push '() groups) (setq ingroup t)
  10406. (when (not (= cnt 0))
  10407. (setq cnt 0)
  10408. (insert "\n"))
  10409. (insert "{ "))
  10410. ((equal e '(:endgroup))
  10411. (setq ingroup nil cnt 0)
  10412. (insert "}\n"))
  10413. ((equal e '(:newline))
  10414. (when (not (= cnt 0))
  10415. (setq cnt 0)
  10416. (insert "\n")
  10417. (setq e (car tbl))
  10418. (while (equal (car tbl) '(:newline))
  10419. (insert "\n")
  10420. (setq tbl (cdr tbl)))))
  10421. (t
  10422. (setq tg (copy-sequence (car e)) c2 nil)
  10423. (if (cdr e)
  10424. (setq c (cdr e))
  10425. ;; automatically assign a character.
  10426. (setq c1 (string-to-char
  10427. (downcase (substring
  10428. tg (if (= (string-to-char tg) ?@) 1 0)))))
  10429. (if (or (rassoc c1 ntable) (rassoc c1 table))
  10430. (while (or (rassoc char ntable) (rassoc char table))
  10431. (setq char (1+ char)))
  10432. (setq c2 c1))
  10433. (setq c (or c2 char)))
  10434. (if ingroup (push tg (car groups)))
  10435. (setq tg (org-add-props tg nil 'face
  10436. (cond
  10437. ((not (assoc tg table))
  10438. (org-get-todo-face tg))
  10439. ((member tg current) c-face)
  10440. ((member tg inherited) i-face)
  10441. (t nil))))
  10442. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10443. (insert "[" c "] " tg (make-string
  10444. (- fwidth 4 (length tg)) ?\ ))
  10445. (push (cons tg c) ntable)
  10446. (when (= (setq cnt (1+ cnt)) ncol)
  10447. (insert "\n")
  10448. (if ingroup (insert " "))
  10449. (setq cnt 0)))))
  10450. (setq ntable (nreverse ntable))
  10451. (insert "\n")
  10452. (goto-char (point-min))
  10453. (if (not expert) (org-fit-window-to-buffer))
  10454. (setq rtn
  10455. (catch 'exit
  10456. (while t
  10457. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  10458. (if groups " [!] no groups" " [!]groups")
  10459. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  10460. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10461. (cond
  10462. ((= c ?\r) (throw 'exit t))
  10463. ((= c ?!)
  10464. (setq groups (not groups))
  10465. (goto-char (point-min))
  10466. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  10467. ((= c ?\C-c)
  10468. (if (not expert)
  10469. (org-fast-tag-show-exit
  10470. (setq exit-after-next (not exit-after-next)))
  10471. (setq expert nil)
  10472. (delete-other-windows)
  10473. (split-window-vertically)
  10474. (org-switch-to-buffer-other-window " *Org tags*")
  10475. (org-fit-window-to-buffer)))
  10476. ((or (= c ?\C-g)
  10477. (and (= c ?q) (not (rassoc c ntable))))
  10478. (org-detach-overlay org-tags-overlay)
  10479. (setq quit-flag t))
  10480. ((= c ?\ )
  10481. (setq current nil)
  10482. (if exit-after-next (setq exit-after-next 'now)))
  10483. ((= c ?\t)
  10484. (condition-case nil
  10485. (setq tg (org-icompleting-read
  10486. "Tag: "
  10487. (or buffer-tags
  10488. (with-current-buffer buf
  10489. (org-get-buffer-tags)))))
  10490. (quit (setq tg "")))
  10491. (when (string-match "\\S-" tg)
  10492. (add-to-list 'buffer-tags (list tg))
  10493. (if (member tg current)
  10494. (setq current (delete tg current))
  10495. (push tg current)))
  10496. (if exit-after-next (setq exit-after-next 'now)))
  10497. ((setq e (rassoc c todo-table) tg (car e))
  10498. (with-current-buffer buf
  10499. (save-excursion (org-todo tg)))
  10500. (if exit-after-next (setq exit-after-next 'now)))
  10501. ((setq e (rassoc c ntable) tg (car e))
  10502. (if (member tg current)
  10503. (setq current (delete tg current))
  10504. (loop for g in groups do
  10505. (if (member tg g)
  10506. (mapc (lambda (x)
  10507. (setq current (delete x current)))
  10508. g)))
  10509. (push tg current))
  10510. (if exit-after-next (setq exit-after-next 'now))))
  10511. ;; Create a sorted list
  10512. (setq current
  10513. (sort current
  10514. (lambda (a b)
  10515. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  10516. (if (eq exit-after-next 'now) (throw 'exit t))
  10517. (goto-char (point-min))
  10518. (beginning-of-line 2)
  10519. (delete-region (point) (point-at-eol))
  10520. (org-fast-tag-insert "Current" current c-face)
  10521. (org-set-current-tags-overlay current ov-prefix)
  10522. (while (re-search-forward
  10523. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  10524. (setq tg (match-string 1))
  10525. (add-text-properties
  10526. (match-beginning 1) (match-end 1)
  10527. (list 'face
  10528. (cond
  10529. ((member tg current) c-face)
  10530. ((member tg inherited) i-face)
  10531. (t (get-text-property (match-beginning 1) 'face))))))
  10532. (goto-char (point-min)))))
  10533. (org-detach-overlay org-tags-overlay)
  10534. (if rtn
  10535. (mapconcat 'identity current ":")
  10536. nil))))
  10537. (defun org-get-tags-string ()
  10538. "Get the TAGS string in the current headline."
  10539. (unless (org-on-heading-p t)
  10540. (error "Not on a heading"))
  10541. (save-excursion
  10542. (beginning-of-line 1)
  10543. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10544. (org-match-string-no-properties 1)
  10545. "")))
  10546. (defun org-get-tags ()
  10547. "Get the list of tags specified in the current headline."
  10548. (org-split-string (org-get-tags-string) ":"))
  10549. (defun org-get-buffer-tags ()
  10550. "Get a table of all tags used in the buffer, for completion."
  10551. (let (tags)
  10552. (save-excursion
  10553. (goto-char (point-min))
  10554. (while (re-search-forward
  10555. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  10556. (when (equal (char-after (point-at-bol 0)) ?*)
  10557. (mapc (lambda (x) (add-to-list 'tags x))
  10558. (org-split-string (org-match-string-no-properties 1) ":")))))
  10559. (mapcar 'list tags)))
  10560. ;;;; The mapping API
  10561. ;;;###autoload
  10562. (defun org-map-entries (func &optional match scope &rest skip)
  10563. "Call FUNC at each headline selected by MATCH in SCOPE.
  10564. FUNC is a function or a lisp form. The function will be called without
  10565. arguments, with the cursor positioned at the beginning of the headline.
  10566. The return values of all calls to the function will be collected and
  10567. returned as a list.
  10568. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  10569. does not need to preserve point. After evaluation, the cursor will be
  10570. moved to the end of the line (presumably of the headline of the
  10571. processed entry) and search continues from there. Under some
  10572. circumstances, this may not produce the wanted results. For example,
  10573. if you have removed (e.g. archived) the current (sub)tree it could
  10574. mean that the next entry will be skipped entirely. In such cases, you
  10575. can specify the position from where search should continue by making
  10576. FUNC set the variable `org-map-continue-from' to the desired buffer
  10577. position.
  10578. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  10579. Only headlines that are matched by this query will be considered during
  10580. the iteration. When MATCH is nil or t, all headlines will be
  10581. visited by the iteration.
  10582. SCOPE determines the scope of this command. It can be any of:
  10583. nil The current buffer, respecting the restriction if any
  10584. tree The subtree started with the entry at point
  10585. file The current buffer, without restriction
  10586. file-with-archives
  10587. The current buffer, and any archives associated with it
  10588. agenda All agenda files
  10589. agenda-with-archives
  10590. All agenda files with any archive files associated with them
  10591. \(file1 file2 ...)
  10592. If this is a list, all files in the list will be scanned
  10593. The remaining args are treated as settings for the skipping facilities of
  10594. the scanner. The following items can be given here:
  10595. archive skip trees with the archive tag.
  10596. comment skip trees with the COMMENT keyword
  10597. function or Emacs Lisp form:
  10598. will be used as value for `org-agenda-skip-function', so whenever
  10599. the the function returns t, FUNC will not be called for that
  10600. entry and search will continue from the point where the
  10601. function leaves it.
  10602. If your function needs to retrieve the tags including inherited tags
  10603. at the *current* entry, you can use the value of the variable
  10604. `org-scanner-tags' which will be much faster than getting the value
  10605. with `org-get-tags-at'. If your function gets properties with
  10606. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  10607. to t around the call to `org-entry-properties' to get the same speedup.
  10608. Note that if your function moves around to retrieve tags and properties at
  10609. a *different* entry, you cannot use these techniques."
  10610. (let* ((org-agenda-archives-mode nil) ; just to make sure
  10611. (org-agenda-skip-archived-trees (memq 'archive skip))
  10612. (org-agenda-skip-comment-trees (memq 'comment skip))
  10613. (org-agenda-skip-function
  10614. (car (org-delete-all '(comment archive) skip)))
  10615. (org-tags-match-list-sublevels t)
  10616. matcher file res
  10617. org-todo-keywords-for-agenda
  10618. org-done-keywords-for-agenda
  10619. org-todo-keyword-alist-for-agenda
  10620. org-tag-alist-for-agenda)
  10621. (cond
  10622. ((eq match t) (setq matcher t))
  10623. ((eq match nil) (setq matcher t))
  10624. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  10625. (save-excursion
  10626. (save-restriction
  10627. (when (eq scope 'tree)
  10628. (org-back-to-heading t)
  10629. (org-narrow-to-subtree)
  10630. (setq scope nil))
  10631. (if (not scope)
  10632. (progn
  10633. (org-prepare-agenda-buffers
  10634. (list (buffer-file-name (current-buffer))))
  10635. (setq res (org-scan-tags func matcher)))
  10636. ;; Get the right scope
  10637. (cond
  10638. ((and scope (listp scope) (symbolp (car scope)))
  10639. (setq scope (eval scope)))
  10640. ((eq scope 'agenda)
  10641. (setq scope (org-agenda-files t)))
  10642. ((eq scope 'agenda-with-archives)
  10643. (setq scope (org-agenda-files t))
  10644. (setq scope (org-add-archive-files scope)))
  10645. ((eq scope 'file)
  10646. (setq scope (list (buffer-file-name))))
  10647. ((eq scope 'file-with-archives)
  10648. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  10649. (org-prepare-agenda-buffers scope)
  10650. (while (setq file (pop scope))
  10651. (with-current-buffer (org-find-base-buffer-visiting file)
  10652. (save-excursion
  10653. (save-restriction
  10654. (widen)
  10655. (goto-char (point-min))
  10656. (setq res (append res (org-scan-tags func matcher))))))))))
  10657. res))
  10658. ;;;; Properties
  10659. ;;; Setting and retrieving properties
  10660. (defconst org-special-properties
  10661. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  10662. "TIMESTAMP" "TIMESTAMP_IA")
  10663. "The special properties valid in Org-mode.
  10664. These are properties that are not defined in the property drawer,
  10665. but in some other way.")
  10666. (defconst org-default-properties
  10667. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  10668. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  10669. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  10670. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  10671. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  10672. "CLOCK_MODELINE_TOTAL")
  10673. "Some properties that are used by Org-mode for various purposes.
  10674. Being in this list makes sure that they are offered for completion.")
  10675. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  10676. "Regular expression matching the first line of a property drawer.")
  10677. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  10678. "Regular expression matching the first line of a property drawer.")
  10679. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  10680. "Regular expression matching the first line of a property drawer.")
  10681. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  10682. "Regular expression matching the first line of a property drawer.")
  10683. (defconst org-property-drawer-re
  10684. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  10685. org-property-end-re "\\)\n?")
  10686. "Matches an entire property drawer.")
  10687. (defconst org-clock-drawer-re
  10688. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  10689. org-property-end-re "\\)\n?")
  10690. "Matches an entire clock drawer.")
  10691. (defun org-property-action ()
  10692. "Do an action on properties."
  10693. (interactive)
  10694. (let (c)
  10695. (org-at-property-p)
  10696. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  10697. (setq c (read-char-exclusive))
  10698. (cond
  10699. ((equal c ?s)
  10700. (call-interactively 'org-set-property))
  10701. ((equal c ?d)
  10702. (call-interactively 'org-delete-property))
  10703. ((equal c ?D)
  10704. (call-interactively 'org-delete-property-globally))
  10705. ((equal c ?c)
  10706. (call-interactively 'org-compute-property-at-point))
  10707. (t (error "No such property action %c" c)))))
  10708. (defun org-set-effort (&optional value)
  10709. "Set the effort property of the current entry.
  10710. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  10711. allowed value."
  10712. (interactive "P")
  10713. (if (equal value 0) (setq value 10))
  10714. (let* ((completion-ignore-case t)
  10715. (prop org-effort-property)
  10716. (cur (org-entry-get nil prop))
  10717. (allowed (org-property-get-allowed-values nil prop 'table))
  10718. (existing (mapcar 'list (org-property-values prop)))
  10719. (val (cond
  10720. ((stringp value) value)
  10721. ((and allowed (integerp value))
  10722. (or (car (nth (1- value) allowed))
  10723. (car (org-last allowed))))
  10724. (allowed
  10725. (org-completing-read "Value: " allowed nil 'req-match))
  10726. (t
  10727. (let (org-completion-use-ido org-completion-use-iswitchb)
  10728. (org-completing-read
  10729. (concat "Value " (if (and cur (string-match "\\S-" cur))
  10730. (concat "[" cur "]") "")
  10731. ": ")
  10732. existing nil nil "" nil cur))))))
  10733. (unless (equal (org-entry-get nil prop) val)
  10734. (org-entry-put nil prop val))
  10735. (message "%s is now %s" prop val)))
  10736. (defun org-at-property-p ()
  10737. "Is the cursor in a property line?"
  10738. ;; FIXME: Does not check if we are actually in the drawer.
  10739. ;; FIXME: also returns true on any drawers.....
  10740. ;; This is used by C-c C-c for property action.
  10741. (save-excursion
  10742. (beginning-of-line 1)
  10743. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  10744. (defun org-get-property-block (&optional beg end force)
  10745. "Return the (beg . end) range of the body of the property drawer.
  10746. BEG and END can be beginning and end of subtree, if not given
  10747. they will be found.
  10748. If the drawer does not exist and FORCE is non-nil, create the drawer."
  10749. (catch 'exit
  10750. (save-excursion
  10751. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  10752. (end (or end (progn (outline-next-heading) (point)))))
  10753. (goto-char beg)
  10754. (if (re-search-forward org-property-start-re end t)
  10755. (setq beg (1+ (match-end 0)))
  10756. (if force
  10757. (save-excursion
  10758. (org-insert-property-drawer)
  10759. (setq end (progn (outline-next-heading) (point))))
  10760. (throw 'exit nil))
  10761. (goto-char beg)
  10762. (if (re-search-forward org-property-start-re end t)
  10763. (setq beg (1+ (match-end 0)))))
  10764. (if (re-search-forward org-property-end-re end t)
  10765. (setq end (match-beginning 0))
  10766. (or force (throw 'exit nil))
  10767. (goto-char beg)
  10768. (setq end beg)
  10769. (org-indent-line-function)
  10770. (insert ":END:\n"))
  10771. (cons beg end)))))
  10772. (defun org-entry-properties (&optional pom which)
  10773. "Get all properties of the entry at point-or-marker POM.
  10774. This includes the TODO keyword, the tags, time strings for deadline,
  10775. scheduled, and clocking, and any additional properties defined in the
  10776. entry. The return value is an alist, keys may occur multiple times
  10777. if the property key was used several times.
  10778. POM may also be nil, in which case the current entry is used.
  10779. If WHICH is nil or `all', get all properties. If WHICH is
  10780. `special' or `standard', only get that subclass."
  10781. (setq which (or which 'all))
  10782. (org-with-point-at pom
  10783. (let ((clockstr (substring org-clock-string 0 -1))
  10784. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  10785. beg end range props sum-props key value string clocksum)
  10786. (save-excursion
  10787. (when (condition-case nil
  10788. (and (org-mode-p) (org-back-to-heading t))
  10789. (error nil))
  10790. (setq beg (point))
  10791. (setq sum-props (get-text-property (point) 'org-summaries))
  10792. (setq clocksum (get-text-property (point) :org-clock-minutes))
  10793. (outline-next-heading)
  10794. (setq end (point))
  10795. (when (memq which '(all special))
  10796. ;; Get the special properties, like TODO and tags
  10797. (goto-char beg)
  10798. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  10799. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  10800. (when (looking-at org-priority-regexp)
  10801. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  10802. (when (and (setq value (org-get-tags-string))
  10803. (string-match "\\S-" value))
  10804. (push (cons "TAGS" value) props))
  10805. (when (setq value (org-get-tags-at))
  10806. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  10807. props))
  10808. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  10809. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  10810. string (if (equal key clockstr)
  10811. (org-no-properties
  10812. (org-trim
  10813. (buffer-substring
  10814. (match-beginning 3) (goto-char (point-at-eol)))))
  10815. (substring (org-match-string-no-properties 3) 1 -1)))
  10816. (unless key
  10817. (if (= (char-after (match-beginning 3)) ?\[)
  10818. (setq key "TIMESTAMP_IA")
  10819. (setq key "TIMESTAMP")))
  10820. (when (or (equal key clockstr) (not (assoc key props)))
  10821. (push (cons key string) props)))
  10822. )
  10823. (when (memq which '(all standard))
  10824. ;; Get the standard properties, like :PROP: ...
  10825. (setq range (org-get-property-block beg end))
  10826. (when range
  10827. (goto-char (car range))
  10828. (while (re-search-forward
  10829. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  10830. (cdr range) t)
  10831. (setq key (org-match-string-no-properties 1)
  10832. value (org-trim (or (org-match-string-no-properties 2) "")))
  10833. (unless (member key excluded)
  10834. (push (cons key (or value "")) props)))))
  10835. (if clocksum
  10836. (push (cons "CLOCKSUM"
  10837. (org-columns-number-to-string (/ (float clocksum) 60.)
  10838. 'add_times))
  10839. props))
  10840. (unless (assoc "CATEGORY" props)
  10841. (setq value (or (org-get-category)
  10842. (progn (org-refresh-category-properties)
  10843. (org-get-category))))
  10844. (push (cons "CATEGORY" value) props))
  10845. (append sum-props (nreverse props)))))))
  10846. (defun org-entry-get (pom property &optional inherit)
  10847. "Get value of PROPERTY for entry at point-or-marker POM.
  10848. If INHERIT is non-nil and the entry does not have the property,
  10849. then also check higher levels of the hierarchy.
  10850. If INHERIT is the symbol `selective', use inheritance only if the setting
  10851. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  10852. If the property is present but empty, the return value is the empty string.
  10853. If the property is not present at all, nil is returned."
  10854. (org-with-point-at pom
  10855. (if (and inherit (if (eq inherit 'selective)
  10856. (org-property-inherit-p property)
  10857. t))
  10858. (org-entry-get-with-inheritance property)
  10859. (if (member property org-special-properties)
  10860. ;; We need a special property. Use brute force, get all properties.
  10861. (cdr (assoc property (org-entry-properties nil 'special)))
  10862. (let ((range (org-get-property-block)))
  10863. (if (and range
  10864. (goto-char (car range))
  10865. (re-search-forward
  10866. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  10867. (cdr range) t))
  10868. ;; Found the property, return it.
  10869. (if (match-end 1)
  10870. (org-match-string-no-properties 1)
  10871. "")))))))
  10872. (defun org-property-or-variable-value (var &optional inherit)
  10873. "Check if there is a property fixing the value of VAR.
  10874. If yes, return this value. If not, return the current value of the variable."
  10875. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  10876. (if (and prop (stringp prop) (string-match "\\S-" prop))
  10877. (read prop)
  10878. (symbol-value var))))
  10879. (defun org-entry-delete (pom property)
  10880. "Delete the property PROPERTY from entry at point-or-marker POM."
  10881. (org-with-point-at pom
  10882. (if (member property org-special-properties)
  10883. nil ; cannot delete these properties.
  10884. (let ((range (org-get-property-block)))
  10885. (if (and range
  10886. (goto-char (car range))
  10887. (re-search-forward
  10888. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  10889. (cdr range) t))
  10890. (progn
  10891. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  10892. t)
  10893. nil)))))
  10894. ;; Multi-values properties are properties that contain multiple values
  10895. ;; These values are assumed to be single words, separated by whitespace.
  10896. (defun org-entry-add-to-multivalued-property (pom property value)
  10897. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  10898. (let* ((old (org-entry-get pom property))
  10899. (values (and old (org-split-string old "[ \t]"))))
  10900. (setq value (org-entry-protect-space value))
  10901. (unless (member value values)
  10902. (setq values (cons value values))
  10903. (org-entry-put pom property
  10904. (mapconcat 'identity values " ")))))
  10905. (defun org-entry-remove-from-multivalued-property (pom property value)
  10906. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  10907. (let* ((old (org-entry-get pom property))
  10908. (values (and old (org-split-string old "[ \t]"))))
  10909. (setq value (org-entry-protect-space value))
  10910. (when (member value values)
  10911. (setq values (delete value values))
  10912. (org-entry-put pom property
  10913. (mapconcat 'identity values " ")))))
  10914. (defun org-entry-member-in-multivalued-property (pom property value)
  10915. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  10916. (let* ((old (org-entry-get pom property))
  10917. (values (and old (org-split-string old "[ \t]"))))
  10918. (setq value (org-entry-protect-space value))
  10919. (member value values)))
  10920. (defun org-entry-get-multivalued-property (pom property)
  10921. "Return a list of values in a multivalued property."
  10922. (let* ((value (org-entry-get pom property))
  10923. (values (and value (org-split-string value "[ \t]"))))
  10924. (mapcar 'org-entry-restore-space values)))
  10925. (defun org-entry-put-multivalued-property (pom property &rest values)
  10926. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  10927. VALUES should be a list of strings. Spaces will be protected."
  10928. (org-entry-put pom property
  10929. (mapconcat 'org-entry-protect-space values " "))
  10930. (let* ((value (org-entry-get pom property))
  10931. (values (and value (org-split-string value "[ \t]"))))
  10932. (mapcar 'org-entry-restore-space values)))
  10933. (defun org-entry-protect-space (s)
  10934. "Protect spaces and newline in string S."
  10935. (while (string-match " " s)
  10936. (setq s (replace-match "%20" t t s)))
  10937. (while (string-match "\n" s)
  10938. (setq s (replace-match "%0A" t t s)))
  10939. s)
  10940. (defun org-entry-restore-space (s)
  10941. "Restore spaces and newline in string S."
  10942. (while (string-match "%20" s)
  10943. (setq s (replace-match " " t t s)))
  10944. (while (string-match "%0A" s)
  10945. (setq s (replace-match "\n" t t s)))
  10946. s)
  10947. (defvar org-entry-property-inherited-from (make-marker)
  10948. "Marker pointing to the entry from where a property was inherited.
  10949. Each call to `org-entry-get-with-inheritance' will set this marker to the
  10950. location of the entry where the inheritance search matched. If there was
  10951. no match, the marker will point nowhere.
  10952. Note that also `org-entry-get' calls this function, if the INHERIT flag
  10953. is set.")
  10954. (defun org-entry-get-with-inheritance (property)
  10955. "Get entry property, and search higher levels if not present."
  10956. (move-marker org-entry-property-inherited-from nil)
  10957. (let (tmp)
  10958. (save-excursion
  10959. (save-restriction
  10960. (widen)
  10961. (catch 'ex
  10962. (while t
  10963. (when (setq tmp (org-entry-get nil property))
  10964. (org-back-to-heading t)
  10965. (move-marker org-entry-property-inherited-from (point))
  10966. (throw 'ex tmp))
  10967. (or (org-up-heading-safe) (throw 'ex nil)))))
  10968. (or tmp
  10969. (cdr (assoc property org-file-properties))
  10970. (cdr (assoc property org-global-properties))
  10971. (cdr (assoc property org-global-properties-fixed))))))
  10972. (defun org-entry-put (pom property value)
  10973. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  10974. (org-with-point-at pom
  10975. (org-back-to-heading t)
  10976. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10977. range)
  10978. (cond
  10979. ((equal property "TODO")
  10980. (when (and (stringp value) (string-match "\\S-" value)
  10981. (not (member value org-todo-keywords-1)))
  10982. (error "\"%s\" is not a valid TODO state" value))
  10983. (if (or (not value)
  10984. (not (string-match "\\S-" value)))
  10985. (setq value 'none))
  10986. (org-todo value)
  10987. (org-set-tags nil 'align))
  10988. ((equal property "PRIORITY")
  10989. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10990. (string-to-char value) ?\ ))
  10991. (org-set-tags nil 'align))
  10992. ((equal property "SCHEDULED")
  10993. (if (re-search-forward org-scheduled-time-regexp end t)
  10994. (cond
  10995. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10996. ((eq value 'later) (org-timestamp-change 1 'day))
  10997. (t (call-interactively 'org-schedule)))
  10998. (call-interactively 'org-schedule)))
  10999. ((equal property "DEADLINE")
  11000. (if (re-search-forward org-deadline-time-regexp end t)
  11001. (cond
  11002. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11003. ((eq value 'later) (org-timestamp-change 1 'day))
  11004. (t (call-interactively 'org-deadline)))
  11005. (call-interactively 'org-deadline)))
  11006. ((member property org-special-properties)
  11007. (error "The %s property can not yet be set with `org-entry-put'"
  11008. property))
  11009. (t ; a non-special property
  11010. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11011. (setq range (org-get-property-block beg end 'force))
  11012. (goto-char (car range))
  11013. (if (re-search-forward
  11014. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11015. (progn
  11016. (delete-region (match-beginning 1) (match-end 1))
  11017. (goto-char (match-beginning 1)))
  11018. (goto-char (cdr range))
  11019. (insert "\n")
  11020. (backward-char 1)
  11021. (org-indent-line-function)
  11022. (insert ":" property ":"))
  11023. (and value (insert " " value))
  11024. (org-indent-line-function)))))))
  11025. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11026. "Get all property keys in the current buffer.
  11027. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11028. like tags and TODO state.
  11029. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11030. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11031. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11032. formats in the current buffer."
  11033. (let (rtn range cfmt s p)
  11034. (save-excursion
  11035. (save-restriction
  11036. (widen)
  11037. (goto-char (point-min))
  11038. (while (re-search-forward org-property-start-re nil t)
  11039. (setq range (org-get-property-block))
  11040. (goto-char (car range))
  11041. (while (re-search-forward
  11042. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11043. (cdr range) t)
  11044. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11045. (outline-next-heading))))
  11046. (when include-specials
  11047. (setq rtn (append org-special-properties rtn)))
  11048. (when include-defaults
  11049. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11050. (add-to-list 'rtn org-effort-property))
  11051. (when include-columns
  11052. (save-excursion
  11053. (save-restriction
  11054. (widen)
  11055. (goto-char (point-min))
  11056. (while (re-search-forward
  11057. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11058. nil t)
  11059. (setq cfmt (match-string 2) s 0)
  11060. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11061. cfmt s)
  11062. (setq s (match-end 0)
  11063. p (match-string 1 cfmt))
  11064. (unless (or (equal p "ITEM")
  11065. (member p org-special-properties))
  11066. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11067. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11068. (defun org-property-values (key)
  11069. "Return a list of all values of property KEY."
  11070. (save-excursion
  11071. (save-restriction
  11072. (widen)
  11073. (goto-char (point-min))
  11074. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11075. values)
  11076. (while (re-search-forward re nil t)
  11077. (add-to-list 'values (org-trim (match-string 1))))
  11078. (delete "" values)))))
  11079. (defun org-insert-property-drawer ()
  11080. "Insert a property drawer into the current entry."
  11081. (interactive)
  11082. (org-back-to-heading t)
  11083. (looking-at outline-regexp)
  11084. (let ((indent (if org-adapt-indentation
  11085. (- (match-end 0)(match-beginning 0))
  11086. 0))
  11087. (beg (point))
  11088. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11089. end hiddenp)
  11090. (outline-next-heading)
  11091. (setq end (point))
  11092. (goto-char beg)
  11093. (while (re-search-forward re end t))
  11094. (setq hiddenp (org-invisible-p))
  11095. (end-of-line 1)
  11096. (and (equal (char-after) ?\n) (forward-char 1))
  11097. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11098. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11099. ;; just skip this line
  11100. (beginning-of-line 2)
  11101. ;; Drawer start, find the end
  11102. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11103. (beginning-of-line 1)))
  11104. (org-skip-over-state-notes)
  11105. (skip-chars-backward " \t\n\r")
  11106. (if (eq (char-before) ?*) (forward-char 1))
  11107. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11108. (beginning-of-line 0)
  11109. (org-indent-to-column indent)
  11110. (beginning-of-line 2)
  11111. (org-indent-to-column indent)
  11112. (beginning-of-line 0)
  11113. (if hiddenp
  11114. (save-excursion
  11115. (org-back-to-heading t)
  11116. (hide-entry))
  11117. (org-flag-drawer t))))
  11118. (defun org-set-property (property value)
  11119. "In the current entry, set PROPERTY to VALUE.
  11120. When called interactively, this will prompt for a property name, offering
  11121. completion on existing and default properties. And then it will prompt
  11122. for a value, offering completion either on allowed values (via an inherited
  11123. xxx_ALL property) or on existing values in other instances of this property
  11124. in the current file."
  11125. (interactive
  11126. (let* ((completion-ignore-case t)
  11127. (keys (org-buffer-property-keys nil t t))
  11128. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  11129. (prop (if (member prop0 keys)
  11130. prop0
  11131. (or (cdr (assoc (downcase prop0)
  11132. (mapcar (lambda (x) (cons (downcase x) x))
  11133. keys)))
  11134. prop0)))
  11135. (cur (org-entry-get nil prop))
  11136. (allowed (org-property-get-allowed-values nil prop 'table))
  11137. (existing (mapcar 'list (org-property-values prop)))
  11138. (val (if allowed
  11139. (org-completing-read "Value: " allowed nil 'req-match)
  11140. (let (org-completion-use-ido org-completion-use-iswitchb)
  11141. (org-completing-read
  11142. (concat "Value " (if (and cur (string-match "\\S-" cur))
  11143. (concat "[" cur "]") "")
  11144. ": ")
  11145. existing nil nil "" nil cur)))))
  11146. (list prop (if (equal val "") cur val))))
  11147. (unless (equal (org-entry-get nil property) value)
  11148. (org-entry-put nil property value)))
  11149. (defun org-delete-property (property)
  11150. "In the current entry, delete PROPERTY."
  11151. (interactive
  11152. (let* ((completion-ignore-case t)
  11153. (prop (org-icompleting-read
  11154. "Property: " (org-entry-properties nil 'standard))))
  11155. (list prop)))
  11156. (message "Property %s %s" property
  11157. (if (org-entry-delete nil property)
  11158. "deleted"
  11159. "was not present in the entry")))
  11160. (defun org-delete-property-globally (property)
  11161. "Remove PROPERTY globally, from all entries."
  11162. (interactive
  11163. (let* ((completion-ignore-case t)
  11164. (prop (org-icompleting-read
  11165. "Globally remove property: "
  11166. (mapcar 'list (org-buffer-property-keys)))))
  11167. (list prop)))
  11168. (save-excursion
  11169. (save-restriction
  11170. (widen)
  11171. (goto-char (point-min))
  11172. (let ((cnt 0))
  11173. (while (re-search-forward
  11174. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  11175. nil t)
  11176. (setq cnt (1+ cnt))
  11177. (replace-match ""))
  11178. (message "Property \"%s\" removed from %d entries" property cnt)))))
  11179. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  11180. (defun org-compute-property-at-point ()
  11181. "Compute the property at point.
  11182. This looks for an enclosing column format, extracts the operator and
  11183. then applies it to the property in the column format's scope."
  11184. (interactive)
  11185. (unless (org-at-property-p)
  11186. (error "Not at a property"))
  11187. (let ((prop (org-match-string-no-properties 2)))
  11188. (org-columns-get-format-and-top-level)
  11189. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  11190. (error "No operator defined for property %s" prop))
  11191. (org-columns-compute prop)))
  11192. (defun org-property-get-allowed-values (pom property &optional table)
  11193. "Get allowed values for the property PROPERTY.
  11194. When TABLE is non-nil, return an alist that can directly be used for
  11195. completion."
  11196. (let (vals)
  11197. (cond
  11198. ((equal property "TODO")
  11199. (setq vals (org-with-point-at pom
  11200. (append org-todo-keywords-1 '("")))))
  11201. ((equal property "PRIORITY")
  11202. (let ((n org-lowest-priority))
  11203. (while (>= n org-highest-priority)
  11204. (push (char-to-string n) vals)
  11205. (setq n (1- n)))))
  11206. ((member property org-special-properties))
  11207. (t
  11208. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  11209. (when (and vals (string-match "\\S-" vals))
  11210. (setq vals (car (read-from-string (concat "(" vals ")"))))
  11211. (setq vals (mapcar (lambda (x)
  11212. (cond ((stringp x) x)
  11213. ((numberp x) (number-to-string x))
  11214. ((symbolp x) (symbol-name x))
  11215. (t "???")))
  11216. vals)))))
  11217. (if table (mapcar 'list vals) vals)))
  11218. (defun org-property-previous-allowed-value (&optional previous)
  11219. "Switch to the next allowed value for this property."
  11220. (interactive)
  11221. (org-property-next-allowed-value t))
  11222. (defun org-property-next-allowed-value (&optional previous)
  11223. "Switch to the next allowed value for this property."
  11224. (interactive)
  11225. (unless (org-at-property-p)
  11226. (error "Not at a property"))
  11227. (let* ((key (match-string 2))
  11228. (value (match-string 3))
  11229. (allowed (or (org-property-get-allowed-values (point) key)
  11230. (and (member value '("[ ]" "[-]" "[X]"))
  11231. '("[ ]" "[X]"))))
  11232. nval)
  11233. (unless allowed
  11234. (error "Allowed values for this property have not been defined"))
  11235. (if previous (setq allowed (reverse allowed)))
  11236. (if (member value allowed)
  11237. (setq nval (car (cdr (member value allowed)))))
  11238. (setq nval (or nval (car allowed)))
  11239. (if (equal nval value)
  11240. (error "Only one allowed value for this property"))
  11241. (org-at-property-p)
  11242. (replace-match (concat " :" key ": " nval) t t)
  11243. (org-indent-line-function)
  11244. (beginning-of-line 1)
  11245. (skip-chars-forward " \t")))
  11246. (defun org-find-entry-with-id (ident)
  11247. "Locate the entry that contains the ID property with exact value IDENT.
  11248. IDENT can be a string, a symbol or a number, this function will search for
  11249. the string representation of it.
  11250. Return the position where this entry starts, or nil if there is no such entry."
  11251. (interactive "sID: ")
  11252. (let ((id (cond
  11253. ((stringp ident) ident)
  11254. ((symbol-name ident) (symbol-name ident))
  11255. ((numberp ident) (number-to-string ident))
  11256. (t (error "IDENT %s must be a string, symbol or number" ident))))
  11257. (case-fold-search nil))
  11258. (save-excursion
  11259. (save-restriction
  11260. (widen)
  11261. (goto-char (point-min))
  11262. (when (re-search-forward
  11263. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  11264. nil t)
  11265. (org-back-to-heading t)
  11266. (point))))))
  11267. ;;;; Timestamps
  11268. (defvar org-last-changed-timestamp nil)
  11269. (defvar org-last-inserted-timestamp nil
  11270. "The last time stamp inserted with `org-insert-time-stamp'.")
  11271. (defvar org-time-was-given) ; dynamically scoped parameter
  11272. (defvar org-end-time-was-given) ; dynamically scoped parameter
  11273. (defvar org-ts-what) ; dynamically scoped parameter
  11274. (defun org-time-stamp (arg &optional inactive)
  11275. "Prompt for a date/time and insert a time stamp.
  11276. If the user specifies a time like HH:MM, or if this command is called
  11277. with a prefix argument, the time stamp will contain date and time.
  11278. Otherwise, only the date will be included. All parts of a date not
  11279. specified by the user will be filled in from the current date/time.
  11280. So if you press just return without typing anything, the time stamp
  11281. will represent the current date/time. If there is already a timestamp
  11282. at the cursor, it will be modified."
  11283. (interactive "P")
  11284. (let* ((ts nil)
  11285. (default-time
  11286. ;; Default time is either today, or, when entering a range,
  11287. ;; the range start.
  11288. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  11289. (save-excursion
  11290. (re-search-backward
  11291. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  11292. (- (point) 20) t)))
  11293. (apply 'encode-time (org-parse-time-string (match-string 1)))
  11294. (current-time)))
  11295. (default-input (and ts (org-get-compact-tod ts)))
  11296. org-time-was-given org-end-time-was-given time)
  11297. (cond
  11298. ((and (org-at-timestamp-p t)
  11299. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  11300. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  11301. (insert "--")
  11302. (setq time (let ((this-command this-command))
  11303. (org-read-date arg 'totime nil nil
  11304. default-time default-input)))
  11305. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  11306. ((org-at-timestamp-p t)
  11307. (setq time (let ((this-command this-command))
  11308. (org-read-date arg 'totime nil nil default-time default-input)))
  11309. (when (org-at-timestamp-p t) ; just to get the match data
  11310. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  11311. (replace-match "")
  11312. (setq org-last-changed-timestamp
  11313. (org-insert-time-stamp
  11314. time (or org-time-was-given arg)
  11315. inactive nil nil (list org-end-time-was-given))))
  11316. (message "Timestamp updated"))
  11317. (t
  11318. (setq time (let ((this-command this-command))
  11319. (org-read-date arg 'totime nil nil default-time default-input)))
  11320. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  11321. nil nil (list org-end-time-was-given))))))
  11322. ;; FIXME: can we use this for something else, like computing time differences?
  11323. (defun org-get-compact-tod (s)
  11324. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  11325. (let* ((t1 (match-string 1 s))
  11326. (h1 (string-to-number (match-string 2 s)))
  11327. (m1 (string-to-number (match-string 3 s)))
  11328. (t2 (and (match-end 4) (match-string 5 s)))
  11329. (h2 (and t2 (string-to-number (match-string 6 s))))
  11330. (m2 (and t2 (string-to-number (match-string 7 s))))
  11331. dh dm)
  11332. (if (not t2)
  11333. t1
  11334. (setq dh (- h2 h1) dm (- m2 m1))
  11335. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  11336. (concat t1 "+" (number-to-string dh)
  11337. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  11338. (defun org-time-stamp-inactive (&optional arg)
  11339. "Insert an inactive time stamp.
  11340. An inactive time stamp is enclosed in square brackets instead of angle
  11341. brackets. It is inactive in the sense that it does not trigger agenda entries,
  11342. does not link to the calendar and cannot be changed with the S-cursor keys.
  11343. So these are more for recording a certain time/date."
  11344. (interactive "P")
  11345. (org-time-stamp arg 'inactive))
  11346. (defvar org-date-ovl (org-make-overlay 1 1))
  11347. (org-overlay-put org-date-ovl 'face 'org-warning)
  11348. (org-detach-overlay org-date-ovl)
  11349. (defvar org-ans1) ; dynamically scoped parameter
  11350. (defvar org-ans2) ; dynamically scoped parameter
  11351. (defvar org-plain-time-of-day-regexp) ; defined below
  11352. (defvar org-overriding-default-time nil) ; dynamically scoped
  11353. (defvar org-read-date-overlay nil)
  11354. (defvar org-dcst nil) ; dynamically scoped
  11355. (defvar org-read-date-history nil)
  11356. (defvar org-read-date-final-answer nil)
  11357. (defun org-read-date (&optional with-time to-time from-string prompt
  11358. default-time default-input)
  11359. "Read a date, possibly a time, and make things smooth for the user.
  11360. The prompt will suggest to enter an ISO date, but you can also enter anything
  11361. which will at least partially be understood by `parse-time-string'.
  11362. Unrecognized parts of the date will default to the current day, month, year,
  11363. hour and minute. If this command is called to replace a timestamp at point,
  11364. of to enter the second timestamp of a range, the default time is taken from the
  11365. existing stamp. For example,
  11366. 3-2-5 --> 2003-02-05
  11367. feb 15 --> currentyear-02-15
  11368. sep 12 9 --> 2009-09-12
  11369. 12:45 --> today 12:45
  11370. 22 sept 0:34 --> currentyear-09-22 0:34
  11371. 12 --> currentyear-currentmonth-12
  11372. Fri --> nearest Friday (today or later)
  11373. etc.
  11374. Furthermore you can specify a relative date by giving, as the *first* thing
  11375. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  11376. change in days weeks, months, years.
  11377. With a single plus or minus, the date is relative to today. With a double
  11378. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  11379. +4d --> four days from today
  11380. +4 --> same as above
  11381. +2w --> two weeks from today
  11382. ++5 --> five days from default date
  11383. The function understands only English month and weekday abbreviations,
  11384. but this can be configured with the variables `parse-time-months' and
  11385. `parse-time-weekdays'.
  11386. While prompting, a calendar is popped up - you can also select the
  11387. date with the mouse (button 1). The calendar shows a period of three
  11388. months. To scroll it to other months, use the keys `>' and `<'.
  11389. If you don't like the calendar, turn it off with
  11390. \(setq org-read-date-popup-calendar nil)
  11391. With optional argument TO-TIME, the date will immediately be converted
  11392. to an internal time.
  11393. With an optional argument WITH-TIME, the prompt will suggest to also
  11394. insert a time. Note that when WITH-TIME is not set, you can still
  11395. enter a time, and this function will inform the calling routine about
  11396. this change. The calling routine may then choose to change the format
  11397. used to insert the time stamp into the buffer to include the time.
  11398. With optional argument FROM-STRING, read from this string instead from
  11399. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  11400. the time/date that is used for everything that is not specified by the
  11401. user."
  11402. (require 'parse-time)
  11403. (let* ((org-time-stamp-rounding-minutes
  11404. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  11405. (org-dcst org-display-custom-times)
  11406. (ct (org-current-time))
  11407. (def (or org-overriding-default-time default-time ct))
  11408. (defdecode (decode-time def))
  11409. (dummy (progn
  11410. (when (< (nth 2 defdecode) org-extend-today-until)
  11411. (setcar (nthcdr 2 defdecode) -1)
  11412. (setcar (nthcdr 1 defdecode) 59)
  11413. (setq def (apply 'encode-time defdecode)
  11414. defdecode (decode-time def)))))
  11415. (calendar-frame-setup nil)
  11416. (calendar-move-hook nil)
  11417. (calendar-view-diary-initially-flag nil)
  11418. (view-diary-entries-initially nil)
  11419. (calendar-view-holidays-initially-flag nil)
  11420. (view-calendar-holidays-initially nil)
  11421. (timestr (format-time-string
  11422. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  11423. (prompt (concat (if prompt (concat prompt " ") "")
  11424. (format "Date+time [%s]: " timestr)))
  11425. ans (org-ans0 "") org-ans1 org-ans2 final)
  11426. (cond
  11427. (from-string (setq ans from-string))
  11428. (org-read-date-popup-calendar
  11429. (save-excursion
  11430. (save-window-excursion
  11431. (calendar)
  11432. (calendar-forward-day (- (time-to-days def)
  11433. (calendar-absolute-from-gregorian
  11434. (calendar-current-date))))
  11435. (org-eval-in-calendar nil t)
  11436. (let* ((old-map (current-local-map))
  11437. (map (copy-keymap calendar-mode-map))
  11438. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  11439. (org-defkey map (kbd "RET") 'org-calendar-select)
  11440. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  11441. 'org-calendar-select-mouse)
  11442. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  11443. 'org-calendar-select-mouse)
  11444. (org-defkey minibuffer-local-map [(meta shift left)]
  11445. (lambda () (interactive)
  11446. (org-eval-in-calendar '(calendar-backward-month 1))))
  11447. (org-defkey minibuffer-local-map [(meta shift right)]
  11448. (lambda () (interactive)
  11449. (org-eval-in-calendar '(calendar-forward-month 1))))
  11450. (org-defkey minibuffer-local-map [(meta shift up)]
  11451. (lambda () (interactive)
  11452. (org-eval-in-calendar '(calendar-backward-year 1))))
  11453. (org-defkey minibuffer-local-map [(meta shift down)]
  11454. (lambda () (interactive)
  11455. (org-eval-in-calendar '(calendar-forward-year 1))))
  11456. (org-defkey minibuffer-local-map [?\e (shift left)]
  11457. (lambda () (interactive)
  11458. (org-eval-in-calendar '(calendar-backward-month 1))))
  11459. (org-defkey minibuffer-local-map [?\e (shift right)]
  11460. (lambda () (interactive)
  11461. (org-eval-in-calendar '(calendar-forward-month 1))))
  11462. (org-defkey minibuffer-local-map [?\e (shift up)]
  11463. (lambda () (interactive)
  11464. (org-eval-in-calendar '(calendar-backward-year 1))))
  11465. (org-defkey minibuffer-local-map [?\e (shift down)]
  11466. (lambda () (interactive)
  11467. (org-eval-in-calendar '(calendar-forward-year 1))))
  11468. (org-defkey minibuffer-local-map [(shift up)]
  11469. (lambda () (interactive)
  11470. (org-eval-in-calendar '(calendar-backward-week 1))))
  11471. (org-defkey minibuffer-local-map [(shift down)]
  11472. (lambda () (interactive)
  11473. (org-eval-in-calendar '(calendar-forward-week 1))))
  11474. (org-defkey minibuffer-local-map [(shift left)]
  11475. (lambda () (interactive)
  11476. (org-eval-in-calendar '(calendar-backward-day 1))))
  11477. (org-defkey minibuffer-local-map [(shift right)]
  11478. (lambda () (interactive)
  11479. (org-eval-in-calendar '(calendar-forward-day 1))))
  11480. (org-defkey minibuffer-local-map ">"
  11481. (lambda () (interactive)
  11482. (org-eval-in-calendar '(scroll-calendar-left 1))))
  11483. (org-defkey minibuffer-local-map "<"
  11484. (lambda () (interactive)
  11485. (org-eval-in-calendar '(scroll-calendar-right 1))))
  11486. (run-hooks 'org-read-date-minibuffer-setup-hook)
  11487. (unwind-protect
  11488. (progn
  11489. (use-local-map map)
  11490. (add-hook 'post-command-hook 'org-read-date-display)
  11491. (setq org-ans0 (read-string prompt default-input
  11492. 'org-read-date-history nil))
  11493. ;; org-ans0: from prompt
  11494. ;; org-ans1: from mouse click
  11495. ;; org-ans2: from calendar motion
  11496. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  11497. (remove-hook 'post-command-hook 'org-read-date-display)
  11498. (use-local-map old-map)
  11499. (when org-read-date-overlay
  11500. (org-delete-overlay org-read-date-overlay)
  11501. (setq org-read-date-overlay nil)))))))
  11502. (t ; Naked prompt only
  11503. (unwind-protect
  11504. (setq ans (read-string prompt default-input
  11505. 'org-read-date-history timestr))
  11506. (when org-read-date-overlay
  11507. (org-delete-overlay org-read-date-overlay)
  11508. (setq org-read-date-overlay nil)))))
  11509. (setq final (org-read-date-analyze ans def defdecode))
  11510. (setq org-read-date-final-answer ans)
  11511. (if to-time
  11512. (apply 'encode-time final)
  11513. (if (and (boundp 'org-time-was-given) org-time-was-given)
  11514. (format "%04d-%02d-%02d %02d:%02d"
  11515. (nth 5 final) (nth 4 final) (nth 3 final)
  11516. (nth 2 final) (nth 1 final))
  11517. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  11518. (defvar def)
  11519. (defvar defdecode)
  11520. (defvar with-time)
  11521. (defun org-read-date-display ()
  11522. "Display the current date prompt interpretation in the minibuffer."
  11523. (when org-read-date-display-live
  11524. (when org-read-date-overlay
  11525. (org-delete-overlay org-read-date-overlay))
  11526. (let ((p (point)))
  11527. (end-of-line 1)
  11528. (while (not (equal (buffer-substring
  11529. (max (point-min) (- (point) 4)) (point))
  11530. " "))
  11531. (insert " "))
  11532. (goto-char p))
  11533. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  11534. " " (or org-ans1 org-ans2)))
  11535. (org-end-time-was-given nil)
  11536. (f (org-read-date-analyze ans def defdecode))
  11537. (fmts (if org-dcst
  11538. org-time-stamp-custom-formats
  11539. org-time-stamp-formats))
  11540. (fmt (if (or with-time
  11541. (and (boundp 'org-time-was-given) org-time-was-given))
  11542. (cdr fmts)
  11543. (car fmts)))
  11544. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  11545. (when (and org-end-time-was-given
  11546. (string-match org-plain-time-of-day-regexp txt))
  11547. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  11548. org-end-time-was-given
  11549. (substring txt (match-end 0)))))
  11550. (setq org-read-date-overlay
  11551. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  11552. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  11553. (defun org-read-date-analyze (ans def defdecode)
  11554. "Analyse the combined answer of the date prompt."
  11555. ;; FIXME: cleanup and comment
  11556. (let (delta deltan deltaw deltadef year month day
  11557. hour minute second wday pm h2 m2 tl wday1
  11558. iso-year iso-weekday iso-week iso-year iso-date)
  11559. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  11560. (setq ans "+0"))
  11561. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  11562. (setq ans (replace-match "" t t ans)
  11563. deltan (car delta)
  11564. deltaw (nth 1 delta)
  11565. deltadef (nth 2 delta)))
  11566. ;; Check if there is an iso week date in there
  11567. ;; If yes, sore the info and postpone interpreting it until the rest
  11568. ;; of the parsing is done
  11569. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  11570. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  11571. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  11572. iso-week (string-to-number (match-string 2 ans)))
  11573. (setq ans (replace-match "" t t ans)))
  11574. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  11575. (when (string-match
  11576. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  11577. (setq year (if (match-end 2)
  11578. (string-to-number (match-string 2 ans))
  11579. (string-to-number (format-time-string "%Y")))
  11580. month (string-to-number (match-string 3 ans))
  11581. day (string-to-number (match-string 4 ans)))
  11582. (if (< year 100) (setq year (+ 2000 year)))
  11583. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  11584. t nil ans)))
  11585. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  11586. ;; If there is a time with am/pm, and *no* time without it, we convert
  11587. ;; so that matching will be successful.
  11588. (loop for i from 1 to 2 do ; twice, for end time as well
  11589. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  11590. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  11591. (setq hour (string-to-number (match-string 1 ans))
  11592. minute (if (match-end 3)
  11593. (string-to-number (match-string 3 ans))
  11594. 0)
  11595. pm (equal ?p
  11596. (string-to-char (downcase (match-string 4 ans)))))
  11597. (if (and (= hour 12) (not pm))
  11598. (setq hour 0)
  11599. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  11600. (setq ans (replace-match (format "%02d:%02d" hour minute)
  11601. t t ans))))
  11602. ;; Check if a time range is given as a duration
  11603. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  11604. (setq hour (string-to-number (match-string 1 ans))
  11605. h2 (+ hour (string-to-number (match-string 3 ans)))
  11606. minute (string-to-number (match-string 2 ans))
  11607. m2 (+ minute (if (match-end 5) (string-to-number
  11608. (match-string 5 ans))0)))
  11609. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  11610. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  11611. t t ans)))
  11612. ;; Check if there is a time range
  11613. (when (boundp 'org-end-time-was-given)
  11614. (setq org-time-was-given nil)
  11615. (when (and (string-match org-plain-time-of-day-regexp ans)
  11616. (match-end 8))
  11617. (setq org-end-time-was-given (match-string 8 ans))
  11618. (setq ans (concat (substring ans 0 (match-beginning 7))
  11619. (substring ans (match-end 7))))))
  11620. (setq tl (parse-time-string ans)
  11621. day (or (nth 3 tl) (nth 3 defdecode))
  11622. month (or (nth 4 tl)
  11623. (if (and org-read-date-prefer-future
  11624. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  11625. (1+ (nth 4 defdecode))
  11626. (nth 4 defdecode)))
  11627. year (or (nth 5 tl)
  11628. (if (and org-read-date-prefer-future
  11629. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  11630. (1+ (nth 5 defdecode))
  11631. (nth 5 defdecode)))
  11632. hour (or (nth 2 tl) (nth 2 defdecode))
  11633. minute (or (nth 1 tl) (nth 1 defdecode))
  11634. second (or (nth 0 tl) 0)
  11635. wday (nth 6 tl))
  11636. ;; Special date definitions below
  11637. (cond
  11638. (iso-week
  11639. ;; There was an iso week
  11640. (setq year (or iso-year year)
  11641. day (or iso-weekday wday 1)
  11642. wday nil ; to make sure that the trigger below does not match
  11643. iso-date (calendar-gregorian-from-absolute
  11644. (calendar-absolute-from-iso
  11645. (list iso-week day year))))
  11646. ; FIXME: Should we also push ISO weeks into the future?
  11647. ; (when (and org-read-date-prefer-future
  11648. ; (not iso-year)
  11649. ; (< (calendar-absolute-from-gregorian iso-date)
  11650. ; (time-to-days (current-time))))
  11651. ; (setq year (1+ year)
  11652. ; iso-date (calendar-gregorian-from-absolute
  11653. ; (calendar-absolute-from-iso
  11654. ; (list iso-week day year)))))
  11655. (setq month (car iso-date)
  11656. year (nth 2 iso-date)
  11657. day (nth 1 iso-date)))
  11658. (deltan
  11659. (unless deltadef
  11660. (let ((now (decode-time (current-time))))
  11661. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  11662. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  11663. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  11664. ((equal deltaw "m") (setq month (+ month deltan)))
  11665. ((equal deltaw "y") (setq year (+ year deltan)))))
  11666. ((and wday (not (nth 3 tl)))
  11667. ;; Weekday was given, but no day, so pick that day in the week
  11668. ;; on or after the derived date.
  11669. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  11670. (unless (equal wday wday1)
  11671. (setq day (+ day (% (- wday wday1 -7) 7))))))
  11672. (if (and (boundp 'org-time-was-given)
  11673. (nth 2 tl))
  11674. (setq org-time-was-given t))
  11675. (if (< year 100) (setq year (+ 2000 year)))
  11676. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  11677. (list second minute hour day month year)))
  11678. (defvar parse-time-weekdays)
  11679. (defun org-read-date-get-relative (s today default)
  11680. "Check string S for special relative date string.
  11681. TODAY and DEFAULT are internal times, for today and for a default.
  11682. Return shift list (N what def-flag)
  11683. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  11684. N is the number of WHATs to shift.
  11685. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  11686. the DEFAULT date rather than TODAY."
  11687. (when (and
  11688. (string-match
  11689. (concat
  11690. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  11691. "\\([0-9]+\\)?"
  11692. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  11693. "\\([ \t]\\|$\\)") s)
  11694. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  11695. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  11696. (string-to-char (substring (match-string 1 s) -1))
  11697. ?+))
  11698. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  11699. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  11700. (what (if (match-end 3) (match-string 3 s) "d"))
  11701. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  11702. (date (if rel default today))
  11703. (wday (nth 6 (decode-time date)))
  11704. delta)
  11705. (if wday1
  11706. (progn
  11707. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  11708. (if (= dir ?-) (setq delta (- delta 7)))
  11709. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  11710. (list delta "d" rel))
  11711. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  11712. (defun org-eval-in-calendar (form &optional keepdate)
  11713. "Eval FORM in the calendar window and return to current window.
  11714. Also, store the cursor date in variable org-ans2."
  11715. (let ((sf (selected-frame))
  11716. (sw (selected-window)))
  11717. (select-window (get-buffer-window "*Calendar*" t))
  11718. (eval form)
  11719. (when (and (not keepdate) (calendar-cursor-to-date))
  11720. (let* ((date (calendar-cursor-to-date))
  11721. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11722. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  11723. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  11724. (select-window sw)
  11725. (org-select-frame-set-input-focus sf)))
  11726. (defun org-calendar-select ()
  11727. "Return to `org-read-date' with the date currently selected.
  11728. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11729. (interactive)
  11730. (when (calendar-cursor-to-date)
  11731. (let* ((date (calendar-cursor-to-date))
  11732. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11733. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11734. (if (active-minibuffer-window) (exit-minibuffer))))
  11735. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  11736. "Insert a date stamp for the date given by the internal TIME.
  11737. WITH-HM means, use the stamp format that includes the time of the day.
  11738. INACTIVE means use square brackets instead of angular ones, so that the
  11739. stamp will not contribute to the agenda.
  11740. PRE and POST are optional strings to be inserted before and after the
  11741. stamp.
  11742. The command returns the inserted time stamp."
  11743. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  11744. stamp)
  11745. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  11746. (insert-before-markers (or pre ""))
  11747. (insert-before-markers (setq stamp (format-time-string fmt time)))
  11748. (when (listp extra)
  11749. (setq extra (car extra))
  11750. (if (and (stringp extra)
  11751. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  11752. (setq extra (format "-%02d:%02d"
  11753. (string-to-number (match-string 1 extra))
  11754. (string-to-number (match-string 2 extra))))
  11755. (setq extra nil)))
  11756. (when extra
  11757. (backward-char 1)
  11758. (insert-before-markers extra)
  11759. (forward-char 1))
  11760. (insert-before-markers (or post ""))
  11761. (setq org-last-inserted-timestamp stamp)))
  11762. (defun org-toggle-time-stamp-overlays ()
  11763. "Toggle the use of custom time stamp formats."
  11764. (interactive)
  11765. (setq org-display-custom-times (not org-display-custom-times))
  11766. (unless org-display-custom-times
  11767. (let ((p (point-min)) (bmp (buffer-modified-p)))
  11768. (while (setq p (next-single-property-change p 'display))
  11769. (if (and (get-text-property p 'display)
  11770. (eq (get-text-property p 'face) 'org-date))
  11771. (remove-text-properties
  11772. p (setq p (next-single-property-change p 'display))
  11773. '(display t))))
  11774. (set-buffer-modified-p bmp)))
  11775. (if (featurep 'xemacs)
  11776. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  11777. (org-restart-font-lock)
  11778. (setq org-table-may-need-update t)
  11779. (if org-display-custom-times
  11780. (message "Time stamps are overlayed with custom format")
  11781. (message "Time stamp overlays removed")))
  11782. (defun org-display-custom-time (beg end)
  11783. "Overlay modified time stamp format over timestamp between BEG and END."
  11784. (let* ((ts (buffer-substring beg end))
  11785. t1 w1 with-hm tf time str w2 (off 0))
  11786. (save-match-data
  11787. (setq t1 (org-parse-time-string ts t))
  11788. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  11789. (setq off (- (match-end 0) (match-beginning 0)))))
  11790. (setq end (- end off))
  11791. (setq w1 (- end beg)
  11792. with-hm (and (nth 1 t1) (nth 2 t1))
  11793. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  11794. time (org-fix-decoded-time t1)
  11795. str (org-add-props
  11796. (format-time-string
  11797. (substring tf 1 -1) (apply 'encode-time time))
  11798. nil 'mouse-face 'highlight)
  11799. w2 (length str))
  11800. (if (not (= w2 w1))
  11801. (add-text-properties (1+ beg) (+ 2 beg)
  11802. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  11803. (if (featurep 'xemacs)
  11804. (progn
  11805. (put-text-property beg end 'invisible t)
  11806. (put-text-property beg end 'end-glyph (make-glyph str)))
  11807. (put-text-property beg end 'display str))))
  11808. (defun org-translate-time (string)
  11809. "Translate all timestamps in STRING to custom format.
  11810. But do this only if the variable `org-display-custom-times' is set."
  11811. (when org-display-custom-times
  11812. (save-match-data
  11813. (let* ((start 0)
  11814. (re org-ts-regexp-both)
  11815. t1 with-hm inactive tf time str beg end)
  11816. (while (setq start (string-match re string start))
  11817. (setq beg (match-beginning 0)
  11818. end (match-end 0)
  11819. t1 (save-match-data
  11820. (org-parse-time-string (substring string beg end) t))
  11821. with-hm (and (nth 1 t1) (nth 2 t1))
  11822. inactive (equal (substring string beg (1+ beg)) "[")
  11823. tf (funcall (if with-hm 'cdr 'car)
  11824. org-time-stamp-custom-formats)
  11825. time (org-fix-decoded-time t1)
  11826. str (format-time-string
  11827. (concat
  11828. (if inactive "[" "<") (substring tf 1 -1)
  11829. (if inactive "]" ">"))
  11830. (apply 'encode-time time))
  11831. string (replace-match str t t string)
  11832. start (+ start (length str)))))))
  11833. string)
  11834. (defun org-fix-decoded-time (time)
  11835. "Set 0 instead of nil for the first 6 elements of time.
  11836. Don't touch the rest."
  11837. (let ((n 0))
  11838. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  11839. (defun org-days-to-time (timestamp-string)
  11840. "Difference between TIMESTAMP-STRING and now in days."
  11841. (- (time-to-days (org-time-string-to-time timestamp-string))
  11842. (time-to-days (current-time))))
  11843. (defun org-deadline-close (timestamp-string &optional ndays)
  11844. "Is the time in TIMESTAMP-STRING close to the current date?"
  11845. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  11846. (and (< (org-days-to-time timestamp-string) ndays)
  11847. (not (org-entry-is-done-p))))
  11848. (defun org-get-wdays (ts)
  11849. "Get the deadline lead time appropriate for timestring TS."
  11850. (cond
  11851. ((<= org-deadline-warning-days 0)
  11852. ;; 0 or negative, enforce this value no matter what
  11853. (- org-deadline-warning-days))
  11854. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  11855. ;; lead time is specified.
  11856. (floor (* (string-to-number (match-string 1 ts))
  11857. (cdr (assoc (match-string 2 ts)
  11858. '(("d" . 1) ("w" . 7)
  11859. ("m" . 30.4) ("y" . 365.25)))))))
  11860. ;; go for the default.
  11861. (t org-deadline-warning-days)))
  11862. (defun org-calendar-select-mouse (ev)
  11863. "Return to `org-read-date' with the date currently selected.
  11864. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  11865. (interactive "e")
  11866. (mouse-set-point ev)
  11867. (when (calendar-cursor-to-date)
  11868. (let* ((date (calendar-cursor-to-date))
  11869. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  11870. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  11871. (if (active-minibuffer-window) (exit-minibuffer))))
  11872. (defun org-check-deadlines (ndays)
  11873. "Check if there are any deadlines due or past due.
  11874. A deadline is considered due if it happens within `org-deadline-warning-days'
  11875. days from today's date. If the deadline appears in an entry marked DONE,
  11876. it is not shown. The prefix arg NDAYS can be used to test that many
  11877. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  11878. (interactive "P")
  11879. (let* ((org-warn-days
  11880. (cond
  11881. ((equal ndays '(4)) 100000)
  11882. (ndays (prefix-numeric-value ndays))
  11883. (t (abs org-deadline-warning-days))))
  11884. (case-fold-search nil)
  11885. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  11886. (callback
  11887. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  11888. (message "%d deadlines past-due or due within %d days"
  11889. (org-occur regexp nil callback)
  11890. org-warn-days)))
  11891. (defun org-check-before-date (date)
  11892. "Check if there are deadlines or scheduled entries before DATE."
  11893. (interactive (list (org-read-date)))
  11894. (let ((case-fold-search nil)
  11895. (regexp (concat "\\<\\(" org-deadline-string
  11896. "\\|" org-scheduled-string
  11897. "\\) *<\\([^>]+\\)>"))
  11898. (callback
  11899. (lambda () (time-less-p
  11900. (org-time-string-to-time (match-string 2))
  11901. (org-time-string-to-time date)))))
  11902. (message "%d entries before %s"
  11903. (org-occur regexp nil callback) date)))
  11904. (defun org-check-after-date (date)
  11905. "Check if there are deadlines or scheduled entries after DATE."
  11906. (interactive (list (org-read-date)))
  11907. (let ((case-fold-search nil)
  11908. (regexp (concat "\\<\\(" org-deadline-string
  11909. "\\|" org-scheduled-string
  11910. "\\) *<\\([^>]+\\)>"))
  11911. (callback
  11912. (lambda () (not
  11913. (time-less-p
  11914. (org-time-string-to-time (match-string 2))
  11915. (org-time-string-to-time date))))))
  11916. (message "%d entries after %s"
  11917. (org-occur regexp nil callback) date)))
  11918. (defun org-evaluate-time-range (&optional to-buffer)
  11919. "Evaluate a time range by computing the difference between start and end.
  11920. Normally the result is just printed in the echo area, but with prefix arg
  11921. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  11922. If the time range is actually in a table, the result is inserted into the
  11923. next column.
  11924. For time difference computation, a year is assumed to be exactly 365
  11925. days in order to avoid rounding problems."
  11926. (interactive "P")
  11927. (or
  11928. (org-clock-update-time-maybe)
  11929. (save-excursion
  11930. (unless (org-at-date-range-p t)
  11931. (goto-char (point-at-bol))
  11932. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  11933. (if (not (org-at-date-range-p t))
  11934. (error "Not at a time-stamp range, and none found in current line")))
  11935. (let* ((ts1 (match-string 1))
  11936. (ts2 (match-string 2))
  11937. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  11938. (match-end (match-end 0))
  11939. (time1 (org-time-string-to-time ts1))
  11940. (time2 (org-time-string-to-time ts2))
  11941. (t1 (org-float-time time1))
  11942. (t2 (org-float-time time2))
  11943. (diff (abs (- t2 t1)))
  11944. (negative (< (- t2 t1) 0))
  11945. ;; (ys (floor (* 365 24 60 60)))
  11946. (ds (* 24 60 60))
  11947. (hs (* 60 60))
  11948. (fy "%dy %dd %02d:%02d")
  11949. (fy1 "%dy %dd")
  11950. (fd "%dd %02d:%02d")
  11951. (fd1 "%dd")
  11952. (fh "%02d:%02d")
  11953. y d h m align)
  11954. (if havetime
  11955. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11956. y 0
  11957. d (floor (/ diff ds)) diff (mod diff ds)
  11958. h (floor (/ diff hs)) diff (mod diff hs)
  11959. m (floor (/ diff 60)))
  11960. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  11961. y 0
  11962. d (floor (+ (/ diff ds) 0.5))
  11963. h 0 m 0))
  11964. (if (not to-buffer)
  11965. (message "%s" (org-make-tdiff-string y d h m))
  11966. (if (org-at-table-p)
  11967. (progn
  11968. (goto-char match-end)
  11969. (setq align t)
  11970. (and (looking-at " *|") (goto-char (match-end 0))))
  11971. (goto-char match-end))
  11972. (if (looking-at
  11973. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  11974. (replace-match ""))
  11975. (if negative (insert " -"))
  11976. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  11977. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  11978. (insert " " (format fh h m))))
  11979. (if align (org-table-align))
  11980. (message "Time difference inserted")))))
  11981. (defun org-make-tdiff-string (y d h m)
  11982. (let ((fmt "")
  11983. (l nil))
  11984. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  11985. l (push y l)))
  11986. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  11987. l (push d l)))
  11988. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  11989. l (push h l)))
  11990. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  11991. l (push m l)))
  11992. (apply 'format fmt (nreverse l))))
  11993. (defun org-time-string-to-time (s)
  11994. (apply 'encode-time (org-parse-time-string s)))
  11995. (defun org-time-string-to-seconds (s)
  11996. (org-float-time (org-time-string-to-time s)))
  11997. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  11998. "Convert a time stamp to an absolute day number.
  11999. If there is a specifyer for a cyclic time stamp, get the closest date to
  12000. DAYNR.
  12001. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12002. the variable date is bound by the calendar when this is called."
  12003. (cond
  12004. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12005. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12006. daynr
  12007. (+ daynr 1000)))
  12008. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12009. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12010. (time-to-days (current-time))) (match-string 0 s)
  12011. prefer show-all))
  12012. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12013. (defun org-days-to-iso-week (days)
  12014. "Return the iso week number."
  12015. (require 'cal-iso)
  12016. (car (calendar-iso-from-absolute days)))
  12017. (defun org-small-year-to-year (year)
  12018. "Convert 2-digit years into 4-digit years.
  12019. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  12020. The year 2000 cannot be abbreviated. Any year larger than 99
  12021. is returned unchanged."
  12022. (if (< year 38)
  12023. (setq year (+ 2000 year))
  12024. (if (< year 100)
  12025. (setq year (+ 1900 year))))
  12026. year)
  12027. (defun org-time-from-absolute (d)
  12028. "Return the time corresponding to date D.
  12029. D may be an absolute day number, or a calendar-type list (month day year)."
  12030. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  12031. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  12032. (defun org-calendar-holiday ()
  12033. "List of holidays, for Diary display in Org-mode."
  12034. (require 'holidays)
  12035. (let ((hl (funcall
  12036. (if (fboundp 'calendar-check-holidays)
  12037. 'calendar-check-holidays 'check-calendar-holidays) date)))
  12038. (if hl (mapconcat 'identity hl "; "))))
  12039. (defun org-diary-sexp-entry (sexp entry date)
  12040. "Process a SEXP diary ENTRY for DATE."
  12041. (require 'diary-lib)
  12042. (let ((result (if calendar-debug-sexp
  12043. (let ((stack-trace-on-error t))
  12044. (eval (car (read-from-string sexp))))
  12045. (condition-case nil
  12046. (eval (car (read-from-string sexp)))
  12047. (error
  12048. (beep)
  12049. (message "Bad sexp at line %d in %s: %s"
  12050. (org-current-line)
  12051. (buffer-file-name) sexp)
  12052. (sleep-for 2))))))
  12053. (cond ((stringp result) result)
  12054. ((and (consp result)
  12055. (stringp (cdr result))) (cdr result))
  12056. (result entry)
  12057. (t nil))))
  12058. (defun org-diary-to-ical-string (frombuf)
  12059. "Get iCalendar entries from diary entries in buffer FROMBUF.
  12060. This uses the icalendar.el library."
  12061. (let* ((tmpdir (if (featurep 'xemacs)
  12062. (temp-directory)
  12063. temporary-file-directory))
  12064. (tmpfile (make-temp-name
  12065. (expand-file-name "orgics" tmpdir)))
  12066. buf rtn b e)
  12067. (save-excursion
  12068. (set-buffer frombuf)
  12069. (icalendar-export-region (point-min) (point-max) tmpfile)
  12070. (setq buf (find-buffer-visiting tmpfile))
  12071. (set-buffer buf)
  12072. (goto-char (point-min))
  12073. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  12074. (setq b (match-beginning 0)))
  12075. (goto-char (point-max))
  12076. (if (re-search-backward "^END:VEVENT" nil t)
  12077. (setq e (match-end 0)))
  12078. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  12079. (kill-buffer buf)
  12080. (delete-file tmpfile)
  12081. rtn))
  12082. (defun org-closest-date (start current change prefer show-all)
  12083. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  12084. When PREFER is `past' return a date that is either CURRENT or past.
  12085. When PREFER is `future', return a date that is either CURRENT or future.
  12086. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  12087. ;; Make the proper lists from the dates
  12088. (catch 'exit
  12089. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  12090. dn dw sday cday n1 n2 n0
  12091. d m y y1 y2 date1 date2 nmonths nm ny m2)
  12092. (setq start (org-date-to-gregorian start)
  12093. current (org-date-to-gregorian
  12094. (if show-all
  12095. current
  12096. (time-to-days (current-time))))
  12097. sday (calendar-absolute-from-gregorian start)
  12098. cday (calendar-absolute-from-gregorian current))
  12099. (if (<= cday sday) (throw 'exit sday))
  12100. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  12101. (setq dn (string-to-number (match-string 1 change))
  12102. dw (cdr (assoc (match-string 2 change) a1)))
  12103. (error "Invalid change specifyer: %s" change))
  12104. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  12105. (cond
  12106. ((eq dw 'day)
  12107. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  12108. n2 (+ n1 dn)))
  12109. ((eq dw 'year)
  12110. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  12111. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  12112. (setq date1 (list m d y1)
  12113. n1 (calendar-absolute-from-gregorian date1)
  12114. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  12115. n2 (calendar-absolute-from-gregorian date2)))
  12116. ((eq dw 'month)
  12117. ;; approx number of month between the two dates
  12118. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  12119. ;; How often does dn fit in there?
  12120. (setq d (nth 1 start) m (car start) y (nth 2 start)
  12121. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  12122. m (+ m nm)
  12123. ny (floor (/ m 12))
  12124. y (+ y ny)
  12125. m (- m (* ny 12)))
  12126. (while (> m 12) (setq m (- m 12) y (1+ y)))
  12127. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  12128. (setq m2 (+ m dn) y2 y)
  12129. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12130. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  12131. (while (<= n2 cday)
  12132. (setq n1 n2 m m2 y y2)
  12133. (setq m2 (+ m dn) y2 y)
  12134. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  12135. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  12136. ;; Make sure n1 is the earlier date
  12137. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  12138. (if show-all
  12139. (cond
  12140. ((eq prefer 'past) n1)
  12141. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12142. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  12143. (cond
  12144. ((eq prefer 'past) n1)
  12145. ((eq prefer 'future) (if (= cday n1) n1 n2))
  12146. (t (if (= cday n1) n1 n2)))))))
  12147. (defun org-date-to-gregorian (date)
  12148. "Turn any specification of DATE into a gregorian date for the calendar."
  12149. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  12150. ((and (listp date) (= (length date) 3)) date)
  12151. ((stringp date)
  12152. (setq date (org-parse-time-string date))
  12153. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  12154. ((listp date)
  12155. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  12156. (defun org-parse-time-string (s &optional nodefault)
  12157. "Parse the standard Org-mode time string.
  12158. This should be a lot faster than the normal `parse-time-string'.
  12159. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  12160. hour and minute fields will be nil if not given."
  12161. (if (string-match org-ts-regexp0 s)
  12162. (list 0
  12163. (if (or (match-beginning 8) (not nodefault))
  12164. (string-to-number (or (match-string 8 s) "0")))
  12165. (if (or (match-beginning 7) (not nodefault))
  12166. (string-to-number (or (match-string 7 s) "0")))
  12167. (string-to-number (match-string 4 s))
  12168. (string-to-number (match-string 3 s))
  12169. (string-to-number (match-string 2 s))
  12170. nil nil nil)
  12171. (error "Not a standard Org-mode time string: %s" s)))
  12172. (defun org-timestamp-up (&optional arg)
  12173. "Increase the date item at the cursor by one.
  12174. If the cursor is on the year, change the year. If it is on the month or
  12175. the day, change that.
  12176. With prefix ARG, change by that many units."
  12177. (interactive "p")
  12178. (org-timestamp-change (prefix-numeric-value arg)))
  12179. (defun org-timestamp-down (&optional arg)
  12180. "Decrease the date item at the cursor by one.
  12181. If the cursor is on the year, change the year. If it is on the month or
  12182. the day, change that.
  12183. With prefix ARG, change by that many units."
  12184. (interactive "p")
  12185. (org-timestamp-change (- (prefix-numeric-value arg))))
  12186. (defun org-timestamp-up-day (&optional arg)
  12187. "Increase the date in the time stamp by one day.
  12188. With prefix ARG, change that many days."
  12189. (interactive "p")
  12190. (if (and (not (org-at-timestamp-p t))
  12191. (org-on-heading-p))
  12192. (org-todo 'up)
  12193. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  12194. (defun org-timestamp-down-day (&optional arg)
  12195. "Decrease the date in the time stamp by one day.
  12196. With prefix ARG, change that many days."
  12197. (interactive "p")
  12198. (if (and (not (org-at-timestamp-p t))
  12199. (org-on-heading-p))
  12200. (org-todo 'down)
  12201. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  12202. (defun org-at-timestamp-p (&optional inactive-ok)
  12203. "Determine if the cursor is in or at a timestamp."
  12204. (interactive)
  12205. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  12206. (pos (point))
  12207. (ans (or (looking-at tsr)
  12208. (save-excursion
  12209. (skip-chars-backward "^[<\n\r\t")
  12210. (if (> (point) (point-min)) (backward-char 1))
  12211. (and (looking-at tsr)
  12212. (> (- (match-end 0) pos) -1))))))
  12213. (and ans
  12214. (boundp 'org-ts-what)
  12215. (setq org-ts-what
  12216. (cond
  12217. ((= pos (match-beginning 0)) 'bracket)
  12218. ((= pos (1- (match-end 0))) 'bracket)
  12219. ((org-pos-in-match-range pos 2) 'year)
  12220. ((org-pos-in-match-range pos 3) 'month)
  12221. ((org-pos-in-match-range pos 7) 'hour)
  12222. ((org-pos-in-match-range pos 8) 'minute)
  12223. ((or (org-pos-in-match-range pos 4)
  12224. (org-pos-in-match-range pos 5)) 'day)
  12225. ((and (> pos (or (match-end 8) (match-end 5)))
  12226. (< pos (match-end 0)))
  12227. (- pos (or (match-end 8) (match-end 5))))
  12228. (t 'day))))
  12229. ans))
  12230. (defun org-toggle-timestamp-type ()
  12231. "Toggle the type (<active> or [inactive]) of a time stamp."
  12232. (interactive)
  12233. (when (org-at-timestamp-p t)
  12234. (let ((beg (match-beginning 0)) (end (match-end 0))
  12235. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  12236. (save-excursion
  12237. (goto-char beg)
  12238. (while (re-search-forward "[][<>]" end t)
  12239. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  12240. t t)))
  12241. (message "Timestamp is now %sactive"
  12242. (if (equal (char-after beg) ?<) "" "in")))))
  12243. (defun org-timestamp-change (n &optional what)
  12244. "Change the date in the time stamp at point.
  12245. The date will be changed by N times WHAT. WHAT can be `day', `month',
  12246. `year', `minute', `second'. If WHAT is not given, the cursor position
  12247. in the timestamp determines what will be changed."
  12248. (let ((pos (point))
  12249. with-hm inactive
  12250. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  12251. org-ts-what
  12252. extra rem
  12253. ts time time0)
  12254. (if (not (org-at-timestamp-p t))
  12255. (error "Not at a timestamp"))
  12256. (if (and (not what) (eq org-ts-what 'bracket))
  12257. (org-toggle-timestamp-type)
  12258. (if (and (not what) (not (eq org-ts-what 'day))
  12259. org-display-custom-times
  12260. (get-text-property (point) 'display)
  12261. (not (get-text-property (1- (point)) 'display)))
  12262. (setq org-ts-what 'day))
  12263. (setq org-ts-what (or what org-ts-what)
  12264. inactive (= (char-after (match-beginning 0)) ?\[)
  12265. ts (match-string 0))
  12266. (replace-match "")
  12267. (if (string-match
  12268. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  12269. ts)
  12270. (setq extra (match-string 1 ts)))
  12271. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  12272. (setq with-hm t))
  12273. (setq time0 (org-parse-time-string ts))
  12274. (when (and (eq org-ts-what 'minute)
  12275. (eq current-prefix-arg nil))
  12276. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  12277. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  12278. (setcar (cdr time0) (+ (nth 1 time0)
  12279. (if (> n 0) (- rem) (- dm rem))))))
  12280. (setq time
  12281. (encode-time (or (car time0) 0)
  12282. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  12283. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  12284. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  12285. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  12286. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  12287. (nthcdr 6 time0)))
  12288. (when (and (member org-ts-what '(hour minute))
  12289. extra
  12290. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  12291. (setq extra (org-modify-ts-extra
  12292. extra
  12293. (if (eq org-ts-what 'hour) 2 5)
  12294. n dm)))
  12295. (when (integerp org-ts-what)
  12296. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  12297. (if (eq what 'calendar)
  12298. (let ((cal-date (org-get-date-from-calendar)))
  12299. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  12300. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  12301. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  12302. (setcar time0 (or (car time0) 0))
  12303. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  12304. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  12305. (setq time (apply 'encode-time time0))))
  12306. (setq org-last-changed-timestamp
  12307. (org-insert-time-stamp time with-hm inactive nil nil extra))
  12308. (org-clock-update-time-maybe)
  12309. (goto-char pos)
  12310. ;; Try to recenter the calendar window, if any
  12311. (if (and org-calendar-follow-timestamp-change
  12312. (get-buffer-window "*Calendar*" t)
  12313. (memq org-ts-what '(day month year)))
  12314. (org-recenter-calendar (time-to-days time))))))
  12315. (defun org-modify-ts-extra (s pos n dm)
  12316. "Change the different parts of the lead-time and repeat fields in timestamp."
  12317. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  12318. ng h m new rem)
  12319. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  12320. (cond
  12321. ((or (org-pos-in-match-range pos 2)
  12322. (org-pos-in-match-range pos 3))
  12323. (setq m (string-to-number (match-string 3 s))
  12324. h (string-to-number (match-string 2 s)))
  12325. (if (org-pos-in-match-range pos 2)
  12326. (setq h (+ h n))
  12327. (setq n (* dm (org-no-warnings (signum n))))
  12328. (when (not (= 0 (setq rem (% m dm))))
  12329. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  12330. (setq m (+ m n)))
  12331. (if (< m 0) (setq m (+ m 60) h (1- h)))
  12332. (if (> m 59) (setq m (- m 60) h (1+ h)))
  12333. (setq h (min 24 (max 0 h)))
  12334. (setq ng 1 new (format "-%02d:%02d" h m)))
  12335. ((org-pos-in-match-range pos 6)
  12336. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  12337. ((org-pos-in-match-range pos 5)
  12338. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  12339. ((org-pos-in-match-range pos 9)
  12340. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  12341. ((org-pos-in-match-range pos 8)
  12342. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  12343. (when ng
  12344. (setq s (concat
  12345. (substring s 0 (match-beginning ng))
  12346. new
  12347. (substring s (match-end ng))))))
  12348. s))
  12349. (defun org-recenter-calendar (date)
  12350. "If the calendar is visible, recenter it to DATE."
  12351. (let* ((win (selected-window))
  12352. (cwin (get-buffer-window "*Calendar*" t))
  12353. (calendar-move-hook nil))
  12354. (when cwin
  12355. (select-window cwin)
  12356. (calendar-goto-date (if (listp date) date
  12357. (calendar-gregorian-from-absolute date)))
  12358. (select-window win))))
  12359. (defun org-goto-calendar (&optional arg)
  12360. "Go to the Emacs calendar at the current date.
  12361. If there is a time stamp in the current line, go to that date.
  12362. A prefix ARG can be used to force the current date."
  12363. (interactive "P")
  12364. (let ((tsr org-ts-regexp) diff
  12365. (calendar-move-hook nil)
  12366. (calendar-view-holidays-initially-flag nil)
  12367. (view-calendar-holidays-initially nil)
  12368. (calendar-view-diary-initially-flag nil)
  12369. (view-diary-entries-initially nil))
  12370. (if (or (org-at-timestamp-p)
  12371. (save-excursion
  12372. (beginning-of-line 1)
  12373. (looking-at (concat ".*" tsr))))
  12374. (let ((d1 (time-to-days (current-time)))
  12375. (d2 (time-to-days
  12376. (org-time-string-to-time (match-string 1)))))
  12377. (setq diff (- d2 d1))))
  12378. (calendar)
  12379. (calendar-goto-today)
  12380. (if (and diff (not arg)) (calendar-forward-day diff))))
  12381. (defun org-get-date-from-calendar ()
  12382. "Return a list (month day year) of date at point in calendar."
  12383. (with-current-buffer "*Calendar*"
  12384. (save-match-data
  12385. (calendar-cursor-to-date))))
  12386. (defun org-date-from-calendar ()
  12387. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  12388. If there is already a time stamp at the cursor position, update it."
  12389. (interactive)
  12390. (if (org-at-timestamp-p t)
  12391. (org-timestamp-change 0 'calendar)
  12392. (let ((cal-date (org-get-date-from-calendar)))
  12393. (org-insert-time-stamp
  12394. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  12395. (defun org-minutes-to-hh:mm-string (m)
  12396. "Compute H:MM from a number of minutes."
  12397. (let ((h (/ m 60)))
  12398. (setq m (- m (* 60 h)))
  12399. (format org-time-clocksum-format h m)))
  12400. (defun org-hh:mm-string-to-minutes (s)
  12401. "Convert a string H:MM to a number of minutes.
  12402. If the string is just a number, interprete it as minutes.
  12403. In fact, the first hh:mm or number in the string will be taken,
  12404. there can be extra stuff in the string.
  12405. If no number is found, the return value is 0."
  12406. (cond
  12407. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  12408. (+ (* (string-to-number (match-string 1 s)) 60)
  12409. (string-to-number (match-string 2 s))))
  12410. ((string-match "\\([0-9]+\\)" s)
  12411. (string-to-number (match-string 1 s)))
  12412. (t 0)))
  12413. ;;;; Files
  12414. (defun org-save-all-org-buffers ()
  12415. "Save all Org-mode buffers without user confirmation."
  12416. (interactive)
  12417. (message "Saving all Org-mode buffers...")
  12418. (save-some-buffers t 'org-mode-p)
  12419. (when (featurep 'org-id) (org-id-locations-save))
  12420. (message "Saving all Org-mode buffers... done"))
  12421. (defun org-revert-all-org-buffers ()
  12422. "Revert all Org-mode buffers.
  12423. Prompt for confirmation when there are unsaved changes.
  12424. Be sure you know what you are doing before letting this function
  12425. overwrite your changes.
  12426. This function is useful in a setup where one tracks org files
  12427. with a version control system, to revert on one machine after pulling
  12428. changes from another. I believe the procedure must be like this:
  12429. 1. M-x org-save-all-org-buffers
  12430. 2. Pull changes from the other machine, resolve conflicts
  12431. 3. M-x org-revert-all-org-buffers"
  12432. (interactive)
  12433. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  12434. (error "Abort"))
  12435. (save-excursion
  12436. (save-window-excursion
  12437. (mapc
  12438. (lambda (b)
  12439. (when (and (with-current-buffer b (org-mode-p))
  12440. (with-current-buffer b buffer-file-name))
  12441. (switch-to-buffer b)
  12442. (revert-buffer t 'no-confirm)))
  12443. (buffer-list))
  12444. (when (and (featurep 'org-id) org-id-track-globally)
  12445. (org-id-locations-load)))))
  12446. ;;;; Agenda files
  12447. ;;;###autoload
  12448. (defun org-iswitchb (&optional arg)
  12449. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  12450. With a prefix argument, restrict available to files.
  12451. With two prefix arguments, restrict available buffers to agenda files."
  12452. (interactive "P")
  12453. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  12454. ((equal arg '(16)) (org-buffer-list 'agenda))
  12455. (t (org-buffer-list)))))
  12456. (switch-to-buffer
  12457. (org-icompleting-read "Org buffer: "
  12458. (mapcar 'list (mapcar 'buffer-name blist))
  12459. nil t))))
  12460. ;;;###autoload
  12461. (defalias 'org-ido-switchb 'org-iswitchb)
  12462. (defun org-buffer-list (&optional predicate exclude-tmp)
  12463. "Return a list of Org buffers.
  12464. PREDICATE can be `export', `files' or `agenda'.
  12465. export restrict the list to Export buffers.
  12466. files restrict the list to buffers visiting Org files.
  12467. agenda restrict the list to buffers visiting agenda files.
  12468. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  12469. (let* ((bfn nil)
  12470. (agenda-files (and (eq predicate 'agenda)
  12471. (mapcar 'file-truename (org-agenda-files t))))
  12472. (filter
  12473. (cond
  12474. ((eq predicate 'files)
  12475. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  12476. ((eq predicate 'export)
  12477. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  12478. ((eq predicate 'agenda)
  12479. (lambda (b)
  12480. (with-current-buffer b
  12481. (and (eq major-mode 'org-mode)
  12482. (setq bfn (buffer-file-name b))
  12483. (member (file-truename bfn) agenda-files)))))
  12484. (t (lambda (b) (with-current-buffer b
  12485. (or (eq major-mode 'org-mode)
  12486. (string-match "\*Org .*Export"
  12487. (buffer-name b)))))))))
  12488. (delq nil
  12489. (mapcar
  12490. (lambda(b)
  12491. (if (and (funcall filter b)
  12492. (or (not exclude-tmp)
  12493. (not (string-match "tmp" (buffer-name b)))))
  12494. b
  12495. nil))
  12496. (buffer-list)))))
  12497. (defun org-agenda-files (&optional unrestricted archives)
  12498. "Get the list of agenda files.
  12499. Optional UNRESTRICTED means return the full list even if a restriction
  12500. is currently in place.
  12501. When ARCHIVES is t, include all archive files hat are really being
  12502. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  12503. `org-agenda-archives-mode' is t."
  12504. (let ((files
  12505. (cond
  12506. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  12507. ((stringp org-agenda-files) (org-read-agenda-file-list))
  12508. ((listp org-agenda-files) org-agenda-files)
  12509. (t (error "Invalid value of `org-agenda-files'")))))
  12510. (setq files (apply 'append
  12511. (mapcar (lambda (f)
  12512. (if (file-directory-p f)
  12513. (directory-files
  12514. f t org-agenda-file-regexp)
  12515. (list f)))
  12516. files)))
  12517. (when org-agenda-skip-unavailable-files
  12518. (setq files (delq nil
  12519. (mapcar (function
  12520. (lambda (file)
  12521. (and (file-readable-p file) file)))
  12522. files))))
  12523. (when (or (eq archives t)
  12524. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  12525. (setq files (org-add-archive-files files)))
  12526. files))
  12527. (defun org-edit-agenda-file-list ()
  12528. "Edit the list of agenda files.
  12529. Depending on setup, this either uses customize to edit the variable
  12530. `org-agenda-files', or it visits the file that is holding the list. In the
  12531. latter case, the buffer is set up in a way that saving it automatically kills
  12532. the buffer and restores the previous window configuration."
  12533. (interactive)
  12534. (if (stringp org-agenda-files)
  12535. (let ((cw (current-window-configuration)))
  12536. (find-file org-agenda-files)
  12537. (org-set-local 'org-window-configuration cw)
  12538. (org-add-hook 'after-save-hook
  12539. (lambda ()
  12540. (set-window-configuration
  12541. (prog1 org-window-configuration
  12542. (kill-buffer (current-buffer))))
  12543. (org-install-agenda-files-menu)
  12544. (message "New agenda file list installed"))
  12545. nil 'local)
  12546. (message "%s" (substitute-command-keys
  12547. "Edit list and finish with \\[save-buffer]")))
  12548. (customize-variable 'org-agenda-files)))
  12549. (defun org-store-new-agenda-file-list (list)
  12550. "Set new value for the agenda file list and save it correctly."
  12551. (if (stringp org-agenda-files)
  12552. (let ((f org-agenda-files) b)
  12553. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  12554. (with-temp-file f
  12555. (insert (mapconcat 'identity list "\n") "\n")))
  12556. (let ((org-mode-hook nil) (org-inhibit-startup t)
  12557. (org-insert-mode-line-in-empty-file nil))
  12558. (setq org-agenda-files list)
  12559. (customize-save-variable 'org-agenda-files org-agenda-files))))
  12560. (defun org-read-agenda-file-list ()
  12561. "Read the list of agenda files from a file."
  12562. (when (file-directory-p org-agenda-files)
  12563. (error "`org-agenda-files' cannot be a single directory"))
  12564. (when (stringp org-agenda-files)
  12565. (with-temp-buffer
  12566. (insert-file-contents org-agenda-files)
  12567. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  12568. ;;;###autoload
  12569. (defun org-cycle-agenda-files ()
  12570. "Cycle through the files in `org-agenda-files'.
  12571. If the current buffer visits an agenda file, find the next one in the list.
  12572. If the current buffer does not, find the first agenda file."
  12573. (interactive)
  12574. (let* ((fs (org-agenda-files t))
  12575. (files (append fs (list (car fs))))
  12576. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  12577. file)
  12578. (unless files (error "No agenda files"))
  12579. (catch 'exit
  12580. (while (setq file (pop files))
  12581. (if (equal (file-truename file) tcf)
  12582. (when (car files)
  12583. (find-file (car files))
  12584. (throw 'exit t))))
  12585. (find-file (car fs)))
  12586. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  12587. (defun org-agenda-file-to-front (&optional to-end)
  12588. "Move/add the current file to the top of the agenda file list.
  12589. If the file is not present in the list, it is added to the front. If it is
  12590. present, it is moved there. With optional argument TO-END, add/move to the
  12591. end of the list."
  12592. (interactive "P")
  12593. (let ((org-agenda-skip-unavailable-files nil)
  12594. (file-alist (mapcar (lambda (x)
  12595. (cons (file-truename x) x))
  12596. (org-agenda-files t)))
  12597. (ctf (file-truename buffer-file-name))
  12598. x had)
  12599. (setq x (assoc ctf file-alist) had x)
  12600. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  12601. (if to-end
  12602. (setq file-alist (append (delq x file-alist) (list x)))
  12603. (setq file-alist (cons x (delq x file-alist))))
  12604. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  12605. (org-install-agenda-files-menu)
  12606. (message "File %s to %s of agenda file list"
  12607. (if had "moved" "added") (if to-end "end" "front"))))
  12608. (defun org-remove-file (&optional file)
  12609. "Remove current file from the list of files in variable `org-agenda-files'.
  12610. These are the files which are being checked for agenda entries.
  12611. Optional argument FILE means, use this file instead of the current."
  12612. (interactive)
  12613. (let* ((org-agenda-skip-unavailable-files nil)
  12614. (file (or file buffer-file-name))
  12615. (true-file (file-truename file))
  12616. (afile (abbreviate-file-name file))
  12617. (files (delq nil (mapcar
  12618. (lambda (x)
  12619. (if (equal true-file
  12620. (file-truename x))
  12621. nil x))
  12622. (org-agenda-files t)))))
  12623. (if (not (= (length files) (length (org-agenda-files t))))
  12624. (progn
  12625. (org-store-new-agenda-file-list files)
  12626. (org-install-agenda-files-menu)
  12627. (message "Removed file: %s" afile))
  12628. (message "File was not in list: %s (not removed)" afile))))
  12629. (defun org-file-menu-entry (file)
  12630. (vector file (list 'find-file file) t))
  12631. (defun org-check-agenda-file (file)
  12632. "Make sure FILE exists. If not, ask user what to do."
  12633. (when (not (file-exists-p file))
  12634. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  12635. (abbreviate-file-name file))
  12636. (let ((r (downcase (read-char-exclusive))))
  12637. (cond
  12638. ((equal r ?r)
  12639. (org-remove-file file)
  12640. (throw 'nextfile t))
  12641. (t (error "Abort"))))))
  12642. (defun org-get-agenda-file-buffer (file)
  12643. "Get a buffer visiting FILE. If the buffer needs to be created, add
  12644. it to the list of buffers which might be released later."
  12645. (let ((buf (org-find-base-buffer-visiting file)))
  12646. (if buf
  12647. buf ; just return it
  12648. ;; Make a new buffer and remember it
  12649. (setq buf (find-file-noselect file))
  12650. (if buf (push buf org-agenda-new-buffers))
  12651. buf)))
  12652. (defun org-release-buffers (blist)
  12653. "Release all buffers in list, asking the user for confirmation when needed.
  12654. When a buffer is unmodified, it is just killed. When modified, it is saved
  12655. \(if the user agrees) and then killed."
  12656. (let (buf file)
  12657. (while (setq buf (pop blist))
  12658. (setq file (buffer-file-name buf))
  12659. (when (and (buffer-modified-p buf)
  12660. file
  12661. (y-or-n-p (format "Save file %s? " file)))
  12662. (with-current-buffer buf (save-buffer)))
  12663. (kill-buffer buf))))
  12664. (defun org-prepare-agenda-buffers (files)
  12665. "Create buffers for all agenda files, protect archived trees and comments."
  12666. (interactive)
  12667. (let ((pa '(:org-archived t))
  12668. (pc '(:org-comment t))
  12669. (pall '(:org-archived t :org-comment t))
  12670. (inhibit-read-only t)
  12671. (rea (concat ":" org-archive-tag ":"))
  12672. bmp file re)
  12673. (save-excursion
  12674. (save-restriction
  12675. (while (setq file (pop files))
  12676. (catch 'nextfile
  12677. (if (bufferp file)
  12678. (set-buffer file)
  12679. (org-check-agenda-file file)
  12680. (set-buffer (org-get-agenda-file-buffer file)))
  12681. (widen)
  12682. (setq bmp (buffer-modified-p))
  12683. (org-refresh-category-properties)
  12684. (setq org-todo-keywords-for-agenda
  12685. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  12686. (setq org-done-keywords-for-agenda
  12687. (append org-done-keywords-for-agenda org-done-keywords))
  12688. (setq org-todo-keyword-alist-for-agenda
  12689. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  12690. (setq org-tag-alist-for-agenda
  12691. (append org-tag-alist-for-agenda org-tag-alist))
  12692. (save-excursion
  12693. (remove-text-properties (point-min) (point-max) pall)
  12694. (when org-agenda-skip-archived-trees
  12695. (goto-char (point-min))
  12696. (while (re-search-forward rea nil t)
  12697. (if (org-on-heading-p t)
  12698. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  12699. (goto-char (point-min))
  12700. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  12701. (while (re-search-forward re nil t)
  12702. (add-text-properties
  12703. (match-beginning 0) (org-end-of-subtree t) pc)))
  12704. (set-buffer-modified-p bmp)))))
  12705. (setq org-todo-keyword-alist-for-agenda
  12706. (org-uniquify org-todo-keyword-alist-for-agenda)
  12707. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  12708. ;;;; Embedded LaTeX
  12709. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  12710. "Keymap for the minor `org-cdlatex-mode'.")
  12711. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  12712. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  12713. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  12714. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  12715. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  12716. (defvar org-cdlatex-texmathp-advice-is-done nil
  12717. "Flag remembering if we have applied the advice to texmathp already.")
  12718. (define-minor-mode org-cdlatex-mode
  12719. "Toggle the minor `org-cdlatex-mode'.
  12720. This mode supports entering LaTeX environment and math in LaTeX fragments
  12721. in Org-mode.
  12722. \\{org-cdlatex-mode-map}"
  12723. nil " OCDL" nil
  12724. (when org-cdlatex-mode (require 'cdlatex))
  12725. (unless org-cdlatex-texmathp-advice-is-done
  12726. (setq org-cdlatex-texmathp-advice-is-done t)
  12727. (defadvice texmathp (around org-math-always-on activate)
  12728. "Always return t in org-mode buffers.
  12729. This is because we want to insert math symbols without dollars even outside
  12730. the LaTeX math segments. If Orgmode thinks that point is actually inside
  12731. an embedded LaTeX fragment, let texmathp do its job.
  12732. \\[org-cdlatex-mode-map]"
  12733. (interactive)
  12734. (let (p)
  12735. (cond
  12736. ((not (org-mode-p)) ad-do-it)
  12737. ((eq this-command 'cdlatex-math-symbol)
  12738. (setq ad-return-value t
  12739. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  12740. (t
  12741. (let ((p (org-inside-LaTeX-fragment-p)))
  12742. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  12743. (setq ad-return-value t
  12744. texmathp-why '("Org-mode embedded math" . 0))
  12745. (if p ad-do-it)))))))))
  12746. (defun turn-on-org-cdlatex ()
  12747. "Unconditionally turn on `org-cdlatex-mode'."
  12748. (org-cdlatex-mode 1))
  12749. (defun org-inside-LaTeX-fragment-p ()
  12750. "Test if point is inside a LaTeX fragment.
  12751. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  12752. sequence appearing also before point.
  12753. Even though the matchers for math are configurable, this function assumes
  12754. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  12755. delimiters are skipped when they have been removed by customization.
  12756. The return value is nil, or a cons cell with the delimiter and
  12757. and the position of this delimiter.
  12758. This function does a reasonably good job, but can locally be fooled by
  12759. for example currency specifications. For example it will assume being in
  12760. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  12761. fragments that are properly closed, but during editing, we have to live
  12762. with the uncertainty caused by missing closing delimiters. This function
  12763. looks only before point, not after."
  12764. (catch 'exit
  12765. (let ((pos (point))
  12766. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  12767. (lim (progn
  12768. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  12769. (point)))
  12770. dd-on str (start 0) m re)
  12771. (goto-char pos)
  12772. (when dodollar
  12773. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  12774. re (nth 1 (assoc "$" org-latex-regexps)))
  12775. (while (string-match re str start)
  12776. (cond
  12777. ((= (match-end 0) (length str))
  12778. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  12779. ((= (match-end 0) (- (length str) 5))
  12780. (throw 'exit nil))
  12781. (t (setq start (match-end 0))))))
  12782. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  12783. (goto-char pos)
  12784. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  12785. (and (match-beginning 2) (throw 'exit nil))
  12786. ;; count $$
  12787. (while (re-search-backward "\\$\\$" lim t)
  12788. (setq dd-on (not dd-on)))
  12789. (goto-char pos)
  12790. (if dd-on (cons "$$" m))))))
  12791. (defun org-try-cdlatex-tab ()
  12792. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  12793. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  12794. - inside a LaTeX fragment, or
  12795. - after the first word in a line, where an abbreviation expansion could
  12796. insert a LaTeX environment."
  12797. (when org-cdlatex-mode
  12798. (cond
  12799. ((save-excursion
  12800. (skip-chars-backward "a-zA-Z0-9*")
  12801. (skip-chars-backward " \t")
  12802. (bolp))
  12803. (cdlatex-tab) t)
  12804. ((org-inside-LaTeX-fragment-p)
  12805. (cdlatex-tab) t)
  12806. (t nil))))
  12807. (defun org-cdlatex-underscore-caret (&optional arg)
  12808. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  12809. Revert to the normal definition outside of these fragments."
  12810. (interactive "P")
  12811. (if (org-inside-LaTeX-fragment-p)
  12812. (call-interactively 'cdlatex-sub-superscript)
  12813. (let (org-cdlatex-mode)
  12814. (call-interactively (key-binding (vector last-input-event))))))
  12815. (defun org-cdlatex-math-modify (&optional arg)
  12816. "Execute `cdlatex-math-modify' in LaTeX fragments.
  12817. Revert to the normal definition outside of these fragments."
  12818. (interactive "P")
  12819. (if (org-inside-LaTeX-fragment-p)
  12820. (call-interactively 'cdlatex-math-modify)
  12821. (let (org-cdlatex-mode)
  12822. (call-interactively (key-binding (vector last-input-event))))))
  12823. (defvar org-latex-fragment-image-overlays nil
  12824. "List of overlays carrying the images of latex fragments.")
  12825. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  12826. (defun org-remove-latex-fragment-image-overlays ()
  12827. "Remove all overlays with LaTeX fragment images in current buffer."
  12828. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  12829. (setq org-latex-fragment-image-overlays nil))
  12830. (defun org-preview-latex-fragment (&optional subtree)
  12831. "Preview the LaTeX fragment at point, or all locally or globally.
  12832. If the cursor is in a LaTeX fragment, create the image and overlay
  12833. it over the source code. If there is no fragment at point, display
  12834. all fragments in the current text, from one headline to the next. With
  12835. prefix SUBTREE, display all fragments in the current subtree. With a
  12836. double prefix `C-u C-u', or when the cursor is before the first headline,
  12837. display all fragments in the buffer.
  12838. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  12839. (interactive "P")
  12840. (org-remove-latex-fragment-image-overlays)
  12841. (save-excursion
  12842. (save-restriction
  12843. (let (beg end at msg)
  12844. (cond
  12845. ((or (equal subtree '(16))
  12846. (not (save-excursion
  12847. (re-search-backward (concat "^" outline-regexp) nil t))))
  12848. (setq beg (point-min) end (point-max)
  12849. msg "Creating images for buffer...%s"))
  12850. ((equal subtree '(4))
  12851. (org-back-to-heading)
  12852. (setq beg (point) end (org-end-of-subtree t)
  12853. msg "Creating images for subtree...%s"))
  12854. (t
  12855. (if (setq at (org-inside-LaTeX-fragment-p))
  12856. (goto-char (max (point-min) (- (cdr at) 2)))
  12857. (org-back-to-heading))
  12858. (setq beg (point) end (progn (outline-next-heading) (point))
  12859. msg (if at "Creating image...%s"
  12860. "Creating images for entry...%s"))))
  12861. (message msg "")
  12862. (narrow-to-region beg end)
  12863. (goto-char beg)
  12864. (org-format-latex
  12865. (concat "ltxpng/" (file-name-sans-extension
  12866. (file-name-nondirectory
  12867. buffer-file-name)))
  12868. default-directory 'overlays msg at 'forbuffer)
  12869. (message msg "done. Use `C-c C-c' to remove images.")))))
  12870. (defvar org-latex-regexps
  12871. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  12872. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  12873. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  12874. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12875. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  12876. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  12877. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  12878. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  12879. "Regular expressions for matching embedded LaTeX.")
  12880. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  12881. "Replace LaTeX fragments with links to an image, and produce images."
  12882. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  12883. (let* ((prefixnodir (file-name-nondirectory prefix))
  12884. (absprefix (expand-file-name prefix dir))
  12885. (todir (file-name-directory absprefix))
  12886. (opt org-format-latex-options)
  12887. (matchers (plist-get opt :matchers))
  12888. (re-list org-latex-regexps)
  12889. (cnt 0) txt link beg end re e checkdir
  12890. executables-checked
  12891. m n block linkfile movefile ov)
  12892. ;; Check if there are old images files with this prefix, and remove them
  12893. (when (file-directory-p todir)
  12894. (mapc 'delete-file
  12895. (directory-files
  12896. todir 'full
  12897. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  12898. ;; Check the different regular expressions
  12899. (while (setq e (pop re-list))
  12900. (setq m (car e) re (nth 1 e) n (nth 2 e)
  12901. block (if (nth 3 e) "\n\n" ""))
  12902. (when (member m matchers)
  12903. (goto-char (point-min))
  12904. (while (re-search-forward re nil t)
  12905. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  12906. (not (get-text-property (match-beginning n)
  12907. 'org-protected))
  12908. (or (not overlays)
  12909. (not (eq (get-char-property (match-beginning n)
  12910. 'org-overlay-type)
  12911. 'org-latex-overlay))))
  12912. (setq txt (match-string n)
  12913. beg (match-beginning n) end (match-end n)
  12914. cnt (1+ cnt)
  12915. linkfile (format "%s_%04d.png" prefix cnt)
  12916. movefile (format "%s_%04d.png" absprefix cnt)
  12917. link (concat block "[[file:" linkfile "]]" block))
  12918. (if msg (message msg cnt))
  12919. (goto-char beg)
  12920. (unless checkdir ; make sure the directory exists
  12921. (setq checkdir t)
  12922. (or (file-directory-p todir) (make-directory todir)))
  12923. (unless executables-checked
  12924. (org-check-external-command
  12925. "latex" "needed to convert LaTeX fragments to images")
  12926. (org-check-external-command
  12927. "dvipng" "needed to convert LaTeX fragments to images")
  12928. (setq executables-checked t))
  12929. (org-create-formula-image
  12930. txt movefile opt forbuffer)
  12931. (if overlays
  12932. (progn
  12933. (mapc (lambda (o)
  12934. (if (eq (org-overlay-get o 'org-overlay-type)
  12935. 'org-latex-overlay)
  12936. (org-delete-overlay o)))
  12937. (org-overlays-in beg end))
  12938. (setq ov (org-make-overlay beg end))
  12939. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  12940. (if (featurep 'xemacs)
  12941. (progn
  12942. (org-overlay-put ov 'invisible t)
  12943. (org-overlay-put
  12944. ov 'end-glyph
  12945. (make-glyph (vector 'png :file movefile))))
  12946. (org-overlay-put
  12947. ov 'display
  12948. (list 'image :type 'png :file movefile :ascent 'center)))
  12949. (push ov org-latex-fragment-image-overlays)
  12950. (goto-char end))
  12951. (delete-region beg end)
  12952. (insert link))))))))
  12953. (defvar org-export-latex-packages-alist) ;; defined in org-latex.el
  12954. ;; This function borrows from Ganesh Swami's latex2png.el
  12955. (defun org-create-formula-image (string tofile options buffer)
  12956. (require 'org-latex)
  12957. (let* ((tmpdir (if (featurep 'xemacs)
  12958. (temp-directory)
  12959. temporary-file-directory))
  12960. (texfilebase (make-temp-name
  12961. (expand-file-name "orgtex" tmpdir)))
  12962. (texfile (concat texfilebase ".tex"))
  12963. (dvifile (concat texfilebase ".dvi"))
  12964. (pngfile (concat texfilebase ".png"))
  12965. (fnh (if (featurep 'xemacs)
  12966. (font-height (get-face-font 'default))
  12967. (face-attribute 'default :height nil)))
  12968. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  12969. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  12970. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  12971. "Black"))
  12972. (bg (or (plist-get options (if buffer :background :html-background))
  12973. "Transparent")))
  12974. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  12975. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  12976. (with-temp-file texfile
  12977. (insert org-format-latex-header
  12978. (if org-export-latex-packages-alist
  12979. (concat "\n"
  12980. (mapconcat (lambda(p)
  12981. (if (equal "" (car p))
  12982. (format "\\usepackage{%s}" (cadr p))
  12983. (format "\\usepackage[%s]{%s}"
  12984. (car p) (cadr p))))
  12985. org-export-latex-packages-alist "\n"))
  12986. "")
  12987. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  12988. (let ((dir default-directory))
  12989. (condition-case nil
  12990. (progn
  12991. (cd tmpdir)
  12992. (call-process "latex" nil nil nil texfile))
  12993. (error nil))
  12994. (cd dir))
  12995. (if (not (file-exists-p dvifile))
  12996. (progn (message "Failed to create dvi file from %s" texfile) nil)
  12997. (condition-case nil
  12998. (call-process "dvipng" nil nil nil
  12999. "-fg" fg "-bg" bg
  13000. "-D" dpi
  13001. ;;"-x" scale "-y" scale
  13002. "-T" "tight"
  13003. "-o" pngfile
  13004. dvifile)
  13005. (error nil))
  13006. (if (not (file-exists-p pngfile))
  13007. (progn (message "Failed to create png file from %s" texfile) nil)
  13008. ;; Use the requested file name and clean up
  13009. (copy-file pngfile tofile 'replace)
  13010. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  13011. (delete-file (concat texfilebase e)))
  13012. pngfile))))
  13013. (defun org-dvipng-color (attr)
  13014. "Return an rgb color specification for dvipng."
  13015. (apply 'format "rgb %s %s %s"
  13016. (mapcar 'org-normalize-color
  13017. (color-values (face-attribute 'default attr nil)))))
  13018. (defun org-normalize-color (value)
  13019. "Return string to be used as color value for an RGB component."
  13020. (format "%g" (/ value 65535.0)))
  13021. ;;;; Key bindings
  13022. ;; Make `C-c C-x' a prefix key
  13023. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  13024. ;; TAB key with modifiers
  13025. (org-defkey org-mode-map "\C-i" 'org-cycle)
  13026. (org-defkey org-mode-map [(tab)] 'org-cycle)
  13027. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  13028. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  13029. (org-defkey org-mode-map "\M-\t" 'org-complete)
  13030. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  13031. ;; The following line is necessary under Suse GNU/Linux
  13032. (unless (featurep 'xemacs)
  13033. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  13034. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  13035. (define-key org-mode-map [backtab] 'org-shifttab)
  13036. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  13037. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  13038. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  13039. ;; Cursor keys with modifiers
  13040. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  13041. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  13042. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  13043. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  13044. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  13045. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  13046. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  13047. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  13048. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  13049. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  13050. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  13051. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  13052. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  13053. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  13054. ;;; Extra keys for tty access.
  13055. ;; We only set them when really needed because otherwise the
  13056. ;; menus don't show the simple keys
  13057. (when (or org-use-extra-keys
  13058. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  13059. (not window-system))
  13060. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  13061. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  13062. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  13063. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  13064. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  13065. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  13066. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  13067. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  13068. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  13069. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  13070. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  13071. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  13072. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  13073. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  13074. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  13075. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  13076. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  13077. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  13078. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  13079. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  13080. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  13081. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  13082. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  13083. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  13084. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  13085. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  13086. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  13087. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  13088. ;; All the other keys
  13089. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  13090. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  13091. (if (boundp 'narrow-map)
  13092. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  13093. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  13094. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  13095. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  13096. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  13097. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  13098. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  13099. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  13100. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  13101. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  13102. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  13103. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  13104. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  13105. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  13106. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  13107. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  13108. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  13109. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  13110. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  13111. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  13112. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  13113. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  13114. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  13115. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  13116. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  13117. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  13118. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  13119. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  13120. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  13121. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  13122. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  13123. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  13124. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  13125. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  13126. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  13127. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  13128. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  13129. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  13130. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  13131. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  13132. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  13133. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  13134. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  13135. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13136. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  13137. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  13138. (org-defkey org-mode-map "\C-c^" 'org-sort)
  13139. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  13140. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  13141. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  13142. (org-defkey org-mode-map "\C-m" 'org-return)
  13143. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  13144. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  13145. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  13146. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  13147. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  13148. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  13149. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  13150. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  13151. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  13152. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  13153. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  13154. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  13155. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  13156. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  13157. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  13158. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  13159. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  13160. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  13161. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  13162. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  13163. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  13164. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  13165. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  13166. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  13167. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  13168. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  13169. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  13170. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  13171. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  13172. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  13173. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  13174. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  13175. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  13176. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  13177. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  13178. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  13179. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  13180. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  13181. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  13182. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  13183. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  13184. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  13185. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  13186. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  13187. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  13188. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  13189. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  13190. (when (featurep 'xemacs)
  13191. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  13192. (defvar org-self-insert-command-undo-counter 0)
  13193. (defvar org-table-auto-blank-field) ; defined in org-table.el
  13194. (defun org-self-insert-command (N)
  13195. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  13196. If the cursor is in a table looking at whitespace, the whitespace is
  13197. overwritten, and the table is not marked as requiring realignment."
  13198. (interactive "p")
  13199. (if (and
  13200. (org-table-p)
  13201. (progn
  13202. ;; check if we blank the field, and if that triggers align
  13203. (and (featurep 'org-table) org-table-auto-blank-field
  13204. (member last-command
  13205. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  13206. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  13207. ;; got extra space, this field does not determine column width
  13208. (let (org-table-may-need-update) (org-table-blank-field))
  13209. ;; no extra space, this field may determine column width
  13210. (org-table-blank-field)))
  13211. t)
  13212. (eq N 1)
  13213. (looking-at "[^|\n]* |"))
  13214. (let (org-table-may-need-update)
  13215. (goto-char (1- (match-end 0)))
  13216. (delete-backward-char 1)
  13217. (goto-char (match-beginning 0))
  13218. (self-insert-command N))
  13219. (setq org-table-may-need-update t)
  13220. (self-insert-command N)
  13221. (org-fix-tags-on-the-fly)
  13222. (if org-self-insert-cluster-for-undo
  13223. (if (not (eq last-command 'org-self-insert-command))
  13224. (setq org-self-insert-command-undo-counter 1)
  13225. (if (>= org-self-insert-command-undo-counter 20)
  13226. (setq org-self-insert-command-undo-counter 1)
  13227. (and (> org-self-insert-command-undo-counter 0)
  13228. buffer-undo-list
  13229. (not (cadr buffer-undo-list)) ; remove nil entry
  13230. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  13231. (setq org-self-insert-command-undo-counter
  13232. (1+ org-self-insert-command-undo-counter)))))))
  13233. (defun org-fix-tags-on-the-fly ()
  13234. (when (and (equal (char-after (point-at-bol)) ?*)
  13235. (org-on-heading-p))
  13236. (org-align-tags-here org-tags-column)))
  13237. (defun org-delete-backward-char (N)
  13238. "Like `delete-backward-char', insert whitespace at field end in tables.
  13239. When deleting backwards, in tables this function will insert whitespace in
  13240. front of the next \"|\" separator, to keep the table aligned. The table will
  13241. still be marked for re-alignment if the field did fill the entire column,
  13242. because, in this case the deletion might narrow the column."
  13243. (interactive "p")
  13244. (if (and (org-table-p)
  13245. (eq N 1)
  13246. (string-match "|" (buffer-substring (point-at-bol) (point)))
  13247. (looking-at ".*?|"))
  13248. (let ((pos (point))
  13249. (noalign (looking-at "[^|\n\r]* |"))
  13250. (c org-table-may-need-update))
  13251. (backward-delete-char N)
  13252. (skip-chars-forward "^|")
  13253. (insert " ")
  13254. (goto-char (1- pos))
  13255. ;; noalign: if there were two spaces at the end, this field
  13256. ;; does not determine the width of the column.
  13257. (if noalign (setq org-table-may-need-update c)))
  13258. (backward-delete-char N)
  13259. (org-fix-tags-on-the-fly)))
  13260. (defun org-delete-char (N)
  13261. "Like `delete-char', but insert whitespace at field end in tables.
  13262. When deleting characters, in tables this function will insert whitespace in
  13263. front of the next \"|\" separator, to keep the table aligned. The table will
  13264. still be marked for re-alignment if the field did fill the entire column,
  13265. because, in this case the deletion might narrow the column."
  13266. (interactive "p")
  13267. (if (and (org-table-p)
  13268. (not (bolp))
  13269. (not (= (char-after) ?|))
  13270. (eq N 1))
  13271. (if (looking-at ".*?|")
  13272. (let ((pos (point))
  13273. (noalign (looking-at "[^|\n\r]* |"))
  13274. (c org-table-may-need-update))
  13275. (replace-match (concat
  13276. (substring (match-string 0) 1 -1)
  13277. " |"))
  13278. (goto-char pos)
  13279. ;; noalign: if there were two spaces at the end, this field
  13280. ;; does not determine the width of the column.
  13281. (if noalign (setq org-table-may-need-update c)))
  13282. (delete-char N))
  13283. (delete-char N)
  13284. (org-fix-tags-on-the-fly)))
  13285. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  13286. (put 'org-self-insert-command 'delete-selection t)
  13287. (put 'orgtbl-self-insert-command 'delete-selection t)
  13288. (put 'org-delete-char 'delete-selection 'supersede)
  13289. (put 'org-delete-backward-char 'delete-selection 'supersede)
  13290. (put 'org-yank 'delete-selection 'yank)
  13291. ;; Make `flyspell-mode' delay after some commands
  13292. (put 'org-self-insert-command 'flyspell-delayed t)
  13293. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  13294. (put 'org-delete-char 'flyspell-delayed t)
  13295. (put 'org-delete-backward-char 'flyspell-delayed t)
  13296. ;; Make pabbrev-mode expand after org-mode commands
  13297. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  13298. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  13299. ;; How to do this: Measure non-white length of current string
  13300. ;; If equal to column width, we should realign.
  13301. (defun org-remap (map &rest commands)
  13302. "In MAP, remap the functions given in COMMANDS.
  13303. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  13304. (let (new old)
  13305. (while commands
  13306. (setq old (pop commands) new (pop commands))
  13307. (if (fboundp 'command-remapping)
  13308. (org-defkey map (vector 'remap old) new)
  13309. (substitute-key-definition old new map global-map)))))
  13310. (when (eq org-enable-table-editor 'optimized)
  13311. ;; If the user wants maximum table support, we need to hijack
  13312. ;; some standard editing functions
  13313. (org-remap org-mode-map
  13314. 'self-insert-command 'org-self-insert-command
  13315. 'delete-char 'org-delete-char
  13316. 'delete-backward-char 'org-delete-backward-char)
  13317. (org-defkey org-mode-map "|" 'org-force-self-insert))
  13318. (defvar org-ctrl-c-ctrl-c-hook nil
  13319. "Hook for functions attaching themselves to `C-c C-c'.
  13320. This can be used to add additional functionality to the C-c C-c key which
  13321. executes context-dependent commands.
  13322. Each function will be called with no arguments. The function must check
  13323. if the context is appropriate for it to act. If yes, it should do its
  13324. thing and then return a non-nil value. If the context is wrong,
  13325. just do nothing and return nil.")
  13326. (defvar org-tab-first-hook nil
  13327. "Hook for functions to attach themselves to TAB.
  13328. See `org-ctrl-c-ctrl-c-hook' for more information.
  13329. This hook runs as the first action when TAB is pressed, even before
  13330. `org-cycle' messes around with the `outline-regexp' to cater for
  13331. inline tasks and plain list item folding.
  13332. If any function in this hook returns t, not other actions like table
  13333. field motion visibility cycling will be done.")
  13334. (defvar org-tab-after-check-for-table-hook nil
  13335. "Hook for functions to attach themselves to TAB.
  13336. See `org-ctrl-c-ctrl-c-hook' for more information.
  13337. This hook runs after it has been established that the cursor is not in a
  13338. table, but before checking if the cursor is in a headline or if global cycling
  13339. should be done.
  13340. If any function in this hook returns t, not other actions like visibility
  13341. cycling will be done.")
  13342. (defvar org-tab-after-check-for-cycling-hook nil
  13343. "Hook for functions to attach themselves to TAB.
  13344. See `org-ctrl-c-ctrl-c-hook' for more information.
  13345. This hook runs after it has been established that not table field motion and
  13346. not visibility should be done because of current context. This is probably
  13347. the place where a package like yasnippets can hook in.")
  13348. (defvar org-metaleft-hook nil
  13349. "Hook for functions attaching themselves to `M-left'.
  13350. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13351. (defvar org-metaright-hook nil
  13352. "Hook for functions attaching themselves to `M-right'.
  13353. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13354. (defvar org-metaup-hook nil
  13355. "Hook for functions attaching themselves to `M-up'.
  13356. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13357. (defvar org-metadown-hook nil
  13358. "Hook for functions attaching themselves to `M-down'.
  13359. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13360. (defvar org-shiftmetaleft-hook nil
  13361. "Hook for functions attaching themselves to `M-S-left'.
  13362. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13363. (defvar org-shiftmetaright-hook nil
  13364. "Hook for functions attaching themselves to `M-S-right'.
  13365. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13366. (defvar org-shiftmetaup-hook nil
  13367. "Hook for functions attaching themselves to `M-S-up'.
  13368. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13369. (defvar org-shiftmetadown-hook nil
  13370. "Hook for functions attaching themselves to `M-S-down'.
  13371. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13372. (defvar org-metareturn-hook nil
  13373. "Hook for functions attaching themselves to `M-RET'.
  13374. See `org-ctrl-c-ctrl-c-hook' for more information.")
  13375. (defun org-modifier-cursor-error ()
  13376. "Throw an error, a modified cursor command was applied in wrong context."
  13377. (error "This command is active in special context like tables, headlines or items"))
  13378. (defun org-shiftselect-error ()
  13379. "Throw an error because Shift-Cursor command was applied in wrong context."
  13380. (if (and (boundp 'shift-select-mode) shift-select-mode)
  13381. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  13382. (error "This command works only in special context like headlines or timestamps.")))
  13383. (defun org-call-for-shift-select (cmd)
  13384. (let ((this-command-keys-shift-translated t))
  13385. (call-interactively cmd)))
  13386. (defun org-shifttab (&optional arg)
  13387. "Global visibility cycling or move to previous table field.
  13388. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13389. on context.
  13390. See the individual commands for more information."
  13391. (interactive "P")
  13392. (cond
  13393. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13394. ((integerp arg)
  13395. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  13396. (message "Content view to level: %d" arg)
  13397. (org-content (prefix-numeric-value arg2))
  13398. (setq org-cycle-global-status 'overview)))
  13399. (t (call-interactively 'org-global-cycle))))
  13400. (defun org-shiftmetaleft ()
  13401. "Promote subtree or delete table column.
  13402. Calls `org-promote-subtree', `org-outdent-item',
  13403. or `org-table-delete-column', depending on context.
  13404. See the individual commands for more information."
  13405. (interactive)
  13406. (cond
  13407. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  13408. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13409. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13410. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13411. (t (org-modifier-cursor-error))))
  13412. (defun org-shiftmetaright ()
  13413. "Demote subtree or insert table column.
  13414. Calls `org-demote-subtree', `org-indent-item',
  13415. or `org-table-insert-column', depending on context.
  13416. See the individual commands for more information."
  13417. (interactive)
  13418. (cond
  13419. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  13420. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13421. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13422. ((org-at-item-p) (call-interactively 'org-indent-item))
  13423. (t (org-modifier-cursor-error))))
  13424. (defun org-shiftmetaup (&optional arg)
  13425. "Move subtree up or kill table row.
  13426. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13427. `org-move-item-up' depending on context. See the individual commands
  13428. for more information."
  13429. (interactive "P")
  13430. (cond
  13431. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  13432. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13433. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13434. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13435. (t (org-modifier-cursor-error))))
  13436. (defun org-shiftmetadown (&optional arg)
  13437. "Move subtree down or insert table row.
  13438. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13439. `org-move-item-down', depending on context. See the individual
  13440. commands for more information."
  13441. (interactive "P")
  13442. (cond
  13443. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  13444. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13445. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13446. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13447. (t (org-modifier-cursor-error))))
  13448. (defun org-metaleft (&optional arg)
  13449. "Promote heading or move table column to left.
  13450. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13451. With no specific context, calls the Emacs default `backward-word'.
  13452. See the individual commands for more information."
  13453. (interactive "P")
  13454. (cond
  13455. ((run-hook-with-args-until-success 'org-metaleft-hook))
  13456. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13457. ((or (org-on-heading-p)
  13458. (and (org-region-active-p)
  13459. (save-excursion
  13460. (goto-char (region-beginning))
  13461. (org-on-heading-p))))
  13462. (call-interactively 'org-do-promote))
  13463. ((or (org-at-item-p)
  13464. (and (org-region-active-p)
  13465. (save-excursion
  13466. (goto-char (region-beginning))
  13467. (org-at-item-p))))
  13468. (call-interactively 'org-outdent-item))
  13469. (t (call-interactively 'backward-word))))
  13470. (defun org-metaright (&optional arg)
  13471. "Demote subtree or move table column to right.
  13472. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13473. With no specific context, calls the Emacs default `forward-word'.
  13474. See the individual commands for more information."
  13475. (interactive "P")
  13476. (cond
  13477. ((run-hook-with-args-until-success 'org-metaright-hook))
  13478. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13479. ((or (org-on-heading-p)
  13480. (and (org-region-active-p)
  13481. (save-excursion
  13482. (goto-char (region-beginning))
  13483. (org-on-heading-p))))
  13484. (call-interactively 'org-do-demote))
  13485. ((or (org-at-item-p)
  13486. (and (org-region-active-p)
  13487. (save-excursion
  13488. (goto-char (region-beginning))
  13489. (org-at-item-p))))
  13490. (call-interactively 'org-indent-item))
  13491. (t (call-interactively 'forward-word))))
  13492. (defun org-metaup (&optional arg)
  13493. "Move subtree up or move table row up.
  13494. Calls `org-move-subtree-up' or `org-table-move-row' or
  13495. `org-move-item-up', depending on context. See the individual commands
  13496. for more information."
  13497. (interactive "P")
  13498. (cond
  13499. ((run-hook-with-args-until-success 'org-metaup-hook))
  13500. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13501. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13502. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13503. (t (transpose-lines 1) (beginning-of-line -1))))
  13504. (defun org-metadown (&optional arg)
  13505. "Move subtree down or move table row down.
  13506. Calls `org-move-subtree-down' or `org-table-move-row' or
  13507. `org-move-item-down', depending on context. See the individual
  13508. commands for more information."
  13509. (interactive "P")
  13510. (cond
  13511. ((run-hook-with-args-until-success 'org-metadown-hook))
  13512. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13513. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13514. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13515. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  13516. (defun org-shiftup (&optional arg)
  13517. "Increase item in timestamp or increase priority of current headline.
  13518. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  13519. depending on context. See the individual commands for more information."
  13520. (interactive "P")
  13521. (cond
  13522. ((and org-support-shift-select (org-region-active-p))
  13523. (org-call-for-shift-select 'previous-line))
  13524. ((org-at-timestamp-p t)
  13525. (call-interactively (if org-edit-timestamp-down-means-later
  13526. 'org-timestamp-down 'org-timestamp-up)))
  13527. ((and (not (eq org-support-shift-select 'always))
  13528. org-enable-priority-commands
  13529. (org-on-heading-p))
  13530. (call-interactively 'org-priority-up))
  13531. ((and (not org-support-shift-select) (org-at-item-p))
  13532. (call-interactively 'org-previous-item))
  13533. ((org-clocktable-try-shift 'up arg))
  13534. (org-support-shift-select
  13535. (org-call-for-shift-select 'previous-line))
  13536. (t (org-shiftselect-error))))
  13537. (defun org-shiftdown (&optional arg)
  13538. "Decrease item in timestamp or decrease priority of current headline.
  13539. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  13540. depending on context. See the individual commands for more information."
  13541. (interactive "P")
  13542. (cond
  13543. ((and org-support-shift-select (org-region-active-p))
  13544. (org-call-for-shift-select 'next-line))
  13545. ((org-at-timestamp-p t)
  13546. (call-interactively (if org-edit-timestamp-down-means-later
  13547. 'org-timestamp-up 'org-timestamp-down)))
  13548. ((and (not (eq org-support-shift-select 'always))
  13549. org-enable-priority-commands
  13550. (org-on-heading-p))
  13551. (call-interactively 'org-priority-down))
  13552. ((and (not org-support-shift-select) (org-at-item-p))
  13553. (call-interactively 'org-next-item))
  13554. ((org-clocktable-try-shift 'down arg))
  13555. (org-support-shift-select
  13556. (org-call-for-shift-select 'next-line))
  13557. (t (org-shiftselect-error))))
  13558. (defun org-shiftright (&optional arg)
  13559. "Cycle the thing at point or in the current line, depending on context.
  13560. Depending on context, this does one of the following:
  13561. - switch a timestamp at point one day into the future
  13562. - on a headline, switch to the next TODO keyword.
  13563. - on an item, switch entire list to the next bullet type
  13564. - on a property line, switch to the next allowed value
  13565. - on a clocktable definition line, move time block into the future"
  13566. (interactive "P")
  13567. (cond
  13568. ((and org-support-shift-select (org-region-active-p))
  13569. (org-call-for-shift-select 'forward-char))
  13570. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  13571. ((and (not (eq org-support-shift-select 'always))
  13572. (org-on-heading-p))
  13573. (let ((org-inhibit-logging
  13574. (not org-treat-S-cursor-todo-selection-as-state-change))
  13575. (org-inhibit-blocking
  13576. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13577. (org-call-with-arg 'org-todo 'right)))
  13578. ((or (and org-support-shift-select
  13579. (not (eq org-support-shift-select 'always))
  13580. (org-at-item-bullet-p))
  13581. (and (not org-support-shift-select) (org-at-item-p)))
  13582. (org-call-with-arg 'org-cycle-list-bullet nil))
  13583. ((and (not (eq org-support-shift-select 'always))
  13584. (org-at-property-p))
  13585. (call-interactively 'org-property-next-allowed-value))
  13586. ((org-clocktable-try-shift 'right arg))
  13587. (org-support-shift-select
  13588. (org-call-for-shift-select 'forward-char))
  13589. (t (org-shiftselect-error))))
  13590. (defun org-shiftleft (&optional arg)
  13591. "Cycle the thing at point or in the current line, depending on context.
  13592. Depending on context, this does one of the following:
  13593. - switch a timestamp at point one day into the past
  13594. - on a headline, switch to the previous TODO keyword.
  13595. - on an item, switch entire list to the previous bullet type
  13596. - on a property line, switch to the previous allowed value
  13597. - on a clocktable definition line, move time block into the past"
  13598. (interactive "P")
  13599. (cond
  13600. ((and org-support-shift-select (org-region-active-p))
  13601. (org-call-for-shift-select 'backward-char))
  13602. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  13603. ((and (not (eq org-support-shift-select 'always))
  13604. (org-on-heading-p))
  13605. (let ((org-inhibit-logging
  13606. (not org-treat-S-cursor-todo-selection-as-state-change))
  13607. (org-inhibit-blocking
  13608. (not org-treat-S-cursor-todo-selection-as-state-change)))
  13609. (org-call-with-arg 'org-todo 'left)))
  13610. ((or (and org-support-shift-select
  13611. (not (eq org-support-shift-select 'always))
  13612. (org-at-item-bullet-p))
  13613. (and (not org-support-shift-select) (org-at-item-p)))
  13614. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  13615. ((and (not (eq org-support-shift-select 'always))
  13616. (org-at-property-p))
  13617. (call-interactively 'org-property-previous-allowed-value))
  13618. ((org-clocktable-try-shift 'left arg))
  13619. (org-support-shift-select
  13620. (org-call-for-shift-select 'backward-char))
  13621. (t (org-shiftselect-error))))
  13622. (defun org-shiftcontrolright ()
  13623. "Switch to next TODO set."
  13624. (interactive)
  13625. (cond
  13626. ((and org-support-shift-select (org-region-active-p))
  13627. (org-call-for-shift-select 'forward-word))
  13628. ((and (not (eq org-support-shift-select 'always))
  13629. (org-on-heading-p))
  13630. (org-call-with-arg 'org-todo 'nextset))
  13631. (org-support-shift-select
  13632. (org-call-for-shift-select 'forward-word))
  13633. (t (org-shiftselect-error))))
  13634. (defun org-shiftcontrolleft ()
  13635. "Switch to previous TODO set."
  13636. (interactive)
  13637. (cond
  13638. ((and org-support-shift-select (org-region-active-p))
  13639. (org-call-for-shift-select 'backward-word))
  13640. ((and (not (eq org-support-shift-select 'always))
  13641. (org-on-heading-p))
  13642. (org-call-with-arg 'org-todo 'previousset))
  13643. (org-support-shift-select
  13644. (org-call-for-shift-select 'backward-word))
  13645. (t (org-shiftselect-error))))
  13646. (defun org-ctrl-c-ret ()
  13647. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  13648. (interactive)
  13649. (cond
  13650. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  13651. (t (call-interactively 'org-insert-heading))))
  13652. (defun org-copy-special ()
  13653. "Copy region in table or copy current subtree.
  13654. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13655. See the individual commands for more information."
  13656. (interactive)
  13657. (call-interactively
  13658. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13659. (defun org-cut-special ()
  13660. "Cut region in table or cut current subtree.
  13661. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13662. See the individual commands for more information."
  13663. (interactive)
  13664. (call-interactively
  13665. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13666. (defun org-paste-special (arg)
  13667. "Paste rectangular region into table, or past subtree relative to level.
  13668. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13669. See the individual commands for more information."
  13670. (interactive "P")
  13671. (if (org-at-table-p)
  13672. (org-table-paste-rectangle)
  13673. (org-paste-subtree arg)))
  13674. (defun org-edit-special ()
  13675. "Call a special editor for the stuff at point.
  13676. When at a table, call the formula editor with `org-table-edit-formulas'.
  13677. When at the first line of an src example, call `org-edit-src-code'.
  13678. When in an #+include line, visit the include file. Otherwise call
  13679. `ffap' to visit the file at point."
  13680. (interactive)
  13681. (cond
  13682. ((org-at-table-p)
  13683. (call-interactively 'org-table-edit-formulas))
  13684. ((save-excursion
  13685. (beginning-of-line 1)
  13686. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  13687. (find-file (org-trim (match-string 1))))
  13688. ((org-edit-src-code))
  13689. ((org-edit-fixed-width-region))
  13690. (t (call-interactively 'ffap))))
  13691. (defun org-ctrl-c-ctrl-c (&optional arg)
  13692. "Set tags in headline, or update according to changed information at point.
  13693. This command does many different things, depending on context:
  13694. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  13695. this is what we do.
  13696. - If the cursor is on a statistics cookie, update it.
  13697. - If the cursor is in a headline, prompt for tags and insert them
  13698. into the current line, aligned to `org-tags-column'. When called
  13699. with prefix arg, realign all tags in the current buffer.
  13700. - If the cursor is in one of the special #+KEYWORD lines, this
  13701. triggers scanning the buffer for these lines and updating the
  13702. information.
  13703. - If the cursor is inside a table, realign the table. This command
  13704. works even if the automatic table editor has been turned off.
  13705. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13706. the entire table.
  13707. - If the cursor is at a footnote reference or definition, jump to
  13708. the corresponding definition or references, respectively.
  13709. - If the cursor is a the beginning of a dynamic block, update it.
  13710. - If the cursor is inside a table created by the table.el package,
  13711. activate that table.
  13712. - If the current buffer is a remember buffer, close note and file
  13713. it. A prefix argument of 1 files to the default location
  13714. without further interaction. A prefix argument of 2 files to
  13715. the currently clocking task.
  13716. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13717. links in this buffer.
  13718. - If the cursor is on a numbered item in a plain list, renumber the
  13719. ordered list.
  13720. - If the cursor is on a checkbox, toggle it."
  13721. (interactive "P")
  13722. (let ((org-enable-table-editor t))
  13723. (cond
  13724. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  13725. org-occur-highlights
  13726. org-latex-fragment-image-overlays)
  13727. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  13728. (org-remove-occur-highlights)
  13729. (org-remove-latex-fragment-image-overlays)
  13730. (message "Temporary highlights/overlays removed from current buffer"))
  13731. ((and (local-variable-p 'org-finish-function (current-buffer))
  13732. (fboundp org-finish-function))
  13733. (funcall org-finish-function))
  13734. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  13735. ((org-at-property-p)
  13736. (call-interactively 'org-property-action))
  13737. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13738. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  13739. (or (org-on-heading-p) (org-at-item-p)))
  13740. (call-interactively 'org-update-statistics-cookies))
  13741. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13742. ((org-at-table.el-p)
  13743. (require 'table)
  13744. (beginning-of-line 1)
  13745. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13746. (call-interactively 'table-recognize-table))
  13747. ((org-at-table-p)
  13748. (org-table-maybe-eval-formula)
  13749. (if arg
  13750. (call-interactively 'org-table-recalculate)
  13751. (org-table-maybe-recalculate-line))
  13752. (call-interactively 'org-table-align))
  13753. ((or (org-footnote-at-reference-p)
  13754. (org-footnote-at-definition-p))
  13755. (call-interactively 'org-footnote-action))
  13756. ((org-at-item-checkbox-p)
  13757. (call-interactively 'org-toggle-checkbox))
  13758. ((org-at-item-p)
  13759. (if arg
  13760. (call-interactively 'org-toggle-checkbox)
  13761. (call-interactively 'org-maybe-renumber-ordered-list)))
  13762. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  13763. ;; Dynamic block
  13764. (beginning-of-line 1)
  13765. (save-excursion (org-update-dblock)))
  13766. ((save-excursion
  13767. (beginning-of-line 1)
  13768. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  13769. (cond
  13770. ((equal (match-string 1) "TBLFM")
  13771. ;; Recalculate the table before this line
  13772. (save-excursion
  13773. (beginning-of-line 1)
  13774. (skip-chars-backward " \r\n\t")
  13775. (if (org-at-table-p)
  13776. (org-call-with-arg 'org-table-recalculate t))))
  13777. (t
  13778. ; (org-set-regexps-and-options)
  13779. ; (org-restart-font-lock)
  13780. (let ((org-inhibit-startup t)) (org-mode-restart))
  13781. (message "Local setup has been refreshed"))))
  13782. ((org-clock-update-time-maybe))
  13783. (t (error "C-c C-c can do nothing useful at this location.")))))
  13784. (defun org-mode-restart ()
  13785. "Restart Org-mode, to scan again for special lines.
  13786. Also updates the keyword regular expressions."
  13787. (interactive)
  13788. (org-mode)
  13789. (message "Org-mode restarted"))
  13790. (defun org-kill-note-or-show-branches ()
  13791. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  13792. (interactive)
  13793. (if (not org-finish-function)
  13794. (call-interactively 'show-branches)
  13795. (let ((org-note-abort t))
  13796. (funcall org-finish-function))))
  13797. (defun org-return (&optional indent)
  13798. "Goto next table row or insert a newline.
  13799. Calls `org-table-next-row' or `newline', depending on context.
  13800. See the individual commands for more information."
  13801. (interactive)
  13802. (cond
  13803. ((bobp) (if indent (newline-and-indent) (newline)))
  13804. ((org-at-table-p)
  13805. (org-table-justify-field-maybe)
  13806. (call-interactively 'org-table-next-row))
  13807. ((and org-return-follows-link
  13808. (eq (get-text-property (point) 'face) 'org-link))
  13809. (call-interactively 'org-open-at-point))
  13810. ((and (org-at-heading-p)
  13811. (looking-at
  13812. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  13813. (org-show-entry)
  13814. (end-of-line 1)
  13815. (newline))
  13816. (t (if indent (newline-and-indent) (newline)))))
  13817. (defun org-return-indent ()
  13818. "Goto next table row or insert a newline and indent.
  13819. Calls `org-table-next-row' or `newline-and-indent', depending on
  13820. context. See the individual commands for more information."
  13821. (interactive)
  13822. (org-return t))
  13823. (defun org-ctrl-c-star ()
  13824. "Compute table, or change heading status of lines.
  13825. Calls `org-table-recalculate' or `org-toggle-heading',
  13826. depending on context."
  13827. (interactive)
  13828. (cond
  13829. ((org-at-table-p)
  13830. (call-interactively 'org-table-recalculate))
  13831. (t
  13832. ;; Convert all lines in region to list items
  13833. (call-interactively 'org-toggle-heading))))
  13834. (defun org-ctrl-c-minus ()
  13835. "Insert separator line in table or modify bullet status of line.
  13836. Also turns a plain line or a region of lines into list items.
  13837. Calls `org-table-insert-hline', `org-toggle-item', or
  13838. `org-cycle-list-bullet', depending on context."
  13839. (interactive)
  13840. (cond
  13841. ((org-at-table-p)
  13842. (call-interactively 'org-table-insert-hline))
  13843. ((org-region-active-p)
  13844. (call-interactively 'org-toggle-item))
  13845. ((org-in-item-p)
  13846. (call-interactively 'org-cycle-list-bullet))
  13847. (t
  13848. (call-interactively 'org-toggle-item))))
  13849. (defun org-toggle-item ()
  13850. "Convert headings or normal lines to items, items to normal lines.
  13851. If there is no active region, only the current line is considered.
  13852. If the first line in the region is a headline, convert all headlines to items.
  13853. If the first line in the region is an item, convert all items to normal lines.
  13854. If the first line is normal text, add an item bullet to each line."
  13855. (interactive)
  13856. (let (l2 l beg end)
  13857. (if (org-region-active-p)
  13858. (setq beg (region-beginning) end (region-end))
  13859. (setq beg (point-at-bol)
  13860. end (min (1+ (point-at-eol)) (point-max))))
  13861. (save-excursion
  13862. (goto-char end)
  13863. (setq l2 (org-current-line))
  13864. (goto-char beg)
  13865. (beginning-of-line 1)
  13866. (setq l (1- (org-current-line)))
  13867. (if (org-at-item-p)
  13868. ;; We already have items, de-itemize
  13869. (while (< (setq l (1+ l)) l2)
  13870. (when (org-at-item-p)
  13871. (goto-char (match-beginning 2))
  13872. (delete-region (match-beginning 2) (match-end 2))
  13873. (and (looking-at "[ \t]+") (replace-match "")))
  13874. (beginning-of-line 2))
  13875. (if (org-on-heading-p)
  13876. ;; Headings, convert to items
  13877. (while (< (setq l (1+ l)) l2)
  13878. (if (looking-at org-outline-regexp)
  13879. (replace-match "- " t t))
  13880. (beginning-of-line 2))
  13881. ;; normal lines, turn them into items
  13882. (while (< (setq l (1+ l)) l2)
  13883. (unless (org-at-item-p)
  13884. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13885. (replace-match "\\1- \\2")))
  13886. (beginning-of-line 2)))))))
  13887. (defun org-toggle-heading (&optional nstars)
  13888. "Convert headings to normal text, or items or text to headings.
  13889. If there is no active region, only the current line is considered.
  13890. If the first line is a heading, remove the stars from all headlines
  13891. in the region.
  13892. If the first line is a plain list item, turn all plain list items
  13893. into headings.
  13894. If the first line is a normal line, turn each and every line in the
  13895. region into a heading.
  13896. When converting a line into a heading, the number of stars is chosen
  13897. such that the lines become children of the current entry. However,
  13898. when a prefix argument is given, its value determines the number of
  13899. stars to add."
  13900. (interactive "P")
  13901. (let (l2 l itemp beg end)
  13902. (if (org-region-active-p)
  13903. (setq beg (region-beginning) end (region-end))
  13904. (setq beg (point-at-bol)
  13905. end (min (1+ (point-at-eol)) (point-max))))
  13906. (save-excursion
  13907. (goto-char end)
  13908. (setq l2 (org-current-line))
  13909. (goto-char beg)
  13910. (beginning-of-line 1)
  13911. (setq l (1- (org-current-line)))
  13912. (if (org-on-heading-p)
  13913. ;; We already have headlines, de-star them
  13914. (while (< (setq l (1+ l)) l2)
  13915. (when (org-on-heading-p t)
  13916. (and (looking-at outline-regexp) (replace-match "")))
  13917. (beginning-of-line 2))
  13918. (setq itemp (org-at-item-p))
  13919. (let* ((stars
  13920. (if nstars
  13921. (make-string (prefix-numeric-value current-prefix-arg)
  13922. ?*)
  13923. (save-excursion
  13924. (if (re-search-backward org-complex-heading-regexp nil t)
  13925. (match-string 1) ""))))
  13926. (add-stars (cond (nstars "")
  13927. ((equal stars "") "*")
  13928. (org-odd-levels-only "**")
  13929. (t "*")))
  13930. (rpl (concat stars add-stars " ")))
  13931. (while (< (setq l (1+ l)) l2)
  13932. (if itemp
  13933. (and (org-at-item-p) (replace-match rpl t t))
  13934. (unless (org-on-heading-p)
  13935. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  13936. (replace-match (concat rpl (match-string 2))))))
  13937. (beginning-of-line 2)))))))
  13938. (defun org-meta-return (&optional arg)
  13939. "Insert a new heading or wrap a region in a table.
  13940. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13941. See the individual commands for more information."
  13942. (interactive "P")
  13943. (cond
  13944. ((run-hook-with-args-until-success 'org-metareturn-hook))
  13945. ((org-at-table-p)
  13946. (call-interactively 'org-table-wrap-region))
  13947. (t (call-interactively 'org-insert-heading))))
  13948. ;;; Menu entries
  13949. ;; Define the Org-mode menus
  13950. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13951. '("Tbl"
  13952. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  13953. ["Next Field" org-cycle (org-at-table-p)]
  13954. ["Previous Field" org-shifttab (org-at-table-p)]
  13955. ["Next Row" org-return (org-at-table-p)]
  13956. "--"
  13957. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13958. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13959. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13960. "--"
  13961. ("Column"
  13962. ["Move Column Left" org-metaleft (org-at-table-p)]
  13963. ["Move Column Right" org-metaright (org-at-table-p)]
  13964. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13965. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  13966. ("Row"
  13967. ["Move Row Up" org-metaup (org-at-table-p)]
  13968. ["Move Row Down" org-metadown (org-at-table-p)]
  13969. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13970. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13971. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13972. "--"
  13973. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  13974. ("Rectangle"
  13975. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13976. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13977. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13978. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13979. "--"
  13980. ("Calculate"
  13981. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13982. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13983. ["Edit Formulas" org-edit-special (org-at-table-p)]
  13984. "--"
  13985. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13986. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13987. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  13988. "--"
  13989. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13990. "--"
  13991. ["Sum Column/Rectangle" org-table-sum
  13992. (or (org-at-table-p) (org-region-active-p))]
  13993. ["Which Column?" org-table-current-column (org-at-table-p)])
  13994. ["Debug Formulas"
  13995. org-table-toggle-formula-debugger
  13996. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  13997. ["Show Col/Row Numbers"
  13998. org-table-toggle-coordinate-overlays
  13999. :style toggle
  14000. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  14001. "--"
  14002. ["Create" org-table-create (and (not (org-at-table-p))
  14003. org-enable-table-editor)]
  14004. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  14005. ["Import from File" org-table-import (not (org-at-table-p))]
  14006. ["Export to File" org-table-export (org-at-table-p)]
  14007. "--"
  14008. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  14009. (easy-menu-define org-org-menu org-mode-map "Org menu"
  14010. '("Org"
  14011. ("Show/Hide"
  14012. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  14013. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  14014. ["Sparse Tree..." org-sparse-tree t]
  14015. ["Reveal Context" org-reveal t]
  14016. ["Show All" show-all t]
  14017. "--"
  14018. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  14019. "--"
  14020. ["New Heading" org-insert-heading t]
  14021. ("Navigate Headings"
  14022. ["Up" outline-up-heading t]
  14023. ["Next" outline-next-visible-heading t]
  14024. ["Previous" outline-previous-visible-heading t]
  14025. ["Next Same Level" outline-forward-same-level t]
  14026. ["Previous Same Level" outline-backward-same-level t]
  14027. "--"
  14028. ["Jump" org-goto t])
  14029. ("Edit Structure"
  14030. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  14031. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  14032. "--"
  14033. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  14034. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  14035. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  14036. "--"
  14037. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  14038. "--"
  14039. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  14040. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  14041. ["Demote Heading" org-metaright (not (org-at-table-p))]
  14042. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  14043. "--"
  14044. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  14045. "--"
  14046. ["Convert to odd levels" org-convert-to-odd-levels t]
  14047. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  14048. ("Editing"
  14049. ["Emphasis..." org-emphasize t]
  14050. ["Edit Source Example" org-edit-special t]
  14051. "--"
  14052. ["Footnote new/jump" org-footnote-action t]
  14053. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  14054. ("Archive"
  14055. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  14056. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  14057. ; :active t :keys "C-u C-c C-x C-a"]
  14058. ["Sparse trees open ARCHIVE trees"
  14059. (setq org-sparse-tree-open-archived-trees
  14060. (not org-sparse-tree-open-archived-trees))
  14061. :style toggle :selected org-sparse-tree-open-archived-trees]
  14062. ["Cycling opens ARCHIVE trees"
  14063. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  14064. :style toggle :selected org-cycle-open-archived-trees]
  14065. "--"
  14066. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  14067. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  14068. ; ["Check and Move Children" (org-archive-subtree '(4))
  14069. ; :active t :keys "C-u C-c C-x C-s"]
  14070. )
  14071. "--"
  14072. ("Hyperlinks"
  14073. ["Store Link (Global)" org-store-link t]
  14074. ["Find existing link to here" org-occur-link-in-agenda-files t]
  14075. ["Insert Link" org-insert-link t]
  14076. ["Follow Link" org-open-at-point t]
  14077. "--"
  14078. ["Next link" org-next-link t]
  14079. ["Previous link" org-previous-link t]
  14080. "--"
  14081. ["Descriptive Links"
  14082. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  14083. :style radio
  14084. :selected (member '(org-link) buffer-invisibility-spec)]
  14085. ["Literal Links"
  14086. (progn
  14087. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  14088. :style radio
  14089. :selected (not (member '(org-link) buffer-invisibility-spec))])
  14090. "--"
  14091. ("TODO Lists"
  14092. ["TODO/DONE/-" org-todo t]
  14093. ("Select keyword"
  14094. ["Next keyword" org-shiftright (org-on-heading-p)]
  14095. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14096. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  14097. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  14098. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  14099. ["Show TODO Tree" org-show-todo-tree t]
  14100. ["Global TODO list" org-todo-list t]
  14101. "--"
  14102. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  14103. :selected org-enforce-todo-dependencies :style toggle :active t]
  14104. "Settings for tree at point"
  14105. ["Do Children sequentially" org-toggle-ordered-property :style radio
  14106. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  14107. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14108. ["Do Children parallel" org-toggle-ordered-property :style radio
  14109. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  14110. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  14111. "--"
  14112. ["Set Priority" org-priority t]
  14113. ["Priority Up" org-shiftup t]
  14114. ["Priority Down" org-shiftdown t]
  14115. "--"
  14116. ["Get news from all feeds" org-feed-update-all t]
  14117. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  14118. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  14119. ("TAGS and Properties"
  14120. ["Set Tags" org-set-tags-command t]
  14121. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  14122. "--"
  14123. ["Set property" org-set-property t]
  14124. ["Column view of properties" org-columns t]
  14125. ["Insert Column View DBlock" org-insert-columns-dblock t])
  14126. ("Dates and Scheduling"
  14127. ["Timestamp" org-time-stamp t]
  14128. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14129. ("Change Date"
  14130. ["1 Day Later" org-shiftright t]
  14131. ["1 Day Earlier" org-shiftleft t]
  14132. ["1 ... Later" org-shiftup t]
  14133. ["1 ... Earlier" org-shiftdown t])
  14134. ["Compute Time Range" org-evaluate-time-range t]
  14135. ["Schedule Item" org-schedule t]
  14136. ["Deadline" org-deadline t]
  14137. "--"
  14138. ["Custom time format" org-toggle-time-stamp-overlays
  14139. :style radio :selected org-display-custom-times]
  14140. "--"
  14141. ["Goto Calendar" org-goto-calendar t]
  14142. ["Date from Calendar" org-date-from-calendar t]
  14143. "--"
  14144. ["Start/Restart Timer" org-timer-start t]
  14145. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  14146. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  14147. ["Insert Timer String" org-timer t]
  14148. ["Insert Timer Item" org-timer-item t])
  14149. ("Logging work"
  14150. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  14151. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  14152. ["Clock out" org-clock-out t]
  14153. ["Clock cancel" org-clock-cancel t]
  14154. "--"
  14155. ["Mark as default task" org-clock-mark-default-task t]
  14156. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  14157. ["Goto running clock" org-clock-goto t]
  14158. "--"
  14159. ["Display times" org-clock-display t]
  14160. ["Create clock table" org-clock-report t]
  14161. "--"
  14162. ["Record DONE time"
  14163. (progn (setq org-log-done (not org-log-done))
  14164. (message "Switching to %s will %s record a timestamp"
  14165. (car org-done-keywords)
  14166. (if org-log-done "automatically" "not")))
  14167. :style toggle :selected org-log-done])
  14168. "--"
  14169. ["Agenda Command..." org-agenda t]
  14170. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  14171. ("File List for Agenda")
  14172. ("Special views current file"
  14173. ["TODO Tree" org-show-todo-tree t]
  14174. ["Check Deadlines" org-check-deadlines t]
  14175. ["Timeline" org-timeline t]
  14176. ["Tags/Property tree" org-match-sparse-tree t])
  14177. "--"
  14178. ["Export/Publish..." org-export t]
  14179. ("LaTeX"
  14180. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  14181. :selected org-cdlatex-mode]
  14182. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  14183. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  14184. ["Modify math symbol" org-cdlatex-math-modify
  14185. (org-inside-LaTeX-fragment-p)]
  14186. ["Insert citation" org-reftex-citation t]
  14187. "--"
  14188. ["Export LaTeX fragments as images"
  14189. (if (featurep 'org-exp)
  14190. (setq org-export-with-LaTeX-fragments
  14191. (not org-export-with-LaTeX-fragments))
  14192. (require 'org-exp))
  14193. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  14194. org-export-with-LaTeX-fragments)])
  14195. "--"
  14196. ("Documentation"
  14197. ["Show Version" org-version t]
  14198. ["Info Documentation" org-info t])
  14199. ("Customize"
  14200. ["Browse Org Group" org-customize t]
  14201. "--"
  14202. ["Expand This Menu" org-create-customize-menu
  14203. (fboundp 'customize-menu-create)])
  14204. ["Send bug report" org-submit-bug-report t]
  14205. "--"
  14206. ("Refresh/Reload"
  14207. ["Refresh setup current buffer" org-mode-restart t]
  14208. ["Reload Org (after update)" org-reload t]
  14209. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  14210. ))
  14211. (defun org-info (&optional node)
  14212. "Read documentation for Org-mode in the info system.
  14213. With optional NODE, go directly to that node."
  14214. (interactive)
  14215. (info (format "(org)%s" (or node ""))))
  14216. ;;;###autoload
  14217. (defun org-submit-bug-report ()
  14218. "Submit a bug report on Org-mode via mail.
  14219. Don't hesitate to report any problems or inaccurate documentation.
  14220. If you don't have setup sending mail from (X)Emacs, please copy the
  14221. output buffer into your mail program, as it gives us important
  14222. information about your Org-mode version and configuration."
  14223. (interactive)
  14224. (require 'reporter)
  14225. (org-load-modules-maybe)
  14226. (org-require-autoloaded-modules)
  14227. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  14228. (reporter-submit-bug-report
  14229. "emacs-orgmode@gnu.org"
  14230. (org-version)
  14231. (let (list)
  14232. (save-window-excursion
  14233. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  14234. (delete-other-windows)
  14235. (erase-buffer)
  14236. (insert "You are about to submit a bug report to the Org-mode mailing list.
  14237. We would like to add your full Org-mode and Outline configuration to the
  14238. bug report. This greatly simplifies the work of the maintainer and
  14239. other experts on the mailing list.
  14240. HOWEVER, some variables you have customized may contain private
  14241. information. The names of customers, colleagues, or friends, might
  14242. appear in the form of file names, tags, todo states, or search strings.
  14243. If you answer yes to the prompt, you might want to check and remove
  14244. such private information before sending the email.")
  14245. (add-text-properties (point-min) (point-max) '(face org-warning))
  14246. (when (yes-or-no-p "Include your Org-mode configuration ")
  14247. (mapatoms
  14248. (lambda (v)
  14249. (and (boundp v)
  14250. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  14251. (or (and (symbol-value v)
  14252. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  14253. (and
  14254. (get v 'custom-type) (get v 'standard-value)
  14255. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  14256. (push v list)))))
  14257. (kill-buffer (get-buffer "*Warn about privacy*"))
  14258. list))
  14259. nil nil
  14260. "Remember to cover the basics, that is, what you expected to happen and
  14261. what in fact did happen. You don't know how to make a good report? See
  14262. http://orgmode.org/manual/Feedback.html#Feedback
  14263. Your bug report will be posted to the Org-mode mailing list.
  14264. ------------------------------------------------------------------------")))
  14265. (defun org-install-agenda-files-menu ()
  14266. (let ((bl (buffer-list)))
  14267. (save-excursion
  14268. (while bl
  14269. (set-buffer (pop bl))
  14270. (if (org-mode-p) (setq bl nil)))
  14271. (when (org-mode-p)
  14272. (easy-menu-change
  14273. '("Org") "File List for Agenda"
  14274. (append
  14275. (list
  14276. ["Edit File List" (org-edit-agenda-file-list) t]
  14277. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  14278. ["Remove Current File from List" org-remove-file t]
  14279. ["Cycle through agenda files" org-cycle-agenda-files t]
  14280. ["Occur in all agenda files" org-occur-in-agenda-files t]
  14281. "--")
  14282. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  14283. ;;;; Documentation
  14284. ;;;###autoload
  14285. (defun org-require-autoloaded-modules ()
  14286. (interactive)
  14287. (mapc 'require
  14288. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  14289. org-docbook org-exp org-html org-icalendar
  14290. org-id org-latex
  14291. org-publish org-remember org-table
  14292. org-timer org-xoxo)))
  14293. ;;;###autoload
  14294. (defun org-reload (&optional uncompiled)
  14295. "Reload all org lisp files.
  14296. With prefix arg UNCOMPILED, load the uncompiled versions."
  14297. (interactive "P")
  14298. (require 'find-func)
  14299. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  14300. (dir-org (file-name-directory (org-find-library-name "org")))
  14301. (dir-org-contrib (ignore-errors
  14302. (file-name-directory
  14303. (org-find-library-name "org-contribdir"))))
  14304. (files
  14305. (append (directory-files dir-org t file-re)
  14306. (and dir-org-contrib
  14307. (directory-files dir-org-contrib t file-re))))
  14308. (remove-re (concat (if (featurep 'xemacs)
  14309. "org-colview" "org-colview-xemacs")
  14310. "\\'")))
  14311. (setq files (mapcar 'file-name-sans-extension files))
  14312. (setq files (mapcar
  14313. (lambda (x) (if (string-match remove-re x) nil x))
  14314. files))
  14315. (setq files (delq nil files))
  14316. (mapc
  14317. (lambda (f)
  14318. (when (featurep (intern (file-name-nondirectory f)))
  14319. (if (and (not uncompiled)
  14320. (file-exists-p (concat f ".elc")))
  14321. (load (concat f ".elc") nil nil t)
  14322. (load (concat f ".el") nil nil t))))
  14323. files))
  14324. (org-version))
  14325. ;;;###autoload
  14326. (defun org-customize ()
  14327. "Call the customize function with org as argument."
  14328. (interactive)
  14329. (org-load-modules-maybe)
  14330. (org-require-autoloaded-modules)
  14331. (customize-browse 'org))
  14332. (defun org-create-customize-menu ()
  14333. "Create a full customization menu for Org-mode, insert it into the menu."
  14334. (interactive)
  14335. (org-load-modules-maybe)
  14336. (org-require-autoloaded-modules)
  14337. (if (fboundp 'customize-menu-create)
  14338. (progn
  14339. (easy-menu-change
  14340. '("Org") "Customize"
  14341. `(["Browse Org group" org-customize t]
  14342. "--"
  14343. ,(customize-menu-create 'org)
  14344. ["Set" Custom-set t]
  14345. ["Save" Custom-save t]
  14346. ["Reset to Current" Custom-reset-current t]
  14347. ["Reset to Saved" Custom-reset-saved t]
  14348. ["Reset to Standard Settings" Custom-reset-standard t]))
  14349. (message "\"Org\"-menu now contains full customization menu"))
  14350. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  14351. ;;;; Miscellaneous stuff
  14352. ;;; Generally useful functions
  14353. (defun org-find-text-property-in-string (prop s)
  14354. "Return the first non-nil value of property PROP in string S."
  14355. (or (get-text-property 0 prop s)
  14356. (get-text-property (or (next-single-property-change 0 prop s) 0)
  14357. prop s)))
  14358. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  14359. "Display the given MESSAGE as a warning."
  14360. (if (fboundp 'display-warning)
  14361. (display-warning 'org message
  14362. (if (featurep 'xemacs)
  14363. 'warning
  14364. :warning))
  14365. (let ((buf (get-buffer-create "*Org warnings*")))
  14366. (with-current-buffer buf
  14367. (goto-char (point-max))
  14368. (insert "Warning (Org): " message)
  14369. (unless (bolp)
  14370. (newline)))
  14371. (display-buffer buf)
  14372. (sit-for 0))))
  14373. (defun org-in-commented-line ()
  14374. "Is point in a line starting with `#'?"
  14375. (equal (char-after (point-at-bol)) ?#))
  14376. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  14377. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  14378. (if (and marker (marker-buffer marker)
  14379. (buffer-live-p (marker-buffer marker)))
  14380. (progn
  14381. (switch-to-buffer (marker-buffer marker))
  14382. (if (or (> marker (point-max)) (< marker (point-min)))
  14383. (widen))
  14384. (goto-char marker)
  14385. (org-show-context 'org-goto))
  14386. (if bookmark
  14387. (bookmark-jump bookmark)
  14388. (error "Cannot find location"))))
  14389. (defun org-quote-csv-field (s)
  14390. "Quote field for inclusion in CSV material."
  14391. (if (string-match "[\",]" s)
  14392. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  14393. s))
  14394. (defun org-plist-delete (plist property)
  14395. "Delete PROPERTY from PLIST.
  14396. This is in contrast to merely setting it to 0."
  14397. (let (p)
  14398. (while plist
  14399. (if (not (eq property (car plist)))
  14400. (setq p (plist-put p (car plist) (nth 1 plist))))
  14401. (setq plist (cddr plist)))
  14402. p))
  14403. (defun org-force-self-insert (N)
  14404. "Needed to enforce self-insert under remapping."
  14405. (interactive "p")
  14406. (self-insert-command N))
  14407. (defun org-string-width (s)
  14408. "Compute width of string, ignoring invisible characters.
  14409. This ignores character with invisibility property `org-link', and also
  14410. characters with property `org-cwidth', because these will become invisible
  14411. upon the next fontification round."
  14412. (let (b l)
  14413. (when (or (eq t buffer-invisibility-spec)
  14414. (assq 'org-link buffer-invisibility-spec))
  14415. (while (setq b (text-property-any 0 (length s)
  14416. 'invisible 'org-link s))
  14417. (setq s (concat (substring s 0 b)
  14418. (substring s (or (next-single-property-change
  14419. b 'invisible s) (length s)))))))
  14420. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  14421. (setq s (concat (substring s 0 b)
  14422. (substring s (or (next-single-property-change
  14423. b 'org-cwidth s) (length s))))))
  14424. (setq l (string-width s) b -1)
  14425. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  14426. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  14427. l))
  14428. (defun org-get-indentation (&optional line)
  14429. "Get the indentation of the current line, interpreting tabs.
  14430. When LINE is given, assume it represents a line and compute its indentation."
  14431. (if line
  14432. (if (string-match "^ *" (org-remove-tabs line))
  14433. (match-end 0))
  14434. (save-excursion
  14435. (beginning-of-line 1)
  14436. (skip-chars-forward " \t")
  14437. (current-column))))
  14438. (defun org-remove-tabs (s &optional width)
  14439. "Replace tabulators in S with spaces.
  14440. Assumes that s is a single line, starting in column 0."
  14441. (setq width (or width tab-width))
  14442. (while (string-match "\t" s)
  14443. (setq s (replace-match
  14444. (make-string
  14445. (- (* width (/ (+ (match-beginning 0) width) width))
  14446. (match-beginning 0)) ?\ )
  14447. t t s)))
  14448. s)
  14449. (defun org-fix-indentation (line ind)
  14450. "Fix indentation in LINE.
  14451. IND is a cons cell with target and minimum indentation.
  14452. If the current indentation in LINE is smaller than the minimum,
  14453. leave it alone. If it is larger than ind, set it to the target."
  14454. (let* ((l (org-remove-tabs line))
  14455. (i (org-get-indentation l))
  14456. (i1 (car ind)) (i2 (cdr ind)))
  14457. (if (>= i i2) (setq l (substring line i2)))
  14458. (if (> i1 0)
  14459. (concat (make-string i1 ?\ ) l)
  14460. l)))
  14461. (defun org-remove-indentation (code &optional n)
  14462. "Remove the maximum common indentation from the lines in CODE.
  14463. N may optionally be the number of spaces to remove."
  14464. (with-temp-buffer
  14465. (insert code)
  14466. (org-do-remove-indentation n)
  14467. (buffer-string)))
  14468. (defun org-do-remove-indentation (&optional n)
  14469. "Remove the maximum common indentation from the buffer."
  14470. (untabify (point-min) (point-max))
  14471. (let ((min 10000) re)
  14472. (if n
  14473. (setq min n)
  14474. (goto-char (point-min))
  14475. (while (re-search-forward "^ *[^ \n]" nil t)
  14476. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  14477. (unless (or (= min 0) (= min 10000))
  14478. (setq re (format "^ \\{%d\\}" min))
  14479. (goto-char (point-min))
  14480. (while (re-search-forward re nil t)
  14481. (replace-match "")
  14482. (end-of-line 1))
  14483. min)))
  14484. (defun org-base-buffer (buffer)
  14485. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  14486. (if (not buffer)
  14487. buffer
  14488. (or (buffer-base-buffer buffer)
  14489. buffer)))
  14490. (defun org-trim (s)
  14491. "Remove whitespace at beginning and end of string."
  14492. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  14493. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  14494. s)
  14495. (defun org-wrap (string &optional width lines)
  14496. "Wrap string to either a number of lines, or a width in characters.
  14497. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  14498. that costs. If there is a word longer than WIDTH, the text is actually
  14499. wrapped to the length of that word.
  14500. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  14501. many lines, whatever width that takes.
  14502. The return value is a list of lines, without newlines at the end."
  14503. (let* ((words (org-split-string string "[ \t\n]+"))
  14504. (maxword (apply 'max (mapcar 'org-string-width words)))
  14505. w ll)
  14506. (cond (width
  14507. (org-do-wrap words (max maxword width)))
  14508. (lines
  14509. (setq w maxword)
  14510. (setq ll (org-do-wrap words maxword))
  14511. (if (<= (length ll) lines)
  14512. ll
  14513. (setq ll words)
  14514. (while (> (length ll) lines)
  14515. (setq w (1+ w))
  14516. (setq ll (org-do-wrap words w)))
  14517. ll))
  14518. (t (error "Cannot wrap this")))))
  14519. (defun org-do-wrap (words width)
  14520. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  14521. (let (lines line)
  14522. (while words
  14523. (setq line (pop words))
  14524. (while (and words (< (+ (length line) (length (car words))) width))
  14525. (setq line (concat line " " (pop words))))
  14526. (setq lines (push line lines)))
  14527. (nreverse lines)))
  14528. (defun org-split-string (string &optional separators)
  14529. "Splits STRING into substrings at SEPARATORS.
  14530. No empty strings are returned if there are matches at the beginning
  14531. and end of string."
  14532. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  14533. (start 0)
  14534. notfirst
  14535. (list nil))
  14536. (while (and (string-match rexp string
  14537. (if (and notfirst
  14538. (= start (match-beginning 0))
  14539. (< start (length string)))
  14540. (1+ start) start))
  14541. (< (match-beginning 0) (length string)))
  14542. (setq notfirst t)
  14543. (or (eq (match-beginning 0) 0)
  14544. (and (eq (match-beginning 0) (match-end 0))
  14545. (eq (match-beginning 0) start))
  14546. (setq list
  14547. (cons (substring string start (match-beginning 0))
  14548. list)))
  14549. (setq start (match-end 0)))
  14550. (or (eq start (length string))
  14551. (setq list
  14552. (cons (substring string start)
  14553. list)))
  14554. (nreverse list)))
  14555. (defun org-quote-vert (s)
  14556. "Replace \"|\" with \"\\vert\"."
  14557. (while (string-match "|" s)
  14558. (setq s (replace-match "\\vert" t t s)))
  14559. s)
  14560. (defun org-uuidgen-p (s)
  14561. "Is S an ID created by UUIDGEN?"
  14562. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  14563. (defun org-context ()
  14564. "Return a list of contexts of the current cursor position.
  14565. If several contexts apply, all are returned.
  14566. Each context entry is a list with a symbol naming the context, and
  14567. two positions indicating start and end of the context. Possible
  14568. contexts are:
  14569. :headline anywhere in a headline
  14570. :headline-stars on the leading stars in a headline
  14571. :todo-keyword on a TODO keyword (including DONE) in a headline
  14572. :tags on the TAGS in a headline
  14573. :priority on the priority cookie in a headline
  14574. :item on the first line of a plain list item
  14575. :item-bullet on the bullet/number of a plain list item
  14576. :checkbox on the checkbox in a plain list item
  14577. :table in an org-mode table
  14578. :table-special on a special filed in a table
  14579. :table-table in a table.el table
  14580. :link on a hyperlink
  14581. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  14582. :target on a <<target>>
  14583. :radio-target on a <<<radio-target>>>
  14584. :latex-fragment on a LaTeX fragment
  14585. :latex-preview on a LaTeX fragment with overlayed preview image
  14586. This function expects the position to be visible because it uses font-lock
  14587. faces as a help to recognize the following contexts: :table-special, :link,
  14588. and :keyword."
  14589. (let* ((f (get-text-property (point) 'face))
  14590. (faces (if (listp f) f (list f)))
  14591. (p (point)) clist o)
  14592. ;; First the large context
  14593. (cond
  14594. ((org-on-heading-p t)
  14595. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  14596. (when (progn
  14597. (beginning-of-line 1)
  14598. (looking-at org-todo-line-tags-regexp))
  14599. (push (org-point-in-group p 1 :headline-stars) clist)
  14600. (push (org-point-in-group p 2 :todo-keyword) clist)
  14601. (push (org-point-in-group p 4 :tags) clist))
  14602. (goto-char p)
  14603. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  14604. (if (looking-at "\\[#[A-Z0-9]\\]")
  14605. (push (org-point-in-group p 0 :priority) clist)))
  14606. ((org-at-item-p)
  14607. (push (org-point-in-group p 2 :item-bullet) clist)
  14608. (push (list :item (point-at-bol)
  14609. (save-excursion (org-end-of-item) (point)))
  14610. clist)
  14611. (and (org-at-item-checkbox-p)
  14612. (push (org-point-in-group p 0 :checkbox) clist)))
  14613. ((org-at-table-p)
  14614. (push (list :table (org-table-begin) (org-table-end)) clist)
  14615. (if (memq 'org-formula faces)
  14616. (push (list :table-special
  14617. (previous-single-property-change p 'face)
  14618. (next-single-property-change p 'face)) clist)))
  14619. ((org-at-table-p 'any)
  14620. (push (list :table-table) clist)))
  14621. (goto-char p)
  14622. ;; Now the small context
  14623. (cond
  14624. ((org-at-timestamp-p)
  14625. (push (org-point-in-group p 0 :timestamp) clist))
  14626. ((memq 'org-link faces)
  14627. (push (list :link
  14628. (previous-single-property-change p 'face)
  14629. (next-single-property-change p 'face)) clist))
  14630. ((memq 'org-special-keyword faces)
  14631. (push (list :keyword
  14632. (previous-single-property-change p 'face)
  14633. (next-single-property-change p 'face)) clist))
  14634. ((org-on-target-p)
  14635. (push (org-point-in-group p 0 :target) clist)
  14636. (goto-char (1- (match-beginning 0)))
  14637. (if (looking-at org-radio-target-regexp)
  14638. (push (org-point-in-group p 0 :radio-target) clist))
  14639. (goto-char p))
  14640. ((setq o (car (delq nil
  14641. (mapcar
  14642. (lambda (x)
  14643. (if (memq x org-latex-fragment-image-overlays) x))
  14644. (org-overlays-at (point))))))
  14645. (push (list :latex-fragment
  14646. (org-overlay-start o) (org-overlay-end o)) clist)
  14647. (push (list :latex-preview
  14648. (org-overlay-start o) (org-overlay-end o)) clist))
  14649. ((org-inside-LaTeX-fragment-p)
  14650. ;; FIXME: positions wrong.
  14651. (push (list :latex-fragment (point) (point)) clist)))
  14652. (setq clist (nreverse (delq nil clist)))
  14653. clist))
  14654. ;; FIXME: Compare with at-regexp-p Do we need both?
  14655. (defun org-in-regexp (re &optional nlines visually)
  14656. "Check if point is inside a match of regexp.
  14657. Normally only the current line is checked, but you can include NLINES extra
  14658. lines both before and after point into the search.
  14659. If VISUALLY is set, require that the cursor is not after the match but
  14660. really on, so that the block visually is on the match."
  14661. (catch 'exit
  14662. (let ((pos (point))
  14663. (eol (point-at-eol (+ 1 (or nlines 0))))
  14664. (inc (if visually 1 0)))
  14665. (save-excursion
  14666. (beginning-of-line (- 1 (or nlines 0)))
  14667. (while (re-search-forward re eol t)
  14668. (if (and (<= (match-beginning 0) pos)
  14669. (>= (+ inc (match-end 0)) pos))
  14670. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  14671. (defun org-at-regexp-p (regexp)
  14672. "Is point inside a match of REGEXP in the current line?"
  14673. (catch 'exit
  14674. (save-excursion
  14675. (let ((pos (point)) (end (point-at-eol)))
  14676. (beginning-of-line 1)
  14677. (while (re-search-forward regexp end t)
  14678. (if (and (<= (match-beginning 0) pos)
  14679. (>= (match-end 0) pos))
  14680. (throw 'exit t)))
  14681. nil))))
  14682. (defun org-occur-in-agenda-files (regexp &optional nlines)
  14683. "Call `multi-occur' with buffers for all agenda files."
  14684. (interactive "sOrg-files matching: \np")
  14685. (let* ((files (org-agenda-files))
  14686. (tnames (mapcar 'file-truename files))
  14687. (extra org-agenda-text-search-extra-files)
  14688. f)
  14689. (when (eq (car extra) 'agenda-archives)
  14690. (setq extra (cdr extra))
  14691. (setq files (org-add-archive-files files)))
  14692. (while (setq f (pop extra))
  14693. (unless (member (file-truename f) tnames)
  14694. (add-to-list 'files f 'append)
  14695. (add-to-list 'tnames (file-truename f) 'append)))
  14696. (multi-occur
  14697. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  14698. regexp)))
  14699. (if (boundp 'occur-mode-find-occurrence-hook)
  14700. ;; Emacs 23
  14701. (add-hook 'occur-mode-find-occurrence-hook
  14702. (lambda ()
  14703. (when (org-mode-p)
  14704. (org-reveal))))
  14705. ;; Emacs 22
  14706. (defadvice occur-mode-goto-occurrence
  14707. (after org-occur-reveal activate)
  14708. (and (org-mode-p) (org-reveal)))
  14709. (defadvice occur-mode-goto-occurrence-other-window
  14710. (after org-occur-reveal activate)
  14711. (and (org-mode-p) (org-reveal)))
  14712. (defadvice occur-mode-display-occurrence
  14713. (after org-occur-reveal activate)
  14714. (when (org-mode-p)
  14715. (let ((pos (occur-mode-find-occurrence)))
  14716. (with-current-buffer (marker-buffer pos)
  14717. (save-excursion
  14718. (goto-char pos)
  14719. (org-reveal)))))))
  14720. (defun org-occur-link-in-agenda-files ()
  14721. "Create a link and search for it in the agendas.
  14722. The link is not stored in `org-stored-links', it is just created
  14723. for the search purpose."
  14724. (interactive)
  14725. (let ((link (condition-case nil
  14726. (org-store-link nil)
  14727. (error "Unable to create a link to here"))))
  14728. (org-occur-in-agenda-files (regexp-quote link))))
  14729. (defun org-uniquify (list)
  14730. "Remove duplicate elements from LIST."
  14731. (let (res)
  14732. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  14733. res))
  14734. (defun org-delete-all (elts list)
  14735. "Remove all elements in ELTS from LIST."
  14736. (while elts
  14737. (setq list (delete (pop elts) list)))
  14738. list)
  14739. (defun org-back-over-empty-lines ()
  14740. "Move backwards over whitespace, to the beginning of the first empty line.
  14741. Returns the number of empty lines passed."
  14742. (let ((pos (point)))
  14743. (skip-chars-backward " \t\n\r")
  14744. (beginning-of-line 2)
  14745. (goto-char (min (point) pos))
  14746. (count-lines (point) pos)))
  14747. (defun org-skip-whitespace ()
  14748. (skip-chars-forward " \t\n\r"))
  14749. (defun org-point-in-group (point group &optional context)
  14750. "Check if POINT is in match-group GROUP.
  14751. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  14752. match. If the match group does ot exist or point is not inside it,
  14753. return nil."
  14754. (and (match-beginning group)
  14755. (>= point (match-beginning group))
  14756. (<= point (match-end group))
  14757. (if context
  14758. (list context (match-beginning group) (match-end group))
  14759. t)))
  14760. (defun org-switch-to-buffer-other-window (&rest args)
  14761. "Switch to buffer in a second window on the current frame.
  14762. In particular, do not allow pop-up frames."
  14763. (let (pop-up-frames special-display-buffer-names special-display-regexps
  14764. special-display-function)
  14765. (apply 'switch-to-buffer-other-window args)))
  14766. (defun org-combine-plists (&rest plists)
  14767. "Create a single property list from all plists in PLISTS.
  14768. The process starts by copying the first list, and then setting properties
  14769. from the other lists. Settings in the last list are the most significant
  14770. ones and overrule settings in the other lists."
  14771. (let ((rtn (copy-sequence (pop plists)))
  14772. p v ls)
  14773. (while plists
  14774. (setq ls (pop plists))
  14775. (while ls
  14776. (setq p (pop ls) v (pop ls))
  14777. (setq rtn (plist-put rtn p v))))
  14778. rtn))
  14779. (defun org-move-line-down (arg)
  14780. "Move the current line down. With prefix argument, move it past ARG lines."
  14781. (interactive "p")
  14782. (let ((col (current-column))
  14783. beg end pos)
  14784. (beginning-of-line 1) (setq beg (point))
  14785. (beginning-of-line 2) (setq end (point))
  14786. (beginning-of-line (+ 1 arg))
  14787. (setq pos (move-marker (make-marker) (point)))
  14788. (insert (delete-and-extract-region beg end))
  14789. (goto-char pos)
  14790. (org-move-to-column col)))
  14791. (defun org-move-line-up (arg)
  14792. "Move the current line up. With prefix argument, move it past ARG lines."
  14793. (interactive "p")
  14794. (let ((col (current-column))
  14795. beg end pos)
  14796. (beginning-of-line 1) (setq beg (point))
  14797. (beginning-of-line 2) (setq end (point))
  14798. (beginning-of-line (- arg))
  14799. (setq pos (move-marker (make-marker) (point)))
  14800. (insert (delete-and-extract-region beg end))
  14801. (goto-char pos)
  14802. (org-move-to-column col)))
  14803. (defun org-replace-escapes (string table)
  14804. "Replace %-escapes in STRING with values in TABLE.
  14805. TABLE is an association list with keys like \"%a\" and string values.
  14806. The sequences in STRING may contain normal field width and padding information,
  14807. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  14808. so values can contain further %-escapes if they are define later in TABLE."
  14809. (let ((case-fold-search nil)
  14810. e re rpl)
  14811. (while (setq e (pop table))
  14812. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  14813. (while (string-match re string)
  14814. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  14815. (cdr e)))
  14816. (setq string (replace-match rpl t t string))))
  14817. string))
  14818. (defun org-sublist (list start end)
  14819. "Return a section of LIST, from START to END.
  14820. Counting starts at 1."
  14821. (let (rtn (c start))
  14822. (setq list (nthcdr (1- start) list))
  14823. (while (and list (<= c end))
  14824. (push (pop list) rtn)
  14825. (setq c (1+ c)))
  14826. (nreverse rtn)))
  14827. (defun org-find-base-buffer-visiting (file)
  14828. "Like `find-buffer-visiting' but always return the base buffer and
  14829. not an indirect buffer."
  14830. (let ((buf (or (get-file-buffer file)
  14831. (find-buffer-visiting file))))
  14832. (if buf
  14833. (or (buffer-base-buffer buf) buf)
  14834. nil)))
  14835. (defun org-image-file-name-regexp (&optional extensions)
  14836. "Return regexp matching the file names of images.
  14837. If EXTENSIONS is given, only match these."
  14838. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  14839. (image-file-name-regexp)
  14840. (let ((image-file-name-extensions
  14841. (or extensions
  14842. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  14843. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  14844. (concat "\\."
  14845. (regexp-opt (nconc (mapcar 'upcase
  14846. image-file-name-extensions)
  14847. image-file-name-extensions)
  14848. t)
  14849. "\\'"))))
  14850. (defun org-file-image-p (file &optional extensions)
  14851. "Return non-nil if FILE is an image."
  14852. (save-match-data
  14853. (string-match (org-image-file-name-regexp extensions) file)))
  14854. (defun org-get-cursor-date ()
  14855. "Return the date at cursor in as a time.
  14856. This works in the calendar and in the agenda, anywhere else it just
  14857. returns the current time."
  14858. (let (date day defd)
  14859. (cond
  14860. ((eq major-mode 'calendar-mode)
  14861. (setq date (calendar-cursor-to-date)
  14862. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14863. ((eq major-mode 'org-agenda-mode)
  14864. (setq day (get-text-property (point) 'day))
  14865. (if day
  14866. (setq date (calendar-gregorian-from-absolute day)
  14867. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  14868. (nth 2 date))))))
  14869. (or defd (current-time))))
  14870. (defvar org-agenda-action-marker (make-marker)
  14871. "Marker pointing to the entry for the next agenda action.")
  14872. (defun org-mark-entry-for-agenda-action ()
  14873. "Mark the current entry as target of an agenda action.
  14874. Agenda actions are actions executed from the agenda with the key `k',
  14875. which make use of the date at the cursor."
  14876. (interactive)
  14877. (move-marker org-agenda-action-marker
  14878. (save-excursion (org-back-to-heading t) (point))
  14879. (current-buffer))
  14880. (message
  14881. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  14882. ;;; Paragraph filling stuff.
  14883. ;; We want this to be just right, so use the full arsenal.
  14884. (defun org-indent-line-function ()
  14885. "Indent line like previous, but further if previous was headline or item."
  14886. (interactive)
  14887. (let* ((pos (point))
  14888. (itemp (org-at-item-p))
  14889. (case-fold-search t)
  14890. (org-drawer-regexp (or org-drawer-regexp "\000"))
  14891. column bpos bcol tpos tcol bullet btype bullet-type)
  14892. ;; Find the previous relevant line
  14893. (beginning-of-line 1)
  14894. (cond
  14895. ((looking-at "#") (setq column 0))
  14896. ((looking-at "\\*+ ") (setq column 0))
  14897. ((and (looking-at "[ \t]*:END:")
  14898. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  14899. (save-excursion
  14900. (goto-char (1- (match-beginning 1)))
  14901. (setq column (current-column))))
  14902. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  14903. (save-excursion
  14904. (re-search-backward
  14905. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  14906. (setq column (org-get-indentation (match-string 0))))
  14907. (t
  14908. (beginning-of-line 0)
  14909. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  14910. (not (looking-at "[ \t]*:END:"))
  14911. (not (looking-at org-drawer-regexp)))
  14912. (beginning-of-line 0))
  14913. (cond
  14914. ((looking-at "\\*+[ \t]+")
  14915. (if (not org-adapt-indentation)
  14916. (setq column 0)
  14917. (goto-char (match-end 0))
  14918. (setq column (current-column))))
  14919. ((looking-at org-drawer-regexp)
  14920. (goto-char (1- (match-beginning 1)))
  14921. (setq column (current-column)))
  14922. ((looking-at "\\([ \t]*\\):END:")
  14923. (goto-char (match-end 1))
  14924. (setq column (current-column)))
  14925. ((org-in-item-p)
  14926. (org-beginning-of-item)
  14927. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  14928. (setq bpos (match-beginning 1) tpos (match-end 0)
  14929. bcol (progn (goto-char bpos) (current-column))
  14930. tcol (progn (goto-char tpos) (current-column))
  14931. bullet (match-string 1)
  14932. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  14933. (if (> tcol (+ bcol org-description-max-indent))
  14934. (setq tcol (+ bcol 5)))
  14935. (if (not itemp)
  14936. (setq column tcol)
  14937. (goto-char pos)
  14938. (beginning-of-line 1)
  14939. (if (looking-at "\\S-")
  14940. (progn
  14941. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  14942. (setq bullet (match-string 1)
  14943. btype (if (string-match "[0-9]" bullet) "n" bullet))
  14944. (setq column (if (equal btype bullet-type) bcol tcol)))
  14945. (setq column (org-get-indentation)))))
  14946. (t (setq column (org-get-indentation))))))
  14947. (goto-char pos)
  14948. (if (<= (current-column) (current-indentation))
  14949. (org-indent-line-to column)
  14950. (save-excursion (org-indent-line-to column)))
  14951. (setq column (current-column))
  14952. (beginning-of-line 1)
  14953. (if (looking-at
  14954. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  14955. (replace-match (concat "\\1" (format org-property-format
  14956. (match-string 2) (match-string 3)))
  14957. t nil))
  14958. (org-move-to-column column)))
  14959. (defun org-set-autofill-regexps ()
  14960. (interactive)
  14961. ;; In the paragraph separator we include headlines, because filling
  14962. ;; text in a line directly attached to a headline would otherwise
  14963. ;; fill the headline as well.
  14964. (org-set-local 'comment-start-skip "^#+[ \t]*")
  14965. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  14966. ;; The paragraph starter includes hand-formatted lists.
  14967. (org-set-local
  14968. 'paragraph-start
  14969. (concat
  14970. "\f" "\\|"
  14971. "[ ]*$" "\\|"
  14972. "\\*+ " "\\|"
  14973. "[ \t]*#" "\\|"
  14974. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  14975. "[ \t]*[:|]" "\\|"
  14976. "\\$\\$" "\\|"
  14977. "\\\\\\(begin\\|end\\|[][]\\)"))
  14978. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  14979. ;; But only if the user has not turned off tables or fixed-width regions
  14980. (org-set-local
  14981. 'auto-fill-inhibit-regexp
  14982. (concat "\\*+ \\|#\\+"
  14983. "\\|[ \t]*" org-keyword-time-regexp
  14984. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  14985. (concat
  14986. "\\|[ \t]*["
  14987. (if org-enable-table-editor "|" "")
  14988. (if org-enable-fixed-width-editor ":" "")
  14989. "]"))))
  14990. ;; We use our own fill-paragraph function, to make sure that tables
  14991. ;; and fixed-width regions are not wrapped. That function will pass
  14992. ;; through to `fill-paragraph' when appropriate.
  14993. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  14994. ; Adaptive filling: To get full control, first make sure that
  14995. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  14996. (org-set-local 'adaptive-fill-regexp "\000")
  14997. (org-set-local 'adaptive-fill-function
  14998. 'org-adaptive-fill-function)
  14999. (org-set-local
  15000. 'align-mode-rules-list
  15001. '((org-in-buffer-settings
  15002. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  15003. (modes . '(org-mode))))))
  15004. (defun org-fill-paragraph (&optional justify)
  15005. "Re-align a table, pass through to fill-paragraph if no table."
  15006. (let ((table-p (org-at-table-p))
  15007. (table.el-p (org-at-table.el-p)))
  15008. (cond ((and (equal (char-after (point-at-bol)) ?*)
  15009. (save-excursion (goto-char (point-at-bol))
  15010. (looking-at outline-regexp)))
  15011. t) ; skip headlines
  15012. (table.el-p t) ; skip table.el tables
  15013. (table-p (org-table-align) t) ; align org-mode tables
  15014. (t nil)))) ; call paragraph-fill
  15015. ;; For reference, this is the default value of adaptive-fill-regexp
  15016. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15017. (defun org-adaptive-fill-function ()
  15018. "Return a fill prefix for org-mode files.
  15019. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15020. work correctly."
  15021. (cond ((looking-at "#[ \t]+")
  15022. (match-string 0))
  15023. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  15024. (save-excursion
  15025. (if (> (match-end 1) (+ (match-beginning 1)
  15026. org-description-max-indent))
  15027. (goto-char (+ (match-beginning 1) 5))
  15028. (goto-char (match-end 0)))
  15029. (make-string (current-column) ?\ )))
  15030. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  15031. (save-excursion
  15032. (goto-char (match-end 0))
  15033. (make-string (current-column) ?\ )))
  15034. (t nil)))
  15035. ;;; Other stuff.
  15036. (defun org-toggle-fixed-width-section (arg)
  15037. "Toggle the fixed-width export.
  15038. If there is no active region, the QUOTE keyword at the current headline is
  15039. inserted or removed. When present, it causes the text between this headline
  15040. and the next to be exported as fixed-width text, and unmodified.
  15041. If there is an active region, this command adds or removes a colon as the
  15042. first character of this line. If the first character of a line is a colon,
  15043. this line is also exported in fixed-width font."
  15044. (interactive "P")
  15045. (let* ((cc 0)
  15046. (regionp (org-region-active-p))
  15047. (beg (if regionp (region-beginning) (point)))
  15048. (end (if regionp (region-end)))
  15049. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  15050. (case-fold-search nil)
  15051. (re "[ \t]*\\(: \\)")
  15052. off)
  15053. (if regionp
  15054. (save-excursion
  15055. (goto-char beg)
  15056. (setq cc (current-column))
  15057. (beginning-of-line 1)
  15058. (setq off (looking-at re))
  15059. (while (> nlines 0)
  15060. (setq nlines (1- nlines))
  15061. (beginning-of-line 1)
  15062. (cond
  15063. (arg
  15064. (org-move-to-column cc t)
  15065. (insert ": \n")
  15066. (forward-line -1))
  15067. ((and off (looking-at re))
  15068. (replace-match "" t t nil 1))
  15069. ((not off) (org-move-to-column cc t) (insert ": ")))
  15070. (forward-line 1)))
  15071. (save-excursion
  15072. (org-back-to-heading)
  15073. (if (looking-at (concat outline-regexp
  15074. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  15075. (replace-match "" t t nil 1)
  15076. (if (looking-at outline-regexp)
  15077. (progn
  15078. (goto-char (match-end 0))
  15079. (insert org-quote-string " "))))))))
  15080. (defun org-reftex-citation ()
  15081. "Use reftex-citation to insert a citation into the buffer.
  15082. This looks for a line like
  15083. #+BIBLIOGRAPHY: foo plain option:-d
  15084. and derives from it that foo.bib is the bbliography file relevant
  15085. for this document. It then installs the necessary environment for RefTeX
  15086. to work in this buffer and calls `reftex-citation' to insert a citation
  15087. into the buffer.
  15088. Export of such citations to both LaTeX and HTML is handled by the contributed
  15089. package org-exp-bibtex by Taru Karttunen."
  15090. (interactive)
  15091. (let ((reftex-docstruct-symbol 'rds)
  15092. (reftex-cite-format "\\cite{%l}")
  15093. rds bib)
  15094. (save-excursion
  15095. (save-restriction
  15096. (widen)
  15097. (let ((case-fold-search t)
  15098. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  15099. (if (not (save-excursion
  15100. (or (re-search-forward re nil t)
  15101. (re-search-backward re nil t))))
  15102. (error "No bibliography defined in file")
  15103. (setq bib (concat (match-string 1) ".bib")
  15104. rds (list (list 'bib bib)))))))
  15105. (call-interactively 'reftex-citation)))
  15106. ;;;; Functions extending outline functionality
  15107. (defun org-beginning-of-line (&optional arg)
  15108. "Go to the beginning of the current line. If that is invisible, continue
  15109. to a visible line beginning. This makes the function of C-a more intuitive.
  15110. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15111. first attempt, and only move to after the tags when the cursor is already
  15112. beyond the end of the headline."
  15113. (interactive "P")
  15114. (let ((pos (point))
  15115. (special (if (consp org-special-ctrl-a/e)
  15116. (car org-special-ctrl-a/e)
  15117. org-special-ctrl-a/e))
  15118. refpos)
  15119. (if (org-bound-and-true-p line-move-visual)
  15120. (beginning-of-visual-line 1)
  15121. (beginning-of-line 1))
  15122. (if (and arg (fboundp 'move-beginning-of-line))
  15123. (call-interactively 'move-beginning-of-line)
  15124. (if (bobp)
  15125. nil
  15126. (backward-char 1)
  15127. (if (org-invisible-p)
  15128. (while (and (not (bobp)) (org-invisible-p))
  15129. (backward-char 1)
  15130. (beginning-of-line 1))
  15131. (forward-char 1))))
  15132. (when special
  15133. (cond
  15134. ((and (looking-at org-complex-heading-regexp)
  15135. (= (char-after (match-end 1)) ?\ ))
  15136. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  15137. (point-at-eol)))
  15138. (goto-char
  15139. (if (eq special t)
  15140. (cond ((> pos refpos) refpos)
  15141. ((= pos (point)) refpos)
  15142. (t (point)))
  15143. (cond ((> pos (point)) (point))
  15144. ((not (eq last-command this-command)) (point))
  15145. (t refpos)))))
  15146. ((org-at-item-p)
  15147. (goto-char
  15148. (if (eq special t)
  15149. (cond ((> pos (match-end 4)) (match-end 4))
  15150. ((= pos (point)) (match-end 4))
  15151. (t (point)))
  15152. (cond ((> pos (point)) (point))
  15153. ((not (eq last-command this-command)) (point))
  15154. (t (match-end 4))))))))
  15155. (org-no-warnings
  15156. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15157. (defun org-end-of-line (&optional arg)
  15158. "Go to the end of the line.
  15159. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  15160. first attempt, and only move to after the tags when the cursor is already
  15161. beyond the end of the headline."
  15162. (interactive "P")
  15163. (let ((special (if (consp org-special-ctrl-a/e)
  15164. (cdr org-special-ctrl-a/e)
  15165. org-special-ctrl-a/e)))
  15166. (if (or (not special)
  15167. (not (org-on-heading-p))
  15168. arg)
  15169. (call-interactively
  15170. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  15171. ((fboundp 'move-end-of-line) 'move-end-of-line)
  15172. (t 'end-of-line)))
  15173. (let ((pos (point)))
  15174. (beginning-of-line 1)
  15175. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  15176. (if (eq special t)
  15177. (if (or (< pos (match-beginning 1))
  15178. (= pos (match-end 0)))
  15179. (goto-char (match-beginning 1))
  15180. (goto-char (match-end 0)))
  15181. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  15182. (goto-char (match-end 0))
  15183. (goto-char (match-beginning 1))))
  15184. (call-interactively (if (fboundp 'move-end-of-line)
  15185. 'move-end-of-line
  15186. 'end-of-line)))))
  15187. (org-no-warnings
  15188. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  15189. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15190. (define-key org-mode-map "\C-e" 'org-end-of-line)
  15191. (defun org-backward-sentence (&optional arg)
  15192. "Go to beginning of sentence, or beginning of table field.
  15193. This will call `backward-sentence' or `org-table-beginning-of-field',
  15194. depending on context."
  15195. (interactive "P")
  15196. (cond
  15197. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  15198. (t (call-interactively 'backward-sentence))))
  15199. (defun org-forward-sentence (&optional arg)
  15200. "Go to end of sentence, or end of table field.
  15201. This will call `forward-sentence' or `org-table-end-of-field',
  15202. depending on context."
  15203. (interactive "P")
  15204. (cond
  15205. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  15206. (t (call-interactively 'forward-sentence))))
  15207. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  15208. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  15209. (defun org-kill-line (&optional arg)
  15210. "Kill line, to tags or end of line."
  15211. (interactive "P")
  15212. (cond
  15213. ((or (not org-special-ctrl-k)
  15214. (bolp)
  15215. (not (org-on-heading-p)))
  15216. (call-interactively 'kill-line))
  15217. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  15218. (kill-region (point) (match-beginning 1))
  15219. (org-set-tags nil t))
  15220. (t (kill-region (point) (point-at-eol)))))
  15221. (define-key org-mode-map "\C-k" 'org-kill-line)
  15222. (defun org-yank (&optional arg)
  15223. "Yank. If the kill is a subtree, treat it specially.
  15224. This command will look at the current kill and check if is a single
  15225. subtree, or a series of subtrees[1]. If it passes the test, and if the
  15226. cursor is at the beginning of a line or after the stars of a currently
  15227. empty headline, then the yank is handled specially. How exactly depends
  15228. on the value of the following variables, both set by default.
  15229. org-yank-folded-subtrees
  15230. When set, the subtree(s) will be folded after insertion, but only
  15231. if doing so would now swallow text after the yanked text.
  15232. org-yank-adjusted-subtrees
  15233. When set, the subtree will be promoted or demoted in order to
  15234. fit into the local outline tree structure, which means that the level
  15235. will be adjusted so that it becomes the smaller one of the two
  15236. *visible* surrounding headings.
  15237. Any prefix to this command will cause `yank' to be called directly with
  15238. no special treatment. In particular, a simple `C-u' prefix will just
  15239. plainly yank the text as it is.
  15240. \[1] The test checks if the first non-white line is a heading
  15241. and if there are no other headings with fewer stars."
  15242. (interactive "P")
  15243. (org-yank-generic 'yank arg))
  15244. (defun org-yank-generic (command arg)
  15245. "Perform some yank-like command.
  15246. This function implements the behavior described in the `org-yank'
  15247. documentation. However, it has been generalized to work for any
  15248. interactive command with similar behavior."
  15249. ;; pretend to be command COMMAND
  15250. (setq this-command command)
  15251. (if arg
  15252. (call-interactively command)
  15253. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  15254. (and (org-kill-is-subtree-p)
  15255. (or (bolp)
  15256. (and (looking-at "[ \t]*$")
  15257. (string-match
  15258. "\\`\\*+\\'"
  15259. (buffer-substring (point-at-bol) (point)))))))
  15260. swallowp)
  15261. (cond
  15262. ((and subtreep org-yank-folded-subtrees)
  15263. (let ((beg (point))
  15264. end)
  15265. (if (and subtreep org-yank-adjusted-subtrees)
  15266. (org-paste-subtree nil nil 'for-yank)
  15267. (call-interactively command))
  15268. (setq end (point))
  15269. (goto-char beg)
  15270. (when (and (bolp) subtreep
  15271. (not (setq swallowp
  15272. (org-yank-folding-would-swallow-text beg end))))
  15273. (or (looking-at outline-regexp)
  15274. (re-search-forward (concat "^" outline-regexp) end t))
  15275. (while (and (< (point) end) (looking-at outline-regexp))
  15276. (hide-subtree)
  15277. (org-cycle-show-empty-lines 'folded)
  15278. (condition-case nil
  15279. (outline-forward-same-level 1)
  15280. (error (goto-char end)))))
  15281. (when swallowp
  15282. (message
  15283. "Inserted text not folded because that would swallow text"))
  15284. (goto-char end)
  15285. (skip-chars-forward " \t\n\r")
  15286. (beginning-of-line 1)
  15287. (push-mark beg 'nomsg)))
  15288. ((and subtreep org-yank-adjusted-subtrees)
  15289. (let ((beg (point-at-bol)))
  15290. (org-paste-subtree nil nil 'for-yank)
  15291. (push-mark beg 'nomsg)))
  15292. (t
  15293. (call-interactively command))))))
  15294. (defun org-yank-folding-would-swallow-text (beg end)
  15295. "Would hide-subtree at BEG swallow any text after END?"
  15296. (let (level)
  15297. (save-excursion
  15298. (goto-char beg)
  15299. (when (or (looking-at outline-regexp)
  15300. (re-search-forward (concat "^" outline-regexp) end t))
  15301. (setq level (org-outline-level)))
  15302. (goto-char end)
  15303. (skip-chars-forward " \t\r\n\v\f")
  15304. (if (or (eobp)
  15305. (and (bolp) (looking-at org-outline-regexp)
  15306. (<= (org-outline-level) level)))
  15307. nil ; Nothing would be swallowed
  15308. t)))) ; something would swallow
  15309. (define-key org-mode-map "\C-y" 'org-yank)
  15310. (defun org-invisible-p ()
  15311. "Check if point is at a character currently not visible."
  15312. ;; Early versions of noutline don't have `outline-invisible-p'.
  15313. (if (fboundp 'outline-invisible-p)
  15314. (outline-invisible-p)
  15315. (get-char-property (point) 'invisible)))
  15316. (defun org-invisible-p2 ()
  15317. "Check if point is at a character currently not visible."
  15318. (save-excursion
  15319. (if (and (eolp) (not (bobp))) (backward-char 1))
  15320. ;; Early versions of noutline don't have `outline-invisible-p'.
  15321. (if (fboundp 'outline-invisible-p)
  15322. (outline-invisible-p)
  15323. (get-char-property (point) 'invisible))))
  15324. (defun org-back-to-heading (&optional invisible-ok)
  15325. "Call `outline-back-to-heading', but provide a better error message."
  15326. (condition-case nil
  15327. (outline-back-to-heading invisible-ok)
  15328. (error (error "Before first headline at position %d in buffer %s"
  15329. (point) (current-buffer)))))
  15330. (defun org-before-first-heading-p ()
  15331. "Before first heading?"
  15332. (save-excursion
  15333. (null (re-search-backward "^\\*+ " nil t))))
  15334. (defalias 'org-on-heading-p 'outline-on-heading-p)
  15335. (defalias 'org-at-heading-p 'outline-on-heading-p)
  15336. (defun org-at-heading-or-item-p ()
  15337. (or (org-on-heading-p) (org-at-item-p)))
  15338. (defun org-on-target-p ()
  15339. (or (org-in-regexp org-radio-target-regexp)
  15340. (org-in-regexp org-target-regexp)))
  15341. (defun org-up-heading-all (arg)
  15342. "Move to the heading line of which the present line is a subheading.
  15343. This function considers both visible and invisible heading lines.
  15344. With argument, move up ARG levels."
  15345. (if (fboundp 'outline-up-heading-all)
  15346. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15347. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15348. (defun org-up-heading-safe ()
  15349. "Move to the heading line of which the present line is a subheading.
  15350. This version will not throw an error. It will return the level of the
  15351. headline found, or nil if no higher level is found.
  15352. Also, this function will be a lot faster than `outline-up-heading',
  15353. because it relies on stars being the outline starters. This can really
  15354. make a significant difference in outlines with very many siblings."
  15355. (let (start-level re)
  15356. (org-back-to-heading t)
  15357. (setq start-level (funcall outline-level))
  15358. (if (equal start-level 1)
  15359. nil
  15360. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  15361. (if (re-search-backward re nil t)
  15362. (funcall outline-level)))))
  15363. (defun org-first-sibling-p ()
  15364. "Is this heading the first child of its parents?"
  15365. (interactive)
  15366. (let ((re (concat "^" outline-regexp))
  15367. level l)
  15368. (unless (org-at-heading-p t)
  15369. (error "Not at a heading"))
  15370. (setq level (funcall outline-level))
  15371. (save-excursion
  15372. (if (not (re-search-backward re nil t))
  15373. t
  15374. (setq l (funcall outline-level))
  15375. (< l level)))))
  15376. (defun org-goto-sibling (&optional previous)
  15377. "Goto the next sibling, even if it is invisible.
  15378. When PREVIOUS is set, go to the previous sibling instead. Returns t
  15379. when a sibling was found. When none is found, return nil and don't
  15380. move point."
  15381. (let ((fun (if previous 're-search-backward 're-search-forward))
  15382. (pos (point))
  15383. (re (concat "^" outline-regexp))
  15384. level l)
  15385. (when (condition-case nil (org-back-to-heading t) (error nil))
  15386. (setq level (funcall outline-level))
  15387. (catch 'exit
  15388. (or previous (forward-char 1))
  15389. (while (funcall fun re nil t)
  15390. (setq l (funcall outline-level))
  15391. (when (< l level) (goto-char pos) (throw 'exit nil))
  15392. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  15393. (goto-char pos)
  15394. nil))))
  15395. (defun org-show-siblings ()
  15396. "Show all siblings of the current headline."
  15397. (save-excursion
  15398. (while (org-goto-sibling) (org-flag-heading nil)))
  15399. (save-excursion
  15400. (while (org-goto-sibling 'previous)
  15401. (org-flag-heading nil))))
  15402. (defun org-show-hidden-entry ()
  15403. "Show an entry where even the heading is hidden."
  15404. (save-excursion
  15405. (org-show-entry)))
  15406. (defun org-flag-heading (flag &optional entry)
  15407. "Flag the current heading. FLAG non-nil means make invisible.
  15408. When ENTRY is non-nil, show the entire entry."
  15409. (save-excursion
  15410. (org-back-to-heading t)
  15411. ;; Check if we should show the entire entry
  15412. (if entry
  15413. (progn
  15414. (org-show-entry)
  15415. (save-excursion
  15416. (and (outline-next-heading)
  15417. (org-flag-heading nil))))
  15418. (outline-flag-region (max (point-min) (1- (point)))
  15419. (save-excursion (outline-end-of-heading) (point))
  15420. flag))))
  15421. (defun org-get-next-sibling ()
  15422. "Move to next heading of the same level, and return point.
  15423. If there is no such heading, return nil.
  15424. This is like outline-next-sibling, but invisible headings are ok."
  15425. (let ((level (funcall outline-level)))
  15426. (outline-next-heading)
  15427. (while (and (not (eobp)) (> (funcall outline-level) level))
  15428. (outline-next-heading))
  15429. (if (or (eobp) (< (funcall outline-level) level))
  15430. nil
  15431. (point))))
  15432. (defun org-get-last-sibling ()
  15433. "Move to previous heading of the same level, and return point.
  15434. If there is no such heading, return nil."
  15435. (let ((opoint (point))
  15436. (level (funcall outline-level)))
  15437. (outline-previous-heading)
  15438. (when (and (/= (point) opoint) (outline-on-heading-p t))
  15439. (while (and (> (funcall outline-level) level)
  15440. (not (bobp)))
  15441. (outline-previous-heading))
  15442. (if (< (funcall outline-level) level)
  15443. nil
  15444. (point)))))
  15445. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  15446. ;; This contains an exact copy of the original function, but it uses
  15447. ;; `org-back-to-heading', to make it work also in invisible
  15448. ;; trees. And is uses an invisible-OK argument.
  15449. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15450. ;; Furthermore, when used inside Org, finding the end of a large subtree
  15451. ;; with many children and grandchildren etc, this can be much faster
  15452. ;; than the outline version.
  15453. (org-back-to-heading invisible-OK)
  15454. (let ((first t)
  15455. (level (funcall outline-level)))
  15456. (if (and (org-mode-p) (< level 1000))
  15457. ;; A true heading (not a plain list item), in Org-mode
  15458. ;; This means we can easily find the end by looking
  15459. ;; only for the right number of stars. Using a regexp to do
  15460. ;; this is so much faster than using a Lisp loop.
  15461. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  15462. (forward-char 1)
  15463. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  15464. ;; something else, do it the slow way
  15465. (while (and (not (eobp))
  15466. (or first (> (funcall outline-level) level)))
  15467. (setq first nil)
  15468. (outline-next-heading)))
  15469. (unless to-heading
  15470. (if (memq (preceding-char) '(?\n ?\^M))
  15471. (progn
  15472. ;; Go to end of line before heading
  15473. (forward-char -1)
  15474. (if (memq (preceding-char) '(?\n ?\^M))
  15475. ;; leave blank line before heading
  15476. (forward-char -1))))))
  15477. (point))
  15478. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  15479. "Use Org version in org-mode, for dramatic speed-up."
  15480. (if (eq major-mode 'org-mode)
  15481. (progn
  15482. (org-end-of-subtree nil t)
  15483. (unless (eobp) (backward-char 1)))
  15484. ad-do-it))
  15485. (defun org-forward-same-level (arg &optional invisible-ok)
  15486. "Move forward to the arg'th subheading at same level as this one.
  15487. Stop at the first and last subheadings of a superior heading."
  15488. (interactive "p")
  15489. (org-back-to-heading invisible-ok)
  15490. (org-on-heading-p)
  15491. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15492. (re (format "^\\*\\{1,%d\\} " level))
  15493. l)
  15494. (forward-char 1)
  15495. (while (> arg 0)
  15496. (while (and (re-search-forward re nil 'move)
  15497. (setq l (- (match-end 0) (match-beginning 0) 1))
  15498. (= l level)
  15499. (not invisible-ok)
  15500. (org-invisible-p))
  15501. (if (< l level) (setq arg 1)))
  15502. (setq arg (1- arg)))
  15503. (beginning-of-line 1)))
  15504. (defun org-backward-same-level (arg &optional invisible-ok)
  15505. "Move backward to the arg'th subheading at same level as this one.
  15506. Stop at the first and last subheadings of a superior heading."
  15507. (interactive "p")
  15508. (org-back-to-heading)
  15509. (org-on-heading-p)
  15510. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  15511. (re (format "^\\*\\{1,%d\\} " level))
  15512. l)
  15513. (while (> arg 0)
  15514. (while (and (re-search-backward re nil 'move)
  15515. (setq l (- (match-end 0) (match-beginning 0) 1))
  15516. (= l level)
  15517. (not invisible-ok)
  15518. (org-invisible-p))
  15519. (if (< l level) (setq arg 1)))
  15520. (setq arg (1- arg)))))
  15521. (defun org-show-subtree ()
  15522. "Show everything after this heading at deeper levels."
  15523. (outline-flag-region
  15524. (point)
  15525. (save-excursion
  15526. (org-end-of-subtree t t))
  15527. nil))
  15528. (defun org-show-entry ()
  15529. "Show the body directly following this heading.
  15530. Show the heading too, if it is currently invisible."
  15531. (interactive)
  15532. (save-excursion
  15533. (condition-case nil
  15534. (progn
  15535. (org-back-to-heading t)
  15536. (outline-flag-region
  15537. (max (point-min) (1- (point)))
  15538. (save-excursion
  15539. (if (re-search-forward
  15540. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  15541. (match-beginning 1)
  15542. (point-max)))
  15543. nil)
  15544. (org-cycle-hide-drawers 'children))
  15545. (error nil))))
  15546. (defun org-make-options-regexp (kwds &optional extra)
  15547. "Make a regular expression for keyword lines."
  15548. (concat
  15549. "^"
  15550. "#?[ \t]*\\+\\("
  15551. (mapconcat 'regexp-quote kwds "\\|")
  15552. (if extra (concat "\\|" extra))
  15553. "\\):[ \t]*"
  15554. "\\(.*\\)"))
  15555. ;; Make isearch reveal the necessary context
  15556. (defun org-isearch-end ()
  15557. "Reveal context after isearch exits."
  15558. (when isearch-success ; only if search was successful
  15559. (if (featurep 'xemacs)
  15560. ;; Under XEmacs, the hook is run in the correct place,
  15561. ;; we directly show the context.
  15562. (org-show-context 'isearch)
  15563. ;; In Emacs the hook runs *before* restoring the overlays.
  15564. ;; So we have to use a one-time post-command-hook to do this.
  15565. ;; (Emacs 22 has a special variable, see function `org-mode')
  15566. (unless (and (boundp 'isearch-mode-end-hook-quit)
  15567. isearch-mode-end-hook-quit)
  15568. ;; Only when the isearch was not quitted.
  15569. (org-add-hook 'post-command-hook 'org-isearch-post-command
  15570. 'append 'local)))))
  15571. (defun org-isearch-post-command ()
  15572. "Remove self from hook, and show context."
  15573. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  15574. (org-show-context 'isearch))
  15575. ;;;; Integration with and fixes for other packages
  15576. ;;; Imenu support
  15577. (defvar org-imenu-markers nil
  15578. "All markers currently used by Imenu.")
  15579. (make-variable-buffer-local 'org-imenu-markers)
  15580. (defun org-imenu-new-marker (&optional pos)
  15581. "Return a new marker for use by Imenu, and remember the marker."
  15582. (let ((m (make-marker)))
  15583. (move-marker m (or pos (point)))
  15584. (push m org-imenu-markers)
  15585. m))
  15586. (defun org-imenu-get-tree ()
  15587. "Produce the index for Imenu."
  15588. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  15589. (setq org-imenu-markers nil)
  15590. (let* ((n org-imenu-depth)
  15591. (re (concat "^" outline-regexp))
  15592. (subs (make-vector (1+ n) nil))
  15593. (last-level 0)
  15594. m level head)
  15595. (save-excursion
  15596. (save-restriction
  15597. (widen)
  15598. (goto-char (point-max))
  15599. (while (re-search-backward re nil t)
  15600. (setq level (org-reduced-level (funcall outline-level)))
  15601. (when (<= level n)
  15602. (looking-at org-complex-heading-regexp)
  15603. (setq head (org-link-display-format
  15604. (org-match-string-no-properties 4))
  15605. m (org-imenu-new-marker))
  15606. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  15607. (if (>= level last-level)
  15608. (push (cons head m) (aref subs level))
  15609. (push (cons head (aref subs (1+ level))) (aref subs level))
  15610. (loop for i from (1+ level) to n do (aset subs i nil)))
  15611. (setq last-level level)))))
  15612. (aref subs 1)))
  15613. (eval-after-load "imenu"
  15614. '(progn
  15615. (add-hook 'imenu-after-jump-hook
  15616. (lambda ()
  15617. (if (eq major-mode 'org-mode)
  15618. (org-show-context 'org-goto))))))
  15619. (defun org-link-display-format (link)
  15620. "Replace a link with either the description, or the link target
  15621. if no description is present"
  15622. (save-match-data
  15623. (if (string-match org-bracket-link-analytic-regexp link)
  15624. (replace-match (or (match-string 5 link)
  15625. (concat (match-string 1 link)
  15626. (match-string 3 link)))
  15627. nil nil link)
  15628. link)))
  15629. ;; Speedbar support
  15630. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  15631. "Overlay marking the agenda restriction line in speedbar.")
  15632. (org-overlay-put org-speedbar-restriction-lock-overlay
  15633. 'face 'org-agenda-restriction-lock)
  15634. (org-overlay-put org-speedbar-restriction-lock-overlay
  15635. 'help-echo "Agendas are currently limited to this item.")
  15636. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  15637. (defun org-speedbar-set-agenda-restriction ()
  15638. "Restrict future agenda commands to the location at point in speedbar.
  15639. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  15640. (interactive)
  15641. (require 'org-agenda)
  15642. (let (p m tp np dir txt)
  15643. (cond
  15644. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15645. 'org-imenu t))
  15646. (setq m (get-text-property p 'org-imenu-marker))
  15647. (save-excursion
  15648. (save-restriction
  15649. (set-buffer (marker-buffer m))
  15650. (goto-char m)
  15651. (org-agenda-set-restriction-lock 'subtree))))
  15652. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  15653. 'speedbar-function 'speedbar-find-file))
  15654. (setq tp (previous-single-property-change
  15655. (1+ p) 'speedbar-function)
  15656. np (next-single-property-change
  15657. tp 'speedbar-function)
  15658. dir (speedbar-line-directory)
  15659. txt (buffer-substring-no-properties (or tp (point-min))
  15660. (or np (point-max))))
  15661. (save-excursion
  15662. (save-restriction
  15663. (set-buffer (find-file-noselect
  15664. (let ((default-directory dir))
  15665. (expand-file-name txt))))
  15666. (unless (org-mode-p)
  15667. (error "Cannot restrict to non-Org-mode file"))
  15668. (org-agenda-set-restriction-lock 'file))))
  15669. (t (error "Don't know how to restrict Org-mode's agenda")))
  15670. (org-move-overlay org-speedbar-restriction-lock-overlay
  15671. (point-at-bol) (point-at-eol))
  15672. (setq current-prefix-arg nil)
  15673. (org-agenda-maybe-redo)))
  15674. (eval-after-load "speedbar"
  15675. '(progn
  15676. (speedbar-add-supported-extension ".org")
  15677. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  15678. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  15679. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  15680. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15681. (add-hook 'speedbar-visiting-tag-hook
  15682. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  15683. ;;; Fixes and Hacks for problems with other packages
  15684. ;; Make flyspell not check words in links, to not mess up our keymap
  15685. (defun org-mode-flyspell-verify ()
  15686. "Don't let flyspell put overlays at active buttons."
  15687. (and (not (get-text-property (point) 'keymap))
  15688. (not (get-text-property (point) 'org-no-flyspell))))
  15689. (defun org-remove-flyspell-overlays-in (beg end)
  15690. "Remove flyspell overlays in region."
  15691. (and (org-bound-and-true-p flyspell-mode)
  15692. (fboundp 'flyspell-delete-region-overlays)
  15693. (flyspell-delete-region-overlays beg end))
  15694. (add-text-properties beg end '(org-no-flyspell t)))
  15695. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15696. (eval-after-load "bookmark"
  15697. '(if (boundp 'bookmark-after-jump-hook)
  15698. ;; We can use the hook
  15699. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15700. ;; Hook not available, use advice
  15701. (defadvice bookmark-jump (after org-make-visible activate)
  15702. "Make the position visible."
  15703. (org-bookmark-jump-unhide))))
  15704. ;; Make sure saveplace show the location if it was hidden
  15705. (eval-after-load "saveplace"
  15706. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  15707. "Make the position visible."
  15708. (org-bookmark-jump-unhide)))
  15709. (defun org-bookmark-jump-unhide ()
  15710. "Unhide the current position, to show the bookmark location."
  15711. (and (org-mode-p)
  15712. (or (org-invisible-p)
  15713. (save-excursion (goto-char (max (point-min) (1- (point))))
  15714. (org-invisible-p)))
  15715. (org-show-context 'bookmark-jump)))
  15716. ;; Make session.el ignore our circular variable
  15717. (eval-after-load "session"
  15718. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  15719. ;;;; Experimental code
  15720. (defun org-closed-in-range ()
  15721. "Sparse tree of items closed in a certain time range.
  15722. Still experimental, may disappear in the future."
  15723. (interactive)
  15724. ;; Get the time interval from the user.
  15725. (let* ((time1 (org-float-time
  15726. (org-read-date nil 'to-time nil "Starting date: ")))
  15727. (time2 (org-float-time
  15728. (org-read-date nil 'to-time nil "End date:")))
  15729. ;; callback function
  15730. (callback (lambda ()
  15731. (let ((time
  15732. (org-float-time
  15733. (apply 'encode-time
  15734. (org-parse-time-string
  15735. (match-string 1))))))
  15736. ;; check if time in interval
  15737. (and (>= time time1) (<= time time2))))))
  15738. ;; make tree, check each match with the callback
  15739. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  15740. ;;;; Finish up
  15741. (provide 'org)
  15742. (run-hooks 'org-load-hook)
  15743. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15744. ;;; org.el ends here