org.el 691 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533
  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: TAG=6.35c
  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-entities)
  83. (require 'org-compat)
  84. (require 'org-faces)
  85. (require 'org-list)
  86. (require 'org-src)
  87. (require 'org-footnote)
  88. ;;;; Customization variables
  89. ;;; Version
  90. (defconst org-version "TAG=6.35c"
  91. "The version number of the file org.el.")
  92. (defun org-version (&optional here)
  93. "Show the org-mode version in the echo area.
  94. With prefix arg HERE, insert it at point."
  95. (interactive "P")
  96. (let* ((origin default-directory)
  97. (version org-version)
  98. (git-version)
  99. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  100. (when (and (file-exists-p (expand-file-name ".git" dir))
  101. (executable-find "git"))
  102. (unwind-protect
  103. (progn
  104. (cd dir)
  105. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  106. (with-current-buffer "*Shell Command Output*"
  107. (goto-char (point-min))
  108. (setq git-version (buffer-substring (point) (point-at-eol))))
  109. (subst-char-in-string ?- ?. git-version t)
  110. (when (string-match "\\S-"
  111. (shell-command-to-string
  112. "git diff-index --name-only HEAD --"))
  113. (setq git-version (concat git-version ".dirty")))
  114. (setq version (concat version " (" git-version ")"))))
  115. (cd origin)))
  116. (setq version (format "Org-mode version %s" version))
  117. (if here (insert version))
  118. (message version)))
  119. ;;; Compatibility constants
  120. ;;; The custom variables
  121. (defgroup org nil
  122. "Outline-based notes management and organizer."
  123. :tag "Org"
  124. :group 'outlines
  125. :group 'calendar)
  126. (defcustom org-mode-hook nil
  127. "Mode hook for Org-mode, run after the mode was turned on."
  128. :group 'org
  129. :type 'hook)
  130. (defcustom org-load-hook nil
  131. "Hook that is run after org.el has been loaded."
  132. :group 'org
  133. :type 'hook)
  134. (defvar org-modules) ; defined below
  135. (defvar org-modules-loaded nil
  136. "Have the modules been loaded already?")
  137. (defun org-load-modules-maybe (&optional force)
  138. "Load all extensions listed in `org-modules'."
  139. (when (or force (not org-modules-loaded))
  140. (mapc (lambda (ext)
  141. (condition-case nil (require ext)
  142. (error (message "Problems while trying to load feature `%s'" ext))))
  143. org-modules)
  144. (setq org-modules-loaded t)))
  145. (defun org-set-modules (var value)
  146. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  147. (set var value)
  148. (when (featurep 'org)
  149. (org-load-modules-maybe 'force)))
  150. (when (org-bound-and-true-p org-modules)
  151. (let ((a (member 'org-infojs org-modules)))
  152. (and a (setcar a 'org-jsinfo))))
  153. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  154. "Modules that should always be loaded together with org.el.
  155. If a description starts with <C>, the file is not part of Emacs
  156. and loading it will require that you have downloaded and properly installed
  157. the org-mode distribution.
  158. You can also use this system to load external packages (i.e. neither Org
  159. core modules, nor modules from the CONTRIB directory). Just add symbols
  160. to the end of the list. If the package is called org-xyz.el, then you need
  161. to add the symbol `xyz', and the package must have a call to
  162. (provide 'org-xyz)"
  163. :group 'org
  164. :set 'org-set-modules
  165. :type
  166. '(set :greedy t
  167. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  168. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  169. (const :tag " crypt: Encryption of subtrees" org-crypt)
  170. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  171. (const :tag " docview: Links to doc-view buffers" org-docview)
  172. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  173. (const :tag " id: Global IDs for identifying entries" org-id)
  174. (const :tag " info: Links to Info nodes" org-info)
  175. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  176. (const :tag " habit: Track your consistency with habits" org-habit)
  177. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  178. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  179. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  180. (const :tag " mew Links to Mew folders/messages" org-mew)
  181. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  182. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  183. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  184. (const :tag " vm: Links to VM folders/messages" org-vm)
  185. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  186. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  187. (const :tag " mouse: Additional mouse support" org-mouse)
  188. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  189. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  190. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  191. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  192. (const :tag "C collector: Collect properties into tables" org-collector)
  193. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  194. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  195. (const :tag "C eval: Include command output as text" org-eval)
  196. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  197. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  198. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  199. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  200. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  201. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  202. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  203. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  204. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  205. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  206. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  207. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  208. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  209. (const :tag "C registry: A registry for Org-mode links" org-registry)
  210. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  211. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  212. (const :tag "C secretary: Team management with org-mode" org-secretary)
  213. (const :tag "C special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  214. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  215. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  216. (const :tag "C track: Keep up with Org-mode development" org-track)
  217. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  218. (defcustom org-support-shift-select nil
  219. "Non-nil means make shift-cursor commands select text when possible.
  220. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  221. selecting a region, or enlarge thusly regions started in this way.
  222. In Org-mode, in special contexts, these same keys are used for other
  223. purposes, important enough to compete with shift selection. Org tries
  224. to balance these needs by supporting `shift-select-mode' outside these
  225. special contexts, under control of this variable.
  226. The default of this variable is nil, to avoid confusing behavior. Shifted
  227. cursor keys will then execute Org commands in the following contexts:
  228. - on a headline, changing TODO state (left/right) and priority (up/down)
  229. - on a time stamp, changing the time
  230. - in a plain list item, changing the bullet type
  231. - in a property definition line, switching between allowed values
  232. - in the BEGIN line of a clock table (changing the time block).
  233. Outside these contexts, the commands will throw an error.
  234. When this variable is t and the cursor is not in a special context,
  235. Org-mode will support shift-selection for making and enlarging regions.
  236. To make this more effective, the bullet cycling will no longer happen
  237. anywhere in an item line, but only if the cursor is exactly on the bullet.
  238. If you set this variable to the symbol `always', then the keys
  239. will not be special in headlines, property lines, and item lines, to make
  240. shift selection work there as well. If this is what you want, you can
  241. use the following alternative commands: `C-c C-t' and `C-c ,' to
  242. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  243. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  244. edited by hand or in column view.
  245. However, when the cursor is on a timestamp, shift-cursor commands
  246. will still edit the time stamp - this is just too good to give up.
  247. XEmacs user should have this variable set to nil, because shift-select-mode
  248. is Emacs 23 only."
  249. :group 'org
  250. :type '(choice
  251. (const :tag "Never" nil)
  252. (const :tag "When outside special context" t)
  253. (const :tag "Everywhere except timestamps" always)))
  254. (defgroup org-startup nil
  255. "Options concerning startup of Org-mode."
  256. :tag "Org Startup"
  257. :group 'org)
  258. (defcustom org-startup-folded t
  259. "Non-nil means entering Org-mode will switch to OVERVIEW.
  260. This can also be configured on a per-file basis by adding one of
  261. the following lines anywhere in the buffer:
  262. #+STARTUP: fold (or `overview', this is equivalent)
  263. #+STARTUP: nofold (or `showall', this is equivalent)
  264. #+STARTUP: content
  265. #+STARTUP: showeverything"
  266. :group 'org-startup
  267. :type '(choice
  268. (const :tag "nofold: show all" nil)
  269. (const :tag "fold: overview" t)
  270. (const :tag "content: all headlines" content)
  271. (const :tag "show everything, even drawers" showeverything)))
  272. (defcustom org-startup-truncated t
  273. "Non-nil means entering Org-mode will set `truncate-lines'.
  274. This is useful since some lines containing links can be very long and
  275. uninteresting. Also tables look terrible when wrapped."
  276. :group 'org-startup
  277. :type 'boolean)
  278. (defcustom org-startup-indented nil
  279. "Non-nil means turn on `org-indent-mode' on startup.
  280. This can also be configured on a per-file basis by adding one of
  281. the following lines anywhere in the buffer:
  282. #+STARTUP: indent
  283. #+STARTUP: noindent"
  284. :group 'org-structure
  285. :type '(choice
  286. (const :tag "Not" nil)
  287. (const :tag "Globally (slow on startup in large files)" t)))
  288. (defcustom org-startup-with-beamer-mode nil
  289. "Non-nil means turn on `org-beamer-mode' on startup.
  290. This can also be configured on a per-file basis by adding one of
  291. the following lines anywhere in the buffer:
  292. #+STARTUP: beamer"
  293. :group 'org-startup
  294. :type 'boolean)
  295. (defcustom org-startup-align-all-tables nil
  296. "Non-nil means align all tables when visiting a file.
  297. This is useful when the column width in tables is forced with <N> cookies
  298. in table fields. Such tables will look correct only after the first re-align.
  299. This can also be configured on a per-file basis by adding one of
  300. the following lines anywhere in the buffer:
  301. #+STARTUP: align
  302. #+STARTUP: noalign"
  303. :group 'org-startup
  304. :type 'boolean)
  305. (defcustom org-insert-mode-line-in-empty-file nil
  306. "Non-nil means insert the first line setting Org-mode in empty files.
  307. When the function `org-mode' is called interactively in an empty file, this
  308. normally means that the file name does not automatically trigger Org-mode.
  309. To ensure that the file will always be in Org-mode in the future, a
  310. line enforcing Org-mode will be inserted into the buffer, if this option
  311. has been set."
  312. :group 'org-startup
  313. :type 'boolean)
  314. (defcustom org-replace-disputed-keys nil
  315. "Non-nil means use alternative key bindings for some keys.
  316. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  317. These keys are also used by other packages like shift-selection-mode'
  318. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  319. If you want to use Org-mode together with one of these other modes,
  320. or more generally if you would like to move some Org-mode commands to
  321. other keys, set this variable and configure the keys with the variable
  322. `org-disputed-keys'.
  323. This option is only relevant at load-time of Org-mode, and must be set
  324. *before* org.el is loaded. Changing it requires a restart of Emacs to
  325. become effective."
  326. :group 'org-startup
  327. :type 'boolean)
  328. (defcustom org-use-extra-keys nil
  329. "Non-nil means use extra key sequence definitions for certain
  330. commands. This happens automatically if you run XEmacs or if
  331. window-system is nil. This variable lets you do the same
  332. manually. You must set it before loading org.
  333. Example: on Carbon Emacs 22 running graphically, with an external
  334. keyboard on a Powerbook, the default way of setting M-left might
  335. not work for either Alt or ESC. Setting this variable will make
  336. it work for ESC."
  337. :group 'org-startup
  338. :type 'boolean)
  339. (if (fboundp 'defvaralias)
  340. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  341. (defcustom org-disputed-keys
  342. '(([(shift up)] . [(meta p)])
  343. ([(shift down)] . [(meta n)])
  344. ([(shift left)] . [(meta -)])
  345. ([(shift right)] . [(meta +)])
  346. ([(control shift right)] . [(meta shift +)])
  347. ([(control shift left)] . [(meta shift -)]))
  348. "Keys for which Org-mode and other modes compete.
  349. This is an alist, cars are the default keys, second element specifies
  350. the alternative to use when `org-replace-disputed-keys' is t.
  351. Keys can be specified in any syntax supported by `define-key'.
  352. The value of this option takes effect only at Org-mode's startup,
  353. therefore you'll have to restart Emacs to apply it after changing."
  354. :group 'org-startup
  355. :type 'alist)
  356. (defun org-key (key)
  357. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  358. Or return the original if not disputed."
  359. (if org-replace-disputed-keys
  360. (let* ((nkey (key-description key))
  361. (x (org-find-if (lambda (x)
  362. (equal (key-description (car x)) nkey))
  363. org-disputed-keys)))
  364. (if x (cdr x) key))
  365. key))
  366. (defun org-find-if (predicate seq)
  367. (catch 'exit
  368. (while seq
  369. (if (funcall predicate (car seq))
  370. (throw 'exit (car seq))
  371. (pop seq)))))
  372. (defun org-defkey (keymap key def)
  373. "Define a key, possibly translated, as returned by `org-key'."
  374. (define-key keymap (org-key key) def))
  375. (defcustom org-ellipsis nil
  376. "The ellipsis to use in the Org-mode outline.
  377. When nil, just use the standard three dots. When a string, use that instead,
  378. When a face, use the standard 3 dots, but with the specified face.
  379. The change affects only Org-mode (which will then use its own display table).
  380. Changing this requires executing `M-x org-mode' in a buffer to become
  381. effective."
  382. :group 'org-startup
  383. :type '(choice (const :tag "Default" nil)
  384. (face :tag "Face" :value org-warning)
  385. (string :tag "String" :value "...#")))
  386. (defvar org-display-table nil
  387. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  388. (defgroup org-keywords nil
  389. "Keywords in Org-mode."
  390. :tag "Org Keywords"
  391. :group 'org)
  392. (defcustom org-deadline-string "DEADLINE:"
  393. "String to mark deadline entries.
  394. A deadline is this string, followed by a time stamp. Should be a word,
  395. terminated by a colon. You can insert a schedule keyword and
  396. a timestamp with \\[org-deadline].
  397. Changes become only effective after restarting Emacs."
  398. :group 'org-keywords
  399. :type 'string)
  400. (defcustom org-scheduled-string "SCHEDULED:"
  401. "String to mark scheduled TODO entries.
  402. A schedule is this string, followed by a time stamp. Should be a word,
  403. terminated by a colon. You can insert a schedule keyword and
  404. a timestamp with \\[org-schedule].
  405. Changes become only effective after restarting Emacs."
  406. :group 'org-keywords
  407. :type 'string)
  408. (defcustom org-closed-string "CLOSED:"
  409. "String used as the prefix for timestamps logging closing a TODO entry."
  410. :group 'org-keywords
  411. :type 'string)
  412. (defcustom org-clock-string "CLOCK:"
  413. "String used as prefix for timestamps clocking work hours on an item."
  414. :group 'org-keywords
  415. :type 'string)
  416. (defcustom org-comment-string "COMMENT"
  417. "Entries starting with this keyword will never be exported.
  418. An entry can be toggled between COMMENT and normal with
  419. \\[org-toggle-comment].
  420. Changes become only effective after restarting Emacs."
  421. :group 'org-keywords
  422. :type 'string)
  423. (defcustom org-quote-string "QUOTE"
  424. "Entries starting with this keyword will be exported in fixed-width font.
  425. Quoting applies only to the text in the entry following the headline, and does
  426. not extend beyond the next headline, even if that is lower level.
  427. An entry can be toggled between QUOTE and normal with
  428. \\[org-toggle-fixed-width-section]."
  429. :group 'org-keywords
  430. :type 'string)
  431. (defconst org-repeat-re
  432. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  433. "Regular expression for specifying repeated events.
  434. After a match, group 1 contains the repeat expression.")
  435. (defgroup org-structure nil
  436. "Options concerning the general structure of Org-mode files."
  437. :tag "Org Structure"
  438. :group 'org)
  439. (defgroup org-reveal-location nil
  440. "Options about how to make context of a location visible."
  441. :tag "Org Reveal Location"
  442. :group 'org-structure)
  443. (defconst org-context-choice
  444. '(choice
  445. (const :tag "Always" t)
  446. (const :tag "Never" nil)
  447. (repeat :greedy t :tag "Individual contexts"
  448. (cons
  449. (choice :tag "Context"
  450. (const agenda)
  451. (const org-goto)
  452. (const occur-tree)
  453. (const tags-tree)
  454. (const link-search)
  455. (const mark-goto)
  456. (const bookmark-jump)
  457. (const isearch)
  458. (const default))
  459. (boolean))))
  460. "Contexts for the reveal options.")
  461. (defcustom org-show-hierarchy-above '((default . t))
  462. "Non-nil means show full hierarchy when revealing a location.
  463. Org-mode often shows locations in an org-mode file which might have
  464. been invisible before. When this is set, the hierarchy of headings
  465. above the exposed location is shown.
  466. Turning this off for example for sparse trees makes them very compact.
  467. Instead of t, this can also be an alist specifying this option for different
  468. contexts. Valid contexts are
  469. agenda when exposing an entry from the agenda
  470. org-goto when using the command `org-goto' on key C-c C-j
  471. occur-tree when using the command `org-occur' on key C-c /
  472. tags-tree when constructing a sparse tree based on tags matches
  473. link-search when exposing search matches associated with a link
  474. mark-goto when exposing the jump goal of a mark
  475. bookmark-jump when exposing a bookmark location
  476. isearch when exiting from an incremental search
  477. default default for all contexts not set explicitly"
  478. :group 'org-reveal-location
  479. :type org-context-choice)
  480. (defcustom org-show-following-heading '((default . nil))
  481. "Non-nil means show following heading when revealing a location.
  482. Org-mode often shows locations in an org-mode file which might have
  483. been invisible before. When this is set, the heading following the
  484. match is shown.
  485. Turning this off for example for sparse trees makes them very compact,
  486. but makes it harder to edit the location of the match. In such a case,
  487. use the command \\[org-reveal] to show more context.
  488. Instead of t, this can also be an alist specifying this option for different
  489. contexts. See `org-show-hierarchy-above' for valid contexts."
  490. :group 'org-reveal-location
  491. :type org-context-choice)
  492. (defcustom org-show-siblings '((default . nil) (isearch t))
  493. "Non-nil means show all sibling heading when revealing a location.
  494. Org-mode often shows locations in an org-mode file which might have
  495. been invisible before. When this is set, the sibling of the current entry
  496. heading are all made visible. If `org-show-hierarchy-above' is t,
  497. the same happens on each level of the hierarchy above the current entry.
  498. By default this is on for the isearch context, off for all other contexts.
  499. Turning this off for example for sparse trees makes them very compact,
  500. but makes it harder to edit the location of the match. In such a case,
  501. use the command \\[org-reveal] to show more context.
  502. Instead of t, this can also be an alist specifying this option for different
  503. contexts. See `org-show-hierarchy-above' for valid contexts."
  504. :group 'org-reveal-location
  505. :type org-context-choice)
  506. (defcustom org-show-entry-below '((default . nil))
  507. "Non-nil means show the entry below a headline when revealing a location.
  508. Org-mode often shows locations in an org-mode file which might have
  509. been invisible before. When this is set, the text below the headline that is
  510. exposed is also shown.
  511. By default this is off for all contexts.
  512. Instead of t, this can also be an alist specifying this option for different
  513. contexts. See `org-show-hierarchy-above' for valid contexts."
  514. :group 'org-reveal-location
  515. :type org-context-choice)
  516. (defcustom org-indirect-buffer-display 'other-window
  517. "How should indirect tree buffers be displayed?
  518. This applies to indirect buffers created with the commands
  519. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  520. Valid values are:
  521. current-window Display in the current window
  522. other-window Just display in another window.
  523. dedicated-frame Create one new frame, and re-use it each time.
  524. new-frame Make a new frame each time. Note that in this case
  525. previously-made indirect buffers are kept, and you need to
  526. kill these buffers yourself."
  527. :group 'org-structure
  528. :group 'org-agenda-windows
  529. :type '(choice
  530. (const :tag "In current window" current-window)
  531. (const :tag "In current frame, other window" other-window)
  532. (const :tag "Each time a new frame" new-frame)
  533. (const :tag "One dedicated frame" dedicated-frame)))
  534. (defcustom org-use-speed-commands nil
  535. "Non-nil means activate single letter commands at beginning of a headline.
  536. This may also be a function to test for appropriate locations where speed
  537. commands should be active."
  538. :group 'org-structure
  539. :type '(choice
  540. (const :tag "Never" nil)
  541. (const :tag "At beginning of headline stars" t)
  542. (function)))
  543. (defcustom org-speed-commands-user nil
  544. "Alist of additional speed commands.
  545. This list will be checked before `org-speed-commands-default'
  546. when the variable `org-use-speed-commands' is non-nil
  547. and when the cursor is at the beginning of a headline.
  548. The car if each entry is a string with a single letter, which must
  549. be assigned to `self-insert-command' in the global map.
  550. The cdr is either a command to be called interactively, a function
  551. to be called, or a form to be evaluated.
  552. An entry that is just a list with a single string will be interpreted
  553. as a descriptive headline that will be added when listing the speed
  554. copmmands in the Help buffer using the `?' speed command."
  555. :group 'org-structure
  556. :type '(repeat :value ("k" . ignore)
  557. (choice :value ("k" . ignore)
  558. (list :tag "Descriptive Headline" (string :tag "Headline"))
  559. (cons :tag "Letter and Command"
  560. (string :tag "Command letter")
  561. (choice
  562. (function)
  563. (sexp))))))
  564. (defgroup org-cycle nil
  565. "Options concerning visibility cycling in Org-mode."
  566. :tag "Org Cycle"
  567. :group 'org-structure)
  568. (defcustom org-cycle-skip-children-state-if-no-children t
  569. "Non-nil means skip CHILDREN state in entries that don't have any."
  570. :group 'org-cycle
  571. :type 'boolean)
  572. (defcustom org-cycle-max-level nil
  573. "Maximum level which should still be subject to visibility cycling.
  574. Levels higher than this will, for cycling, be treated as text, not a headline.
  575. When `org-odd-levels-only' is set, a value of N in this variable actually
  576. means 2N-1 stars as the limiting headline.
  577. When nil, cycle all levels.
  578. Note that the limiting level of cycling is also influenced by
  579. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  580. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  581. than its value."
  582. :group 'org-cycle
  583. :type '(choice
  584. (const :tag "No limit" nil)
  585. (integer :tag "Maximum level")))
  586. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  587. "Names of drawers. Drawers are not opened by cycling on the headline above.
  588. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  589. this:
  590. :DRAWERNAME:
  591. .....
  592. :END:
  593. The drawer \"PROPERTIES\" is special for capturing properties through
  594. the property API.
  595. Drawers can be defined on the per-file basis with a line like:
  596. #+DRAWERS: HIDDEN STATE PROPERTIES"
  597. :group 'org-structure
  598. :group 'org-cycle
  599. :type '(repeat (string :tag "Drawer Name")))
  600. (defcustom org-hide-block-startup nil
  601. "Non-nil means entering Org-mode will fold all blocks.
  602. This can also be set in on a per-file basis with
  603. #+STARTUP: hideblocks
  604. #+STARTUP: showblocks"
  605. :group 'org-startup
  606. :group 'org-cycle
  607. :type 'boolean)
  608. (defcustom org-cycle-global-at-bob nil
  609. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  610. This makes it possible to do global cycling without having to use S-TAB or
  611. C-u TAB. For this special case to work, the first line of the buffer
  612. must not be a headline - it may be empty or some other text. When used in
  613. this way, `org-cycle-hook' is disables temporarily, to make sure the
  614. cursor stays at the beginning of the buffer.
  615. When this option is nil, don't do anything special at the beginning
  616. of the buffer."
  617. :group 'org-cycle
  618. :type 'boolean)
  619. (defcustom org-cycle-level-after-item/entry-creation t
  620. "Non-nil means cycle entry level or item indentation in new empty entries.
  621. When the cursor is at the end of an empty headline, i.e with only stars
  622. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  623. and then all possible anchestor states, before returning to the original state.
  624. This makes data entry extremely fast: M-RET to create a new headline,
  625. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  626. When the cursor is at the end of an empty plain list item, one TAB will
  627. make it a subitem, two or more tabs will back up to make this an item
  628. higher up in the item hierarchy."
  629. :group 'org-cycle
  630. :type 'boolean)
  631. (defcustom org-cycle-emulate-tab t
  632. "Where should `org-cycle' emulate TAB.
  633. nil Never
  634. white Only in completely white lines
  635. whitestart Only at the beginning of lines, before the first non-white char
  636. t Everywhere except in headlines
  637. exc-hl-bol Everywhere except at the start of a headline
  638. If TAB is used in a place where it does not emulate TAB, the current subtree
  639. visibility is cycled."
  640. :group 'org-cycle
  641. :type '(choice (const :tag "Never" nil)
  642. (const :tag "Only in completely white lines" white)
  643. (const :tag "Before first char in a line" whitestart)
  644. (const :tag "Everywhere except in headlines" t)
  645. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  646. ))
  647. (defcustom org-cycle-separator-lines 2
  648. "Number of empty lines needed to keep an empty line between collapsed trees.
  649. If you leave an empty line between the end of a subtree and the following
  650. headline, this empty line is hidden when the subtree is folded.
  651. Org-mode will leave (exactly) one empty line visible if the number of
  652. empty lines is equal or larger to the number given in this variable.
  653. So the default 2 means at least 2 empty lines after the end of a subtree
  654. are needed to produce free space between a collapsed subtree and the
  655. following headline.
  656. If the number is negative, and the number of empty lines is at least -N,
  657. all empty lines are shown.
  658. Special case: when 0, never leave empty lines in collapsed view."
  659. :group 'org-cycle
  660. :type 'integer)
  661. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  662. (defcustom org-pre-cycle-hook nil
  663. "Hook that is run before visibility cycling is happening.
  664. The function(s) in this hook must accept a single argument which indicates
  665. the new state that will be set right after running this hook. The
  666. argument is a symbol. Before a global state change, it can have the values
  667. `overview', `content', or `all'. Before a local state change, it can have
  668. the values `folded', `children', or `subtree'."
  669. :group 'org-cycle
  670. :type 'hook)
  671. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  672. org-cycle-hide-drawers
  673. org-cycle-show-empty-lines
  674. org-optimize-window-after-visibility-change)
  675. "Hook that is run after `org-cycle' has changed the buffer visibility.
  676. The function(s) in this hook must accept a single argument which indicates
  677. the new state that was set by the most recent `org-cycle' command. The
  678. argument is a symbol. After a global state change, it can have the values
  679. `overview', `content', or `all'. After a local state change, it can have
  680. the values `folded', `children', or `subtree'."
  681. :group 'org-cycle
  682. :type 'hook)
  683. (defgroup org-edit-structure nil
  684. "Options concerning structure editing in Org-mode."
  685. :tag "Org Edit Structure"
  686. :group 'org-structure)
  687. (defcustom org-odd-levels-only nil
  688. "Non-nil means skip even levels and only use odd levels for the outline.
  689. This has the effect that two stars are being added/taken away in
  690. promotion/demotion commands. It also influences how levels are
  691. handled by the exporters.
  692. Changing it requires restart of `font-lock-mode' to become effective
  693. for fontification also in regions already fontified.
  694. You may also set this on a per-file basis by adding one of the following
  695. lines to the buffer:
  696. #+STARTUP: odd
  697. #+STARTUP: oddeven"
  698. :group 'org-edit-structure
  699. :group 'org-appearance
  700. :type 'boolean)
  701. (defcustom org-adapt-indentation t
  702. "Non-nil means adapt indentation to outline node level.
  703. When this variable is set, Org assumes that you write outlines by
  704. indenting text in each node to align with the headline (after the stars).
  705. The following issues are influenced by this variable:
  706. - When this is set and the *entire* text in an entry is indented, the
  707. indentation is increased by one space in a demotion command, and
  708. decreased by one in a promotion command. If any line in the entry
  709. body starts with text at column 0, indentation is not changed at all.
  710. - Property drawers and planning information is inserted indented when
  711. this variable s set. When nil, they will not be indented.
  712. - TAB indents a line relative to context. The lines below a headline
  713. will be indented when this variable is set.
  714. Note that this is all about true indentation, by adding and removing
  715. space characters. See also `org-indent.el' which does level-dependent
  716. indentation in a virtual way, i.e. at display time in Emacs."
  717. :group 'org-edit-structure
  718. :type 'boolean)
  719. (defcustom org-special-ctrl-a/e nil
  720. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  721. When t, `C-a' will bring back the cursor to the beginning of the
  722. headline text, i.e. after the stars and after a possible TODO keyword.
  723. In an item, this will be the position after the bullet.
  724. When the cursor is already at that position, another `C-a' will bring
  725. it to the beginning of the line.
  726. `C-e' will jump to the end of the headline, ignoring the presence of tags
  727. in the headline. A second `C-e' will then jump to the true end of the
  728. line, after any tags. This also means that, when this variable is
  729. non-nil, `C-e' also will never jump beyond the end of the heading of a
  730. folded section, i.e. not after the ellipses.
  731. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  732. going to the true line boundary first. Only a directly following, identical
  733. keypress will bring the cursor to the special positions.
  734. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  735. set separately."
  736. :group 'org-edit-structure
  737. :type '(choice
  738. (const :tag "off" nil)
  739. (const :tag "on: after stars/bullet and before tags first" t)
  740. (const :tag "reversed: true line boundary first" reversed)
  741. (cons :tag "Set C-a and C-e separately"
  742. (choice :tag "Special C-a"
  743. (const :tag "off" nil)
  744. (const :tag "on: after stars/bullet first" t)
  745. (const :tag "reversed: before stars/bullet first" reversed))
  746. (choice :tag "Special C-e"
  747. (const :tag "off" nil)
  748. (const :tag "on: before tags first" t)
  749. (const :tag "reversed: after tags first" reversed)))))
  750. (if (fboundp 'defvaralias)
  751. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  752. (defcustom org-special-ctrl-k nil
  753. "Non-nil means `C-k' will behave specially in headlines.
  754. When nil, `C-k' will call the default `kill-line' command.
  755. When t, the following will happen while the cursor is in the headline:
  756. - When the cursor is at the beginning of a headline, kill the entire
  757. line and possible the folded subtree below the line.
  758. - When in the middle of the headline text, kill the headline up to the tags.
  759. - When after the headline text, kill the tags."
  760. :group 'org-edit-structure
  761. :type 'boolean)
  762. (defcustom org-yank-folded-subtrees t
  763. "Non-nil means when yanking subtrees, fold them.
  764. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  765. it starts with a heading and all other headings in it are either children
  766. or siblings, then fold all the subtrees. However, do this only if no
  767. text after the yank would be swallowed into a folded tree by this action."
  768. :group 'org-edit-structure
  769. :type 'boolean)
  770. (defcustom org-yank-adjusted-subtrees nil
  771. "Non-nil means when yanking subtrees, adjust the level.
  772. With this setting, `org-paste-subtree' is used to insert the subtree, see
  773. this function for details."
  774. :group 'org-edit-structure
  775. :type 'boolean)
  776. (defcustom org-M-RET-may-split-line '((default . t))
  777. "Non-nil means M-RET will split the line at the cursor position.
  778. When nil, it will go to the end of the line before making a
  779. new line.
  780. You may also set this option in a different way for different
  781. contexts. Valid contexts are:
  782. headline when creating a new headline
  783. item when creating a new item
  784. table in a table field
  785. default the value to be used for all contexts not explicitly
  786. customized"
  787. :group 'org-structure
  788. :group 'org-table
  789. :type '(choice
  790. (const :tag "Always" t)
  791. (const :tag "Never" nil)
  792. (repeat :greedy t :tag "Individual contexts"
  793. (cons
  794. (choice :tag "Context"
  795. (const headline)
  796. (const item)
  797. (const table)
  798. (const default))
  799. (boolean)))))
  800. (defcustom org-insert-heading-respect-content nil
  801. "Non-nil means insert new headings after the current subtree.
  802. When nil, the new heading is created directly after the current line.
  803. The commands \\[org-insert-heading-respect-content] and
  804. \\[org-insert-todo-heading-respect-content] turn this variable on
  805. for the duration of the command."
  806. :group 'org-structure
  807. :type 'boolean)
  808. (defcustom org-blank-before-new-entry '((heading . auto)
  809. (plain-list-item . auto))
  810. "Should `org-insert-heading' leave a blank line before new heading/item?
  811. The value is an alist, with `heading' and `plain-list-item' as car,
  812. and a boolean flag as cdr. For plain lists, if the variable
  813. `org-empty-line-terminates-plain-lists' is set, the setting here
  814. is ignored and no empty line is inserted, to keep the list in tact."
  815. :group 'org-edit-structure
  816. :type '(list
  817. (cons (const heading)
  818. (choice (const :tag "Never" nil)
  819. (const :tag "Always" t)
  820. (const :tag "Auto" auto)))
  821. (cons (const plain-list-item)
  822. (choice (const :tag "Never" nil)
  823. (const :tag "Always" t)
  824. (const :tag "Auto" auto)))))
  825. (defcustom org-insert-heading-hook nil
  826. "Hook being run after inserting a new heading."
  827. :group 'org-edit-structure
  828. :type 'hook)
  829. (defcustom org-enable-fixed-width-editor t
  830. "Non-nil means lines starting with \":\" are treated as fixed-width.
  831. This currently only means they are never auto-wrapped.
  832. When nil, such lines will be treated like ordinary lines.
  833. See also the QUOTE keyword."
  834. :group 'org-edit-structure
  835. :type 'boolean)
  836. (defcustom org-goto-auto-isearch t
  837. "Non-nil means typing characters in org-goto starts incremental search."
  838. :group 'org-edit-structure
  839. :type 'boolean)
  840. (defgroup org-sparse-trees nil
  841. "Options concerning sparse trees in Org-mode."
  842. :tag "Org Sparse Trees"
  843. :group 'org-structure)
  844. (defcustom org-highlight-sparse-tree-matches t
  845. "Non-nil means highlight all matches that define a sparse tree.
  846. The highlights will automatically disappear the next time the buffer is
  847. changed by an edit command."
  848. :group 'org-sparse-trees
  849. :type 'boolean)
  850. (defcustom org-remove-highlights-with-change t
  851. "Non-nil means any change to the buffer will remove temporary highlights.
  852. Such highlights are created by `org-occur' and `org-clock-display'.
  853. When nil, `C-c C-c needs to be used to get rid of the highlights.
  854. The highlights created by `org-preview-latex-fragment' always need
  855. `C-c C-c' to be removed."
  856. :group 'org-sparse-trees
  857. :group 'org-time
  858. :type 'boolean)
  859. (defcustom org-occur-hook '(org-first-headline-recenter)
  860. "Hook that is run after `org-occur' has constructed a sparse tree.
  861. This can be used to recenter the window to show as much of the structure
  862. as possible."
  863. :group 'org-sparse-trees
  864. :type 'hook)
  865. (defgroup org-imenu-and-speedbar nil
  866. "Options concerning imenu and speedbar in Org-mode."
  867. :tag "Org Imenu and Speedbar"
  868. :group 'org-structure)
  869. (defcustom org-imenu-depth 2
  870. "The maximum level for Imenu access to Org-mode headlines.
  871. This also applied for speedbar access."
  872. :group 'org-imenu-and-speedbar
  873. :type 'integer)
  874. (defgroup org-table nil
  875. "Options concerning tables in Org-mode."
  876. :tag "Org Table"
  877. :group 'org)
  878. (defcustom org-enable-table-editor 'optimized
  879. "Non-nil means lines starting with \"|\" are handled by the table editor.
  880. When nil, such lines will be treated like ordinary lines.
  881. When equal to the symbol `optimized', the table editor will be optimized to
  882. do the following:
  883. - Automatic overwrite mode in front of whitespace in table fields.
  884. This makes the structure of the table stay in tact as long as the edited
  885. field does not exceed the column width.
  886. - Minimize the number of realigns. Normally, the table is aligned each time
  887. TAB or RET are pressed to move to another field. With optimization this
  888. happens only if changes to a field might have changed the column width.
  889. Optimization requires replacing the functions `self-insert-command',
  890. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  891. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  892. very good at guessing when a re-align will be necessary, but you can always
  893. force one with \\[org-ctrl-c-ctrl-c].
  894. If you would like to use the optimized version in Org-mode, but the
  895. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  896. This variable can be used to turn on and off the table editor during a session,
  897. but in order to toggle optimization, a restart is required.
  898. See also the variable `org-table-auto-blank-field'."
  899. :group 'org-table
  900. :type '(choice
  901. (const :tag "off" nil)
  902. (const :tag "on" t)
  903. (const :tag "on, optimized" optimized)))
  904. (defcustom org-self-insert-cluster-for-undo t
  905. "Non-nil means cluster self-insert commands for undo when possible.
  906. If this is set, then, like in the Emacs command loop, 20 consecutive
  907. characters will be undone together.
  908. This is configurable, because there is some impact on typing performance."
  909. :group 'org-table
  910. :type 'boolean)
  911. (defcustom org-table-tab-recognizes-table.el t
  912. "Non-nil means TAB will automatically notice a table.el table.
  913. When it sees such a table, it moves point into it and - if necessary -
  914. calls `table-recognize-table'."
  915. :group 'org-table-editing
  916. :type 'boolean)
  917. (defgroup org-link nil
  918. "Options concerning links in Org-mode."
  919. :tag "Org Link"
  920. :group 'org)
  921. (defvar org-link-abbrev-alist-local nil
  922. "Buffer-local version of `org-link-abbrev-alist', which see.
  923. The value of this is taken from the #+LINK lines.")
  924. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  925. (defcustom org-link-abbrev-alist nil
  926. "Alist of link abbreviations.
  927. The car of each element is a string, to be replaced at the start of a link.
  928. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  929. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  930. [[linkkey:tag][description]]
  931. The 'linkkey' must be a word word, starting with a letter, followed
  932. by letters, numbers, '-' or '_'.
  933. If REPLACE is a string, the tag will simply be appended to create the link.
  934. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  935. the placeholder \"%h\" will cause a url-encoded version of the tag to
  936. be inserted at that point (see the function `url-hexify-string').
  937. REPLACE may also be a function that will be called with the tag as the
  938. only argument to create the link, which should be returned as a string.
  939. See the manual for examples."
  940. :group 'org-link
  941. :type '(repeat
  942. (cons
  943. (string :tag "Protocol")
  944. (choice
  945. (string :tag "Format")
  946. (function)))))
  947. (defcustom org-descriptive-links t
  948. "Non-nil means hide link part and only show description of bracket links.
  949. Bracket links are like [[link][description]]. This variable sets the initial
  950. state in new org-mode buffers. The setting can then be toggled on a
  951. per-buffer basis from the Org->Hyperlinks menu."
  952. :group 'org-link
  953. :type 'boolean)
  954. (defcustom org-link-file-path-type 'adaptive
  955. "How the path name in file links should be stored.
  956. Valid values are:
  957. relative Relative to the current directory, i.e. the directory of the file
  958. into which the link is being inserted.
  959. absolute Absolute path, if possible with ~ for home directory.
  960. noabbrev Absolute path, no abbreviation of home directory.
  961. adaptive Use relative path for files in the current directory and sub-
  962. directories of it. For other files, use an absolute path."
  963. :group 'org-link
  964. :type '(choice
  965. (const relative)
  966. (const absolute)
  967. (const noabbrev)
  968. (const adaptive)))
  969. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  970. "Types of links that should be activated in Org-mode files.
  971. This is a list of symbols, each leading to the activation of a certain link
  972. type. In principle, it does not hurt to turn on most link types - there may
  973. be a small gain when turning off unused link types. The types are:
  974. bracket The recommended [[link][description]] or [[link]] links with hiding.
  975. angular Links in angular brackets that may contain whitespace like
  976. <bbdb:Carsten Dominik>.
  977. plain Plain links in normal text, no whitespace, like http://google.com.
  978. radio Text that is matched by a radio target, see manual for details.
  979. tag Tag settings in a headline (link to tag search).
  980. date Time stamps (link to calendar).
  981. footnote Footnote labels.
  982. Changing this variable requires a restart of Emacs to become effective."
  983. :group 'org-link
  984. :type '(set :greedy t
  985. (const :tag "Double bracket links (new style)" bracket)
  986. (const :tag "Angular bracket links (old style)" angular)
  987. (const :tag "Plain text links" plain)
  988. (const :tag "Radio target matches" radio)
  989. (const :tag "Tags" tag)
  990. (const :tag "Timestamps" date)
  991. (const :tag "Footnotes" footnote)))
  992. (defcustom org-make-link-description-function nil
  993. "Function to use to generate link descriptions from links. If
  994. nil the link location will be used. This function must take two
  995. parameters; the first is the link and the second the description
  996. org-insert-link has generated, and should return the description
  997. to use."
  998. :group 'org-link
  999. :type 'function)
  1000. (defgroup org-link-store nil
  1001. "Options concerning storing links in Org-mode."
  1002. :tag "Org Store Link"
  1003. :group 'org-link)
  1004. (defcustom org-email-link-description-format "Email %c: %.30s"
  1005. "Format of the description part of a link to an email or usenet message.
  1006. The following %-escapes will be replaced by corresponding information:
  1007. %F full \"From\" field
  1008. %f name, taken from \"From\" field, address if no name
  1009. %T full \"To\" field
  1010. %t first name in \"To\" field, address if no name
  1011. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1012. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1013. %s subject
  1014. %m message-id.
  1015. You may use normal field width specification between the % and the letter.
  1016. This is for example useful to limit the length of the subject.
  1017. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1018. :group 'org-link-store
  1019. :type 'string)
  1020. (defcustom org-from-is-user-regexp
  1021. (let (r1 r2)
  1022. (when (and user-mail-address (not (string= user-mail-address "")))
  1023. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1024. (when (and user-full-name (not (string= user-full-name "")))
  1025. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1026. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1027. "Regexp matched against the \"From:\" header of an email or usenet message.
  1028. It should match if the message is from the user him/herself."
  1029. :group 'org-link-store
  1030. :type 'regexp)
  1031. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1032. "Non-nil means storing a link to an Org file will use entry IDs.
  1033. Note that before this variable is even considered, org-id must be loaded,
  1034. so please customize `org-modules' and turn it on.
  1035. The variable can have the following values:
  1036. t Create an ID if needed to make a link to the current entry.
  1037. create-if-interactive
  1038. If `org-store-link' is called directly (interactively, as a user
  1039. command), do create an ID to support the link. But when doing the
  1040. job for remember, only use the ID if it already exists. The
  1041. purpose of this setting is to avoid proliferation of unwanted
  1042. IDs, just because you happen to be in an Org file when you
  1043. call `org-remember' that automatically and preemptively
  1044. creates a link. If you do want to get an ID link in a remember
  1045. template to an entry not having an ID, create it first by
  1046. explicitly creating a link to it, using `C-c C-l' first.
  1047. create-if-interactive-and-no-custom-id
  1048. Like create-if-interactive, but do not create an ID if there is
  1049. a CUSTOM_ID property defined in the entry. This is the default.
  1050. use-existing
  1051. Use existing ID, do not create one.
  1052. nil Never use an ID to make a link, instead link using a text search for
  1053. the headline text."
  1054. :group 'org-link-store
  1055. :type '(choice
  1056. (const :tag "Create ID to make link" t)
  1057. (const :tag "Create if storing link interactively"
  1058. create-if-interactive)
  1059. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1060. create-if-interactive-and-no-custom-id)
  1061. (const :tag "Only use existing" use-existing)
  1062. (const :tag "Do not use ID to create link" nil)))
  1063. (defcustom org-context-in-file-links t
  1064. "Non-nil means file links from `org-store-link' contain context.
  1065. A search string will be added to the file name with :: as separator and
  1066. used to find the context when the link is activated by the command
  1067. `org-open-at-point'.
  1068. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1069. negates this setting for the duration of the command."
  1070. :group 'org-link-store
  1071. :type 'boolean)
  1072. (defcustom org-keep-stored-link-after-insertion nil
  1073. "Non-nil means keep link in list for entire session.
  1074. The command `org-store-link' adds a link pointing to the current
  1075. location to an internal list. These links accumulate during a session.
  1076. The command `org-insert-link' can be used to insert links into any
  1077. Org-mode file (offering completion for all stored links). When this
  1078. option is nil, every link which has been inserted once using \\[org-insert-link]
  1079. will be removed from the list, to make completing the unused links
  1080. more efficient."
  1081. :group 'org-link-store
  1082. :type 'boolean)
  1083. (defgroup org-link-follow nil
  1084. "Options concerning following links in Org-mode."
  1085. :tag "Org Follow Link"
  1086. :group 'org-link)
  1087. (defcustom org-link-translation-function nil
  1088. "Function to translate links with different syntax to Org syntax.
  1089. This can be used to translate links created for example by the Planner
  1090. or emacs-wiki packages to Org syntax.
  1091. The function must accept two parameters, a TYPE containing the link
  1092. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1093. which is everything after the link protocol. It should return a cons
  1094. with possibly modified values of type and path.
  1095. Org contains a function for this, so if you set this variable to
  1096. `org-translate-link-from-planner', you should be able follow many
  1097. links created by planner."
  1098. :group 'org-link-follow
  1099. :type 'function)
  1100. (defcustom org-follow-link-hook nil
  1101. "Hook that is run after a link has been followed."
  1102. :group 'org-link-follow
  1103. :type 'hook)
  1104. (defcustom org-tab-follows-link nil
  1105. "Non-nil means on links TAB will follow the link.
  1106. Needs to be set before org.el is loaded.
  1107. This really should not be used, it does not make sense, and the
  1108. implementation is bad."
  1109. :group 'org-link-follow
  1110. :type 'boolean)
  1111. (defcustom org-return-follows-link nil
  1112. "Non-nil means on links RET will follow the link.
  1113. Needs to be set before org.el is loaded."
  1114. :group 'org-link-follow
  1115. :type 'boolean)
  1116. (defcustom org-mouse-1-follows-link
  1117. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1118. "Non-nil means mouse-1 on a link will follow the link.
  1119. A longer mouse click will still set point. Does not work on XEmacs.
  1120. Needs to be set before org.el is loaded."
  1121. :group 'org-link-follow
  1122. :type 'boolean)
  1123. (defcustom org-mark-ring-length 4
  1124. "Number of different positions to be recorded in the ring
  1125. Changing this requires a restart of Emacs to work correctly."
  1126. :group 'org-link-follow
  1127. :type 'integer)
  1128. (defcustom org-link-frame-setup
  1129. '((vm . vm-visit-folder-other-frame)
  1130. (gnus . gnus-other-frame)
  1131. (file . find-file-other-window))
  1132. "Setup the frame configuration for following links.
  1133. When following a link with Emacs, it may often be useful to display
  1134. this link in another window or frame. This variable can be used to
  1135. set this up for the different types of links.
  1136. For VM, use any of
  1137. `vm-visit-folder'
  1138. `vm-visit-folder-other-frame'
  1139. For Gnus, use any of
  1140. `gnus'
  1141. `gnus-other-frame'
  1142. `org-gnus-no-new-news'
  1143. For FILE, use any of
  1144. `find-file'
  1145. `find-file-other-window'
  1146. `find-file-other-frame'
  1147. For the calendar, use the variable `calendar-setup'.
  1148. For BBDB, it is currently only possible to display the matches in
  1149. another window."
  1150. :group 'org-link-follow
  1151. :type '(list
  1152. (cons (const vm)
  1153. (choice
  1154. (const vm-visit-folder)
  1155. (const vm-visit-folder-other-window)
  1156. (const vm-visit-folder-other-frame)))
  1157. (cons (const gnus)
  1158. (choice
  1159. (const gnus)
  1160. (const gnus-other-frame)
  1161. (const org-gnus-no-new-news)))
  1162. (cons (const file)
  1163. (choice
  1164. (const find-file)
  1165. (const find-file-other-window)
  1166. (const find-file-other-frame)))))
  1167. (defcustom org-display-internal-link-with-indirect-buffer nil
  1168. "Non-nil means use indirect buffer to display infile links.
  1169. Activating internal links (from one location in a file to another location
  1170. in the same file) normally just jumps to the location. When the link is
  1171. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1172. another window. When this option is set, the other window actually displays
  1173. an indirect buffer clone of the current buffer, to avoid any visibility
  1174. changes to the current buffer."
  1175. :group 'org-link-follow
  1176. :type 'boolean)
  1177. (defcustom org-open-non-existing-files nil
  1178. "Non-nil means `org-open-file' will open non-existing files.
  1179. When nil, an error will be generated.
  1180. This variable applies only to external applications because they
  1181. might choke on non-existing files. If the link is to a file that
  1182. will be opened in Emacs, the variable is ignored."
  1183. :group 'org-link-follow
  1184. :type 'boolean)
  1185. (defcustom org-open-directory-means-index-dot-org nil
  1186. "Non-nil means a link to a directory really means to index.org.
  1187. When nil, following a directory link will run dired or open a finder/explorer
  1188. window on that directory."
  1189. :group 'org-link-follow
  1190. :type 'boolean)
  1191. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1192. "Function and arguments to call for following mailto links.
  1193. This is a list with the first element being a lisp function, and the
  1194. remaining elements being arguments to the function. In string arguments,
  1195. %a will be replaced by the address, and %s will be replaced by the subject
  1196. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1197. :group 'org-link-follow
  1198. :type '(choice
  1199. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1200. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1201. (const :tag "message-mail" (message-mail "%a" "%s"))
  1202. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1203. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1204. "Non-nil means ask for confirmation before executing shell links.
  1205. Shell links can be dangerous: just think about a link
  1206. [[shell:rm -rf ~/*][Google Search]]
  1207. This link would show up in your Org-mode document as \"Google Search\",
  1208. but really it would remove your entire home directory.
  1209. Therefore we advise against setting this variable to nil.
  1210. Just change it to `y-or-n-p' if you want to confirm with a
  1211. single keystroke rather than having to type \"yes\"."
  1212. :group 'org-link-follow
  1213. :type '(choice
  1214. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1215. (const :tag "with y-or-n (faster)" y-or-n-p)
  1216. (const :tag "no confirmation (dangerous)" nil)))
  1217. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1218. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1219. Elisp links can be dangerous: just think about a link
  1220. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1221. This link would show up in your Org-mode document as \"Google Search\",
  1222. but really it would remove your entire home directory.
  1223. Therefore we advise against setting this variable to nil.
  1224. Just change it to `y-or-n-p' if you want to confirm with a
  1225. single keystroke rather than having to type \"yes\"."
  1226. :group 'org-link-follow
  1227. :type '(choice
  1228. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1229. (const :tag "with y-or-n (faster)" y-or-n-p)
  1230. (const :tag "no confirmation (dangerous)" nil)))
  1231. (defconst org-file-apps-defaults-gnu
  1232. '((remote . emacs)
  1233. (system . mailcap)
  1234. (t . mailcap))
  1235. "Default file applications on a UNIX or GNU/Linux system.
  1236. See `org-file-apps'.")
  1237. (defconst org-file-apps-defaults-macosx
  1238. '((remote . emacs)
  1239. (t . "open %s")
  1240. (system . "open %s")
  1241. ("ps.gz" . "gv %s")
  1242. ("eps.gz" . "gv %s")
  1243. ("dvi" . "xdvi %s")
  1244. ("fig" . "xfig %s"))
  1245. "Default file applications on a MacOS X system.
  1246. The system \"open\" is known as a default, but we use X11 applications
  1247. for some files for which the OS does not have a good default.
  1248. See `org-file-apps'.")
  1249. (defconst org-file-apps-defaults-windowsnt
  1250. (list
  1251. '(remote . emacs)
  1252. (cons t
  1253. (list (if (featurep 'xemacs)
  1254. 'mswindows-shell-execute
  1255. 'w32-shell-execute)
  1256. "open" 'file))
  1257. (cons 'system
  1258. (list (if (featurep 'xemacs)
  1259. 'mswindows-shell-execute
  1260. 'w32-shell-execute)
  1261. "open" 'file)))
  1262. "Default file applications on a Windows NT system.
  1263. The system \"open\" is used for most files.
  1264. See `org-file-apps'.")
  1265. (defcustom org-file-apps
  1266. '(
  1267. (auto-mode . emacs)
  1268. ("\\.mm\\'" . default)
  1269. ("\\.x?html?\\'" . default)
  1270. ("\\.pdf\\'" . default)
  1271. )
  1272. "External applications for opening `file:path' items in a document.
  1273. Org-mode uses system defaults for different file types, but
  1274. you can use this variable to set the application for a given file
  1275. extension. The entries in this list are cons cells where the car identifies
  1276. files and the cdr the corresponding command. Possible values for the
  1277. file identifier are
  1278. \"regex\" Regular expression matched against the file: link. For
  1279. backward compatibility, this can also be a string with only
  1280. alphanumeric characters, which is then interpreted as an
  1281. extension.
  1282. `directory' Matches a directory
  1283. `remote' Matches a remote file, accessible through tramp or efs.
  1284. Remote files most likely should be visited through Emacs
  1285. because external applications cannot handle such paths.
  1286. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1287. so all files Emacs knows how to handle. Using this with
  1288. command `emacs' will open most files in Emacs. Beware that this
  1289. will also open html files inside Emacs, unless you add
  1290. (\"html\" . default) to the list as well.
  1291. t Default for files not matched by any of the other options.
  1292. `system' The system command to open files, like `open' on Windows
  1293. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1294. that will be selected if you call `C-c C-o' with a double
  1295. `C-u C-u' prefix.
  1296. Possible values for the command are:
  1297. `emacs' The file will be visited by the current Emacs process.
  1298. `default' Use the default application for this file type, which is the
  1299. association for t in the list, most likely in the system-specific
  1300. part.
  1301. This can be used to overrule an unwanted setting in the
  1302. system-specific variable.
  1303. `system' Use the system command for opening files, like \"open\".
  1304. This command is specified by the entry whose car is `system'.
  1305. Most likely, the system-specific version of this variable
  1306. does define this command, but you can overrule/replace it
  1307. here.
  1308. string A command to be executed by a shell; %s will be replaced
  1309. by the path to the file. If the file identifier is a regex,
  1310. %n will be replaced by the match of the nth match group.
  1311. sexp A Lisp form which will be evaluated. The file path will
  1312. be available in the Lisp variable `file', the link itself
  1313. in the Lisp variable `link'. If the file identifier is a regex,
  1314. the original match data will be restored, so subexpression
  1315. matches are accessible using (match-string n link).
  1316. For more examples, see the system specific constants
  1317. `org-file-apps-defaults-macosx'
  1318. `org-file-apps-defaults-windowsnt'
  1319. `org-file-apps-defaults-gnu'."
  1320. :group 'org-link-follow
  1321. :type '(repeat
  1322. (cons (choice :value ""
  1323. (string :tag "Extension")
  1324. (const :tag "System command to open files" system)
  1325. (const :tag "Default for unrecognized files" t)
  1326. (const :tag "Remote file" remote)
  1327. (const :tag "Links to a directory" directory)
  1328. (const :tag "Any files that have Emacs modes"
  1329. auto-mode))
  1330. (choice :value ""
  1331. (const :tag "Visit with Emacs" emacs)
  1332. (const :tag "Use default" default)
  1333. (const :tag "Use the system command" system)
  1334. (string :tag "Command")
  1335. (sexp :tag "Lisp form")))))
  1336. (defgroup org-refile nil
  1337. "Options concerning refiling entries in Org-mode."
  1338. :tag "Org Refile"
  1339. :group 'org)
  1340. (defcustom org-directory "~/org"
  1341. "Directory with org files.
  1342. This is just a default location to look for Org files. There is no need
  1343. at all to put your files into this directory. It is only used in the
  1344. following situations:
  1345. 1. When a remember template specifies a target file that is not an
  1346. absolute path. The path will then be interpreted relative to
  1347. `org-directory'
  1348. 2. When a remember note is filed away in an interactive way (when exiting the
  1349. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1350. with `org-directory' as the default path."
  1351. :group 'org-refile
  1352. :group 'org-remember
  1353. :type 'directory)
  1354. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1355. "Default target for storing notes.
  1356. Used by the hooks for remember.el. This can be a string, or nil to mean
  1357. the value of `remember-data-file'.
  1358. You can set this on a per-template basis with the variable
  1359. `org-remember-templates'."
  1360. :group 'org-refile
  1361. :group 'org-remember
  1362. :type '(choice
  1363. (const :tag "Default from remember-data-file" nil)
  1364. file))
  1365. (defcustom org-goto-interface 'outline
  1366. "The default interface to be used for `org-goto'.
  1367. Allowed values are:
  1368. outline The interface shows an outline of the relevant file
  1369. and the correct heading is found by moving through
  1370. the outline or by searching with incremental search.
  1371. outline-path-completion Headlines in the current buffer are offered via
  1372. completion. This is the interface also used by
  1373. the refile command."
  1374. :group 'org-refile
  1375. :type '(choice
  1376. (const :tag "Outline" outline)
  1377. (const :tag "Outline-path-completion" outline-path-completion)))
  1378. (defcustom org-goto-max-level 5
  1379. "Maximum level to be considered when running org-goto with refile interface."
  1380. :group 'org-refile
  1381. :type 'integer)
  1382. (defcustom org-reverse-note-order nil
  1383. "Non-nil means store new notes at the beginning of a file or entry.
  1384. When nil, new notes will be filed to the end of a file or entry.
  1385. This can also be a list with cons cells of regular expressions that
  1386. are matched against file names, and values."
  1387. :group 'org-remember
  1388. :group 'org-refile
  1389. :type '(choice
  1390. (const :tag "Reverse always" t)
  1391. (const :tag "Reverse never" nil)
  1392. (repeat :tag "By file name regexp"
  1393. (cons regexp boolean))))
  1394. (defcustom org-log-refile nil
  1395. "Information to record when a task is refiled.
  1396. Possible values are:
  1397. nil Don't add anything
  1398. time Add a time stamp to the task
  1399. note Prompt for a note and add it with template `org-log-note-headings'
  1400. This option can also be set with on a per-file-basis with
  1401. #+STARTUP: nologrefile
  1402. #+STARTUP: logrefile
  1403. #+STARTUP: lognoterefile
  1404. You can have local logging settings for a subtree by setting the LOGGING
  1405. property to one or more of these keywords.
  1406. When bulk-refiling from the agenda, the value `note' is forbidden and
  1407. will temporarily be changed to `time'."
  1408. :group 'org-refile
  1409. :group 'org-progress
  1410. :type '(choice
  1411. (const :tag "No logging" nil)
  1412. (const :tag "Record timestamp" time)
  1413. (const :tag "Record timestamp with note." note)))
  1414. (defcustom org-refile-targets nil
  1415. "Targets for refiling entries with \\[org-refile].
  1416. This is list of cons cells. Each cell contains:
  1417. - a specification of the files to be considered, either a list of files,
  1418. or a symbol whose function or variable value will be used to retrieve
  1419. a file name or a list of file names. If you use `org-agenda-files' for
  1420. that, all agenda files will be scanned for targets. Nil means consider
  1421. headings in the current buffer.
  1422. - A specification of how to find candidate refile targets. This may be
  1423. any of:
  1424. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1425. This tag has to be present in all target headlines, inheritance will
  1426. not be considered.
  1427. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1428. todo keyword.
  1429. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1430. headlines that are refiling targets.
  1431. - a cons cell (:level . N). Any headline of level N is considered a target.
  1432. Note that, when `org-odd-levels-only' is set, level corresponds to
  1433. order in hierarchy, not to the number of stars.
  1434. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1435. Note that, when `org-odd-levels-only' is set, level corresponds to
  1436. order in hierarchy, not to the number of stars.
  1437. You can set the variable `org-refile-target-verify-function' to a function
  1438. to verify each headline found by the simple critery above.
  1439. When this variable is nil, all top-level headlines in the current buffer
  1440. are used, equivalent to the value `((nil . (:level . 1))'."
  1441. :group 'org-refile
  1442. :type '(repeat
  1443. (cons
  1444. (choice :value org-agenda-files
  1445. (const :tag "All agenda files" org-agenda-files)
  1446. (const :tag "Current buffer" nil)
  1447. (function) (variable) (file))
  1448. (choice :tag "Identify target headline by"
  1449. (cons :tag "Specific tag" (const :value :tag) (string))
  1450. (cons :tag "TODO keyword" (const :value :todo) (string))
  1451. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1452. (cons :tag "Level number" (const :value :level) (integer))
  1453. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1454. (defcustom org-refile-target-verify-function nil
  1455. "Function to verify if the headline at point should be a refile target.
  1456. The function will be called without arguments, with point at the
  1457. beginning of the headline. It should return t and leave point
  1458. where it is if the headline is a valid target for refiling.
  1459. If the target should not be selected, the function must return nil.
  1460. In addition to this, it may move point to a place from where the search
  1461. should be continued. For example, the function may decide that the entire
  1462. subtree of the current entry should be excluded and move point to the end
  1463. of the subtree."
  1464. :group 'org-refile
  1465. :type 'function)
  1466. (defcustom org-refile-use-outline-path nil
  1467. "Non-nil means provide refile targets as paths.
  1468. So a level 3 headline will be available as level1/level2/level3.
  1469. When the value is `file', also include the file name (without directory)
  1470. into the path. In this case, you can also stop the completion after
  1471. the file name, to get entries inserted as top level in the file.
  1472. When `full-file-path', include the full file path."
  1473. :group 'org-refile
  1474. :type '(choice
  1475. (const :tag "Not" nil)
  1476. (const :tag "Yes" t)
  1477. (const :tag "Start with file name" file)
  1478. (const :tag "Start with full file path" full-file-path)))
  1479. (defcustom org-outline-path-complete-in-steps t
  1480. "Non-nil means complete the outline path in hierarchical steps.
  1481. When Org-mode uses the refile interface to select an outline path
  1482. \(see variable `org-refile-use-outline-path'), the completion of
  1483. the path can be done is a single go, or if can be done in steps down
  1484. the headline hierarchy. Going in steps is probably the best if you
  1485. do not use a special completion package like `ido' or `icicles'.
  1486. However, when using these packages, going in one step can be very
  1487. fast, while still showing the whole path to the entry."
  1488. :group 'org-refile
  1489. :type 'boolean)
  1490. (defcustom org-refile-allow-creating-parent-nodes nil
  1491. "Non-nil means allow to create new nodes as refile targets.
  1492. New nodes are then created by adding \"/new node name\" to the completion
  1493. of an existing node. When the value of this variable is `confirm',
  1494. new node creation must be confirmed by the user (recommended)
  1495. When nil, the completion must match an existing entry.
  1496. Note that, if the new heading is not seen by the criteria
  1497. listed in `org-refile-targets', multiple instances of the same
  1498. heading would be created by trying again to file under the new
  1499. heading."
  1500. :group 'org-refile
  1501. :type '(choice
  1502. (const :tag "Never" nil)
  1503. (const :tag "Always" t)
  1504. (const :tag "Prompt for confirmation" confirm)))
  1505. (defgroup org-todo nil
  1506. "Options concerning TODO items in Org-mode."
  1507. :tag "Org TODO"
  1508. :group 'org)
  1509. (defgroup org-progress nil
  1510. "Options concerning Progress logging in Org-mode."
  1511. :tag "Org Progress"
  1512. :group 'org-time)
  1513. (defvar org-todo-interpretation-widgets
  1514. '(
  1515. (:tag "Sequence (cycling hits every state)" sequence)
  1516. (:tag "Type (cycling directly to DONE)" type))
  1517. "The available interpretation symbols for customizing
  1518. `org-todo-keywords'.
  1519. Interested libraries should add to this list.")
  1520. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1521. "List of TODO entry keyword sequences and their interpretation.
  1522. \\<org-mode-map>This is a list of sequences.
  1523. Each sequence starts with a symbol, either `sequence' or `type',
  1524. indicating if the keywords should be interpreted as a sequence of
  1525. action steps, or as different types of TODO items. The first
  1526. keywords are states requiring action - these states will select a headline
  1527. for inclusion into the global TODO list Org-mode produces. If one of
  1528. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1529. signify that no further action is necessary. If \"|\" is not found,
  1530. the last keyword is treated as the only DONE state of the sequence.
  1531. The command \\[org-todo] cycles an entry through these states, and one
  1532. additional state where no keyword is present. For details about this
  1533. cycling, see the manual.
  1534. TODO keywords and interpretation can also be set on a per-file basis with
  1535. the special #+SEQ_TODO and #+TYP_TODO lines.
  1536. Each keyword can optionally specify a character for fast state selection
  1537. \(in combination with the variable `org-use-fast-todo-selection')
  1538. and specifiers for state change logging, using the same syntax
  1539. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1540. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1541. indicates to record a time stamp each time this state is selected.
  1542. Each keyword may also specify if a timestamp or a note should be
  1543. recorded when entering or leaving the state, by adding additional
  1544. characters in the parenthesis after the keyword. This looks like this:
  1545. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1546. record only the time of the state change. With X and Y being either
  1547. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1548. Y when leaving the state if and only if the *target* state does not
  1549. define X. You may omit any of the fast-selection key or X or /Y,
  1550. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1551. For backward compatibility, this variable may also be just a list
  1552. of keywords - in this case the interpretation (sequence or type) will be
  1553. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1554. :group 'org-todo
  1555. :group 'org-keywords
  1556. :type '(choice
  1557. (repeat :tag "Old syntax, just keywords"
  1558. (string :tag "Keyword"))
  1559. (repeat :tag "New syntax"
  1560. (cons
  1561. (choice
  1562. :tag "Interpretation"
  1563. ;;Quick and dirty way to see
  1564. ;;`org-todo-interpretations'. This takes the
  1565. ;;place of item arguments
  1566. :convert-widget
  1567. (lambda (widget)
  1568. (widget-put widget
  1569. :args (mapcar
  1570. #'(lambda (x)
  1571. (widget-convert
  1572. (cons 'const x)))
  1573. org-todo-interpretation-widgets))
  1574. widget))
  1575. (repeat
  1576. (string :tag "Keyword"))))))
  1577. (defvar org-todo-keywords-1 nil
  1578. "All TODO and DONE keywords active in a buffer.")
  1579. (make-variable-buffer-local 'org-todo-keywords-1)
  1580. (defvar org-todo-keywords-for-agenda nil)
  1581. (defvar org-done-keywords-for-agenda nil)
  1582. (defvar org-drawers-for-agenda nil)
  1583. (defvar org-todo-keyword-alist-for-agenda nil)
  1584. (defvar org-tag-alist-for-agenda nil)
  1585. (defvar org-agenda-contributing-files nil)
  1586. (defvar org-not-done-keywords nil)
  1587. (make-variable-buffer-local 'org-not-done-keywords)
  1588. (defvar org-done-keywords nil)
  1589. (make-variable-buffer-local 'org-done-keywords)
  1590. (defvar org-todo-heads nil)
  1591. (make-variable-buffer-local 'org-todo-heads)
  1592. (defvar org-todo-sets nil)
  1593. (make-variable-buffer-local 'org-todo-sets)
  1594. (defvar org-todo-log-states nil)
  1595. (make-variable-buffer-local 'org-todo-log-states)
  1596. (defvar org-todo-kwd-alist nil)
  1597. (make-variable-buffer-local 'org-todo-kwd-alist)
  1598. (defvar org-todo-key-alist nil)
  1599. (make-variable-buffer-local 'org-todo-key-alist)
  1600. (defvar org-todo-key-trigger nil)
  1601. (make-variable-buffer-local 'org-todo-key-trigger)
  1602. (defcustom org-todo-interpretation 'sequence
  1603. "Controls how TODO keywords are interpreted.
  1604. This variable is in principle obsolete and is only used for
  1605. backward compatibility, if the interpretation of todo keywords is
  1606. not given already in `org-todo-keywords'. See that variable for
  1607. more information."
  1608. :group 'org-todo
  1609. :group 'org-keywords
  1610. :type '(choice (const sequence)
  1611. (const type)))
  1612. (defcustom org-use-fast-todo-selection t
  1613. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1614. This variable describes if and under what circumstances the cycling
  1615. mechanism for TODO keywords will be replaced by a single-key, direct
  1616. selection scheme.
  1617. When nil, fast selection is never used.
  1618. When the symbol `prefix', it will be used when `org-todo' is called with
  1619. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1620. in an agenda buffer.
  1621. When t, fast selection is used by default. In this case, the prefix
  1622. argument forces cycling instead.
  1623. In all cases, the special interface is only used if access keys have actually
  1624. been assigned by the user, i.e. if keywords in the configuration are followed
  1625. by a letter in parenthesis, like TODO(t)."
  1626. :group 'org-todo
  1627. :type '(choice
  1628. (const :tag "Never" nil)
  1629. (const :tag "By default" t)
  1630. (const :tag "Only with C-u C-c C-t" prefix)))
  1631. (defcustom org-provide-todo-statistics t
  1632. "Non-nil means update todo statistics after insert and toggle.
  1633. ALL-HEADLINES means update todo statistics by including headlines
  1634. with no TODO keyword as well, counting them as not done.
  1635. A list of TODO keywords means the same, but skip keywords that are
  1636. not in this list.
  1637. When this is set, todo statistics is updated in the parent of the
  1638. current entry each time a todo state is changed."
  1639. :group 'org-todo
  1640. :type '(choice
  1641. (const :tag "Yes, only for TODO entries" t)
  1642. (const :tag "Yes, including all entries" 'all-headlines)
  1643. (repeat :tag "Yes, for TODOs in this list"
  1644. (string :tag "TODO keyword"))
  1645. (other :tag "No TODO statistics" nil)))
  1646. (defcustom org-hierarchical-todo-statistics t
  1647. "Non-nil means TODO statistics covers just direct children.
  1648. When nil, all entries in the subtree are considered.
  1649. This has only an effect if `org-provide-todo-statistics' is set.
  1650. To set this to nil for only a single subtree, use a COOKIE_DATA
  1651. property and include the word \"recursive\" into the value."
  1652. :group 'org-todo
  1653. :type 'boolean)
  1654. (defcustom org-after-todo-state-change-hook nil
  1655. "Hook which is run after the state of a TODO item was changed.
  1656. The new state (a string with a TODO keyword, or nil) is available in the
  1657. Lisp variable `state'."
  1658. :group 'org-todo
  1659. :type 'hook)
  1660. (defvar org-blocker-hook nil
  1661. "Hook for functions that are allowed to block a state change.
  1662. Each function gets as its single argument a property list, see
  1663. `org-trigger-hook' for more information about this list.
  1664. If any of the functions in this hook returns nil, the state change
  1665. is blocked.")
  1666. (defvar org-trigger-hook nil
  1667. "Hook for functions that are triggered by a state change.
  1668. Each function gets as its single argument a property list with at least
  1669. the following elements:
  1670. (:type type-of-change :position pos-at-entry-start
  1671. :from old-state :to new-state)
  1672. Depending on the type, more properties may be present.
  1673. This mechanism is currently implemented for:
  1674. TODO state changes
  1675. ------------------
  1676. :type todo-state-change
  1677. :from previous state (keyword as a string), or nil, or a symbol
  1678. 'todo' or 'done', to indicate the general type of state.
  1679. :to new state, like in :from")
  1680. (defcustom org-enforce-todo-dependencies nil
  1681. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1682. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1683. be blocked if any prior sibling is not yet done.
  1684. Finally, if the parent is blocked because of ordered siblings of its own,
  1685. the child will also be blocked.
  1686. This variable needs to be set before org.el is loaded, and you need to
  1687. restart Emacs after a change to make the change effective. The only way
  1688. to change is while Emacs is running is through the customize interface."
  1689. :set (lambda (var val)
  1690. (set var val)
  1691. (if val
  1692. (add-hook 'org-blocker-hook
  1693. 'org-block-todo-from-children-or-siblings-or-parent)
  1694. (remove-hook 'org-blocker-hook
  1695. 'org-block-todo-from-children-or-siblings-or-parent)))
  1696. :group 'org-todo
  1697. :type 'boolean)
  1698. (defcustom org-enforce-todo-checkbox-dependencies nil
  1699. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1700. When this is nil, checkboxes have no influence on switching TODO states.
  1701. When non-nil, you first need to check off all check boxes before the TODO
  1702. entry can be switched to DONE.
  1703. This variable needs to be set before org.el is loaded, and you need to
  1704. restart Emacs after a change to make the change effective. The only way
  1705. to change is while Emacs is running is through the customize interface."
  1706. :set (lambda (var val)
  1707. (set var val)
  1708. (if val
  1709. (add-hook 'org-blocker-hook
  1710. 'org-block-todo-from-checkboxes)
  1711. (remove-hook 'org-blocker-hook
  1712. 'org-block-todo-from-checkboxes)))
  1713. :group 'org-todo
  1714. :type 'boolean)
  1715. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1716. "Non-nil means inserting a TODO heading is treated as state change.
  1717. So when the command \\[org-insert-todo-heading] is used, state change
  1718. logging will apply if appropriate. When nil, the new TODO item will
  1719. be inserted directly, and no logging will take place."
  1720. :group 'org-todo
  1721. :type 'boolean)
  1722. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1723. "Non-nil means switching TODO states with S-cursor counts as state change.
  1724. This is the default behavior. However, setting this to nil allows a
  1725. convenient way to select a TODO state and bypass any logging associated
  1726. with that."
  1727. :group 'org-todo
  1728. :type 'boolean)
  1729. (defcustom org-todo-state-tags-triggers nil
  1730. "Tag changes that should be triggered by TODO state changes.
  1731. This is a list. Each entry is
  1732. (state-change (tag . flag) .......)
  1733. State-change can be a string with a state, and empty string to indicate the
  1734. state that has no TODO keyword, or it can be one of the symbols `todo'
  1735. or `done', meaning any not-done or done state, respectively."
  1736. :group 'org-todo
  1737. :group 'org-tags
  1738. :type '(repeat
  1739. (cons (choice :tag "When changing to"
  1740. (const :tag "Not-done state" todo)
  1741. (const :tag "Done state" done)
  1742. (string :tag "State"))
  1743. (repeat
  1744. (cons :tag "Tag action"
  1745. (string :tag "Tag")
  1746. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1747. (defcustom org-log-done nil
  1748. "Information to record when a task moves to the DONE state.
  1749. Possible values are:
  1750. nil Don't add anything, just change the keyword
  1751. time Add a time stamp to the task
  1752. note Prompt for a note and add it with template `org-log-note-headings'
  1753. This option can also be set with on a per-file-basis with
  1754. #+STARTUP: nologdone
  1755. #+STARTUP: logdone
  1756. #+STARTUP: lognotedone
  1757. You can have local logging settings for a subtree by setting the LOGGING
  1758. property to one or more of these keywords."
  1759. :group 'org-todo
  1760. :group 'org-progress
  1761. :type '(choice
  1762. (const :tag "No logging" nil)
  1763. (const :tag "Record CLOSED timestamp" time)
  1764. (const :tag "Record CLOSED timestamp with note." note)))
  1765. ;; Normalize old uses of org-log-done.
  1766. (cond
  1767. ((eq org-log-done t) (setq org-log-done 'time))
  1768. ((and (listp org-log-done) (memq 'done org-log-done))
  1769. (setq org-log-done 'note)))
  1770. (defcustom org-log-reschedule nil
  1771. "Information to record when the scheduling date of a tasks is modified.
  1772. Possible values are:
  1773. nil Don't add anything, just change the date
  1774. time Add a time stamp to the task
  1775. note Prompt for a note and add it with template `org-log-note-headings'
  1776. This option can also be set with on a per-file-basis with
  1777. #+STARTUP: nologreschedule
  1778. #+STARTUP: logreschedule
  1779. #+STARTUP: lognotereschedule"
  1780. :group 'org-todo
  1781. :group 'org-progress
  1782. :type '(choice
  1783. (const :tag "No logging" nil)
  1784. (const :tag "Record timestamp" time)
  1785. (const :tag "Record timestamp with note." note)))
  1786. (defcustom org-log-redeadline nil
  1787. "Information to record when the deadline date of a tasks is modified.
  1788. Possible values are:
  1789. nil Don't add anything, just change the date
  1790. time Add a time stamp to the task
  1791. note Prompt for a note and add it with template `org-log-note-headings'
  1792. This option can also be set with on a per-file-basis with
  1793. #+STARTUP: nologredeadline
  1794. #+STARTUP: logredeadline
  1795. #+STARTUP: lognoteredeadline
  1796. You can have local logging settings for a subtree by setting the LOGGING
  1797. property to one or more of these keywords."
  1798. :group 'org-todo
  1799. :group 'org-progress
  1800. :type '(choice
  1801. (const :tag "No logging" nil)
  1802. (const :tag "Record timestamp" time)
  1803. (const :tag "Record timestamp with note." note)))
  1804. (defcustom org-log-note-clock-out nil
  1805. "Non-nil means record a note when clocking out of an item.
  1806. This can also be configured on a per-file basis by adding one of
  1807. the following lines anywhere in the buffer:
  1808. #+STARTUP: lognoteclock-out
  1809. #+STARTUP: nolognoteclock-out"
  1810. :group 'org-todo
  1811. :group 'org-progress
  1812. :type 'boolean)
  1813. (defcustom org-log-done-with-time t
  1814. "Non-nil means the CLOSED time stamp will contain date and time.
  1815. When nil, only the date will be recorded."
  1816. :group 'org-progress
  1817. :type 'boolean)
  1818. (defcustom org-log-note-headings
  1819. '((done . "CLOSING NOTE %t")
  1820. (state . "State %-12s from %-12S %t")
  1821. (note . "Note taken on %t")
  1822. (reschedule . "Rescheduled from %S on %t")
  1823. (delschedule . "Not scheduled, was %S on %t")
  1824. (redeadline . "New deadline from %S on %t")
  1825. (deldeadline . "Removed deadline, was %S on %t")
  1826. (refile . "Refiled on %t")
  1827. (clock-out . ""))
  1828. "Headings for notes added to entries.
  1829. The value is an alist, with the car being a symbol indicating the note
  1830. context, and the cdr is the heading to be used. The heading may also be the
  1831. empty string.
  1832. %t in the heading will be replaced by a time stamp.
  1833. %s will be replaced by the new TODO state, in double quotes.
  1834. %S will be replaced by the old TODO state, in double quotes.
  1835. %u will be replaced by the user name.
  1836. %U will be replaced by the full user name.
  1837. In fact, it is not a good idea to change the `state' entry, because
  1838. agenda log mode depends on the format of these entries."
  1839. :group 'org-todo
  1840. :group 'org-progress
  1841. :type '(list :greedy t
  1842. (cons (const :tag "Heading when closing an item" done) string)
  1843. (cons (const :tag
  1844. "Heading when changing todo state (todo sequence only)"
  1845. state) string)
  1846. (cons (const :tag "Heading when just taking a note" note) string)
  1847. (cons (const :tag "Heading when clocking out" clock-out) string)
  1848. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  1849. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1850. (cons (const :tag "Heading when changing deadline" redeadline) string)
  1851. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  1852. (cons (const :tag "Heading when refiling" refile) string)))
  1853. (unless (assq 'note org-log-note-headings)
  1854. (push '(note . "%t") org-log-note-headings))
  1855. (defcustom org-log-into-drawer nil
  1856. "Non-nil means insert state change notes and time stamps into a drawer.
  1857. When nil, state changes notes will be inserted after the headline and
  1858. any scheduling and clock lines, but not inside a drawer.
  1859. The value of this variable should be the name of the drawer to use.
  1860. LOGBOOK is proposed at the default drawer for this purpose, you can
  1861. also set this to a string to define the drawer of your choice.
  1862. A value of t is also allowed, representing \"LOGBOOK\".
  1863. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1864. will be ignored.
  1865. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1866. a subtree."
  1867. :group 'org-todo
  1868. :group 'org-progress
  1869. :type '(choice
  1870. (const :tag "Not into a drawer" nil)
  1871. (const :tag "LOGBOOK" t)
  1872. (string :tag "Other")))
  1873. (if (fboundp 'defvaralias)
  1874. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1875. (defun org-log-into-drawer ()
  1876. "Return the value of `org-log-into-drawer', but let properties overrule.
  1877. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1878. used instead of the default value."
  1879. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1880. (cond
  1881. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1882. ((equal p "t") "LOGBOOK")
  1883. (t p))))
  1884. (defcustom org-log-state-notes-insert-after-drawers nil
  1885. "Non-nil means insert state change notes after any drawers in entry.
  1886. Only the drawers that *immediately* follow the headline and the
  1887. deadline/scheduled line are skipped.
  1888. When nil, insert notes right after the heading and perhaps the line
  1889. with deadline/scheduling if present.
  1890. This variable will have no effect if `org-log-into-drawer' is
  1891. set."
  1892. :group 'org-todo
  1893. :group 'org-progress
  1894. :type 'boolean)
  1895. (defcustom org-log-states-order-reversed t
  1896. "Non-nil means the latest state note will be directly after heading.
  1897. When nil, the state change notes will be ordered according to time."
  1898. :group 'org-todo
  1899. :group 'org-progress
  1900. :type 'boolean)
  1901. (defcustom org-log-repeat 'time
  1902. "Non-nil means record moving through the DONE state when triggering repeat.
  1903. An auto-repeating task is immediately switched back to TODO when
  1904. marked DONE. If you are not logging state changes (by adding \"@\"
  1905. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1906. record a closing note, there will be no record of the task moving
  1907. through DONE. This variable forces taking a note anyway.
  1908. nil Don't force a record
  1909. time Record a time stamp
  1910. note Record a note
  1911. This option can also be set with on a per-file-basis with
  1912. #+STARTUP: logrepeat
  1913. #+STARTUP: lognoterepeat
  1914. #+STARTUP: nologrepeat
  1915. You can have local logging settings for a subtree by setting the LOGGING
  1916. property to one or more of these keywords."
  1917. :group 'org-todo
  1918. :group 'org-progress
  1919. :type '(choice
  1920. (const :tag "Don't force a record" nil)
  1921. (const :tag "Force recording the DONE state" time)
  1922. (const :tag "Force recording a note with the DONE state" note)))
  1923. (defgroup org-priorities nil
  1924. "Priorities in Org-mode."
  1925. :tag "Org Priorities"
  1926. :group 'org-todo)
  1927. (defcustom org-enable-priority-commands t
  1928. "Non-nil means priority commands are active.
  1929. When nil, these commands will be disabled, so that you never accidentally
  1930. set a priority."
  1931. :group 'org-priorities
  1932. :type 'boolean)
  1933. (defcustom org-highest-priority ?A
  1934. "The highest priority of TODO items. A character like ?A, ?B etc.
  1935. Must have a smaller ASCII number than `org-lowest-priority'."
  1936. :group 'org-priorities
  1937. :type 'character)
  1938. (defcustom org-lowest-priority ?C
  1939. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1940. Must have a larger ASCII number than `org-highest-priority'."
  1941. :group 'org-priorities
  1942. :type 'character)
  1943. (defcustom org-default-priority ?B
  1944. "The default priority of TODO items.
  1945. This is the priority an item get if no explicit priority is given."
  1946. :group 'org-priorities
  1947. :type 'character)
  1948. (defcustom org-priority-start-cycle-with-default t
  1949. "Non-nil means start with default priority when starting to cycle.
  1950. When this is nil, the first step in the cycle will be (depending on the
  1951. command used) one higher or lower that the default priority."
  1952. :group 'org-priorities
  1953. :type 'boolean)
  1954. (defgroup org-time nil
  1955. "Options concerning time stamps and deadlines in Org-mode."
  1956. :tag "Org Time"
  1957. :group 'org)
  1958. (defcustom org-insert-labeled-timestamps-at-point nil
  1959. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  1960. When nil, these labeled time stamps are forces into the second line of an
  1961. entry, just after the headline. When scheduling from the global TODO list,
  1962. the time stamp will always be forced into the second line."
  1963. :group 'org-time
  1964. :type 'boolean)
  1965. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1966. "Formats for `format-time-string' which are used for time stamps.
  1967. It is not recommended to change this constant.")
  1968. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1969. "Number of minutes to round time stamps to.
  1970. These are two values, the first applies when first creating a time stamp.
  1971. The second applies when changing it with the commands `S-up' and `S-down'.
  1972. When changing the time stamp, this means that it will change in steps
  1973. of N minutes, as given by the second value.
  1974. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1975. numbers should be factors of 60, so for example 5, 10, 15.
  1976. When this is larger than 1, you can still force an exact time-stamp by using
  1977. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1978. and by using a prefix arg to `S-up/down' to specify the exact number
  1979. of minutes to shift."
  1980. :group 'org-time
  1981. :get '(lambda (var) ; Make sure all entries have 5 elements
  1982. (if (integerp (default-value var))
  1983. (list (default-value var) 5)
  1984. (default-value var)))
  1985. :type '(list
  1986. (integer :tag "when inserting times")
  1987. (integer :tag "when modifying times")))
  1988. ;; Normalize old customizations of this variable.
  1989. (when (integerp org-time-stamp-rounding-minutes)
  1990. (setq org-time-stamp-rounding-minutes
  1991. (list org-time-stamp-rounding-minutes
  1992. org-time-stamp-rounding-minutes)))
  1993. (defcustom org-display-custom-times nil
  1994. "Non-nil means overlay custom formats over all time stamps.
  1995. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1996. To turn this on on a per-file basis, insert anywhere in the file:
  1997. #+STARTUP: customtime"
  1998. :group 'org-time
  1999. :set 'set-default
  2000. :type 'sexp)
  2001. (make-variable-buffer-local 'org-display-custom-times)
  2002. (defcustom org-time-stamp-custom-formats
  2003. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2004. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2005. These are overlayed over the default ISO format if the variable
  2006. `org-display-custom-times' is set. Time like %H:%M should be at the
  2007. end of the second format. The custom formats are also honored by export
  2008. commands, if custom time display is turned on at the time of export."
  2009. :group 'org-time
  2010. :type 'sexp)
  2011. (defun org-time-stamp-format (&optional long inactive)
  2012. "Get the right format for a time string."
  2013. (let ((f (if long (cdr org-time-stamp-formats)
  2014. (car org-time-stamp-formats))))
  2015. (if inactive
  2016. (concat "[" (substring f 1 -1) "]")
  2017. f)))
  2018. (defcustom org-time-clocksum-format "%d:%02d"
  2019. "The format string used when creating CLOCKSUM lines, or when
  2020. org-mode generates a time duration."
  2021. :group 'org-time
  2022. :type 'string)
  2023. (defcustom org-time-clocksum-use-fractional nil
  2024. "If non-nil, \\[org-clock-display] uses fractional times.
  2025. org-mode generates a time duration."
  2026. :group 'org-time
  2027. :type 'boolean)
  2028. (defcustom org-time-clocksum-fractional-format "%.2f"
  2029. "The format string used when creating CLOCKSUM lines, or when
  2030. org-mode generates a time duration."
  2031. :group 'org-time
  2032. :type 'string)
  2033. (defcustom org-deadline-warning-days 14
  2034. "No. of days before expiration during which a deadline becomes active.
  2035. This variable governs the display in sparse trees and in the agenda.
  2036. When 0 or negative, it means use this number (the absolute value of it)
  2037. even if a deadline has a different individual lead time specified.
  2038. Custom commands can set this variable in the options section."
  2039. :group 'org-time
  2040. :group 'org-agenda-daily/weekly
  2041. :type 'integer)
  2042. (defcustom org-read-date-prefer-future t
  2043. "Non-nil means assume future for incomplete date input from user.
  2044. This affects the following situations:
  2045. 1. The user gives a month but not a year.
  2046. For example, if it is april and you enter \"feb 2\", this will be read
  2047. as feb 2, *next* year. \"May 5\", however, will be this year.
  2048. 2. The user gives a day, but no month.
  2049. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2050. read this as the third of *next* month. However, if you enter \"17\",
  2051. it will be considered as *this* month.
  2052. If you set this variable to the symbol `time', then also the following
  2053. will work:
  2054. 3. If the user gives a time, but no day. If the time is before now,
  2055. to will be interpreted as tomorrow.
  2056. Currently none of this works for ISO week specifications.
  2057. When this option is nil, the current day, month and year will always be
  2058. used as defaults."
  2059. :group 'org-time
  2060. :type '(choice
  2061. (const :tag "Never" nil)
  2062. (const :tag "Check month and day" t)
  2063. (const :tag "Check month, day, and time" time)))
  2064. (defcustom org-read-date-display-live t
  2065. "Non-nil means display current interpretation of date prompt live.
  2066. This display will be in an overlay, in the minibuffer."
  2067. :group 'org-time
  2068. :type 'boolean)
  2069. (defcustom org-read-date-popup-calendar t
  2070. "Non-nil means pop up a calendar when prompting for a date.
  2071. In the calendar, the date can be selected with mouse-1. However, the
  2072. minibuffer will also be active, and you can simply enter the date as well.
  2073. When nil, only the minibuffer will be available."
  2074. :group 'org-time
  2075. :type 'boolean)
  2076. (if (fboundp 'defvaralias)
  2077. (defvaralias 'org-popup-calendar-for-date-prompt
  2078. 'org-read-date-popup-calendar))
  2079. (defcustom org-read-date-minibuffer-setup-hook nil
  2080. "Hook to be used to set up keys for the date/time interface.
  2081. Add key definitions to `minibuffer-local-map', which will be a temporary
  2082. copy."
  2083. :group 'org-time
  2084. :type 'hook)
  2085. (defcustom org-extend-today-until 0
  2086. "The hour when your day really ends. Must be an integer.
  2087. This has influence for the following applications:
  2088. - When switching the agenda to \"today\". It it is still earlier than
  2089. the time given here, the day recognized as TODAY is actually yesterday.
  2090. - When a date is read from the user and it is still before the time given
  2091. here, the current date and time will be assumed to be yesterday, 23:59.
  2092. Also, timestamps inserted in remember templates follow this rule.
  2093. IMPORTANT: This is a feature whose implementation is and likely will
  2094. remain incomplete. Really, it is only here because past midnight seems to
  2095. be the favorite working time of John Wiegley :-)"
  2096. :group 'org-time
  2097. :type 'integer)
  2098. (defcustom org-edit-timestamp-down-means-later nil
  2099. "Non-nil means S-down will increase the time in a time stamp.
  2100. When nil, S-up will increase."
  2101. :group 'org-time
  2102. :type 'boolean)
  2103. (defcustom org-calendar-follow-timestamp-change t
  2104. "Non-nil means make the calendar window follow timestamp changes.
  2105. When a timestamp is modified and the calendar window is visible, it will be
  2106. moved to the new date."
  2107. :group 'org-time
  2108. :type 'boolean)
  2109. (defgroup org-tags nil
  2110. "Options concerning tags in Org-mode."
  2111. :tag "Org Tags"
  2112. :group 'org)
  2113. (defcustom org-tag-alist nil
  2114. "List of tags allowed in Org-mode files.
  2115. When this list is nil, Org-mode will base TAG input on what is already in the
  2116. buffer.
  2117. The value of this variable is an alist, the car of each entry must be a
  2118. keyword as a string, the cdr may be a character that is used to select
  2119. that tag through the fast-tag-selection interface.
  2120. See the manual for details."
  2121. :group 'org-tags
  2122. :type '(repeat
  2123. (choice
  2124. (cons (string :tag "Tag name")
  2125. (character :tag "Access char"))
  2126. (list :tag "Start radio group"
  2127. (const :startgroup)
  2128. (option (string :tag "Group description")))
  2129. (list :tag "End radio group"
  2130. (const :endgroup)
  2131. (option (string :tag "Group description")))
  2132. (const :tag "New line" (:newline)))))
  2133. (defcustom org-tag-persistent-alist nil
  2134. "List of tags that will always appear in all Org-mode files.
  2135. This is in addition to any in buffer settings or customizations
  2136. of `org-tag-alist'.
  2137. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2138. The value of this variable is an alist, the car of each entry must be a
  2139. keyword as a string, the cdr may be a character that is used to select
  2140. that tag through the fast-tag-selection interface.
  2141. See the manual for details.
  2142. To disable these tags on a per-file basis, insert anywhere in the file:
  2143. #+STARTUP: noptag"
  2144. :group 'org-tags
  2145. :type '(repeat
  2146. (choice
  2147. (cons (string :tag "Tag name")
  2148. (character :tag "Access char"))
  2149. (const :tag "Start radio group" (:startgroup))
  2150. (const :tag "End radio group" (:endgroup))
  2151. (const :tag "New line" (:newline)))))
  2152. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2153. "If non-nil, always offer completion for all tags of all agenda files.
  2154. Instead of customizing this variable directly, you might want to
  2155. set it locally for remember buffers, because there no list of
  2156. tags in that file can be created dynamically (there are none).
  2157. (add-hook 'org-remember-mode-hook
  2158. (lambda ()
  2159. (set (make-local-variable
  2160. 'org-complete-tags-always-offer-all-agenda-tags)
  2161. t)))"
  2162. :group 'org-tags
  2163. :type 'boolean)
  2164. (defvar org-file-tags nil
  2165. "List of tags that can be inherited by all entries in the file.
  2166. The tags will be inherited if the variable `org-use-tag-inheritance'
  2167. says they should be.
  2168. This variable is populated from #+FILETAGS lines.")
  2169. (defcustom org-use-fast-tag-selection 'auto
  2170. "Non-nil means use fast tag selection scheme.
  2171. This is a special interface to select and deselect tags with single keys.
  2172. When nil, fast selection is never used.
  2173. When the symbol `auto', fast selection is used if and only if selection
  2174. characters for tags have been configured, either through the variable
  2175. `org-tag-alist' or through a #+TAGS line in the buffer.
  2176. When t, fast selection is always used and selection keys are assigned
  2177. automatically if necessary."
  2178. :group 'org-tags
  2179. :type '(choice
  2180. (const :tag "Always" t)
  2181. (const :tag "Never" nil)
  2182. (const :tag "When selection characters are configured" 'auto)))
  2183. (defcustom org-fast-tag-selection-single-key nil
  2184. "Non-nil means fast tag selection exits after first change.
  2185. When nil, you have to press RET to exit it.
  2186. During fast tag selection, you can toggle this flag with `C-c'.
  2187. This variable can also have the value `expert'. In this case, the window
  2188. displaying the tags menu is not even shown, until you press C-c again."
  2189. :group 'org-tags
  2190. :type '(choice
  2191. (const :tag "No" nil)
  2192. (const :tag "Yes" t)
  2193. (const :tag "Expert" expert)))
  2194. (defvar org-fast-tag-selection-include-todo nil
  2195. "Non-nil means fast tags selection interface will also offer TODO states.
  2196. This is an undocumented feature, you should not rely on it.")
  2197. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2198. "The column to which tags should be indented in a headline.
  2199. If this number is positive, it specifies the column. If it is negative,
  2200. it means that the tags should be flushright to that column. For example,
  2201. -80 works well for a normal 80 character screen."
  2202. :group 'org-tags
  2203. :type 'integer)
  2204. (defcustom org-auto-align-tags t
  2205. "Non-nil means realign tags after pro/demotion of TODO state change.
  2206. These operations change the length of a headline and therefore shift
  2207. the tags around. With this options turned on, after each such operation
  2208. the tags are again aligned to `org-tags-column'."
  2209. :group 'org-tags
  2210. :type 'boolean)
  2211. (defcustom org-use-tag-inheritance t
  2212. "Non-nil means tags in levels apply also for sublevels.
  2213. When nil, only the tags directly given in a specific line apply there.
  2214. This may also be a list of tags that should be inherited, or a regexp that
  2215. matches tags that should be inherited. Additional control is possible
  2216. with the variable `org-tags-exclude-from-inheritance' which gives an
  2217. explicit list of tags to be excluded from inheritance., even if the value of
  2218. `org-use-tag-inheritance' would select it for inheritance.
  2219. If this option is t, a match early-on in a tree can lead to a large
  2220. number of matches in the subtree when constructing the agenda or creating
  2221. a sparse tree. If you only want to see the first match in a tree during
  2222. a search, check out the variable `org-tags-match-list-sublevels'."
  2223. :group 'org-tags
  2224. :type '(choice
  2225. (const :tag "Not" nil)
  2226. (const :tag "Always" t)
  2227. (repeat :tag "Specific tags" (string :tag "Tag"))
  2228. (regexp :tag "Tags matched by regexp")))
  2229. (defcustom org-tags-exclude-from-inheritance nil
  2230. "List of tags that should never be inherited.
  2231. This is a way to exclude a few tags from inheritance. For way to do
  2232. the opposite, to actively allow inheritance for selected tags,
  2233. see the variable `org-use-tag-inheritance'."
  2234. :group 'org-tags
  2235. :type '(repeat (string :tag "Tag")))
  2236. (defun org-tag-inherit-p (tag)
  2237. "Check if TAG is one that should be inherited."
  2238. (cond
  2239. ((member tag org-tags-exclude-from-inheritance) nil)
  2240. ((eq org-use-tag-inheritance t) t)
  2241. ((not org-use-tag-inheritance) nil)
  2242. ((stringp org-use-tag-inheritance)
  2243. (string-match org-use-tag-inheritance tag))
  2244. ((listp org-use-tag-inheritance)
  2245. (member tag org-use-tag-inheritance))
  2246. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2247. (defcustom org-tags-match-list-sublevels t
  2248. "Non-nil means list also sublevels of headlines matching a search.
  2249. This variable applies to tags/property searches, and also to stuck
  2250. projects because this search is based on a tags match as well.
  2251. When set to the symbol `indented', sublevels are indented with
  2252. leading dots.
  2253. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2254. the sublevels of a headline matching a tag search often also match
  2255. the same search. Listing all of them can create very long lists.
  2256. Setting this variable to nil causes subtrees of a match to be skipped.
  2257. This variable is semi-obsolete and probably should always be true. It
  2258. is better to limit inheritance to certain tags using the variables
  2259. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2260. :group 'org-tags
  2261. :type '(choice
  2262. (const :tag "No, don't list them" nil)
  2263. (const :tag "Yes, do list them" t)
  2264. (const :tag "List them, indented with leading dots" indented)))
  2265. (defcustom org-tags-sort-function nil
  2266. "When set, tags are sorted using this function as a comparator"
  2267. :group 'org-tags
  2268. :type '(choice
  2269. (const :tag "No sorting" nil)
  2270. (const :tag "Alphabetical" string<)
  2271. (const :tag "Reverse alphabetical" string>)
  2272. (function :tag "Custom function" nil)))
  2273. (defvar org-tags-history nil
  2274. "History of minibuffer reads for tags.")
  2275. (defvar org-last-tags-completion-table nil
  2276. "The last used completion table for tags.")
  2277. (defvar org-after-tags-change-hook nil
  2278. "Hook that is run after the tags in a line have changed.")
  2279. (defgroup org-properties nil
  2280. "Options concerning properties in Org-mode."
  2281. :tag "Org Properties"
  2282. :group 'org)
  2283. (defcustom org-property-format "%-10s %s"
  2284. "How property key/value pairs should be formatted by `indent-line'.
  2285. When `indent-line' hits a property definition, it will format the line
  2286. according to this format, mainly to make sure that the values are
  2287. lined-up with respect to each other."
  2288. :group 'org-properties
  2289. :type 'string)
  2290. (defcustom org-use-property-inheritance nil
  2291. "Non-nil means properties apply also for sublevels.
  2292. This setting is chiefly used during property searches. Turning it on can
  2293. cause significant overhead when doing a search, which is why it is not
  2294. on by default.
  2295. When nil, only the properties directly given in the current entry count.
  2296. When t, every property is inherited. The value may also be a list of
  2297. properties that should have inheritance, or a regular expression matching
  2298. properties that should be inherited.
  2299. However, note that some special properties use inheritance under special
  2300. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2301. and the properties ending in \"_ALL\" when they are used as descriptor
  2302. for valid values of a property.
  2303. Note for programmers:
  2304. When querying an entry with `org-entry-get', you can control if inheritance
  2305. should be used. By default, `org-entry-get' looks only at the local
  2306. properties. You can request inheritance by setting the inherit argument
  2307. to t (to force inheritance) or to `selective' (to respect the setting
  2308. in this variable)."
  2309. :group 'org-properties
  2310. :type '(choice
  2311. (const :tag "Not" nil)
  2312. (const :tag "Always" t)
  2313. (repeat :tag "Specific properties" (string :tag "Property"))
  2314. (regexp :tag "Properties matched by regexp")))
  2315. (defun org-property-inherit-p (property)
  2316. "Check if PROPERTY is one that should be inherited."
  2317. (cond
  2318. ((eq org-use-property-inheritance t) t)
  2319. ((not org-use-property-inheritance) nil)
  2320. ((stringp org-use-property-inheritance)
  2321. (string-match org-use-property-inheritance property))
  2322. ((listp org-use-property-inheritance)
  2323. (member property org-use-property-inheritance))
  2324. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2325. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2326. "The default column format, if no other format has been defined.
  2327. This variable can be set on the per-file basis by inserting a line
  2328. #+COLUMNS: %25ITEM ....."
  2329. :group 'org-properties
  2330. :type 'string)
  2331. (defcustom org-columns-ellipses ".."
  2332. "The ellipses to be used when a field in column view is truncated.
  2333. When this is the empty string, as many characters as possible are shown,
  2334. but then there will be no visual indication that the field has been truncated.
  2335. When this is a string of length N, the last N characters of a truncated
  2336. field are replaced by this string. If the column is narrower than the
  2337. ellipses string, only part of the ellipses string will be shown."
  2338. :group 'org-properties
  2339. :type 'string)
  2340. (defcustom org-columns-modify-value-for-display-function nil
  2341. "Function that modifies values for display in column view.
  2342. For example, it can be used to cut out a certain part from a time stamp.
  2343. The function must take 2 arguments:
  2344. column-title The title of the column (*not* the property name)
  2345. value The value that should be modified.
  2346. The function should return the value that should be displayed,
  2347. or nil if the normal value should be used."
  2348. :group 'org-properties
  2349. :type 'function)
  2350. (defcustom org-effort-property "Effort"
  2351. "The property that is being used to keep track of effort estimates.
  2352. Effort estimates given in this property need to have the format H:MM."
  2353. :group 'org-properties
  2354. :group 'org-progress
  2355. :type '(string :tag "Property"))
  2356. (defconst org-global-properties-fixed
  2357. '(("VISIBILITY_ALL" . "folded children content all")
  2358. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2359. "List of property/value pairs that can be inherited by any entry.
  2360. These are fixed values, for the preset properties. The user variable
  2361. that can be used to add to this list is `org-global-properties'.
  2362. The entries in this list are cons cells where the car is a property
  2363. name and cdr is a string with the value. If the value represents
  2364. multiple items like an \"_ALL\" property, separate the items by
  2365. spaces.")
  2366. (defcustom org-global-properties nil
  2367. "List of property/value pairs that can be inherited by any entry.
  2368. This list will be combined with the constant `org-global-properties-fixed'.
  2369. The entries in this list are cons cells where the car is a property
  2370. name and cdr is a string with the value.
  2371. You can set buffer-local values for the same purpose in the variable
  2372. `org-file-properties' this by adding lines like
  2373. #+PROPERTY: NAME VALUE"
  2374. :group 'org-properties
  2375. :type '(repeat
  2376. (cons (string :tag "Property")
  2377. (string :tag "Value"))))
  2378. (defvar org-file-properties nil
  2379. "List of property/value pairs that can be inherited by any entry.
  2380. Valid for the current buffer.
  2381. This variable is populated from #+PROPERTY lines.")
  2382. (make-variable-buffer-local 'org-file-properties)
  2383. (defgroup org-agenda nil
  2384. "Options concerning agenda views in Org-mode."
  2385. :tag "Org Agenda"
  2386. :group 'org)
  2387. (defvar org-category nil
  2388. "Variable used by org files to set a category for agenda display.
  2389. Such files should use a file variable to set it, for example
  2390. # -*- mode: org; org-category: \"ELisp\"
  2391. or contain a special line
  2392. #+CATEGORY: ELisp
  2393. If the file does not specify a category, then file's base name
  2394. is used instead.")
  2395. (make-variable-buffer-local 'org-category)
  2396. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2397. (defcustom org-agenda-files nil
  2398. "The files to be used for agenda display.
  2399. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2400. \\[org-remove-file]. You can also use customize to edit the list.
  2401. If an entry is a directory, all files in that directory that are matched by
  2402. `org-agenda-file-regexp' will be part of the file list.
  2403. If the value of the variable is not a list but a single file name, then
  2404. the list of agenda files is actually stored and maintained in that file, one
  2405. agenda file per line. In this file paths can be given relative to
  2406. `org-directory'. Tilde expansion and environment variable substitution
  2407. are also made."
  2408. :group 'org-agenda
  2409. :type '(choice
  2410. (repeat :tag "List of files and directories" file)
  2411. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2412. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2413. "Regular expression to match files for `org-agenda-files'.
  2414. If any element in the list in that variable contains a directory instead
  2415. of a normal file, all files in that directory that are matched by this
  2416. regular expression will be included."
  2417. :group 'org-agenda
  2418. :type 'regexp)
  2419. (defcustom org-agenda-text-search-extra-files nil
  2420. "List of extra files to be searched by text search commands.
  2421. These files will be search in addition to the agenda files by the
  2422. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2423. Note that these files will only be searched for text search commands,
  2424. not for the other agenda views like todo lists, tag searches or the weekly
  2425. agenda. This variable is intended to list notes and possibly archive files
  2426. that should also be searched by these two commands.
  2427. In fact, if the first element in the list is the symbol `agenda-archives',
  2428. than all archive files of all agenda files will be added to the search
  2429. scope."
  2430. :group 'org-agenda
  2431. :type '(set :greedy t
  2432. (const :tag "Agenda Archives" agenda-archives)
  2433. (repeat :inline t (file))))
  2434. (if (fboundp 'defvaralias)
  2435. (defvaralias 'org-agenda-multi-occur-extra-files
  2436. 'org-agenda-text-search-extra-files))
  2437. (defcustom org-agenda-skip-unavailable-files nil
  2438. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2439. A nil value means to remove them, after a query, from the list."
  2440. :group 'org-agenda
  2441. :type 'boolean)
  2442. (defcustom org-calendar-to-agenda-key [?c]
  2443. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2444. The command `org-calendar-goto-agenda' will be bound to this key. The
  2445. default is the character `c' because then `c' can be used to switch back and
  2446. forth between agenda and calendar."
  2447. :group 'org-agenda
  2448. :type 'sexp)
  2449. (defcustom org-calendar-agenda-action-key [?k]
  2450. "The key to be installed in `calendar-mode-map' for agenda-action.
  2451. The command `org-agenda-action' will be bound to this key. The
  2452. default is the character `k' because we use the same key in the agenda."
  2453. :group 'org-agenda
  2454. :type 'sexp)
  2455. (defcustom org-calendar-insert-diary-entry-key [?i]
  2456. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2457. This option is irrelevant until `org-agenda-diary-file' has been configured
  2458. to point to an Org-mode file. When that is the case, the command
  2459. `org-agenda-diary-entry' will be bound to the key given here, by default
  2460. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2461. if you want to continue doing this, you need to change this to a different
  2462. key."
  2463. :group 'org-agenda
  2464. :type 'sexp)
  2465. (defcustom org-agenda-diary-file 'diary-file
  2466. "File to which to add new entries with the `i' key in agenda and calendar.
  2467. When this is the symbol `diary-file', the functionality in the Emacs
  2468. calendar will be used to add entries to the `diary-file'. But when this
  2469. points to a file, `org-agenda-diary-entry' will be used instead."
  2470. :group 'org-agenda
  2471. :type '(choice
  2472. (const :tag "The standard Emacs diary file" diary-file)
  2473. (file :tag "Special Org file diary entries")))
  2474. (eval-after-load "calendar"
  2475. '(progn
  2476. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2477. 'org-calendar-goto-agenda)
  2478. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2479. 'org-agenda-action)
  2480. (add-hook 'calendar-mode-hook
  2481. (lambda ()
  2482. (unless (eq org-agenda-diary-file 'diary-file)
  2483. (define-key calendar-mode-map
  2484. org-calendar-insert-diary-entry-key
  2485. 'org-agenda-diary-entry))))))
  2486. (defgroup org-latex nil
  2487. "Options for embedding LaTeX code into Org-mode."
  2488. :tag "Org LaTeX"
  2489. :group 'org)
  2490. (defcustom org-format-latex-options
  2491. '(:foreground default :background default :scale 1.0
  2492. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2493. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2494. "Options for creating images from LaTeX fragments.
  2495. This is a property list with the following properties:
  2496. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2497. `default' means use the foreground of the default face.
  2498. :background the background color, or \"Transparent\".
  2499. `default' means use the background of the default face.
  2500. :scale a scaling factor for the size of the images.
  2501. :html-foreground, :html-background, :html-scale
  2502. the same numbers for HTML export.
  2503. :matchers a list indicating which matchers should be used to
  2504. find LaTeX fragments. Valid members of this list are:
  2505. \"begin\" find environments
  2506. \"$1\" find single characters surrounded by $.$
  2507. \"$\" find math expressions surrounded by $...$
  2508. \"$$\" find math expressions surrounded by $$....$$
  2509. \"\\(\" find math expressions surrounded by \\(...\\)
  2510. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2511. :group 'org-latex
  2512. :type 'plist)
  2513. (defcustom org-format-latex-signal-error t
  2514. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2515. When nil, just push out a message."
  2516. :group 'org-latex
  2517. :type 'boolean)
  2518. (defcustom org-format-latex-header "\\documentclass{article}
  2519. \\usepackage[usenames]{color}
  2520. \\usepackage{amsmath}
  2521. \\usepackage[mathscr]{eucal}
  2522. \\pagestyle{empty} % do not remove
  2523. \[PACKAGES]
  2524. \[DEFAULT-PACKAGES]
  2525. % The settings below are copied from fullpage.sty
  2526. \\setlength{\\textwidth}{\\paperwidth}
  2527. \\addtolength{\\textwidth}{-3cm}
  2528. \\setlength{\\oddsidemargin}{1.5cm}
  2529. \\addtolength{\\oddsidemargin}{-2.54cm}
  2530. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2531. \\setlength{\\textheight}{\\paperheight}
  2532. \\addtolength{\\textheight}{-\\headheight}
  2533. \\addtolength{\\textheight}{-\\headsep}
  2534. \\addtolength{\\textheight}{-\\footskip}
  2535. \\addtolength{\\textheight}{-3cm}
  2536. \\setlength{\\topmargin}{1.5cm}
  2537. \\addtolength{\\topmargin}{-2.54cm}"
  2538. "The document header used for processing LaTeX fragments.
  2539. It is imperative that this header make sure that no page number
  2540. appears on the page. The package defined in the variables
  2541. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2542. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2543. will be appended."
  2544. :group 'org-latex
  2545. :type 'string)
  2546. (defvar org-format-latex-header-extra nil)
  2547. ;; The following variables are defined here because is it also used
  2548. ;; when formatting latex fragments. Originally it was part of the
  2549. ;; LaTeX exporter, which is why the name includes "export".
  2550. (defcustom org-export-latex-default-packages-alist
  2551. '(("AUTO" "inputenc")
  2552. ("T1" "fontenc")
  2553. ("" "fixltx2e")
  2554. ("" "graphicx")
  2555. ("" "longtable")
  2556. ("" "float")
  2557. ("" "wrapfig")
  2558. ("" "soul")
  2559. ("" "t1enc")
  2560. ("" "textcomp")
  2561. ("" "marvosym")
  2562. ("" "wasysym")
  2563. ("" "latexsym")
  2564. ("" "amssymb")
  2565. ("" "hyperref")
  2566. ; ("" "microtype")
  2567. "\\tolerance=1000"
  2568. )
  2569. "Alist of default packages to be inserted in the header.
  2570. Change this only if one of the packages here causes an incompatibility
  2571. with another package you are using.
  2572. The packages in this list are needed by one part or another of Org-mode
  2573. to function properly.
  2574. - inputenc, fontenc, t1enc: for basic font and character selection
  2575. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2576. for interpreting the entities in `org-entities'. You can skip some of these
  2577. packages if you don't use any of the symbols in it.
  2578. - graphicx: for including images
  2579. - float, wrapfig: for figure placement
  2580. - longtable: for long tables
  2581. - hyperref: for cross references
  2582. Therefore you should not modify this variable unless you know what you
  2583. are doing. The one reason to change it anyway is that you might be loading
  2584. some other package that conflicts with one of the default packages.
  2585. Each cell is of the format \( \"options\" \"package\" \)."
  2586. :group 'org-export-latex
  2587. :type '(repeat
  2588. (choice
  2589. (string :tag "A line of LaTeX")
  2590. (list :tag "options/package pair"
  2591. (string :tag "options")
  2592. (string :tag "package")))))
  2593. (defcustom org-export-latex-packages-alist nil
  2594. "Alist of packages to be inserted in every LaTeX the header.
  2595. These will be inserted after `org-export-latex-default-packages-alist'.
  2596. Each cell is of the format \( \"options\" \"package\" \).
  2597. Make sure that you only lis packages here which:
  2598. - you want in every file
  2599. - do not conflict with the default packages in
  2600. `org-export-latex-default-packages-alist'
  2601. - do not conflict with the setup in `org-format-latex-header'."
  2602. :group 'org-export-latex
  2603. :type '(repeat
  2604. (choice
  2605. (string :tag "A line of LaTeX")
  2606. (list :tag "options/package pair"
  2607. (string :tag "options")
  2608. (string :tag "package")))))
  2609. (defgroup org-appearance nil
  2610. "Settings for Org-mode appearance."
  2611. :tag "Org Appearance"
  2612. :group 'org)
  2613. (defcustom org-level-color-stars-only nil
  2614. "Non-nil means fontify only the stars in each headline.
  2615. When nil, the entire headline is fontified.
  2616. Changing it requires restart of `font-lock-mode' to become effective
  2617. also in regions already fontified."
  2618. :group 'org-appearance
  2619. :type 'boolean)
  2620. (defcustom org-hide-leading-stars nil
  2621. "Non-nil means hide the first N-1 stars in a headline.
  2622. This works by using the face `org-hide' for these stars. This
  2623. face is white for a light background, and black for a dark
  2624. background. You may have to customize the face `org-hide' to
  2625. make this work.
  2626. Changing it requires restart of `font-lock-mode' to become effective
  2627. also in regions already fontified.
  2628. You may also set this on a per-file basis by adding one of the following
  2629. lines to the buffer:
  2630. #+STARTUP: hidestars
  2631. #+STARTUP: showstars"
  2632. :group 'org-appearance
  2633. :type 'boolean)
  2634. (defcustom org-hidden-keywords nil
  2635. "List of keywords that should be hidden when typed in the org buffer.
  2636. For example, add #+TITLE to this list in order to make the
  2637. document title appear in the buffer without the initial #+TITLE:
  2638. keyword."
  2639. :group 'org-appearance
  2640. :type '(set (const :tag "#+AUTHOR" author)
  2641. (const :tag "#+DATE" date)
  2642. (const :tag "#+EMAIL" email)
  2643. (const :tag "#+TITLE" title)))
  2644. (defcustom org-fontify-done-headline nil
  2645. "Non-nil means change the face of a headline if it is marked DONE.
  2646. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2647. When this is non-nil, the headline after the keyword is set to the
  2648. `org-headline-done' as an additional indication."
  2649. :group 'org-appearance
  2650. :type 'boolean)
  2651. (defcustom org-fontify-emphasized-text t
  2652. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2653. Changing this variable requires a restart of Emacs to take effect."
  2654. :group 'org-appearance
  2655. :type 'boolean)
  2656. (defcustom org-fontify-whole-heading-line nil
  2657. "Non-nil means fontify the whole line for headings.
  2658. This is useful when setting a background color for the
  2659. org-level-* faces."
  2660. :group 'org-appearance
  2661. :type 'boolean)
  2662. (defcustom org-highlight-latex-fragments-and-specials nil
  2663. "Non-nil means fontify what is treated specially by the exporters."
  2664. :group 'org-appearance
  2665. :type 'boolean)
  2666. (defcustom org-hide-emphasis-markers nil
  2667. "Non-nil mean font-lock should hide the emphasis marker characters."
  2668. :group 'org-appearance
  2669. :type 'boolean)
  2670. (defvar org-emph-re nil
  2671. "Regular expression for matching emphasis.")
  2672. (defvar org-verbatim-re nil
  2673. "Regular expression for matching verbatim text.")
  2674. (defvar org-emphasis-regexp-components) ; defined just below
  2675. (defvar org-emphasis-alist) ; defined just below
  2676. (defun org-set-emph-re (var val)
  2677. "Set variable and compute the emphasis regular expression."
  2678. (set var val)
  2679. (when (and (boundp 'org-emphasis-alist)
  2680. (boundp 'org-emphasis-regexp-components)
  2681. org-emphasis-alist org-emphasis-regexp-components)
  2682. (let* ((e org-emphasis-regexp-components)
  2683. (pre (car e))
  2684. (post (nth 1 e))
  2685. (border (nth 2 e))
  2686. (body (nth 3 e))
  2687. (nl (nth 4 e))
  2688. (body1 (concat body "*?"))
  2689. (markers (mapconcat 'car org-emphasis-alist ""))
  2690. (vmarkers (mapconcat
  2691. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2692. org-emphasis-alist "")))
  2693. ;; make sure special characters appear at the right position in the class
  2694. (if (string-match "\\^" markers)
  2695. (setq markers (concat (replace-match "" t t markers) "^")))
  2696. (if (string-match "-" markers)
  2697. (setq markers (concat (replace-match "" t t markers) "-")))
  2698. (if (string-match "\\^" vmarkers)
  2699. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2700. (if (string-match "-" vmarkers)
  2701. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2702. (if (> nl 0)
  2703. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2704. (int-to-string nl) "\\}")))
  2705. ;; Make the regexp
  2706. (setq org-emph-re
  2707. (concat "\\([" pre "]\\|^\\)"
  2708. "\\("
  2709. "\\([" markers "]\\)"
  2710. "\\("
  2711. "[^" border "]\\|"
  2712. "[^" border "]"
  2713. body1
  2714. "[^" border "]"
  2715. "\\)"
  2716. "\\3\\)"
  2717. "\\([" post "]\\|$\\)"))
  2718. (setq org-verbatim-re
  2719. (concat "\\([" pre "]\\|^\\)"
  2720. "\\("
  2721. "\\([" vmarkers "]\\)"
  2722. "\\("
  2723. "[^" border "]\\|"
  2724. "[^" border "]"
  2725. body1
  2726. "[^" border "]"
  2727. "\\)"
  2728. "\\3\\)"
  2729. "\\([" post "]\\|$\\)")))))
  2730. (defcustom org-emphasis-regexp-components
  2731. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2732. "Components used to build the regular expression for emphasis.
  2733. This is a list with 6 entries. Terminology: In an emphasis string
  2734. like \" *strong word* \", we call the initial space PREMATCH, the final
  2735. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2736. and \"trong wor\" is the body. The different components in this variable
  2737. specify what is allowed/forbidden in each part:
  2738. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2739. post Chars allowed as postmatch. End of line will be allowed too.
  2740. border The chars *forbidden* as border characters.
  2741. body-regexp A regexp like \".\" to match a body character. Don't use
  2742. non-shy groups here, and don't allow newline here.
  2743. newline The maximum number of newlines allowed in an emphasis exp.
  2744. Use customize to modify this, or restart Emacs after changing it."
  2745. :group 'org-appearance
  2746. :set 'org-set-emph-re
  2747. :type '(list
  2748. (sexp :tag "Allowed chars in pre ")
  2749. (sexp :tag "Allowed chars in post ")
  2750. (sexp :tag "Forbidden chars in border ")
  2751. (sexp :tag "Regexp for body ")
  2752. (integer :tag "number of newlines allowed")
  2753. (option (boolean :tag "Please ignore this button"))))
  2754. (defcustom org-emphasis-alist
  2755. `(("*" bold "<b>" "</b>")
  2756. ("/" italic "<i>" "</i>")
  2757. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2758. ("=" org-code "<code>" "</code>" verbatim)
  2759. ("~" org-verbatim "<code>" "</code>" verbatim)
  2760. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2761. "<del>" "</del>")
  2762. )
  2763. "Special syntax for emphasized text.
  2764. Text starting and ending with a special character will be emphasized, for
  2765. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2766. characters, the face to be used by font-lock for highlighting in Org-mode
  2767. Emacs buffers, and the HTML tags to be used for this.
  2768. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2769. Use customize to modify this, or restart Emacs after changing it."
  2770. :group 'org-appearance
  2771. :set 'org-set-emph-re
  2772. :type '(repeat
  2773. (list
  2774. (string :tag "Marker character")
  2775. (choice
  2776. (face :tag "Font-lock-face")
  2777. (plist :tag "Face property list"))
  2778. (string :tag "HTML start tag")
  2779. (string :tag "HTML end tag")
  2780. (option (const verbatim)))))
  2781. (defvar org-protecting-blocks
  2782. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2783. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2784. This is needed for font-lock setup.")
  2785. ;;; Miscellaneous options
  2786. (defgroup org-completion nil
  2787. "Completion in Org-mode."
  2788. :tag "Org Completion"
  2789. :group 'org)
  2790. (defcustom org-completion-use-ido nil
  2791. "Non-nil means use ido completion wherever possible.
  2792. Note that `ido-mode' must be active for this variable to be relevant.
  2793. If you decide to turn this variable on, you might well want to turn off
  2794. `org-outline-path-complete-in-steps'.
  2795. See also `org-completion-use-iswitchb'."
  2796. :group 'org-completion
  2797. :type 'boolean)
  2798. (defcustom org-completion-use-iswitchb nil
  2799. "Non-nil means use iswitchb completion wherever possible.
  2800. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2801. If you decide to turn this variable on, you might well want to turn off
  2802. `org-outline-path-complete-in-steps'.
  2803. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2804. :group 'org-completion
  2805. :type 'boolean)
  2806. (defcustom org-completion-fallback-command 'hippie-expand
  2807. "The expansion command called by \\[org-complete] in normal context.
  2808. Normal means no org-mode-specific context."
  2809. :group 'org-completion
  2810. :type 'function)
  2811. ;;; Functions and variables from their packages
  2812. ;; Declared here to avoid compiler warnings
  2813. ;; XEmacs only
  2814. (defvar outline-mode-menu-heading)
  2815. (defvar outline-mode-menu-show)
  2816. (defvar outline-mode-menu-hide)
  2817. (defvar zmacs-regions) ; XEmacs regions
  2818. ;; Emacs only
  2819. (defvar mark-active)
  2820. ;; Various packages
  2821. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2822. (declare-function calendar-forward-day "cal-move" (arg))
  2823. (declare-function calendar-goto-date "cal-move" (date))
  2824. (declare-function calendar-goto-today "cal-move" ())
  2825. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2826. (defvar calc-embedded-close-formula)
  2827. (defvar calc-embedded-open-formula)
  2828. (declare-function cdlatex-tab "ext:cdlatex" ())
  2829. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2830. (defvar font-lock-unfontify-region-function)
  2831. (declare-function iswitchb-read-buffer "iswitchb"
  2832. (prompt &optional default require-match start matches-set))
  2833. (defvar iswitchb-temp-buflist)
  2834. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2835. (defvar org-agenda-tags-todo-honor-ignore-options)
  2836. (declare-function org-agenda-skip "org-agenda" ())
  2837. (declare-function
  2838. org-format-agenda-item "org-agenda"
  2839. (extra txt &optional category tags dotime noprefix remove-re habitp))
  2840. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2841. (declare-function org-agenda-change-all-lines "org-agenda"
  2842. (newhead hdmarker &optional fixface just-this))
  2843. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2844. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2845. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2846. (beg end))
  2847. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2848. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2849. "org-agenda" (&optional end))
  2850. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2851. (declare-function org-indent-mode "org-indent" (&optional arg))
  2852. (declare-function parse-time-string "parse-time" (string))
  2853. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2854. (defvar remember-data-file)
  2855. (defvar texmathp-why)
  2856. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2857. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2858. (defvar w3m-current-url)
  2859. (defvar w3m-current-title)
  2860. (defvar org-latex-regexps)
  2861. ;;; Autoload and prepare some org modules
  2862. ;; Some table stuff that needs to be defined here, because it is used
  2863. ;; by the functions setting up org-mode or checking for table context.
  2864. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2865. "Detects an org-type or table-type table.")
  2866. (defconst org-table-line-regexp "^[ \t]*|"
  2867. "Detects an org-type table line.")
  2868. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2869. "Detects an org-type table line.")
  2870. (defconst org-table-hline-regexp "^[ \t]*|-"
  2871. "Detects an org-type table hline.")
  2872. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2873. "Detects a table-type table hline.")
  2874. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2875. "Searching from within a table (any type) this finds the first line
  2876. outside the table.")
  2877. ;; Autoload the functions in org-table.el that are needed by functions here.
  2878. (eval-and-compile
  2879. (org-autoload "org-table"
  2880. '(org-table-align org-table-begin org-table-blank-field
  2881. org-table-convert org-table-convert-region org-table-copy-down
  2882. org-table-copy-region org-table-create
  2883. org-table-create-or-convert-from-region
  2884. org-table-create-with-table.el org-table-current-dline
  2885. org-table-cut-region org-table-delete-column org-table-edit-field
  2886. org-table-edit-formulas org-table-end org-table-eval-formula
  2887. org-table-export org-table-field-info
  2888. org-table-get-stored-formulas org-table-goto-column
  2889. org-table-hline-and-move org-table-import org-table-insert-column
  2890. org-table-insert-hline org-table-insert-row org-table-iterate
  2891. org-table-justify-field-maybe org-table-kill-row
  2892. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2893. org-table-move-column org-table-move-column-left
  2894. org-table-move-column-right org-table-move-row
  2895. org-table-move-row-down org-table-move-row-up
  2896. org-table-next-field org-table-next-row org-table-paste-rectangle
  2897. org-table-previous-field org-table-recalculate
  2898. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2899. org-table-toggle-coordinate-overlays
  2900. org-table-toggle-formula-debugger org-table-wrap-region
  2901. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2902. (defun org-at-table-p (&optional table-type)
  2903. "Return t if the cursor is inside an org-type table.
  2904. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2905. (if org-enable-table-editor
  2906. (save-excursion
  2907. (beginning-of-line 1)
  2908. (looking-at (if table-type org-table-any-line-regexp
  2909. org-table-line-regexp)))
  2910. nil))
  2911. (defsubst org-table-p () (org-at-table-p))
  2912. (defun org-at-table.el-p ()
  2913. "Return t if and only if we are at a table.el table."
  2914. (and (org-at-table-p 'any)
  2915. (save-excursion
  2916. (goto-char (org-table-begin 'any))
  2917. (looking-at org-table1-hline-regexp))))
  2918. (defun org-table-recognize-table.el ()
  2919. "If there is a table.el table nearby, recognize it and move into it."
  2920. (if org-table-tab-recognizes-table.el
  2921. (if (org-at-table.el-p)
  2922. (progn
  2923. (beginning-of-line 1)
  2924. (if (looking-at org-table-dataline-regexp)
  2925. nil
  2926. (if (looking-at org-table1-hline-regexp)
  2927. (progn
  2928. (beginning-of-line 2)
  2929. (if (looking-at org-table-any-border-regexp)
  2930. (beginning-of-line -1)))))
  2931. (if (re-search-forward "|" (org-table-end t) t)
  2932. (progn
  2933. (require 'table)
  2934. (if (table--at-cell-p (point))
  2935. t
  2936. (message "recognizing table.el table...")
  2937. (table-recognize-table)
  2938. (message "recognizing table.el table...done")))
  2939. (error "This should not happen..."))
  2940. t)
  2941. nil)
  2942. nil))
  2943. (defun org-at-table-hline-p ()
  2944. "Return t if the cursor is inside a hline in a table."
  2945. (if org-enable-table-editor
  2946. (save-excursion
  2947. (beginning-of-line 1)
  2948. (looking-at org-table-hline-regexp))
  2949. nil))
  2950. (defvar org-table-clean-did-remove-column nil)
  2951. (defun org-table-map-tables (function)
  2952. "Apply FUNCTION to the start of all tables in the buffer."
  2953. (save-excursion
  2954. (save-restriction
  2955. (widen)
  2956. (goto-char (point-min))
  2957. (while (re-search-forward org-table-any-line-regexp nil t)
  2958. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2959. (beginning-of-line 1)
  2960. (when (looking-at org-table-line-regexp)
  2961. (save-excursion (funcall function))
  2962. (or (looking-at org-table-line-regexp)
  2963. (forward-char 1)))
  2964. (re-search-forward org-table-any-border-regexp nil 1))))
  2965. (message "Mapping tables: done"))
  2966. ;; Declare and autoload functions from org-exp.el & Co
  2967. (declare-function org-default-export-plist "org-exp")
  2968. (declare-function org-infile-export-plist "org-exp")
  2969. (declare-function org-get-current-options "org-exp")
  2970. (eval-and-compile
  2971. (org-autoload "org-exp"
  2972. '(org-export org-export-visible
  2973. org-insert-export-options-template
  2974. org-table-clean-before-export))
  2975. (org-autoload "org-ascii"
  2976. '(org-export-as-ascii org-export-ascii-preprocess
  2977. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2978. org-export-region-as-ascii))
  2979. (org-autoload "org-latex"
  2980. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  2981. org-replace-region-by-latex org-export-region-as-latex
  2982. org-export-as-latex org-export-as-pdf
  2983. org-export-as-pdf-and-open))
  2984. (org-autoload "org-html"
  2985. '(org-export-as-html-and-open
  2986. org-export-as-html-batch org-export-as-html-to-buffer
  2987. org-replace-region-by-html org-export-region-as-html
  2988. org-export-as-html))
  2989. (org-autoload "org-docbook"
  2990. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  2991. org-replace-region-by-docbook org-export-region-as-docbook
  2992. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  2993. org-export-as-docbook))
  2994. (org-autoload "org-icalendar"
  2995. '(org-export-icalendar-this-file
  2996. org-export-icalendar-all-agenda-files
  2997. org-export-icalendar-combine-agenda-files))
  2998. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  2999. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3000. ;; Declare and autoload functions from org-agenda.el
  3001. (eval-and-compile
  3002. (org-autoload "org-agenda"
  3003. '(org-agenda org-agenda-list org-search-view
  3004. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3005. org-diary org-agenda-to-appt
  3006. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3007. ;; Autoload org-remember
  3008. (eval-and-compile
  3009. (org-autoload "org-remember"
  3010. '(org-remember-insinuate org-remember-annotation
  3011. org-remember-apply-template org-remember org-remember-handler)))
  3012. ;; Autoload org-clock.el
  3013. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3014. (beg end))
  3015. (declare-function org-clock-update-mode-line "org-clock" ())
  3016. (declare-function org-resolve-clocks "org-clock"
  3017. (&optional also-non-dangling-p prompt last-valid))
  3018. (defvar org-clock-start-time)
  3019. (defvar org-clock-marker (make-marker)
  3020. "Marker recording the last clock-in.")
  3021. (defvar org-clock-hd-marker (make-marker)
  3022. "Marker recording the last clock-in, but the headline position.")
  3023. (defvar org-clock-heading ""
  3024. "The heading of the current clock entry.")
  3025. (defun org-clock-is-active ()
  3026. "Return non-nil if clock is currently running.
  3027. The return value is actually the clock marker."
  3028. (marker-buffer org-clock-marker))
  3029. (eval-and-compile
  3030. (org-autoload
  3031. "org-clock"
  3032. '(org-clock-in org-clock-out org-clock-cancel
  3033. org-clock-goto org-clock-sum org-clock-display
  3034. org-clock-remove-overlays org-clock-report
  3035. org-clocktable-shift org-dblock-write:clocktable
  3036. org-get-clocktable org-resolve-clocks)))
  3037. (defun org-clock-update-time-maybe ()
  3038. "If this is a CLOCK line, update it and return t.
  3039. Otherwise, return nil."
  3040. (interactive)
  3041. (save-excursion
  3042. (beginning-of-line 1)
  3043. (skip-chars-forward " \t")
  3044. (when (looking-at org-clock-string)
  3045. (let ((re (concat "[ \t]*" org-clock-string
  3046. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3047. "\\([ \t]*=>.*\\)?\\)?"))
  3048. ts te h m s neg)
  3049. (cond
  3050. ((not (looking-at re))
  3051. nil)
  3052. ((not (match-end 2))
  3053. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3054. (> org-clock-marker (point))
  3055. (<= org-clock-marker (point-at-eol)))
  3056. ;; The clock is running here
  3057. (setq org-clock-start-time
  3058. (apply 'encode-time
  3059. (org-parse-time-string (match-string 1))))
  3060. (org-clock-update-mode-line)))
  3061. (t
  3062. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3063. (end-of-line 1)
  3064. (setq ts (match-string 1)
  3065. te (match-string 3))
  3066. (setq s (- (org-float-time
  3067. (apply 'encode-time (org-parse-time-string te)))
  3068. (org-float-time
  3069. (apply 'encode-time (org-parse-time-string ts))))
  3070. neg (< s 0)
  3071. s (abs s)
  3072. h (floor (/ s 3600))
  3073. s (- s (* 3600 h))
  3074. m (floor (/ s 60))
  3075. s (- s (* 60 s)))
  3076. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3077. t))))))
  3078. (defun org-check-running-clock ()
  3079. "Check if the current buffer contains the running clock.
  3080. If yes, offer to stop it and to save the buffer with the changes."
  3081. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3082. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3083. (buffer-name))))
  3084. (org-clock-out)
  3085. (when (y-or-n-p "Save changed buffer?")
  3086. (save-buffer))))
  3087. (defun org-clocktable-try-shift (dir n)
  3088. "Check if this line starts a clock table, if yes, shift the time block."
  3089. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3090. (org-clocktable-shift dir n)))
  3091. ;; Autoload org-timer.el
  3092. (eval-and-compile
  3093. (org-autoload
  3094. "org-timer"
  3095. '(org-timer-start org-timer org-timer-item
  3096. org-timer-change-times-in-region
  3097. org-timer-set-timer
  3098. org-timer-reset-timers
  3099. org-timer-show-remaining-time)))
  3100. ;; Autoload org-feed.el
  3101. (eval-and-compile
  3102. (org-autoload
  3103. "org-feed"
  3104. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3105. ;; Autoload org-indent.el
  3106. ;; Define the variable already here, to make sure we have it.
  3107. (defvar org-indent-mode nil
  3108. "Non-nil if Org-Indent mode is enabled.
  3109. Use the command `org-indent-mode' to change this variable.")
  3110. (eval-and-compile
  3111. (org-autoload
  3112. "org-indent"
  3113. '(org-indent-mode)))
  3114. ;; Autoload org-mobile.el
  3115. (eval-and-compile
  3116. (org-autoload
  3117. "org-mobile"
  3118. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3119. ;; Autoload archiving code
  3120. ;; The stuff that is needed for cycling and tags has to be defined here.
  3121. (defgroup org-archive nil
  3122. "Options concerning archiving in Org-mode."
  3123. :tag "Org Archive"
  3124. :group 'org-structure)
  3125. (defcustom org-archive-location "%s_archive::"
  3126. "The location where subtrees should be archived.
  3127. The value of this variable is a string, consisting of two parts,
  3128. separated by a double-colon. The first part is a filename and
  3129. the second part is a headline.
  3130. When the filename is omitted, archiving happens in the same file.
  3131. %s in the filename will be replaced by the current file
  3132. name (without the directory part). Archiving to a different file
  3133. is useful to keep archived entries from contributing to the
  3134. Org-mode Agenda.
  3135. The archived entries will be filed as subtrees of the specified
  3136. headline. When the headline is omitted, the subtrees are simply
  3137. filed away at the end of the file, as top-level entries. Also in
  3138. the heading you can use %s to represent the file name, this can be
  3139. useful when using the same archive for a number of different files.
  3140. Here are a few examples:
  3141. \"%s_archive::\"
  3142. If the current file is Projects.org, archive in file
  3143. Projects.org_archive, as top-level trees. This is the default.
  3144. \"::* Archived Tasks\"
  3145. Archive in the current file, under the top-level headline
  3146. \"* Archived Tasks\".
  3147. \"~/org/archive.org::\"
  3148. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3149. \"~/org/archive.org::From %s\"
  3150. Archive in file ~/org/archive.org (absolute path), under headlines
  3151. \"From FILENAME\" where file name is the current file name.
  3152. \"basement::** Finished Tasks\"
  3153. Archive in file ./basement (relative path), as level 3 trees
  3154. below the level 2 heading \"** Finished Tasks\".
  3155. You may set this option on a per-file basis by adding to the buffer a
  3156. line like
  3157. #+ARCHIVE: basement::** Finished Tasks
  3158. You may also define it locally for a subtree by setting an ARCHIVE property
  3159. in the entry. If such a property is found in an entry, or anywhere up
  3160. the hierarchy, it will be used."
  3161. :group 'org-archive
  3162. :type 'string)
  3163. (defcustom org-archive-tag "ARCHIVE"
  3164. "The tag that marks a subtree as archived.
  3165. An archived subtree does not open during visibility cycling, and does
  3166. not contribute to the agenda listings.
  3167. After changing this, font-lock must be restarted in the relevant buffers to
  3168. get the proper fontification."
  3169. :group 'org-archive
  3170. :group 'org-keywords
  3171. :type 'string)
  3172. (defcustom org-agenda-skip-archived-trees t
  3173. "Non-nil means the agenda will skip any items located in archived trees.
  3174. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3175. variable is no longer recommended, you should leave it at the value t.
  3176. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3177. :group 'org-archive
  3178. :group 'org-agenda-skip
  3179. :type 'boolean)
  3180. (defcustom org-columns-skip-archived-trees t
  3181. "Non-nil means ignore archived trees when creating column view."
  3182. :group 'org-archive
  3183. :group 'org-properties
  3184. :type 'boolean)
  3185. (defcustom org-cycle-open-archived-trees nil
  3186. "Non-nil means `org-cycle' will open archived trees.
  3187. An archived tree is a tree marked with the tag ARCHIVE.
  3188. When nil, archived trees will stay folded. You can still open them with
  3189. normal outline commands like `show-all', but not with the cycling commands."
  3190. :group 'org-archive
  3191. :group 'org-cycle
  3192. :type 'boolean)
  3193. (defcustom org-sparse-tree-open-archived-trees nil
  3194. "Non-nil means sparse tree construction shows matches in archived trees.
  3195. When nil, matches in these trees are highlighted, but the trees are kept in
  3196. collapsed state."
  3197. :group 'org-archive
  3198. :group 'org-sparse-trees
  3199. :type 'boolean)
  3200. (defun org-cycle-hide-archived-subtrees (state)
  3201. "Re-hide all archived subtrees after a visibility state change."
  3202. (when (and (not org-cycle-open-archived-trees)
  3203. (not (memq state '(overview folded))))
  3204. (save-excursion
  3205. (let* ((globalp (memq state '(contents all)))
  3206. (beg (if globalp (point-min) (point)))
  3207. (end (if globalp (point-max) (org-end-of-subtree t))))
  3208. (org-hide-archived-subtrees beg end)
  3209. (goto-char beg)
  3210. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3211. (message "%s" (substitute-command-keys
  3212. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3213. (defun org-force-cycle-archived ()
  3214. "Cycle subtree even if it is archived."
  3215. (interactive)
  3216. (setq this-command 'org-cycle)
  3217. (let ((org-cycle-open-archived-trees t))
  3218. (call-interactively 'org-cycle)))
  3219. (defun org-hide-archived-subtrees (beg end)
  3220. "Re-hide all archived subtrees after a visibility state change."
  3221. (save-excursion
  3222. (let* ((re (concat ":" org-archive-tag ":")))
  3223. (goto-char beg)
  3224. (while (re-search-forward re end t)
  3225. (when (org-on-heading-p)
  3226. (org-flag-subtree t)
  3227. (org-end-of-subtree t))))))
  3228. (defun org-flag-subtree (flag)
  3229. (save-excursion
  3230. (org-back-to-heading t)
  3231. (outline-end-of-heading)
  3232. (outline-flag-region (point)
  3233. (progn (org-end-of-subtree t) (point))
  3234. flag)))
  3235. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3236. (eval-and-compile
  3237. (org-autoload "org-archive"
  3238. '(org-add-archive-files org-archive-subtree
  3239. org-archive-to-archive-sibling org-toggle-archive-tag
  3240. org-archive-subtree-default
  3241. org-archive-subtree-default-with-confirmation)))
  3242. ;; Autoload Column View Code
  3243. (declare-function org-columns-number-to-string "org-colview")
  3244. (declare-function org-columns-get-format-and-top-level "org-colview")
  3245. (declare-function org-columns-compute "org-colview")
  3246. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3247. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3248. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3249. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3250. ;; Autoload ID code
  3251. (declare-function org-id-store-link "org-id")
  3252. (declare-function org-id-locations-load "org-id")
  3253. (declare-function org-id-locations-save "org-id")
  3254. (defvar org-id-track-globally)
  3255. (org-autoload "org-id"
  3256. '(org-id-get-create org-id-new org-id-copy org-id-get
  3257. org-id-get-with-outline-path-completion
  3258. org-id-get-with-outline-drilling
  3259. org-id-goto org-id-find org-id-store-link))
  3260. ;; Autoload Plotting Code
  3261. (org-autoload "org-plot"
  3262. '(org-plot/gnuplot))
  3263. ;;; Variables for pre-computed regular expressions, all buffer local
  3264. (defvar org-drawer-regexp nil
  3265. "Matches first line of a hidden block.")
  3266. (make-variable-buffer-local 'org-drawer-regexp)
  3267. (defvar org-todo-regexp nil
  3268. "Matches any of the TODO state keywords.")
  3269. (make-variable-buffer-local 'org-todo-regexp)
  3270. (defvar org-not-done-regexp nil
  3271. "Matches any of the TODO state keywords except the last one.")
  3272. (make-variable-buffer-local 'org-not-done-regexp)
  3273. (defvar org-not-done-heading-regexp nil
  3274. "Matches a TODO headline that is not done.")
  3275. (make-variable-buffer-local 'org-not-done-regexp)
  3276. (defvar org-todo-line-regexp nil
  3277. "Matches a headline and puts TODO state into group 2 if present.")
  3278. (make-variable-buffer-local 'org-todo-line-regexp)
  3279. (defvar org-complex-heading-regexp nil
  3280. "Matches a headline and puts everything into groups:
  3281. group 1: the stars
  3282. group 2: The todo keyword, maybe
  3283. group 3: Priority cookie
  3284. group 4: True headline
  3285. group 5: Tags")
  3286. (make-variable-buffer-local 'org-complex-heading-regexp)
  3287. (defvar org-complex-heading-regexp-format nil)
  3288. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3289. (defvar org-todo-line-tags-regexp nil
  3290. "Matches a headline and puts TODO state into group 2 if present.
  3291. Also put tags into group 4 if tags are present.")
  3292. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3293. (defvar org-nl-done-regexp nil
  3294. "Matches newline followed by a headline with the DONE keyword.")
  3295. (make-variable-buffer-local 'org-nl-done-regexp)
  3296. (defvar org-looking-at-done-regexp nil
  3297. "Matches the DONE keyword a point.")
  3298. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3299. (defvar org-ds-keyword-length 12
  3300. "Maximum length of the Deadline and SCHEDULED keywords.")
  3301. (make-variable-buffer-local 'org-ds-keyword-length)
  3302. (defvar org-deadline-regexp nil
  3303. "Matches the DEADLINE keyword.")
  3304. (make-variable-buffer-local 'org-deadline-regexp)
  3305. (defvar org-deadline-time-regexp nil
  3306. "Matches the DEADLINE keyword together with a time stamp.")
  3307. (make-variable-buffer-local 'org-deadline-time-regexp)
  3308. (defvar org-deadline-line-regexp nil
  3309. "Matches the DEADLINE keyword and the rest of the line.")
  3310. (make-variable-buffer-local 'org-deadline-line-regexp)
  3311. (defvar org-scheduled-regexp nil
  3312. "Matches the SCHEDULED keyword.")
  3313. (make-variable-buffer-local 'org-scheduled-regexp)
  3314. (defvar org-scheduled-time-regexp nil
  3315. "Matches the SCHEDULED keyword together with a time stamp.")
  3316. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3317. (defvar org-closed-time-regexp nil
  3318. "Matches the CLOSED keyword together with a time stamp.")
  3319. (make-variable-buffer-local 'org-closed-time-regexp)
  3320. (defvar org-keyword-time-regexp nil
  3321. "Matches any of the 4 keywords, together with the time stamp.")
  3322. (make-variable-buffer-local 'org-keyword-time-regexp)
  3323. (defvar org-keyword-time-not-clock-regexp nil
  3324. "Matches any of the 3 keywords, together with the time stamp.")
  3325. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3326. (defvar org-maybe-keyword-time-regexp nil
  3327. "Matches a timestamp, possibly preceeded by a keyword.")
  3328. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3329. (defvar org-planning-or-clock-line-re nil
  3330. "Matches a line with planning or clock info.")
  3331. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3332. (defvar org-all-time-keywords nil
  3333. "List of time keywords.")
  3334. (make-variable-buffer-local 'org-all-time-keywords)
  3335. (defconst org-plain-time-of-day-regexp
  3336. (concat
  3337. "\\(\\<[012]?[0-9]"
  3338. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3339. "\\(--?"
  3340. "\\(\\<[012]?[0-9]"
  3341. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3342. "\\)?")
  3343. "Regular expression to match a plain time or time range.
  3344. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3345. groups carry important information:
  3346. 0 the full match
  3347. 1 the first time, range or not
  3348. 8 the second time, if it is a range.")
  3349. (defconst org-plain-time-extension-regexp
  3350. (concat
  3351. "\\(\\<[012]?[0-9]"
  3352. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3353. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3354. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3355. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3356. groups carry important information:
  3357. 0 the full match
  3358. 7 hours of duration
  3359. 9 minutes of duration")
  3360. (defconst org-stamp-time-of-day-regexp
  3361. (concat
  3362. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3363. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3364. "\\(--?"
  3365. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3366. "Regular expression to match a timestamp time or time range.
  3367. After a match, the following groups carry important information:
  3368. 0 the full match
  3369. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3370. 2 the first time, range or not
  3371. 4 the second time, if it is a range.")
  3372. (defconst org-startup-options
  3373. '(("fold" org-startup-folded t)
  3374. ("overview" org-startup-folded t)
  3375. ("nofold" org-startup-folded nil)
  3376. ("showall" org-startup-folded nil)
  3377. ("showeverything" org-startup-folded showeverything)
  3378. ("content" org-startup-folded content)
  3379. ("indent" org-startup-indented t)
  3380. ("noindent" org-startup-indented nil)
  3381. ("hidestars" org-hide-leading-stars t)
  3382. ("showstars" org-hide-leading-stars nil)
  3383. ("odd" org-odd-levels-only t)
  3384. ("oddeven" org-odd-levels-only nil)
  3385. ("align" org-startup-align-all-tables t)
  3386. ("noalign" org-startup-align-all-tables nil)
  3387. ("customtime" org-display-custom-times t)
  3388. ("logdone" org-log-done time)
  3389. ("lognotedone" org-log-done note)
  3390. ("nologdone" org-log-done nil)
  3391. ("lognoteclock-out" org-log-note-clock-out t)
  3392. ("nolognoteclock-out" org-log-note-clock-out nil)
  3393. ("logrepeat" org-log-repeat state)
  3394. ("lognoterepeat" org-log-repeat note)
  3395. ("nologrepeat" org-log-repeat nil)
  3396. ("logreschedule" org-log-reschedule time)
  3397. ("lognotereschedule" org-log-reschedule note)
  3398. ("nologreschedule" org-log-reschedule nil)
  3399. ("logredeadline" org-log-redeadline time)
  3400. ("lognoteredeadline" org-log-redeadline note)
  3401. ("nologredeadline" org-log-redeadline nil)
  3402. ("logrefile" org-log-refile time)
  3403. ("lognoterefile" org-log-refile note)
  3404. ("nologrefile" org-log-refile nil)
  3405. ("fninline" org-footnote-define-inline t)
  3406. ("nofninline" org-footnote-define-inline nil)
  3407. ("fnlocal" org-footnote-section nil)
  3408. ("fnauto" org-footnote-auto-label t)
  3409. ("fnprompt" org-footnote-auto-label nil)
  3410. ("fnconfirm" org-footnote-auto-label confirm)
  3411. ("fnplain" org-footnote-auto-label plain)
  3412. ("fnadjust" org-footnote-auto-adjust t)
  3413. ("nofnadjust" org-footnote-auto-adjust nil)
  3414. ("constcgs" constants-unit-system cgs)
  3415. ("constSI" constants-unit-system SI)
  3416. ("noptag" org-tag-persistent-alist nil)
  3417. ("hideblocks" org-hide-block-startup t)
  3418. ("nohideblocks" org-hide-block-startup nil)
  3419. ("beamer" org-startup-with-beamer-mode t))
  3420. "Variable associated with STARTUP options for org-mode.
  3421. Each element is a list of three items: The startup options as written
  3422. in the #+STARTUP line, the corresponding variable, and the value to
  3423. set this variable to if the option is found. An optional forth element PUSH
  3424. means to push this value onto the list in the variable.")
  3425. (defun org-set-regexps-and-options ()
  3426. "Precompute regular expressions for current buffer."
  3427. (when (org-mode-p)
  3428. (org-set-local 'org-todo-kwd-alist nil)
  3429. (org-set-local 'org-todo-key-alist nil)
  3430. (org-set-local 'org-todo-key-trigger nil)
  3431. (org-set-local 'org-todo-keywords-1 nil)
  3432. (org-set-local 'org-done-keywords nil)
  3433. (org-set-local 'org-todo-heads nil)
  3434. (org-set-local 'org-todo-sets nil)
  3435. (org-set-local 'org-todo-log-states nil)
  3436. (org-set-local 'org-file-properties nil)
  3437. (org-set-local 'org-file-tags nil)
  3438. (let ((re (org-make-options-regexp
  3439. '("CATEGORY" "TODO" "COLUMNS"
  3440. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3441. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS")
  3442. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3443. (splitre "[ \t]+")
  3444. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3445. tail sep kws1 prio props ftags drawers beamer-p
  3446. ext-setup-or-nil setup-contents (start 0))
  3447. (save-excursion
  3448. (save-restriction
  3449. (widen)
  3450. (goto-char (point-min))
  3451. (while (or (and ext-setup-or-nil
  3452. (string-match re ext-setup-or-nil start)
  3453. (setq start (match-end 0)))
  3454. (and (setq ext-setup-or-nil nil start 0)
  3455. (re-search-forward re nil t)))
  3456. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3457. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3458. (cond
  3459. ((equal key "CATEGORY")
  3460. (if (string-match "[ \t]+$" value)
  3461. (setq value (replace-match "" t t value)))
  3462. (setq cat value))
  3463. ((member key '("SEQ_TODO" "TODO"))
  3464. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3465. ((equal key "TYP_TODO")
  3466. (push (cons 'type (org-split-string value splitre)) kwds))
  3467. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3468. ;; general TODO-like setup
  3469. (push (cons (intern (downcase (match-string 1 key)))
  3470. (org-split-string value splitre)) kwds))
  3471. ((equal key "TAGS")
  3472. (setq tags (append tags (if tags '("\\n") nil)
  3473. (org-split-string value splitre))))
  3474. ((equal key "COLUMNS")
  3475. (org-set-local 'org-columns-default-format value))
  3476. ((equal key "LINK")
  3477. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3478. (push (cons (match-string 1 value)
  3479. (org-trim (match-string 2 value)))
  3480. links)))
  3481. ((equal key "PRIORITIES")
  3482. (setq prio (org-split-string value " +")))
  3483. ((equal key "PROPERTY")
  3484. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3485. (push (cons (match-string 1 value) (match-string 2 value))
  3486. props)))
  3487. ((equal key "FILETAGS")
  3488. (when (string-match "\\S-" value)
  3489. (setq ftags
  3490. (append
  3491. ftags
  3492. (apply 'append
  3493. (mapcar (lambda (x) (org-split-string x ":"))
  3494. (org-split-string value)))))))
  3495. ((equal key "DRAWERS")
  3496. (setq drawers (org-split-string value splitre)))
  3497. ((equal key "CONSTANTS")
  3498. (setq const (append const (org-split-string value splitre))))
  3499. ((equal key "STARTUP")
  3500. (let ((opts (org-split-string value splitre))
  3501. l var val)
  3502. (while (setq l (pop opts))
  3503. (when (setq l (assoc l org-startup-options))
  3504. (setq var (nth 1 l) val (nth 2 l))
  3505. (if (not (nth 3 l))
  3506. (set (make-local-variable var) val)
  3507. (if (not (listp (symbol-value var)))
  3508. (set (make-local-variable var) nil))
  3509. (set (make-local-variable var) (symbol-value var))
  3510. (add-to-list var val))))))
  3511. ((equal key "ARCHIVE")
  3512. (string-match " *$" value)
  3513. (setq arch (replace-match "" t t value))
  3514. (remove-text-properties 0 (length arch)
  3515. '(face t fontified t) arch))
  3516. ((equal key "LATEX_CLASS")
  3517. (setq beamer-p (equal value "beamer")))
  3518. ((equal key "SETUPFILE")
  3519. (setq setup-contents (org-file-contents
  3520. (expand-file-name
  3521. (org-remove-double-quotes value))
  3522. 'noerror))
  3523. (if (not ext-setup-or-nil)
  3524. (setq ext-setup-or-nil setup-contents start 0)
  3525. (setq ext-setup-or-nil
  3526. (concat (substring ext-setup-or-nil 0 start)
  3527. "\n" setup-contents "\n"
  3528. (substring ext-setup-or-nil start)))))
  3529. ))))
  3530. (when cat
  3531. (org-set-local 'org-category (intern cat))
  3532. (push (cons "CATEGORY" cat) props))
  3533. (when prio
  3534. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3535. (setq prio (mapcar 'string-to-char prio))
  3536. (org-set-local 'org-highest-priority (nth 0 prio))
  3537. (org-set-local 'org-lowest-priority (nth 1 prio))
  3538. (org-set-local 'org-default-priority (nth 2 prio)))
  3539. (and props (org-set-local 'org-file-properties (nreverse props)))
  3540. (and ftags (org-set-local 'org-file-tags
  3541. (mapcar 'org-add-prop-inherited ftags)))
  3542. (and drawers (org-set-local 'org-drawers drawers))
  3543. (and arch (org-set-local 'org-archive-location arch))
  3544. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3545. ;; Process the TODO keywords
  3546. (unless kwds
  3547. ;; Use the global values as if they had been given locally.
  3548. (setq kwds (default-value 'org-todo-keywords))
  3549. (if (stringp (car kwds))
  3550. (setq kwds (list (cons org-todo-interpretation
  3551. (default-value 'org-todo-keywords)))))
  3552. (setq kwds (reverse kwds)))
  3553. (setq kwds (nreverse kwds))
  3554. (let (inter kws kw)
  3555. (while (setq kws (pop kwds))
  3556. (let ((kws (or
  3557. (run-hook-with-args-until-success
  3558. 'org-todo-setup-filter-hook kws)
  3559. kws)))
  3560. (setq inter (pop kws) sep (member "|" kws)
  3561. kws0 (delete "|" (copy-sequence kws))
  3562. kwsa nil
  3563. kws1 (mapcar
  3564. (lambda (x)
  3565. ;; 1 2
  3566. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3567. (progn
  3568. (setq kw (match-string 1 x)
  3569. key (and (match-end 2) (match-string 2 x))
  3570. log (org-extract-log-state-settings x))
  3571. (push (cons kw (and key (string-to-char key))) kwsa)
  3572. (and log (push log org-todo-log-states))
  3573. kw)
  3574. (error "Invalid TODO keyword %s" x)))
  3575. kws0)
  3576. kwsa (if kwsa (append '((:startgroup))
  3577. (nreverse kwsa)
  3578. '((:endgroup))))
  3579. hw (car kws1)
  3580. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3581. tail (list inter hw (car dws) (org-last dws))))
  3582. (add-to-list 'org-todo-heads hw 'append)
  3583. (push kws1 org-todo-sets)
  3584. (setq org-done-keywords (append org-done-keywords dws nil))
  3585. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3586. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3587. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3588. (setq org-todo-sets (nreverse org-todo-sets)
  3589. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3590. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3591. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3592. ;; Process the constants
  3593. (when const
  3594. (let (e cst)
  3595. (while (setq e (pop const))
  3596. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3597. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3598. (setq org-table-formula-constants-local cst)))
  3599. ;; Process the tags.
  3600. (when tags
  3601. (let (e tgs)
  3602. (while (setq e (pop tags))
  3603. (cond
  3604. ((equal e "{") (push '(:startgroup) tgs))
  3605. ((equal e "}") (push '(:endgroup) tgs))
  3606. ((equal e "\\n") (push '(:newline) tgs))
  3607. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3608. (push (cons (match-string 1 e)
  3609. (string-to-char (match-string 2 e)))
  3610. tgs))
  3611. (t (push (list e) tgs))))
  3612. (org-set-local 'org-tag-alist nil)
  3613. (while (setq e (pop tgs))
  3614. (or (and (stringp (car e))
  3615. (assoc (car e) org-tag-alist))
  3616. (push e org-tag-alist)))))
  3617. ;; Compute the regular expressions and other local variables
  3618. (if (not org-done-keywords)
  3619. (setq org-done-keywords (and org-todo-keywords-1
  3620. (list (org-last org-todo-keywords-1)))))
  3621. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3622. (length org-scheduled-string)
  3623. (length org-clock-string)
  3624. (length org-closed-string)))
  3625. org-drawer-regexp
  3626. (concat "^[ \t]*:\\("
  3627. (mapconcat 'regexp-quote org-drawers "\\|")
  3628. "\\):[ \t]*$")
  3629. org-not-done-keywords
  3630. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3631. org-todo-regexp
  3632. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3633. "\\|") "\\)\\>")
  3634. org-not-done-regexp
  3635. (concat "\\<\\("
  3636. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3637. "\\)\\>")
  3638. org-not-done-heading-regexp
  3639. (concat "^\\(\\*+\\)[ \t]+\\("
  3640. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3641. "\\)\\>")
  3642. org-todo-line-regexp
  3643. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3644. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3645. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3646. org-complex-heading-regexp
  3647. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3648. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3649. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3650. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3651. org-complex-heading-regexp-format
  3652. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3653. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3654. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3655. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3656. org-nl-done-regexp
  3657. (concat "\n\\*+[ \t]+"
  3658. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3659. "\\)" "\\>")
  3660. org-todo-line-tags-regexp
  3661. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3662. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3663. (org-re
  3664. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3665. org-looking-at-done-regexp
  3666. (concat "^" "\\(?:"
  3667. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3668. "\\>")
  3669. org-deadline-regexp (concat "\\<" org-deadline-string)
  3670. org-deadline-time-regexp
  3671. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3672. org-deadline-line-regexp
  3673. (concat "\\<\\(" org-deadline-string "\\).*")
  3674. org-scheduled-regexp
  3675. (concat "\\<" org-scheduled-string)
  3676. org-scheduled-time-regexp
  3677. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3678. org-closed-time-regexp
  3679. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3680. org-keyword-time-regexp
  3681. (concat "\\<\\(" org-scheduled-string
  3682. "\\|" org-deadline-string
  3683. "\\|" org-closed-string
  3684. "\\|" org-clock-string "\\)"
  3685. " *[[<]\\([^]>]+\\)[]>]")
  3686. org-keyword-time-not-clock-regexp
  3687. (concat "\\<\\(" org-scheduled-string
  3688. "\\|" org-deadline-string
  3689. "\\|" org-closed-string
  3690. "\\)"
  3691. " *[[<]\\([^]>]+\\)[]>]")
  3692. org-maybe-keyword-time-regexp
  3693. (concat "\\(\\<\\(" org-scheduled-string
  3694. "\\|" org-deadline-string
  3695. "\\|" org-closed-string
  3696. "\\|" org-clock-string "\\)\\)?"
  3697. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3698. org-planning-or-clock-line-re
  3699. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3700. "\\|" org-deadline-string
  3701. "\\|" org-closed-string "\\|" org-clock-string
  3702. "\\)\\>\\)")
  3703. org-all-time-keywords
  3704. (mapcar (lambda (w) (substring w 0 -1))
  3705. (list org-scheduled-string org-deadline-string
  3706. org-clock-string org-closed-string))
  3707. )
  3708. (org-compute-latex-and-specials-regexp)
  3709. (org-set-font-lock-defaults))))
  3710. (defun org-file-contents (file &optional noerror)
  3711. "Return the contents of FILE, as a string."
  3712. (if (or (not file)
  3713. (not (file-readable-p file)))
  3714. (if noerror
  3715. (progn
  3716. (message "Cannot read file %s" file)
  3717. (ding) (sit-for 2)
  3718. "")
  3719. (error "Cannot read file %s" file))
  3720. (with-temp-buffer
  3721. (insert-file-contents file)
  3722. (buffer-string))))
  3723. (defun org-extract-log-state-settings (x)
  3724. "Extract the log state setting from a TODO keyword string.
  3725. This will extract info from a string like \"WAIT(w@/!)\"."
  3726. (let (kw key log1 log2)
  3727. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3728. (setq kw (match-string 1 x)
  3729. key (and (match-end 2) (match-string 2 x))
  3730. log1 (and (match-end 3) (match-string 3 x))
  3731. log2 (and (match-end 4) (match-string 4 x)))
  3732. (and (or log1 log2)
  3733. (list kw
  3734. (and log1 (if (equal log1 "!") 'time 'note))
  3735. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3736. (defun org-remove-keyword-keys (list)
  3737. "Remove a pair of parenthesis at the end of each string in LIST."
  3738. (mapcar (lambda (x)
  3739. (if (string-match "(.*)$" x)
  3740. (substring x 0 (match-beginning 0))
  3741. x))
  3742. list))
  3743. (defun org-assign-fast-keys (alist)
  3744. "Assign fast keys to a keyword-key alist.
  3745. Respect keys that are already there."
  3746. (let (new e (alt ?0))
  3747. (while (setq e (pop alist))
  3748. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  3749. (cdr e)) ;; Key already assigned.
  3750. (push e new)
  3751. (let ((clist (string-to-list (downcase (car e))))
  3752. (used (append new alist)))
  3753. (when (= (car clist) ?@)
  3754. (pop clist))
  3755. (while (and clist (rassoc (car clist) used))
  3756. (pop clist))
  3757. (unless clist
  3758. (while (rassoc alt used)
  3759. (incf alt)))
  3760. (push (cons (car e) (or (car clist) alt)) new))))
  3761. (nreverse new)))
  3762. ;;; Some variables used in various places
  3763. (defvar org-window-configuration nil
  3764. "Used in various places to store a window configuration.")
  3765. (defvar org-selected-window nil
  3766. "Used in various places to store a window configuration.")
  3767. (defvar org-finish-function nil
  3768. "Function to be called when `C-c C-c' is used.
  3769. This is for getting out of special buffers like remember.")
  3770. ;; FIXME: Occasionally check by commenting these, to make sure
  3771. ;; no other functions uses these, forgetting to let-bind them.
  3772. (defvar entry)
  3773. (defvar last-state)
  3774. (defvar date)
  3775. ;; Defined somewhere in this file, but used before definition.
  3776. (defvar org-entities) ;; defined in org-entities.el
  3777. (defvar org-struct-menu)
  3778. (defvar org-org-menu)
  3779. (defvar org-tbl-menu)
  3780. ;;;; Define the Org-mode
  3781. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3782. (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."))
  3783. ;; We use a before-change function to check if a table might need
  3784. ;; an update.
  3785. (defvar org-table-may-need-update t
  3786. "Indicates that a table might need an update.
  3787. This variable is set by `org-before-change-function'.
  3788. `org-table-align' sets it back to nil.")
  3789. (defun org-before-change-function (beg end)
  3790. "Every change indicates that a table might need an update."
  3791. (setq org-table-may-need-update t))
  3792. (defvar org-mode-map)
  3793. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3794. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  3795. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3796. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3797. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3798. (defvar org-table-buffer-is-an nil)
  3799. (defconst org-outline-regexp "\\*+ ")
  3800. ;;;###autoload
  3801. (define-derived-mode org-mode outline-mode "Org"
  3802. "Outline-based notes management and organizer, alias
  3803. \"Carsten's outline-mode for keeping track of everything.\"
  3804. Org-mode develops organizational tasks around a NOTES file which
  3805. contains information about projects as plain text. Org-mode is
  3806. implemented on top of outline-mode, which is ideal to keep the content
  3807. of large files well structured. It supports ToDo items, deadlines and
  3808. time stamps, which magically appear in the diary listing of the Emacs
  3809. calendar. Tables are easily created with a built-in table editor.
  3810. Plain text URL-like links connect to websites, emails (VM), Usenet
  3811. messages (Gnus), BBDB entries, and any files related to the project.
  3812. For printing and sharing of notes, an Org-mode file (or a part of it)
  3813. can be exported as a structured ASCII or HTML file.
  3814. The following commands are available:
  3815. \\{org-mode-map}"
  3816. ;; Get rid of Outline menus, they are not needed
  3817. ;; Need to do this here because define-derived-mode sets up
  3818. ;; the keymap so late. Still, it is a waste to call this each time
  3819. ;; we switch another buffer into org-mode.
  3820. (if (featurep 'xemacs)
  3821. (when (boundp 'outline-mode-menu-heading)
  3822. ;; Assume this is Greg's port, it used easymenu
  3823. (easy-menu-remove outline-mode-menu-heading)
  3824. (easy-menu-remove outline-mode-menu-show)
  3825. (easy-menu-remove outline-mode-menu-hide))
  3826. (define-key org-mode-map [menu-bar headings] 'undefined)
  3827. (define-key org-mode-map [menu-bar hide] 'undefined)
  3828. (define-key org-mode-map [menu-bar show] 'undefined))
  3829. (org-load-modules-maybe)
  3830. (easy-menu-add org-org-menu)
  3831. (easy-menu-add org-tbl-menu)
  3832. (org-install-agenda-files-menu)
  3833. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3834. (org-add-to-invisibility-spec '(org-cwidth))
  3835. (org-add-to-invisibility-spec '(org-hide-block . t))
  3836. (when (featurep 'xemacs)
  3837. (org-set-local 'line-move-ignore-invisible t))
  3838. (org-set-local 'outline-regexp org-outline-regexp)
  3839. (org-set-local 'outline-level 'org-outline-level)
  3840. (when (and org-ellipsis
  3841. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3842. (fboundp 'make-glyph-code))
  3843. (unless org-display-table
  3844. (setq org-display-table (make-display-table)))
  3845. (set-display-table-slot
  3846. org-display-table 4
  3847. (vconcat (mapcar
  3848. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3849. org-ellipsis)))
  3850. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3851. (setq buffer-display-table org-display-table))
  3852. (org-set-regexps-and-options)
  3853. (when (and org-tag-faces (not org-tags-special-faces-re))
  3854. ;; tag faces set outside customize.... force initialization.
  3855. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3856. ;; Calc embedded
  3857. (org-set-local 'calc-embedded-open-mode "# ")
  3858. (modify-syntax-entry ?# "<")
  3859. (modify-syntax-entry ?@ "w")
  3860. (if org-startup-truncated (setq truncate-lines t))
  3861. (org-set-local 'font-lock-unfontify-region-function
  3862. 'org-unfontify-region)
  3863. ;; Activate before-change-function
  3864. (org-set-local 'org-table-may-need-update t)
  3865. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3866. 'local)
  3867. ;; Check for running clock before killing a buffer
  3868. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3869. ;; Paragraphs and auto-filling
  3870. (org-set-autofill-regexps)
  3871. (setq indent-line-function 'org-indent-line-function)
  3872. (org-update-radio-target-regexp)
  3873. ;; Make sure dependence stuff works reliably, even for users who set it
  3874. ;; too late :-(
  3875. (if org-enforce-todo-dependencies
  3876. (add-hook 'org-blocker-hook
  3877. 'org-block-todo-from-children-or-siblings-or-parent)
  3878. (remove-hook 'org-blocker-hook
  3879. 'org-block-todo-from-children-or-siblings-or-parent))
  3880. (if org-enforce-todo-checkbox-dependencies
  3881. (add-hook 'org-blocker-hook
  3882. 'org-block-todo-from-checkboxes)
  3883. (remove-hook 'org-blocker-hook
  3884. 'org-block-todo-from-checkboxes))
  3885. ;; Comment characters
  3886. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3887. (org-set-local 'comment-padding " ")
  3888. ;; Align options lines
  3889. (org-set-local
  3890. 'align-mode-rules-list
  3891. '((org-in-buffer-settings
  3892. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3893. (modes . '(org-mode)))))
  3894. ;; Imenu
  3895. (org-set-local 'imenu-create-index-function
  3896. 'org-imenu-get-tree)
  3897. ;; Make isearch reveal context
  3898. (if (or (featurep 'xemacs)
  3899. (not (boundp 'outline-isearch-open-invisible-function)))
  3900. ;; Emacs 21 and XEmacs make use of the hook
  3901. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3902. ;; Emacs 22 deals with this through a special variable
  3903. (org-set-local 'outline-isearch-open-invisible-function
  3904. (lambda (&rest ignore) (org-show-context 'isearch))))
  3905. ;; Turn on org-beamer-mode?
  3906. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  3907. ;; If empty file that did not turn on org-mode automatically, make it to.
  3908. (if (and org-insert-mode-line-in-empty-file
  3909. (interactive-p)
  3910. (= (point-min) (point-max)))
  3911. (insert "# -*- mode: org -*-\n\n"))
  3912. (unless org-inhibit-startup
  3913. (when org-startup-align-all-tables
  3914. (let ((bmp (buffer-modified-p)))
  3915. (org-table-map-tables 'org-table-align)
  3916. (set-buffer-modified-p bmp)))
  3917. (when org-startup-indented
  3918. (require 'org-indent)
  3919. (org-indent-mode 1))
  3920. (unless org-inhibit-startup-visibility-stuff
  3921. (org-set-startup-visibility))))
  3922. (when (fboundp 'abbrev-table-put)
  3923. (abbrev-table-put org-mode-abbrev-table
  3924. :parents (list text-mode-abbrev-table)))
  3925. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3926. (defun org-current-time ()
  3927. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3928. (if (> (car org-time-stamp-rounding-minutes) 1)
  3929. (let ((r (car org-time-stamp-rounding-minutes))
  3930. (time (decode-time)))
  3931. (apply 'encode-time
  3932. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3933. (nthcdr 2 time))))
  3934. (current-time)))
  3935. ;;;; Font-Lock stuff, including the activators
  3936. (defvar org-mouse-map (make-sparse-keymap))
  3937. (org-defkey org-mouse-map
  3938. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3939. (org-defkey org-mouse-map
  3940. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3941. (when org-mouse-1-follows-link
  3942. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3943. (when org-tab-follows-link
  3944. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3945. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3946. (require 'font-lock)
  3947. (defconst org-non-link-chars "]\t\n\r<>")
  3948. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3949. "shell" "elisp"))
  3950. (defvar org-link-types-re nil
  3951. "Matches a link that has a url-like prefix like \"http:\"")
  3952. (defvar org-link-re-with-space nil
  3953. "Matches a link with spaces, optional angular brackets around it.")
  3954. (defvar org-link-re-with-space2 nil
  3955. "Matches a link with spaces, optional angular brackets around it.")
  3956. (defvar org-link-re-with-space3 nil
  3957. "Matches a link with spaces, only for internal part in bracket links.")
  3958. (defvar org-angle-link-re nil
  3959. "Matches link with angular brackets, spaces are allowed.")
  3960. (defvar org-plain-link-re nil
  3961. "Matches plain link, without spaces.")
  3962. (defvar org-bracket-link-regexp nil
  3963. "Matches a link in double brackets.")
  3964. (defvar org-bracket-link-analytic-regexp nil
  3965. "Regular expression used to analyze links.
  3966. Here is what the match groups contain after a match:
  3967. 1: http:
  3968. 2: http
  3969. 3: path
  3970. 4: [desc]
  3971. 5: desc")
  3972. (defvar org-bracket-link-analytic-regexp++ nil
  3973. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3974. (defvar org-any-link-re nil
  3975. "Regular expression matching any link.")
  3976. (defun org-make-link-regexps ()
  3977. "Update the link regular expressions.
  3978. This should be called after the variable `org-link-types' has changed."
  3979. (setq org-link-types-re
  3980. (concat
  3981. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  3982. org-link-re-with-space
  3983. (concat
  3984. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3985. "\\([^" org-non-link-chars " ]"
  3986. "[^" org-non-link-chars "]*"
  3987. "[^" org-non-link-chars " ]\\)>?")
  3988. org-link-re-with-space2
  3989. (concat
  3990. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3991. "\\([^" org-non-link-chars " ]"
  3992. "[^\t\n\r]*"
  3993. "[^" org-non-link-chars " ]\\)>?")
  3994. org-link-re-with-space3
  3995. (concat
  3996. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3997. "\\([^" org-non-link-chars " ]"
  3998. "[^\t\n\r]*\\)")
  3999. org-angle-link-re
  4000. (concat
  4001. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4002. "\\([^" org-non-link-chars " ]"
  4003. "[^" org-non-link-chars "]*"
  4004. "\\)>")
  4005. org-plain-link-re
  4006. (concat
  4007. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4008. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4009. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4010. org-bracket-link-regexp
  4011. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4012. org-bracket-link-analytic-regexp
  4013. (concat
  4014. "\\[\\["
  4015. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4016. "\\([^]]+\\)"
  4017. "\\]"
  4018. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4019. "\\]")
  4020. org-bracket-link-analytic-regexp++
  4021. (concat
  4022. "\\[\\["
  4023. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4024. "\\([^]]+\\)"
  4025. "\\]"
  4026. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4027. "\\]")
  4028. org-any-link-re
  4029. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4030. org-angle-link-re "\\)\\|\\("
  4031. org-plain-link-re "\\)")))
  4032. (org-make-link-regexps)
  4033. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4034. "Regular expression for fast time stamp matching.")
  4035. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4036. "Regular expression for fast time stamp matching.")
  4037. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4038. "Regular expression matching time strings for analysis.
  4039. This one does not require the space after the date, so it can be used
  4040. on a string that terminates immediately after the date.")
  4041. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4042. "Regular expression matching time strings for analysis.")
  4043. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4044. "Regular expression matching time stamps, with groups.")
  4045. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4046. "Regular expression matching time stamps (also [..]), with groups.")
  4047. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4048. "Regular expression matching a time stamp range.")
  4049. (defconst org-tr-regexp-both
  4050. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4051. "Regular expression matching a time stamp range.")
  4052. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4053. org-ts-regexp "\\)?")
  4054. "Regular expression matching a time stamp or time stamp range.")
  4055. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4056. org-ts-regexp-both "\\)?")
  4057. "Regular expression matching a time stamp or time stamp range.
  4058. The time stamps may be either active or inactive.")
  4059. (defvar org-emph-face nil)
  4060. (defun org-do-emphasis-faces (limit)
  4061. "Run through the buffer and add overlays to links."
  4062. (let (rtn a)
  4063. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4064. (if (not (= (char-after (match-beginning 3))
  4065. (char-after (match-beginning 4))))
  4066. (progn
  4067. (setq rtn t)
  4068. (setq a (assoc (match-string 3) org-emphasis-alist))
  4069. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4070. 'face
  4071. (nth 1 a))
  4072. (and (nth 4 a)
  4073. (org-remove-flyspell-overlays-in
  4074. (match-beginning 0) (match-end 0)))
  4075. (add-text-properties (match-beginning 2) (match-end 2)
  4076. '(font-lock-multiline t))
  4077. (when org-hide-emphasis-markers
  4078. (add-text-properties (match-end 4) (match-beginning 5)
  4079. '(invisible org-link))
  4080. (add-text-properties (match-beginning 3) (match-end 3)
  4081. '(invisible org-link)))))
  4082. (backward-char 1))
  4083. rtn))
  4084. (defun org-emphasize (&optional char)
  4085. "Insert or change an emphasis, i.e. a font like bold or italic.
  4086. If there is an active region, change that region to a new emphasis.
  4087. If there is no region, just insert the marker characters and position
  4088. the cursor between them.
  4089. CHAR should be either the marker character, or the first character of the
  4090. HTML tag associated with that emphasis. If CHAR is a space, the means
  4091. to remove the emphasis of the selected region.
  4092. If char is not given (for example in an interactive call) it
  4093. will be prompted for."
  4094. (interactive)
  4095. (let ((eal org-emphasis-alist) e det
  4096. (erc org-emphasis-regexp-components)
  4097. (prompt "")
  4098. (string "") beg end move tag c s)
  4099. (if (org-region-active-p)
  4100. (setq beg (region-beginning) end (region-end)
  4101. string (buffer-substring beg end))
  4102. (setq move t))
  4103. (while (setq e (pop eal))
  4104. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4105. c (aref tag 0))
  4106. (push (cons c (string-to-char (car e))) det)
  4107. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4108. (substring tag 1)))))
  4109. (setq det (nreverse det))
  4110. (unless char
  4111. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4112. (setq char (read-char-exclusive)))
  4113. (setq char (or (cdr (assoc char det)) char))
  4114. (if (equal char ?\ )
  4115. (setq s "" move nil)
  4116. (unless (assoc (char-to-string char) org-emphasis-alist)
  4117. (error "No such emphasis marker: \"%c\"" char))
  4118. (setq s (char-to-string char)))
  4119. (while (and (> (length string) 1)
  4120. (equal (substring string 0 1) (substring string -1))
  4121. (assoc (substring string 0 1) org-emphasis-alist))
  4122. (setq string (substring string 1 -1)))
  4123. (setq string (concat s string s))
  4124. (if beg (delete-region beg end))
  4125. (unless (or (bolp)
  4126. (string-match (concat "[" (nth 0 erc) "\n]")
  4127. (char-to-string (char-before (point)))))
  4128. (insert " "))
  4129. (unless (or (eobp)
  4130. (string-match (concat "[" (nth 1 erc) "\n]")
  4131. (char-to-string (char-after (point)))))
  4132. (insert " ") (backward-char 1))
  4133. (insert string)
  4134. (and move (backward-char 1))))
  4135. (defconst org-nonsticky-props
  4136. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4137. (defsubst org-rear-nonsticky-at (pos)
  4138. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4139. (defun org-activate-plain-links (limit)
  4140. "Run through the buffer and add overlays to links."
  4141. (catch 'exit
  4142. (let (f)
  4143. (if (re-search-forward org-plain-link-re limit t)
  4144. (progn
  4145. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4146. (setq f (get-text-property (match-beginning 0) 'face))
  4147. (if (or (eq f 'org-tag)
  4148. (and (listp f) (memq 'org-tag f)))
  4149. nil
  4150. (add-text-properties (match-beginning 0) (match-end 0)
  4151. (list 'mouse-face 'highlight
  4152. 'face 'org-link
  4153. 'keymap org-mouse-map))
  4154. (org-rear-nonsticky-at (match-end 0)))
  4155. t)))))
  4156. (defun org-activate-code (limit)
  4157. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4158. (progn
  4159. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4160. (remove-text-properties (match-beginning 0) (match-end 0)
  4161. '(display t invisible t intangible t))
  4162. t)))
  4163. (defun org-fontify-meta-lines-and-blocks (limit)
  4164. "Fontify #+ lines and blocks, in the correct ways."
  4165. (let ((case-fold-search t))
  4166. (if (re-search-forward
  4167. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4168. limit t)
  4169. (let ((beg (match-beginning 0))
  4170. (beg1 (line-beginning-position 2))
  4171. (dc1 (downcase (match-string 2)))
  4172. (dc3 (downcase (match-string 3)))
  4173. end end1 quoting block-type)
  4174. (cond
  4175. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4176. ;; a single line of backend-specific content
  4177. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4178. (remove-text-properties (match-beginning 0) (match-end 0)
  4179. '(display t invisible t intangible t))
  4180. (add-text-properties (match-beginning 1) (match-end 3)
  4181. '(font-lock-fontified t face org-meta-line))
  4182. (add-text-properties (match-beginning 6) (match-end 6)
  4183. '(font-lock-fontified t face org-block))
  4184. t)
  4185. ((and (match-end 4) (equal dc3 "begin"))
  4186. ;; Truely a block
  4187. (setq block-type (downcase (match-string 5))
  4188. quoting (member block-type org-protecting-blocks))
  4189. (when (re-search-forward
  4190. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4191. nil t) ;; on purpose, we look further than LIMIT
  4192. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4193. (when quoting
  4194. (remove-text-properties beg end
  4195. '(display t invisible t intangible t)))
  4196. (add-text-properties
  4197. beg end
  4198. '(font-lock-fontified t font-lock-multiline t))
  4199. (add-text-properties beg beg1 '(face org-meta-line))
  4200. (add-text-properties end1 end '(face org-meta-line))
  4201. (cond
  4202. (quoting
  4203. (add-text-properties beg1 end1 '(face org-block)))
  4204. ((not org-fontify-quote-and-verse-blocks))
  4205. ((string= block-type "quote")
  4206. (add-text-properties beg1 end1 '(face org-quote)))
  4207. ((string= block-type "verse")
  4208. (add-text-properties beg1 end1 '(face org-verse))))
  4209. t))
  4210. ((member dc1 '("title:" "author:" "email:" "date:"))
  4211. (add-text-properties
  4212. beg (match-end 3)
  4213. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4214. '(font-lock-fontified t invisible t)
  4215. '(font-lock-fontified t face org-document-info-keyword)))
  4216. (add-text-properties
  4217. (match-beginning 6) (match-end 6)
  4218. (if (string-equal dc1 "title:")
  4219. '(font-lock-fontified t face org-document-title)
  4220. '(font-lock-fontified t face org-document-info))))
  4221. ((not (member (char-after beg) '(?\ ?\t)))
  4222. ;; just any other in-buffer setting, but not indented
  4223. (add-text-properties
  4224. beg (match-end 0)
  4225. '(font-lock-fontified t face org-meta-line))
  4226. t)
  4227. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4228. "orgtbl:" "tblfm:" "tblname:"))
  4229. (and (match-end 4) (equal dc3 "attr")))
  4230. (add-text-properties
  4231. beg (match-end 0)
  4232. '(font-lock-fontified t face org-meta-line))
  4233. t)
  4234. ((member dc3 '(" " ""))
  4235. (add-text-properties
  4236. beg (match-end 0)
  4237. '(font-lock-fontified t face font-lock-comment-face)))
  4238. (t nil))))))
  4239. (defun org-activate-angle-links (limit)
  4240. "Run through the buffer and add overlays to links."
  4241. (if (re-search-forward org-angle-link-re limit t)
  4242. (progn
  4243. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4244. (add-text-properties (match-beginning 0) (match-end 0)
  4245. (list 'mouse-face 'highlight
  4246. 'keymap org-mouse-map))
  4247. (org-rear-nonsticky-at (match-end 0))
  4248. t)))
  4249. (defun org-activate-footnote-links (limit)
  4250. "Run through the buffer and add overlays to links."
  4251. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4252. limit t)
  4253. (progn
  4254. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4255. (add-text-properties (match-beginning 2) (match-end 2)
  4256. (list 'mouse-face 'highlight
  4257. 'keymap org-mouse-map
  4258. 'help-echo
  4259. (if (= (point-at-bol) (match-beginning 2))
  4260. "Footnote definition"
  4261. "Footnote reference")
  4262. ))
  4263. (org-rear-nonsticky-at (match-end 2))
  4264. t)))
  4265. (defun org-activate-bracket-links (limit)
  4266. "Run through the buffer and add overlays to bracketed links."
  4267. (if (re-search-forward org-bracket-link-regexp limit t)
  4268. (let* ((help (concat "LINK: "
  4269. (org-match-string-no-properties 1)))
  4270. ;; FIXME: above we should remove the escapes.
  4271. ;; but that requires another match, protecting match data,
  4272. ;; a lot of overhead for font-lock.
  4273. (ip (org-maybe-intangible
  4274. (list 'invisible 'org-link
  4275. 'keymap org-mouse-map 'mouse-face 'highlight
  4276. 'font-lock-multiline t 'help-echo help)))
  4277. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4278. 'font-lock-multiline t 'help-echo help)))
  4279. ;; We need to remove the invisible property here. Table narrowing
  4280. ;; may have made some of this invisible.
  4281. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4282. (remove-text-properties (match-beginning 0) (match-end 0)
  4283. '(invisible nil))
  4284. (if (match-end 3)
  4285. (progn
  4286. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4287. (org-rear-nonsticky-at (match-beginning 3))
  4288. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4289. (org-rear-nonsticky-at (match-end 3))
  4290. (add-text-properties (match-end 3) (match-end 0) ip)
  4291. (org-rear-nonsticky-at (match-end 0)))
  4292. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4293. (org-rear-nonsticky-at (match-beginning 1))
  4294. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4295. (org-rear-nonsticky-at (match-end 1))
  4296. (add-text-properties (match-end 1) (match-end 0) ip)
  4297. (org-rear-nonsticky-at (match-end 0)))
  4298. t)))
  4299. (defun org-activate-dates (limit)
  4300. "Run through the buffer and add overlays to dates."
  4301. (if (re-search-forward org-tsr-regexp-both limit t)
  4302. (progn
  4303. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4304. (add-text-properties (match-beginning 0) (match-end 0)
  4305. (list 'mouse-face 'highlight
  4306. 'keymap org-mouse-map))
  4307. (org-rear-nonsticky-at (match-end 0))
  4308. (when org-display-custom-times
  4309. (if (match-end 3)
  4310. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4311. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4312. t)))
  4313. (defvar org-target-link-regexp nil
  4314. "Regular expression matching radio targets in plain text.")
  4315. (make-variable-buffer-local 'org-target-link-regexp)
  4316. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4317. "Regular expression matching a link target.")
  4318. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4319. "Regular expression matching a radio target.")
  4320. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4321. "Regular expression matching any target.")
  4322. (defun org-activate-target-links (limit)
  4323. "Run through the buffer and add overlays to target matches."
  4324. (when org-target-link-regexp
  4325. (let ((case-fold-search t))
  4326. (if (re-search-forward org-target-link-regexp limit t)
  4327. (progn
  4328. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4329. (add-text-properties (match-beginning 0) (match-end 0)
  4330. (list 'mouse-face 'highlight
  4331. 'keymap org-mouse-map
  4332. 'help-echo "Radio target link"
  4333. 'org-linked-text t))
  4334. (org-rear-nonsticky-at (match-end 0))
  4335. t)))))
  4336. (defun org-update-radio-target-regexp ()
  4337. "Find all radio targets in this file and update the regular expression."
  4338. (interactive)
  4339. (when (memq 'radio org-activate-links)
  4340. (setq org-target-link-regexp
  4341. (org-make-target-link-regexp (org-all-targets 'radio)))
  4342. (org-restart-font-lock)))
  4343. (defun org-hide-wide-columns (limit)
  4344. (let (s e)
  4345. (setq s (text-property-any (point) (or limit (point-max))
  4346. 'org-cwidth t))
  4347. (when s
  4348. (setq e (next-single-property-change s 'org-cwidth))
  4349. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4350. (goto-char e)
  4351. t)))
  4352. (defvar org-latex-and-specials-regexp nil
  4353. "Regular expression for highlighting export special stuff.")
  4354. (defvar org-match-substring-regexp)
  4355. (defvar org-match-substring-with-braces-regexp)
  4356. ;; This should be with the exporter code, but we also use if for font-locking
  4357. (defconst org-export-html-special-string-regexps
  4358. '(("\\\\-" . "&shy;")
  4359. ("---\\([^-]\\)" . "&mdash;\\1")
  4360. ("--\\([^-]\\)" . "&ndash;\\1")
  4361. ("\\.\\.\\." . "&hellip;"))
  4362. "Regular expressions for special string conversion.")
  4363. (defun org-compute-latex-and-specials-regexp ()
  4364. "Compute regular expression for stuff treated specially by exporters."
  4365. (if (not org-highlight-latex-fragments-and-specials)
  4366. (org-set-local 'org-latex-and-specials-regexp nil)
  4367. (require 'org-exp)
  4368. (let*
  4369. ((matchers (plist-get org-format-latex-options :matchers))
  4370. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4371. org-latex-regexps)))
  4372. (org-export-allow-BIND nil)
  4373. (options (org-combine-plists (org-default-export-plist)
  4374. (org-infile-export-plist)))
  4375. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4376. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4377. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4378. (org-export-html-expand (plist-get options :expand-quoted-html))
  4379. (org-export-with-special-strings (plist-get options :special-strings))
  4380. (re-sub
  4381. (cond
  4382. ((equal org-export-with-sub-superscripts '{})
  4383. (list org-match-substring-with-braces-regexp))
  4384. (org-export-with-sub-superscripts
  4385. (list org-match-substring-regexp))
  4386. (t nil)))
  4387. (re-latex
  4388. (if org-export-with-LaTeX-fragments
  4389. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4390. (re-macros
  4391. (if org-export-with-TeX-macros
  4392. (list (concat "\\\\"
  4393. (regexp-opt
  4394. (append (mapcar 'car (append org-entities-user
  4395. org-entities))
  4396. (if (boundp 'org-latex-entities)
  4397. (mapcar (lambda (x)
  4398. (or (car-safe x) x))
  4399. org-latex-entities)
  4400. nil))
  4401. 'words))) ; FIXME
  4402. ))
  4403. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4404. (re-special (if org-export-with-special-strings
  4405. (mapcar (lambda (x) (car x))
  4406. org-export-html-special-string-regexps)))
  4407. (re-rest
  4408. (delq nil
  4409. (list
  4410. (if org-export-html-expand "@<[^>\n]+>")
  4411. ))))
  4412. (org-set-local
  4413. 'org-latex-and-specials-regexp
  4414. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4415. re-rest) "\\|")))))
  4416. (defun org-do-latex-and-special-faces (limit)
  4417. "Run through the buffer and add overlays to links."
  4418. (when org-latex-and-specials-regexp
  4419. (let (rtn d)
  4420. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4421. limit t))
  4422. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4423. 'face))
  4424. '(org-code org-verbatim underline)))
  4425. (progn
  4426. (setq rtn t
  4427. d (cond ((member (char-after (1+ (match-beginning 0)))
  4428. '(?_ ?^)) 1)
  4429. (t 0)))
  4430. (font-lock-prepend-text-property
  4431. (+ d (match-beginning 0)) (match-end 0)
  4432. 'face 'org-latex-and-export-specials)
  4433. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4434. '(font-lock-multiline t)))))
  4435. rtn)))
  4436. (defun org-restart-font-lock ()
  4437. "Restart font-lock-mode, to force refontification."
  4438. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4439. (font-lock-mode -1)
  4440. (font-lock-mode 1)))
  4441. (defun org-all-targets (&optional radio)
  4442. "Return a list of all targets in this file.
  4443. With optional argument RADIO, only find radio targets."
  4444. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4445. rtn)
  4446. (save-excursion
  4447. (goto-char (point-min))
  4448. (while (re-search-forward re nil t)
  4449. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4450. rtn)))
  4451. (defun org-make-target-link-regexp (targets)
  4452. "Make regular expression matching all strings in TARGETS.
  4453. The regular expression finds the targets also if there is a line break
  4454. between words."
  4455. (and targets
  4456. (concat
  4457. "\\<\\("
  4458. (mapconcat
  4459. (lambda (x)
  4460. (while (string-match " +" x)
  4461. (setq x (replace-match "\\s-+" t t x)))
  4462. x)
  4463. targets
  4464. "\\|")
  4465. "\\)\\>")))
  4466. (defun org-activate-tags (limit)
  4467. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4468. (progn
  4469. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4470. (add-text-properties (match-beginning 1) (match-end 1)
  4471. (list 'mouse-face 'highlight
  4472. 'keymap org-mouse-map))
  4473. (org-rear-nonsticky-at (match-end 1))
  4474. t)))
  4475. (defun org-outline-level ()
  4476. "Compute the outline level of the heading at point.
  4477. This function assumes that the cursor is at the beginning of a line matched
  4478. by outline-regexp. Otherwise it returns garbage.
  4479. If this is called at a normal headline, the level is the number of stars.
  4480. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4481. For plain list items, if they are matched by `outline-regexp', this returns
  4482. 1000 plus the line indentation."
  4483. (save-excursion
  4484. (looking-at outline-regexp)
  4485. (if (match-beginning 1)
  4486. (+ (org-get-string-indentation (match-string 1)) 1000)
  4487. (1- (- (match-end 0) (match-beginning 0))))))
  4488. (defvar org-font-lock-keywords nil)
  4489. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4490. "Regular expression matching a property line.")
  4491. (defvar org-font-lock-hook nil
  4492. "Functions to be called for special font lock stuff.")
  4493. (defun org-font-lock-hook (limit)
  4494. (run-hook-with-args 'org-font-lock-hook limit))
  4495. (defun org-set-font-lock-defaults ()
  4496. (let* ((em org-fontify-emphasized-text)
  4497. (lk org-activate-links)
  4498. (org-font-lock-extra-keywords
  4499. (list
  4500. ;; Call the hook
  4501. '(org-font-lock-hook)
  4502. ;; Headlines
  4503. `(,(if org-fontify-whole-heading-line
  4504. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4505. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4506. (1 (org-get-level-face 1))
  4507. (2 (org-get-level-face 2))
  4508. (3 (org-get-level-face 3)))
  4509. ;; Table lines
  4510. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4511. (1 'org-table t))
  4512. ;; Table internals
  4513. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4514. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4515. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4516. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4517. ;; Drawers
  4518. (list org-drawer-regexp '(0 'org-special-keyword t))
  4519. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4520. ;; Properties
  4521. (list org-property-re
  4522. '(1 'org-special-keyword t)
  4523. '(3 'org-property-value t))
  4524. ;; Links
  4525. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4526. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4527. (if (memq 'plain lk) '(org-activate-plain-links))
  4528. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4529. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4530. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4531. (if (memq 'footnote lk) '(org-activate-footnote-links
  4532. (2 'org-footnote t)))
  4533. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4534. '(org-hide-wide-columns (0 nil append))
  4535. ;; TODO lines
  4536. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4537. '(1 (org-get-todo-face 1) t))
  4538. ;; DONE
  4539. (if org-fontify-done-headline
  4540. (list (concat "^[*]+ +\\<\\("
  4541. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4542. "\\)\\(.*\\)")
  4543. '(2 'org-headline-done t))
  4544. nil)
  4545. ;; Priorities
  4546. '(org-font-lock-add-priority-faces)
  4547. ;; Tags
  4548. '(org-font-lock-add-tag-faces)
  4549. ;; Special keywords
  4550. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4551. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4552. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4553. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4554. ;; Emphasis
  4555. (if em
  4556. (if (featurep 'xemacs)
  4557. '(org-do-emphasis-faces (0 nil append))
  4558. '(org-do-emphasis-faces)))
  4559. ;; Checkboxes
  4560. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4561. 2 'org-checkbox prepend)
  4562. (if org-provide-checkbox-statistics
  4563. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4564. (0 (org-get-checkbox-statistics-face) t)))
  4565. ;; Description list items
  4566. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4567. 2 'bold prepend)
  4568. ;; ARCHIVEd headings
  4569. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4570. '(1 'org-archived prepend))
  4571. ;; Specials
  4572. '(org-do-latex-and-special-faces)
  4573. ;; Code
  4574. '(org-activate-code (1 'org-code t))
  4575. ;; COMMENT
  4576. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4577. "\\|" org-quote-string "\\)\\>")
  4578. '(1 'org-special-keyword t))
  4579. '("^#.*" (0 'font-lock-comment-face t))
  4580. ;; Blocks and meta lines
  4581. '(org-fontify-meta-lines-and-blocks)
  4582. )))
  4583. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4584. ;; Now set the full font-lock-keywords
  4585. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4586. (org-set-local 'font-lock-defaults
  4587. '(org-font-lock-keywords t nil nil backward-paragraph))
  4588. (kill-local-variable 'font-lock-keywords) nil))
  4589. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4590. "Fontify string S like in Org-mode"
  4591. (with-temp-buffer
  4592. (insert s)
  4593. (let ((org-odd-levels-only odd-levels))
  4594. (org-mode)
  4595. (font-lock-fontify-buffer)
  4596. (buffer-string))))
  4597. (defvar org-m nil)
  4598. (defvar org-l nil)
  4599. (defvar org-f nil)
  4600. (defun org-get-level-face (n)
  4601. "Get the right face for match N in font-lock matching of headlines."
  4602. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4603. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4604. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4605. (cond
  4606. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4607. ((eq n 2) org-f)
  4608. (t (if org-level-color-stars-only nil org-f))))
  4609. (defun org-get-todo-face (kwd)
  4610. "Get the right face for a TODO keyword KWD.
  4611. If KWD is a number, get the corresponding match group."
  4612. (if (numberp kwd) (setq kwd (match-string kwd)))
  4613. (or (org-face-from-face-or-color
  4614. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4615. (and (member kwd org-done-keywords) 'org-done)
  4616. 'org-todo))
  4617. (defun org-face-from-face-or-color (context inherit face-or-color)
  4618. "Create a face list that inherits INHERIT, but sets the foreground color.
  4619. When FACE-OR-COLOR is not a string, just return it."
  4620. (if (stringp face-or-color)
  4621. (list :inherit inherit
  4622. (cdr (assoc context org-faces-easy-properties))
  4623. face-or-color)
  4624. face-or-color))
  4625. (defun org-font-lock-add-tag-faces (limit)
  4626. "Add the special tag faces."
  4627. (when (and org-tag-faces org-tags-special-faces-re)
  4628. (while (re-search-forward org-tags-special-faces-re limit t)
  4629. (add-text-properties (match-beginning 1) (match-end 1)
  4630. (list 'face (org-get-tag-face 1)
  4631. 'font-lock-fontified t))
  4632. (backward-char 1))))
  4633. (defun org-font-lock-add-priority-faces (limit)
  4634. "Add the special priority faces."
  4635. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4636. (add-text-properties
  4637. (match-beginning 0) (match-end 0)
  4638. (list 'face (or (org-face-from-face-or-color
  4639. 'priority 'org-special-keyword
  4640. (cdr (assoc (char-after (match-beginning 1))
  4641. org-priority-faces)))
  4642. 'org-special-keyword)
  4643. 'font-lock-fontified t))))
  4644. (defun org-get-tag-face (kwd)
  4645. "Get the right face for a TODO keyword KWD.
  4646. If KWD is a number, get the corresponding match group."
  4647. (if (numberp kwd) (setq kwd (match-string kwd)))
  4648. (or (org-face-from-face-or-color
  4649. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  4650. 'org-tag))
  4651. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4652. "Remove fontification and activation overlays from links."
  4653. (font-lock-default-unfontify-region beg end)
  4654. (let* ((buffer-undo-list t)
  4655. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4656. (inhibit-modification-hooks t)
  4657. deactivate-mark buffer-file-name buffer-file-truename)
  4658. (remove-text-properties
  4659. beg end
  4660. (if org-indent-mode
  4661. ;; also remove line-prefix and wrap-prefix properties
  4662. '(mouse-face t keymap t org-linked-text t
  4663. invisible t intangible t
  4664. line-prefix t wrap-prefix t
  4665. org-no-flyspell t)
  4666. '(mouse-face t keymap t org-linked-text t
  4667. invisible t intangible t
  4668. org-no-flyspell t)))))
  4669. ;;;; Visibility cycling, including org-goto and indirect buffer
  4670. ;;; Cycling
  4671. (defvar org-cycle-global-status nil)
  4672. (make-variable-buffer-local 'org-cycle-global-status)
  4673. (defvar org-cycle-subtree-status nil)
  4674. (make-variable-buffer-local 'org-cycle-subtree-status)
  4675. ;;;###autoload
  4676. (defvar org-inlinetask-min-level)
  4677. (defun org-cycle (&optional arg)
  4678. "TAB-action and visibility cycling for Org-mode.
  4679. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4680. is outline visibility cycling, but it also invokes other actions
  4681. in special contexts.
  4682. - When this function is called with a prefix argument, rotate the entire
  4683. buffer through 3 states (global cycling)
  4684. 1. OVERVIEW: Show only top-level headlines.
  4685. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4686. 3. SHOW ALL: Show everything.
  4687. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4688. determined by the variable `org-startup-folded', and by any VISIBILITY
  4689. properties in the buffer.
  4690. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4691. including any drawers.
  4692. - When inside a table, re-align the table and move to the next field.
  4693. - When point is at the beginning of a headline, rotate the subtree started
  4694. by this line through 3 different states (local cycling)
  4695. 1. FOLDED: Only the main headline is shown.
  4696. 2. CHILDREN: The main headline and the direct children are shown.
  4697. From this state, you can move to one of the children
  4698. and zoom in further.
  4699. 3. SUBTREE: Show the entire subtree, including body text.
  4700. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4701. - When point is at the beginning of an empty headline and the variable
  4702. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  4703. of the headline by demoting and promoting it to likely levels. This
  4704. speeds up creation document structure by presing TAB once or several
  4705. times right after creating a new headline.
  4706. - When there is a numeric prefix, go up to a heading with level ARG, do
  4707. a `show-subtree' and return to the previous cursor position. If ARG
  4708. is negative, go up that many levels.
  4709. - When point is not at the beginning of a headline, execute the global
  4710. binding for TAB, which is re-indenting the line. See the option
  4711. `org-cycle-emulate-tab' for details.
  4712. - Special case: if point is at the beginning of the buffer and there is
  4713. no headline in line 1, this function will act as if called with prefix arg.
  4714. But only if also the variable `org-cycle-global-at-bob' is t."
  4715. (interactive "P")
  4716. (org-load-modules-maybe)
  4717. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4718. (and org-cycle-level-after-item/entry-creation
  4719. (or (org-cycle-level)
  4720. (org-cycle-item-indentation))))
  4721. (let* ((limit-level
  4722. (or org-cycle-max-level
  4723. (and (boundp 'org-inlinetask-min-level)
  4724. org-inlinetask-min-level
  4725. (1- org-inlinetask-min-level))))
  4726. (nstars (and limit-level
  4727. (if org-odd-levels-only
  4728. (and limit-level (1- (* limit-level 2)))
  4729. limit-level)))
  4730. (outline-regexp
  4731. (cond
  4732. ((not (org-mode-p)) outline-regexp)
  4733. ((or (eq org-cycle-include-plain-lists 'integrate)
  4734. (and org-cycle-include-plain-lists (org-at-item-p)))
  4735. (concat "\\(?:\\*"
  4736. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4737. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4738. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4739. (bob-special (and org-cycle-global-at-bob (bobp)
  4740. (not (looking-at outline-regexp))))
  4741. (org-cycle-hook
  4742. (if bob-special
  4743. (delq 'org-optimize-window-after-visibility-change
  4744. (copy-sequence org-cycle-hook))
  4745. org-cycle-hook))
  4746. (pos (point)))
  4747. (if (or bob-special (equal arg '(4)))
  4748. ;; special case: use global cycling
  4749. (setq arg t))
  4750. (cond
  4751. ((equal arg '(16))
  4752. (org-set-startup-visibility)
  4753. (message "Startup visibility, plus VISIBILITY properties"))
  4754. ((equal arg '(64))
  4755. (show-all)
  4756. (message "Entire buffer visible, including drawers"))
  4757. ((org-at-table-p 'any)
  4758. ;; Enter the table or move to the next field in the table
  4759. (if (org-at-table.el-p)
  4760. (message "Use C-c ' to edit table.el tables")
  4761. (if arg (org-table-edit-field t)
  4762. (org-table-justify-field-maybe)
  4763. (call-interactively 'org-table-next-field))))
  4764. ((run-hook-with-args-until-success
  4765. 'org-tab-after-check-for-table-hook))
  4766. ((eq arg t) ;; Global cycling
  4767. (org-cycle-internal-global))
  4768. ((and org-drawers org-drawer-regexp
  4769. (save-excursion
  4770. (beginning-of-line 1)
  4771. (looking-at org-drawer-regexp)))
  4772. ;; Toggle block visibility
  4773. (org-flag-drawer
  4774. (not (get-char-property (match-end 0) 'invisible))))
  4775. ((integerp arg)
  4776. ;; Show-subtree, ARG levels up from here.
  4777. (save-excursion
  4778. (org-back-to-heading)
  4779. (outline-up-heading (if (< arg 0) (- arg)
  4780. (- (funcall outline-level) arg)))
  4781. (org-show-subtree)))
  4782. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4783. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4784. (org-cycle-internal-local))
  4785. ;; TAB emulation and template completion
  4786. (buffer-read-only (org-back-to-heading))
  4787. ((run-hook-with-args-until-success
  4788. 'org-tab-after-check-for-cycling-hook))
  4789. ((org-try-structure-completion))
  4790. ((org-try-cdlatex-tab))
  4791. ((run-hook-with-args-until-success
  4792. 'org-tab-before-tab-emulation-hook))
  4793. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4794. (or (not (bolp))
  4795. (not (looking-at outline-regexp))))
  4796. (call-interactively (global-key-binding "\t")))
  4797. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4798. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4799. (or (and (eq org-cycle-emulate-tab 'white)
  4800. (= (match-end 0) (point-at-eol)))
  4801. (and (eq org-cycle-emulate-tab 'whitestart)
  4802. (>= (match-end 0) pos))))
  4803. t
  4804. (eq org-cycle-emulate-tab t))
  4805. (call-interactively (global-key-binding "\t")))
  4806. (t (save-excursion
  4807. (org-back-to-heading)
  4808. (org-cycle)))))))
  4809. (defun org-cycle-internal-global ()
  4810. "Do the global cycling action."
  4811. (cond
  4812. ((and (eq last-command this-command)
  4813. (eq org-cycle-global-status 'overview))
  4814. ;; We just created the overview - now do table of contents
  4815. ;; This can be slow in very large buffers, so indicate action
  4816. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4817. (message "CONTENTS...")
  4818. (org-content)
  4819. (message "CONTENTS...done")
  4820. (setq org-cycle-global-status 'contents)
  4821. (run-hook-with-args 'org-cycle-hook 'contents))
  4822. ((and (eq last-command this-command)
  4823. (eq org-cycle-global-status 'contents))
  4824. ;; We just showed the table of contents - now show everything
  4825. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4826. (show-all)
  4827. (message "SHOW ALL")
  4828. (setq org-cycle-global-status 'all)
  4829. (run-hook-with-args 'org-cycle-hook 'all))
  4830. (t
  4831. ;; Default action: go to overview
  4832. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4833. (org-overview)
  4834. (message "OVERVIEW")
  4835. (setq org-cycle-global-status 'overview)
  4836. (run-hook-with-args 'org-cycle-hook 'overview))))
  4837. (defun org-cycle-internal-local ()
  4838. "Do the local cycling action."
  4839. (org-back-to-heading)
  4840. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4841. ;; First, some boundaries
  4842. (save-excursion
  4843. (org-back-to-heading)
  4844. (setq level (funcall outline-level))
  4845. (save-excursion
  4846. (beginning-of-line 2)
  4847. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4848. ; XEmacs does not have `next-single-char-property-change'
  4849. ; I'm not sure about Emacs 21.
  4850. (while (and (not (eobp)) ;; this is like `next-line'
  4851. (get-char-property (1- (point)) 'invisible))
  4852. (beginning-of-line 2))
  4853. (while (and (not (eobp)) ;; this is like `next-line'
  4854. (get-char-property (1- (point)) 'invisible))
  4855. (goto-char (next-single-char-property-change (point) 'invisible))
  4856. ;;;??? (or (bolp) (beginning-of-line 2))))
  4857. (and (eolp) (beginning-of-line 2))))
  4858. (setq eol (point)))
  4859. (outline-end-of-heading) (setq eoh (point))
  4860. (save-excursion
  4861. (outline-next-heading)
  4862. (setq has-children (and (org-at-heading-p t)
  4863. (> (funcall outline-level) level))))
  4864. (org-end-of-subtree t)
  4865. (unless (eobp)
  4866. (skip-chars-forward " \t\n")
  4867. (beginning-of-line 1) ; in case this is an item
  4868. )
  4869. (setq eos (if (eobp) (point) (1- (point)))))
  4870. ;; Find out what to do next and set `this-command'
  4871. (cond
  4872. ((= eos eoh)
  4873. ;; Nothing is hidden behind this heading
  4874. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4875. (message "EMPTY ENTRY")
  4876. (setq org-cycle-subtree-status nil)
  4877. (save-excursion
  4878. (goto-char eos)
  4879. (outline-next-heading)
  4880. (if (org-invisible-p) (org-flag-heading nil))))
  4881. ((and (or (>= eol eos)
  4882. (not (string-match "\\S-" (buffer-substring eol eos))))
  4883. (or has-children
  4884. (not (setq children-skipped
  4885. org-cycle-skip-children-state-if-no-children))))
  4886. ;; Entire subtree is hidden in one line: children view
  4887. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4888. (org-show-entry)
  4889. (show-children)
  4890. (message "CHILDREN")
  4891. (save-excursion
  4892. (goto-char eos)
  4893. (outline-next-heading)
  4894. (if (org-invisible-p) (org-flag-heading nil)))
  4895. (setq org-cycle-subtree-status 'children)
  4896. (run-hook-with-args 'org-cycle-hook 'children))
  4897. ((or children-skipped
  4898. (and (eq last-command this-command)
  4899. (eq org-cycle-subtree-status 'children)))
  4900. ;; We just showed the children, or no children are there,
  4901. ;; now show everything.
  4902. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4903. (org-show-subtree)
  4904. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4905. (setq org-cycle-subtree-status 'subtree)
  4906. (run-hook-with-args 'org-cycle-hook 'subtree))
  4907. (t
  4908. ;; Default action: hide the subtree.
  4909. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4910. (hide-subtree)
  4911. (message "FOLDED")
  4912. (setq org-cycle-subtree-status 'folded)
  4913. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4914. ;;;###autoload
  4915. (defun org-global-cycle (&optional arg)
  4916. "Cycle the global visibility. For details see `org-cycle'.
  4917. With C-u prefix arg, switch to startup visibility.
  4918. With a numeric prefix, show all headlines up to that level."
  4919. (interactive "P")
  4920. (let ((org-cycle-include-plain-lists
  4921. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4922. (cond
  4923. ((integerp arg)
  4924. (show-all)
  4925. (hide-sublevels arg)
  4926. (setq org-cycle-global-status 'contents))
  4927. ((equal arg '(4))
  4928. (org-set-startup-visibility)
  4929. (message "Startup visibility, plus VISIBILITY properties."))
  4930. (t
  4931. (org-cycle '(4))))))
  4932. (defun org-set-startup-visibility ()
  4933. "Set the visibility required by startup options and properties."
  4934. (cond
  4935. ((eq org-startup-folded t)
  4936. (org-cycle '(4)))
  4937. ((eq org-startup-folded 'content)
  4938. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4939. (org-cycle '(4)) (org-cycle '(4)))))
  4940. (unless (eq org-startup-folded 'showeverything)
  4941. (if org-hide-block-startup (org-hide-block-all))
  4942. (org-set-visibility-according-to-property 'no-cleanup)
  4943. (org-cycle-hide-archived-subtrees 'all)
  4944. (org-cycle-hide-drawers 'all)
  4945. (org-cycle-show-empty-lines 'all)))
  4946. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4947. "Switch subtree visibilities according to :VISIBILITY: property."
  4948. (interactive)
  4949. (let (org-show-entry-below state)
  4950. (save-excursion
  4951. (goto-char (point-min))
  4952. (while (re-search-forward
  4953. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4954. nil t)
  4955. (setq state (match-string 1))
  4956. (save-excursion
  4957. (org-back-to-heading t)
  4958. (hide-subtree)
  4959. (org-reveal)
  4960. (cond
  4961. ((equal state '("fold" "folded"))
  4962. (hide-subtree))
  4963. ((equal state "children")
  4964. (org-show-hidden-entry)
  4965. (show-children))
  4966. ((equal state "content")
  4967. (save-excursion
  4968. (save-restriction
  4969. (org-narrow-to-subtree)
  4970. (org-content))))
  4971. ((member state '("all" "showall"))
  4972. (show-subtree)))))
  4973. (unless no-cleanup
  4974. (org-cycle-hide-archived-subtrees 'all)
  4975. (org-cycle-hide-drawers 'all)
  4976. (org-cycle-show-empty-lines 'all)))))
  4977. (defun org-overview ()
  4978. "Switch to overview mode, showing only top-level headlines.
  4979. Really, this shows all headlines with level equal or greater than the level
  4980. of the first headline in the buffer. This is important, because if the
  4981. first headline is not level one, then (hide-sublevels 1) gives confusing
  4982. results."
  4983. (interactive)
  4984. (let ((level (save-excursion
  4985. (goto-char (point-min))
  4986. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4987. (progn
  4988. (goto-char (match-beginning 0))
  4989. (funcall outline-level))))))
  4990. (and level (hide-sublevels level))))
  4991. (defun org-content (&optional arg)
  4992. "Show all headlines in the buffer, like a table of contents.
  4993. With numerical argument N, show content up to level N."
  4994. (interactive "P")
  4995. (save-excursion
  4996. ;; Visit all headings and show their offspring
  4997. (and (integerp arg) (org-overview))
  4998. (goto-char (point-max))
  4999. (catch 'exit
  5000. (while (and (progn (condition-case nil
  5001. (outline-previous-visible-heading 1)
  5002. (error (goto-char (point-min))))
  5003. t)
  5004. (looking-at outline-regexp))
  5005. (if (integerp arg)
  5006. (show-children (1- arg))
  5007. (show-branches))
  5008. (if (bobp) (throw 'exit nil))))))
  5009. (defun org-optimize-window-after-visibility-change (state)
  5010. "Adjust the window after a change in outline visibility.
  5011. This function is the default value of the hook `org-cycle-hook'."
  5012. (when (get-buffer-window (current-buffer))
  5013. (cond
  5014. ((eq state 'content) nil)
  5015. ((eq state 'all) nil)
  5016. ((eq state 'folded) nil)
  5017. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5018. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5019. (defun org-remove-empty-overlays-at (pos)
  5020. "Remove outline overlays that do not contain non-white stuff."
  5021. (mapc
  5022. (lambda (o)
  5023. (and (eq 'outline (org-overlay-get o 'invisible))
  5024. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  5025. (org-overlay-end o))))
  5026. (org-delete-overlay o)))
  5027. (org-overlays-at pos)))
  5028. (defun org-clean-visibility-after-subtree-move ()
  5029. "Fix visibility issues after moving a subtree."
  5030. ;; First, find a reasonable region to look at:
  5031. ;; Start two siblings above, end three below
  5032. (let* ((beg (save-excursion
  5033. (and (org-get-last-sibling)
  5034. (org-get-last-sibling))
  5035. (point)))
  5036. (end (save-excursion
  5037. (and (org-get-next-sibling)
  5038. (org-get-next-sibling)
  5039. (org-get-next-sibling))
  5040. (if (org-at-heading-p)
  5041. (point-at-eol)
  5042. (point))))
  5043. (level (looking-at "\\*+"))
  5044. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5045. (save-excursion
  5046. (save-restriction
  5047. (narrow-to-region beg end)
  5048. (when re
  5049. ;; Properly fold already folded siblings
  5050. (goto-char (point-min))
  5051. (while (re-search-forward re nil t)
  5052. (if (and (not (org-invisible-p))
  5053. (save-excursion
  5054. (goto-char (point-at-eol)) (org-invisible-p)))
  5055. (hide-entry))))
  5056. (org-cycle-show-empty-lines 'overview)
  5057. (org-cycle-hide-drawers 'overview)))))
  5058. (defun org-cycle-show-empty-lines (state)
  5059. "Show empty lines above all visible headlines.
  5060. The region to be covered depends on STATE when called through
  5061. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5062. entire buffer covered. Note that an empty line is only shown if there
  5063. are at least `org-cycle-separator-lines' empty lines before the headline."
  5064. (when (not (= org-cycle-separator-lines 0))
  5065. (save-excursion
  5066. (let* ((n (abs org-cycle-separator-lines))
  5067. (re (cond
  5068. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5069. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5070. (t (let ((ns (number-to-string (- n 2))))
  5071. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5072. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5073. beg end b e)
  5074. (cond
  5075. ((memq state '(overview contents t))
  5076. (setq beg (point-min) end (point-max)))
  5077. ((memq state '(children folded))
  5078. (setq beg (point) end (progn (org-end-of-subtree t t)
  5079. (beginning-of-line 2)
  5080. (point)))))
  5081. (when beg
  5082. (goto-char beg)
  5083. (while (re-search-forward re end t)
  5084. (unless (get-char-property (match-end 1) 'invisible)
  5085. (setq e (match-end 1))
  5086. (if (< org-cycle-separator-lines 0)
  5087. (setq b (save-excursion
  5088. (goto-char (match-beginning 0))
  5089. (org-back-over-empty-lines)
  5090. (if (save-excursion
  5091. (goto-char (max (point-min) (1- (point))))
  5092. (org-on-heading-p))
  5093. (1- (point))
  5094. (point))))
  5095. (setq b (match-beginning 1)))
  5096. (outline-flag-region b e nil)))))))
  5097. ;; Never hide empty lines at the end of the file.
  5098. (save-excursion
  5099. (goto-char (point-max))
  5100. (outline-previous-heading)
  5101. (outline-end-of-heading)
  5102. (if (and (looking-at "[ \t\n]+")
  5103. (= (match-end 0) (point-max)))
  5104. (outline-flag-region (point) (match-end 0) nil))))
  5105. (defun org-show-empty-lines-in-parent ()
  5106. "Move to the parent and re-show empty lines before visible headlines."
  5107. (save-excursion
  5108. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5109. (org-cycle-show-empty-lines context))))
  5110. (defun org-files-list ()
  5111. "Return `org-agenda-files' list, plus all open org-mode files.
  5112. This is useful for operations that need to scan all of a user's
  5113. open and agenda-wise Org files."
  5114. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5115. (dolist (buf (buffer-list))
  5116. (with-current-buffer buf
  5117. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5118. (let ((file (expand-file-name (buffer-file-name))))
  5119. (unless (member file files)
  5120. (push file files))))))
  5121. files))
  5122. (defsubst org-entry-beginning-position ()
  5123. "Return the beginning position of the current entry."
  5124. (save-excursion (outline-back-to-heading t) (point)))
  5125. (defsubst org-entry-end-position ()
  5126. "Return the end position of the current entry."
  5127. (save-excursion (outline-next-heading) (point)))
  5128. (defun org-cycle-hide-drawers (state)
  5129. "Re-hide all drawers after a visibility state change."
  5130. (when (and (org-mode-p)
  5131. (not (memq state '(overview folded contents))))
  5132. (save-excursion
  5133. (let* ((globalp (memq state '(contents all)))
  5134. (beg (if globalp (point-min) (point)))
  5135. (end (if globalp (point-max)
  5136. (if (eq state 'children)
  5137. (save-excursion (outline-next-heading) (point))
  5138. (org-end-of-subtree t)))))
  5139. (goto-char beg)
  5140. (while (re-search-forward org-drawer-regexp end t)
  5141. (org-flag-drawer t))))))
  5142. (defun org-flag-drawer (flag)
  5143. (save-excursion
  5144. (beginning-of-line 1)
  5145. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5146. (let ((b (match-end 0))
  5147. (outline-regexp org-outline-regexp))
  5148. (if (re-search-forward
  5149. "^[ \t]*:END:"
  5150. (save-excursion (outline-next-heading) (point)) t)
  5151. (outline-flag-region b (point-at-eol) flag)
  5152. (error ":END: line missing at position %s" b))))))
  5153. (defun org-subtree-end-visible-p ()
  5154. "Is the end of the current subtree visible?"
  5155. (pos-visible-in-window-p
  5156. (save-excursion (org-end-of-subtree t) (point))))
  5157. (defun org-first-headline-recenter (&optional N)
  5158. "Move cursor to the first headline and recenter the headline.
  5159. Optional argument N means put the headline into the Nth line of the window."
  5160. (goto-char (point-min))
  5161. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5162. (beginning-of-line)
  5163. (recenter (prefix-numeric-value N))))
  5164. ;;; Saving and restoring visibility
  5165. (defun org-outline-overlay-data (&optional use-markers)
  5166. "Return a list of the locations of all outline overlays.
  5167. The are overlays with the `invisible' property value `outline'.
  5168. The return valus is a list of cons cells, with start and stop
  5169. positions for each overlay.
  5170. If USE-MARKERS is set, return the positions as markers."
  5171. (let (beg end)
  5172. (save-excursion
  5173. (save-restriction
  5174. (widen)
  5175. (delq nil
  5176. (mapcar (lambda (o)
  5177. (when (eq (org-overlay-get o 'invisible) 'outline)
  5178. (setq beg (org-overlay-start o)
  5179. end (org-overlay-end o))
  5180. (and beg end (> end beg)
  5181. (if use-markers
  5182. (cons (move-marker (make-marker) beg)
  5183. (move-marker (make-marker) end))
  5184. (cons beg end)))))
  5185. (org-overlays-in (point-min) (point-max))))))))
  5186. (defun org-set-outline-overlay-data (data)
  5187. "Create visibility overlays for all positions in DATA.
  5188. DATA should have been made by `org-outline-overlay-data'."
  5189. (let (o)
  5190. (save-excursion
  5191. (save-restriction
  5192. (widen)
  5193. (show-all)
  5194. (mapc (lambda (c)
  5195. (setq o (org-make-overlay (car c) (cdr c)))
  5196. (org-overlay-put o 'invisible 'outline))
  5197. data)))))
  5198. (defmacro org-save-outline-visibility (use-markers &rest body)
  5199. "Save and restore outline visibility around BODY.
  5200. If USE-MARKERS is non-nil, use markers for the positions.
  5201. This means that the buffer may change while running BODY,
  5202. but it also means that the buffer should stay alive
  5203. during the operation, because otherwise all these markers will
  5204. point nowhere."
  5205. `(let ((data (org-outline-overlay-data ,use-markers)))
  5206. (unwind-protect
  5207. (progn
  5208. ,@body
  5209. (org-set-outline-overlay-data data))
  5210. (when ,use-markers
  5211. (mapc (lambda (c)
  5212. (and (markerp (car c)) (move-marker (car c) nil))
  5213. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5214. data)))))
  5215. ;;; Folding of blocks
  5216. (defconst org-block-regexp
  5217. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5218. "Regular expression for hiding blocks.")
  5219. (defvar org-hide-block-overlays nil
  5220. "Overlays hiding blocks.")
  5221. (make-variable-buffer-local 'org-hide-block-overlays)
  5222. (defun org-block-map (function &optional start end)
  5223. "Call func at the head of all source blocks in the current
  5224. buffer. Optional arguments START and END can be used to limit
  5225. the range."
  5226. (let ((start (or start (point-min)))
  5227. (end (or end (point-max))))
  5228. (save-excursion
  5229. (goto-char start)
  5230. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5231. (save-excursion
  5232. (save-match-data
  5233. (goto-char (match-beginning 0))
  5234. (funcall function)))))))
  5235. (defun org-hide-block-toggle-all ()
  5236. "Toggle the visibility of all blocks in the current buffer."
  5237. (org-block-map #'org-hide-block-toggle))
  5238. (defun org-hide-block-all ()
  5239. "Fold all blocks in the current buffer."
  5240. (interactive)
  5241. (org-show-block-all)
  5242. (org-block-map #'org-hide-block-toggle-maybe))
  5243. (defun org-show-block-all ()
  5244. "Unfold all blocks in the current buffer."
  5245. (mapc 'org-delete-overlay org-hide-block-overlays)
  5246. (setq org-hide-block-overlays nil))
  5247. (defun org-hide-block-toggle-maybe ()
  5248. "Toggle visibility of block at point."
  5249. (interactive)
  5250. (let ((case-fold-search t))
  5251. (if (save-excursion
  5252. (beginning-of-line 1)
  5253. (looking-at org-block-regexp))
  5254. (progn (org-hide-block-toggle)
  5255. t) ;; to signal that we took action
  5256. nil))) ;; to signal that we did not
  5257. (defun org-hide-block-toggle (&optional force)
  5258. "Toggle the visibility of the current block."
  5259. (interactive)
  5260. (save-excursion
  5261. (beginning-of-line)
  5262. (if (re-search-forward org-block-regexp nil t)
  5263. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5264. (end (match-end 0)) ;; end of entire body
  5265. ov)
  5266. (if (memq t (mapcar (lambda (overlay)
  5267. (eq (org-overlay-get overlay 'invisible)
  5268. 'org-hide-block))
  5269. (org-overlays-at start)))
  5270. (if (or (not force) (eq force 'off))
  5271. (mapc (lambda (ov)
  5272. (when (member ov org-hide-block-overlays)
  5273. (setq org-hide-block-overlays
  5274. (delq ov org-hide-block-overlays)))
  5275. (when (eq (org-overlay-get ov 'invisible)
  5276. 'org-hide-block)
  5277. (org-delete-overlay ov)))
  5278. (org-overlays-at start)))
  5279. (setq ov (org-make-overlay start end))
  5280. (org-overlay-put ov 'invisible 'org-hide-block)
  5281. ;; make the block accessible to isearch
  5282. (org-overlay-put
  5283. ov 'isearch-open-invisible
  5284. (lambda (ov)
  5285. (when (member ov org-hide-block-overlays)
  5286. (setq org-hide-block-overlays
  5287. (delq ov org-hide-block-overlays)))
  5288. (when (eq (org-overlay-get ov 'invisible)
  5289. 'org-hide-block)
  5290. (org-delete-overlay ov))))
  5291. (push ov org-hide-block-overlays)))
  5292. (error "Not looking at a source block"))))
  5293. ;; org-tab-after-check-for-cycling-hook
  5294. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5295. ;; Remove overlays when changing major mode
  5296. (add-hook 'org-mode-hook
  5297. (lambda () (org-add-hook 'change-major-mode-hook
  5298. 'org-show-block-all 'append 'local)))
  5299. ;;; Org-goto
  5300. (defvar org-goto-window-configuration nil)
  5301. (defvar org-goto-marker nil)
  5302. (defvar org-goto-map
  5303. (let ((map (make-sparse-keymap)))
  5304. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5305. (while (setq cmd (pop cmds))
  5306. (substitute-key-definition cmd cmd map global-map)))
  5307. (suppress-keymap map)
  5308. (org-defkey map "\C-m" 'org-goto-ret)
  5309. (org-defkey map [(return)] 'org-goto-ret)
  5310. (org-defkey map [(left)] 'org-goto-left)
  5311. (org-defkey map [(right)] 'org-goto-right)
  5312. (org-defkey map [(control ?g)] 'org-goto-quit)
  5313. (org-defkey map "\C-i" 'org-cycle)
  5314. (org-defkey map [(tab)] 'org-cycle)
  5315. (org-defkey map [(down)] 'outline-next-visible-heading)
  5316. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5317. (if org-goto-auto-isearch
  5318. (if (fboundp 'define-key-after)
  5319. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5320. nil)
  5321. (org-defkey map "q" 'org-goto-quit)
  5322. (org-defkey map "n" 'outline-next-visible-heading)
  5323. (org-defkey map "p" 'outline-previous-visible-heading)
  5324. (org-defkey map "f" 'outline-forward-same-level)
  5325. (org-defkey map "b" 'outline-backward-same-level)
  5326. (org-defkey map "u" 'outline-up-heading))
  5327. (org-defkey map "/" 'org-occur)
  5328. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5329. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5330. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5331. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5332. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5333. map))
  5334. (defconst org-goto-help
  5335. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5336. RET=jump to location [Q]uit and return to previous location
  5337. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5338. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5339. ;; FIXME: Docstring does not mention both interfaces
  5340. (defun org-goto (&optional alternative-interface)
  5341. "Look up a different location in the current file, keeping current visibility.
  5342. When you want look-up or go to a different location in a document, the
  5343. fastest way is often to fold the entire buffer and then dive into the tree.
  5344. This method has the disadvantage, that the previous location will be folded,
  5345. which may not be what you want.
  5346. This command works around this by showing a copy of the current buffer
  5347. in an indirect buffer, in overview mode. You can dive into the tree in
  5348. that copy, use org-occur and incremental search to find a location.
  5349. When pressing RET or `Q', the command returns to the original buffer in
  5350. which the visibility is still unchanged. After RET is will also jump to
  5351. the location selected in the indirect buffer and expose the
  5352. the headline hierarchy above."
  5353. (interactive "P")
  5354. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5355. (org-refile-use-outline-path t)
  5356. (org-refile-target-verify-function nil)
  5357. (interface
  5358. (if (not alternative-interface)
  5359. org-goto-interface
  5360. (if (eq org-goto-interface 'outline)
  5361. 'outline-path-completion
  5362. 'outline)))
  5363. (org-goto-start-pos (point))
  5364. (selected-point
  5365. (if (eq interface 'outline)
  5366. (car (org-get-location (current-buffer) org-goto-help))
  5367. (nth 3 (org-refile-get-location "Goto: ")))))
  5368. (if selected-point
  5369. (progn
  5370. (org-mark-ring-push org-goto-start-pos)
  5371. (goto-char selected-point)
  5372. (if (or (org-invisible-p) (org-invisible-p2))
  5373. (org-show-context 'org-goto)))
  5374. (message "Quit"))))
  5375. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5376. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5377. (defvar org-goto-local-auto-isearch-map) ; defined below
  5378. (defun org-get-location (buf help)
  5379. "Let the user select a location in the Org-mode buffer BUF.
  5380. This function uses a recursive edit. It returns the selected position
  5381. or nil."
  5382. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5383. (isearch-hide-immediately nil)
  5384. (isearch-search-fun-function
  5385. (lambda () 'org-goto-local-search-headings))
  5386. (org-goto-selected-point org-goto-exit-command)
  5387. (pop-up-frames nil)
  5388. (special-display-buffer-names nil)
  5389. (special-display-regexps nil)
  5390. (special-display-function nil))
  5391. (save-excursion
  5392. (save-window-excursion
  5393. (delete-other-windows)
  5394. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5395. (switch-to-buffer
  5396. (condition-case nil
  5397. (make-indirect-buffer (current-buffer) "*org-goto*")
  5398. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5399. (with-output-to-temp-buffer "*Help*"
  5400. (princ help))
  5401. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5402. (setq buffer-read-only nil)
  5403. (let ((org-startup-truncated t)
  5404. (org-startup-folded nil)
  5405. (org-startup-align-all-tables nil))
  5406. (org-mode)
  5407. (org-overview))
  5408. (setq buffer-read-only t)
  5409. (if (and (boundp 'org-goto-start-pos)
  5410. (integer-or-marker-p org-goto-start-pos))
  5411. (let ((org-show-hierarchy-above t)
  5412. (org-show-siblings t)
  5413. (org-show-following-heading t))
  5414. (goto-char org-goto-start-pos)
  5415. (and (org-invisible-p) (org-show-context)))
  5416. (goto-char (point-min)))
  5417. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5418. (message "Select location and press RET")
  5419. (use-local-map org-goto-map)
  5420. (recursive-edit)
  5421. ))
  5422. (kill-buffer "*org-goto*")
  5423. (cons org-goto-selected-point org-goto-exit-command)))
  5424. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5425. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5426. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5427. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5428. (defun org-goto-local-search-headings (string bound noerror)
  5429. "Search and make sure that any matches are in headlines."
  5430. (catch 'return
  5431. (while (if isearch-forward
  5432. (search-forward string bound noerror)
  5433. (search-backward string bound noerror))
  5434. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5435. (and (member :headline context)
  5436. (not (member :tags context))))
  5437. (throw 'return (point))))))
  5438. (defun org-goto-local-auto-isearch ()
  5439. "Start isearch."
  5440. (interactive)
  5441. (goto-char (point-min))
  5442. (let ((keys (this-command-keys)))
  5443. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5444. (isearch-mode t)
  5445. (isearch-process-search-char (string-to-char keys)))))
  5446. (defun org-goto-ret (&optional arg)
  5447. "Finish `org-goto' by going to the new location."
  5448. (interactive "P")
  5449. (setq org-goto-selected-point (point)
  5450. org-goto-exit-command 'return)
  5451. (throw 'exit nil))
  5452. (defun org-goto-left ()
  5453. "Finish `org-goto' by going to the new location."
  5454. (interactive)
  5455. (if (org-on-heading-p)
  5456. (progn
  5457. (beginning-of-line 1)
  5458. (setq org-goto-selected-point (point)
  5459. org-goto-exit-command 'left)
  5460. (throw 'exit nil))
  5461. (error "Not on a heading")))
  5462. (defun org-goto-right ()
  5463. "Finish `org-goto' by going to the new location."
  5464. (interactive)
  5465. (if (org-on-heading-p)
  5466. (progn
  5467. (setq org-goto-selected-point (point)
  5468. org-goto-exit-command 'right)
  5469. (throw 'exit nil))
  5470. (error "Not on a heading")))
  5471. (defun org-goto-quit ()
  5472. "Finish `org-goto' without cursor motion."
  5473. (interactive)
  5474. (setq org-goto-selected-point nil)
  5475. (setq org-goto-exit-command 'quit)
  5476. (throw 'exit nil))
  5477. ;;; Indirect buffer display of subtrees
  5478. (defvar org-indirect-dedicated-frame nil
  5479. "This is the frame being used for indirect tree display.")
  5480. (defvar org-last-indirect-buffer nil)
  5481. (defun org-tree-to-indirect-buffer (&optional arg)
  5482. "Create indirect buffer and narrow it to current subtree.
  5483. With numerical prefix ARG, go up to this level and then take that tree.
  5484. If ARG is negative, go up that many levels.
  5485. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5486. indirect buffer previously made with this command, to avoid proliferation of
  5487. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5488. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5489. is kept so that you can work with several indirect buffers at the same time.
  5490. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5491. requests that a new frame be made for the new buffer, so that the dedicated
  5492. frame is not changed."
  5493. (interactive "P")
  5494. (let ((cbuf (current-buffer))
  5495. (cwin (selected-window))
  5496. (pos (point))
  5497. beg end level heading ibuf)
  5498. (save-excursion
  5499. (org-back-to-heading t)
  5500. (when (numberp arg)
  5501. (setq level (org-outline-level))
  5502. (if (< arg 0) (setq arg (+ level arg)))
  5503. (while (> (setq level (org-outline-level)) arg)
  5504. (outline-up-heading 1 t)))
  5505. (setq beg (point)
  5506. heading (org-get-heading))
  5507. (org-end-of-subtree t t)
  5508. (if (org-on-heading-p) (backward-char 1))
  5509. (setq end (point)))
  5510. (if (and (buffer-live-p org-last-indirect-buffer)
  5511. (not (eq org-indirect-buffer-display 'new-frame))
  5512. (not arg))
  5513. (kill-buffer org-last-indirect-buffer))
  5514. (setq ibuf (org-get-indirect-buffer cbuf)
  5515. org-last-indirect-buffer ibuf)
  5516. (cond
  5517. ((or (eq org-indirect-buffer-display 'new-frame)
  5518. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5519. (select-frame (make-frame))
  5520. (delete-other-windows)
  5521. (switch-to-buffer ibuf)
  5522. (org-set-frame-title heading))
  5523. ((eq org-indirect-buffer-display 'dedicated-frame)
  5524. (raise-frame
  5525. (select-frame (or (and org-indirect-dedicated-frame
  5526. (frame-live-p org-indirect-dedicated-frame)
  5527. org-indirect-dedicated-frame)
  5528. (setq org-indirect-dedicated-frame (make-frame)))))
  5529. (delete-other-windows)
  5530. (switch-to-buffer ibuf)
  5531. (org-set-frame-title (concat "Indirect: " heading)))
  5532. ((eq org-indirect-buffer-display 'current-window)
  5533. (switch-to-buffer ibuf))
  5534. ((eq org-indirect-buffer-display 'other-window)
  5535. (pop-to-buffer ibuf))
  5536. (t (error "Invalid value")))
  5537. (if (featurep 'xemacs)
  5538. (save-excursion (org-mode) (turn-on-font-lock)))
  5539. (narrow-to-region beg end)
  5540. (show-all)
  5541. (goto-char pos)
  5542. (and (window-live-p cwin) (select-window cwin))))
  5543. (defun org-get-indirect-buffer (&optional buffer)
  5544. (setq buffer (or buffer (current-buffer)))
  5545. (let ((n 1) (base (buffer-name buffer)) bname)
  5546. (while (buffer-live-p
  5547. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5548. (setq n (1+ n)))
  5549. (condition-case nil
  5550. (make-indirect-buffer buffer bname 'clone)
  5551. (error (make-indirect-buffer buffer bname)))))
  5552. (defun org-set-frame-title (title)
  5553. "Set the title of the current frame to the string TITLE."
  5554. ;; FIXME: how to name a single frame in XEmacs???
  5555. (unless (featurep 'xemacs)
  5556. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5557. ;;;; Structure editing
  5558. ;;; Inserting headlines
  5559. (defun org-previous-line-empty-p ()
  5560. (save-excursion
  5561. (and (not (bobp))
  5562. (or (beginning-of-line 0) t)
  5563. (save-match-data
  5564. (looking-at "[ \t]*$")))))
  5565. (defun org-insert-heading (&optional force-heading invisible-ok)
  5566. "Insert a new heading or item with same depth at point.
  5567. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5568. If point is at the beginning of a headline, insert a sibling before the
  5569. current headline. If point is not at the beginning, do not split the line,
  5570. but create the new headline after the current line.
  5571. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5572. This is important for non-interactive uses of the command."
  5573. (interactive "P")
  5574. (if (or (= (buffer-size) 0)
  5575. (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
  5576. (org-on-heading-p))))
  5577. (not (org-in-item-p))))
  5578. (insert "\n* ")
  5579. (when (or force-heading (not (org-insert-item)))
  5580. (let* ((empty-line-p nil)
  5581. (head (save-excursion
  5582. (condition-case nil
  5583. (progn
  5584. (org-back-to-heading invisible-ok)
  5585. (setq empty-line-p (org-previous-line-empty-p))
  5586. (match-string 0))
  5587. (error "*"))))
  5588. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5589. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5590. pos hide-previous previous-pos)
  5591. (cond
  5592. ((and (org-on-heading-p) (bolp)
  5593. (or (bobp)
  5594. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5595. ;; insert before the current line
  5596. (open-line (if blank 2 1)))
  5597. ((and (bolp)
  5598. (not org-insert-heading-respect-content)
  5599. (or (bobp)
  5600. (save-excursion
  5601. (backward-char 1) (not (org-invisible-p)))))
  5602. ;; insert right here
  5603. nil)
  5604. (t
  5605. ;; somewhere in the line
  5606. (save-excursion
  5607. (setq previous-pos (point-at-bol))
  5608. (end-of-line)
  5609. (setq hide-previous (org-invisible-p)))
  5610. (and org-insert-heading-respect-content (org-show-subtree))
  5611. (let ((split
  5612. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5613. (save-excursion
  5614. (let ((p (point)))
  5615. (goto-char (point-at-bol))
  5616. (and (looking-at org-complex-heading-regexp)
  5617. (> p (match-beginning 4)))))))
  5618. tags pos)
  5619. (cond
  5620. (org-insert-heading-respect-content
  5621. (org-end-of-subtree nil t)
  5622. (or (bolp) (newline))
  5623. (or (org-previous-line-empty-p)
  5624. (and blank (newline)))
  5625. (open-line 1))
  5626. ((org-on-heading-p)
  5627. (when hide-previous
  5628. (show-children)
  5629. (org-show-entry))
  5630. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5631. (setq tags (and (match-end 2) (match-string 2)))
  5632. (and (match-end 1)
  5633. (delete-region (match-beginning 1) (match-end 1)))
  5634. (setq pos (point-at-bol))
  5635. (or split (end-of-line 1))
  5636. (delete-horizontal-space)
  5637. (if (string-match "\\`\\*+\\'"
  5638. (buffer-substring (point-at-bol) (point)))
  5639. (insert " "))
  5640. (newline (if blank 2 1))
  5641. (when tags
  5642. (save-excursion
  5643. (goto-char pos)
  5644. (end-of-line 1)
  5645. (insert " " tags)
  5646. (org-set-tags nil 'align))))
  5647. (t
  5648. (or split (end-of-line 1))
  5649. (newline (if blank 2 1)))))))
  5650. (insert head) (just-one-space)
  5651. (setq pos (point))
  5652. (end-of-line 1)
  5653. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5654. (when (and org-insert-heading-respect-content hide-previous)
  5655. (save-excursion
  5656. (goto-char previous-pos)
  5657. (hide-subtree)))
  5658. (run-hooks 'org-insert-heading-hook)))))
  5659. (defun org-get-heading (&optional no-tags)
  5660. "Return the heading of the current entry, without the stars."
  5661. (save-excursion
  5662. (org-back-to-heading t)
  5663. (if (looking-at
  5664. (if no-tags
  5665. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5666. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5667. (match-string 1) "")))
  5668. (defun org-heading-components ()
  5669. "Return the components of the current heading.
  5670. This is a list with the following elements:
  5671. - the level as an integer
  5672. - the reduced level, different if `org-odd-levels-only' is set.
  5673. - the TODO keyword, or nil
  5674. - the priority character, like ?A, or nil if no priority is given
  5675. - the headline text itself, or the tags string if no headline text
  5676. - the tags string, or nil."
  5677. (save-excursion
  5678. (org-back-to-heading t)
  5679. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  5680. (list (length (match-string 1))
  5681. (org-reduced-level (length (match-string 1)))
  5682. (org-match-string-no-properties 2)
  5683. (and (match-end 3) (aref (match-string 3) 2))
  5684. (org-match-string-no-properties 4)
  5685. (org-match-string-no-properties 5)))))
  5686. (defun org-get-entry ()
  5687. "Get the entry text, after heading, entire subtree."
  5688. (save-excursion
  5689. (org-back-to-heading t)
  5690. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5691. (defun org-insert-heading-after-current ()
  5692. "Insert a new heading with same level as current, after current subtree."
  5693. (interactive)
  5694. (org-back-to-heading)
  5695. (org-insert-heading)
  5696. (org-move-subtree-down)
  5697. (end-of-line 1))
  5698. (defun org-insert-heading-respect-content ()
  5699. (interactive)
  5700. (let ((org-insert-heading-respect-content t))
  5701. (org-insert-heading t)))
  5702. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5703. (interactive "P")
  5704. (let ((org-insert-heading-respect-content t))
  5705. (org-insert-todo-heading force-state t)))
  5706. (defun org-insert-todo-heading (arg &optional force-heading)
  5707. "Insert a new heading with the same level and TODO state as current heading.
  5708. If the heading has no TODO state, or if the state is DONE, use the first
  5709. state (TODO by default). Also with prefix arg, force first state."
  5710. (interactive "P")
  5711. (when (or force-heading (not (org-insert-item 'checkbox)))
  5712. (org-insert-heading force-heading)
  5713. (save-excursion
  5714. (org-back-to-heading)
  5715. (outline-previous-heading)
  5716. (looking-at org-todo-line-regexp))
  5717. (let*
  5718. ((new-mark-x
  5719. (if (or arg
  5720. (not (match-beginning 2))
  5721. (member (match-string 2) org-done-keywords))
  5722. (car org-todo-keywords-1)
  5723. (match-string 2)))
  5724. (new-mark
  5725. (or
  5726. (run-hook-with-args-until-success
  5727. 'org-todo-get-default-hook new-mark-x nil)
  5728. new-mark-x)))
  5729. (beginning-of-line 1)
  5730. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5731. (if org-treat-insert-todo-heading-as-state-change
  5732. (org-todo new-mark)
  5733. (insert new-mark " "))))
  5734. (when org-provide-todo-statistics
  5735. (org-update-parent-todo-statistics))))
  5736. (defun org-insert-subheading (arg)
  5737. "Insert a new subheading and demote it.
  5738. Works for outline headings and for plain lists alike."
  5739. (interactive "P")
  5740. (org-insert-heading arg)
  5741. (cond
  5742. ((org-on-heading-p) (org-do-demote))
  5743. ((org-at-item-p) (org-indent-item 1))))
  5744. (defun org-insert-todo-subheading (arg)
  5745. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5746. Works for outline headings and for plain lists alike."
  5747. (interactive "P")
  5748. (org-insert-todo-heading arg)
  5749. (cond
  5750. ((org-on-heading-p) (org-do-demote))
  5751. ((org-at-item-p) (org-indent-item 1))))
  5752. ;;; Promotion and Demotion
  5753. (defvar org-after-demote-entry-hook nil
  5754. "Hook run after an entry has been demoted.
  5755. The cursor will be at the beginning of the entry.
  5756. When a subtree is being demoted, the hook will be called for each node.")
  5757. (defvar org-after-promote-entry-hook nil
  5758. "Hook run after an entry has been promoted.
  5759. The cursor will be at the beginning of the entry.
  5760. When a subtree is being promoted, the hook will be called for each node.")
  5761. (defun org-promote-subtree ()
  5762. "Promote the entire subtree.
  5763. See also `org-promote'."
  5764. (interactive)
  5765. (save-excursion
  5766. (org-map-tree 'org-promote))
  5767. (org-fix-position-after-promote))
  5768. (defun org-demote-subtree ()
  5769. "Demote the entire subtree. See `org-demote'.
  5770. See also `org-promote'."
  5771. (interactive)
  5772. (save-excursion
  5773. (org-map-tree 'org-demote))
  5774. (org-fix-position-after-promote))
  5775. (defun org-do-promote ()
  5776. "Promote the current heading higher up the tree.
  5777. If the region is active in `transient-mark-mode', promote all headings
  5778. in the region."
  5779. (interactive)
  5780. (save-excursion
  5781. (if (org-region-active-p)
  5782. (org-map-region 'org-promote (region-beginning) (region-end))
  5783. (org-promote)))
  5784. (org-fix-position-after-promote))
  5785. (defun org-do-demote ()
  5786. "Demote the current heading lower down the tree.
  5787. If the region is active in `transient-mark-mode', demote all headings
  5788. in the region."
  5789. (interactive)
  5790. (save-excursion
  5791. (if (org-region-active-p)
  5792. (org-map-region 'org-demote (region-beginning) (region-end))
  5793. (org-demote)))
  5794. (org-fix-position-after-promote))
  5795. (defun org-fix-position-after-promote ()
  5796. "Make sure that after pro/demotion cursor position is right."
  5797. (let ((pos (point)))
  5798. (when (save-excursion
  5799. (beginning-of-line 1)
  5800. (looking-at org-todo-line-regexp)
  5801. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5802. (cond ((eobp) (insert " "))
  5803. ((eolp) (insert " "))
  5804. ((equal (char-after) ?\ ) (forward-char 1))))))
  5805. (defun org-current-level ()
  5806. "Return the level of the current entry, or nil if before the first headline.
  5807. The level is the number of stars at the beginning of the headline."
  5808. (save-excursion
  5809. (condition-case nil
  5810. (progn
  5811. (org-back-to-heading t)
  5812. (funcall outline-level))
  5813. (error nil))))
  5814. (defun org-get-previous-line-level ()
  5815. "Return the outline depth of the last headline before the current line.
  5816. Returns 0 for the first headline in the buffer, and nil if before the
  5817. first headline."
  5818. (let ((current-level (org-current-level))
  5819. (prev-level (when (> (line-number-at-pos) 1)
  5820. (save-excursion
  5821. (beginning-of-line 0)
  5822. (org-current-level)))))
  5823. (cond ((null current-level) nil) ; Before first headline
  5824. ((null prev-level) 0) ; At first headline
  5825. (prev-level))))
  5826. (defun org-reduced-level (l)
  5827. "Compute the effective level of a heading.
  5828. This takes into account the setting of `org-odd-levels-only'."
  5829. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5830. (defun org-level-increment ()
  5831. "Return the number of stars that will be added or removed at a
  5832. time to headlines when structure editing, based on the value of
  5833. `org-odd-levels-only'."
  5834. (if org-odd-levels-only 2 1))
  5835. (defun org-get-valid-level (level &optional change)
  5836. "Rectify a level change under the influence of `org-odd-levels-only'
  5837. LEVEL is a current level, CHANGE is by how much the level should be
  5838. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5839. even level numbers will become the next higher odd number."
  5840. (if org-odd-levels-only
  5841. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5842. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5843. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5844. (max 1 (+ level (or change 0)))))
  5845. (if (boundp 'define-obsolete-function-alias)
  5846. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5847. (define-obsolete-function-alias 'org-get-legal-level
  5848. 'org-get-valid-level)
  5849. (define-obsolete-function-alias 'org-get-legal-level
  5850. 'org-get-valid-level "23.1")))
  5851. (defun org-promote ()
  5852. "Promote the current heading higher up the tree.
  5853. If the region is active in `transient-mark-mode', promote all headings
  5854. in the region."
  5855. (org-back-to-heading t)
  5856. (let* ((level (save-match-data (funcall outline-level)))
  5857. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5858. (diff (abs (- level (length up-head) -1))))
  5859. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5860. (replace-match up-head nil t)
  5861. ;; Fixup tag positioning
  5862. (and org-auto-align-tags (org-set-tags nil t))
  5863. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5864. (run-hooks 'org-after-promote-entry-hook)))
  5865. (defun org-demote ()
  5866. "Demote the current heading lower down the tree.
  5867. If the region is active in `transient-mark-mode', demote all headings
  5868. in the region."
  5869. (org-back-to-heading t)
  5870. (let* ((level (save-match-data (funcall outline-level)))
  5871. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5872. (diff (abs (- level (length down-head) -1))))
  5873. (replace-match down-head nil t)
  5874. ;; Fixup tag positioning
  5875. (and org-auto-align-tags (org-set-tags nil t))
  5876. (if org-adapt-indentation (org-fixup-indentation diff))
  5877. (run-hooks 'org-after-demote-entry-hook)))
  5878. (defun org-cycle-level ()
  5879. "Cycle the level of an empty headline through possible states.
  5880. This goes first to child, then to parent, level, then up the hierarchy.
  5881. After top level, it switches back to sibling level."
  5882. (interactive)
  5883. (let ((org-adapt-indentation nil))
  5884. (when (org-point-at-end-of-empty-headline)
  5885. (setq this-command 'org-cycle-level) ; Only needed for caching
  5886. (let ((cur-level (org-current-level))
  5887. (prev-level (org-get-previous-line-level)))
  5888. (cond
  5889. ;; If first headline in file, promote to top-level.
  5890. ((= prev-level 0)
  5891. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5892. do (org-do-promote)))
  5893. ;; If same level as prev, demote one.
  5894. ((= prev-level cur-level)
  5895. (org-do-demote))
  5896. ;; If parent is top-level, promote to top level if not already.
  5897. ((= prev-level 1)
  5898. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5899. do (org-do-promote)))
  5900. ;; If top-level, return to prev-level.
  5901. ((= cur-level 1)
  5902. (loop repeat (/ (- prev-level 1) (org-level-increment))
  5903. do (org-do-demote)))
  5904. ;; If less than prev-level, promote one.
  5905. ((< cur-level prev-level)
  5906. (org-do-promote))
  5907. ;; If deeper than prev-level, promote until higher than
  5908. ;; prev-level.
  5909. ((> cur-level prev-level)
  5910. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  5911. do (org-do-promote))))
  5912. t))))
  5913. (defun org-map-tree (fun)
  5914. "Call FUN for every heading underneath the current one."
  5915. (org-back-to-heading)
  5916. (let ((level (funcall outline-level)))
  5917. (save-excursion
  5918. (funcall fun)
  5919. (while (and (progn
  5920. (outline-next-heading)
  5921. (> (funcall outline-level) level))
  5922. (not (eobp)))
  5923. (funcall fun)))))
  5924. (defun org-map-region (fun beg end)
  5925. "Call FUN for every heading between BEG and END."
  5926. (let ((org-ignore-region t))
  5927. (save-excursion
  5928. (setq end (copy-marker end))
  5929. (goto-char beg)
  5930. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5931. (< (point) end))
  5932. (funcall fun))
  5933. (while (and (progn
  5934. (outline-next-heading)
  5935. (< (point) end))
  5936. (not (eobp)))
  5937. (funcall fun)))))
  5938. (defun org-fixup-indentation (diff)
  5939. "Change the indentation in the current entry by DIFF
  5940. However, if any line in the current entry has no indentation, or if it
  5941. would end up with no indentation after the change, nothing at all is done."
  5942. (save-excursion
  5943. (let ((end (save-excursion (outline-next-heading)
  5944. (point-marker)))
  5945. (prohibit (if (> diff 0)
  5946. "^\\S-"
  5947. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5948. col)
  5949. (unless (save-excursion (end-of-line 1)
  5950. (re-search-forward prohibit end t))
  5951. (while (and (< (point) end)
  5952. (re-search-forward "^[ \t]+" end t))
  5953. (goto-char (match-end 0))
  5954. (setq col (current-column))
  5955. (if (< diff 0) (replace-match ""))
  5956. (org-indent-to-column (+ diff col))))
  5957. (move-marker end nil))))
  5958. (defun org-convert-to-odd-levels ()
  5959. "Convert an org-mode file with all levels allowed to one with odd levels.
  5960. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5961. level 5 etc."
  5962. (interactive)
  5963. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5964. (let ((outline-regexp org-outline-regexp)
  5965. (outline-level 'org-outline-level)
  5966. (org-odd-levels-only nil) n)
  5967. (save-excursion
  5968. (goto-char (point-min))
  5969. (while (re-search-forward "^\\*\\*+ " nil t)
  5970. (setq n (- (length (match-string 0)) 2))
  5971. (while (>= (setq n (1- n)) 0)
  5972. (org-demote))
  5973. (end-of-line 1))))))
  5974. (defun org-convert-to-oddeven-levels ()
  5975. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5976. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5977. section with an even level, conversion would destroy the structure of the file. An error
  5978. is signaled in this case."
  5979. (interactive)
  5980. (goto-char (point-min))
  5981. ;; First check if there are no even levels
  5982. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5983. (org-show-context t)
  5984. (error "Not all levels are odd in this file. Conversion not possible"))
  5985. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5986. (let ((outline-regexp org-outline-regexp)
  5987. (outline-level 'org-outline-level)
  5988. (org-odd-levels-only nil) n)
  5989. (save-excursion
  5990. (goto-char (point-min))
  5991. (while (re-search-forward "^\\*\\*+ " nil t)
  5992. (setq n (/ (1- (length (match-string 0))) 2))
  5993. (while (>= (setq n (1- n)) 0)
  5994. (org-promote))
  5995. (end-of-line 1))))))
  5996. (defun org-tr-level (n)
  5997. "Make N odd if required."
  5998. (if org-odd-levels-only (1+ (/ n 2)) n))
  5999. ;;; Vertical tree motion, cutting and pasting of subtrees
  6000. (defun org-move-subtree-up (&optional arg)
  6001. "Move the current subtree up past ARG headlines of the same level."
  6002. (interactive "p")
  6003. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6004. (defun org-move-subtree-down (&optional arg)
  6005. "Move the current subtree down past ARG headlines of the same level."
  6006. (interactive "p")
  6007. (setq arg (prefix-numeric-value arg))
  6008. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6009. 'org-get-last-sibling))
  6010. (ins-point (make-marker))
  6011. (cnt (abs arg))
  6012. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6013. ;; Select the tree
  6014. (org-back-to-heading)
  6015. (setq beg0 (point))
  6016. (save-excursion
  6017. (setq ne-beg (org-back-over-empty-lines))
  6018. (setq beg (point)))
  6019. (save-match-data
  6020. (save-excursion (outline-end-of-heading)
  6021. (setq folded (org-invisible-p)))
  6022. (outline-end-of-subtree))
  6023. (outline-next-heading)
  6024. (setq ne-end (org-back-over-empty-lines))
  6025. (setq end (point))
  6026. (goto-char beg0)
  6027. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6028. ;; include less whitespace
  6029. (save-excursion
  6030. (goto-char beg)
  6031. (forward-line (- ne-beg ne-end))
  6032. (setq beg (point))))
  6033. ;; Find insertion point, with error handling
  6034. (while (> cnt 0)
  6035. (or (and (funcall movfunc) (looking-at outline-regexp))
  6036. (progn (goto-char beg0)
  6037. (error "Cannot move past superior level or buffer limit")))
  6038. (setq cnt (1- cnt)))
  6039. (if (> arg 0)
  6040. ;; Moving forward - still need to move over subtree
  6041. (progn (org-end-of-subtree t t)
  6042. (save-excursion
  6043. (org-back-over-empty-lines)
  6044. (or (bolp) (newline)))))
  6045. (setq ne-ins (org-back-over-empty-lines))
  6046. (move-marker ins-point (point))
  6047. (setq txt (buffer-substring beg end))
  6048. (org-save-markers-in-region beg end)
  6049. (delete-region beg end)
  6050. (org-remove-empty-overlays-at beg)
  6051. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6052. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6053. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6054. (let ((bbb (point)))
  6055. (insert-before-markers txt)
  6056. (org-reinstall-markers-in-region bbb)
  6057. (move-marker ins-point bbb))
  6058. (or (bolp) (insert "\n"))
  6059. (setq ins-end (point))
  6060. (goto-char ins-point)
  6061. (org-skip-whitespace)
  6062. (when (and (< arg 0)
  6063. (org-first-sibling-p)
  6064. (> ne-ins ne-beg))
  6065. ;; Move whitespace back to beginning
  6066. (save-excursion
  6067. (goto-char ins-end)
  6068. (let ((kill-whole-line t))
  6069. (kill-line (- ne-ins ne-beg)) (point)))
  6070. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6071. (move-marker ins-point nil)
  6072. (if folded
  6073. (hide-subtree)
  6074. (org-show-entry)
  6075. (show-children)
  6076. (org-cycle-hide-drawers 'children))
  6077. (org-clean-visibility-after-subtree-move)))
  6078. (defvar org-subtree-clip ""
  6079. "Clipboard for cut and paste of subtrees.
  6080. This is actually only a copy of the kill, because we use the normal kill
  6081. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6082. (defvar org-subtree-clip-folded nil
  6083. "Was the last copied subtree folded?
  6084. This is used to fold the tree back after pasting.")
  6085. (defun org-cut-subtree (&optional n)
  6086. "Cut the current subtree into the clipboard.
  6087. With prefix arg N, cut this many sequential subtrees.
  6088. This is a short-hand for marking the subtree and then cutting it."
  6089. (interactive "p")
  6090. (org-copy-subtree n 'cut))
  6091. (defun org-copy-subtree (&optional n cut force-store-markers)
  6092. "Cut the current subtree into the clipboard.
  6093. With prefix arg N, cut this many sequential subtrees.
  6094. This is a short-hand for marking the subtree and then copying it.
  6095. If CUT is non-nil, actually cut the subtree.
  6096. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6097. of some markers in the region, even if CUT is non-nil. This is
  6098. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6099. (interactive "p")
  6100. (let (beg end folded (beg0 (point)))
  6101. (if (interactive-p)
  6102. (org-back-to-heading nil) ; take what looks like a subtree
  6103. (org-back-to-heading t)) ; take what is really there
  6104. (org-back-over-empty-lines)
  6105. (setq beg (point))
  6106. (skip-chars-forward " \t\r\n")
  6107. (save-match-data
  6108. (save-excursion (outline-end-of-heading)
  6109. (setq folded (org-invisible-p)))
  6110. (condition-case nil
  6111. (org-forward-same-level (1- n) t)
  6112. (error nil))
  6113. (org-end-of-subtree t t))
  6114. (org-back-over-empty-lines)
  6115. (setq end (point))
  6116. (goto-char beg0)
  6117. (when (> end beg)
  6118. (setq org-subtree-clip-folded folded)
  6119. (when (or cut force-store-markers)
  6120. (org-save-markers-in-region beg end))
  6121. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6122. (setq org-subtree-clip (current-kill 0))
  6123. (message "%s: Subtree(s) with %d characters"
  6124. (if cut "Cut" "Copied")
  6125. (length org-subtree-clip)))))
  6126. (defun org-paste-subtree (&optional level tree for-yank)
  6127. "Paste the clipboard as a subtree, with modification of headline level.
  6128. The entire subtree is promoted or demoted in order to match a new headline
  6129. level.
  6130. If the cursor is at the beginning of a headline, the same level as
  6131. that headline is used to paste the tree
  6132. If not, the new level is derived from the *visible* headings
  6133. before and after the insertion point, and taken to be the inferior headline
  6134. level of the two. So if the previous visible heading is level 3 and the
  6135. next is level 4 (or vice versa), level 4 will be used for insertion.
  6136. This makes sure that the subtree remains an independent subtree and does
  6137. not swallow low level entries.
  6138. You can also force a different level, either by using a numeric prefix
  6139. argument, or by inserting the heading marker by hand. For example, if the
  6140. cursor is after \"*****\", then the tree will be shifted to level 5.
  6141. If optional TREE is given, use this text instead of the kill ring.
  6142. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6143. move back over whitespace before inserting, and move point to the end of
  6144. the inserted text when done."
  6145. (interactive "P")
  6146. (setq tree (or tree (and kill-ring (current-kill 0))))
  6147. (unless (org-kill-is-subtree-p tree)
  6148. (error "%s"
  6149. (substitute-command-keys
  6150. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6151. (let* ((visp (not (org-invisible-p)))
  6152. (txt tree)
  6153. (^re (concat "^\\(" outline-regexp "\\)"))
  6154. (re (concat "\\(" outline-regexp "\\)"))
  6155. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6156. (old-level (if (string-match ^re txt)
  6157. (- (match-end 0) (match-beginning 0) 1)
  6158. -1))
  6159. (force-level (cond (level (prefix-numeric-value level))
  6160. ((and (looking-at "[ \t]*$")
  6161. (string-match
  6162. ^re_ (buffer-substring
  6163. (point-at-bol) (point))))
  6164. (- (match-end 1) (match-beginning 1)))
  6165. ((and (bolp)
  6166. (looking-at org-outline-regexp))
  6167. (- (match-end 0) (point) 1))
  6168. (t nil)))
  6169. (previous-level (save-excursion
  6170. (condition-case nil
  6171. (progn
  6172. (outline-previous-visible-heading 1)
  6173. (if (looking-at re)
  6174. (- (match-end 0) (match-beginning 0) 1)
  6175. 1))
  6176. (error 1))))
  6177. (next-level (save-excursion
  6178. (condition-case nil
  6179. (progn
  6180. (or (looking-at outline-regexp)
  6181. (outline-next-visible-heading 1))
  6182. (if (looking-at re)
  6183. (- (match-end 0) (match-beginning 0) 1)
  6184. 1))
  6185. (error 1))))
  6186. (new-level (or force-level (max previous-level next-level)))
  6187. (shift (if (or (= old-level -1)
  6188. (= new-level -1)
  6189. (= old-level new-level))
  6190. 0
  6191. (- new-level old-level)))
  6192. (delta (if (> shift 0) -1 1))
  6193. (func (if (> shift 0) 'org-demote 'org-promote))
  6194. (org-odd-levels-only nil)
  6195. beg end newend)
  6196. ;; Remove the forced level indicator
  6197. (if force-level
  6198. (delete-region (point-at-bol) (point)))
  6199. ;; Paste
  6200. (beginning-of-line 1)
  6201. (unless for-yank (org-back-over-empty-lines))
  6202. (setq beg (point))
  6203. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6204. (insert-before-markers txt)
  6205. (unless (string-match "\n\\'" txt) (insert "\n"))
  6206. (setq newend (point))
  6207. (org-reinstall-markers-in-region beg)
  6208. (setq end (point))
  6209. (goto-char beg)
  6210. (skip-chars-forward " \t\n\r")
  6211. (setq beg (point))
  6212. (if (and (org-invisible-p) visp)
  6213. (save-excursion (outline-show-heading)))
  6214. ;; Shift if necessary
  6215. (unless (= shift 0)
  6216. (save-restriction
  6217. (narrow-to-region beg end)
  6218. (while (not (= shift 0))
  6219. (org-map-region func (point-min) (point-max))
  6220. (setq shift (+ delta shift)))
  6221. (goto-char (point-min))
  6222. (setq newend (point-max))))
  6223. (when (or (interactive-p) for-yank)
  6224. (message "Clipboard pasted as level %d subtree" new-level))
  6225. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6226. kill-ring
  6227. (eq org-subtree-clip (current-kill 0))
  6228. org-subtree-clip-folded)
  6229. ;; The tree was folded before it was killed/copied
  6230. (hide-subtree))
  6231. (and for-yank (goto-char newend))))
  6232. (defun org-kill-is-subtree-p (&optional txt)
  6233. "Check if the current kill is an outline subtree, or a set of trees.
  6234. Returns nil if kill does not start with a headline, or if the first
  6235. headline level is not the largest headline level in the tree.
  6236. So this will actually accept several entries of equal levels as well,
  6237. which is OK for `org-paste-subtree'.
  6238. If optional TXT is given, check this string instead of the current kill."
  6239. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6240. (start-level (and kill
  6241. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6242. org-outline-regexp "\\)")
  6243. kill)
  6244. (- (match-end 2) (match-beginning 2) 1)))
  6245. (re (concat "^" org-outline-regexp))
  6246. (start (1+ (or (match-beginning 2) -1))))
  6247. (if (not start-level)
  6248. (progn
  6249. nil) ;; does not even start with a heading
  6250. (catch 'exit
  6251. (while (setq start (string-match re kill (1+ start)))
  6252. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6253. (throw 'exit nil)))
  6254. t))))
  6255. (defvar org-markers-to-move nil
  6256. "Markers that should be moved with a cut-and-paste operation.
  6257. Those markers are stored together with their positions relative to
  6258. the start of the region.")
  6259. (defun org-save-markers-in-region (beg end)
  6260. "Check markers in region.
  6261. If these markers are between BEG and END, record their position relative
  6262. to BEG, so that after moving the block of text, we can put the markers back
  6263. into place.
  6264. This function gets called just before an entry or tree gets cut from the
  6265. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6266. called immediately, to move the markers with the entries."
  6267. (setq org-markers-to-move nil)
  6268. (when (featurep 'org-clock)
  6269. (org-clock-save-markers-for-cut-and-paste beg end))
  6270. (when (featurep 'org-agenda)
  6271. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6272. (defun org-check-and-save-marker (marker beg end)
  6273. "Check if MARKER is between BEG and END.
  6274. If yes, remember the marker and the distance to BEG."
  6275. (when (and (marker-buffer marker)
  6276. (equal (marker-buffer marker) (current-buffer)))
  6277. (if (and (>= marker beg) (< marker end))
  6278. (push (cons marker (- marker beg)) org-markers-to-move))))
  6279. (defun org-reinstall-markers-in-region (beg)
  6280. "Move all remembered markers to their position relative to BEG."
  6281. (mapc (lambda (x)
  6282. (move-marker (car x) (+ beg (cdr x))))
  6283. org-markers-to-move)
  6284. (setq org-markers-to-move nil))
  6285. (defun org-narrow-to-subtree ()
  6286. "Narrow buffer to the current subtree."
  6287. (interactive)
  6288. (save-excursion
  6289. (save-match-data
  6290. (narrow-to-region
  6291. (progn (org-back-to-heading t) (point))
  6292. (progn (org-end-of-subtree t t)
  6293. (if (org-on-heading-p) (backward-char 1))
  6294. (point))))))
  6295. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6296. "Clone the task (subtree) at point N times.
  6297. The clones will be inserted as siblings.
  6298. In interactive use, the user will be prompted for the number of clones
  6299. to be produced, and for a time SHIFT, which may be a repeater as used
  6300. in time stamps, for example `+3d'.
  6301. When a valid repeater is given and the entry contains any time stamps,
  6302. the clones will become a sequence in time, with time stamps in the
  6303. subtree shifted for each clone produced. If SHIFT is nil or the
  6304. empty string, time stamps will be left alone.
  6305. If the original subtree did contain time stamps with a repeater,
  6306. the following will happen:
  6307. - the repeater will be removed in each clone
  6308. - an additional clone will be produced, with the current, unshifted
  6309. date(s) in the entry.
  6310. - the original entry will be placed *after* all the clones, with
  6311. repeater intact.
  6312. - the start days in the repeater in the original entry will be shifted
  6313. to past the last clone.
  6314. I this way you can spell out a number of instances of a repeating task,
  6315. and still retain the repeater to cover future instances of the task."
  6316. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6317. (let (beg end template task
  6318. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6319. (if (not (and (integerp n) (> n 0)))
  6320. (error "Invalid number of replications %s" n))
  6321. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6322. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6323. shift)))
  6324. (error "Invalid shift specification %s" shift))
  6325. (when doshift
  6326. (setq shift-n (string-to-number (match-string 1 shift))
  6327. shift-what (cdr (assoc (match-string 2 shift)
  6328. '(("d" . day) ("w" . week)
  6329. ("m" . month) ("y" . year))))))
  6330. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6331. (setq nmin 1 nmax n)
  6332. (org-back-to-heading t)
  6333. (setq beg (point))
  6334. (org-end-of-subtree t t)
  6335. (or (bolp) (insert "\n"))
  6336. (setq end (point))
  6337. (setq template (buffer-substring beg end))
  6338. (when (and doshift
  6339. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6340. (delete-region beg end)
  6341. (setq end beg)
  6342. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6343. (goto-char end)
  6344. (loop for n from nmin to nmax do
  6345. (if (not doshift)
  6346. (setq task template)
  6347. (with-temp-buffer
  6348. (insert template)
  6349. (org-mode)
  6350. (goto-char (point-min))
  6351. (while (re-search-forward org-ts-regexp-both nil t)
  6352. (org-timestamp-change (* n shift-n) shift-what))
  6353. (unless (= n n-no-remove)
  6354. (goto-char (point-min))
  6355. (while (re-search-forward org-ts-regexp nil t)
  6356. (save-excursion
  6357. (goto-char (match-beginning 0))
  6358. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6359. (delete-region (match-beginning 1) (match-end 1))))))
  6360. (setq task (buffer-string))))
  6361. (insert task))
  6362. (goto-char beg)))
  6363. ;;; Outline Sorting
  6364. (defun org-sort (with-case)
  6365. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6366. Optional argument WITH-CASE means sort case-sensitively.
  6367. With a double prefix argument, also remove duplicate entries."
  6368. (interactive "P")
  6369. (if (org-at-table-p)
  6370. (org-call-with-arg 'org-table-sort-lines with-case)
  6371. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6372. (defun org-sort-remove-invisible (s)
  6373. (remove-text-properties 0 (length s) org-rm-props s)
  6374. (while (string-match org-bracket-link-regexp s)
  6375. (setq s (replace-match (if (match-end 2)
  6376. (match-string 3 s)
  6377. (match-string 1 s)) t t s)))
  6378. s)
  6379. (defvar org-priority-regexp) ; defined later in the file
  6380. (defvar org-after-sorting-entries-or-items-hook nil
  6381. "Hook that is run after a bunch of entries or items have been sorted.
  6382. When children are sorted, the cursor is in the parent line when this
  6383. hook gets called. When a region or a plain list is sorted, the cursor
  6384. will be in the first entry of the sorted region/list.")
  6385. (defun org-sort-entries-or-items
  6386. (&optional with-case sorting-type getkey-func compare-func property)
  6387. "Sort entries on a certain level of an outline tree, or plain list items.
  6388. If there is an active region, the entries in the region are sorted.
  6389. Else, if the cursor is before the first entry, sort the top-level items.
  6390. Else, the children of the entry at point are sorted.
  6391. If the cursor is at the first item in a plain list, the list items will be
  6392. sorted.
  6393. Sorting can be alphabetically, numerically, by date/time as given by
  6394. a time stamp, by a property or by priority.
  6395. The command prompts for the sorting type unless it has been given to the
  6396. function through the SORTING-TYPE argument, which needs to a character,
  6397. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6398. precise meaning of each character:
  6399. n Numerically, by converting the beginning of the entry/item to a number.
  6400. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6401. t By date/time, either the first active time stamp in the entry, or, if
  6402. none exist, by the first inactive one.
  6403. In items, only the first line will be checked.
  6404. s By the scheduled date/time.
  6405. d By deadline date/time.
  6406. c By creation time, which is assumed to be the first inactive time stamp
  6407. at the beginning of a line.
  6408. p By priority according to the cookie.
  6409. r By the value of a property.
  6410. Capital letters will reverse the sort order.
  6411. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6412. called with point at the beginning of the record. It must return either
  6413. a string or a number that should serve as the sorting key for that record.
  6414. Comparing entries ignores case by default. However, with an optional argument
  6415. WITH-CASE, the sorting considers case as well."
  6416. (interactive "P")
  6417. (let ((case-func (if with-case 'identity 'downcase))
  6418. start beg end stars re re2
  6419. txt what tmp plain-list-p)
  6420. ;; Find beginning and end of region to sort
  6421. (cond
  6422. ((org-region-active-p)
  6423. ;; we will sort the region
  6424. (setq end (region-end)
  6425. what "region")
  6426. (goto-char (region-beginning))
  6427. (if (not (org-on-heading-p)) (outline-next-heading))
  6428. (setq start (point)))
  6429. ((org-at-item-p)
  6430. ;; we will sort this plain list
  6431. (org-beginning-of-item-list) (setq start (point))
  6432. (org-end-of-item-list)
  6433. (or (bolp) (insert "\n"))
  6434. (setq end (point))
  6435. (goto-char start)
  6436. (setq plain-list-p t
  6437. what "plain list"))
  6438. ((or (org-on-heading-p)
  6439. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6440. ;; we will sort the children of the current headline
  6441. (org-back-to-heading)
  6442. (setq start (point)
  6443. end (progn (org-end-of-subtree t t)
  6444. (or (bolp) (insert "\n"))
  6445. (org-back-over-empty-lines)
  6446. (point))
  6447. what "children")
  6448. (goto-char start)
  6449. (show-subtree)
  6450. (outline-next-heading))
  6451. (t
  6452. ;; we will sort the top-level entries in this file
  6453. (goto-char (point-min))
  6454. (or (org-on-heading-p) (outline-next-heading))
  6455. (setq start (point))
  6456. (goto-char (point-max))
  6457. (beginning-of-line 1)
  6458. (when (looking-at ".*?\\S-")
  6459. ;; File ends in a non-white line
  6460. (end-of-line 1)
  6461. (insert "\n"))
  6462. (setq end (point-max))
  6463. (setq what "top-level")
  6464. (goto-char start)
  6465. (show-all)))
  6466. (setq beg (point))
  6467. (if (>= beg end) (error "Nothing to sort"))
  6468. (unless plain-list-p
  6469. (looking-at "\\(\\*+\\)")
  6470. (setq stars (match-string 1)
  6471. re (concat "^" (regexp-quote stars) " +")
  6472. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6473. txt (buffer-substring beg end))
  6474. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6475. (if (and (not (equal stars "*")) (string-match re2 txt))
  6476. (error "Region to sort contains a level above the first entry")))
  6477. (unless sorting-type
  6478. (message
  6479. (if plain-list-p
  6480. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6481. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6482. [t]ime [s]cheduled [d]eadline [c]reated
  6483. A/N/T/S/D/C/P/O/F means reversed:")
  6484. what)
  6485. (setq sorting-type (read-char-exclusive))
  6486. (and (= (downcase sorting-type) ?f)
  6487. (setq getkey-func
  6488. (org-icompleting-read "Sort using function: "
  6489. obarray 'fboundp t nil nil))
  6490. (setq getkey-func (intern getkey-func)))
  6491. (and (= (downcase sorting-type) ?r)
  6492. (setq property
  6493. (org-icompleting-read "Property: "
  6494. (mapcar 'list (org-buffer-property-keys t))
  6495. nil t))))
  6496. (message "Sorting entries...")
  6497. (save-restriction
  6498. (narrow-to-region start end)
  6499. (let ((dcst (downcase sorting-type))
  6500. (case-fold-search nil)
  6501. (now (current-time)))
  6502. (sort-subr
  6503. (/= dcst sorting-type)
  6504. ;; This function moves to the beginning character of the "record" to
  6505. ;; be sorted.
  6506. (if plain-list-p
  6507. (lambda nil
  6508. (if (org-at-item-p) t (goto-char (point-max))))
  6509. (lambda nil
  6510. (if (re-search-forward re nil t)
  6511. (goto-char (match-beginning 0))
  6512. (goto-char (point-max)))))
  6513. ;; This function moves to the last character of the "record" being
  6514. ;; sorted.
  6515. (if plain-list-p
  6516. 'org-end-of-item
  6517. (lambda nil
  6518. (save-match-data
  6519. (condition-case nil
  6520. (outline-forward-same-level 1)
  6521. (error
  6522. (goto-char (point-max)))))))
  6523. ;; This function returns the value that gets sorted against.
  6524. (if plain-list-p
  6525. (lambda nil
  6526. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6527. (cond
  6528. ((= dcst ?n)
  6529. (string-to-number (buffer-substring (match-end 0)
  6530. (point-at-eol))))
  6531. ((= dcst ?a)
  6532. (buffer-substring (match-end 0) (point-at-eol)))
  6533. ((= dcst ?t)
  6534. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6535. (re-search-forward org-ts-regexp-both
  6536. (point-at-eol) t))
  6537. (org-time-string-to-seconds (match-string 0))
  6538. (org-float-time now)))
  6539. ((= dcst ?f)
  6540. (if getkey-func
  6541. (progn
  6542. (setq tmp (funcall getkey-func))
  6543. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6544. tmp)
  6545. (error "Invalid key function `%s'" getkey-func)))
  6546. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6547. (lambda nil
  6548. (cond
  6549. ((= dcst ?n)
  6550. (if (looking-at org-complex-heading-regexp)
  6551. (string-to-number (match-string 4))
  6552. nil))
  6553. ((= dcst ?a)
  6554. (if (looking-at org-complex-heading-regexp)
  6555. (funcall case-func (match-string 4))
  6556. nil))
  6557. ((= dcst ?t)
  6558. (let ((end (save-excursion (outline-next-heading) (point))))
  6559. (if (or (re-search-forward org-ts-regexp end t)
  6560. (re-search-forward org-ts-regexp-both end t))
  6561. (org-time-string-to-seconds (match-string 0))
  6562. (org-float-time now))))
  6563. ((= dcst ?c)
  6564. (let ((end (save-excursion (outline-next-heading) (point))))
  6565. (if (re-search-forward
  6566. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6567. end t)
  6568. (org-time-string-to-seconds (match-string 0))
  6569. (org-float-time now))))
  6570. ((= dcst ?s)
  6571. (let ((end (save-excursion (outline-next-heading) (point))))
  6572. (if (re-search-forward org-scheduled-time-regexp end t)
  6573. (org-time-string-to-seconds (match-string 1))
  6574. (org-float-time now))))
  6575. ((= dcst ?d)
  6576. (let ((end (save-excursion (outline-next-heading) (point))))
  6577. (if (re-search-forward org-deadline-time-regexp end t)
  6578. (org-time-string-to-seconds (match-string 1))
  6579. (org-float-time now))))
  6580. ((= dcst ?p)
  6581. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6582. (string-to-char (match-string 2))
  6583. org-default-priority))
  6584. ((= dcst ?r)
  6585. (or (org-entry-get nil property) ""))
  6586. ((= dcst ?o)
  6587. (if (looking-at org-complex-heading-regexp)
  6588. (- 9999 (length (member (match-string 2)
  6589. org-todo-keywords-1)))))
  6590. ((= dcst ?f)
  6591. (if getkey-func
  6592. (progn
  6593. (setq tmp (funcall getkey-func))
  6594. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6595. tmp)
  6596. (error "Invalid key function `%s'" getkey-func)))
  6597. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6598. nil
  6599. (cond
  6600. ((= dcst ?a) 'string<)
  6601. ((= dcst ?f) compare-func)
  6602. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6603. (t nil)))))
  6604. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6605. (message "Sorting entries...done")))
  6606. (defun org-do-sort (table what &optional with-case sorting-type)
  6607. "Sort TABLE of WHAT according to SORTING-TYPE.
  6608. The user will be prompted for the SORTING-TYPE if the call to this
  6609. function does not specify it. WHAT is only for the prompt, to indicate
  6610. what is being sorted. The sorting key will be extracted from
  6611. the car of the elements of the table.
  6612. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6613. (unless sorting-type
  6614. (message
  6615. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6616. what)
  6617. (setq sorting-type (read-char-exclusive)))
  6618. (let ((dcst (downcase sorting-type))
  6619. extractfun comparefun)
  6620. ;; Define the appropriate functions
  6621. (cond
  6622. ((= dcst ?n)
  6623. (setq extractfun 'string-to-number
  6624. comparefun (if (= dcst sorting-type) '< '>)))
  6625. ((= dcst ?a)
  6626. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6627. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6628. comparefun (if (= dcst sorting-type)
  6629. 'string<
  6630. (lambda (a b) (and (not (string< a b))
  6631. (not (string= a b)))))))
  6632. ((= dcst ?t)
  6633. (setq extractfun
  6634. (lambda (x)
  6635. (if (or (string-match org-ts-regexp x)
  6636. (string-match org-ts-regexp-both x))
  6637. (org-float-time
  6638. (org-time-string-to-time (match-string 0 x)))
  6639. 0))
  6640. comparefun (if (= dcst sorting-type) '< '>)))
  6641. (t (error "Invalid sorting type `%c'" sorting-type)))
  6642. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6643. table)
  6644. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6645. ;;; The orgstruct minor mode
  6646. ;; Define a minor mode which can be used in other modes in order to
  6647. ;; integrate the org-mode structure editing commands.
  6648. ;; This is really a hack, because the org-mode structure commands use
  6649. ;; keys which normally belong to the major mode. Here is how it
  6650. ;; works: The minor mode defines all the keys necessary to operate the
  6651. ;; structure commands, but wraps the commands into a function which
  6652. ;; tests if the cursor is currently at a headline or a plain list
  6653. ;; item. If that is the case, the structure command is used,
  6654. ;; temporarily setting many Org-mode variables like regular
  6655. ;; expressions for filling etc. However, when any of those keys is
  6656. ;; used at a different location, function uses `key-binding' to look
  6657. ;; up if the key has an associated command in another currently active
  6658. ;; keymap (minor modes, major mode, global), and executes that
  6659. ;; command. There might be problems if any of the keys is otherwise
  6660. ;; used as a prefix key.
  6661. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6662. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6663. ;; addresses this by checking explicitly for both bindings.
  6664. (defvar orgstruct-mode-map (make-sparse-keymap)
  6665. "Keymap for the minor `orgstruct-mode'.")
  6666. (defvar org-local-vars nil
  6667. "List of local variables, for use by `orgstruct-mode'")
  6668. ;;;###autoload
  6669. (define-minor-mode orgstruct-mode
  6670. "Toggle the minor more `orgstruct-mode'.
  6671. This mode is for using Org-mode structure commands in other modes.
  6672. The following key behave as if Org-mode was active, if the cursor
  6673. is on a headline, or on a plain list item (both in the definition
  6674. of Org-mode).
  6675. M-up Move entry/item up
  6676. M-down Move entry/item down
  6677. M-left Promote
  6678. M-right Demote
  6679. M-S-up Move entry/item up
  6680. M-S-down Move entry/item down
  6681. M-S-left Promote subtree
  6682. M-S-right Demote subtree
  6683. M-q Fill paragraph and items like in Org-mode
  6684. C-c ^ Sort entries
  6685. C-c - Cycle list bullet
  6686. TAB Cycle item visibility
  6687. M-RET Insert new heading/item
  6688. S-M-RET Insert new TODO heading / Checkbox item
  6689. C-c C-c Set tags / toggle checkbox"
  6690. nil " OrgStruct" nil
  6691. (org-load-modules-maybe)
  6692. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6693. ;;;###autoload
  6694. (defun turn-on-orgstruct ()
  6695. "Unconditionally turn on `orgstruct-mode'."
  6696. (orgstruct-mode 1))
  6697. (defun orgstruct++-mode (&optional arg)
  6698. "Toggle `orgstruct-mode', the enhanced version of it.
  6699. In addition to setting orgstruct-mode, this also exports all indentation
  6700. and autofilling variables from org-mode into the buffer. It will also
  6701. recognize item context in multiline items.
  6702. Note that turning off orgstruct-mode will *not* remove the
  6703. indentation/paragraph settings. This can only be done by refreshing the
  6704. major mode, for example with \\[normal-mode]."
  6705. (interactive "P")
  6706. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6707. (if (< arg 1)
  6708. (orgstruct-mode -1)
  6709. (orgstruct-mode 1)
  6710. (let (var val)
  6711. (mapc
  6712. (lambda (x)
  6713. (when (string-match
  6714. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6715. (symbol-name (car x)))
  6716. (setq var (car x) val (nth 1 x))
  6717. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6718. org-local-vars)
  6719. (org-set-local 'orgstruct-is-++ t))))
  6720. (defvar orgstruct-is-++ nil
  6721. "Is orgstruct-mode in ++ version in the current-buffer?")
  6722. (make-variable-buffer-local 'orgstruct-is-++)
  6723. ;;;###autoload
  6724. (defun turn-on-orgstruct++ ()
  6725. "Unconditionally turn on `orgstruct++-mode'."
  6726. (orgstruct++-mode 1))
  6727. (defun orgstruct-error ()
  6728. "Error when there is no default binding for a structure key."
  6729. (interactive)
  6730. (error "This key has no function outside structure elements"))
  6731. (defun orgstruct-setup ()
  6732. "Setup orgstruct keymaps."
  6733. (let ((nfunc 0)
  6734. (bindings
  6735. (list
  6736. '([(meta up)] org-metaup)
  6737. '([(meta down)] org-metadown)
  6738. '([(meta left)] org-metaleft)
  6739. '([(meta right)] org-metaright)
  6740. '([(meta shift up)] org-shiftmetaup)
  6741. '([(meta shift down)] org-shiftmetadown)
  6742. '([(meta shift left)] org-shiftmetaleft)
  6743. '([(meta shift right)] org-shiftmetaright)
  6744. '([?\e (up)] org-metaup)
  6745. '([?\e (down)] org-metadown)
  6746. '([?\e (left)] org-metaleft)
  6747. '([?\e (right)] org-metaright)
  6748. '([?\e (shift up)] org-shiftmetaup)
  6749. '([?\e (shift down)] org-shiftmetadown)
  6750. '([?\e (shift left)] org-shiftmetaleft)
  6751. '([?\e (shift right)] org-shiftmetaright)
  6752. '([(shift up)] org-shiftup)
  6753. '([(shift down)] org-shiftdown)
  6754. '([(shift left)] org-shiftleft)
  6755. '([(shift right)] org-shiftright)
  6756. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6757. '("\M-q" fill-paragraph)
  6758. '("\C-c^" org-sort)
  6759. '("\C-c-" org-cycle-list-bullet)))
  6760. elt key fun cmd)
  6761. (while (setq elt (pop bindings))
  6762. (setq nfunc (1+ nfunc))
  6763. (setq key (org-key (car elt))
  6764. fun (nth 1 elt)
  6765. cmd (orgstruct-make-binding fun nfunc key))
  6766. (org-defkey orgstruct-mode-map key cmd))
  6767. ;; Special treatment needed for TAB and RET
  6768. (org-defkey orgstruct-mode-map [(tab)]
  6769. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6770. (org-defkey orgstruct-mode-map "\C-i"
  6771. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6772. (org-defkey orgstruct-mode-map "\M-\C-m"
  6773. (orgstruct-make-binding 'org-insert-heading 105
  6774. "\M-\C-m" [(meta return)]))
  6775. (org-defkey orgstruct-mode-map [(meta return)]
  6776. (orgstruct-make-binding 'org-insert-heading 106
  6777. [(meta return)] "\M-\C-m"))
  6778. (org-defkey orgstruct-mode-map [(shift meta return)]
  6779. (orgstruct-make-binding 'org-insert-todo-heading 107
  6780. [(meta return)] "\M-\C-m"))
  6781. (org-defkey orgstruct-mode-map "\e\C-m"
  6782. (orgstruct-make-binding 'org-insert-heading 108
  6783. "\e\C-m" [?\e (return)]))
  6784. (org-defkey orgstruct-mode-map [?\e (return)]
  6785. (orgstruct-make-binding 'org-insert-heading 109
  6786. [?\e (return)] "\e\C-m"))
  6787. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6788. (orgstruct-make-binding 'org-insert-todo-heading 110
  6789. [?\e (return)] "\e\C-m"))
  6790. (unless org-local-vars
  6791. (setq org-local-vars (org-get-local-variables)))
  6792. t))
  6793. (defun orgstruct-make-binding (fun n &rest keys)
  6794. "Create a function for binding in the structure minor mode.
  6795. FUN is the command to call inside a table. N is used to create a unique
  6796. command name. KEYS are keys that should be checked in for a command
  6797. to execute outside of tables."
  6798. (eval
  6799. (list 'defun
  6800. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6801. '(arg)
  6802. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6803. "Outside of structure, run the binding of `"
  6804. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6805. "'.")
  6806. '(interactive "p")
  6807. (list 'if
  6808. `(org-context-p 'headline 'item
  6809. (and orgstruct-is-++
  6810. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6811. 'item-body))
  6812. (list 'org-run-like-in-org-mode (list 'quote fun))
  6813. (list 'let '(orgstruct-mode)
  6814. (list 'call-interactively
  6815. (append '(or)
  6816. (mapcar (lambda (k)
  6817. (list 'key-binding k))
  6818. keys)
  6819. '('orgstruct-error))))))))
  6820. (defun org-context-p (&rest contexts)
  6821. "Check if local context is any of CONTEXTS.
  6822. Possible values in the list of contexts are `table', `headline', and `item'."
  6823. (let ((pos (point)))
  6824. (goto-char (point-at-bol))
  6825. (prog1 (or (and (memq 'table contexts)
  6826. (looking-at "[ \t]*|"))
  6827. (and (memq 'headline contexts)
  6828. ;;????????? (looking-at "\\*+"))
  6829. (looking-at outline-regexp))
  6830. (and (memq 'item contexts)
  6831. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6832. (and (memq 'item-body contexts)
  6833. (org-in-item-p)))
  6834. (goto-char pos))))
  6835. (defun org-get-local-variables ()
  6836. "Return a list of all local variables in an org-mode buffer."
  6837. (let (varlist)
  6838. (with-current-buffer (get-buffer-create "*Org tmp*")
  6839. (erase-buffer)
  6840. (org-mode)
  6841. (setq varlist (buffer-local-variables)))
  6842. (kill-buffer "*Org tmp*")
  6843. (delq nil
  6844. (mapcar
  6845. (lambda (x)
  6846. (setq x
  6847. (if (symbolp x)
  6848. (list x)
  6849. (list (car x) (list 'quote (cdr x)))))
  6850. (if (string-match
  6851. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6852. (symbol-name (car x)))
  6853. x nil))
  6854. varlist))))
  6855. ;;;###autoload
  6856. (defun org-run-like-in-org-mode (cmd)
  6857. "Run a command, pretending that the current buffer is in Org-mode.
  6858. This will temporarily bind local variables that are typically bound in
  6859. Org-mode to the values they have in Org-mode, and then interactively
  6860. call CMD."
  6861. (org-load-modules-maybe)
  6862. (unless org-local-vars
  6863. (setq org-local-vars (org-get-local-variables)))
  6864. (eval (list 'let org-local-vars
  6865. (list 'call-interactively (list 'quote cmd)))))
  6866. ;;;; Archiving
  6867. (defun org-get-category (&optional pos)
  6868. "Get the category applying to position POS."
  6869. (get-text-property (or pos (point)) 'org-category))
  6870. (defun org-refresh-category-properties ()
  6871. "Refresh category text properties in the buffer."
  6872. (let ((def-cat (cond
  6873. ((null org-category)
  6874. (if buffer-file-name
  6875. (file-name-sans-extension
  6876. (file-name-nondirectory buffer-file-name))
  6877. "???"))
  6878. ((symbolp org-category) (symbol-name org-category))
  6879. (t org-category)))
  6880. beg end cat pos optionp)
  6881. (org-unmodified
  6882. (save-excursion
  6883. (save-restriction
  6884. (widen)
  6885. (goto-char (point-min))
  6886. (put-text-property (point) (point-max) 'org-category def-cat)
  6887. (while (re-search-forward
  6888. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6889. (setq pos (match-end 0)
  6890. optionp (equal (char-after (match-beginning 0)) ?#)
  6891. cat (org-trim (match-string 2)))
  6892. (if optionp
  6893. (setq beg (point-at-bol) end (point-max))
  6894. (org-back-to-heading t)
  6895. (setq beg (point) end (org-end-of-subtree t t)))
  6896. (put-text-property beg end 'org-category cat)
  6897. (goto-char pos)))))))
  6898. ;;;; Link Stuff
  6899. ;;; Link abbreviations
  6900. (defun org-link-expand-abbrev (link)
  6901. "Apply replacements as defined in `org-link-abbrev-alist."
  6902. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6903. (let* ((key (match-string 1 link))
  6904. (as (or (assoc key org-link-abbrev-alist-local)
  6905. (assoc key org-link-abbrev-alist)))
  6906. (tag (and (match-end 2) (match-string 3 link)))
  6907. rpl)
  6908. (if (not as)
  6909. link
  6910. (setq rpl (cdr as))
  6911. (cond
  6912. ((symbolp rpl) (funcall rpl tag))
  6913. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6914. ((string-match "%h" rpl)
  6915. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6916. (t (concat rpl tag)))))
  6917. link))
  6918. ;;; Storing and inserting links
  6919. (defvar org-insert-link-history nil
  6920. "Minibuffer history for links inserted with `org-insert-link'.")
  6921. (defvar org-stored-links nil
  6922. "Contains the links stored with `org-store-link'.")
  6923. (defvar org-store-link-plist nil
  6924. "Plist with info about the most recently link created with `org-store-link'.")
  6925. (defvar org-link-protocols nil
  6926. "Link protocols added to Org-mode using `org-add-link-type'.")
  6927. (defvar org-store-link-functions nil
  6928. "List of functions that are called to create and store a link.
  6929. Each function will be called in turn until one returns a non-nil
  6930. value. Each function should check if it is responsible for creating
  6931. this link (for example by looking at the major mode).
  6932. If not, it must exit and return nil.
  6933. If yes, it should return a non-nil value after a calling
  6934. `org-store-link-props' with a list of properties and values.
  6935. Special properties are:
  6936. :type The link prefix. like \"http\". This must be given.
  6937. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6938. This is obligatory as well.
  6939. :description Optional default description for the second pair
  6940. of brackets in an Org-mode link. The user can still change
  6941. this when inserting this link into an Org-mode buffer.
  6942. In addition to these, any additional properties can be specified
  6943. and then used in remember templates.")
  6944. (defun org-add-link-type (type &optional follow export)
  6945. "Add TYPE to the list of `org-link-types'.
  6946. Re-compute all regular expressions depending on `org-link-types'
  6947. FOLLOW and EXPORT are two functions.
  6948. FOLLOW should take the link path as the single argument and do whatever
  6949. is necessary to follow the link, for example find a file or display
  6950. a mail message.
  6951. EXPORT should format the link path for export to one of the export formats.
  6952. It should be a function accepting three arguments:
  6953. path the path of the link, the text after the prefix (like \"http:\")
  6954. desc the description of the link, if any, nil if there was no description
  6955. format the export format, a symbol like `html' or `latex'.
  6956. The function may use the FORMAT information to return different values
  6957. depending on the format. The return value will be put literally into
  6958. the exported file.
  6959. Org-mode has a built-in default for exporting links. If you are happy with
  6960. this default, there is no need to define an export function for the link
  6961. type. For a simple example of an export function, see `org-bbdb.el'."
  6962. (add-to-list 'org-link-types type t)
  6963. (org-make-link-regexps)
  6964. (if (assoc type org-link-protocols)
  6965. (setcdr (assoc type org-link-protocols) (list follow export))
  6966. (push (list type follow export) org-link-protocols)))
  6967. (defvar org-agenda-buffer-name)
  6968. ;;;###autoload
  6969. (defun org-store-link (arg)
  6970. "\\<org-mode-map>Store an org-link to the current location.
  6971. This link is added to `org-stored-links' and can later be inserted
  6972. into an org-buffer with \\[org-insert-link].
  6973. For some link types, a prefix arg is interpreted:
  6974. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6975. For file links, arg negates `org-context-in-file-links'."
  6976. (interactive "P")
  6977. (org-load-modules-maybe)
  6978. (setq org-store-link-plist nil) ; reset
  6979. (let ((outline-regexp (org-get-limited-outline-regexp))
  6980. link cpltxt desc description search txt custom-id)
  6981. (cond
  6982. ((run-hook-with-args-until-success 'org-store-link-functions)
  6983. (setq link (plist-get org-store-link-plist :link)
  6984. desc (or (plist-get org-store-link-plist :description) link)))
  6985. ((equal (buffer-name) "*Org Edit Src Example*")
  6986. (let (label gc)
  6987. (while (or (not label)
  6988. (save-excursion
  6989. (save-restriction
  6990. (widen)
  6991. (goto-char (point-min))
  6992. (re-search-forward
  6993. (regexp-quote (format org-coderef-label-format label))
  6994. nil t))))
  6995. (when label (message "Label exists already") (sit-for 2))
  6996. (setq label (read-string "Code line label: " label)))
  6997. (end-of-line 1)
  6998. (setq link (format org-coderef-label-format label))
  6999. (setq gc (- 79 (length link)))
  7000. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7001. (insert link)
  7002. (setq link (concat "(" label ")") desc nil)))
  7003. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7004. ;; We are in the agenda, link to referenced location
  7005. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7006. (get-text-property (point) 'org-marker))))
  7007. (when m
  7008. (org-with-point-at m
  7009. (call-interactively 'org-store-link)))))
  7010. ((eq major-mode 'calendar-mode)
  7011. (let ((cd (calendar-cursor-to-date)))
  7012. (setq link
  7013. (format-time-string
  7014. (car org-time-stamp-formats)
  7015. (apply 'encode-time
  7016. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7017. nil nil nil))))
  7018. (org-store-link-props :type "calendar" :date cd)))
  7019. ((eq major-mode 'w3-mode)
  7020. (setq cpltxt (if (and (buffer-name)
  7021. (not (string-match "Untitled" (buffer-name))))
  7022. (buffer-name)
  7023. (url-view-url t))
  7024. link (org-make-link (url-view-url t)))
  7025. (org-store-link-props :type "w3" :url (url-view-url t)))
  7026. ((eq major-mode 'w3m-mode)
  7027. (setq cpltxt (or w3m-current-title w3m-current-url)
  7028. link (org-make-link w3m-current-url))
  7029. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7030. ((setq search (run-hook-with-args-until-success
  7031. 'org-create-file-search-functions))
  7032. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7033. "::" search))
  7034. (setq cpltxt (or description link)))
  7035. ((eq major-mode 'image-mode)
  7036. (setq cpltxt (concat "file:"
  7037. (abbreviate-file-name buffer-file-name))
  7038. link (org-make-link cpltxt))
  7039. (org-store-link-props :type "image" :file buffer-file-name))
  7040. ((eq major-mode 'dired-mode)
  7041. ;; link to the file in the current line
  7042. (let ((file (dired-get-filename nil t)))
  7043. (setq file (if file
  7044. (abbreviate-file-name
  7045. (expand-file-name (dired-get-filename nil t)))
  7046. ;; otherwise, no file so use current directory.
  7047. default-directory))
  7048. (setq cpltxt (concat "file:" file)
  7049. link (org-make-link cpltxt))))
  7050. ((and buffer-file-name (org-mode-p))
  7051. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7052. (cond
  7053. ((org-in-regexp "<<\\(.*?\\)>>")
  7054. (setq cpltxt
  7055. (concat "file:"
  7056. (abbreviate-file-name buffer-file-name)
  7057. "::" (match-string 1))
  7058. link (org-make-link cpltxt)))
  7059. ((and (featurep 'org-id)
  7060. (or (eq org-link-to-org-use-id t)
  7061. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7062. (interactive-p))
  7063. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7064. (interactive-p)
  7065. (not custom-id))
  7066. (and org-link-to-org-use-id
  7067. (condition-case nil
  7068. (org-entry-get nil "ID")
  7069. (error nil)))))
  7070. ;; We can make a link using the ID.
  7071. (setq link (condition-case nil
  7072. (prog1 (org-id-store-link)
  7073. (setq desc (plist-get org-store-link-plist
  7074. :description)))
  7075. (error
  7076. ;; probably before first headline, link to file only
  7077. (concat "file:"
  7078. (abbreviate-file-name buffer-file-name))))))
  7079. (t
  7080. ;; Just link to current headline
  7081. (setq cpltxt (concat "file:"
  7082. (abbreviate-file-name buffer-file-name)))
  7083. ;; Add a context search string
  7084. (when (org-xor org-context-in-file-links arg)
  7085. (setq txt (cond
  7086. ((org-on-heading-p) nil)
  7087. ((org-region-active-p)
  7088. (buffer-substring (region-beginning) (region-end)))
  7089. (t nil)))
  7090. (when (or (null txt) (string-match "\\S-" txt))
  7091. (setq cpltxt
  7092. (concat cpltxt "::"
  7093. (condition-case nil
  7094. (org-make-org-heading-search-string txt)
  7095. (error "")))
  7096. desc (or (nth 4 (ignore-errors
  7097. (org-heading-components))) "NONE"))))
  7098. (if (string-match "::\\'" cpltxt)
  7099. (setq cpltxt (substring cpltxt 0 -2)))
  7100. (setq link (org-make-link cpltxt)))))
  7101. ((buffer-file-name (buffer-base-buffer))
  7102. ;; Just link to this file here.
  7103. (setq cpltxt (concat "file:"
  7104. (abbreviate-file-name
  7105. (buffer-file-name (buffer-base-buffer)))))
  7106. ;; Add a context string
  7107. (when (org-xor org-context-in-file-links arg)
  7108. (setq txt (if (org-region-active-p)
  7109. (buffer-substring (region-beginning) (region-end))
  7110. (buffer-substring (point-at-bol) (point-at-eol))))
  7111. ;; Only use search option if there is some text.
  7112. (when (string-match "\\S-" txt)
  7113. (setq cpltxt
  7114. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7115. desc "NONE")))
  7116. (setq link (org-make-link cpltxt)))
  7117. ((interactive-p)
  7118. (error "Cannot link to a buffer which is not visiting a file"))
  7119. (t (setq link nil)))
  7120. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7121. (setq link (or link cpltxt)
  7122. desc (or desc cpltxt))
  7123. (if (equal desc "NONE") (setq desc nil))
  7124. (if (and (or (interactive-p) executing-kbd-macro) link)
  7125. (progn
  7126. (setq org-stored-links
  7127. (cons (list link desc) org-stored-links))
  7128. (message "Stored: %s" (or desc link))
  7129. (when custom-id
  7130. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7131. "::#" custom-id))
  7132. (setq org-stored-links
  7133. (cons (list link desc) org-stored-links))))
  7134. (and link (org-make-link-string link desc)))))
  7135. (defun org-store-link-props (&rest plist)
  7136. "Store link properties, extract names and addresses."
  7137. (let (x adr)
  7138. (when (setq x (plist-get plist :from))
  7139. (setq adr (mail-extract-address-components x))
  7140. (setq plist (plist-put plist :fromname (car adr)))
  7141. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7142. (when (setq x (plist-get plist :to))
  7143. (setq adr (mail-extract-address-components x))
  7144. (setq plist (plist-put plist :toname (car adr)))
  7145. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7146. (let ((from (plist-get plist :from))
  7147. (to (plist-get plist :to)))
  7148. (when (and from to org-from-is-user-regexp)
  7149. (setq plist
  7150. (plist-put plist :fromto
  7151. (if (string-match org-from-is-user-regexp from)
  7152. (concat "to %t")
  7153. (concat "from %f"))))))
  7154. (setq org-store-link-plist plist))
  7155. (defun org-add-link-props (&rest plist)
  7156. "Add these properties to the link property list."
  7157. (let (key value)
  7158. (while plist
  7159. (setq key (pop plist) value (pop plist))
  7160. (setq org-store-link-plist
  7161. (plist-put org-store-link-plist key value)))))
  7162. (defun org-email-link-description (&optional fmt)
  7163. "Return the description part of an email link.
  7164. This takes information from `org-store-link-plist' and formats it
  7165. according to FMT (default from `org-email-link-description-format')."
  7166. (setq fmt (or fmt org-email-link-description-format))
  7167. (let* ((p org-store-link-plist)
  7168. (to (plist-get p :toaddress))
  7169. (from (plist-get p :fromaddress))
  7170. (table
  7171. (list
  7172. (cons "%c" (plist-get p :fromto))
  7173. (cons "%F" (plist-get p :from))
  7174. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7175. (cons "%T" (plist-get p :to))
  7176. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7177. (cons "%s" (plist-get p :subject))
  7178. (cons "%m" (plist-get p :message-id)))))
  7179. (when (string-match "%c" fmt)
  7180. ;; Check if the user wrote this message
  7181. (if (and org-from-is-user-regexp from to
  7182. (save-match-data (string-match org-from-is-user-regexp from)))
  7183. (setq fmt (replace-match "to %t" t t fmt))
  7184. (setq fmt (replace-match "from %f" t t fmt))))
  7185. (org-replace-escapes fmt table)))
  7186. (defun org-make-org-heading-search-string (&optional string heading)
  7187. "Make search string for STRING or current headline."
  7188. (interactive)
  7189. (let ((s (or string (org-get-heading))))
  7190. (unless (and string (not heading))
  7191. ;; We are using a headline, clean up garbage in there.
  7192. (if (string-match org-todo-regexp s)
  7193. (setq s (replace-match "" t t s)))
  7194. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7195. (setq s (replace-match "" t t s)))
  7196. (setq s (org-trim s))
  7197. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7198. org-comment-string "\\)") s)
  7199. (setq s (replace-match "" t t s)))
  7200. (while (string-match org-ts-regexp s)
  7201. (setq s (replace-match "" t t s))))
  7202. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7203. (setq s (replace-match " " t t s)))
  7204. (or string (setq s (concat "*" s))) ; Add * for headlines
  7205. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7206. (defun org-make-link (&rest strings)
  7207. "Concatenate STRINGS."
  7208. (apply 'concat strings))
  7209. (defun org-make-link-string (link &optional description)
  7210. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7211. (unless (string-match "\\S-" link)
  7212. (error "Empty link"))
  7213. (when (and description
  7214. (stringp description)
  7215. (not (string-match "\\S-" description)))
  7216. (setq description nil))
  7217. (when (stringp description)
  7218. ;; Remove brackets from the description, they are fatal.
  7219. (while (string-match "\\[" description)
  7220. (setq description (replace-match "{" t t description)))
  7221. (while (string-match "\\]" description)
  7222. (setq description (replace-match "}" t t description))))
  7223. (when (equal (org-link-escape link) description)
  7224. ;; No description needed, it is identical
  7225. (setq description nil))
  7226. (when (and (not description)
  7227. (not (equal link (org-link-escape link))))
  7228. (setq description (org-extract-attributes link)))
  7229. (concat "[[" (org-link-escape link) "]"
  7230. (if description (concat "[" description "]") "")
  7231. "]"))
  7232. (defconst org-link-escape-chars
  7233. '((?\ . "%20")
  7234. (?\[ . "%5B")
  7235. (?\] . "%5D")
  7236. (?\340 . "%E0") ; `a
  7237. (?\342 . "%E2") ; ^a
  7238. (?\347 . "%E7") ; ,c
  7239. (?\350 . "%E8") ; `e
  7240. (?\351 . "%E9") ; 'e
  7241. (?\352 . "%EA") ; ^e
  7242. (?\356 . "%EE") ; ^i
  7243. (?\364 . "%F4") ; ^o
  7244. (?\371 . "%F9") ; `u
  7245. (?\373 . "%FB") ; ^u
  7246. (?\; . "%3B")
  7247. ;; (?? . "%3F")
  7248. (?= . "%3D")
  7249. (?+ . "%2B")
  7250. )
  7251. "Association list of escapes for some characters problematic in links.
  7252. This is the list that is used for internal purposes.")
  7253. (defvar org-url-encoding-use-url-hexify nil)
  7254. (defconst org-link-escape-chars-browser
  7255. '((?\ . "%20")) ; 32 for the SPC char
  7256. "Association list of escapes for some characters problematic in links.
  7257. This is the list that is used before handing over to the browser.")
  7258. (defun org-link-escape (text &optional table)
  7259. "Escape characters in TEXT that are problematic for links."
  7260. (if (and org-url-encoding-use-url-hexify (not table))
  7261. (url-hexify-string text)
  7262. (setq table (or table org-link-escape-chars))
  7263. (when text
  7264. (let ((re (mapconcat (lambda (x) (regexp-quote
  7265. (char-to-string (car x))))
  7266. table "\\|")))
  7267. (while (string-match re text)
  7268. (setq text
  7269. (replace-match
  7270. (cdr (assoc (string-to-char (match-string 0 text))
  7271. table))
  7272. t t text)))
  7273. text))))
  7274. (defun org-link-unescape (text &optional table)
  7275. "Reverse the action of `org-link-escape'."
  7276. (if (and org-url-encoding-use-url-hexify (not table))
  7277. (url-unhex-string text)
  7278. (setq table (or table org-link-escape-chars))
  7279. (when text
  7280. (let ((case-fold-search t)
  7281. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7282. table "\\|")))
  7283. (while (string-match re text)
  7284. (setq text
  7285. (replace-match
  7286. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7287. table)))
  7288. t t text)))
  7289. text))))
  7290. (defun org-xor (a b)
  7291. "Exclusive or."
  7292. (if a (not b) b))
  7293. (defun org-fixup-message-id-for-http (s)
  7294. "Replace special characters in a message id, so it can be used in an http query."
  7295. (while (string-match "<" s)
  7296. (setq s (replace-match "%3C" t t s)))
  7297. (while (string-match ">" s)
  7298. (setq s (replace-match "%3E" t t s)))
  7299. (while (string-match "@" s)
  7300. (setq s (replace-match "%40" t t s)))
  7301. s)
  7302. ;;;###autoload
  7303. (defun org-insert-link-global ()
  7304. "Insert a link like Org-mode does.
  7305. This command can be called in any mode to insert a link in Org-mode syntax."
  7306. (interactive)
  7307. (org-load-modules-maybe)
  7308. (org-run-like-in-org-mode 'org-insert-link))
  7309. (defun org-insert-link (&optional complete-file link-location)
  7310. "Insert a link. At the prompt, enter the link.
  7311. Completion can be used to insert any of the link protocol prefixes like
  7312. http or ftp in use.
  7313. The history can be used to select a link previously stored with
  7314. `org-store-link'. When the empty string is entered (i.e. if you just
  7315. press RET at the prompt), the link defaults to the most recently
  7316. stored link. As SPC triggers completion in the minibuffer, you need to
  7317. use M-SPC or C-q SPC to force the insertion of a space character.
  7318. You will also be prompted for a description, and if one is given, it will
  7319. be displayed in the buffer instead of the link.
  7320. If there is already a link at point, this command will allow you to edit link
  7321. and description parts.
  7322. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7323. be selected using completion. The path to the file will be relative to the
  7324. current directory if the file is in the current directory or a subdirectory.
  7325. Otherwise, the link will be the absolute path as completed in the minibuffer
  7326. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7327. option `org-link-file-path-type'.
  7328. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7329. the current directory or below.
  7330. With three \\[universal-argument] prefixes, negate the meaning of
  7331. `org-keep-stored-link-after-insertion'.
  7332. If `org-make-link-description-function' is non-nil, this function will be
  7333. called with the link target, and the result will be the default
  7334. link description.
  7335. If the LINK-LOCATION parameter is non-nil, this value will be
  7336. used as the link location instead of reading one interactively."
  7337. (interactive "P")
  7338. (let* ((wcf (current-window-configuration))
  7339. (region (if (org-region-active-p)
  7340. (buffer-substring (region-beginning) (region-end))))
  7341. (remove (and region (list (region-beginning) (region-end))))
  7342. (desc region)
  7343. tmphist ; byte-compile incorrectly complains about this
  7344. (link link-location)
  7345. entry file all-prefixes)
  7346. (cond
  7347. (link-location) ; specified by arg, just use it.
  7348. ((org-in-regexp org-bracket-link-regexp 1)
  7349. ;; We do have a link at point, and we are going to edit it.
  7350. (setq remove (list (match-beginning 0) (match-end 0)))
  7351. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7352. (setq link (read-string "Link: "
  7353. (org-link-unescape
  7354. (org-match-string-no-properties 1)))))
  7355. ((or (org-in-regexp org-angle-link-re)
  7356. (org-in-regexp org-plain-link-re))
  7357. ;; Convert to bracket link
  7358. (setq remove (list (match-beginning 0) (match-end 0))
  7359. link (read-string "Link: "
  7360. (org-remove-angle-brackets (match-string 0)))))
  7361. ((member complete-file '((4) (16)))
  7362. ;; Completing read for file names.
  7363. (setq link (org-file-complete-link complete-file)))
  7364. (t
  7365. ;; Read link, with completion for stored links.
  7366. (with-output-to-temp-buffer "*Org Links*"
  7367. (princ "Insert a link.
  7368. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7369. (when org-stored-links
  7370. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7371. (princ (mapconcat
  7372. (lambda (x)
  7373. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7374. (reverse org-stored-links) "\n"))))
  7375. (let ((cw (selected-window)))
  7376. (select-window (get-buffer-window "*Org Links*"))
  7377. (setq truncate-lines t)
  7378. (unless (pos-visible-in-window-p (point-max))
  7379. (org-fit-window-to-buffer))
  7380. (and (window-live-p cw) (select-window cw)))
  7381. ;; Fake a link history, containing the stored links.
  7382. (setq tmphist (append (mapcar 'car org-stored-links)
  7383. org-insert-link-history))
  7384. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7385. (mapcar 'car org-link-abbrev-alist)
  7386. org-link-types))
  7387. (unwind-protect
  7388. (progn
  7389. (setq link
  7390. (let ((org-completion-use-ido nil)
  7391. (org-completion-use-iswitchb nil))
  7392. (org-completing-read
  7393. "Link: "
  7394. (append
  7395. (mapcar (lambda (x) (list (concat x ":")))
  7396. all-prefixes)
  7397. (mapcar 'car org-stored-links))
  7398. nil nil nil
  7399. 'tmphist
  7400. (car (car org-stored-links)))))
  7401. (if (not (string-match "\\S-" link))
  7402. (error "No link selected"))
  7403. (if (or (member link all-prefixes)
  7404. (and (equal ":" (substring link -1))
  7405. (member (substring link 0 -1) all-prefixes)
  7406. (setq link (substring link 0 -1))))
  7407. (setq link (org-link-try-special-completion link))))
  7408. (set-window-configuration wcf)
  7409. (kill-buffer "*Org Links*"))
  7410. (setq entry (assoc link org-stored-links))
  7411. (or entry (push link org-insert-link-history))
  7412. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7413. (not org-keep-stored-link-after-insertion))
  7414. (setq org-stored-links (delq (assoc link org-stored-links)
  7415. org-stored-links)))
  7416. (setq desc (or desc (nth 1 entry)))))
  7417. (if (string-match org-plain-link-re link)
  7418. ;; URL-like link, normalize the use of angular brackets.
  7419. (setq link (org-make-link (org-remove-angle-brackets link))))
  7420. ;; Check if we are linking to the current file with a search option
  7421. ;; If yes, simplify the link by using only the search option.
  7422. (when (and buffer-file-name
  7423. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7424. (let* ((path (match-string 1 link))
  7425. (case-fold-search nil)
  7426. (search (match-string 2 link)))
  7427. (save-match-data
  7428. (if (equal (file-truename buffer-file-name) (file-truename path))
  7429. ;; We are linking to this same file, with a search option
  7430. (setq link search)))))
  7431. ;; Check if we can/should use a relative path. If yes, simplify the link
  7432. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7433. (let* ((type (match-string 1 link))
  7434. (path (match-string 2 link))
  7435. (origpath path)
  7436. (case-fold-search nil))
  7437. (cond
  7438. ((or (eq org-link-file-path-type 'absolute)
  7439. (equal complete-file '(16)))
  7440. (setq path (abbreviate-file-name (expand-file-name path))))
  7441. ((eq org-link-file-path-type 'noabbrev)
  7442. (setq path (expand-file-name path)))
  7443. ((eq org-link-file-path-type 'relative)
  7444. (setq path (file-relative-name path)))
  7445. (t
  7446. (save-match-data
  7447. (if (string-match (concat "^" (regexp-quote
  7448. (file-name-as-directory
  7449. (expand-file-name "."))))
  7450. (expand-file-name path))
  7451. ;; We are linking a file with relative path name.
  7452. (setq path (substring (expand-file-name path)
  7453. (match-end 0)))
  7454. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7455. (setq link (concat type path))
  7456. (if (equal desc origpath)
  7457. (setq desc path))))
  7458. (if org-make-link-description-function
  7459. (setq desc (funcall org-make-link-description-function link desc)))
  7460. (setq desc (read-string "Description: " desc))
  7461. (unless (string-match "\\S-" desc) (setq desc nil))
  7462. (if remove (apply 'delete-region remove))
  7463. (insert (org-make-link-string link desc))))
  7464. (defun org-link-try-special-completion (type)
  7465. "If there is completion support for link type TYPE, offer it."
  7466. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7467. (if (functionp fun)
  7468. (funcall fun)
  7469. (read-string "Link (no completion support): " (concat type ":")))))
  7470. (defun org-file-complete-link (&optional arg)
  7471. "Create a file link using completion."
  7472. (let (file link)
  7473. (setq file (read-file-name "File: "))
  7474. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7475. (pwd1 (file-name-as-directory (abbreviate-file-name
  7476. (expand-file-name ".")))))
  7477. (cond
  7478. ((equal arg '(16))
  7479. (setq link (org-make-link
  7480. "file:"
  7481. (abbreviate-file-name (expand-file-name file)))))
  7482. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7483. (setq link (org-make-link "file:" (match-string 1 file))))
  7484. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7485. (expand-file-name file))
  7486. (setq link (org-make-link
  7487. "file:" (match-string 1 (expand-file-name file)))))
  7488. (t (setq link (org-make-link "file:" file)))))
  7489. link))
  7490. (defun org-completing-read (&rest args)
  7491. "Completing-read with SPACE being a normal character."
  7492. (let ((minibuffer-local-completion-map
  7493. (copy-keymap minibuffer-local-completion-map)))
  7494. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7495. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7496. (apply 'org-icompleting-read args)))
  7497. (defun org-completing-read-no-i (&rest args)
  7498. (let (org-completion-use-ido org-completion-use-iswitchb)
  7499. (apply 'org-completing-read args)))
  7500. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7501. "Use iswitch as a completing-read replacement to choose from choices.
  7502. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7503. from."
  7504. (let* ((iswitchb-use-virtual-buffers nil)
  7505. (iswitchb-make-buflist-hook
  7506. (lambda ()
  7507. (setq iswitchb-temp-buflist choices))))
  7508. (iswitchb-read-buffer prompt)))
  7509. (defun org-icompleting-read (&rest args)
  7510. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7511. (org-without-partial-completion
  7512. (if (and org-completion-use-ido
  7513. (fboundp 'ido-completing-read)
  7514. (boundp 'ido-mode) ido-mode
  7515. (listp (second args)))
  7516. (let ((ido-enter-matching-directory nil))
  7517. (apply 'ido-completing-read (concat (car args))
  7518. (if (consp (car (nth 1 args)))
  7519. (mapcar (lambda (x) (car x)) (nth 1 args))
  7520. (nth 1 args))
  7521. (cddr args)))
  7522. (if (and org-completion-use-iswitchb
  7523. (boundp 'iswitchb-mode) iswitchb-mode
  7524. (listp (second args)))
  7525. (apply 'org-iswitchb-completing-read (concat (car args))
  7526. (if (consp (car (nth 1 args)))
  7527. (mapcar (lambda (x) (car x)) (nth 1 args))
  7528. (nth 1 args))
  7529. (cddr args))
  7530. (apply 'completing-read args)))))
  7531. (defun org-extract-attributes (s)
  7532. "Extract the attributes cookie from a string and set as text property."
  7533. (let (a attr (start 0) key value)
  7534. (save-match-data
  7535. (when (string-match "{{\\([^}]+\\)}}$" s)
  7536. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7537. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7538. (setq key (match-string 1 a) value (match-string 2 a)
  7539. start (match-end 0)
  7540. attr (plist-put attr (intern key) value))))
  7541. (org-add-props s nil 'org-attr attr))
  7542. s))
  7543. (defun org-extract-attributes-from-string (tag)
  7544. (let (key value attr)
  7545. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7546. (setq key (match-string 1 tag) value (match-string 2 tag)
  7547. tag (replace-match "" t t tag)
  7548. attr (plist-put attr (intern key) value)))
  7549. (cons tag attr)))
  7550. (defun org-attributes-to-string (plist)
  7551. "Format a property list into an HTML attribute list."
  7552. (let ((s "") key value)
  7553. (while plist
  7554. (setq key (pop plist) value (pop plist))
  7555. (and value
  7556. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7557. s))
  7558. ;;; Opening/following a link
  7559. (defvar org-link-search-failed nil)
  7560. (defvar org-open-link-functions nil
  7561. "Hook for functions finding a plain text link.
  7562. These functions must take a single argument, the link content.
  7563. They will be called for links that look like [[link text][description]]
  7564. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7565. like a filename (e.g. \"./blue.png\").
  7566. These functions will be called *before* Org attempts to resolve the
  7567. link by doing text searches in the current buffer - so if you want a
  7568. link \"[[target]]\" to still find \"<<target>>\", your function should
  7569. handle this as a special case.
  7570. When the function does handle the link, it must return a non-nil value.
  7571. If it decides that it is not responsible for this link, it must return
  7572. nil to indicate that that Org-mode can continue with other options
  7573. like exact and fuzzy text search.")
  7574. (defun org-next-link ()
  7575. "Move forward to the next link.
  7576. If the link is in hidden text, expose it."
  7577. (interactive)
  7578. (when (and org-link-search-failed (eq this-command last-command))
  7579. (goto-char (point-min))
  7580. (message "Link search wrapped back to beginning of buffer"))
  7581. (setq org-link-search-failed nil)
  7582. (let* ((pos (point))
  7583. (ct (org-context))
  7584. (a (assoc :link ct)))
  7585. (if a (goto-char (nth 2 a)))
  7586. (if (re-search-forward org-any-link-re nil t)
  7587. (progn
  7588. (goto-char (match-beginning 0))
  7589. (if (org-invisible-p) (org-show-context)))
  7590. (goto-char pos)
  7591. (setq org-link-search-failed t)
  7592. (error "No further link found"))))
  7593. (defun org-previous-link ()
  7594. "Move backward to the previous link.
  7595. If the link is in hidden text, expose it."
  7596. (interactive)
  7597. (when (and org-link-search-failed (eq this-command last-command))
  7598. (goto-char (point-max))
  7599. (message "Link search wrapped back to end of buffer"))
  7600. (setq org-link-search-failed nil)
  7601. (let* ((pos (point))
  7602. (ct (org-context))
  7603. (a (assoc :link ct)))
  7604. (if a (goto-char (nth 1 a)))
  7605. (if (re-search-backward org-any-link-re nil t)
  7606. (progn
  7607. (goto-char (match-beginning 0))
  7608. (if (org-invisible-p) (org-show-context)))
  7609. (goto-char pos)
  7610. (setq org-link-search-failed t)
  7611. (error "No further link found"))))
  7612. (defun org-translate-link (s)
  7613. "Translate a link string if a translation function has been defined."
  7614. (if (and org-link-translation-function
  7615. (fboundp org-link-translation-function)
  7616. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7617. (progn
  7618. (setq s (funcall org-link-translation-function
  7619. (match-string 1) (match-string 2)))
  7620. (concat (car s) ":" (cdr s)))
  7621. s))
  7622. (defun org-translate-link-from-planner (type path)
  7623. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7624. This is still an experimental function, your mileage may vary."
  7625. (cond
  7626. ((member type '("http" "https" "news" "ftp"))
  7627. ;; standard Internet links are the same.
  7628. nil)
  7629. ((and (equal type "irc") (string-match "^//" path))
  7630. ;; Planner has two / at the beginning of an irc link, we have 1.
  7631. ;; We should have zero, actually....
  7632. (setq path (substring path 1)))
  7633. ((and (equal type "lisp") (string-match "^/" path))
  7634. ;; Planner has a slash, we do not.
  7635. (setq type "elisp" path (substring path 1)))
  7636. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7637. ;; A typical message link. Planner has the id after the final slash,
  7638. ;; we separate it with a hash mark
  7639. (setq path (concat (match-string 1 path) "#"
  7640. (org-remove-angle-brackets (match-string 2 path)))))
  7641. )
  7642. (cons type path))
  7643. (defun org-find-file-at-mouse (ev)
  7644. "Open file link or URL at mouse."
  7645. (interactive "e")
  7646. (mouse-set-point ev)
  7647. (org-open-at-point 'in-emacs))
  7648. (defun org-open-at-mouse (ev)
  7649. "Open file link or URL at mouse."
  7650. (interactive "e")
  7651. (mouse-set-point ev)
  7652. (if (eq major-mode 'org-agenda-mode)
  7653. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7654. (org-open-at-point))
  7655. (defvar org-window-config-before-follow-link nil
  7656. "The window configuration before following a link.
  7657. This is saved in case the need arises to restore it.")
  7658. (defvar org-open-link-marker (make-marker)
  7659. "Marker pointing to the location where `org-open-at-point; was called.")
  7660. ;;;###autoload
  7661. (defun org-open-at-point-global ()
  7662. "Follow a link like Org-mode does.
  7663. This command can be called in any mode to follow a link that has
  7664. Org-mode syntax."
  7665. (interactive)
  7666. (org-run-like-in-org-mode 'org-open-at-point))
  7667. ;;;###autoload
  7668. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7669. "Open a link in the string S, as if it was in Org-mode."
  7670. (interactive "sLink: \nP")
  7671. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7672. (with-temp-buffer
  7673. (let ((org-inhibit-startup t))
  7674. (org-mode)
  7675. (insert s)
  7676. (goto-char (point-min))
  7677. (when reference-buffer
  7678. (setq org-link-abbrev-alist-local
  7679. (with-current-buffer reference-buffer
  7680. org-link-abbrev-alist-local)))
  7681. (org-open-at-point arg reference-buffer)))))
  7682. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7683. "Open link at or after point.
  7684. If there is no link at point, this function will search forward up to
  7685. the end of the current line.
  7686. Normally, files will be opened by an appropriate application. If the
  7687. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7688. With a double prefix argument, try to open outside of Emacs, in the
  7689. application the system uses for this file type."
  7690. (interactive "P")
  7691. (org-load-modules-maybe)
  7692. (move-marker org-open-link-marker (point))
  7693. (setq org-window-config-before-follow-link (current-window-configuration))
  7694. (org-remove-occur-highlights nil nil t)
  7695. (cond
  7696. ((and (org-on-heading-p)
  7697. (not (org-in-regexp
  7698. (concat org-plain-link-re "\\|"
  7699. org-bracket-link-regexp "\\|"
  7700. org-angle-link-re "\\|"
  7701. "[ \t]:[^ \t\n]+:[ \t]*$")))
  7702. (not (get-text-property (point) 'org-linked-text)))
  7703. (or (org-offer-links-in-entry in-emacs)
  7704. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7705. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7706. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7707. (org-footnote-action))
  7708. (t
  7709. (let (type path link line search (pos (point)))
  7710. (catch 'match
  7711. (save-excursion
  7712. (skip-chars-forward "^]\n\r")
  7713. (when (org-in-regexp org-bracket-link-regexp 1)
  7714. (setq link (org-extract-attributes
  7715. (org-link-unescape (org-match-string-no-properties 1))))
  7716. (while (string-match " *\n *" link)
  7717. (setq link (replace-match " " t t link)))
  7718. (setq link (org-link-expand-abbrev link))
  7719. (cond
  7720. ((or (file-name-absolute-p link)
  7721. (string-match "^\\.\\.?/" link))
  7722. (setq type "file" path link))
  7723. ((string-match org-link-re-with-space3 link)
  7724. (setq type (match-string 1 link) path (match-string 2 link)))
  7725. (t (setq type "thisfile" path link)))
  7726. (throw 'match t)))
  7727. (when (get-text-property (point) 'org-linked-text)
  7728. (setq type "thisfile"
  7729. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7730. (1+ (point)) (point))
  7731. path (buffer-substring
  7732. (previous-single-property-change pos 'org-linked-text)
  7733. (next-single-property-change pos 'org-linked-text)))
  7734. (throw 'match t))
  7735. (save-excursion
  7736. (when (or (org-in-regexp org-angle-link-re)
  7737. (org-in-regexp org-plain-link-re))
  7738. (setq type (match-string 1) path (match-string 2))
  7739. (throw 'match t)))
  7740. (save-excursion
  7741. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7742. (setq type "tags"
  7743. path (match-string 1))
  7744. (while (string-match ":" path)
  7745. (setq path (replace-match "+" t t path)))
  7746. (throw 'match t)))
  7747. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7748. (setq type "tree-match"
  7749. path (match-string 1))
  7750. (throw 'match t)))
  7751. (unless path
  7752. (error "No link found"))
  7753. ;; switch back to reference buffer
  7754. ;; needed when if called in a temporary buffer through
  7755. ;; org-open-link-from-string
  7756. (with-current-buffer (or reference-buffer (current-buffer))
  7757. ;; Remove any trailing spaces in path
  7758. (if (string-match " +\\'" path)
  7759. (setq path (replace-match "" t t path)))
  7760. (if (and org-link-translation-function
  7761. (fboundp org-link-translation-function))
  7762. ;; Check if we need to translate the link
  7763. (let ((tmp (funcall org-link-translation-function type path)))
  7764. (setq type (car tmp) path (cdr tmp))))
  7765. (cond
  7766. ((assoc type org-link-protocols)
  7767. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7768. ((equal type "mailto")
  7769. (let ((cmd (car org-link-mailto-program))
  7770. (args (cdr org-link-mailto-program)) args1
  7771. (address path) (subject "") a)
  7772. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7773. (setq address (match-string 1 path)
  7774. subject (org-link-escape (match-string 2 path))))
  7775. (while args
  7776. (cond
  7777. ((not (stringp (car args))) (push (pop args) args1))
  7778. (t (setq a (pop args))
  7779. (if (string-match "%a" a)
  7780. (setq a (replace-match address t t a)))
  7781. (if (string-match "%s" a)
  7782. (setq a (replace-match subject t t a)))
  7783. (push a args1))))
  7784. (apply cmd (nreverse args1))))
  7785. ((member type '("http" "https" "ftp" "news"))
  7786. (browse-url (concat type ":" (org-link-escape
  7787. path org-link-escape-chars-browser))))
  7788. ((member type '("message"))
  7789. (browse-url (concat type ":" path)))
  7790. ((string= type "tags")
  7791. (org-tags-view in-emacs path))
  7792. ((string= type "tree-match")
  7793. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7794. ((string= type "file")
  7795. (if (string-match "::\\([0-9]+\\)\\'" path)
  7796. (setq line (string-to-number (match-string 1 path))
  7797. path (substring path 0 (match-beginning 0)))
  7798. (if (string-match "::\\(.+\\)\\'" path)
  7799. (setq search (match-string 1 path)
  7800. path (substring path 0 (match-beginning 0)))))
  7801. (if (string-match "[*?{]" (file-name-nondirectory path))
  7802. (dired path)
  7803. (org-open-file path in-emacs line search)))
  7804. ((string= type "news")
  7805. (require 'org-gnus)
  7806. (org-gnus-follow-link path))
  7807. ((string= type "shell")
  7808. (let ((cmd path))
  7809. (if (or (not org-confirm-shell-link-function)
  7810. (funcall org-confirm-shell-link-function
  7811. (format "Execute \"%s\" in shell? "
  7812. (org-add-props cmd nil
  7813. 'face 'org-warning))))
  7814. (progn
  7815. (message "Executing %s" cmd)
  7816. (shell-command cmd))
  7817. (error "Abort"))))
  7818. ((string= type "elisp")
  7819. (let ((cmd path))
  7820. (if (or (not org-confirm-elisp-link-function)
  7821. (funcall org-confirm-elisp-link-function
  7822. (format "Execute \"%s\" as elisp? "
  7823. (org-add-props cmd nil
  7824. 'face 'org-warning))))
  7825. (message "%s => %s" cmd
  7826. (if (equal (string-to-char cmd) ?\()
  7827. (eval (read cmd))
  7828. (call-interactively (read cmd))))
  7829. (error "Abort"))))
  7830. ((and (string= type "thisfile")
  7831. (run-hook-with-args-until-success
  7832. 'org-open-link-functions path)))
  7833. ((string= type "thisfile")
  7834. (if in-emacs
  7835. (switch-to-buffer-other-window
  7836. (org-get-buffer-for-internal-link (current-buffer)))
  7837. (org-mark-ring-push))
  7838. (let ((cmd `(org-link-search
  7839. ,path
  7840. ,(cond ((equal in-emacs '(4)) 'occur)
  7841. ((equal in-emacs '(16)) 'org-occur)
  7842. (t nil))
  7843. ,pos)))
  7844. (condition-case nil (eval cmd)
  7845. (error (progn (widen) (eval cmd))))))
  7846. (t
  7847. (browse-url-at-point)))))))
  7848. (move-marker org-open-link-marker nil)
  7849. (run-hook-with-args 'org-follow-link-hook))
  7850. (defun org-offer-links-in-entry (&optional nth zero)
  7851. "Offer links in the current entry and follow the selected link.
  7852. If there is only one link, follow it immediately as well.
  7853. If NTH is an integer, immediately pick the NTH link found.
  7854. If ZERO is a string, check also this string for a link, and if
  7855. there is one, offer it as link number zero."
  7856. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7857. "\\(" org-angle-link-re "\\)\\|"
  7858. "\\(" org-plain-link-re "\\)"))
  7859. (cnt ?0)
  7860. (in-emacs (if (integerp nth) nil nth))
  7861. have-zero end links link c)
  7862. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7863. (push (match-string 0 zero) links)
  7864. (setq cnt (1- cnt) have-zero t))
  7865. (save-excursion
  7866. (org-back-to-heading t)
  7867. (setq end (save-excursion (outline-next-heading) (point)))
  7868. (while (re-search-forward re end t)
  7869. (push (match-string 0) links))
  7870. (setq links (org-uniquify (reverse links))))
  7871. (cond
  7872. ((null links)
  7873. (message "No links"))
  7874. ((equal (length links) 1)
  7875. (setq link (list (car links))))
  7876. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7877. (setq link (nth (if have-zero nth (1- nth)) links)))
  7878. (t ; we have to select a link
  7879. (save-excursion
  7880. (save-window-excursion
  7881. (delete-other-windows)
  7882. (with-output-to-temp-buffer "*Select Link*"
  7883. (mapc (lambda (l)
  7884. (if (not (string-match org-bracket-link-regexp l))
  7885. (princ (format "[%c] %s\n" (incf cnt)
  7886. (org-remove-angle-brackets l)))
  7887. (if (match-end 3)
  7888. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7889. (match-string 3 l) (match-string 1 l)))
  7890. (princ (format "[%c] %s\n" (incf cnt)
  7891. (match-string 1 l))))))
  7892. links))
  7893. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7894. (message "Select link to open, RET to open all:")
  7895. (setq c (read-char-exclusive))
  7896. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7897. (when (equal c ?q) (error "Abort"))
  7898. (if (equal c ?\C-m)
  7899. (setq link links)
  7900. (setq nth (- c ?0))
  7901. (if have-zero (setq nth (1+ nth)))
  7902. (unless (and (integerp nth) (>= (length links) nth))
  7903. (error "Invalid link selection"))
  7904. (setq link (list (nth (1- nth) links))))))
  7905. (if link
  7906. (let ((buf (current-buffer)))
  7907. (dolist (l link)
  7908. (org-open-link-from-string l in-emacs buf))
  7909. t)
  7910. nil)))
  7911. ;; Add special file links that specify the way of opening
  7912. (org-add-link-type "file+sys" 'org-open-file-with-system)
  7913. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  7914. (defun org-open-file-with-system (path)
  7915. "Open file at PATH using the system way of opeing it."
  7916. (org-open-file path 'system))
  7917. (defun org-open-file-with-emacs (path)
  7918. "Open file at PATH in emacs."
  7919. (org-open-file path 'emacs))
  7920. (defun org-remove-file-link-modifiers ()
  7921. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  7922. (goto-char (point-min))
  7923. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  7924. (org-if-unprotected
  7925. (replace-match "file:" t t))))
  7926. (eval-after-load "org-exp"
  7927. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  7928. 'org-remove-file-link-modifiers))
  7929. ;;;; Time estimates
  7930. (defun org-get-effort (&optional pom)
  7931. "Get the effort estimate for the current entry."
  7932. (org-entry-get pom org-effort-property))
  7933. ;;; File search
  7934. (defvar org-create-file-search-functions nil
  7935. "List of functions to construct the right search string for a file link.
  7936. These functions are called in turn with point at the location to
  7937. which the link should point.
  7938. A function in the hook should first test if it would like to
  7939. handle this file type, for example by checking the major-mode or
  7940. the file extension. If it decides not to handle this file, it
  7941. should just return nil to give other functions a chance. If it
  7942. does handle the file, it must return the search string to be used
  7943. when following the link. The search string will be part of the
  7944. file link, given after a double colon, and `org-open-at-point'
  7945. will automatically search for it. If special measures must be
  7946. taken to make the search successful, another function should be
  7947. added to the companion hook `org-execute-file-search-functions',
  7948. which see.
  7949. A function in this hook may also use `setq' to set the variable
  7950. `description' to provide a suggestion for the descriptive text to
  7951. be used for this link when it gets inserted into an Org-mode
  7952. buffer with \\[org-insert-link].")
  7953. (defvar org-execute-file-search-functions nil
  7954. "List of functions to execute a file search triggered by a link.
  7955. Functions added to this hook must accept a single argument, the
  7956. search string that was part of the file link, the part after the
  7957. double colon. The function must first check if it would like to
  7958. handle this search, for example by checking the major-mode or the
  7959. file extension. If it decides not to handle this search, it
  7960. should just return nil to give other functions a chance. If it
  7961. does handle the search, it must return a non-nil value to keep
  7962. other functions from trying.
  7963. Each function can access the current prefix argument through the
  7964. variable `current-prefix-argument'. Note that a single prefix is
  7965. used to force opening a link in Emacs, so it may be good to only
  7966. use a numeric or double prefix to guide the search function.
  7967. In case this is needed, a function in this hook can also restore
  7968. the window configuration before `org-open-at-point' was called using:
  7969. (set-window-configuration org-window-config-before-follow-link)")
  7970. (defun org-link-search (s &optional type avoid-pos)
  7971. "Search for a link search option.
  7972. If S is surrounded by forward slashes, it is interpreted as a
  7973. regular expression. In org-mode files, this will create an `org-occur'
  7974. sparse tree. In ordinary files, `occur' will be used to list matches.
  7975. If the current buffer is in `dired-mode', grep will be used to search
  7976. in all files. If AVOID-POS is given, ignore matches near that position."
  7977. (let ((case-fold-search t)
  7978. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7979. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7980. (append '(("") (" ") ("\t") ("\n"))
  7981. org-emphasis-alist)
  7982. "\\|") "\\)"))
  7983. (pos (point))
  7984. (pre nil) (post nil)
  7985. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7986. (cond
  7987. ;; First check if there are any special
  7988. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7989. ;; Now try the builtin stuff
  7990. ((and (equal (string-to-char s0) ?#)
  7991. (> (length s0) 1)
  7992. (save-excursion
  7993. (goto-char (point-min))
  7994. (and
  7995. (re-search-forward
  7996. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7997. (setq type 'dedicated
  7998. pos (match-beginning 0))))
  7999. ;; There is an exact target for this
  8000. (goto-char pos)
  8001. (org-back-to-heading t)))
  8002. ((save-excursion
  8003. (goto-char (point-min))
  8004. (and
  8005. (re-search-forward
  8006. (concat "<<" (regexp-quote s0) ">>") nil t)
  8007. (setq type 'dedicated
  8008. pos (match-beginning 0))))
  8009. ;; There is an exact target for this
  8010. (goto-char pos))
  8011. ((and (string-match "^(\\(.*\\))$" s0)
  8012. (save-excursion
  8013. (goto-char (point-min))
  8014. (and
  8015. (re-search-forward
  8016. (concat "[^[]" (regexp-quote
  8017. (format org-coderef-label-format
  8018. (match-string 1 s0))))
  8019. nil t)
  8020. (setq type 'dedicated
  8021. pos (1+ (match-beginning 0))))))
  8022. ;; There is a coderef target for this
  8023. (goto-char pos))
  8024. ((string-match "^/\\(.*\\)/$" s)
  8025. ;; A regular expression
  8026. (cond
  8027. ((org-mode-p)
  8028. (org-occur (match-string 1 s)))
  8029. ;;((eq major-mode 'dired-mode)
  8030. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8031. (t (org-do-occur (match-string 1 s)))))
  8032. (t
  8033. ;; A normal search strings
  8034. (when (equal (string-to-char s) ?*)
  8035. ;; Anchor on headlines, post may include tags.
  8036. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8037. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  8038. s (substring s 1)))
  8039. (remove-text-properties
  8040. 0 (length s)
  8041. '(face nil mouse-face nil keymap nil fontified nil) s)
  8042. ;; Make a series of regular expressions to find a match
  8043. (setq words (org-split-string s "[ \n\r\t]+")
  8044. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8045. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8046. "\\)" markers)
  8047. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8048. re2a (concat "[ \t\r\n]" re2a_)
  8049. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8050. re4 (concat "[^a-zA-Z_]" re4_)
  8051. re1 (concat pre re2 post)
  8052. re3 (concat pre (if pre re4_ re4) post)
  8053. re5 (concat pre ".*" re4)
  8054. re2 (concat pre re2)
  8055. re2a (concat pre (if pre re2a_ re2a))
  8056. re4 (concat pre (if pre re4_ re4))
  8057. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8058. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8059. re5 "\\)"
  8060. ))
  8061. (cond
  8062. ((eq type 'org-occur) (org-occur reall))
  8063. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8064. (t (goto-char (point-min))
  8065. (setq type 'fuzzy)
  8066. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8067. (org-search-not-self 1 re1 nil t)
  8068. (org-search-not-self 1 re2 nil t)
  8069. (org-search-not-self 1 re2a nil t)
  8070. (org-search-not-self 1 re3 nil t)
  8071. (org-search-not-self 1 re4 nil t)
  8072. (org-search-not-self 1 re5 nil t)
  8073. )
  8074. (goto-char (match-beginning 1))
  8075. (goto-char pos)
  8076. (error "No match")))))
  8077. (t
  8078. ;; Normal string-search
  8079. (goto-char (point-min))
  8080. (if (search-forward s nil t)
  8081. (goto-char (match-beginning 0))
  8082. (error "No match"))))
  8083. (and (org-mode-p) (org-show-context 'link-search))
  8084. type))
  8085. (defun org-search-not-self (group &rest args)
  8086. "Execute `re-search-forward', but only accept matches that do not
  8087. enclose the position of `org-open-link-marker'."
  8088. (let ((m org-open-link-marker))
  8089. (catch 'exit
  8090. (while (apply 're-search-forward args)
  8091. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8092. (goto-char (match-end group))
  8093. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8094. (> (match-beginning 0) (marker-position m))
  8095. (< (match-end 0) (marker-position m)))
  8096. (save-match-data
  8097. (or (not (org-in-regexp
  8098. org-bracket-link-analytic-regexp 1))
  8099. (not (match-end 4)) ; no description
  8100. (and (<= (match-beginning 4) (point))
  8101. (>= (match-end 4) (point))))))
  8102. (throw 'exit (point))))))))
  8103. (defun org-get-buffer-for-internal-link (buffer)
  8104. "Return a buffer to be used for displaying the link target of internal links."
  8105. (cond
  8106. ((not org-display-internal-link-with-indirect-buffer)
  8107. buffer)
  8108. ((string-match "(Clone)$" (buffer-name buffer))
  8109. (message "Buffer is already a clone, not making another one")
  8110. ;; we also do not modify visibility in this case
  8111. buffer)
  8112. (t ; make a new indirect buffer for displaying the link
  8113. (let* ((bn (buffer-name buffer))
  8114. (ibn (concat bn "(Clone)"))
  8115. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8116. (with-current-buffer ib (org-overview))
  8117. ib))))
  8118. (defun org-do-occur (regexp &optional cleanup)
  8119. "Call the Emacs command `occur'.
  8120. If CLEANUP is non-nil, remove the printout of the regular expression
  8121. in the *Occur* buffer. This is useful if the regex is long and not useful
  8122. to read."
  8123. (occur regexp)
  8124. (when cleanup
  8125. (let ((cwin (selected-window)) win beg end)
  8126. (when (setq win (get-buffer-window "*Occur*"))
  8127. (select-window win))
  8128. (goto-char (point-min))
  8129. (when (re-search-forward "match[a-z]+" nil t)
  8130. (setq beg (match-end 0))
  8131. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8132. (setq end (1- (match-beginning 0)))))
  8133. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8134. (goto-char (point-min))
  8135. (select-window cwin))))
  8136. ;;; The mark ring for links jumps
  8137. (defvar org-mark-ring nil
  8138. "Mark ring for positions before jumps in Org-mode.")
  8139. (defvar org-mark-ring-last-goto nil
  8140. "Last position in the mark ring used to go back.")
  8141. ;; Fill and close the ring
  8142. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8143. (loop for i from 1 to org-mark-ring-length do
  8144. (push (make-marker) org-mark-ring))
  8145. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8146. org-mark-ring)
  8147. (defun org-mark-ring-push (&optional pos buffer)
  8148. "Put the current position or POS into the mark ring and rotate it."
  8149. (interactive)
  8150. (setq pos (or pos (point)))
  8151. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8152. (move-marker (car org-mark-ring)
  8153. (or pos (point))
  8154. (or buffer (current-buffer)))
  8155. (message "%s"
  8156. (substitute-command-keys
  8157. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8158. (defun org-mark-ring-goto (&optional n)
  8159. "Jump to the previous position in the mark ring.
  8160. With prefix arg N, jump back that many stored positions. When
  8161. called several times in succession, walk through the entire ring.
  8162. Org-mode commands jumping to a different position in the current file,
  8163. or to another Org-mode file, automatically push the old position
  8164. onto the ring."
  8165. (interactive "p")
  8166. (let (p m)
  8167. (if (eq last-command this-command)
  8168. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8169. (setq p org-mark-ring))
  8170. (setq org-mark-ring-last-goto p)
  8171. (setq m (car p))
  8172. (switch-to-buffer (marker-buffer m))
  8173. (goto-char m)
  8174. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8175. (defun org-remove-angle-brackets (s)
  8176. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8177. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8178. s)
  8179. (defun org-add-angle-brackets (s)
  8180. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8181. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8182. s)
  8183. (defun org-remove-double-quotes (s)
  8184. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8185. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8186. s)
  8187. ;;; Following specific links
  8188. (defun org-follow-timestamp-link ()
  8189. (cond
  8190. ((org-at-date-range-p t)
  8191. (let ((org-agenda-start-on-weekday)
  8192. (t1 (match-string 1))
  8193. (t2 (match-string 2)))
  8194. (setq t1 (time-to-days (org-time-string-to-time t1))
  8195. t2 (time-to-days (org-time-string-to-time t2)))
  8196. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8197. ((org-at-timestamp-p t)
  8198. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8199. (substring (match-string 1) 0 10)))
  8200. 1))
  8201. (t (error "This should not happen"))))
  8202. ;;; Following file links
  8203. (defvar org-wait nil)
  8204. (defun org-open-file (path &optional in-emacs line search)
  8205. "Open the file at PATH.
  8206. First, this expands any special file name abbreviations. Then the
  8207. configuration variable `org-file-apps' is checked if it contains an
  8208. entry for this file type, and if yes, the corresponding command is launched.
  8209. If no application is found, Emacs simply visits the file.
  8210. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8211. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  8212. and to use an external application to visit the file.
  8213. Optional LINE specifies a line to go to, optional SEARCH a string to
  8214. search for. If LINE or SEARCH is given, but IN-EMACS is nil, it will
  8215. be assumed that org-open-file was called to open a file: link, and the
  8216. original link to match against org-file-apps will be reconstructed
  8217. from PATH and whichever of LINE or SEARCH is given.
  8218. If the file does not exist, an error is thrown."
  8219. (let* ((file (if (equal path "")
  8220. buffer-file-name
  8221. (substitute-in-file-name (expand-file-name path))))
  8222. (apps (append org-file-apps (org-default-apps)))
  8223. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8224. (dirp (if remp nil (file-directory-p file)))
  8225. (file (if (and dirp org-open-directory-means-index-dot-org)
  8226. (concat (file-name-as-directory file) "index.org")
  8227. file))
  8228. (a-m-a-p (assq 'auto-mode apps))
  8229. (dfile (downcase file))
  8230. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8231. (link (cond ((and (eq line nil)
  8232. (eq search nil))
  8233. file)
  8234. (line
  8235. (concat file "::" (number-to-string line)))
  8236. (search
  8237. (concat file "::" search))))
  8238. (dlink (downcase link))
  8239. (old-buffer (current-buffer))
  8240. (old-pos (point))
  8241. (old-mode major-mode)
  8242. ext cmd link-match-data)
  8243. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8244. (setq ext (match-string 1 dfile))
  8245. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8246. (setq ext (match-string 1 dfile))))
  8247. (cond
  8248. ((member in-emacs '((16) system))
  8249. (setq cmd (cdr (assoc 'system apps))))
  8250. (in-emacs (setq cmd 'emacs))
  8251. (t
  8252. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8253. (and dirp (cdr (assoc 'directory apps)))
  8254. ;; if we find a match in org-file-apps, store the match
  8255. ;; data for later
  8256. (let ((match (assoc-default dlink (org-apps-regexp-alist
  8257. apps a-m-a-p)
  8258. 'string-match)))
  8259. (if match
  8260. (progn (setq link-match-data (match-data))
  8261. match)
  8262. nil))
  8263. (cdr (assoc ext apps))
  8264. (cdr (assoc t apps))))))
  8265. (when (eq cmd 'system)
  8266. (setq cmd (cdr (assoc 'system apps))))
  8267. (when (eq cmd 'default)
  8268. (setq cmd (cdr (assoc t apps))))
  8269. (when (eq cmd 'mailcap)
  8270. (require 'mailcap)
  8271. (mailcap-parse-mailcaps)
  8272. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8273. (command (mailcap-mime-info mime-type)))
  8274. (if (stringp command)
  8275. (setq cmd command)
  8276. (setq cmd 'emacs))))
  8277. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8278. (not (file-exists-p file))
  8279. (not org-open-non-existing-files))
  8280. (error "No such file: %s" file))
  8281. (cond
  8282. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8283. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8284. (while (string-match "['\"]%s['\"]" cmd)
  8285. (setq cmd (replace-match "%s" t t cmd)))
  8286. (while (string-match "%s" cmd)
  8287. (setq cmd (replace-match
  8288. (save-match-data
  8289. (shell-quote-argument
  8290. (convert-standard-filename file)))
  8291. t t cmd)))
  8292. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8293. (save-match-data
  8294. (let ((match-index 1)
  8295. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8296. (set-match-data link-match-data)
  8297. (while (<= match-index number-of-groups)
  8298. (let ((regex (concat "%" (number-to-string match-index)))
  8299. (replace-with (match-string match-index dlink)))
  8300. (while (string-match regex cmd)
  8301. (setq cmd (replace-match replace-with t t cmd))))
  8302. (setq match-index (+ match-index 1)))))
  8303. (save-window-excursion
  8304. (start-process-shell-command cmd nil cmd)
  8305. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8306. ))
  8307. ((or (stringp cmd)
  8308. (eq cmd 'emacs))
  8309. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8310. (widen)
  8311. (if line (org-goto-line line)
  8312. (if search (org-link-search search))))
  8313. ((consp cmd)
  8314. (let ((file (convert-standard-filename file)))
  8315. (save-match-data
  8316. (set-match-data link-match-data)
  8317. (eval cmd))))
  8318. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8319. (and (org-mode-p) (eq old-mode 'org-mode)
  8320. (or (not (equal old-buffer (current-buffer)))
  8321. (not (equal old-pos (point))))
  8322. (org-mark-ring-push old-pos old-buffer))))
  8323. (defun org-default-apps ()
  8324. "Return the default applications for this operating system."
  8325. (cond
  8326. ((eq system-type 'darwin)
  8327. org-file-apps-defaults-macosx)
  8328. ((eq system-type 'windows-nt)
  8329. org-file-apps-defaults-windowsnt)
  8330. (t org-file-apps-defaults-gnu)))
  8331. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8332. "Convert extensions to regular expressions in the cars of LIST.
  8333. Also, weed out any non-string entries, because the return value is used
  8334. only for regexp matching.
  8335. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8336. point to the symbol `emacs', indicating that the file should
  8337. be opened in Emacs."
  8338. (append
  8339. (delq nil
  8340. (mapcar (lambda (x)
  8341. (if (not (stringp (car x)))
  8342. nil
  8343. (if (string-match "\\W" (car x))
  8344. x
  8345. (cons (concat "\\." (car x) "\\(::.*\\)?\\'")
  8346. (cdr x)))))
  8347. list))
  8348. (if add-auto-mode
  8349. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8350. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8351. (defun org-file-remote-p (file)
  8352. "Test whether FILE specifies a location on a remote system.
  8353. Return non-nil if the location is indeed remote.
  8354. For example, the filename \"/user@host:/foo\" specifies a location
  8355. on the system \"/user@host:\"."
  8356. (cond ((fboundp 'file-remote-p)
  8357. (file-remote-p file))
  8358. ((fboundp 'tramp-handle-file-remote-p)
  8359. (tramp-handle-file-remote-p file))
  8360. ((and (boundp 'ange-ftp-name-format)
  8361. (string-match (car ange-ftp-name-format) file))
  8362. t)
  8363. (t nil)))
  8364. ;;;; Refiling
  8365. (defun org-get-org-file ()
  8366. "Read a filename, with default directory `org-directory'."
  8367. (let ((default (or org-default-notes-file remember-data-file)))
  8368. (read-file-name (format "File name [%s]: " default)
  8369. (file-name-as-directory org-directory)
  8370. default)))
  8371. (defun org-notes-order-reversed-p ()
  8372. "Check if the current file should receive notes in reversed order."
  8373. (cond
  8374. ((not org-reverse-note-order) nil)
  8375. ((eq t org-reverse-note-order) t)
  8376. ((not (listp org-reverse-note-order)) nil)
  8377. (t (catch 'exit
  8378. (let ((all org-reverse-note-order)
  8379. entry)
  8380. (while (setq entry (pop all))
  8381. (if (string-match (car entry) buffer-file-name)
  8382. (throw 'exit (cdr entry))))
  8383. nil)))))
  8384. (defvar org-refile-target-table nil
  8385. "The list of refile targets, created by `org-refile'.")
  8386. (defvar org-agenda-new-buffers nil
  8387. "Buffers created to visit agenda files.")
  8388. (defun org-get-refile-targets (&optional default-buffer)
  8389. "Produce a table with refile targets."
  8390. (let ((case-fold-search nil)
  8391. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8392. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8393. targets txt re files f desc descre fast-path-p level pos0)
  8394. (message "Getting targets...")
  8395. (with-current-buffer (or default-buffer (current-buffer))
  8396. (while (setq entry (pop entries))
  8397. (setq files (car entry) desc (cdr entry))
  8398. (setq fast-path-p nil)
  8399. (cond
  8400. ((null files) (setq files (list (current-buffer))))
  8401. ((eq files 'org-agenda-files)
  8402. (setq files (org-agenda-files 'unrestricted)))
  8403. ((and (symbolp files) (fboundp files))
  8404. (setq files (funcall files)))
  8405. ((and (symbolp files) (boundp files))
  8406. (setq files (symbol-value files))))
  8407. (if (stringp files) (setq files (list files)))
  8408. (cond
  8409. ((eq (car desc) :tag)
  8410. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8411. ((eq (car desc) :todo)
  8412. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8413. ((eq (car desc) :regexp)
  8414. (setq descre (cdr desc)))
  8415. ((eq (car desc) :level)
  8416. (setq descre (concat "^\\*\\{" (number-to-string
  8417. (if org-odd-levels-only
  8418. (1- (* 2 (cdr desc)))
  8419. (cdr desc)))
  8420. "\\}[ \t]")))
  8421. ((eq (car desc) :maxlevel)
  8422. (setq fast-path-p t)
  8423. (setq descre (concat "^\\*\\{1," (number-to-string
  8424. (if org-odd-levels-only
  8425. (1- (* 2 (cdr desc)))
  8426. (cdr desc)))
  8427. "\\}[ \t]")))
  8428. (t (error "Bad refiling target description %s" desc)))
  8429. (while (setq f (pop files))
  8430. (with-current-buffer
  8431. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8432. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  8433. (setq f (and f (expand-file-name f)))
  8434. (if (eq org-refile-use-outline-path 'file)
  8435. (push (list (file-name-nondirectory f) f nil nil) targets))
  8436. (save-excursion
  8437. (save-restriction
  8438. (widen)
  8439. (goto-char (point-min))
  8440. (while (re-search-forward descre nil t)
  8441. (goto-char (setq pos0 (point-at-bol)))
  8442. (catch 'next
  8443. (when org-refile-target-verify-function
  8444. (save-match-data
  8445. (or (funcall org-refile-target-verify-function)
  8446. (throw 'next t))))
  8447. (when (looking-at org-complex-heading-regexp)
  8448. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  8449. txt (org-link-display-format (match-string 4))
  8450. re (concat "^" (regexp-quote
  8451. (buffer-substring (match-beginning 1)
  8452. (match-end 4)))))
  8453. (if (match-end 5) (setq re (concat re "[ \t]+"
  8454. (regexp-quote
  8455. (match-string 5)))))
  8456. (setq re (concat re "[ \t]*$"))
  8457. (when org-refile-use-outline-path
  8458. (setq txt (mapconcat 'org-protect-slash
  8459. (append
  8460. (if (eq org-refile-use-outline-path 'file)
  8461. (list (file-name-nondirectory
  8462. (buffer-file-name (buffer-base-buffer))))
  8463. (if (eq org-refile-use-outline-path 'full-file-path)
  8464. (list (buffer-file-name (buffer-base-buffer)))))
  8465. (org-get-outline-path fast-path-p level txt)
  8466. (list txt))
  8467. "/")))
  8468. (push (list txt f re (point)) targets)))
  8469. (when (= (point) pos0)
  8470. ;; verification function has not moved point
  8471. (goto-char (point-at-eol))))))))))
  8472. (message "Getting targets...done")
  8473. (nreverse targets)))
  8474. (defun org-protect-slash (s)
  8475. (while (string-match "/" s)
  8476. (setq s (replace-match "\\" t t s)))
  8477. s)
  8478. (defvar org-olpa (make-vector 20 nil))
  8479. (defun org-get-outline-path (&optional fastp level heading)
  8480. "Return the outline path to the current entry, as a list.
  8481. The parameters FASTP, LEVEL, and HEADING are for use be a scanner
  8482. routine which makes outline path derivations for an entire file,
  8483. avoiding backtracing."
  8484. (if fastp
  8485. (progn
  8486. (if (> level 19)
  8487. (error "Outline path failure, more than 19 levels."))
  8488. (loop for i from level upto 19 do
  8489. (aset org-olpa i nil))
  8490. (prog1
  8491. (delq nil (append org-olpa nil))
  8492. (aset org-olpa level heading)))
  8493. (let (rtn case-fold-search)
  8494. (save-excursion
  8495. (save-restriction
  8496. (widen)
  8497. (while (org-up-heading-safe)
  8498. (when (looking-at org-complex-heading-regexp)
  8499. (push (org-match-string-no-properties 4) rtn)))
  8500. rtn)))))
  8501. (defun org-format-outline-path (path &optional width prefix)
  8502. "Format the outlie path PATH for display.
  8503. Width is the maximum number of characters that is available.
  8504. Prefix is a prefix to be included in the returned string,
  8505. such as the file name."
  8506. (setq width (or width 79))
  8507. (if prefix (setq width (- width (length prefix))))
  8508. (if (not path)
  8509. (or prefix "")
  8510. (let* ((nsteps (length path))
  8511. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8512. (maxwidth (if (<= total-width width)
  8513. 10000 ;; everything fits
  8514. ;; we need to shorten the level headings
  8515. (/ (- width nsteps) nsteps)))
  8516. (org-odd-levels-only nil)
  8517. (n 0)
  8518. (total (1+ (length prefix))))
  8519. (setq maxwidth (max maxwidth 10))
  8520. (concat prefix
  8521. (mapconcat
  8522. (lambda (h)
  8523. (setq n (1+ n))
  8524. (if (and (= n nsteps) (< maxwidth 10000))
  8525. (setq maxwidth (- total-width total)))
  8526. (if (< (length h) maxwidth)
  8527. (progn (setq total (+ total (length h) 1)) h)
  8528. (setq h (substring h 0 (- maxwidth 2))
  8529. total (+ total maxwidth 1))
  8530. (if (string-match "[ \t]+\\'" h)
  8531. (setq h (substring h 0 (match-beginning 0))))
  8532. (setq h (concat h "..")))
  8533. (org-add-props h nil 'face
  8534. (nth (% (1- n) org-n-level-faces)
  8535. org-level-faces))
  8536. h)
  8537. path "/")))))
  8538. (defun org-display-outline-path (&optional file current)
  8539. "Display the current outline path in the echo area."
  8540. (interactive "P")
  8541. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  8542. (case-fold-search nil)
  8543. (path (and (org-mode-p) (org-get-outline-path))))
  8544. (if current (setq path (append path
  8545. (save-excursion
  8546. (org-back-to-heading t)
  8547. (if (looking-at org-complex-heading-regexp)
  8548. (list (match-string 4)))))))
  8549. (message "%s"
  8550. (org-format-outline-path
  8551. path
  8552. (1- (frame-width))
  8553. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  8554. (defvar org-refile-history nil
  8555. "History for refiling operations.")
  8556. (defvar org-after-refile-insert-hook nil
  8557. "Hook run after `org-refile' has inserted its stuff at the new location.
  8558. Note that this is still *before* the stuff will be removed from
  8559. the *old* location.")
  8560. (defun org-refile (&optional goto default-buffer rfloc)
  8561. "Move the entry at point to another heading.
  8562. The list of target headings is compiled using the information in
  8563. `org-refile-targets', which see. This list is created before each use
  8564. and will therefore always be up-to-date.
  8565. At the target location, the entry is filed as a subitem of the target heading.
  8566. Depending on `org-reverse-note-order', the new subitem will either be the
  8567. first or the last subitem.
  8568. If there is an active region, all entries in that region will be moved.
  8569. However, the region must fulfil the requirement that the first heading
  8570. is the first one sets the top-level of the moved text - at most siblings
  8571. below it are allowed.
  8572. With prefix arg GOTO, the command will only visit the target location,
  8573. not actually move anything.
  8574. With a double prefix `C-u C-u', go to the location where the last refiling
  8575. operation has put the subtree.
  8576. With a prefix argument of `2', refile to the running clock.
  8577. RFLOC can be a refile location obtained in a different way.
  8578. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  8579. (interactive "P")
  8580. (let* ((cbuf (current-buffer))
  8581. (regionp (org-region-active-p))
  8582. (region-start (and regionp (region-beginning)))
  8583. (region-end (and regionp (region-end)))
  8584. (region-length (and regionp (- region-end region-start)))
  8585. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8586. pos it nbuf file re level reversed)
  8587. (setq last-command nil)
  8588. (when regionp
  8589. (goto-char region-start)
  8590. (or (bolp) (goto-char (point-at-bol)))
  8591. (setq region-start (point))
  8592. (unless (org-kill-is-subtree-p
  8593. (buffer-substring region-start region-end))
  8594. (error "The region is not a (sequence of) subtree(s)")))
  8595. (if (equal goto '(16))
  8596. (org-refile-goto-last-stored)
  8597. (when (or
  8598. (and (equal goto 2)
  8599. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8600. (prog1
  8601. (setq it (list (or org-clock-heading "running clock")
  8602. (buffer-file-name
  8603. (marker-buffer org-clock-hd-marker))
  8604. ""
  8605. (marker-position org-clock-hd-marker)))
  8606. (setq goto nil)))
  8607. (setq it (or rfloc
  8608. (save-excursion
  8609. (org-refile-get-location
  8610. (if goto "Goto: " "Refile to: ") default-buffer
  8611. org-refile-allow-creating-parent-nodes)))))
  8612. (setq file (nth 1 it)
  8613. re (nth 2 it)
  8614. pos (nth 3 it))
  8615. (if (and (not goto)
  8616. pos
  8617. (equal (buffer-file-name) file)
  8618. (if regionp
  8619. (and (>= pos region-start)
  8620. (<= pos region-end))
  8621. (and (>= pos (point))
  8622. (< pos (save-excursion
  8623. (org-end-of-subtree t t))))))
  8624. (error "Cannot refile to position inside the tree or region"))
  8625. (setq nbuf (or (find-buffer-visiting file)
  8626. (find-file-noselect file)))
  8627. (if goto
  8628. (progn
  8629. (switch-to-buffer nbuf)
  8630. (goto-char pos)
  8631. (org-show-context 'org-goto))
  8632. (if regionp
  8633. (progn
  8634. (org-kill-new (buffer-substring region-start region-end))
  8635. (org-save-markers-in-region region-start region-end))
  8636. (org-copy-subtree 1 nil t))
  8637. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8638. (find-file-noselect file)))
  8639. (setq reversed (org-notes-order-reversed-p))
  8640. (save-excursion
  8641. (save-restriction
  8642. (widen)
  8643. (if pos
  8644. (progn
  8645. (goto-char pos)
  8646. (looking-at outline-regexp)
  8647. (setq level (org-get-valid-level (funcall outline-level) 1))
  8648. (goto-char
  8649. (if reversed
  8650. (or (outline-next-heading) (point-max))
  8651. (or (save-excursion (org-get-next-sibling))
  8652. (org-end-of-subtree t t)
  8653. (point-max)))))
  8654. (setq level 1)
  8655. (if (not reversed)
  8656. (goto-char (point-max))
  8657. (goto-char (point-min))
  8658. (or (outline-next-heading) (goto-char (point-max)))))
  8659. (if (not (bolp)) (newline))
  8660. (org-paste-subtree level)
  8661. (when org-log-refile
  8662. (org-add-log-setup 'refile nil nil 'findpos
  8663. org-log-refile)
  8664. (unless (eq org-log-refile 'note)
  8665. (save-excursion (org-add-log-note))))
  8666. (and org-auto-align-tags (org-set-tags nil t))
  8667. (bookmark-set "org-refile-last-stored")
  8668. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8669. (run-hooks 'org-after-refile-insert-hook))))
  8670. (if regionp
  8671. (delete-region (point) (+ (point) region-length))
  8672. (org-cut-subtree))
  8673. (when (featurep 'org-inlinetask)
  8674. (org-inlinetask-remove-END-maybe))
  8675. (setq org-markers-to-move nil)
  8676. (message "Refiled to \"%s\"" (car it))))))
  8677. (org-reveal))
  8678. (defun org-refile-goto-last-stored ()
  8679. "Go to the location where the last refile was stored."
  8680. (interactive)
  8681. (bookmark-jump "org-refile-last-stored")
  8682. (message "This is the location of the last refile"))
  8683. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8684. "Prompt the user for a refile location, using PROMPT."
  8685. (let ((org-refile-targets org-refile-targets)
  8686. (org-refile-use-outline-path org-refile-use-outline-path))
  8687. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8688. (unless org-refile-target-table
  8689. (error "No refile targets"))
  8690. (let* ((cbuf (current-buffer))
  8691. (partial-completion-mode nil)
  8692. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8693. (cfunc (if (and org-refile-use-outline-path
  8694. org-outline-path-complete-in-steps)
  8695. 'org-olpath-completing-read
  8696. 'org-icompleting-read))
  8697. (extra (if org-refile-use-outline-path "/" ""))
  8698. (filename (and cfn (expand-file-name cfn)))
  8699. (tbl (mapcar
  8700. (lambda (x)
  8701. (if (and (not (member org-refile-use-outline-path
  8702. '(file full-file-path)))
  8703. (not (equal filename (nth 1 x))))
  8704. (cons (concat (car x) extra " ("
  8705. (file-name-nondirectory (nth 1 x)) ")")
  8706. (cdr x))
  8707. (cons (concat (car x) extra) (cdr x))))
  8708. org-refile-target-table))
  8709. (completion-ignore-case t)
  8710. pa answ parent-target child parent old-hist)
  8711. (setq old-hist org-refile-history)
  8712. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8713. nil 'org-refile-history))
  8714. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8715. (if pa
  8716. (progn
  8717. (when (or (not org-refile-history)
  8718. (not (eq old-hist org-refile-history))
  8719. (not (equal (car pa) (car org-refile-history))))
  8720. (setq org-refile-history
  8721. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8722. org-refile-history
  8723. (cdr org-refile-history))))
  8724. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8725. (pop org-refile-history)))
  8726. pa)
  8727. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8728. (progn
  8729. (setq parent (match-string 1 answ)
  8730. child (match-string 2 answ))
  8731. (setq parent-target (or (assoc parent tbl)
  8732. (assoc (concat parent "/") tbl)))
  8733. (when (and parent-target
  8734. (or (eq new-nodes t)
  8735. (and (eq new-nodes 'confirm)
  8736. (y-or-n-p (format "Create new node \"%s\"? "
  8737. child)))))
  8738. (org-refile-new-child parent-target child)))
  8739. (error "Invalid target location")))))
  8740. (defun org-refile-new-child (parent-target child)
  8741. "Use refile target PARENT-TARGET to add new CHILD below it."
  8742. (unless parent-target
  8743. (error "Cannot find parent for new node"))
  8744. (let ((file (nth 1 parent-target))
  8745. (pos (nth 3 parent-target))
  8746. level)
  8747. (with-current-buffer (or (find-buffer-visiting file)
  8748. (find-file-noselect file))
  8749. (save-excursion
  8750. (save-restriction
  8751. (widen)
  8752. (if pos
  8753. (goto-char pos)
  8754. (goto-char (point-max))
  8755. (if (not (bolp)) (newline)))
  8756. (when (looking-at outline-regexp)
  8757. (setq level (funcall outline-level))
  8758. (org-end-of-subtree t t))
  8759. (org-back-over-empty-lines)
  8760. (insert "\n" (make-string
  8761. (if pos (org-get-valid-level level 1) 1) ?*)
  8762. " " child "\n")
  8763. (beginning-of-line 0)
  8764. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8765. (defun org-olpath-completing-read (prompt collection &rest args)
  8766. "Read an outline path like a file name."
  8767. (let ((thetable collection)
  8768. (org-completion-use-ido nil) ; does not work with ido.
  8769. (org-completion-use-iswitchb nil)) ; or iswitchb
  8770. (apply
  8771. 'org-icompleting-read prompt
  8772. (lambda (string predicate &optional flag)
  8773. (let (rtn r f (l (length string)))
  8774. (cond
  8775. ((eq flag nil)
  8776. ;; try completion
  8777. (try-completion string thetable))
  8778. ((eq flag t)
  8779. ;; all-completions
  8780. (setq rtn (all-completions string thetable predicate))
  8781. (mapcar
  8782. (lambda (x)
  8783. (setq r (substring x l))
  8784. (if (string-match " ([^)]*)$" x)
  8785. (setq f (match-string 0 x))
  8786. (setq f ""))
  8787. (if (string-match "/" r)
  8788. (concat string (substring r 0 (match-end 0)) f)
  8789. x))
  8790. rtn))
  8791. ((eq flag 'lambda)
  8792. ;; exact match?
  8793. (assoc string thetable)))
  8794. ))
  8795. args)))
  8796. ;;;; Dynamic blocks
  8797. (defun org-find-dblock (name)
  8798. "Find the first dynamic block with name NAME in the buffer.
  8799. If not found, stay at current position and return nil."
  8800. (let (pos)
  8801. (save-excursion
  8802. (goto-char (point-min))
  8803. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8804. nil t)
  8805. (match-beginning 0))))
  8806. (if pos (goto-char pos))
  8807. pos))
  8808. (defconst org-dblock-start-re
  8809. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8810. "Matches the start line of a dynamic block, with parameters.")
  8811. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8812. "Matches the end of a dynamic block.")
  8813. (defun org-create-dblock (plist)
  8814. "Create a dynamic block section, with parameters taken from PLIST.
  8815. PLIST must contain a :name entry which is used as name of the block."
  8816. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8817. (end-of-line 1)
  8818. (newline))
  8819. (let ((col (current-column))
  8820. (name (plist-get plist :name)))
  8821. (insert "#+BEGIN: " name)
  8822. (while plist
  8823. (if (eq (car plist) :name)
  8824. (setq plist (cddr plist))
  8825. (insert " " (prin1-to-string (pop plist)))))
  8826. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8827. (beginning-of-line -2)))
  8828. (defun org-prepare-dblock ()
  8829. "Prepare dynamic block for refresh.
  8830. This empties the block, puts the cursor at the insert position and returns
  8831. the property list including an extra property :name with the block name."
  8832. (unless (looking-at org-dblock-start-re)
  8833. (error "Not at a dynamic block"))
  8834. (let* ((begdel (1+ (match-end 0)))
  8835. (name (org-no-properties (match-string 1)))
  8836. (params (append (list :name name)
  8837. (read (concat "(" (match-string 3) ")")))))
  8838. (save-excursion
  8839. (beginning-of-line 1)
  8840. (skip-chars-forward " \t")
  8841. (setq params (plist-put params :indentation-column (current-column))))
  8842. (unless (re-search-forward org-dblock-end-re nil t)
  8843. (error "Dynamic block not terminated"))
  8844. (setq params
  8845. (append params
  8846. (list :content (buffer-substring
  8847. begdel (match-beginning 0)))))
  8848. (delete-region begdel (match-beginning 0))
  8849. (goto-char begdel)
  8850. (open-line 1)
  8851. params))
  8852. (defun org-map-dblocks (&optional command)
  8853. "Apply COMMAND to all dynamic blocks in the current buffer.
  8854. If COMMAND is not given, use `org-update-dblock'."
  8855. (let ((cmd (or command 'org-update-dblock)))
  8856. (save-excursion
  8857. (goto-char (point-min))
  8858. (while (re-search-forward org-dblock-start-re nil t)
  8859. (goto-char (match-beginning 0))
  8860. (save-excursion
  8861. (condition-case nil
  8862. (funcall cmd)
  8863. (error (message "Error during update of dynamic block"))))
  8864. (unless (re-search-forward org-dblock-end-re nil t)
  8865. (error "Dynamic block not terminated"))))))
  8866. (defun org-dblock-update (&optional arg)
  8867. "User command for updating dynamic blocks.
  8868. Update the dynamic block at point. With prefix ARG, update all dynamic
  8869. blocks in the buffer."
  8870. (interactive "P")
  8871. (if arg
  8872. (org-update-all-dblocks)
  8873. (or (looking-at org-dblock-start-re)
  8874. (org-beginning-of-dblock))
  8875. (org-update-dblock)))
  8876. (defun org-update-dblock ()
  8877. "Update the dynamic block at point
  8878. This means to empty the block, parse for parameters and then call
  8879. the correct writing function."
  8880. (save-window-excursion
  8881. (let* ((pos (point))
  8882. (line (org-current-line))
  8883. (params (org-prepare-dblock))
  8884. (name (plist-get params :name))
  8885. (indent (plist-get params :indentation-column))
  8886. (cmd (intern (concat "org-dblock-write:" name))))
  8887. (message "Updating dynamic block `%s' at line %d..." name line)
  8888. (funcall cmd params)
  8889. (message "Updating dynamic block `%s' at line %d...done" name line)
  8890. (goto-char pos)
  8891. (when (and indent (> indent 0))
  8892. (setq indent (make-string indent ?\ ))
  8893. (save-excursion
  8894. (org-beginning-of-dblock)
  8895. (forward-line 1)
  8896. (while (not (looking-at org-dblock-end-re))
  8897. (insert indent)
  8898. (beginning-of-line 2))
  8899. (when (looking-at org-dblock-end-re)
  8900. (and (looking-at "[ \t]+")
  8901. (replace-match ""))
  8902. (insert indent)))))))
  8903. (defun org-beginning-of-dblock ()
  8904. "Find the beginning of the dynamic block at point.
  8905. Error if there is no such block at point."
  8906. (let ((pos (point))
  8907. beg)
  8908. (end-of-line 1)
  8909. (if (and (re-search-backward org-dblock-start-re nil t)
  8910. (setq beg (match-beginning 0))
  8911. (re-search-forward org-dblock-end-re nil t)
  8912. (> (match-end 0) pos))
  8913. (goto-char beg)
  8914. (goto-char pos)
  8915. (error "Not in a dynamic block"))))
  8916. (defun org-update-all-dblocks ()
  8917. "Update all dynamic blocks in the buffer.
  8918. This function can be used in a hook."
  8919. (when (org-mode-p)
  8920. (org-map-dblocks 'org-update-dblock)))
  8921. ;;;; Completion
  8922. (defconst org-additional-option-like-keywords
  8923. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8924. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8925. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  8926. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  8927. "BEGIN:" "END:"
  8928. "ORGTBL" "TBLFM:" "TBLNAME:"
  8929. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8930. "BEGIN_QUOTE" "END_QUOTE"
  8931. "BEGIN_VERSE" "END_VERSE"
  8932. "BEGIN_CENTER" "END_CENTER"
  8933. "BEGIN_SRC" "END_SRC"
  8934. "CATEGORY" "COLUMNS"
  8935. "CAPTION" "LABEL"
  8936. "SETUPFILE"
  8937. "BIND"
  8938. "MACRO"))
  8939. (defcustom org-structure-template-alist
  8940. '(
  8941. ("s" "#+begin_src ?\n\n#+end_src"
  8942. "<src lang=\"?\">\n\n</src>")
  8943. ("e" "#+begin_example\n?\n#+end_example"
  8944. "<example>\n?\n</example>")
  8945. ("q" "#+begin_quote\n?\n#+end_quote"
  8946. "<quote>\n?\n</quote>")
  8947. ("v" "#+begin_verse\n?\n#+end_verse"
  8948. "<verse>\n?\n/verse>")
  8949. ("c" "#+begin_center\n?\n#+end_center"
  8950. "<center>\n?\n/center>")
  8951. ("l" "#+begin_latex\n?\n#+end_latex"
  8952. "<literal style=\"latex\">\n?\n</literal>")
  8953. ("L" "#+latex: "
  8954. "<literal style=\"latex\">?</literal>")
  8955. ("h" "#+begin_html\n?\n#+end_html"
  8956. "<literal style=\"html\">\n?\n</literal>")
  8957. ("H" "#+html: "
  8958. "<literal style=\"html\">?</literal>")
  8959. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8960. ("A" "#+ascii: ")
  8961. ("i" "#+include %file ?"
  8962. "<include file=%file markup=\"?\">")
  8963. )
  8964. "Structure completion elements.
  8965. This is a list of abbreviation keys and values. The value gets inserted
  8966. it you type @samp{.} followed by the key and then the completion key,
  8967. usually `M-TAB'. %file will be replaced by a file name after prompting
  8968. for the file using completion.
  8969. There are two templates for each key, the first uses the original Org syntax,
  8970. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8971. the default when the /org-mtags.el/ module has been loaded. See also the
  8972. variable `org-mtags-prefer-muse-templates'.
  8973. This is an experimental feature, it is undecided if it is going to stay in."
  8974. :group 'org-completion
  8975. :type '(repeat
  8976. (string :tag "Key")
  8977. (string :tag "Template")
  8978. (string :tag "Muse Template")))
  8979. (defun org-try-structure-completion ()
  8980. "Try to complete a structure template before point.
  8981. This looks for strings like \"<e\" on an otherwise empty line and
  8982. expands them."
  8983. (let ((l (buffer-substring (point-at-bol) (point)))
  8984. a)
  8985. (when (and (looking-at "[ \t]*$")
  8986. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8987. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8988. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8989. (match-beginning 1)) a)
  8990. t)))
  8991. (defun org-complete-expand-structure-template (start cell)
  8992. "Expand a structure template."
  8993. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8994. (rpl (nth (if musep 2 1) cell))
  8995. (ind ""))
  8996. (delete-region start (point))
  8997. (when (string-match "\\`#\\+" rpl)
  8998. (cond
  8999. ((bolp))
  9000. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9001. (setq ind (buffer-substring (point-at-bol) (point))))
  9002. (t (newline))))
  9003. (setq start (point))
  9004. (if (string-match "%file" rpl)
  9005. (setq rpl (replace-match
  9006. (concat
  9007. "\""
  9008. (save-match-data
  9009. (abbreviate-file-name (read-file-name "Include file: ")))
  9010. "\"")
  9011. t t rpl)))
  9012. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9013. (concat "\n" ind)))
  9014. (insert rpl)
  9015. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9016. (defun org-complete (&optional arg)
  9017. "Perform completion on word at point.
  9018. At the beginning of a headline, this completes TODO keywords as given in
  9019. `org-todo-keywords'.
  9020. If the current word is preceded by a backslash, completes the TeX symbols
  9021. that are supported for HTML support.
  9022. If the current word is preceded by \"#+\", completes special words for
  9023. setting file options.
  9024. In the line after \"#+STARTUP:, complete valid keywords.\"
  9025. At all other locations, this simply calls the value of
  9026. `org-completion-fallback-command'."
  9027. (interactive "P")
  9028. (org-without-partial-completion
  9029. (catch 'exit
  9030. (let* ((a nil)
  9031. (end (point))
  9032. (beg1 (save-excursion
  9033. (skip-chars-backward (org-re "[:alnum:]_@"))
  9034. (point)))
  9035. (beg (save-excursion
  9036. (skip-chars-backward "a-zA-Z0-9_:$")
  9037. (point)))
  9038. (confirm (lambda (x) (stringp (car x))))
  9039. (searchhead (equal (char-before beg) ?*))
  9040. (struct
  9041. (when (and (member (char-before beg1) '(?. ?<))
  9042. (setq a (assoc (buffer-substring beg1 (point))
  9043. org-structure-template-alist)))
  9044. (org-complete-expand-structure-template (1- beg1) a)
  9045. (throw 'exit t)))
  9046. (tag (and (equal (char-before beg1) ?:)
  9047. (equal (char-after (point-at-bol)) ?*)))
  9048. (prop (and (equal (char-before beg1) ?:)
  9049. (not (equal (char-after (point-at-bol)) ?*))))
  9050. (texp (equal (char-before beg) ?\\))
  9051. (link (equal (char-before beg) ?\[))
  9052. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  9053. beg)
  9054. "#+"))
  9055. (startup (string-match "^#\\+STARTUP:.*"
  9056. (buffer-substring (point-at-bol) (point))))
  9057. (completion-ignore-case opt)
  9058. (type nil)
  9059. (tbl nil)
  9060. (table (cond
  9061. (opt
  9062. (setq type :opt)
  9063. (require 'org-exp)
  9064. (append
  9065. (delq nil
  9066. (mapcar
  9067. (lambda (x)
  9068. (if (string-match
  9069. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9070. (cons (match-string 2 x)
  9071. (match-string 1 x))))
  9072. (org-split-string (org-get-current-options) "\n")))
  9073. (mapcar 'list org-additional-option-like-keywords)))
  9074. (startup
  9075. (setq type :startup)
  9076. org-startup-options)
  9077. (link (append org-link-abbrev-alist-local
  9078. org-link-abbrev-alist))
  9079. (texp
  9080. (setq type :tex)
  9081. (append org-entities-user org-entities))
  9082. ((string-match "\\`\\*+[ \t]+\\'"
  9083. (buffer-substring (point-at-bol) beg))
  9084. (setq type :todo)
  9085. (mapcar 'list org-todo-keywords-1))
  9086. (searchhead
  9087. (setq type :searchhead)
  9088. (save-excursion
  9089. (goto-char (point-min))
  9090. (while (re-search-forward org-todo-line-regexp nil t)
  9091. (push (list
  9092. (org-make-org-heading-search-string
  9093. (match-string 3) t))
  9094. tbl)))
  9095. tbl)
  9096. (tag (setq type :tag beg beg1)
  9097. (or org-tag-alist (org-get-buffer-tags)))
  9098. (prop (setq type :prop beg beg1)
  9099. (mapcar 'list (org-buffer-property-keys nil t t)))
  9100. (t (progn
  9101. (call-interactively org-completion-fallback-command)
  9102. (throw 'exit nil)))))
  9103. (pattern (buffer-substring-no-properties beg end))
  9104. (completion (try-completion pattern table confirm)))
  9105. (cond ((eq completion t)
  9106. (if (not (assoc (upcase pattern) table))
  9107. (message "Already complete")
  9108. (if (and (equal type :opt)
  9109. (not (member (car (assoc (upcase pattern) table))
  9110. org-additional-option-like-keywords)))
  9111. (insert (substring (cdr (assoc (upcase pattern) table))
  9112. (length pattern)))
  9113. (if (memq type '(:tag :prop)) (insert ":")))))
  9114. ((null completion)
  9115. (message "Can't find completion for \"%s\"" pattern)
  9116. (ding))
  9117. ((not (string= pattern completion))
  9118. (delete-region beg end)
  9119. (if (string-match " +$" completion)
  9120. (setq completion (replace-match "" t t completion)))
  9121. (insert completion)
  9122. (if (get-buffer-window "*Completions*")
  9123. (delete-window (get-buffer-window "*Completions*")))
  9124. (if (assoc completion table)
  9125. (if (eq type :todo) (insert " ")
  9126. (if (memq type '(:tag :prop)) (insert ":"))))
  9127. (if (and (equal type :opt) (assoc completion table))
  9128. (message "%s" (substitute-command-keys
  9129. "Press \\[org-complete] again to insert example settings"))))
  9130. (t
  9131. (message "Making completion list...")
  9132. (let ((list (sort (all-completions pattern table confirm)
  9133. 'string<)))
  9134. (with-output-to-temp-buffer "*Completions*"
  9135. (condition-case nil
  9136. ;; Protection needed for XEmacs and emacs 21
  9137. (display-completion-list list pattern)
  9138. (error (display-completion-list list)))))
  9139. (message "Making completion list...%s" "done")))))))
  9140. ;;;; TODO, DEADLINE, Comments
  9141. (defun org-toggle-comment ()
  9142. "Change the COMMENT state of an entry."
  9143. (interactive)
  9144. (save-excursion
  9145. (org-back-to-heading)
  9146. (let (case-fold-search)
  9147. (if (looking-at (concat outline-regexp
  9148. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9149. (replace-match "" t t nil 1)
  9150. (if (looking-at outline-regexp)
  9151. (progn
  9152. (goto-char (match-end 0))
  9153. (insert org-comment-string " ")))))))
  9154. (defvar org-last-todo-state-is-todo nil
  9155. "This is non-nil when the last TODO state change led to a TODO state.
  9156. If the last change removed the TODO tag or switched to DONE, then
  9157. this is nil.")
  9158. (defvar org-setting-tags nil) ; dynamically skipped
  9159. (defun org-parse-local-options (string var)
  9160. "Parse STRING for startup setting relevant for variable VAR."
  9161. (let ((rtn (symbol-value var))
  9162. e opts)
  9163. (save-match-data
  9164. (if (or (not string) (not (string-match "\\S-" string)))
  9165. rtn
  9166. (setq opts (delq nil (mapcar (lambda (x)
  9167. (setq e (assoc x org-startup-options))
  9168. (if (eq (nth 1 e) var) e nil))
  9169. (org-split-string string "[ \t]+"))))
  9170. (if (not opts)
  9171. rtn
  9172. (setq rtn nil)
  9173. (while (setq e (pop opts))
  9174. (if (not (nth 3 e))
  9175. (setq rtn (nth 2 e))
  9176. (if (not (listp rtn)) (setq rtn nil))
  9177. (push (nth 2 e) rtn)))
  9178. rtn)))))
  9179. (defvar org-todo-setup-filter-hook nil
  9180. "Hook for functions that pre-filter todo specs.
  9181. Each function takes a todo spec and returns either `nil' or the spec
  9182. transformed into canonical form." )
  9183. (defvar org-todo-get-default-hook nil
  9184. "Hook for functions that get a default item for todo.
  9185. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9186. `nil' or a string to be used for the todo mark." )
  9187. (defvar org-agenda-headline-snapshot-before-repeat)
  9188. (defun org-todo (&optional arg)
  9189. "Change the TODO state of an item.
  9190. The state of an item is given by a keyword at the start of the heading,
  9191. like
  9192. *** TODO Write paper
  9193. *** DONE Call mom
  9194. The different keywords are specified in the variable `org-todo-keywords'.
  9195. By default the available states are \"TODO\" and \"DONE\".
  9196. So for this example: when the item starts with TODO, it is changed to DONE.
  9197. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9198. DONE are present, add TODO at the beginning of the heading.
  9199. With C-u prefix arg, use completion to determine the new state.
  9200. With numeric prefix arg, switch to that state.
  9201. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  9202. With a triple C-u prefix, circumvent any state blocking.
  9203. For calling through lisp, arg is also interpreted in the following way:
  9204. 'none -> empty state
  9205. \"\"(empty string) -> switch to empty state
  9206. 'done -> switch to DONE
  9207. 'nextset -> switch to the next set of keywords
  9208. 'previousset -> switch to the previous set of keywords
  9209. \"WAITING\" -> switch to the specified keyword, but only if it
  9210. really is a member of `org-todo-keywords'."
  9211. (interactive "P")
  9212. (if (equal arg '(16)) (setq arg 'nextset))
  9213. (let ((org-blocker-hook org-blocker-hook)
  9214. (case-fold-search nil))
  9215. (when (equal arg '(64))
  9216. (setq arg nil org-blocker-hook nil))
  9217. (when (and org-blocker-hook
  9218. (or org-inhibit-blocking
  9219. (org-entry-get nil "NOBLOCKING")))
  9220. (setq org-blocker-hook nil))
  9221. (save-excursion
  9222. (catch 'exit
  9223. (org-back-to-heading t)
  9224. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9225. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9226. (looking-at " *"))
  9227. (let* ((match-data (match-data))
  9228. (startpos (point-at-bol))
  9229. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  9230. (org-log-done org-log-done)
  9231. (org-log-repeat org-log-repeat)
  9232. (org-todo-log-states org-todo-log-states)
  9233. (this (match-string 1))
  9234. (hl-pos (match-beginning 0))
  9235. (head (org-get-todo-sequence-head this))
  9236. (ass (assoc head org-todo-kwd-alist))
  9237. (interpret (nth 1 ass))
  9238. (done-word (nth 3 ass))
  9239. (final-done-word (nth 4 ass))
  9240. (last-state (or this ""))
  9241. (completion-ignore-case t)
  9242. (member (member this org-todo-keywords-1))
  9243. (tail (cdr member))
  9244. (state (cond
  9245. ((and org-todo-key-trigger
  9246. (or (and (equal arg '(4))
  9247. (eq org-use-fast-todo-selection 'prefix))
  9248. (and (not arg) org-use-fast-todo-selection
  9249. (not (eq org-use-fast-todo-selection
  9250. 'prefix)))))
  9251. ;; Use fast selection
  9252. (org-fast-todo-selection))
  9253. ((and (equal arg '(4))
  9254. (or (not org-use-fast-todo-selection)
  9255. (not org-todo-key-trigger)))
  9256. ;; Read a state with completion
  9257. (org-icompleting-read
  9258. "State: " (mapcar (lambda(x) (list x))
  9259. org-todo-keywords-1)
  9260. nil t))
  9261. ((eq arg 'right)
  9262. (if this
  9263. (if tail (car tail) nil)
  9264. (car org-todo-keywords-1)))
  9265. ((eq arg 'left)
  9266. (if (equal member org-todo-keywords-1)
  9267. nil
  9268. (if this
  9269. (nth (- (length org-todo-keywords-1)
  9270. (length tail) 2)
  9271. org-todo-keywords-1)
  9272. (org-last org-todo-keywords-1))))
  9273. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9274. (setq arg nil))) ; hack to fall back to cycling
  9275. (arg
  9276. ;; user or caller requests a specific state
  9277. (cond
  9278. ((equal arg "") nil)
  9279. ((eq arg 'none) nil)
  9280. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9281. ((eq arg 'nextset)
  9282. (or (car (cdr (member head org-todo-heads)))
  9283. (car org-todo-heads)))
  9284. ((eq arg 'previousset)
  9285. (let ((org-todo-heads (reverse org-todo-heads)))
  9286. (or (car (cdr (member head org-todo-heads)))
  9287. (car org-todo-heads))))
  9288. ((car (member arg org-todo-keywords-1)))
  9289. ((stringp arg)
  9290. (error "State `%s' not valid in this file" arg))
  9291. ((nth (1- (prefix-numeric-value arg))
  9292. org-todo-keywords-1))))
  9293. ((null member) (or head (car org-todo-keywords-1)))
  9294. ((equal this final-done-word) nil) ;; -> make empty
  9295. ((null tail) nil) ;; -> first entry
  9296. ((memq interpret '(type priority))
  9297. (if (eq this-command last-command)
  9298. (car tail)
  9299. (if (> (length tail) 0)
  9300. (or done-word (car org-done-keywords))
  9301. nil)))
  9302. (t
  9303. (car tail))))
  9304. (state (or
  9305. (run-hook-with-args-until-success
  9306. 'org-todo-get-default-hook state last-state)
  9307. state))
  9308. (next (if state (concat " " state " ") " "))
  9309. (change-plist (list :type 'todo-state-change :from this :to state
  9310. :position startpos))
  9311. dolog now-done-p)
  9312. (when org-blocker-hook
  9313. (setq org-last-todo-state-is-todo
  9314. (not (member this org-done-keywords)))
  9315. (unless (save-excursion
  9316. (save-match-data
  9317. (run-hook-with-args-until-failure
  9318. 'org-blocker-hook change-plist)))
  9319. (if (interactive-p)
  9320. (error "TODO state change from %s to %s blocked" this state)
  9321. ;; fail silently
  9322. (message "TODO state change from %s to %s blocked" this state)
  9323. (throw 'exit nil))))
  9324. (store-match-data match-data)
  9325. (replace-match next t t)
  9326. (unless (pos-visible-in-window-p hl-pos)
  9327. (message "TODO state changed to %s" (org-trim next)))
  9328. (unless head
  9329. (setq head (org-get-todo-sequence-head state)
  9330. ass (assoc head org-todo-kwd-alist)
  9331. interpret (nth 1 ass)
  9332. done-word (nth 3 ass)
  9333. final-done-word (nth 4 ass)))
  9334. (when (memq arg '(nextset previousset))
  9335. (message "Keyword-Set %d/%d: %s"
  9336. (- (length org-todo-sets) -1
  9337. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9338. (length org-todo-sets)
  9339. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9340. (setq org-last-todo-state-is-todo
  9341. (not (member state org-done-keywords)))
  9342. (setq now-done-p (and (member state org-done-keywords)
  9343. (not (member this org-done-keywords))))
  9344. (and logging (org-local-logging logging))
  9345. (when (and (or org-todo-log-states org-log-done)
  9346. (not (eq org-inhibit-logging t))
  9347. (not (memq arg '(nextset previousset))))
  9348. ;; we need to look at recording a time and note
  9349. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9350. (nth 2 (assoc this org-todo-log-states))))
  9351. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9352. (setq dolog 'time))
  9353. (when (and state
  9354. (member state org-not-done-keywords)
  9355. (not (member this org-not-done-keywords)))
  9356. ;; This is now a todo state and was not one before
  9357. ;; If there was a CLOSED time stamp, get rid of it.
  9358. (org-add-planning-info nil nil 'closed))
  9359. (when (and now-done-p org-log-done)
  9360. ;; It is now done, and it was not done before
  9361. (org-add-planning-info 'closed (org-current-time))
  9362. (if (and (not dolog) (eq 'note org-log-done))
  9363. (org-add-log-setup 'done state this 'findpos 'note)))
  9364. (when (and state dolog)
  9365. ;; This is a non-nil state, and we need to log it
  9366. (org-add-log-setup 'state state this 'findpos dolog)))
  9367. ;; Fixup tag positioning
  9368. (org-todo-trigger-tag-changes state)
  9369. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9370. (when org-provide-todo-statistics
  9371. (org-update-parent-todo-statistics))
  9372. (run-hooks 'org-after-todo-state-change-hook)
  9373. (if (and arg (not (member state org-done-keywords)))
  9374. (setq head (org-get-todo-sequence-head state)))
  9375. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9376. ;; Do we need to trigger a repeat?
  9377. (when now-done-p
  9378. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9379. ;; This is for the agenda, take a snapshot of the headline.
  9380. (save-match-data
  9381. (setq org-agenda-headline-snapshot-before-repeat
  9382. (org-get-heading))))
  9383. (org-auto-repeat-maybe state))
  9384. ;; Fixup cursor location if close to the keyword
  9385. (if (and (outline-on-heading-p)
  9386. (not (bolp))
  9387. (save-excursion (beginning-of-line 1)
  9388. (looking-at org-todo-line-regexp))
  9389. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9390. (progn
  9391. (goto-char (or (match-end 2) (match-end 1)))
  9392. (and (looking-at " ") (just-one-space))))
  9393. (when org-trigger-hook
  9394. (save-excursion
  9395. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9396. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9397. "Block turning an entry into a TODO, using the hierarchy.
  9398. This checks whether the current task should be blocked from state
  9399. changes. Such blocking occurs when:
  9400. 1. The task has children which are not all in a completed state.
  9401. 2. A task has a parent with the property :ORDERED:, and there
  9402. are siblings prior to the current task with incomplete
  9403. status.
  9404. 3. The parent of the task is blocked because it has siblings that should
  9405. be done first, or is child of a block grandparent TODO entry."
  9406. (if (not org-enforce-todo-dependencies)
  9407. t ; if locally turned off don't block
  9408. (catch 'dont-block
  9409. ;; If this is not a todo state change, or if this entry is already DONE,
  9410. ;; do not block
  9411. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9412. (member (plist-get change-plist :from)
  9413. (cons 'done org-done-keywords))
  9414. (member (plist-get change-plist :to)
  9415. (cons 'todo org-not-done-keywords))
  9416. (not (plist-get change-plist :to)))
  9417. (throw 'dont-block t))
  9418. ;; If this task has children, and any are undone, it's blocked
  9419. (save-excursion
  9420. (org-back-to-heading t)
  9421. (let ((this-level (funcall outline-level)))
  9422. (outline-next-heading)
  9423. (let ((child-level (funcall outline-level)))
  9424. (while (and (not (eobp))
  9425. (> child-level this-level))
  9426. ;; this todo has children, check whether they are all
  9427. ;; completed
  9428. (if (and (not (org-entry-is-done-p))
  9429. (org-entry-is-todo-p))
  9430. (throw 'dont-block nil))
  9431. (outline-next-heading)
  9432. (setq child-level (funcall outline-level))))))
  9433. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9434. ;; any previous siblings are undone, it's blocked
  9435. (save-excursion
  9436. (org-back-to-heading t)
  9437. (let* ((pos (point))
  9438. (parent-pos (and (org-up-heading-safe) (point))))
  9439. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9440. (when (and (org-entry-get (point) "ORDERED")
  9441. (forward-line 1)
  9442. (re-search-forward org-not-done-heading-regexp pos t))
  9443. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9444. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9445. (while t
  9446. (goto-char parent-pos)
  9447. (if (not (looking-at org-not-done-heading-regexp))
  9448. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9449. (setq pos (point))
  9450. (setq parent-pos (and (org-up-heading-safe) (point)))
  9451. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9452. (when (and (org-entry-get (point) "ORDERED")
  9453. (forward-line 1)
  9454. (re-search-forward org-not-done-heading-regexp pos t))
  9455. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9456. (defcustom org-track-ordered-property-with-tag nil
  9457. "Should the ORDERED property also be shown as a tag?
  9458. The ORDERED property decides if an entry should require subtasks to be
  9459. completed in sequence. Since a property is not very visible, setting
  9460. this option means that toggling the ORDERED property with the command
  9461. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9462. not relevant for the behavior, but it makes things more visible.
  9463. Note that toggling the tag with tags commands will not change the property
  9464. and therefore not influence behavior!
  9465. This can be t, meaning the tag ORDERED should be used, It can also be a
  9466. string to select a different tag for this task."
  9467. :group 'org-todo
  9468. :type '(choice
  9469. (const :tag "No tracking" nil)
  9470. (const :tag "Track with ORDERED tag" t)
  9471. (string :tag "Use other tag")))
  9472. (defun org-toggle-ordered-property ()
  9473. "Toggle the ORDERED property of the current entry.
  9474. For better visibility, you can track the value of this property with a tag.
  9475. See variable `org-track-ordered-property-with-tag'."
  9476. (interactive)
  9477. (let* ((t1 org-track-ordered-property-with-tag)
  9478. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9479. (save-excursion
  9480. (org-back-to-heading)
  9481. (if (org-entry-get nil "ORDERED")
  9482. (progn
  9483. (org-delete-property "ORDERED")
  9484. (and tag (org-toggle-tag tag 'off))
  9485. (message "Subtasks can be completed in arbitrary order"))
  9486. (org-entry-put nil "ORDERED" "t")
  9487. (and tag (org-toggle-tag tag 'on))
  9488. (message "Subtasks must be completed in sequence")))))
  9489. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9490. (defun org-block-todo-from-checkboxes (change-plist)
  9491. "Block turning an entry into a TODO, using checkboxes.
  9492. This checks whether the current task should be blocked from state
  9493. changes because there are unchecked boxes in this entry."
  9494. (if (not org-enforce-todo-checkbox-dependencies)
  9495. t ; if locally turned off don't block
  9496. (catch 'dont-block
  9497. ;; If this is not a todo state change, or if this entry is already DONE,
  9498. ;; do not block
  9499. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9500. (member (plist-get change-plist :from)
  9501. (cons 'done org-done-keywords))
  9502. (member (plist-get change-plist :to)
  9503. (cons 'todo org-not-done-keywords))
  9504. (not (plist-get change-plist :to)))
  9505. (throw 'dont-block t))
  9506. ;; If this task has checkboxes that are not checked, it's blocked
  9507. (save-excursion
  9508. (org-back-to-heading t)
  9509. (let ((beg (point)) end)
  9510. (outline-next-heading)
  9511. (setq end (point))
  9512. (goto-char beg)
  9513. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9514. end t)
  9515. (progn
  9516. (if (boundp 'org-blocked-by-checkboxes)
  9517. (setq org-blocked-by-checkboxes t))
  9518. (throw 'dont-block nil)))))
  9519. t))) ; do not block
  9520. (defun org-entry-blocked-p ()
  9521. "Is the current entry blocked?"
  9522. (if (org-entry-get nil "NOBLOCKING")
  9523. nil ;; Never block this entry
  9524. (not
  9525. (run-hook-with-args-until-failure
  9526. 'org-blocker-hook
  9527. (list :type 'todo-state-change
  9528. :position (point)
  9529. :from 'todo
  9530. :to 'done)))))
  9531. (defun org-update-statistics-cookies (all)
  9532. "Update the statistics cookie, either from TODO or from checkboxes.
  9533. This should be called with the cursor in a line with a statistics cookie."
  9534. (interactive "P")
  9535. (if all
  9536. (progn
  9537. (org-update-checkbox-count 'all)
  9538. (org-map-entries 'org-update-parent-todo-statistics))
  9539. (if (not (org-on-heading-p))
  9540. (org-update-checkbox-count)
  9541. (let ((pos (move-marker (make-marker) (point)))
  9542. end l1 l2)
  9543. (ignore-errors (org-back-to-heading t))
  9544. (if (not (org-on-heading-p))
  9545. (org-update-checkbox-count)
  9546. (setq l1 (org-outline-level))
  9547. (setq end (save-excursion
  9548. (outline-next-heading)
  9549. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9550. (point)))
  9551. (if (and (save-excursion
  9552. (re-search-forward
  9553. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  9554. (not (save-excursion (re-search-forward
  9555. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9556. (org-update-checkbox-count)
  9557. (if (and l2 (> l2 l1))
  9558. (progn
  9559. (goto-char end)
  9560. (org-update-parent-todo-statistics))
  9561. (goto-char pos)
  9562. (beginning-of-line 1)
  9563. (while (re-search-forward
  9564. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  9565. (point-at-eol) t)
  9566. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  9567. (goto-char pos)
  9568. (move-marker pos nil)))))
  9569. (defvar org-entry-property-inherited-from) ;; defined below
  9570. (defun org-update-parent-todo-statistics ()
  9571. "Update any statistics cookie in the parent of the current headline.
  9572. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9573. the entire subtree and this will travel up the hierarchy and update
  9574. statistics everywhere."
  9575. (interactive)
  9576. (let* ((lim 0) prop
  9577. (recursive (or (not org-hierarchical-todo-statistics)
  9578. (string-match
  9579. "\\<recursive\\>"
  9580. (or (setq prop (org-entry-get
  9581. nil "COOKIE_DATA" 'inherit)) ""))))
  9582. (lim (or (and prop (marker-position
  9583. org-entry-property-inherited-from))
  9584. lim))
  9585. (first t)
  9586. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9587. level ltoggle l1 new ndel
  9588. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9589. (catch 'exit
  9590. (save-excursion
  9591. (beginning-of-line 1)
  9592. (if (org-at-heading-p)
  9593. (setq ltoggle (funcall outline-level))
  9594. (error "This should not happen"))
  9595. (while (and (setq level (org-up-heading-safe))
  9596. (or recursive first)
  9597. (>= (point) lim))
  9598. (setq first nil cookie-present nil)
  9599. (unless (and level
  9600. (not (string-match
  9601. "\\<checkbox\\>"
  9602. (downcase
  9603. (or (org-entry-get
  9604. nil "COOKIE_DATA")
  9605. "")))))
  9606. (throw 'exit nil))
  9607. (while (re-search-forward box-re (point-at-eol) t)
  9608. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9609. (setq is-percent (match-end 2))
  9610. (save-match-data
  9611. (unless (outline-next-heading) (throw 'exit nil))
  9612. (while (and (looking-at org-complex-heading-regexp)
  9613. (> (setq l1 (length (match-string 1))) level))
  9614. (setq kwd (and (or recursive (= l1 ltoggle))
  9615. (match-string 2)))
  9616. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9617. (and (listp org-provide-todo-statistics)
  9618. (or (member kwd org-provide-todo-statistics)
  9619. (member kwd org-done-keywords))))
  9620. (setq cnt-all (1+ cnt-all))
  9621. (if (eq org-provide-todo-statistics t)
  9622. (and kwd (setq cnt-all (1+ cnt-all)))))
  9623. (and (member kwd org-done-keywords)
  9624. (setq cnt-done (1+ cnt-done)))
  9625. (outline-next-heading)))
  9626. (setq new
  9627. (if is-percent
  9628. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9629. (format "[%d/%d]" cnt-done cnt-all))
  9630. ndel (- (match-end 0) (match-beginning 0)))
  9631. (goto-char (match-beginning 0))
  9632. (insert new)
  9633. (delete-region (point) (+ (point) ndel)))
  9634. (when cookie-present
  9635. (run-hook-with-args 'org-after-todo-statistics-hook
  9636. cnt-done (- cnt-all cnt-done))))))
  9637. (run-hooks 'org-todo-statistics-hook)))
  9638. (defvar org-after-todo-statistics-hook nil
  9639. "Hook that is called after a TODO statistics cookie has been updated.
  9640. Each function is called with two arguments: the number of not-done entries
  9641. and the number of done entries.
  9642. For example, the following function, when added to this hook, will switch
  9643. an entry to DONE when all children are done, and back to TODO when new
  9644. entries are set to a TODO status. Note that this hook is only called
  9645. when there is a statistics cookie in the headline!
  9646. (defun org-summary-todo (n-done n-not-done)
  9647. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9648. (let (org-log-done org-log-states) ; turn off logging
  9649. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9650. ")
  9651. (defvar org-todo-statistics-hook nil
  9652. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9653. This hook runs even if there is no statistics cookie present, in which case
  9654. `org-after-todo-statistics-hook' would not run.")
  9655. (defun org-todo-trigger-tag-changes (state)
  9656. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9657. (let ((l org-todo-state-tags-triggers)
  9658. changes)
  9659. (when (or (not state) (equal state ""))
  9660. (setq changes (append changes (cdr (assoc "" l)))))
  9661. (when (and (stringp state) (> (length state) 0))
  9662. (setq changes (append changes (cdr (assoc state l)))))
  9663. (when (member state org-not-done-keywords)
  9664. (setq changes (append changes (cdr (assoc 'todo l)))))
  9665. (when (member state org-done-keywords)
  9666. (setq changes (append changes (cdr (assoc 'done l)))))
  9667. (dolist (c changes)
  9668. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9669. (defun org-local-logging (value)
  9670. "Get logging settings from a property VALUE."
  9671. (let* (words w a)
  9672. ;; directly set the variables, they are already local.
  9673. (setq org-log-done nil
  9674. org-log-repeat nil
  9675. org-todo-log-states nil)
  9676. (setq words (org-split-string value))
  9677. (while (setq w (pop words))
  9678. (cond
  9679. ((setq a (assoc w org-startup-options))
  9680. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9681. (set (nth 1 a) (nth 2 a))))
  9682. ((setq a (org-extract-log-state-settings w))
  9683. (and (member (car a) org-todo-keywords-1)
  9684. (push a org-todo-log-states)))))))
  9685. (defun org-get-todo-sequence-head (kwd)
  9686. "Return the head of the TODO sequence to which KWD belongs.
  9687. If KWD is not set, check if there is a text property remembering the
  9688. right sequence."
  9689. (let (p)
  9690. (cond
  9691. ((not kwd)
  9692. (or (get-text-property (point-at-bol) 'org-todo-head)
  9693. (progn
  9694. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9695. nil (point-at-eol)))
  9696. (get-text-property p 'org-todo-head))))
  9697. ((not (member kwd org-todo-keywords-1))
  9698. (car org-todo-keywords-1))
  9699. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9700. (defun org-fast-todo-selection ()
  9701. "Fast TODO keyword selection with single keys.
  9702. Returns the new TODO keyword, or nil if no state change should occur."
  9703. (let* ((fulltable org-todo-key-alist)
  9704. (done-keywords org-done-keywords) ;; needed for the faces.
  9705. (maxlen (apply 'max (mapcar
  9706. (lambda (x)
  9707. (if (stringp (car x)) (string-width (car x)) 0))
  9708. fulltable)))
  9709. (expert nil)
  9710. (fwidth (+ maxlen 3 1 3))
  9711. (ncol (/ (- (window-width) 4) fwidth))
  9712. tg cnt e c tbl
  9713. groups ingroup)
  9714. (save-excursion
  9715. (save-window-excursion
  9716. (if expert
  9717. (set-buffer (get-buffer-create " *Org todo*"))
  9718. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9719. (erase-buffer)
  9720. (org-set-local 'org-done-keywords done-keywords)
  9721. (setq tbl fulltable cnt 0)
  9722. (while (setq e (pop tbl))
  9723. (cond
  9724. ((equal e '(:startgroup))
  9725. (push '() groups) (setq ingroup t)
  9726. (when (not (= cnt 0))
  9727. (setq cnt 0)
  9728. (insert "\n"))
  9729. (insert "{ "))
  9730. ((equal e '(:endgroup))
  9731. (setq ingroup nil cnt 0)
  9732. (insert "}\n"))
  9733. ((equal e '(:newline))
  9734. (when (not (= cnt 0))
  9735. (setq cnt 0)
  9736. (insert "\n")
  9737. (setq e (car tbl))
  9738. (while (equal (car tbl) '(:newline))
  9739. (insert "\n")
  9740. (setq tbl (cdr tbl)))))
  9741. (t
  9742. (setq tg (car e) c (cdr e))
  9743. (if ingroup (push tg (car groups)))
  9744. (setq tg (org-add-props tg nil 'face
  9745. (org-get-todo-face tg)))
  9746. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9747. (insert "[" c "] " tg (make-string
  9748. (- fwidth 4 (length tg)) ?\ ))
  9749. (when (= (setq cnt (1+ cnt)) ncol)
  9750. (insert "\n")
  9751. (if ingroup (insert " "))
  9752. (setq cnt 0)))))
  9753. (insert "\n")
  9754. (goto-char (point-min))
  9755. (if (not expert) (org-fit-window-to-buffer))
  9756. (message "[a-z..]:Set [SPC]:clear")
  9757. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9758. (cond
  9759. ((or (= c ?\C-g)
  9760. (and (= c ?q) (not (rassoc c fulltable))))
  9761. (setq quit-flag t))
  9762. ((= c ?\ ) nil)
  9763. ((setq e (rassoc c fulltable) tg (car e))
  9764. tg)
  9765. (t (setq quit-flag t)))))))
  9766. (defun org-entry-is-todo-p ()
  9767. (member (org-get-todo-state) org-not-done-keywords))
  9768. (defun org-entry-is-done-p ()
  9769. (member (org-get-todo-state) org-done-keywords))
  9770. (defun org-get-todo-state ()
  9771. (save-excursion
  9772. (org-back-to-heading t)
  9773. (and (looking-at org-todo-line-regexp)
  9774. (match-end 2)
  9775. (match-string 2))))
  9776. (defun org-at-date-range-p (&optional inactive-ok)
  9777. "Is the cursor inside a date range?"
  9778. (interactive)
  9779. (save-excursion
  9780. (catch 'exit
  9781. (let ((pos (point)))
  9782. (skip-chars-backward "^[<\r\n")
  9783. (skip-chars-backward "<[")
  9784. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9785. (>= (match-end 0) pos)
  9786. (throw 'exit t))
  9787. (skip-chars-backward "^<[\r\n")
  9788. (skip-chars-backward "<[")
  9789. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9790. (>= (match-end 0) pos)
  9791. (throw 'exit t)))
  9792. nil)))
  9793. (defun org-get-repeat (&optional tagline)
  9794. "Check if there is a deadline/schedule with repeater in this entry."
  9795. (save-match-data
  9796. (save-excursion
  9797. (org-back-to-heading t)
  9798. (and (re-search-forward (if tagline
  9799. (concat tagline "\\s-*" org-repeat-re)
  9800. org-repeat-re)
  9801. (org-entry-end-position) t)
  9802. (match-string-no-properties 1)))))
  9803. (defvar org-last-changed-timestamp)
  9804. (defvar org-last-inserted-timestamp)
  9805. (defvar org-log-post-message)
  9806. (defvar org-log-note-purpose)
  9807. (defvar org-log-note-how)
  9808. (defvar org-log-note-extra)
  9809. (defun org-auto-repeat-maybe (done-word)
  9810. "Check if the current headline contains a repeated deadline/schedule.
  9811. If yes, set TODO state back to what it was and change the base date
  9812. of repeating deadline/scheduled time stamps to new date.
  9813. This function is run automatically after each state change to a DONE state."
  9814. ;; last-state is dynamically scoped into this function
  9815. (let* ((repeat (org-get-repeat))
  9816. (aa (assoc last-state org-todo-kwd-alist))
  9817. (interpret (nth 1 aa))
  9818. (head (nth 2 aa))
  9819. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9820. (msg "Entry repeats: ")
  9821. (org-log-done nil)
  9822. (org-todo-log-states nil)
  9823. (nshiftmax 10) (nshift 0)
  9824. re type n what ts time)
  9825. (when repeat
  9826. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9827. (org-todo (if (eq interpret 'type) last-state head))
  9828. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9829. (org-time-stamp-format t t)))
  9830. (when org-log-repeat
  9831. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9832. (memq 'org-add-log-note post-command-hook))
  9833. ;; OK, we are already setup for some record
  9834. (if (eq org-log-repeat 'note)
  9835. ;; make sure we take a note, not only a time stamp
  9836. (setq org-log-note-how 'note))
  9837. ;; Set up for taking a record
  9838. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9839. last-state
  9840. 'findpos org-log-repeat)))
  9841. (org-back-to-heading t)
  9842. (org-add-planning-info nil nil 'closed)
  9843. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9844. org-deadline-time-regexp "\\)\\|\\("
  9845. org-ts-regexp "\\)"))
  9846. (while (re-search-forward
  9847. re (save-excursion (outline-next-heading) (point)) t)
  9848. (setq type (if (match-end 1) org-scheduled-string
  9849. (if (match-end 3) org-deadline-string "Plain:"))
  9850. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9851. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9852. (setq n (string-to-number (match-string 2 ts))
  9853. what (match-string 3 ts))
  9854. (if (equal what "w") (setq n (* n 7) what "d"))
  9855. ;; Preparation, see if we need to modify the start date for the change
  9856. (when (match-end 1)
  9857. (setq time (save-match-data (org-time-string-to-time ts)))
  9858. (cond
  9859. ((equal (match-string 1 ts) ".")
  9860. ;; Shift starting date to today
  9861. (org-timestamp-change
  9862. (- (time-to-days (current-time)) (time-to-days time))
  9863. 'day))
  9864. ((equal (match-string 1 ts) "+")
  9865. (while (or (= nshift 0)
  9866. (<= (time-to-days time) (time-to-days (current-time))))
  9867. (when (= (incf nshift) nshiftmax)
  9868. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9869. (error "Abort")))
  9870. (org-timestamp-change n (cdr (assoc what whata)))
  9871. (org-at-timestamp-p t)
  9872. (setq ts (match-string 1))
  9873. (setq time (save-match-data (org-time-string-to-time ts))))
  9874. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9875. ;; rematch, so that we have everything in place for the real shift
  9876. (org-at-timestamp-p t)
  9877. (setq ts (match-string 1))
  9878. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9879. (org-timestamp-change n (cdr (assoc what whata)))
  9880. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9881. (setq org-log-post-message msg)
  9882. (message "%s" msg))))
  9883. (defun org-show-todo-tree (arg)
  9884. "Make a compact tree which shows all headlines marked with TODO.
  9885. The tree will show the lines where the regexp matches, and all higher
  9886. headlines above the match.
  9887. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9888. With a numeric prefix N, construct a sparse tree for the Nth element
  9889. of `org-todo-keywords-1'."
  9890. (interactive "P")
  9891. (let ((case-fold-search nil)
  9892. (kwd-re
  9893. (cond ((null arg) org-not-done-regexp)
  9894. ((equal arg '(4))
  9895. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9896. (mapcar 'list org-todo-keywords-1))))
  9897. (concat "\\("
  9898. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9899. "\\)\\>")))
  9900. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9901. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9902. org-todo-keywords-1)))
  9903. (t (error "Invalid prefix argument: %s" arg)))))
  9904. (message "%d TODO entries found"
  9905. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9906. (defun org-deadline (&optional remove time)
  9907. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9908. With argument REMOVE, remove any deadline from the item.
  9909. When TIME is set, it should be an internal time specification, and the
  9910. scheduling will use the corresponding date."
  9911. (interactive "P")
  9912. (let* ((old-date (org-entry-get nil "DEADLINE"))
  9913. (repeater (and old-date
  9914. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  9915. (match-string 1 old-date))))
  9916. (if remove
  9917. (progn
  9918. (when (and old-date org-log-redeadline)
  9919. (org-add-log-setup 'deldeadline nil old-date 'findpos
  9920. org-log-redeadline))
  9921. (org-remove-timestamp-with-keyword org-deadline-string)
  9922. (message "Item no longer has a deadline."))
  9923. (org-add-planning-info 'deadline time 'closed)
  9924. (when (and old-date org-log-redeadline
  9925. (not (equal old-date
  9926. (substring org-last-inserted-timestamp 1 -1))))
  9927. (org-add-log-setup 'redeadline nil old-date 'findpos
  9928. org-log-redeadline))
  9929. (when repeater
  9930. (save-excursion
  9931. (org-back-to-heading t)
  9932. (when (re-search-forward (concat org-deadline-string " "
  9933. org-last-inserted-timestamp)
  9934. (save-excursion
  9935. (outline-next-heading) (point)) t)
  9936. (goto-char (1- (match-end 0)))
  9937. (insert " " repeater)
  9938. (setq org-last-inserted-timestamp
  9939. (concat (substring org-last-inserted-timestamp 0 -1)
  9940. " " repeater
  9941. (substring org-last-inserted-timestamp -1))))))
  9942. (message "Deadline on %s" org-last-inserted-timestamp))))
  9943. (defun org-schedule (&optional remove time)
  9944. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9945. With argument REMOVE, remove any scheduling date from the item.
  9946. When TIME is set, it should be an internal time specification, and the
  9947. scheduling will use the corresponding date."
  9948. (interactive "P")
  9949. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  9950. (repeater (and old-date
  9951. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  9952. (match-string 1 old-date))))
  9953. (if remove
  9954. (progn
  9955. (when (and old-date org-log-reschedule)
  9956. (org-add-log-setup 'delschedule nil old-date 'findpos
  9957. org-log-reschedule))
  9958. (org-remove-timestamp-with-keyword org-scheduled-string)
  9959. (message "Item is no longer scheduled."))
  9960. (org-add-planning-info 'scheduled time 'closed)
  9961. (when (and old-date org-log-reschedule
  9962. (not (equal old-date
  9963. (substring org-last-inserted-timestamp 1 -1))))
  9964. (org-add-log-setup 'reschedule nil old-date 'findpos
  9965. org-log-reschedule))
  9966. (when repeater
  9967. (save-excursion
  9968. (org-back-to-heading t)
  9969. (when (re-search-forward (concat org-scheduled-string " "
  9970. org-last-inserted-timestamp)
  9971. (save-excursion
  9972. (outline-next-heading) (point)) t)
  9973. (goto-char (1- (match-end 0)))
  9974. (insert " " repeater)
  9975. (setq org-last-inserted-timestamp
  9976. (concat (substring org-last-inserted-timestamp 0 -1)
  9977. " " repeater
  9978. (substring org-last-inserted-timestamp -1))))))
  9979. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9980. (defun org-get-scheduled-time (pom &optional inherit)
  9981. "Get the scheduled time as a time tuple, of a format suitable
  9982. for calling org-schedule with, or if there is no scheduling,
  9983. returns nil."
  9984. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9985. (when time
  9986. (apply 'encode-time (org-parse-time-string time)))))
  9987. (defun org-get-deadline-time (pom &optional inherit)
  9988. "Get the deadine as a time tuple, of a format suitable for
  9989. calling org-deadline with, or if there is no scheduling, returns
  9990. nil."
  9991. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9992. (when time
  9993. (apply 'encode-time (org-parse-time-string time)))))
  9994. (defun org-remove-timestamp-with-keyword (keyword)
  9995. "Remove all time stamps with KEYWORD in the current entry."
  9996. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9997. beg)
  9998. (save-excursion
  9999. (org-back-to-heading t)
  10000. (setq beg (point))
  10001. (outline-next-heading)
  10002. (while (re-search-backward re beg t)
  10003. (replace-match "")
  10004. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10005. (equal (char-before) ?\ ))
  10006. (backward-delete-char 1)
  10007. (if (string-match "^[ \t]*$" (buffer-substring
  10008. (point-at-bol) (point-at-eol)))
  10009. (delete-region (point-at-bol)
  10010. (min (point-max) (1+ (point-at-eol))))))))))
  10011. (defun org-add-planning-info (what &optional time &rest remove)
  10012. "Insert new timestamp with keyword in the line directly after the headline.
  10013. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10014. If non is given, the user is prompted for a date.
  10015. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10016. be removed."
  10017. (interactive)
  10018. (let (org-time-was-given org-end-time-was-given ts
  10019. end default-time default-input)
  10020. (catch 'exit
  10021. (when (and (not time) (memq what '(scheduled deadline)))
  10022. ;; Try to get a default date/time from existing timestamp
  10023. (save-excursion
  10024. (org-back-to-heading t)
  10025. (setq end (save-excursion (outline-next-heading) (point)))
  10026. (when (re-search-forward (if (eq what 'scheduled)
  10027. org-scheduled-time-regexp
  10028. org-deadline-time-regexp)
  10029. end t)
  10030. (setq ts (match-string 1)
  10031. default-time
  10032. (apply 'encode-time (org-parse-time-string ts))
  10033. default-input (and ts (org-get-compact-tod ts))))))
  10034. (when what
  10035. ;; If necessary, get the time from the user
  10036. (setq time (or time (org-read-date nil 'to-time nil nil
  10037. default-time default-input))))
  10038. (when (and org-insert-labeled-timestamps-at-point
  10039. (member what '(scheduled deadline)))
  10040. (insert
  10041. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10042. (org-insert-time-stamp time org-time-was-given
  10043. nil nil nil (list org-end-time-was-given))
  10044. (setq what nil))
  10045. (save-excursion
  10046. (save-restriction
  10047. (let (col list elt ts buffer-invisibility-spec)
  10048. (org-back-to-heading t)
  10049. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10050. (goto-char (match-end 1))
  10051. (setq col (current-column))
  10052. (goto-char (match-end 0))
  10053. (if (eobp) (insert "\n") (forward-char 1))
  10054. (when (and (not what)
  10055. (not (looking-at
  10056. (concat "[ \t]*"
  10057. org-keyword-time-not-clock-regexp))))
  10058. ;; Nothing to add, nothing to remove...... :-)
  10059. (throw 'exit nil))
  10060. (if (and (not (looking-at outline-regexp))
  10061. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10062. "[^\r\n]*"))
  10063. (not (equal (match-string 1) org-clock-string)))
  10064. (narrow-to-region (match-beginning 0) (match-end 0))
  10065. (insert-before-markers "\n")
  10066. (backward-char 1)
  10067. (narrow-to-region (point) (point))
  10068. (and org-adapt-indentation (org-indent-to-column col)))
  10069. ;; Check if we have to remove something.
  10070. (setq list (cons what remove))
  10071. (while list
  10072. (setq elt (pop list))
  10073. (goto-char (point-min))
  10074. (when (or (and (eq elt 'scheduled)
  10075. (re-search-forward org-scheduled-time-regexp nil t))
  10076. (and (eq elt 'deadline)
  10077. (re-search-forward org-deadline-time-regexp nil t))
  10078. (and (eq elt 'closed)
  10079. (re-search-forward org-closed-time-regexp nil t)))
  10080. (replace-match "")
  10081. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10082. (skip-chars-backward " ")
  10083. (if (looking-at " +") (replace-match ""))))
  10084. (goto-char (point-max))
  10085. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10086. (when what
  10087. (insert
  10088. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10089. (cond ((eq what 'scheduled) org-scheduled-string)
  10090. ((eq what 'deadline) org-deadline-string)
  10091. ((eq what 'closed) org-closed-string))
  10092. " ")
  10093. (setq ts (org-insert-time-stamp
  10094. time
  10095. (or org-time-was-given
  10096. (and (eq what 'closed) org-log-done-with-time))
  10097. (eq what 'closed)
  10098. nil nil (list org-end-time-was-given)))
  10099. (end-of-line 1))
  10100. (goto-char (point-min))
  10101. (widen)
  10102. (if (and (looking-at "[ \t]+\n")
  10103. (equal (char-before) ?\n))
  10104. (delete-region (1- (point)) (point-at-eol)))
  10105. ts))))))
  10106. (defvar org-log-note-marker (make-marker))
  10107. (defvar org-log-note-purpose nil)
  10108. (defvar org-log-note-state nil)
  10109. (defvar org-log-note-previous-state nil)
  10110. (defvar org-log-note-how nil)
  10111. (defvar org-log-note-extra nil)
  10112. (defvar org-log-note-window-configuration nil)
  10113. (defvar org-log-note-return-to (make-marker))
  10114. (defvar org-log-post-message nil
  10115. "Message to be displayed after a log note has been stored.
  10116. The auto-repeater uses this.")
  10117. (defun org-add-note ()
  10118. "Add a note to the current entry.
  10119. This is done in the same way as adding a state change note."
  10120. (interactive)
  10121. (org-add-log-setup 'note nil nil 'findpos nil))
  10122. (defvar org-property-end-re)
  10123. (defun org-add-log-setup (&optional purpose state prev-state
  10124. findpos how &optional extra)
  10125. "Set up the post command hook to take a note.
  10126. If this is about to TODO state change, the new state is expected in STATE.
  10127. When FINDPOS is non-nil, find the correct position for the note in
  10128. the current entry. If not, assume that it can be inserted at point.
  10129. HOW is an indicator what kind of note should be created.
  10130. EXTRA is additional text that will be inserted into the notes buffer."
  10131. (let* ((org-log-into-drawer (org-log-into-drawer))
  10132. (drawer (cond ((stringp org-log-into-drawer)
  10133. org-log-into-drawer)
  10134. (org-log-into-drawer "LOGBOOK")
  10135. (t nil))))
  10136. (save-restriction
  10137. (save-excursion
  10138. (when findpos
  10139. (org-back-to-heading t)
  10140. (narrow-to-region (point) (save-excursion
  10141. (outline-next-heading) (point)))
  10142. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10143. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10144. "[^\r\n]*\\)?"))
  10145. (goto-char (match-end 0))
  10146. (cond
  10147. (drawer
  10148. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10149. nil t)
  10150. (progn
  10151. (goto-char (match-end 0))
  10152. (or org-log-states-order-reversed
  10153. (and (re-search-forward org-property-end-re nil t)
  10154. (goto-char (1- (match-beginning 0))))))
  10155. (insert "\n:" drawer ":\n:END:")
  10156. (beginning-of-line 0)
  10157. (org-indent-line-function)
  10158. (beginning-of-line 2)
  10159. (org-indent-line-function)
  10160. (end-of-line 0)))
  10161. ((and org-log-state-notes-insert-after-drawers
  10162. (save-excursion
  10163. (forward-line) (looking-at org-drawer-regexp)))
  10164. (forward-line)
  10165. (while (looking-at org-drawer-regexp)
  10166. (goto-char (match-end 0))
  10167. (re-search-forward org-property-end-re (point-max) t)
  10168. (forward-line))
  10169. (forward-line -1)))
  10170. (unless org-log-states-order-reversed
  10171. (and (= (char-after) ?\n) (forward-char 1))
  10172. (org-skip-over-state-notes)
  10173. (skip-chars-backward " \t\n\r")))
  10174. (move-marker org-log-note-marker (point))
  10175. (setq org-log-note-purpose purpose
  10176. org-log-note-state state
  10177. org-log-note-previous-state prev-state
  10178. org-log-note-how how
  10179. org-log-note-extra extra)
  10180. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10181. (defun org-skip-over-state-notes ()
  10182. "Skip past the list of State notes in an entry."
  10183. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10184. (while (looking-at "[ \t]*- State")
  10185. (condition-case nil
  10186. (org-next-item)
  10187. (error (org-end-of-item)))))
  10188. (defun org-add-log-note (&optional purpose)
  10189. "Pop up a window for taking a note, and add this note later at point."
  10190. (remove-hook 'post-command-hook 'org-add-log-note)
  10191. (setq org-log-note-window-configuration (current-window-configuration))
  10192. (delete-other-windows)
  10193. (move-marker org-log-note-return-to (point))
  10194. (switch-to-buffer (marker-buffer org-log-note-marker))
  10195. (goto-char org-log-note-marker)
  10196. (org-switch-to-buffer-other-window "*Org Note*")
  10197. (erase-buffer)
  10198. (if (memq org-log-note-how '(time state))
  10199. (let (current-prefix-arg) (org-store-log-note))
  10200. (let ((org-inhibit-startup t)) (org-mode))
  10201. (insert (format "# Insert note for %s.
  10202. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10203. (cond
  10204. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10205. ((eq org-log-note-purpose 'done) "closed todo item")
  10206. ((eq org-log-note-purpose 'state)
  10207. (format "state change from \"%s\" to \"%s\""
  10208. (or org-log-note-previous-state "")
  10209. (or org-log-note-state "")))
  10210. ((eq org-log-note-purpose 'reschedule)
  10211. "rescheduling")
  10212. ((eq org-log-note-purpose 'delschedule)
  10213. "no longer scheduled")
  10214. ((eq org-log-note-purpose 'redeadline)
  10215. "changing deadline")
  10216. ((eq org-log-note-purpose 'deldeadline)
  10217. "removing deadline")
  10218. ((eq org-log-note-purpose 'refile)
  10219. "refiling")
  10220. ((eq org-log-note-purpose 'note)
  10221. "this entry")
  10222. (t (error "This should not happen")))))
  10223. (if org-log-note-extra (insert org-log-note-extra))
  10224. (org-set-local 'org-finish-function 'org-store-log-note)))
  10225. (defvar org-note-abort nil) ; dynamically scoped
  10226. (defun org-store-log-note ()
  10227. "Finish taking a log note, and insert it to where it belongs."
  10228. (let ((txt (buffer-string))
  10229. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10230. lines ind)
  10231. (kill-buffer (current-buffer))
  10232. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10233. (setq txt (replace-match "" t t txt)))
  10234. (if (string-match "\\s-+\\'" txt)
  10235. (setq txt (replace-match "" t t txt)))
  10236. (setq lines (org-split-string txt "\n"))
  10237. (when (and note (string-match "\\S-" note))
  10238. (setq note
  10239. (org-replace-escapes
  10240. note
  10241. (list (cons "%u" (user-login-name))
  10242. (cons "%U" user-full-name)
  10243. (cons "%t" (format-time-string
  10244. (org-time-stamp-format 'long 'inactive)
  10245. (current-time)))
  10246. (cons "%s" (if org-log-note-state
  10247. (concat "\"" org-log-note-state "\"")
  10248. ""))
  10249. (cons "%S" (if org-log-note-previous-state
  10250. (concat "\"" org-log-note-previous-state "\"")
  10251. "\"\"")))))
  10252. (if lines (setq note (concat note " \\\\")))
  10253. (push note lines))
  10254. (when (or current-prefix-arg org-note-abort)
  10255. (when org-log-into-drawer
  10256. (org-remove-empty-drawer-at
  10257. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10258. org-log-note-marker))
  10259. (setq lines nil))
  10260. (when lines
  10261. (with-current-buffer (marker-buffer org-log-note-marker)
  10262. (save-excursion
  10263. (goto-char org-log-note-marker)
  10264. (move-marker org-log-note-marker nil)
  10265. (end-of-line 1)
  10266. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10267. (insert "- " (pop lines))
  10268. (org-indent-line-function)
  10269. (beginning-of-line 1)
  10270. (looking-at "[ \t]*")
  10271. (setq ind (concat (match-string 0) " "))
  10272. (end-of-line 1)
  10273. (while lines (insert "\n" ind (pop lines)))
  10274. (message "Note stored")
  10275. (org-back-to-heading t)
  10276. (org-cycle-hide-drawers 'children)))))
  10277. (set-window-configuration org-log-note-window-configuration)
  10278. (with-current-buffer (marker-buffer org-log-note-return-to)
  10279. (goto-char org-log-note-return-to))
  10280. (move-marker org-log-note-return-to nil)
  10281. (and org-log-post-message (message "%s" org-log-post-message)))
  10282. (defun org-remove-empty-drawer-at (drawer pos)
  10283. "Remove an empty drawer DRAWER at position POS.
  10284. POS may also be a marker."
  10285. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10286. (save-excursion
  10287. (save-restriction
  10288. (widen)
  10289. (goto-char pos)
  10290. (if (org-in-regexp
  10291. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10292. (replace-match ""))))))
  10293. (defun org-sparse-tree (&optional arg)
  10294. "Create a sparse tree, prompt for the details.
  10295. This command can create sparse trees. You first need to select the type
  10296. of match used to create the tree:
  10297. t Show entries with a specific TODO keyword.
  10298. m Show entries selected by a tags/property match.
  10299. p Enter a property name and its value (both with completion on existing
  10300. names/values) and show entries with that property.
  10301. / Show entries matching a regular expression (`r' can be used as well)
  10302. d Show deadlines due within `org-deadline-warning-days'.
  10303. b Show deadlines and scheduled items before a date.
  10304. a Show deadlines and scheduled items after a date."
  10305. (interactive "P")
  10306. (let (ans kwd value)
  10307. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  10308. (setq ans (read-char-exclusive))
  10309. (cond
  10310. ((equal ans ?d)
  10311. (call-interactively 'org-check-deadlines))
  10312. ((equal ans ?b)
  10313. (call-interactively 'org-check-before-date))
  10314. ((equal ans ?a)
  10315. (call-interactively 'org-check-after-date))
  10316. ((equal ans ?t)
  10317. (org-show-todo-tree '(4)))
  10318. ((member ans '(?T ?m))
  10319. (call-interactively 'org-match-sparse-tree))
  10320. ((member ans '(?p ?P))
  10321. (setq kwd (org-icompleting-read "Property: "
  10322. (mapcar 'list (org-buffer-property-keys))))
  10323. (setq value (org-icompleting-read "Value: "
  10324. (mapcar 'list (org-property-values kwd))))
  10325. (unless (string-match "\\`{.*}\\'" value)
  10326. (setq value (concat "\"" value "\"")))
  10327. (org-match-sparse-tree arg (concat kwd "=" value)))
  10328. ((member ans '(?r ?R ?/))
  10329. (call-interactively 'org-occur))
  10330. (t (error "No such sparse tree command \"%c\"" ans)))))
  10331. (defvar org-occur-highlights nil
  10332. "List of overlays used for occur matches.")
  10333. (make-variable-buffer-local 'org-occur-highlights)
  10334. (defvar org-occur-parameters nil
  10335. "Parameters of the active org-occur calls.
  10336. This is a list, each call to org-occur pushes as cons cell,
  10337. containing the regular expression and the callback, onto the list.
  10338. The list can contain several entries if `org-occur' has been called
  10339. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10340. will only contain one set of parameters. When the highlights are
  10341. removed (for example with `C-c C-c', or with the next edit (depending
  10342. on `org-remove-highlights-with-change'), this variable is emptied
  10343. as well.")
  10344. (make-variable-buffer-local 'org-occur-parameters)
  10345. (defun org-occur (regexp &optional keep-previous callback)
  10346. "Make a compact tree which shows all matches of REGEXP.
  10347. The tree will show the lines where the regexp matches, and all higher
  10348. headlines above the match. It will also show the heading after the match,
  10349. to make sure editing the matching entry is easy.
  10350. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10351. call to `org-occur' will be kept, to allow stacking of calls to this
  10352. command.
  10353. If CALLBACK is non-nil, it is a function which is called to confirm
  10354. that the match should indeed be shown."
  10355. (interactive "sRegexp: \nP")
  10356. (when (equal regexp "")
  10357. (error "Regexp cannot be empty"))
  10358. (unless keep-previous
  10359. (org-remove-occur-highlights nil nil t))
  10360. (push (cons regexp callback) org-occur-parameters)
  10361. (let ((cnt 0))
  10362. (save-excursion
  10363. (goto-char (point-min))
  10364. (if (or (not keep-previous) ; do not want to keep
  10365. (not org-occur-highlights)) ; no previous matches
  10366. ;; hide everything
  10367. (org-overview))
  10368. (while (re-search-forward regexp nil t)
  10369. (when (or (not callback)
  10370. (save-match-data (funcall callback)))
  10371. (setq cnt (1+ cnt))
  10372. (when org-highlight-sparse-tree-matches
  10373. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10374. (org-show-context 'occur-tree))))
  10375. (when org-remove-highlights-with-change
  10376. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10377. nil 'local))
  10378. (unless org-sparse-tree-open-archived-trees
  10379. (org-hide-archived-subtrees (point-min) (point-max)))
  10380. (run-hooks 'org-occur-hook)
  10381. (if (interactive-p)
  10382. (message "%d match(es) for regexp %s" cnt regexp))
  10383. cnt))
  10384. (defun org-show-context (&optional key)
  10385. "Make sure point and context and visible.
  10386. How much context is shown depends upon the variables
  10387. `org-show-hierarchy-above', `org-show-following-heading'. and
  10388. `org-show-siblings'."
  10389. (let ((heading-p (org-on-heading-p t))
  10390. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10391. (following-p (org-get-alist-option org-show-following-heading key))
  10392. (entry-p (org-get-alist-option org-show-entry-below key))
  10393. (siblings-p (org-get-alist-option org-show-siblings key)))
  10394. (catch 'exit
  10395. ;; Show heading or entry text
  10396. (if (and heading-p (not entry-p))
  10397. (org-flag-heading nil) ; only show the heading
  10398. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10399. (org-show-hidden-entry))) ; show entire entry
  10400. (when following-p
  10401. ;; Show next sibling, or heading below text
  10402. (save-excursion
  10403. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10404. (org-flag-heading nil))))
  10405. (when siblings-p (org-show-siblings))
  10406. (when hierarchy-p
  10407. ;; show all higher headings, possibly with siblings
  10408. (save-excursion
  10409. (while (and (condition-case nil
  10410. (progn (org-up-heading-all 1) t)
  10411. (error nil))
  10412. (not (bobp)))
  10413. (org-flag-heading nil)
  10414. (when siblings-p (org-show-siblings))))))))
  10415. (defvar org-reveal-start-hook nil
  10416. "Hook run before revealing a location.")
  10417. (defun org-reveal (&optional siblings)
  10418. "Show current entry, hierarchy above it, and the following headline.
  10419. This can be used to show a consistent set of context around locations
  10420. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10421. not t for the search context.
  10422. With optional argument SIBLINGS, on each level of the hierarchy all
  10423. siblings are shown. This repairs the tree structure to what it would
  10424. look like when opened with hierarchical calls to `org-cycle'.
  10425. With double optional argument `C-u C-u', go to the parent and show the
  10426. entire tree."
  10427. (interactive "P")
  10428. (run-hooks 'org-reveal-start-hook)
  10429. (let ((org-show-hierarchy-above t)
  10430. (org-show-following-heading t)
  10431. (org-show-siblings (if siblings t org-show-siblings)))
  10432. (org-show-context nil))
  10433. (when (equal siblings '(16))
  10434. (save-excursion
  10435. (when (org-up-heading-safe)
  10436. (org-show-subtree)
  10437. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10438. (defun org-highlight-new-match (beg end)
  10439. "Highlight from BEG to END and mark the highlight is an occur headline."
  10440. (let ((ov (org-make-overlay beg end)))
  10441. (org-overlay-put ov 'face 'secondary-selection)
  10442. (push ov org-occur-highlights)))
  10443. (defun org-remove-occur-highlights (&optional beg end noremove)
  10444. "Remove the occur highlights from the buffer.
  10445. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10446. from the `before-change-functions' in the current buffer."
  10447. (interactive)
  10448. (unless org-inhibit-highlight-removal
  10449. (mapc 'org-delete-overlay org-occur-highlights)
  10450. (setq org-occur-highlights nil)
  10451. (setq org-occur-parameters nil)
  10452. (unless noremove
  10453. (remove-hook 'before-change-functions
  10454. 'org-remove-occur-highlights 'local))))
  10455. ;;;; Priorities
  10456. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10457. "Regular expression matching the priority indicator.")
  10458. (defvar org-remove-priority-next-time nil)
  10459. (defun org-priority-up ()
  10460. "Increase the priority of the current item."
  10461. (interactive)
  10462. (org-priority 'up))
  10463. (defun org-priority-down ()
  10464. "Decrease the priority of the current item."
  10465. (interactive)
  10466. (org-priority 'down))
  10467. (defun org-priority (&optional action)
  10468. "Change the priority of an item by ARG.
  10469. ACTION can be `set', `up', `down', or a character."
  10470. (interactive)
  10471. (unless org-enable-priority-commands
  10472. (error "Priority commands are disabled"))
  10473. (setq action (or action 'set))
  10474. (let (current new news have remove)
  10475. (save-excursion
  10476. (org-back-to-heading t)
  10477. (if (looking-at org-priority-regexp)
  10478. (setq current (string-to-char (match-string 2))
  10479. have t)
  10480. (setq current org-default-priority))
  10481. (cond
  10482. ((eq action 'remove)
  10483. (setq remove t new ?\ ))
  10484. ((or (eq action 'set)
  10485. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10486. (if (not (eq action 'set))
  10487. (setq new action)
  10488. (message "Priority %c-%c, SPC to remove: "
  10489. org-highest-priority org-lowest-priority)
  10490. (setq new (read-char-exclusive)))
  10491. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10492. (= (upcase org-lowest-priority) org-lowest-priority))
  10493. (setq new (upcase new)))
  10494. (cond ((equal new ?\ ) (setq remove t))
  10495. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10496. (error "Priority must be between `%c' and `%c'"
  10497. org-highest-priority org-lowest-priority))))
  10498. ((eq action 'up)
  10499. (if (and (not have) (eq last-command this-command))
  10500. (setq new org-lowest-priority)
  10501. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10502. org-default-priority (1- current)))))
  10503. ((eq action 'down)
  10504. (if (and (not have) (eq last-command this-command))
  10505. (setq new org-highest-priority)
  10506. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10507. org-default-priority (1+ current)))))
  10508. (t (error "Invalid action")))
  10509. (if (or (< (upcase new) org-highest-priority)
  10510. (> (upcase new) org-lowest-priority))
  10511. (setq remove t))
  10512. (setq news (format "%c" new))
  10513. (if have
  10514. (if remove
  10515. (replace-match "" t t nil 1)
  10516. (replace-match news t t nil 2))
  10517. (if remove
  10518. (error "No priority cookie found in line")
  10519. (let ((case-fold-search nil))
  10520. (looking-at org-todo-line-regexp))
  10521. (if (match-end 2)
  10522. (progn
  10523. (goto-char (match-end 2))
  10524. (insert " [#" news "]"))
  10525. (goto-char (match-beginning 3))
  10526. (insert "[#" news "] "))))
  10527. (org-preserve-lc (org-set-tags nil 'align)))
  10528. (if remove
  10529. (message "Priority removed")
  10530. (message "Priority of current item set to %s" news))))
  10531. (defun org-get-priority (s)
  10532. "Find priority cookie and return priority."
  10533. (save-match-data
  10534. (if (not (string-match org-priority-regexp s))
  10535. (* 1000 (- org-lowest-priority org-default-priority))
  10536. (* 1000 (- org-lowest-priority
  10537. (string-to-char (match-string 2 s)))))))
  10538. ;;;; Tags
  10539. (defvar org-agenda-archives-mode)
  10540. (defvar org-map-continue-from nil
  10541. "Position from where mapping should continue.
  10542. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10543. (defvar org-scanner-tags nil
  10544. "The current tag list while the tags scanner is running.")
  10545. (defvar org-trust-scanner-tags nil
  10546. "Should `org-get-tags-at' use the tags fro the scanner.
  10547. This is for internal dynamical scoping only.
  10548. When this is non-nil, the function `org-get-tags-at' will return the value
  10549. of `org-scanner-tags' instead of building the list by itself. This
  10550. can lead to large speed-ups when the tags scanner is used in a file with
  10551. many entries, and when the list of tags is retrieved, for example to
  10552. obtain a list of properties. Building the tags list for each entry in such
  10553. a file becomes an N^2 operation - but with this variable set, it scales
  10554. as N.")
  10555. (defun org-scan-tags (action matcher &optional todo-only)
  10556. "Scan headline tags with inheritance and produce output ACTION.
  10557. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10558. or `agenda' to produce an entry list for an agenda view. It can also be
  10559. a Lisp form or a function that should be called at each matched headline, in
  10560. this case the return value is a list of all return values from these calls.
  10561. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10562. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10563. only lines with a TODO keyword are included in the output."
  10564. (require 'org-agenda)
  10565. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10566. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10567. (org-re
  10568. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10569. (props (list 'face 'default
  10570. 'done-face 'org-agenda-done
  10571. 'undone-face 'default
  10572. 'mouse-face 'highlight
  10573. 'org-not-done-regexp org-not-done-regexp
  10574. 'org-todo-regexp org-todo-regexp
  10575. 'help-echo
  10576. (format "mouse-2 or RET jump to org file %s"
  10577. (abbreviate-file-name
  10578. (or (buffer-file-name (buffer-base-buffer))
  10579. (buffer-name (buffer-base-buffer)))))))
  10580. (case-fold-search nil)
  10581. (org-map-continue-from nil)
  10582. lspos tags tags-list
  10583. (tags-alist (list (cons 0 org-file-tags)))
  10584. (llast 0) rtn rtn1 level category i txt
  10585. todo marker entry priority)
  10586. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10587. (setq action (list 'lambda nil action)))
  10588. (save-excursion
  10589. (goto-char (point-min))
  10590. (when (eq action 'sparse-tree)
  10591. (org-overview)
  10592. (org-remove-occur-highlights))
  10593. (while (re-search-forward re nil t)
  10594. (catch :skip
  10595. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10596. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10597. (goto-char (setq lspos (match-beginning 0)))
  10598. (setq level (org-reduced-level (funcall outline-level))
  10599. category (org-get-category))
  10600. (setq i llast llast level)
  10601. ;; remove tag lists from same and sublevels
  10602. (while (>= i level)
  10603. (when (setq entry (assoc i tags-alist))
  10604. (setq tags-alist (delete entry tags-alist)))
  10605. (setq i (1- i)))
  10606. ;; add the next tags
  10607. (when tags
  10608. (setq tags (org-split-string tags ":")
  10609. tags-alist
  10610. (cons (cons level tags) tags-alist)))
  10611. ;; compile tags for current headline
  10612. (setq tags-list
  10613. (if org-use-tag-inheritance
  10614. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10615. tags)
  10616. org-scanner-tags tags-list)
  10617. (when org-use-tag-inheritance
  10618. (setcdr (car tags-alist)
  10619. (mapcar (lambda (x)
  10620. (setq x (copy-sequence x))
  10621. (org-add-prop-inherited x))
  10622. (cdar tags-alist))))
  10623. (when (and tags org-use-tag-inheritance
  10624. (or (not (eq t org-use-tag-inheritance))
  10625. org-tags-exclude-from-inheritance))
  10626. ;; selective inheritance, remove uninherited ones
  10627. (setcdr (car tags-alist)
  10628. (org-remove-uniherited-tags (cdar tags-alist))))
  10629. (when (and (or (not todo-only)
  10630. (and (member todo org-not-done-keywords)
  10631. (or (not org-agenda-tags-todo-honor-ignore-options)
  10632. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10633. (let ((case-fold-search t)) (eval matcher))
  10634. (or
  10635. (not (member org-archive-tag tags-list))
  10636. ;; we have an archive tag, should we use this anyway?
  10637. (or (not org-agenda-skip-archived-trees)
  10638. (and (eq action 'agenda) org-agenda-archives-mode))))
  10639. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10640. ;; select this headline
  10641. (cond
  10642. ((eq action 'sparse-tree)
  10643. (and org-highlight-sparse-tree-matches
  10644. (org-get-heading) (match-end 0)
  10645. (org-highlight-new-match
  10646. (match-beginning 0) (match-beginning 1)))
  10647. (org-show-context 'tags-tree))
  10648. ((eq action 'agenda)
  10649. (setq txt (org-format-agenda-item
  10650. ""
  10651. (concat
  10652. (if (eq org-tags-match-list-sublevels 'indented)
  10653. (make-string (1- level) ?.) "")
  10654. (org-get-heading))
  10655. category
  10656. tags-list
  10657. )
  10658. priority (org-get-priority txt))
  10659. (goto-char lspos)
  10660. (setq marker (org-agenda-new-marker))
  10661. (org-add-props txt props
  10662. 'org-marker marker 'org-hd-marker marker 'org-category category
  10663. 'todo-state todo
  10664. 'priority priority 'type "tagsmatch")
  10665. (push txt rtn))
  10666. ((functionp action)
  10667. (setq org-map-continue-from nil)
  10668. (save-excursion
  10669. (setq rtn1 (funcall action))
  10670. (push rtn1 rtn)))
  10671. (t (error "Invalid action")))
  10672. ;; if we are to skip sublevels, jump to end of subtree
  10673. (unless org-tags-match-list-sublevels
  10674. (org-end-of-subtree t)
  10675. (backward-char 1))))
  10676. ;; Get the correct position from where to continue
  10677. (if org-map-continue-from
  10678. (goto-char org-map-continue-from)
  10679. (and (= (point) lspos) (end-of-line 1)))))
  10680. (when (and (eq action 'sparse-tree)
  10681. (not org-sparse-tree-open-archived-trees))
  10682. (org-hide-archived-subtrees (point-min) (point-max)))
  10683. (nreverse rtn)))
  10684. (defun org-remove-uniherited-tags (tags)
  10685. "Remove all tags that are not inherited from the list TAGS."
  10686. (cond
  10687. ((eq org-use-tag-inheritance t)
  10688. (if org-tags-exclude-from-inheritance
  10689. (org-delete-all org-tags-exclude-from-inheritance tags)
  10690. tags))
  10691. ((not org-use-tag-inheritance) nil)
  10692. ((stringp org-use-tag-inheritance)
  10693. (delq nil (mapcar
  10694. (lambda (x)
  10695. (if (and (string-match org-use-tag-inheritance x)
  10696. (not (member x org-tags-exclude-from-inheritance)))
  10697. x nil))
  10698. tags)))
  10699. ((listp org-use-tag-inheritance)
  10700. (delq nil (mapcar
  10701. (lambda (x)
  10702. (if (member x org-use-tag-inheritance) x nil))
  10703. tags)))))
  10704. (defvar todo-only) ;; dynamically scoped
  10705. (defun org-match-sparse-tree (&optional todo-only match)
  10706. "Create a sparse tree according to tags string MATCH.
  10707. MATCH can contain positive and negative selection of tags, like
  10708. \"+WORK+URGENT-WITHBOSS\".
  10709. If optional argument TODO-ONLY is non-nil, only select lines that are
  10710. also TODO lines."
  10711. (interactive "P")
  10712. (org-prepare-agenda-buffers (list (current-buffer)))
  10713. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10714. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10715. (defvar org-cached-props nil)
  10716. (defun org-cached-entry-get (pom property)
  10717. (if (or (eq t org-use-property-inheritance)
  10718. (and (stringp org-use-property-inheritance)
  10719. (string-match org-use-property-inheritance property))
  10720. (and (listp org-use-property-inheritance)
  10721. (member property org-use-property-inheritance)))
  10722. ;; Caching is not possible, check it directly
  10723. (org-entry-get pom property 'inherit)
  10724. ;; Get all properties, so that we can do complicated checks easily
  10725. (cdr (assoc property (or org-cached-props
  10726. (setq org-cached-props
  10727. (org-entry-properties pom)))))))
  10728. (defun org-global-tags-completion-table (&optional files)
  10729. "Return the list of all tags in all agenda buffer/files."
  10730. (save-excursion
  10731. (org-uniquify
  10732. (delq nil
  10733. (apply 'append
  10734. (mapcar
  10735. (lambda (file)
  10736. (set-buffer (find-file-noselect file))
  10737. (append (org-get-buffer-tags)
  10738. (mapcar (lambda (x) (if (stringp (car-safe x))
  10739. (list (car-safe x)) nil))
  10740. org-tag-alist)))
  10741. (if (and files (car files))
  10742. files
  10743. (org-agenda-files))))))))
  10744. (defun org-make-tags-matcher (match)
  10745. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10746. ;; todo-only is scoped dynamically into this function, and the function
  10747. ;; may change it if the matcher asks for it.
  10748. (unless match
  10749. ;; Get a new match request, with completion
  10750. (let ((org-last-tags-completion-table
  10751. (org-global-tags-completion-table)))
  10752. (setq match (org-completing-read-no-i
  10753. "Match: " 'org-tags-completion-function nil nil nil
  10754. 'org-tags-history))))
  10755. ;; Parse the string and create a lisp form
  10756. (let ((match0 match)
  10757. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10758. minus tag mm
  10759. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10760. orterms term orlist re-p str-p level-p level-op time-p
  10761. prop-p pn pv po cat-p gv rest)
  10762. (if (string-match "/+" match)
  10763. ;; match contains also a todo-matching request
  10764. (progn
  10765. (setq tagsmatch (substring match 0 (match-beginning 0))
  10766. todomatch (substring match (match-end 0)))
  10767. (if (string-match "^!" todomatch)
  10768. (setq todo-only t todomatch (substring todomatch 1)))
  10769. (if (string-match "^\\s-*$" todomatch)
  10770. (setq todomatch nil)))
  10771. ;; only matching tags
  10772. (setq tagsmatch match todomatch nil))
  10773. ;; Make the tags matcher
  10774. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10775. (setq tagsmatcher t)
  10776. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10777. (while (setq term (pop orterms))
  10778. (while (and (equal (substring term -1) "\\") orterms)
  10779. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10780. (while (string-match re term)
  10781. (setq rest (substring term (match-end 0))
  10782. minus (and (match-end 1)
  10783. (equal (match-string 1 term) "-"))
  10784. tag (match-string 2 term)
  10785. re-p (equal (string-to-char tag) ?{)
  10786. level-p (match-end 4)
  10787. prop-p (match-end 5)
  10788. mm (cond
  10789. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10790. (level-p
  10791. (setq level-op (org-op-to-function (match-string 3 term)))
  10792. `(,level-op level ,(string-to-number
  10793. (match-string 4 term))))
  10794. (prop-p
  10795. (setq pn (match-string 5 term)
  10796. po (match-string 6 term)
  10797. pv (match-string 7 term)
  10798. cat-p (equal pn "CATEGORY")
  10799. re-p (equal (string-to-char pv) ?{)
  10800. str-p (equal (string-to-char pv) ?\")
  10801. time-p (save-match-data
  10802. (string-match "^\"[[<].*[]>]\"$" pv))
  10803. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10804. (if time-p (setq pv (org-matcher-time pv)))
  10805. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10806. (cond
  10807. ((equal pn "CATEGORY")
  10808. (setq gv '(get-text-property (point) 'org-category)))
  10809. ((equal pn "TODO")
  10810. (setq gv 'todo))
  10811. (t
  10812. (setq gv `(org-cached-entry-get nil ,pn))))
  10813. (if re-p
  10814. (if (eq po 'org<>)
  10815. `(not (string-match ,pv (or ,gv "")))
  10816. `(string-match ,pv (or ,gv "")))
  10817. (if str-p
  10818. `(,po (or ,gv "") ,pv)
  10819. `(,po (string-to-number (or ,gv ""))
  10820. ,(string-to-number pv) ))))
  10821. (t `(member ,tag tags-list)))
  10822. mm (if minus (list 'not mm) mm)
  10823. term rest)
  10824. (push mm tagsmatcher))
  10825. (push (if (> (length tagsmatcher) 1)
  10826. (cons 'and tagsmatcher)
  10827. (car tagsmatcher))
  10828. orlist)
  10829. (setq tagsmatcher nil))
  10830. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10831. (setq tagsmatcher
  10832. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10833. ;; Make the todo matcher
  10834. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10835. (setq todomatcher t)
  10836. (setq orterms (org-split-string todomatch "|") orlist nil)
  10837. (while (setq term (pop orterms))
  10838. (while (string-match re term)
  10839. (setq minus (and (match-end 1)
  10840. (equal (match-string 1 term) "-"))
  10841. kwd (match-string 2 term)
  10842. re-p (equal (string-to-char kwd) ?{)
  10843. term (substring term (match-end 0))
  10844. mm (if re-p
  10845. `(string-match ,(substring kwd 1 -1) todo)
  10846. (list 'equal 'todo kwd))
  10847. mm (if minus (list 'not mm) mm))
  10848. (push mm todomatcher))
  10849. (push (if (> (length todomatcher) 1)
  10850. (cons 'and todomatcher)
  10851. (car todomatcher))
  10852. orlist)
  10853. (setq todomatcher nil))
  10854. (setq todomatcher (if (> (length orlist) 1)
  10855. (cons 'or orlist) (car orlist))))
  10856. ;; Return the string and lisp forms of the matcher
  10857. (setq matcher (if todomatcher
  10858. (list 'and tagsmatcher todomatcher)
  10859. tagsmatcher))
  10860. (cons match0 matcher)))
  10861. (defun org-op-to-function (op &optional stringp)
  10862. "Turn an operator into the appropriate function."
  10863. (setq op
  10864. (cond
  10865. ((equal op "<" ) '(< string< org-time<))
  10866. ((equal op ">" ) '(> org-string> org-time>))
  10867. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10868. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10869. ((member op '("=" "==")) '(= string= org-time=))
  10870. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10871. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10872. (defun org<> (a b) (not (= a b)))
  10873. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10874. (defun org-string>= (a b) (not (string< a b)))
  10875. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10876. (defun org-string<> (a b) (not (string= a b)))
  10877. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10878. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10879. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10880. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10881. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10882. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10883. (defun org-2ft (s)
  10884. "Convert S to a floating point time.
  10885. If S is already a number, just return it. If it is a string, parse
  10886. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10887. (cond
  10888. ((numberp s) s)
  10889. ((stringp s)
  10890. (condition-case nil
  10891. (float-time (apply 'encode-time (org-parse-time-string s)))
  10892. (error 0.)))
  10893. (t 0.)))
  10894. (defun org-time-today ()
  10895. "Time in seconds today at 0:00.
  10896. Returns the float number of seconds since the beginning of the
  10897. epoch to the beginning of today (00:00)."
  10898. (float-time (apply 'encode-time
  10899. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10900. (defun org-matcher-time (s)
  10901. "Interpret a time comparison value."
  10902. (save-match-data
  10903. (cond
  10904. ((string= s "<now>") (float-time))
  10905. ((string= s "<today>") (org-time-today))
  10906. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10907. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10908. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10909. (+ (org-time-today)
  10910. (* (string-to-number (match-string 1 s))
  10911. (cdr (assoc (match-string 2 s)
  10912. '(("d" . 86400.0) ("w" . 604800.0)
  10913. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10914. (t (org-2ft s)))))
  10915. (defun org-match-any-p (re list)
  10916. "Does re match any element of list?"
  10917. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10918. (delq nil list))
  10919. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10920. (defvar org-tags-overlay (org-make-overlay 1 1))
  10921. (org-detach-overlay org-tags-overlay)
  10922. (defun org-get-local-tags-at (&optional pos)
  10923. "Get a list of tags defined in the current headline."
  10924. (org-get-tags-at pos 'local))
  10925. (defun org-get-local-tags ()
  10926. "Get a list of tags defined in the current headline."
  10927. (org-get-tags-at nil 'local))
  10928. (defun org-get-tags-at (&optional pos local)
  10929. "Get a list of all headline tags applicable at POS.
  10930. POS defaults to point. If tags are inherited, the list contains
  10931. the targets in the same sequence as the headlines appear, i.e.
  10932. the tags of the current headline come last.
  10933. When LOCAL is non-nil, only return tags from the current headline,
  10934. ignore inherited ones."
  10935. (interactive)
  10936. (if (and org-trust-scanner-tags
  10937. (or (not pos) (equal pos (point)))
  10938. (not local))
  10939. org-scanner-tags
  10940. (let (tags ltags lastpos parent)
  10941. (save-excursion
  10942. (save-restriction
  10943. (widen)
  10944. (goto-char (or pos (point)))
  10945. (save-match-data
  10946. (catch 'done
  10947. (condition-case nil
  10948. (progn
  10949. (org-back-to-heading t)
  10950. (while (not (equal lastpos (point)))
  10951. (setq lastpos (point))
  10952. (when (looking-at
  10953. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10954. (setq ltags (org-split-string
  10955. (org-match-string-no-properties 1) ":"))
  10956. (when parent
  10957. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10958. (setq tags (append
  10959. (if parent
  10960. (org-remove-uniherited-tags ltags)
  10961. ltags)
  10962. tags)))
  10963. (or org-use-tag-inheritance (throw 'done t))
  10964. (if local (throw 'done t))
  10965. (or (org-up-heading-safe) (error nil))
  10966. (setq parent t)))
  10967. (error nil)))))
  10968. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10969. (defun org-add-prop-inherited (s)
  10970. (add-text-properties 0 (length s) '(inherited t) s)
  10971. s)
  10972. (defun org-toggle-tag (tag &optional onoff)
  10973. "Toggle the tag TAG for the current line.
  10974. If ONOFF is `on' or `off', don't toggle but set to this state."
  10975. (let (res current)
  10976. (save-excursion
  10977. (org-back-to-heading t)
  10978. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10979. (point-at-eol) t)
  10980. (progn
  10981. (setq current (match-string 1))
  10982. (replace-match ""))
  10983. (setq current ""))
  10984. (setq current (nreverse (org-split-string current ":")))
  10985. (cond
  10986. ((eq onoff 'on)
  10987. (setq res t)
  10988. (or (member tag current) (push tag current)))
  10989. ((eq onoff 'off)
  10990. (or (not (member tag current)) (setq current (delete tag current))))
  10991. (t (if (member tag current)
  10992. (setq current (delete tag current))
  10993. (setq res t)
  10994. (push tag current))))
  10995. (end-of-line 1)
  10996. (if current
  10997. (progn
  10998. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10999. (org-set-tags nil t))
  11000. (delete-horizontal-space))
  11001. (run-hooks 'org-after-tags-change-hook))
  11002. res))
  11003. (defun org-align-tags-here (to-col)
  11004. ;; Assumes that this is a headline
  11005. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11006. (beginning-of-line 1)
  11007. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11008. (< pos (match-beginning 2)))
  11009. (progn
  11010. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11011. (goto-char (match-beginning 1))
  11012. (insert " ")
  11013. (delete-region (point) (1+ (match-beginning 2)))
  11014. (setq ncol (max (1+ (current-column))
  11015. (1+ col)
  11016. (if (> to-col 0)
  11017. to-col
  11018. (- (abs to-col) tags-l))))
  11019. (setq p (point))
  11020. (insert (make-string (- ncol (current-column)) ?\ ))
  11021. (setq ncol (current-column))
  11022. (when indent-tabs-mode (tabify p (point-at-eol)))
  11023. (org-move-to-column (min ncol col) t))
  11024. (goto-char pos))))
  11025. (defun org-set-tags-command (&optional arg just-align)
  11026. "Call the set-tags command for the current entry."
  11027. (interactive "P")
  11028. (if (org-on-heading-p)
  11029. (org-set-tags arg just-align)
  11030. (save-excursion
  11031. (org-back-to-heading t)
  11032. (org-set-tags arg just-align))))
  11033. (defun org-set-tags-to (data)
  11034. "Set the tags of the current entry to DATA, replacing the current tags.
  11035. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11036. If DATA is nil or the empty string, any tags will be removed."
  11037. (interactive "sTags: ")
  11038. (setq data
  11039. (cond
  11040. ((eq data nil) "")
  11041. ((equal data "") "")
  11042. ((stringp data)
  11043. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11044. ":"))
  11045. ((listp data)
  11046. (concat ":" (mapconcat 'identity data ":") ":"))
  11047. (t nil)))
  11048. (when data
  11049. (save-excursion
  11050. (org-back-to-heading t)
  11051. (when (looking-at org-complex-heading-regexp)
  11052. (if (match-end 5)
  11053. (progn
  11054. (goto-char (match-beginning 5))
  11055. (insert data)
  11056. (delete-region (point) (point-at-eol))
  11057. (org-set-tags nil 'align))
  11058. (goto-char (point-at-eol))
  11059. (insert " " data)
  11060. (org-set-tags nil 'align)))
  11061. (beginning-of-line 1)
  11062. (if (looking-at ".*?\\([ \t]+\\)$")
  11063. (delete-region (match-beginning 1) (match-end 1))))))
  11064. (defun org-set-tags (&optional arg just-align)
  11065. "Set the tags for the current headline.
  11066. With prefix ARG, realign all tags in headings in the current buffer."
  11067. (interactive "P")
  11068. (let* ((re (concat "^" outline-regexp))
  11069. (current (org-get-tags-string))
  11070. (col (current-column))
  11071. (org-setting-tags t)
  11072. table current-tags inherited-tags ; computed below when needed
  11073. tags p0 c0 c1 rpl)
  11074. (if arg
  11075. (save-excursion
  11076. (goto-char (point-min))
  11077. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11078. (while (re-search-forward re nil t)
  11079. (org-set-tags nil t)
  11080. (end-of-line 1)))
  11081. (message "All tags realigned to column %d" org-tags-column))
  11082. (if just-align
  11083. (setq tags current)
  11084. ;; Get a new set of tags from the user
  11085. (save-excursion
  11086. (setq table (append org-tag-persistent-alist
  11087. (or org-tag-alist (org-get-buffer-tags))
  11088. (and org-complete-tags-always-offer-all-agenda-tags
  11089. (org-global-tags-completion-table (org-agenda-files))))
  11090. org-last-tags-completion-table table
  11091. current-tags (org-split-string current ":")
  11092. inherited-tags (nreverse
  11093. (nthcdr (length current-tags)
  11094. (nreverse (org-get-tags-at))))
  11095. tags
  11096. (if (or (eq t org-use-fast-tag-selection)
  11097. (and org-use-fast-tag-selection
  11098. (delq nil (mapcar 'cdr table))))
  11099. (org-fast-tag-selection
  11100. current-tags inherited-tags table
  11101. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  11102. (let ((org-add-colon-after-tag-completion t))
  11103. (org-trim
  11104. (org-without-partial-completion
  11105. (org-icompleting-read "Tags: " 'org-tags-completion-function
  11106. nil nil current 'org-tags-history)))))))
  11107. (while (string-match "[-+&]+" tags)
  11108. ;; No boolean logic, just a list
  11109. (setq tags (replace-match ":" t t tags))))
  11110. (if org-tags-sort-function
  11111. (setq tags (mapconcat 'identity
  11112. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  11113. org-tags-sort-function) ":")))
  11114. (if (string-match "\\`[\t ]*\\'" tags)
  11115. (setq tags "")
  11116. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11117. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11118. ;; Insert new tags at the correct column
  11119. (beginning-of-line 1)
  11120. (cond
  11121. ((and (equal current "") (equal tags "")))
  11122. ((re-search-forward
  11123. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11124. (point-at-eol) t)
  11125. (if (equal tags "")
  11126. (setq rpl "")
  11127. (goto-char (match-beginning 0))
  11128. (setq c0 (current-column) p0 (if (equal (char-before) ?*)
  11129. (1+ (point)) (point))
  11130. c1 (max (1+ c0) (if (> org-tags-column 0)
  11131. org-tags-column
  11132. (- (- org-tags-column) (length tags))))
  11133. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11134. (replace-match rpl t t)
  11135. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11136. tags)
  11137. (t (error "Tags alignment failed")))
  11138. (org-move-to-column col)
  11139. (unless just-align
  11140. (run-hooks 'org-after-tags-change-hook)))))
  11141. (defun org-change-tag-in-region (beg end tag off)
  11142. "Add or remove TAG for each entry in the region.
  11143. This works in the agenda, and also in an org-mode buffer."
  11144. (interactive
  11145. (list (region-beginning) (region-end)
  11146. (let ((org-last-tags-completion-table
  11147. (if (org-mode-p)
  11148. (org-get-buffer-tags)
  11149. (org-global-tags-completion-table))))
  11150. (org-icompleting-read
  11151. "Tag: " 'org-tags-completion-function nil nil nil
  11152. 'org-tags-history))
  11153. (progn
  11154. (message "[s]et or [r]emove? ")
  11155. (equal (read-char-exclusive) ?r))))
  11156. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11157. (let ((agendap (equal major-mode 'org-agenda-mode))
  11158. l1 l2 m buf pos newhead (cnt 0))
  11159. (goto-char end)
  11160. (setq l2 (1- (org-current-line)))
  11161. (goto-char beg)
  11162. (setq l1 (org-current-line))
  11163. (loop for l from l1 to l2 do
  11164. (org-goto-line l)
  11165. (setq m (get-text-property (point) 'org-hd-marker))
  11166. (when (or (and (org-mode-p) (org-on-heading-p))
  11167. (and agendap m))
  11168. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11169. pos (if agendap m (point)))
  11170. (with-current-buffer buf
  11171. (save-excursion
  11172. (save-restriction
  11173. (goto-char pos)
  11174. (setq cnt (1+ cnt))
  11175. (org-toggle-tag tag (if off 'off 'on))
  11176. (setq newhead (org-get-heading)))))
  11177. (and agendap (org-agenda-change-all-lines newhead m))))
  11178. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11179. (defun org-tags-completion-function (string predicate &optional flag)
  11180. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11181. (confirm (lambda (x) (stringp (car x)))))
  11182. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11183. (setq s1 (match-string 1 string)
  11184. s2 (match-string 2 string))
  11185. (setq s1 "" s2 string))
  11186. (cond
  11187. ((eq flag nil)
  11188. ;; try completion
  11189. (setq rtn (try-completion s2 ctable confirm))
  11190. (if (stringp rtn)
  11191. (setq rtn
  11192. (concat s1 s2 (substring rtn (length s2))
  11193. (if (and org-add-colon-after-tag-completion
  11194. (assoc rtn ctable))
  11195. ":" ""))))
  11196. rtn)
  11197. ((eq flag t)
  11198. ;; all-completions
  11199. (all-completions s2 ctable confirm)
  11200. )
  11201. ((eq flag 'lambda)
  11202. ;; exact match?
  11203. (assoc s2 ctable)))
  11204. ))
  11205. (defun org-fast-tag-insert (kwd tags face &optional end)
  11206. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11207. (insert (format "%-12s" (concat kwd ":"))
  11208. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11209. (or end "")))
  11210. (defun org-fast-tag-show-exit (flag)
  11211. (save-excursion
  11212. (org-goto-line 3)
  11213. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11214. (replace-match ""))
  11215. (when flag
  11216. (end-of-line 1)
  11217. (org-move-to-column (- (window-width) 19) t)
  11218. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11219. (defun org-set-current-tags-overlay (current prefix)
  11220. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11221. (if (featurep 'xemacs)
  11222. (org-overlay-display org-tags-overlay (concat prefix s)
  11223. 'secondary-selection)
  11224. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11225. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11226. (defvar org-last-tag-selection-key nil)
  11227. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11228. "Fast tag selection with single keys.
  11229. CURRENT is the current list of tags in the headline, INHERITED is the
  11230. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11231. possibly with grouping information. TODO-TABLE is a similar table with
  11232. TODO keywords, should these have keys assigned to them.
  11233. If the keys are nil, a-z are automatically assigned.
  11234. Returns the new tags string, or nil to not change the current settings."
  11235. (let* ((fulltable (append table todo-table))
  11236. (maxlen (apply 'max (mapcar
  11237. (lambda (x)
  11238. (if (stringp (car x)) (string-width (car x)) 0))
  11239. fulltable)))
  11240. (buf (current-buffer))
  11241. (expert (eq org-fast-tag-selection-single-key 'expert))
  11242. (buffer-tags nil)
  11243. (fwidth (+ maxlen 3 1 3))
  11244. (ncol (/ (- (window-width) 4) fwidth))
  11245. (i-face 'org-done)
  11246. (c-face 'org-todo)
  11247. tg cnt e c char c1 c2 ntable tbl rtn
  11248. ov-start ov-end ov-prefix
  11249. (exit-after-next org-fast-tag-selection-single-key)
  11250. (done-keywords org-done-keywords)
  11251. groups ingroup)
  11252. (save-excursion
  11253. (beginning-of-line 1)
  11254. (if (looking-at
  11255. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11256. (setq ov-start (match-beginning 1)
  11257. ov-end (match-end 1)
  11258. ov-prefix "")
  11259. (setq ov-start (1- (point-at-eol))
  11260. ov-end (1+ ov-start))
  11261. (skip-chars-forward "^\n\r")
  11262. (setq ov-prefix
  11263. (concat
  11264. (buffer-substring (1- (point)) (point))
  11265. (if (> (current-column) org-tags-column)
  11266. " "
  11267. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11268. (org-move-overlay org-tags-overlay ov-start ov-end)
  11269. (save-window-excursion
  11270. (if expert
  11271. (set-buffer (get-buffer-create " *Org tags*"))
  11272. (delete-other-windows)
  11273. (split-window-vertically)
  11274. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11275. (erase-buffer)
  11276. (org-set-local 'org-done-keywords done-keywords)
  11277. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11278. (org-fast-tag-insert "Current" current c-face "\n\n")
  11279. (org-fast-tag-show-exit exit-after-next)
  11280. (org-set-current-tags-overlay current ov-prefix)
  11281. (setq tbl fulltable char ?a cnt 0)
  11282. (while (setq e (pop tbl))
  11283. (cond
  11284. ((equal (car e) :startgroup)
  11285. (push '() groups) (setq ingroup t)
  11286. (when (not (= cnt 0))
  11287. (setq cnt 0)
  11288. (insert "\n"))
  11289. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11290. ((equal (car e) :endgroup)
  11291. (setq ingroup nil cnt 0)
  11292. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11293. ((equal e '(:newline))
  11294. (when (not (= cnt 0))
  11295. (setq cnt 0)
  11296. (insert "\n")
  11297. (setq e (car tbl))
  11298. (while (equal (car tbl) '(:newline))
  11299. (insert "\n")
  11300. (setq tbl (cdr tbl)))))
  11301. (t
  11302. (setq tg (copy-sequence (car e)) c2 nil)
  11303. (if (cdr e)
  11304. (setq c (cdr e))
  11305. ;; automatically assign a character.
  11306. (setq c1 (string-to-char
  11307. (downcase (substring
  11308. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11309. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11310. (while (or (rassoc char ntable) (rassoc char table))
  11311. (setq char (1+ char)))
  11312. (setq c2 c1))
  11313. (setq c (or c2 char)))
  11314. (if ingroup (push tg (car groups)))
  11315. (setq tg (org-add-props tg nil 'face
  11316. (cond
  11317. ((not (assoc tg table))
  11318. (org-get-todo-face tg))
  11319. ((member tg current) c-face)
  11320. ((member tg inherited) i-face)
  11321. (t nil))))
  11322. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11323. (insert "[" c "] " tg (make-string
  11324. (- fwidth 4 (length tg)) ?\ ))
  11325. (push (cons tg c) ntable)
  11326. (when (= (setq cnt (1+ cnt)) ncol)
  11327. (insert "\n")
  11328. (if ingroup (insert " "))
  11329. (setq cnt 0)))))
  11330. (setq ntable (nreverse ntable))
  11331. (insert "\n")
  11332. (goto-char (point-min))
  11333. (if (not expert) (org-fit-window-to-buffer))
  11334. (setq rtn
  11335. (catch 'exit
  11336. (while t
  11337. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11338. (if (not groups) "no " "")
  11339. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11340. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11341. (setq org-last-tag-selection-key c)
  11342. (cond
  11343. ((= c ?\r) (throw 'exit t))
  11344. ((= c ?!)
  11345. (setq groups (not groups))
  11346. (goto-char (point-min))
  11347. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11348. ((= c ?\C-c)
  11349. (if (not expert)
  11350. (org-fast-tag-show-exit
  11351. (setq exit-after-next (not exit-after-next)))
  11352. (setq expert nil)
  11353. (delete-other-windows)
  11354. (split-window-vertically)
  11355. (org-switch-to-buffer-other-window " *Org tags*")
  11356. (org-fit-window-to-buffer)))
  11357. ((or (= c ?\C-g)
  11358. (and (= c ?q) (not (rassoc c ntable))))
  11359. (org-detach-overlay org-tags-overlay)
  11360. (setq quit-flag t))
  11361. ((= c ?\ )
  11362. (setq current nil)
  11363. (if exit-after-next (setq exit-after-next 'now)))
  11364. ((= c ?\t)
  11365. (condition-case nil
  11366. (setq tg (org-icompleting-read
  11367. "Tag: "
  11368. (or buffer-tags
  11369. (with-current-buffer buf
  11370. (org-get-buffer-tags)))))
  11371. (quit (setq tg "")))
  11372. (when (string-match "\\S-" tg)
  11373. (add-to-list 'buffer-tags (list tg))
  11374. (if (member tg current)
  11375. (setq current (delete tg current))
  11376. (push tg current)))
  11377. (if exit-after-next (setq exit-after-next 'now)))
  11378. ((setq e (rassoc c todo-table) tg (car e))
  11379. (with-current-buffer buf
  11380. (save-excursion (org-todo tg)))
  11381. (if exit-after-next (setq exit-after-next 'now)))
  11382. ((setq e (rassoc c ntable) tg (car e))
  11383. (if (member tg current)
  11384. (setq current (delete tg current))
  11385. (loop for g in groups do
  11386. (if (member tg g)
  11387. (mapc (lambda (x)
  11388. (setq current (delete x current)))
  11389. g)))
  11390. (push tg current))
  11391. (if exit-after-next (setq exit-after-next 'now))))
  11392. ;; Create a sorted list
  11393. (setq current
  11394. (sort current
  11395. (lambda (a b)
  11396. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11397. (if (eq exit-after-next 'now) (throw 'exit t))
  11398. (goto-char (point-min))
  11399. (beginning-of-line 2)
  11400. (delete-region (point) (point-at-eol))
  11401. (org-fast-tag-insert "Current" current c-face)
  11402. (org-set-current-tags-overlay current ov-prefix)
  11403. (while (re-search-forward
  11404. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11405. (setq tg (match-string 1))
  11406. (add-text-properties
  11407. (match-beginning 1) (match-end 1)
  11408. (list 'face
  11409. (cond
  11410. ((member tg current) c-face)
  11411. ((member tg inherited) i-face)
  11412. (t (get-text-property (match-beginning 1) 'face))))))
  11413. (goto-char (point-min)))))
  11414. (org-detach-overlay org-tags-overlay)
  11415. (if rtn
  11416. (mapconcat 'identity current ":")
  11417. nil))))
  11418. (defun org-get-tags-string ()
  11419. "Get the TAGS string in the current headline."
  11420. (unless (org-on-heading-p t)
  11421. (error "Not on a heading"))
  11422. (save-excursion
  11423. (beginning-of-line 1)
  11424. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11425. (org-match-string-no-properties 1)
  11426. "")))
  11427. (defun org-get-tags ()
  11428. "Get the list of tags specified in the current headline."
  11429. (org-split-string (org-get-tags-string) ":"))
  11430. (defun org-get-buffer-tags ()
  11431. "Get a table of all tags used in the buffer, for completion."
  11432. (let (tags)
  11433. (save-excursion
  11434. (goto-char (point-min))
  11435. (while (re-search-forward
  11436. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  11437. (when (equal (char-after (point-at-bol 0)) ?*)
  11438. (mapc (lambda (x) (add-to-list 'tags x))
  11439. (org-split-string (org-match-string-no-properties 1) ":")))))
  11440. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11441. (mapcar 'list tags)))
  11442. ;;;; The mapping API
  11443. ;;;###autoload
  11444. (defun org-map-entries (func &optional match scope &rest skip)
  11445. "Call FUNC at each headline selected by MATCH in SCOPE.
  11446. FUNC is a function or a lisp form. The function will be called without
  11447. arguments, with the cursor positioned at the beginning of the headline.
  11448. The return values of all calls to the function will be collected and
  11449. returned as a list.
  11450. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11451. does not need to preserve point. After evaluation, the cursor will be
  11452. moved to the end of the line (presumably of the headline of the
  11453. processed entry) and search continues from there. Under some
  11454. circumstances, this may not produce the wanted results. For example,
  11455. if you have removed (e.g. archived) the current (sub)tree it could
  11456. mean that the next entry will be skipped entirely. In such cases, you
  11457. can specify the position from where search should continue by making
  11458. FUNC set the variable `org-map-continue-from' to the desired buffer
  11459. position.
  11460. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11461. Only headlines that are matched by this query will be considered during
  11462. the iteration. When MATCH is nil or t, all headlines will be
  11463. visited by the iteration.
  11464. SCOPE determines the scope of this command. It can be any of:
  11465. nil The current buffer, respecting the restriction if any
  11466. tree The subtree started with the entry at point
  11467. file The current buffer, without restriction
  11468. file-with-archives
  11469. The current buffer, and any archives associated with it
  11470. agenda All agenda files
  11471. agenda-with-archives
  11472. All agenda files with any archive files associated with them
  11473. \(file1 file2 ...)
  11474. If this is a list, all files in the list will be scanned
  11475. The remaining args are treated as settings for the skipping facilities of
  11476. the scanner. The following items can be given here:
  11477. archive skip trees with the archive tag.
  11478. comment skip trees with the COMMENT keyword
  11479. function or Emacs Lisp form:
  11480. will be used as value for `org-agenda-skip-function', so whenever
  11481. the function returns t, FUNC will not be called for that
  11482. entry and search will continue from the point where the
  11483. function leaves it.
  11484. If your function needs to retrieve the tags including inherited tags
  11485. at the *current* entry, you can use the value of the variable
  11486. `org-scanner-tags' which will be much faster than getting the value
  11487. with `org-get-tags-at'. If your function gets properties with
  11488. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11489. to t around the call to `org-entry-properties' to get the same speedup.
  11490. Note that if your function moves around to retrieve tags and properties at
  11491. a *different* entry, you cannot use these techniques."
  11492. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11493. (org-agenda-skip-archived-trees (memq 'archive skip))
  11494. (org-agenda-skip-comment-trees (memq 'comment skip))
  11495. (org-agenda-skip-function
  11496. (car (org-delete-all '(comment archive) skip)))
  11497. (org-tags-match-list-sublevels t)
  11498. matcher file res
  11499. org-todo-keywords-for-agenda
  11500. org-done-keywords-for-agenda
  11501. org-todo-keyword-alist-for-agenda
  11502. org-drawers-for-agenda
  11503. org-tag-alist-for-agenda)
  11504. (cond
  11505. ((eq match t) (setq matcher t))
  11506. ((eq match nil) (setq matcher t))
  11507. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  11508. (save-excursion
  11509. (save-restriction
  11510. (when (eq scope 'tree)
  11511. (org-back-to-heading t)
  11512. (org-narrow-to-subtree)
  11513. (setq scope nil))
  11514. (if (not scope)
  11515. (progn
  11516. (org-prepare-agenda-buffers
  11517. (list (buffer-file-name (current-buffer))))
  11518. (setq res (org-scan-tags func matcher)))
  11519. ;; Get the right scope
  11520. (cond
  11521. ((and scope (listp scope) (symbolp (car scope)))
  11522. (setq scope (eval scope)))
  11523. ((eq scope 'agenda)
  11524. (setq scope (org-agenda-files t)))
  11525. ((eq scope 'agenda-with-archives)
  11526. (setq scope (org-agenda-files t))
  11527. (setq scope (org-add-archive-files scope)))
  11528. ((eq scope 'file)
  11529. (setq scope (list (buffer-file-name))))
  11530. ((eq scope 'file-with-archives)
  11531. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  11532. (org-prepare-agenda-buffers scope)
  11533. (while (setq file (pop scope))
  11534. (with-current-buffer (org-find-base-buffer-visiting file)
  11535. (save-excursion
  11536. (save-restriction
  11537. (widen)
  11538. (goto-char (point-min))
  11539. (setq res (append res (org-scan-tags func matcher))))))))))
  11540. res))
  11541. ;;;; Properties
  11542. ;;; Setting and retrieving properties
  11543. (defconst org-special-properties
  11544. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11545. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  11546. "The special properties valid in Org-mode.
  11547. These are properties that are not defined in the property drawer,
  11548. but in some other way.")
  11549. (defconst org-default-properties
  11550. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11551. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11552. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11553. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11554. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  11555. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  11556. "Some properties that are used by Org-mode for various purposes.
  11557. Being in this list makes sure that they are offered for completion.")
  11558. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11559. "Regular expression matching the first line of a property drawer.")
  11560. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11561. "Regular expression matching the last line of a property drawer.")
  11562. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11563. "Regular expression matching the first line of a property drawer.")
  11564. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11565. "Regular expression matching the first line of a property drawer.")
  11566. (defconst org-property-drawer-re
  11567. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11568. org-property-end-re "\\)\n?")
  11569. "Matches an entire property drawer.")
  11570. (defconst org-clock-drawer-re
  11571. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11572. org-property-end-re "\\)\n?")
  11573. "Matches an entire clock drawer.")
  11574. (defun org-property-action ()
  11575. "Do an action on properties."
  11576. (interactive)
  11577. (let (c)
  11578. (org-at-property-p)
  11579. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11580. (setq c (read-char-exclusive))
  11581. (cond
  11582. ((equal c ?s)
  11583. (call-interactively 'org-set-property))
  11584. ((equal c ?d)
  11585. (call-interactively 'org-delete-property))
  11586. ((equal c ?D)
  11587. (call-interactively 'org-delete-property-globally))
  11588. ((equal c ?c)
  11589. (call-interactively 'org-compute-property-at-point))
  11590. (t (error "No such property action %c" c)))))
  11591. (defun org-set-effort (&optional value)
  11592. "Set the effort property of the current entry.
  11593. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11594. allowed value."
  11595. (interactive "P")
  11596. (if (equal value 0) (setq value 10))
  11597. (let* ((completion-ignore-case t)
  11598. (prop org-effort-property)
  11599. (cur (org-entry-get nil prop))
  11600. (allowed (org-property-get-allowed-values nil prop 'table))
  11601. (existing (mapcar 'list (org-property-values prop)))
  11602. rpl
  11603. (val (cond
  11604. ((stringp value) value)
  11605. ((and allowed (integerp value))
  11606. (or (car (nth (1- value) allowed))
  11607. (car (org-last allowed))))
  11608. (allowed
  11609. (message "Select 1-9,0, [RET%s]: %s"
  11610. (if cur (concat "=" cur) "")
  11611. (mapconcat 'car allowed " "))
  11612. (setq rpl (read-char-exclusive))
  11613. (if (equal rpl ?\r)
  11614. cur
  11615. (setq rpl (- rpl ?0))
  11616. (if (equal rpl 0) (setq rpl 10))
  11617. (if (and (> rpl 0) (<= rpl (length allowed)))
  11618. (car (nth (1- rpl) allowed))
  11619. (org-completing-read "Effort: " allowed nil))))
  11620. (t
  11621. (let (org-completion-use-ido org-completion-use-iswitchb)
  11622. (org-completing-read
  11623. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  11624. (concat "[" cur "]") "")
  11625. ": ")
  11626. existing nil nil "" nil cur))))))
  11627. (unless (equal (org-entry-get nil prop) val)
  11628. (org-entry-put nil prop val))
  11629. (message "%s is now %s" prop val)))
  11630. (defun org-at-property-p ()
  11631. "Is cursor inside a property drawer?"
  11632. (save-excursion
  11633. (beginning-of-line 1)
  11634. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  11635. (let ((match (match-data)) ;; Keep match-data for use by calling
  11636. (p (point)) ;; procedures.
  11637. (range (unless (org-before-first-heading-p)
  11638. (org-get-property-block))))
  11639. (prog1 (and range (<= (car range) p) (< p (cdr range)))
  11640. (set-match-data match))))))
  11641. (defun org-get-property-block (&optional beg end force)
  11642. "Return the (beg . end) range of the body of the property drawer.
  11643. BEG and END can be beginning and end of subtree, if not given
  11644. they will be found.
  11645. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11646. (catch 'exit
  11647. (save-excursion
  11648. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11649. (end (or end (progn (outline-next-heading) (point)))))
  11650. (goto-char beg)
  11651. (if (re-search-forward org-property-start-re end t)
  11652. (setq beg (1+ (match-end 0)))
  11653. (if force
  11654. (save-excursion
  11655. (org-insert-property-drawer)
  11656. (setq end (progn (outline-next-heading) (point))))
  11657. (throw 'exit nil))
  11658. (goto-char beg)
  11659. (if (re-search-forward org-property-start-re end t)
  11660. (setq beg (1+ (match-end 0)))))
  11661. (if (re-search-forward org-property-end-re end t)
  11662. (setq end (match-beginning 0))
  11663. (or force (throw 'exit nil))
  11664. (goto-char beg)
  11665. (setq end beg)
  11666. (org-indent-line-function)
  11667. (insert ":END:\n"))
  11668. (cons beg end)))))
  11669. (defun org-entry-properties (&optional pom which specific)
  11670. "Get all properties of the entry at point-or-marker POM.
  11671. This includes the TODO keyword, the tags, time strings for deadline,
  11672. scheduled, and clocking, and any additional properties defined in the
  11673. entry. The return value is an alist, keys may occur multiple times
  11674. if the property key was used several times.
  11675. POM may also be nil, in which case the current entry is used.
  11676. If WHICH is nil or `all', get all properties. If WHICH is
  11677. `special' or `standard', only get that subclass. If WHICH
  11678. is a string only get exactly this property. Specific can be a string, the
  11679. specific property we are interested in. Specifying it can speed
  11680. things up because then unnecessary parsing is avoided."
  11681. (setq which (or which 'all))
  11682. (org-with-point-at pom
  11683. (let ((clockstr (substring org-clock-string 0 -1))
  11684. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  11685. (case-fold-search nil)
  11686. beg end range props sum-props key value string clocksum)
  11687. (save-excursion
  11688. (when (condition-case nil
  11689. (and (org-mode-p) (org-back-to-heading t))
  11690. (error nil))
  11691. (setq beg (point))
  11692. (setq sum-props (get-text-property (point) 'org-summaries))
  11693. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11694. (outline-next-heading)
  11695. (setq end (point))
  11696. (when (memq which '(all special))
  11697. ;; Get the special properties, like TODO and tags
  11698. (goto-char beg)
  11699. (when (and (or (not specific) (string= specific "TODO"))
  11700. (looking-at org-todo-line-regexp) (match-end 2))
  11701. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11702. (when (and (or (not specific) (string= specific "PRIORITY"))
  11703. (looking-at org-priority-regexp))
  11704. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11705. (when (and (or (not specific) (string= specific "TAGS"))
  11706. (setq value (org-get-tags-string))
  11707. (string-match "\\S-" value))
  11708. (push (cons "TAGS" value) props))
  11709. (when (and (or (not specific) (string= specific "ALLTAGS"))
  11710. (setq value (org-get-tags-at)))
  11711. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  11712. ":"))
  11713. props))
  11714. (when (or (not specific) (string= specific "BLOCKED"))
  11715. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  11716. (when (or (not specific)
  11717. (member specific org-all-time-keywords)
  11718. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  11719. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11720. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11721. string (if (equal key clockstr)
  11722. (org-no-properties
  11723. (org-trim
  11724. (buffer-substring
  11725. (match-beginning 3) (goto-char (point-at-eol)))))
  11726. (substring (org-match-string-no-properties 3) 1 -1)))
  11727. (unless key
  11728. (if (= (char-after (match-beginning 3)) ?\[)
  11729. (setq key "TIMESTAMP_IA")
  11730. (setq key "TIMESTAMP")))
  11731. (when (or (equal key clockstr) (not (assoc key props)))
  11732. (push (cons key string) props))))
  11733. )
  11734. (when (memq which '(all standard))
  11735. ;; Get the standard properties, like :PROP: ...
  11736. (setq range (org-get-property-block beg end))
  11737. (when range
  11738. (goto-char (car range))
  11739. (while (re-search-forward
  11740. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11741. (cdr range) t)
  11742. (setq key (org-match-string-no-properties 1)
  11743. value (org-trim (or (org-match-string-no-properties 2) "")))
  11744. (unless (member key excluded)
  11745. (push (cons key (or value "")) props)))))
  11746. (if clocksum
  11747. (push (cons "CLOCKSUM"
  11748. (org-columns-number-to-string (/ (float clocksum) 60.)
  11749. 'add_times))
  11750. props))
  11751. (unless (assoc "CATEGORY" props)
  11752. (setq value (or (org-get-category)
  11753. (progn (org-refresh-category-properties)
  11754. (org-get-category))))
  11755. (push (cons "CATEGORY" value) props))
  11756. (append sum-props (nreverse props)))))))
  11757. (defun org-entry-get (pom property &optional inherit)
  11758. "Get value of PROPERTY for entry at point-or-marker POM.
  11759. If INHERIT is non-nil and the entry does not have the property,
  11760. then also check higher levels of the hierarchy.
  11761. If INHERIT is the symbol `selective', use inheritance only if the setting
  11762. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11763. If the property is present but empty, the return value is the empty string.
  11764. If the property is not present at all, nil is returned."
  11765. (org-with-point-at pom
  11766. (if (and inherit (if (eq inherit 'selective)
  11767. (org-property-inherit-p property)
  11768. t))
  11769. (org-entry-get-with-inheritance property)
  11770. (if (member property org-special-properties)
  11771. ;; We need a special property. Use `org-entry-properties' to
  11772. ;; retrieve it, but specify the wanted property
  11773. (cdr (assoc property (org-entry-properties nil 'special property)))
  11774. (let ((range (org-get-property-block)))
  11775. (if (and range
  11776. (goto-char (car range))
  11777. (re-search-forward
  11778. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11779. (cdr range) t))
  11780. ;; Found the property, return it.
  11781. (if (match-end 1)
  11782. (org-match-string-no-properties 1)
  11783. "")))))))
  11784. (defun org-property-or-variable-value (var &optional inherit)
  11785. "Check if there is a property fixing the value of VAR.
  11786. If yes, return this value. If not, return the current value of the variable."
  11787. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11788. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11789. (read prop)
  11790. (symbol-value var))))
  11791. (defun org-entry-delete (pom property)
  11792. "Delete the property PROPERTY from entry at point-or-marker POM."
  11793. (org-with-point-at pom
  11794. (if (member property org-special-properties)
  11795. nil ; cannot delete these properties.
  11796. (let ((range (org-get-property-block)))
  11797. (if (and range
  11798. (goto-char (car range))
  11799. (re-search-forward
  11800. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11801. (cdr range) t))
  11802. (progn
  11803. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11804. t)
  11805. nil)))))
  11806. ;; Multi-values properties are properties that contain multiple values
  11807. ;; These values are assumed to be single words, separated by whitespace.
  11808. (defun org-entry-add-to-multivalued-property (pom property value)
  11809. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11810. (let* ((old (org-entry-get pom property))
  11811. (values (and old (org-split-string old "[ \t]"))))
  11812. (setq value (org-entry-protect-space value))
  11813. (unless (member value values)
  11814. (setq values (cons value values))
  11815. (org-entry-put pom property
  11816. (mapconcat 'identity values " ")))))
  11817. (defun org-entry-remove-from-multivalued-property (pom property value)
  11818. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11819. (let* ((old (org-entry-get pom property))
  11820. (values (and old (org-split-string old "[ \t]"))))
  11821. (setq value (org-entry-protect-space value))
  11822. (when (member value values)
  11823. (setq values (delete value values))
  11824. (org-entry-put pom property
  11825. (mapconcat 'identity values " ")))))
  11826. (defun org-entry-member-in-multivalued-property (pom property value)
  11827. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11828. (let* ((old (org-entry-get pom property))
  11829. (values (and old (org-split-string old "[ \t]"))))
  11830. (setq value (org-entry-protect-space value))
  11831. (member value values)))
  11832. (defun org-entry-get-multivalued-property (pom property)
  11833. "Return a list of values in a multivalued property."
  11834. (let* ((value (org-entry-get pom property))
  11835. (values (and value (org-split-string value "[ \t]"))))
  11836. (mapcar 'org-entry-restore-space values)))
  11837. (defun org-entry-put-multivalued-property (pom property &rest values)
  11838. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11839. VALUES should be a list of strings. Spaces will be protected."
  11840. (org-entry-put pom property
  11841. (mapconcat 'org-entry-protect-space values " "))
  11842. (let* ((value (org-entry-get pom property))
  11843. (values (and value (org-split-string value "[ \t]"))))
  11844. (mapcar 'org-entry-restore-space values)))
  11845. (defun org-entry-protect-space (s)
  11846. "Protect spaces and newline in string S."
  11847. (while (string-match " " s)
  11848. (setq s (replace-match "%20" t t s)))
  11849. (while (string-match "\n" s)
  11850. (setq s (replace-match "%0A" t t s)))
  11851. s)
  11852. (defun org-entry-restore-space (s)
  11853. "Restore spaces and newline in string S."
  11854. (while (string-match "%20" s)
  11855. (setq s (replace-match " " t t s)))
  11856. (while (string-match "%0A" s)
  11857. (setq s (replace-match "\n" t t s)))
  11858. s)
  11859. (defvar org-entry-property-inherited-from (make-marker)
  11860. "Marker pointing to the entry from where a property was inherited.
  11861. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11862. location of the entry where the inheritance search matched. If there was
  11863. no match, the marker will point nowhere.
  11864. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11865. is set.")
  11866. (defun org-entry-get-with-inheritance (property)
  11867. "Get entry property, and search higher levels if not present."
  11868. (move-marker org-entry-property-inherited-from nil)
  11869. (let (tmp)
  11870. (save-excursion
  11871. (save-restriction
  11872. (widen)
  11873. (catch 'ex
  11874. (while t
  11875. (when (setq tmp (org-entry-get nil property))
  11876. (org-back-to-heading t)
  11877. (move-marker org-entry-property-inherited-from (point))
  11878. (throw 'ex tmp))
  11879. (or (org-up-heading-safe) (throw 'ex nil)))))
  11880. (or tmp
  11881. (cdr (assoc property org-file-properties))
  11882. (cdr (assoc property org-global-properties))
  11883. (cdr (assoc property org-global-properties-fixed))))))
  11884. (defvar org-property-changed-functions nil
  11885. "Hook called when the value of a property has changed.
  11886. Each hook function should accept two arguments, the name of the property
  11887. and the new value.")
  11888. (defun org-entry-put (pom property value)
  11889. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11890. (org-with-point-at pom
  11891. (org-back-to-heading t)
  11892. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11893. range)
  11894. (cond
  11895. ((equal property "TODO")
  11896. (when (and (stringp value) (string-match "\\S-" value)
  11897. (not (member value org-todo-keywords-1)))
  11898. (error "\"%s\" is not a valid TODO state" value))
  11899. (if (or (not value)
  11900. (not (string-match "\\S-" value)))
  11901. (setq value 'none))
  11902. (org-todo value)
  11903. (org-set-tags nil 'align))
  11904. ((equal property "PRIORITY")
  11905. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11906. (string-to-char value) ?\ ))
  11907. (org-set-tags nil 'align))
  11908. ((equal property "SCHEDULED")
  11909. (if (re-search-forward org-scheduled-time-regexp end t)
  11910. (cond
  11911. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11912. ((eq value 'later) (org-timestamp-change 1 'day))
  11913. (t (call-interactively 'org-schedule)))
  11914. (call-interactively 'org-schedule)))
  11915. ((equal property "DEADLINE")
  11916. (if (re-search-forward org-deadline-time-regexp end t)
  11917. (cond
  11918. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11919. ((eq value 'later) (org-timestamp-change 1 'day))
  11920. (t (call-interactively 'org-deadline)))
  11921. (call-interactively 'org-deadline)))
  11922. ((member property org-special-properties)
  11923. (error "The %s property can not yet be set with `org-entry-put'"
  11924. property))
  11925. (t ; a non-special property
  11926. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11927. (setq range (org-get-property-block beg end 'force))
  11928. (goto-char (car range))
  11929. (if (re-search-forward
  11930. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11931. (progn
  11932. (delete-region (match-beginning 1) (match-end 1))
  11933. (goto-char (match-beginning 1)))
  11934. (goto-char (cdr range))
  11935. (insert "\n")
  11936. (backward-char 1)
  11937. (org-indent-line-function)
  11938. (insert ":" property ":"))
  11939. (and value (insert " " value))
  11940. (org-indent-line-function)))))
  11941. (run-hook-with-args 'org-property-changed-functions property value)))
  11942. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11943. "Get all property keys in the current buffer.
  11944. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11945. like tags and TODO state.
  11946. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11947. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11948. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11949. formats in the current buffer."
  11950. (let (rtn range cfmt s p)
  11951. (save-excursion
  11952. (save-restriction
  11953. (widen)
  11954. (goto-char (point-min))
  11955. (while (re-search-forward org-property-start-re nil t)
  11956. (setq range (org-get-property-block))
  11957. (goto-char (car range))
  11958. (while (re-search-forward
  11959. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11960. (cdr range) t)
  11961. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11962. (outline-next-heading))))
  11963. (when include-specials
  11964. (setq rtn (append org-special-properties rtn)))
  11965. (when include-defaults
  11966. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11967. (add-to-list 'rtn org-effort-property))
  11968. (when include-columns
  11969. (save-excursion
  11970. (save-restriction
  11971. (widen)
  11972. (goto-char (point-min))
  11973. (while (re-search-forward
  11974. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11975. nil t)
  11976. (setq cfmt (match-string 2) s 0)
  11977. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11978. cfmt s)
  11979. (setq s (match-end 0)
  11980. p (match-string 1 cfmt))
  11981. (unless (or (equal p "ITEM")
  11982. (member p org-special-properties))
  11983. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11984. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11985. (defun org-property-values (key)
  11986. "Return a list of all values of property KEY."
  11987. (save-excursion
  11988. (save-restriction
  11989. (widen)
  11990. (goto-char (point-min))
  11991. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11992. values)
  11993. (while (re-search-forward re nil t)
  11994. (add-to-list 'values (org-trim (match-string 1))))
  11995. (delete "" values)))))
  11996. (defun org-insert-property-drawer ()
  11997. "Insert a property drawer into the current entry."
  11998. (interactive)
  11999. (org-back-to-heading t)
  12000. (looking-at outline-regexp)
  12001. (let ((indent (if org-adapt-indentation
  12002. (- (match-end 0)(match-beginning 0))
  12003. 0))
  12004. (beg (point))
  12005. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12006. end hiddenp)
  12007. (outline-next-heading)
  12008. (setq end (point))
  12009. (goto-char beg)
  12010. (while (re-search-forward re end t))
  12011. (setq hiddenp (org-invisible-p))
  12012. (end-of-line 1)
  12013. (and (equal (char-after) ?\n) (forward-char 1))
  12014. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12015. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12016. ;; just skip this line
  12017. (beginning-of-line 2)
  12018. ;; Drawer start, find the end
  12019. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12020. (beginning-of-line 1)))
  12021. (org-skip-over-state-notes)
  12022. (skip-chars-backward " \t\n\r")
  12023. (if (eq (char-before) ?*) (forward-char 1))
  12024. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12025. (beginning-of-line 0)
  12026. (org-indent-to-column indent)
  12027. (beginning-of-line 2)
  12028. (org-indent-to-column indent)
  12029. (beginning-of-line 0)
  12030. (if hiddenp
  12031. (save-excursion
  12032. (org-back-to-heading t)
  12033. (hide-entry))
  12034. (org-flag-drawer t))))
  12035. (defun org-set-property (property value)
  12036. "In the current entry, set PROPERTY to VALUE.
  12037. When called interactively, this will prompt for a property name, offering
  12038. completion on existing and default properties. And then it will prompt
  12039. for a value, offering completion either on allowed values (via an inherited
  12040. xxx_ALL property) or on existing values in other instances of this property
  12041. in the current file."
  12042. (interactive
  12043. (let* ((completion-ignore-case t)
  12044. (keys (org-buffer-property-keys nil t t))
  12045. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12046. (prop (if (member prop0 keys)
  12047. prop0
  12048. (or (cdr (assoc (downcase prop0)
  12049. (mapcar (lambda (x) (cons (downcase x) x))
  12050. keys)))
  12051. prop0)))
  12052. (cur (org-entry-get nil prop))
  12053. (prompt (concat prop " value"
  12054. (if (and cur (string-match "\\S-" cur))
  12055. (concat " [" cur "]") "") ": "))
  12056. (allowed (org-property-get-allowed-values nil prop 'table))
  12057. (existing (mapcar 'list (org-property-values prop)))
  12058. (val (if allowed
  12059. (org-completing-read prompt allowed nil
  12060. (not (get-text-property 0 'org-unrestricted
  12061. (caar allowed))))
  12062. (let (org-completion-use-ido org-completion-use-iswitchb)
  12063. (org-completing-read prompt existing nil nil "" nil cur)))))
  12064. (list prop (if (equal val "") cur val))))
  12065. (unless (equal (org-entry-get nil property) value)
  12066. (org-entry-put nil property value)))
  12067. (defun org-delete-property (property)
  12068. "In the current entry, delete PROPERTY."
  12069. (interactive
  12070. (let* ((completion-ignore-case t)
  12071. (prop (org-icompleting-read "Property: " (org-entry-properties nil 'standard))))
  12072. (list prop)))
  12073. (message "Property %s %s" property
  12074. (if (org-entry-delete nil property)
  12075. "deleted"
  12076. "was not present in the entry")))
  12077. (defun org-delete-property-globally (property)
  12078. "Remove PROPERTY globally, from all entries."
  12079. (interactive
  12080. (let* ((completion-ignore-case t)
  12081. (prop (org-icompleting-read
  12082. "Globally remove property: "
  12083. (mapcar 'list (org-buffer-property-keys)))))
  12084. (list prop)))
  12085. (save-excursion
  12086. (save-restriction
  12087. (widen)
  12088. (goto-char (point-min))
  12089. (let ((cnt 0))
  12090. (while (re-search-forward
  12091. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12092. nil t)
  12093. (setq cnt (1+ cnt))
  12094. (replace-match ""))
  12095. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12096. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12097. (defun org-compute-property-at-point ()
  12098. "Compute the property at point.
  12099. This looks for an enclosing column format, extracts the operator and
  12100. then applies it to the property in the column format's scope."
  12101. (interactive)
  12102. (unless (org-at-property-p)
  12103. (error "Not at a property"))
  12104. (let ((prop (org-match-string-no-properties 2)))
  12105. (org-columns-get-format-and-top-level)
  12106. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12107. (error "No operator defined for property %s" prop))
  12108. (org-columns-compute prop)))
  12109. (defvar org-property-allowed-value-functions nil
  12110. "Hook for functions supplying allowed values for a specific property.
  12111. The functions must take a single argument, the name of the property, and
  12112. return a flat list of allowed values. If \":ETC\" is one of
  12113. the values, this means that these values are intended as defaults for
  12114. completion, but that other values should be allowed too.
  12115. The functions must return nil if they are not responsible for this
  12116. property.")
  12117. (defun org-property-get-allowed-values (pom property &optional table)
  12118. "Get allowed values for the property PROPERTY.
  12119. When TABLE is non-nil, return an alist that can directly be used for
  12120. completion."
  12121. (let (vals)
  12122. (cond
  12123. ((equal property "TODO")
  12124. (setq vals (org-with-point-at pom
  12125. (append org-todo-keywords-1 '("")))))
  12126. ((equal property "PRIORITY")
  12127. (let ((n org-lowest-priority))
  12128. (while (>= n org-highest-priority)
  12129. (push (char-to-string n) vals)
  12130. (setq n (1- n)))))
  12131. ((member property org-special-properties))
  12132. ((setq vals (run-hook-with-args-until-success
  12133. 'org-property-allowed-value-functions property)))
  12134. (t
  12135. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12136. (when (and vals (string-match "\\S-" vals))
  12137. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12138. (setq vals (mapcar (lambda (x)
  12139. (cond ((stringp x) x)
  12140. ((numberp x) (number-to-string x))
  12141. ((symbolp x) (symbol-name x))
  12142. (t "???")))
  12143. vals)))))
  12144. (when (member ":ETC" vals)
  12145. (setq vals (remove ":ETC" vals))
  12146. (org-add-props (car vals) '(org-unrestricted t)))
  12147. (if table (mapcar 'list vals) vals)))
  12148. (defun org-property-previous-allowed-value (&optional previous)
  12149. "Switch to the next allowed value for this property."
  12150. (interactive)
  12151. (org-property-next-allowed-value t))
  12152. (defun org-property-next-allowed-value (&optional previous)
  12153. "Switch to the next allowed value for this property."
  12154. (interactive)
  12155. (unless (org-at-property-p)
  12156. (error "Not at a property"))
  12157. (let* ((key (match-string 2))
  12158. (value (match-string 3))
  12159. (allowed (or (org-property-get-allowed-values (point) key)
  12160. (and (member value '("[ ]" "[-]" "[X]"))
  12161. '("[ ]" "[X]"))))
  12162. nval)
  12163. (unless allowed
  12164. (error "Allowed values for this property have not been defined"))
  12165. (if previous (setq allowed (reverse allowed)))
  12166. (if (member value allowed)
  12167. (setq nval (car (cdr (member value allowed)))))
  12168. (setq nval (or nval (car allowed)))
  12169. (if (equal nval value)
  12170. (error "Only one allowed value for this property"))
  12171. (org-at-property-p)
  12172. (replace-match (concat " :" key ": " nval) t t)
  12173. (org-indent-line-function)
  12174. (beginning-of-line 1)
  12175. (skip-chars-forward " \t")
  12176. (run-hook-with-args 'org-property-changed-functions key nval)))
  12177. (defun org-find-entry-with-id (ident)
  12178. "Locate the entry that contains the ID property with exact value IDENT.
  12179. IDENT can be a string, a symbol or a number, this function will search for
  12180. the string representation of it.
  12181. Return the position where this entry starts, or nil if there is no such entry."
  12182. (interactive "sID: ")
  12183. (let ((id (cond
  12184. ((stringp ident) ident)
  12185. ((symbol-name ident) (symbol-name ident))
  12186. ((numberp ident) (number-to-string ident))
  12187. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12188. (case-fold-search nil))
  12189. (save-excursion
  12190. (save-restriction
  12191. (widen)
  12192. (goto-char (point-min))
  12193. (when (re-search-forward
  12194. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12195. nil t)
  12196. (org-back-to-heading t)
  12197. (point))))))
  12198. ;;;; Timestamps
  12199. (defvar org-last-changed-timestamp nil)
  12200. (defvar org-last-inserted-timestamp nil
  12201. "The last time stamp inserted with `org-insert-time-stamp'.")
  12202. (defvar org-time-was-given) ; dynamically scoped parameter
  12203. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12204. (defvar org-ts-what) ; dynamically scoped parameter
  12205. (defun org-time-stamp (arg &optional inactive)
  12206. "Prompt for a date/time and insert a time stamp.
  12207. If the user specifies a time like HH:MM, or if this command is called
  12208. with a prefix argument, the time stamp will contain date and time.
  12209. Otherwise, only the date will be included. All parts of a date not
  12210. specified by the user will be filled in from the current date/time.
  12211. So if you press just return without typing anything, the time stamp
  12212. will represent the current date/time. If there is already a timestamp
  12213. at the cursor, it will be modified."
  12214. (interactive "P")
  12215. (let* ((ts nil)
  12216. (default-time
  12217. ;; Default time is either today, or, when entering a range,
  12218. ;; the range start.
  12219. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12220. (save-excursion
  12221. (re-search-backward
  12222. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12223. (- (point) 20) t)))
  12224. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12225. (current-time)))
  12226. (default-input (and ts (org-get-compact-tod ts)))
  12227. org-time-was-given org-end-time-was-given time)
  12228. (cond
  12229. ((and (org-at-timestamp-p t)
  12230. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12231. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12232. (insert "--")
  12233. (setq time (let ((this-command this-command))
  12234. (org-read-date arg 'totime nil nil
  12235. default-time default-input)))
  12236. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12237. ((org-at-timestamp-p t)
  12238. (setq time (let ((this-command this-command))
  12239. (org-read-date arg 'totime nil nil default-time default-input)))
  12240. (when (org-at-timestamp-p t) ; just to get the match data
  12241. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12242. (replace-match "")
  12243. (setq org-last-changed-timestamp
  12244. (org-insert-time-stamp
  12245. time (or org-time-was-given arg)
  12246. inactive nil nil (list org-end-time-was-given))))
  12247. (message "Timestamp updated"))
  12248. (t
  12249. (setq time (let ((this-command this-command))
  12250. (org-read-date arg 'totime nil nil default-time default-input)))
  12251. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12252. nil nil (list org-end-time-was-given))))))
  12253. ;; FIXME: can we use this for something else, like computing time differences?
  12254. (defun org-get-compact-tod (s)
  12255. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12256. (let* ((t1 (match-string 1 s))
  12257. (h1 (string-to-number (match-string 2 s)))
  12258. (m1 (string-to-number (match-string 3 s)))
  12259. (t2 (and (match-end 4) (match-string 5 s)))
  12260. (h2 (and t2 (string-to-number (match-string 6 s))))
  12261. (m2 (and t2 (string-to-number (match-string 7 s))))
  12262. dh dm)
  12263. (if (not t2)
  12264. t1
  12265. (setq dh (- h2 h1) dm (- m2 m1))
  12266. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12267. (concat t1 "+" (number-to-string dh)
  12268. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12269. (defun org-time-stamp-inactive (&optional arg)
  12270. "Insert an inactive time stamp.
  12271. An inactive time stamp is enclosed in square brackets instead of angle
  12272. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12273. does not link to the calendar and cannot be changed with the S-cursor keys.
  12274. So these are more for recording a certain time/date."
  12275. (interactive "P")
  12276. (org-time-stamp arg 'inactive))
  12277. (defvar org-date-ovl (org-make-overlay 1 1))
  12278. (org-overlay-put org-date-ovl 'face 'org-warning)
  12279. (org-detach-overlay org-date-ovl)
  12280. (defvar org-ans1) ; dynamically scoped parameter
  12281. (defvar org-ans2) ; dynamically scoped parameter
  12282. (defvar org-plain-time-of-day-regexp) ; defined below
  12283. (defvar org-overriding-default-time nil) ; dynamically scoped
  12284. (defvar org-read-date-overlay nil)
  12285. (defvar org-dcst nil) ; dynamically scoped
  12286. (defvar org-read-date-history nil)
  12287. (defvar org-read-date-final-answer nil)
  12288. (defun org-read-date (&optional with-time to-time from-string prompt
  12289. default-time default-input)
  12290. "Read a date, possibly a time, and make things smooth for the user.
  12291. The prompt will suggest to enter an ISO date, but you can also enter anything
  12292. which will at least partially be understood by `parse-time-string'.
  12293. Unrecognized parts of the date will default to the current day, month, year,
  12294. hour and minute. If this command is called to replace a timestamp at point,
  12295. of to enter the second timestamp of a range, the default time is taken from the
  12296. existing stamp. For example,
  12297. 3-2-5 --> 2003-02-05
  12298. feb 15 --> currentyear-02-15
  12299. sep 12 9 --> 2009-09-12
  12300. 12:45 --> today 12:45
  12301. 22 sept 0:34 --> currentyear-09-22 0:34
  12302. 12 --> currentyear-currentmonth-12
  12303. Fri --> nearest Friday (today or later)
  12304. etc.
  12305. Furthermore you can specify a relative date by giving, as the *first* thing
  12306. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12307. change in days weeks, months, years.
  12308. With a single plus or minus, the date is relative to today. With a double
  12309. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12310. +4d --> four days from today
  12311. +4 --> same as above
  12312. +2w --> two weeks from today
  12313. ++5 --> five days from default date
  12314. The function understands only English month and weekday abbreviations,
  12315. but this can be configured with the variables `parse-time-months' and
  12316. `parse-time-weekdays'.
  12317. While prompting, a calendar is popped up - you can also select the
  12318. date with the mouse (button 1). The calendar shows a period of three
  12319. months. To scroll it to other months, use the keys `>' and `<'.
  12320. If you don't like the calendar, turn it off with
  12321. \(setq org-read-date-popup-calendar nil)
  12322. With optional argument TO-TIME, the date will immediately be converted
  12323. to an internal time.
  12324. With an optional argument WITH-TIME, the prompt will suggest to also
  12325. insert a time. Note that when WITH-TIME is not set, you can still
  12326. enter a time, and this function will inform the calling routine about
  12327. this change. The calling routine may then choose to change the format
  12328. used to insert the time stamp into the buffer to include the time.
  12329. With optional argument FROM-STRING, read from this string instead from
  12330. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12331. the time/date that is used for everything that is not specified by the
  12332. user."
  12333. (require 'parse-time)
  12334. (let* ((org-time-stamp-rounding-minutes
  12335. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12336. (org-dcst org-display-custom-times)
  12337. (ct (org-current-time))
  12338. (def (or org-overriding-default-time default-time ct))
  12339. (defdecode (decode-time def))
  12340. (dummy (progn
  12341. (when (< (nth 2 defdecode) org-extend-today-until)
  12342. (setcar (nthcdr 2 defdecode) -1)
  12343. (setcar (nthcdr 1 defdecode) 59)
  12344. (setq def (apply 'encode-time defdecode)
  12345. defdecode (decode-time def)))))
  12346. (calendar-frame-setup nil)
  12347. (calendar-move-hook nil)
  12348. (calendar-view-diary-initially-flag nil)
  12349. (view-diary-entries-initially nil)
  12350. (calendar-view-holidays-initially-flag nil)
  12351. (view-calendar-holidays-initially nil)
  12352. (timestr (format-time-string
  12353. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12354. (prompt (concat (if prompt (concat prompt " ") "")
  12355. (format "Date+time [%s]: " timestr)))
  12356. ans (org-ans0 "") org-ans1 org-ans2 final)
  12357. (cond
  12358. (from-string (setq ans from-string))
  12359. (org-read-date-popup-calendar
  12360. (save-excursion
  12361. (save-window-excursion
  12362. (calendar)
  12363. (calendar-forward-day (- (time-to-days def)
  12364. (calendar-absolute-from-gregorian
  12365. (calendar-current-date))))
  12366. (org-eval-in-calendar nil t)
  12367. (let* ((old-map (current-local-map))
  12368. (map (copy-keymap calendar-mode-map))
  12369. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12370. (org-defkey map (kbd "RET") 'org-calendar-select)
  12371. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  12372. 'org-calendar-select-mouse)
  12373. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  12374. 'org-calendar-select-mouse)
  12375. (org-defkey minibuffer-local-map [(meta shift left)]
  12376. (lambda () (interactive)
  12377. (org-eval-in-calendar '(calendar-backward-month 1))))
  12378. (org-defkey minibuffer-local-map [(meta shift right)]
  12379. (lambda () (interactive)
  12380. (org-eval-in-calendar '(calendar-forward-month 1))))
  12381. (org-defkey minibuffer-local-map [(meta shift up)]
  12382. (lambda () (interactive)
  12383. (org-eval-in-calendar '(calendar-backward-year 1))))
  12384. (org-defkey minibuffer-local-map [(meta shift down)]
  12385. (lambda () (interactive)
  12386. (org-eval-in-calendar '(calendar-forward-year 1))))
  12387. (org-defkey minibuffer-local-map [?\e (shift left)]
  12388. (lambda () (interactive)
  12389. (org-eval-in-calendar '(calendar-backward-month 1))))
  12390. (org-defkey minibuffer-local-map [?\e (shift right)]
  12391. (lambda () (interactive)
  12392. (org-eval-in-calendar '(calendar-forward-month 1))))
  12393. (org-defkey minibuffer-local-map [?\e (shift up)]
  12394. (lambda () (interactive)
  12395. (org-eval-in-calendar '(calendar-backward-year 1))))
  12396. (org-defkey minibuffer-local-map [?\e (shift down)]
  12397. (lambda () (interactive)
  12398. (org-eval-in-calendar '(calendar-forward-year 1))))
  12399. (org-defkey minibuffer-local-map [(shift up)]
  12400. (lambda () (interactive)
  12401. (org-eval-in-calendar '(calendar-backward-week 1))))
  12402. (org-defkey minibuffer-local-map [(shift down)]
  12403. (lambda () (interactive)
  12404. (org-eval-in-calendar '(calendar-forward-week 1))))
  12405. (org-defkey minibuffer-local-map [(shift left)]
  12406. (lambda () (interactive)
  12407. (org-eval-in-calendar '(calendar-backward-day 1))))
  12408. (org-defkey minibuffer-local-map [(shift right)]
  12409. (lambda () (interactive)
  12410. (org-eval-in-calendar '(calendar-forward-day 1))))
  12411. (org-defkey minibuffer-local-map ">"
  12412. (lambda () (interactive)
  12413. (org-eval-in-calendar '(scroll-calendar-left 1))))
  12414. (org-defkey minibuffer-local-map "<"
  12415. (lambda () (interactive)
  12416. (org-eval-in-calendar '(scroll-calendar-right 1))))
  12417. (run-hooks 'org-read-date-minibuffer-setup-hook)
  12418. (unwind-protect
  12419. (progn
  12420. (use-local-map map)
  12421. (add-hook 'post-command-hook 'org-read-date-display)
  12422. (setq org-ans0 (read-string prompt default-input
  12423. 'org-read-date-history nil))
  12424. ;; org-ans0: from prompt
  12425. ;; org-ans1: from mouse click
  12426. ;; org-ans2: from calendar motion
  12427. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  12428. (remove-hook 'post-command-hook 'org-read-date-display)
  12429. (use-local-map old-map)
  12430. (when org-read-date-overlay
  12431. (org-delete-overlay org-read-date-overlay)
  12432. (setq org-read-date-overlay nil)))))))
  12433. (t ; Naked prompt only
  12434. (unwind-protect
  12435. (setq ans (read-string prompt default-input
  12436. 'org-read-date-history timestr))
  12437. (when org-read-date-overlay
  12438. (org-delete-overlay org-read-date-overlay)
  12439. (setq org-read-date-overlay nil)))))
  12440. (setq final (org-read-date-analyze ans def defdecode))
  12441. (setq org-read-date-final-answer ans)
  12442. (if to-time
  12443. (apply 'encode-time final)
  12444. (if (and (boundp 'org-time-was-given) org-time-was-given)
  12445. (format "%04d-%02d-%02d %02d:%02d"
  12446. (nth 5 final) (nth 4 final) (nth 3 final)
  12447. (nth 2 final) (nth 1 final))
  12448. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  12449. (defvar def)
  12450. (defvar defdecode)
  12451. (defvar with-time)
  12452. (defvar org-read-date-analyze-futurep nil)
  12453. (defun org-read-date-display ()
  12454. "Display the current date prompt interpretation in the minibuffer."
  12455. (when org-read-date-display-live
  12456. (when org-read-date-overlay
  12457. (org-delete-overlay org-read-date-overlay))
  12458. (let ((p (point)))
  12459. (end-of-line 1)
  12460. (while (not (equal (buffer-substring
  12461. (max (point-min) (- (point) 4)) (point))
  12462. " "))
  12463. (insert " "))
  12464. (goto-char p))
  12465. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  12466. " " (or org-ans1 org-ans2)))
  12467. (org-end-time-was-given nil)
  12468. (f (org-read-date-analyze ans def defdecode))
  12469. (fmts (if org-dcst
  12470. org-time-stamp-custom-formats
  12471. org-time-stamp-formats))
  12472. (fmt (if (or with-time
  12473. (and (boundp 'org-time-was-given) org-time-was-given))
  12474. (cdr fmts)
  12475. (car fmts)))
  12476. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  12477. (when (and org-end-time-was-given
  12478. (string-match org-plain-time-of-day-regexp txt))
  12479. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  12480. org-end-time-was-given
  12481. (substring txt (match-end 0)))))
  12482. (when org-read-date-analyze-futurep
  12483. (setq txt (concat txt " (=>F)")))
  12484. (setq org-read-date-overlay
  12485. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  12486. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  12487. (defun org-read-date-analyze (ans def defdecode)
  12488. "Analyse the combined answer of the date prompt."
  12489. ;; FIXME: cleanup and comment
  12490. (let ((nowdecode (decode-time (current-time)))
  12491. delta deltan deltaw deltadef year month day
  12492. hour minute second wday pm h2 m2 tl wday1
  12493. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  12494. (setq org-read-date-analyze-futurep nil)
  12495. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  12496. (setq ans "+0"))
  12497. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  12498. (setq ans (replace-match "" t t ans)
  12499. deltan (car delta)
  12500. deltaw (nth 1 delta)
  12501. deltadef (nth 2 delta)))
  12502. ;; Check if there is an iso week date in there
  12503. ;; If yes, store the info and postpone interpreting it until the rest
  12504. ;; of the parsing is done
  12505. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  12506. (setq iso-year (if (match-end 1)
  12507. (org-small-year-to-year
  12508. (string-to-number (match-string 1 ans))))
  12509. iso-weekday (if (match-end 3)
  12510. (string-to-number (match-string 3 ans)))
  12511. iso-week (string-to-number (match-string 2 ans)))
  12512. (setq ans (replace-match "" t t ans)))
  12513. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  12514. (when (string-match
  12515. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  12516. (setq year (if (match-end 2)
  12517. (string-to-number (match-string 2 ans))
  12518. (progn (setq kill-year t)
  12519. (string-to-number (format-time-string "%Y"))))
  12520. month (string-to-number (match-string 3 ans))
  12521. day (string-to-number (match-string 4 ans)))
  12522. (if (< year 100) (setq year (+ 2000 year)))
  12523. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12524. t nil ans)))
  12525. ;; Help matching american dates, like 5/30 or 5/30/7
  12526. (when (string-match
  12527. "^ *\\([0-3]?[0-9]\\)/\\([0-1]?[0-9]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  12528. (setq year (if (match-end 4)
  12529. (string-to-number (match-string 4 ans))
  12530. (progn (setq kill-year t)
  12531. (string-to-number (format-time-string "%Y"))))
  12532. month (string-to-number (match-string 1 ans))
  12533. day (string-to-number (match-string 2 ans)))
  12534. (if (< year 100) (setq year (+ 2000 year)))
  12535. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12536. t nil ans)))
  12537. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  12538. ;; If there is a time with am/pm, and *no* time without it, we convert
  12539. ;; so that matching will be successful.
  12540. (loop for i from 1 to 2 do ; twice, for end time as well
  12541. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  12542. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  12543. (setq hour (string-to-number (match-string 1 ans))
  12544. minute (if (match-end 3)
  12545. (string-to-number (match-string 3 ans))
  12546. 0)
  12547. pm (equal ?p
  12548. (string-to-char (downcase (match-string 4 ans)))))
  12549. (if (and (= hour 12) (not pm))
  12550. (setq hour 0)
  12551. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12552. (setq ans (replace-match (format "%02d:%02d" hour minute)
  12553. t t ans))))
  12554. ;; Check if a time range is given as a duration
  12555. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  12556. (setq hour (string-to-number (match-string 1 ans))
  12557. h2 (+ hour (string-to-number (match-string 3 ans)))
  12558. minute (string-to-number (match-string 2 ans))
  12559. m2 (+ minute (if (match-end 5) (string-to-number
  12560. (match-string 5 ans))0)))
  12561. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  12562. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  12563. t t ans)))
  12564. ;; Check if there is a time range
  12565. (when (boundp 'org-end-time-was-given)
  12566. (setq org-time-was-given nil)
  12567. (when (and (string-match org-plain-time-of-day-regexp ans)
  12568. (match-end 8))
  12569. (setq org-end-time-was-given (match-string 8 ans))
  12570. (setq ans (concat (substring ans 0 (match-beginning 7))
  12571. (substring ans (match-end 7))))))
  12572. (setq tl (parse-time-string ans)
  12573. day (or (nth 3 tl) (nth 3 defdecode))
  12574. month (or (nth 4 tl)
  12575. (if (and org-read-date-prefer-future
  12576. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  12577. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  12578. (nth 4 defdecode)))
  12579. year (or (and (not kill-year) (nth 5 tl))
  12580. (if (and org-read-date-prefer-future
  12581. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  12582. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  12583. (nth 5 defdecode)))
  12584. hour (or (nth 2 tl) (nth 2 defdecode))
  12585. minute (or (nth 1 tl) (nth 1 defdecode))
  12586. second (or (nth 0 tl) 0)
  12587. wday (nth 6 tl))
  12588. (when (and (eq org-read-date-prefer-future 'time)
  12589. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  12590. (equal day (nth 3 nowdecode))
  12591. (equal month (nth 4 nowdecode))
  12592. (equal year (nth 5 nowdecode))
  12593. (nth 2 tl)
  12594. (or (< (nth 2 tl) (nth 2 nowdecode))
  12595. (and (= (nth 2 tl) (nth 2 nowdecode))
  12596. (nth 1 tl)
  12597. (< (nth 1 tl) (nth 1 nowdecode)))))
  12598. (setq day (1+ day)
  12599. futurep t))
  12600. ;; Special date definitions below
  12601. (cond
  12602. (iso-week
  12603. ;; There was an iso week
  12604. (require 'cal-iso)
  12605. (setq futurep nil)
  12606. (setq year (or iso-year year)
  12607. day (or iso-weekday wday 1)
  12608. wday nil ; to make sure that the trigger below does not match
  12609. iso-date (calendar-gregorian-from-absolute
  12610. (calendar-absolute-from-iso
  12611. (list iso-week day year))))
  12612. ; FIXME: Should we also push ISO weeks into the future?
  12613. ; (when (and org-read-date-prefer-future
  12614. ; (not iso-year)
  12615. ; (< (calendar-absolute-from-gregorian iso-date)
  12616. ; (time-to-days (current-time))))
  12617. ; (setq year (1+ year)
  12618. ; iso-date (calendar-gregorian-from-absolute
  12619. ; (calendar-absolute-from-iso
  12620. ; (list iso-week day year)))))
  12621. (setq month (car iso-date)
  12622. year (nth 2 iso-date)
  12623. day (nth 1 iso-date)))
  12624. (deltan
  12625. (setq futurep nil)
  12626. (unless deltadef
  12627. (let ((now (decode-time (current-time))))
  12628. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12629. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12630. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12631. ((equal deltaw "m") (setq month (+ month deltan)))
  12632. ((equal deltaw "y") (setq year (+ year deltan)))))
  12633. ((and wday (not (nth 3 tl)))
  12634. (setq futurep nil)
  12635. ;; Weekday was given, but no day, so pick that day in the week
  12636. ;; on or after the derived date.
  12637. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12638. (unless (equal wday wday1)
  12639. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12640. (if (and (boundp 'org-time-was-given)
  12641. (nth 2 tl))
  12642. (setq org-time-was-given t))
  12643. (if (< year 100) (setq year (+ 2000 year)))
  12644. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12645. (setq org-read-date-analyze-futurep futurep)
  12646. (list second minute hour day month year)))
  12647. (defvar parse-time-weekdays)
  12648. (defun org-read-date-get-relative (s today default)
  12649. "Check string S for special relative date string.
  12650. TODAY and DEFAULT are internal times, for today and for a default.
  12651. Return shift list (N what def-flag)
  12652. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12653. N is the number of WHATs to shift.
  12654. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12655. the DEFAULT date rather than TODAY."
  12656. (when (and
  12657. (string-match
  12658. (concat
  12659. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12660. "\\([0-9]+\\)?"
  12661. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12662. "\\([ \t]\\|$\\)") s)
  12663. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12664. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12665. (string-to-char (substring (match-string 1 s) -1))
  12666. ?+))
  12667. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12668. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12669. (what (if (match-end 3) (match-string 3 s) "d"))
  12670. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12671. (date (if rel default today))
  12672. (wday (nth 6 (decode-time date)))
  12673. delta)
  12674. (if wday1
  12675. (progn
  12676. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12677. (if (= dir ?-) (setq delta (- delta 7)))
  12678. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12679. (list delta "d" rel))
  12680. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12681. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  12682. "Turn a user-specified date into the internal representation.
  12683. The internal representation needed by the calendar is (month day year).
  12684. This is a wrapper to handle the brain-dead convention in calendar that
  12685. user function argument order change dependent on argument order."
  12686. (if (boundp 'calendar-date-style)
  12687. (cond
  12688. ((eq calendar-date-style 'american)
  12689. (list arg1 arg2 arg3))
  12690. ((eq calendar-date-style 'european)
  12691. (list arg2 arg1 arg3))
  12692. ((eq calendar-date-style 'iso)
  12693. (list arg2 arg3 arg1)))
  12694. (if (org-bound-and-true-p european-calendar-style)
  12695. (list arg2 arg1 arg3)
  12696. (list arg1 arg2 arg3))))
  12697. (defun org-eval-in-calendar (form &optional keepdate)
  12698. "Eval FORM in the calendar window and return to current window.
  12699. Also, store the cursor date in variable org-ans2."
  12700. (let ((sf (selected-frame))
  12701. (sw (selected-window)))
  12702. (select-window (get-buffer-window "*Calendar*" t))
  12703. (eval form)
  12704. (when (and (not keepdate) (calendar-cursor-to-date))
  12705. (let* ((date (calendar-cursor-to-date))
  12706. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12707. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12708. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12709. (select-window sw)
  12710. (org-select-frame-set-input-focus sf)))
  12711. (defun org-calendar-select ()
  12712. "Return to `org-read-date' with the date currently selected.
  12713. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12714. (interactive)
  12715. (when (calendar-cursor-to-date)
  12716. (let* ((date (calendar-cursor-to-date))
  12717. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12718. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12719. (if (active-minibuffer-window) (exit-minibuffer))))
  12720. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12721. "Insert a date stamp for the date given by the internal TIME.
  12722. WITH-HM means use the stamp format that includes the time of the day.
  12723. INACTIVE means use square brackets instead of angular ones, so that the
  12724. stamp will not contribute to the agenda.
  12725. PRE and POST are optional strings to be inserted before and after the
  12726. stamp.
  12727. The command returns the inserted time stamp."
  12728. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12729. stamp)
  12730. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12731. (insert-before-markers (or pre ""))
  12732. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12733. (when (listp extra)
  12734. (setq extra (car extra))
  12735. (if (and (stringp extra)
  12736. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12737. (setq extra (format "-%02d:%02d"
  12738. (string-to-number (match-string 1 extra))
  12739. (string-to-number (match-string 2 extra))))
  12740. (setq extra nil)))
  12741. (when extra
  12742. (backward-char 1)
  12743. (insert-before-markers extra)
  12744. (forward-char 1))
  12745. (insert-before-markers (or post ""))
  12746. (setq org-last-inserted-timestamp stamp)))
  12747. (defun org-toggle-time-stamp-overlays ()
  12748. "Toggle the use of custom time stamp formats."
  12749. (interactive)
  12750. (setq org-display-custom-times (not org-display-custom-times))
  12751. (unless org-display-custom-times
  12752. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12753. (while (setq p (next-single-property-change p 'display))
  12754. (if (and (get-text-property p 'display)
  12755. (eq (get-text-property p 'face) 'org-date))
  12756. (remove-text-properties
  12757. p (setq p (next-single-property-change p 'display))
  12758. '(display t))))
  12759. (set-buffer-modified-p bmp)))
  12760. (if (featurep 'xemacs)
  12761. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12762. (org-restart-font-lock)
  12763. (setq org-table-may-need-update t)
  12764. (if org-display-custom-times
  12765. (message "Time stamps are overlayed with custom format")
  12766. (message "Time stamp overlays removed")))
  12767. (defun org-display-custom-time (beg end)
  12768. "Overlay modified time stamp format over timestamp between BEG and END."
  12769. (let* ((ts (buffer-substring beg end))
  12770. t1 w1 with-hm tf time str w2 (off 0))
  12771. (save-match-data
  12772. (setq t1 (org-parse-time-string ts t))
  12773. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  12774. (setq off (- (match-end 0) (match-beginning 0)))))
  12775. (setq end (- end off))
  12776. (setq w1 (- end beg)
  12777. with-hm (and (nth 1 t1) (nth 2 t1))
  12778. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12779. time (org-fix-decoded-time t1)
  12780. str (org-add-props
  12781. (format-time-string
  12782. (substring tf 1 -1) (apply 'encode-time time))
  12783. nil 'mouse-face 'highlight)
  12784. w2 (length str))
  12785. (if (not (= w2 w1))
  12786. (add-text-properties (1+ beg) (+ 2 beg)
  12787. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12788. (if (featurep 'xemacs)
  12789. (progn
  12790. (put-text-property beg end 'invisible t)
  12791. (put-text-property beg end 'end-glyph (make-glyph str)))
  12792. (put-text-property beg end 'display str))))
  12793. (defun org-translate-time (string)
  12794. "Translate all timestamps in STRING to custom format.
  12795. But do this only if the variable `org-display-custom-times' is set."
  12796. (when org-display-custom-times
  12797. (save-match-data
  12798. (let* ((start 0)
  12799. (re org-ts-regexp-both)
  12800. t1 with-hm inactive tf time str beg end)
  12801. (while (setq start (string-match re string start))
  12802. (setq beg (match-beginning 0)
  12803. end (match-end 0)
  12804. t1 (save-match-data
  12805. (org-parse-time-string (substring string beg end) t))
  12806. with-hm (and (nth 1 t1) (nth 2 t1))
  12807. inactive (equal (substring string beg (1+ beg)) "[")
  12808. tf (funcall (if with-hm 'cdr 'car)
  12809. org-time-stamp-custom-formats)
  12810. time (org-fix-decoded-time t1)
  12811. str (format-time-string
  12812. (concat
  12813. (if inactive "[" "<") (substring tf 1 -1)
  12814. (if inactive "]" ">"))
  12815. (apply 'encode-time time))
  12816. string (replace-match str t t string)
  12817. start (+ start (length str)))))))
  12818. string)
  12819. (defun org-fix-decoded-time (time)
  12820. "Set 0 instead of nil for the first 6 elements of time.
  12821. Don't touch the rest."
  12822. (let ((n 0))
  12823. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12824. (defun org-days-to-time (timestamp-string)
  12825. "Difference between TIMESTAMP-STRING and now in days."
  12826. (- (time-to-days (org-time-string-to-time timestamp-string))
  12827. (time-to-days (current-time))))
  12828. (defun org-deadline-close (timestamp-string &optional ndays)
  12829. "Is the time in TIMESTAMP-STRING close to the current date?"
  12830. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12831. (and (< (org-days-to-time timestamp-string) ndays)
  12832. (not (org-entry-is-done-p))))
  12833. (defun org-get-wdays (ts)
  12834. "Get the deadline lead time appropriate for timestring TS."
  12835. (cond
  12836. ((<= org-deadline-warning-days 0)
  12837. ;; 0 or negative, enforce this value no matter what
  12838. (- org-deadline-warning-days))
  12839. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12840. ;; lead time is specified.
  12841. (floor (* (string-to-number (match-string 1 ts))
  12842. (cdr (assoc (match-string 2 ts)
  12843. '(("d" . 1) ("w" . 7)
  12844. ("m" . 30.4) ("y" . 365.25)))))))
  12845. ;; go for the default.
  12846. (t org-deadline-warning-days)))
  12847. (defun org-calendar-select-mouse (ev)
  12848. "Return to `org-read-date' with the date currently selected.
  12849. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12850. (interactive "e")
  12851. (mouse-set-point ev)
  12852. (when (calendar-cursor-to-date)
  12853. (let* ((date (calendar-cursor-to-date))
  12854. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12855. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12856. (if (active-minibuffer-window) (exit-minibuffer))))
  12857. (defun org-check-deadlines (ndays)
  12858. "Check if there are any deadlines due or past due.
  12859. A deadline is considered due if it happens within `org-deadline-warning-days'
  12860. days from today's date. If the deadline appears in an entry marked DONE,
  12861. it is not shown. The prefix arg NDAYS can be used to test that many
  12862. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12863. (interactive "P")
  12864. (let* ((org-warn-days
  12865. (cond
  12866. ((equal ndays '(4)) 100000)
  12867. (ndays (prefix-numeric-value ndays))
  12868. (t (abs org-deadline-warning-days))))
  12869. (case-fold-search nil)
  12870. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12871. (callback
  12872. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12873. (message "%d deadlines past-due or due within %d days"
  12874. (org-occur regexp nil callback)
  12875. org-warn-days)))
  12876. (defun org-check-before-date (date)
  12877. "Check if there are deadlines or scheduled entries before DATE."
  12878. (interactive (list (org-read-date)))
  12879. (let ((case-fold-search nil)
  12880. (regexp (concat "\\<\\(" org-deadline-string
  12881. "\\|" org-scheduled-string
  12882. "\\) *<\\([^>]+\\)>"))
  12883. (callback
  12884. (lambda () (time-less-p
  12885. (org-time-string-to-time (match-string 2))
  12886. (org-time-string-to-time date)))))
  12887. (message "%d entries before %s"
  12888. (org-occur regexp nil callback) date)))
  12889. (defun org-check-after-date (date)
  12890. "Check if there are deadlines or scheduled entries after DATE."
  12891. (interactive (list (org-read-date)))
  12892. (let ((case-fold-search nil)
  12893. (regexp (concat "\\<\\(" org-deadline-string
  12894. "\\|" org-scheduled-string
  12895. "\\) *<\\([^>]+\\)>"))
  12896. (callback
  12897. (lambda () (not
  12898. (time-less-p
  12899. (org-time-string-to-time (match-string 2))
  12900. (org-time-string-to-time date))))))
  12901. (message "%d entries after %s"
  12902. (org-occur regexp nil callback) date)))
  12903. (defun org-evaluate-time-range (&optional to-buffer)
  12904. "Evaluate a time range by computing the difference between start and end.
  12905. Normally the result is just printed in the echo area, but with prefix arg
  12906. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12907. If the time range is actually in a table, the result is inserted into the
  12908. next column.
  12909. For time difference computation, a year is assumed to be exactly 365
  12910. days in order to avoid rounding problems."
  12911. (interactive "P")
  12912. (or
  12913. (org-clock-update-time-maybe)
  12914. (save-excursion
  12915. (unless (org-at-date-range-p t)
  12916. (goto-char (point-at-bol))
  12917. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12918. (if (not (org-at-date-range-p t))
  12919. (error "Not at a time-stamp range, and none found in current line")))
  12920. (let* ((ts1 (match-string 1))
  12921. (ts2 (match-string 2))
  12922. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12923. (match-end (match-end 0))
  12924. (time1 (org-time-string-to-time ts1))
  12925. (time2 (org-time-string-to-time ts2))
  12926. (t1 (org-float-time time1))
  12927. (t2 (org-float-time time2))
  12928. (diff (abs (- t2 t1)))
  12929. (negative (< (- t2 t1) 0))
  12930. ;; (ys (floor (* 365 24 60 60)))
  12931. (ds (* 24 60 60))
  12932. (hs (* 60 60))
  12933. (fy "%dy %dd %02d:%02d")
  12934. (fy1 "%dy %dd")
  12935. (fd "%dd %02d:%02d")
  12936. (fd1 "%dd")
  12937. (fh "%02d:%02d")
  12938. y d h m align)
  12939. (if havetime
  12940. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12941. y 0
  12942. d (floor (/ diff ds)) diff (mod diff ds)
  12943. h (floor (/ diff hs)) diff (mod diff hs)
  12944. m (floor (/ diff 60)))
  12945. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12946. y 0
  12947. d (floor (+ (/ diff ds) 0.5))
  12948. h 0 m 0))
  12949. (if (not to-buffer)
  12950. (message "%s" (org-make-tdiff-string y d h m))
  12951. (if (org-at-table-p)
  12952. (progn
  12953. (goto-char match-end)
  12954. (setq align t)
  12955. (and (looking-at " *|") (goto-char (match-end 0))))
  12956. (goto-char match-end))
  12957. (if (looking-at
  12958. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12959. (replace-match ""))
  12960. (if negative (insert " -"))
  12961. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12962. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12963. (insert " " (format fh h m))))
  12964. (if align (org-table-align))
  12965. (message "Time difference inserted")))))
  12966. (defun org-make-tdiff-string (y d h m)
  12967. (let ((fmt "")
  12968. (l nil))
  12969. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12970. l (push y l)))
  12971. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12972. l (push d l)))
  12973. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12974. l (push h l)))
  12975. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12976. l (push m l)))
  12977. (apply 'format fmt (nreverse l))))
  12978. (defun org-time-string-to-time (s)
  12979. (apply 'encode-time (org-parse-time-string s)))
  12980. (defun org-time-string-to-seconds (s)
  12981. (org-float-time (org-time-string-to-time s)))
  12982. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  12983. "Convert a time stamp to an absolute day number.
  12984. If there is a specifyer for a cyclic time stamp, get the closest date to
  12985. DAYNR.
  12986. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12987. the variable date is bound by the calendar when this is called."
  12988. (cond
  12989. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12990. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12991. daynr
  12992. (+ daynr 1000)))
  12993. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12994. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12995. (time-to-days (current-time))) (match-string 0 s)
  12996. prefer show-all))
  12997. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12998. (defun org-days-to-iso-week (days)
  12999. "Return the iso week number."
  13000. (require 'cal-iso)
  13001. (car (calendar-iso-from-absolute days)))
  13002. (defun org-small-year-to-year (year)
  13003. "Convert 2-digit years into 4-digit years.
  13004. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13005. The year 2000 cannot be abbreviated. Any year larger than 99
  13006. is returned unchanged."
  13007. (if (< year 38)
  13008. (setq year (+ 2000 year))
  13009. (if (< year 100)
  13010. (setq year (+ 1900 year))))
  13011. year)
  13012. (defun org-time-from-absolute (d)
  13013. "Return the time corresponding to date D.
  13014. D may be an absolute day number, or a calendar-type list (month day year)."
  13015. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13016. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13017. (defun org-calendar-holiday ()
  13018. "List of holidays, for Diary display in Org-mode."
  13019. (require 'holidays)
  13020. (let ((hl (funcall
  13021. (if (fboundp 'calendar-check-holidays)
  13022. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13023. (if hl (mapconcat 'identity hl "; "))))
  13024. (defun org-diary-sexp-entry (sexp entry date)
  13025. "Process a SEXP diary ENTRY for DATE."
  13026. (require 'diary-lib)
  13027. (let ((result (if calendar-debug-sexp
  13028. (let ((stack-trace-on-error t))
  13029. (eval (car (read-from-string sexp))))
  13030. (condition-case nil
  13031. (eval (car (read-from-string sexp)))
  13032. (error
  13033. (beep)
  13034. (message "Bad sexp at line %d in %s: %s"
  13035. (org-current-line)
  13036. (buffer-file-name) sexp)
  13037. (sleep-for 2))))))
  13038. (cond ((stringp result) result)
  13039. ((and (consp result)
  13040. (stringp (cdr result))) (cdr result))
  13041. (result entry)
  13042. (t nil))))
  13043. (defun org-diary-to-ical-string (frombuf)
  13044. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13045. This uses the icalendar.el library."
  13046. (let* ((tmpdir (if (featurep 'xemacs)
  13047. (temp-directory)
  13048. temporary-file-directory))
  13049. (tmpfile (make-temp-name
  13050. (expand-file-name "orgics" tmpdir)))
  13051. buf rtn b e)
  13052. (with-current-buffer frombuf
  13053. (icalendar-export-region (point-min) (point-max) tmpfile)
  13054. (setq buf (find-buffer-visiting tmpfile))
  13055. (set-buffer buf)
  13056. (goto-char (point-min))
  13057. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13058. (setq b (match-beginning 0)))
  13059. (goto-char (point-max))
  13060. (if (re-search-backward "^END:VEVENT" nil t)
  13061. (setq e (match-end 0)))
  13062. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13063. (kill-buffer buf)
  13064. (delete-file tmpfile)
  13065. rtn))
  13066. (defun org-closest-date (start current change prefer show-all)
  13067. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13068. When PREFER is `past' return a date that is either CURRENT or past.
  13069. When PREFER is `future', return a date that is either CURRENT or future.
  13070. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13071. ;; Make the proper lists from the dates
  13072. (catch 'exit
  13073. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13074. dn dw sday cday n1 n2 n0
  13075. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13076. (setq start (org-date-to-gregorian start)
  13077. current (org-date-to-gregorian
  13078. (if show-all
  13079. current
  13080. (time-to-days (current-time))))
  13081. sday (calendar-absolute-from-gregorian start)
  13082. cday (calendar-absolute-from-gregorian current))
  13083. (if (<= cday sday) (throw 'exit sday))
  13084. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13085. (setq dn (string-to-number (match-string 1 change))
  13086. dw (cdr (assoc (match-string 2 change) a1)))
  13087. (error "Invalid change specifyer: %s" change))
  13088. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13089. (cond
  13090. ((eq dw 'day)
  13091. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13092. n2 (+ n1 dn)))
  13093. ((eq dw 'year)
  13094. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13095. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13096. (setq date1 (list m d y1)
  13097. n1 (calendar-absolute-from-gregorian date1)
  13098. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13099. n2 (calendar-absolute-from-gregorian date2)))
  13100. ((eq dw 'month)
  13101. ;; approx number of month between the two dates
  13102. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13103. ;; How often does dn fit in there?
  13104. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13105. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13106. m (+ m nm)
  13107. ny (floor (/ m 12))
  13108. y (+ y ny)
  13109. m (- m (* ny 12)))
  13110. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13111. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13112. (setq m2 (+ m dn) y2 y)
  13113. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13114. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13115. (while (<= n2 cday)
  13116. (setq n1 n2 m m2 y y2)
  13117. (setq m2 (+ m dn) y2 y)
  13118. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13119. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13120. ;; Make sure n1 is the earlier date
  13121. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13122. (if show-all
  13123. (cond
  13124. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13125. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13126. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13127. (cond
  13128. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13129. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13130. (t (if (= cday n1) n1 n2)))))))
  13131. (defun org-date-to-gregorian (date)
  13132. "Turn any specification of DATE into a gregorian date for the calendar."
  13133. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13134. ((and (listp date) (= (length date) 3)) date)
  13135. ((stringp date)
  13136. (setq date (org-parse-time-string date))
  13137. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13138. ((listp date)
  13139. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13140. (defun org-parse-time-string (s &optional nodefault)
  13141. "Parse the standard Org-mode time string.
  13142. This should be a lot faster than the normal `parse-time-string'.
  13143. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13144. hour and minute fields will be nil if not given."
  13145. (if (string-match org-ts-regexp0 s)
  13146. (list 0
  13147. (if (or (match-beginning 8) (not nodefault))
  13148. (string-to-number (or (match-string 8 s) "0")))
  13149. (if (or (match-beginning 7) (not nodefault))
  13150. (string-to-number (or (match-string 7 s) "0")))
  13151. (string-to-number (match-string 4 s))
  13152. (string-to-number (match-string 3 s))
  13153. (string-to-number (match-string 2 s))
  13154. nil nil nil)
  13155. (error "Not a standard Org-mode time string: %s" s)))
  13156. (defun org-timestamp-up (&optional arg)
  13157. "Increase the date item at the cursor by one.
  13158. If the cursor is on the year, change the year. If it is on the month or
  13159. the day, change that.
  13160. With prefix ARG, change by that many units."
  13161. (interactive "p")
  13162. (org-timestamp-change (prefix-numeric-value arg)))
  13163. (defun org-timestamp-down (&optional arg)
  13164. "Decrease the date item at the cursor by one.
  13165. If the cursor is on the year, change the year. If it is on the month or
  13166. the day, change that.
  13167. With prefix ARG, change by that many units."
  13168. (interactive "p")
  13169. (org-timestamp-change (- (prefix-numeric-value arg))))
  13170. (defun org-timestamp-up-day (&optional arg)
  13171. "Increase the date in the time stamp by one day.
  13172. With prefix ARG, change that many days."
  13173. (interactive "p")
  13174. (if (and (not (org-at-timestamp-p t))
  13175. (org-on-heading-p))
  13176. (org-todo 'up)
  13177. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  13178. (defun org-timestamp-down-day (&optional arg)
  13179. "Decrease the date in the time stamp by one day.
  13180. With prefix ARG, change that many days."
  13181. (interactive "p")
  13182. (if (and (not (org-at-timestamp-p t))
  13183. (org-on-heading-p))
  13184. (org-todo 'down)
  13185. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  13186. (defun org-at-timestamp-p (&optional inactive-ok)
  13187. "Determine if the cursor is in or at a timestamp."
  13188. (interactive)
  13189. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13190. (pos (point))
  13191. (ans (or (looking-at tsr)
  13192. (save-excursion
  13193. (skip-chars-backward "^[<\n\r\t")
  13194. (if (> (point) (point-min)) (backward-char 1))
  13195. (and (looking-at tsr)
  13196. (> (- (match-end 0) pos) -1))))))
  13197. (and ans
  13198. (boundp 'org-ts-what)
  13199. (setq org-ts-what
  13200. (cond
  13201. ((= pos (match-beginning 0)) 'bracket)
  13202. ((= pos (1- (match-end 0))) 'bracket)
  13203. ((org-pos-in-match-range pos 2) 'year)
  13204. ((org-pos-in-match-range pos 3) 'month)
  13205. ((org-pos-in-match-range pos 7) 'hour)
  13206. ((org-pos-in-match-range pos 8) 'minute)
  13207. ((or (org-pos-in-match-range pos 4)
  13208. (org-pos-in-match-range pos 5)) 'day)
  13209. ((and (> pos (or (match-end 8) (match-end 5)))
  13210. (< pos (match-end 0)))
  13211. (- pos (or (match-end 8) (match-end 5))))
  13212. (t 'day))))
  13213. ans))
  13214. (defun org-toggle-timestamp-type ()
  13215. "Toggle the type (<active> or [inactive]) of a time stamp."
  13216. (interactive)
  13217. (when (org-at-timestamp-p t)
  13218. (let ((beg (match-beginning 0)) (end (match-end 0))
  13219. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13220. (save-excursion
  13221. (goto-char beg)
  13222. (while (re-search-forward "[][<>]" end t)
  13223. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13224. t t)))
  13225. (message "Timestamp is now %sactive"
  13226. (if (equal (char-after beg) ?<) "" "in")))))
  13227. (defun org-timestamp-change (n &optional what)
  13228. "Change the date in the time stamp at point.
  13229. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13230. `year', `minute', `second'. If WHAT is not given, the cursor position
  13231. in the timestamp determines what will be changed."
  13232. (let ((pos (point))
  13233. with-hm inactive
  13234. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13235. org-ts-what
  13236. extra rem
  13237. ts time time0)
  13238. (if (not (org-at-timestamp-p t))
  13239. (error "Not at a timestamp"))
  13240. (if (and (not what) (eq org-ts-what 'bracket))
  13241. (org-toggle-timestamp-type)
  13242. (if (and (not what) (not (eq org-ts-what 'day))
  13243. org-display-custom-times
  13244. (get-text-property (point) 'display)
  13245. (not (get-text-property (1- (point)) 'display)))
  13246. (setq org-ts-what 'day))
  13247. (setq org-ts-what (or what org-ts-what)
  13248. inactive (= (char-after (match-beginning 0)) ?\[)
  13249. ts (match-string 0))
  13250. (replace-match "")
  13251. (if (string-match
  13252. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13253. ts)
  13254. (setq extra (match-string 1 ts)))
  13255. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13256. (setq with-hm t))
  13257. (setq time0 (org-parse-time-string ts))
  13258. (when (and (eq org-ts-what 'minute)
  13259. (eq current-prefix-arg nil))
  13260. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13261. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13262. (setcar (cdr time0) (+ (nth 1 time0)
  13263. (if (> n 0) (- rem) (- dm rem))))))
  13264. (setq time
  13265. (encode-time (or (car time0) 0)
  13266. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13267. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13268. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13269. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13270. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13271. (nthcdr 6 time0)))
  13272. (when (and (member org-ts-what '(hour minute))
  13273. extra
  13274. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13275. (setq extra (org-modify-ts-extra
  13276. extra
  13277. (if (eq org-ts-what 'hour) 2 5)
  13278. n dm)))
  13279. (when (integerp org-ts-what)
  13280. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13281. (if (eq what 'calendar)
  13282. (let ((cal-date (org-get-date-from-calendar)))
  13283. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13284. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13285. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13286. (setcar time0 (or (car time0) 0))
  13287. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13288. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13289. (setq time (apply 'encode-time time0))))
  13290. (setq org-last-changed-timestamp
  13291. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13292. (org-clock-update-time-maybe)
  13293. (goto-char pos)
  13294. ;; Try to recenter the calendar window, if any
  13295. (if (and org-calendar-follow-timestamp-change
  13296. (get-buffer-window "*Calendar*" t)
  13297. (memq org-ts-what '(day month year)))
  13298. (org-recenter-calendar (time-to-days time))))))
  13299. (defun org-modify-ts-extra (s pos n dm)
  13300. "Change the different parts of the lead-time and repeat fields in timestamp."
  13301. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13302. ng h m new rem)
  13303. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13304. (cond
  13305. ((or (org-pos-in-match-range pos 2)
  13306. (org-pos-in-match-range pos 3))
  13307. (setq m (string-to-number (match-string 3 s))
  13308. h (string-to-number (match-string 2 s)))
  13309. (if (org-pos-in-match-range pos 2)
  13310. (setq h (+ h n))
  13311. (setq n (* dm (org-no-warnings (signum n))))
  13312. (when (not (= 0 (setq rem (% m dm))))
  13313. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13314. (setq m (+ m n)))
  13315. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13316. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13317. (setq h (min 24 (max 0 h)))
  13318. (setq ng 1 new (format "-%02d:%02d" h m)))
  13319. ((org-pos-in-match-range pos 6)
  13320. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13321. ((org-pos-in-match-range pos 5)
  13322. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13323. ((org-pos-in-match-range pos 9)
  13324. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13325. ((org-pos-in-match-range pos 8)
  13326. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13327. (when ng
  13328. (setq s (concat
  13329. (substring s 0 (match-beginning ng))
  13330. new
  13331. (substring s (match-end ng))))))
  13332. s))
  13333. (defun org-recenter-calendar (date)
  13334. "If the calendar is visible, recenter it to DATE."
  13335. (let* ((win (selected-window))
  13336. (cwin (get-buffer-window "*Calendar*" t))
  13337. (calendar-move-hook nil))
  13338. (when cwin
  13339. (select-window cwin)
  13340. (calendar-goto-date (if (listp date) date
  13341. (calendar-gregorian-from-absolute date)))
  13342. (select-window win))))
  13343. (defun org-goto-calendar (&optional arg)
  13344. "Go to the Emacs calendar at the current date.
  13345. If there is a time stamp in the current line, go to that date.
  13346. A prefix ARG can be used to force the current date."
  13347. (interactive "P")
  13348. (let ((tsr org-ts-regexp) diff
  13349. (calendar-move-hook nil)
  13350. (calendar-view-holidays-initially-flag nil)
  13351. (view-calendar-holidays-initially nil)
  13352. (calendar-view-diary-initially-flag nil)
  13353. (view-diary-entries-initially nil))
  13354. (if (or (org-at-timestamp-p)
  13355. (save-excursion
  13356. (beginning-of-line 1)
  13357. (looking-at (concat ".*" tsr))))
  13358. (let ((d1 (time-to-days (current-time)))
  13359. (d2 (time-to-days
  13360. (org-time-string-to-time (match-string 1)))))
  13361. (setq diff (- d2 d1))))
  13362. (calendar)
  13363. (calendar-goto-today)
  13364. (if (and diff (not arg)) (calendar-forward-day diff))))
  13365. (defun org-get-date-from-calendar ()
  13366. "Return a list (month day year) of date at point in calendar."
  13367. (with-current-buffer "*Calendar*"
  13368. (save-match-data
  13369. (calendar-cursor-to-date))))
  13370. (defun org-date-from-calendar ()
  13371. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13372. If there is already a time stamp at the cursor position, update it."
  13373. (interactive)
  13374. (if (org-at-timestamp-p t)
  13375. (org-timestamp-change 0 'calendar)
  13376. (let ((cal-date (org-get-date-from-calendar)))
  13377. (org-insert-time-stamp
  13378. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  13379. (defun org-minutes-to-hh:mm-string (m)
  13380. "Compute H:MM from a number of minutes."
  13381. (let ((h (/ m 60)))
  13382. (setq m (- m (* 60 h)))
  13383. (format org-time-clocksum-format h m)))
  13384. (defun org-hh:mm-string-to-minutes (s)
  13385. "Convert a string H:MM to a number of minutes.
  13386. If the string is just a number, interpret it as minutes.
  13387. In fact, the first hh:mm or number in the string will be taken,
  13388. there can be extra stuff in the string.
  13389. If no number is found, the return value is 0."
  13390. (cond
  13391. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  13392. (+ (* (string-to-number (match-string 1 s)) 60)
  13393. (string-to-number (match-string 2 s))))
  13394. ((string-match "\\([0-9]+\\)" s)
  13395. (string-to-number (match-string 1 s)))
  13396. (t 0)))
  13397. ;;;; Files
  13398. (defun org-save-all-org-buffers ()
  13399. "Save all Org-mode buffers without user confirmation."
  13400. (interactive)
  13401. (message "Saving all Org-mode buffers...")
  13402. (save-some-buffers t 'org-mode-p)
  13403. (when (featurep 'org-id) (org-id-locations-save))
  13404. (message "Saving all Org-mode buffers... done"))
  13405. (defun org-revert-all-org-buffers ()
  13406. "Revert all Org-mode buffers.
  13407. Prompt for confirmation when there are unsaved changes.
  13408. Be sure you know what you are doing before letting this function
  13409. overwrite your changes.
  13410. This function is useful in a setup where one tracks org files
  13411. with a version control system, to revert on one machine after pulling
  13412. changes from another. I believe the procedure must be like this:
  13413. 1. M-x org-save-all-org-buffers
  13414. 2. Pull changes from the other machine, resolve conflicts
  13415. 3. M-x org-revert-all-org-buffers"
  13416. (interactive)
  13417. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  13418. (error "Abort"))
  13419. (save-excursion
  13420. (save-window-excursion
  13421. (mapc
  13422. (lambda (b)
  13423. (when (and (with-current-buffer b (org-mode-p))
  13424. (with-current-buffer b buffer-file-name))
  13425. (switch-to-buffer b)
  13426. (revert-buffer t 'no-confirm)))
  13427. (buffer-list))
  13428. (when (and (featurep 'org-id) org-id-track-globally)
  13429. (org-id-locations-load)))))
  13430. ;;;; Agenda files
  13431. ;;;###autoload
  13432. (defun org-iswitchb (&optional arg)
  13433. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  13434. With a prefix argument, restrict available to files.
  13435. With two prefix arguments, restrict available buffers to agenda files."
  13436. (interactive "P")
  13437. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  13438. ((equal arg '(16)) (org-buffer-list 'agenda))
  13439. (t (org-buffer-list)))))
  13440. (switch-to-buffer
  13441. (org-icompleting-read "Org buffer: "
  13442. (mapcar 'list (mapcar 'buffer-name blist))
  13443. nil t))))
  13444. ;;;###autoload
  13445. (defalias 'org-ido-switchb 'org-iswitchb)
  13446. (defun org-buffer-list (&optional predicate exclude-tmp)
  13447. "Return a list of Org buffers.
  13448. PREDICATE can be `export', `files' or `agenda'.
  13449. export restrict the list to Export buffers.
  13450. files restrict the list to buffers visiting Org files.
  13451. agenda restrict the list to buffers visiting agenda files.
  13452. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  13453. (let* ((bfn nil)
  13454. (agenda-files (and (eq predicate 'agenda)
  13455. (mapcar 'file-truename (org-agenda-files t))))
  13456. (filter
  13457. (cond
  13458. ((eq predicate 'files)
  13459. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  13460. ((eq predicate 'export)
  13461. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  13462. ((eq predicate 'agenda)
  13463. (lambda (b)
  13464. (with-current-buffer b
  13465. (and (eq major-mode 'org-mode)
  13466. (setq bfn (buffer-file-name b))
  13467. (member (file-truename bfn) agenda-files)))))
  13468. (t (lambda (b) (with-current-buffer b
  13469. (or (eq major-mode 'org-mode)
  13470. (string-match "\*Org .*Export"
  13471. (buffer-name b)))))))))
  13472. (delq nil
  13473. (mapcar
  13474. (lambda(b)
  13475. (if (and (funcall filter b)
  13476. (or (not exclude-tmp)
  13477. (not (string-match "tmp" (buffer-name b)))))
  13478. b
  13479. nil))
  13480. (buffer-list)))))
  13481. (defun org-agenda-files (&optional unrestricted archives)
  13482. "Get the list of agenda files.
  13483. Optional UNRESTRICTED means return the full list even if a restriction
  13484. is currently in place.
  13485. When ARCHIVES is t, include all archive files that are really being
  13486. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  13487. `org-agenda-archives-mode' is t."
  13488. (let ((files
  13489. (cond
  13490. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  13491. ((stringp org-agenda-files) (org-read-agenda-file-list))
  13492. ((listp org-agenda-files) org-agenda-files)
  13493. (t (error "Invalid value of `org-agenda-files'")))))
  13494. (setq files (apply 'append
  13495. (mapcar (lambda (f)
  13496. (if (file-directory-p f)
  13497. (directory-files
  13498. f t org-agenda-file-regexp)
  13499. (list f)))
  13500. files)))
  13501. (when org-agenda-skip-unavailable-files
  13502. (setq files (delq nil
  13503. (mapcar (function
  13504. (lambda (file)
  13505. (and (file-readable-p file) file)))
  13506. files))))
  13507. (when (or (eq archives t)
  13508. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  13509. (setq files (org-add-archive-files files)))
  13510. files))
  13511. (defun org-edit-agenda-file-list ()
  13512. "Edit the list of agenda files.
  13513. Depending on setup, this either uses customize to edit the variable
  13514. `org-agenda-files', or it visits the file that is holding the list. In the
  13515. latter case, the buffer is set up in a way that saving it automatically kills
  13516. the buffer and restores the previous window configuration."
  13517. (interactive)
  13518. (if (stringp org-agenda-files)
  13519. (let ((cw (current-window-configuration)))
  13520. (find-file org-agenda-files)
  13521. (org-set-local 'org-window-configuration cw)
  13522. (org-add-hook 'after-save-hook
  13523. (lambda ()
  13524. (set-window-configuration
  13525. (prog1 org-window-configuration
  13526. (kill-buffer (current-buffer))))
  13527. (org-install-agenda-files-menu)
  13528. (message "New agenda file list installed"))
  13529. nil 'local)
  13530. (message "%s" (substitute-command-keys
  13531. "Edit list and finish with \\[save-buffer]")))
  13532. (customize-variable 'org-agenda-files)))
  13533. (defun org-store-new-agenda-file-list (list)
  13534. "Set new value for the agenda file list and save it correctly."
  13535. (if (stringp org-agenda-files)
  13536. (let ((fe (org-read-agenda-file-list t)) b u)
  13537. (while (setq b (find-buffer-visiting org-agenda-files))
  13538. (kill-buffer b))
  13539. (with-temp-file org-agenda-files
  13540. (insert
  13541. (mapconcat
  13542. (lambda (f) ;; Keep un-expanded entries.
  13543. (if (setq u (assoc f fe))
  13544. (cdr u)
  13545. f))
  13546. list "\n")
  13547. "\n")))
  13548. (let ((org-mode-hook nil) (org-inhibit-startup t)
  13549. (org-insert-mode-line-in-empty-file nil))
  13550. (setq org-agenda-files list)
  13551. (customize-save-variable 'org-agenda-files org-agenda-files))))
  13552. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  13553. "Read the list of agenda files from a file.
  13554. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  13555. filenames, used by `org-store-new-agenda-file-list' to write back
  13556. un-expanded file names."
  13557. (when (file-directory-p org-agenda-files)
  13558. (error "`org-agenda-files' cannot be a single directory"))
  13559. (when (stringp org-agenda-files)
  13560. (with-temp-buffer
  13561. (insert-file-contents org-agenda-files)
  13562. (mapcar
  13563. (lambda (f)
  13564. (let ((e (expand-file-name (substitute-in-file-name f)
  13565. org-directory)))
  13566. (if pair-with-expansion
  13567. (cons e f)
  13568. e)))
  13569. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  13570. ;;;###autoload
  13571. (defun org-cycle-agenda-files ()
  13572. "Cycle through the files in `org-agenda-files'.
  13573. If the current buffer visits an agenda file, find the next one in the list.
  13574. If the current buffer does not, find the first agenda file."
  13575. (interactive)
  13576. (let* ((fs (org-agenda-files t))
  13577. (files (append fs (list (car fs))))
  13578. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  13579. file)
  13580. (unless files (error "No agenda files"))
  13581. (catch 'exit
  13582. (while (setq file (pop files))
  13583. (if (equal (file-truename file) tcf)
  13584. (when (car files)
  13585. (find-file (car files))
  13586. (throw 'exit t))))
  13587. (find-file (car fs)))
  13588. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  13589. (defun org-agenda-file-to-front (&optional to-end)
  13590. "Move/add the current file to the top of the agenda file list.
  13591. If the file is not present in the list, it is added to the front. If it is
  13592. present, it is moved there. With optional argument TO-END, add/move to the
  13593. end of the list."
  13594. (interactive "P")
  13595. (let ((org-agenda-skip-unavailable-files nil)
  13596. (file-alist (mapcar (lambda (x)
  13597. (cons (file-truename x) x))
  13598. (org-agenda-files t)))
  13599. (ctf (file-truename buffer-file-name))
  13600. x had)
  13601. (setq x (assoc ctf file-alist) had x)
  13602. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  13603. (if to-end
  13604. (setq file-alist (append (delq x file-alist) (list x)))
  13605. (setq file-alist (cons x (delq x file-alist))))
  13606. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  13607. (org-install-agenda-files-menu)
  13608. (message "File %s to %s of agenda file list"
  13609. (if had "moved" "added") (if to-end "end" "front"))))
  13610. (defun org-remove-file (&optional file)
  13611. "Remove current file from the list of files in variable `org-agenda-files'.
  13612. These are the files which are being checked for agenda entries.
  13613. Optional argument FILE means use this file instead of the current."
  13614. (interactive)
  13615. (let* ((org-agenda-skip-unavailable-files nil)
  13616. (file (or file buffer-file-name))
  13617. (true-file (file-truename file))
  13618. (afile (abbreviate-file-name file))
  13619. (files (delq nil (mapcar
  13620. (lambda (x)
  13621. (if (equal true-file
  13622. (file-truename x))
  13623. nil x))
  13624. (org-agenda-files t)))))
  13625. (if (not (= (length files) (length (org-agenda-files t))))
  13626. (progn
  13627. (org-store-new-agenda-file-list files)
  13628. (org-install-agenda-files-menu)
  13629. (message "Removed file: %s" afile))
  13630. (message "File was not in list: %s (not removed)" afile))))
  13631. (defun org-file-menu-entry (file)
  13632. (vector file (list 'find-file file) t))
  13633. (defun org-check-agenda-file (file)
  13634. "Make sure FILE exists. If not, ask user what to do."
  13635. (when (not (file-exists-p file))
  13636. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13637. (abbreviate-file-name file))
  13638. (let ((r (downcase (read-char-exclusive))))
  13639. (cond
  13640. ((equal r ?r)
  13641. (org-remove-file file)
  13642. (throw 'nextfile t))
  13643. (t (error "Abort"))))))
  13644. (defun org-get-agenda-file-buffer (file)
  13645. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13646. it to the list of buffers which might be released later."
  13647. (let ((buf (org-find-base-buffer-visiting file)))
  13648. (if buf
  13649. buf ; just return it
  13650. ;; Make a new buffer and remember it
  13651. (setq buf (find-file-noselect file))
  13652. (if buf (push buf org-agenda-new-buffers))
  13653. buf)))
  13654. (defun org-release-buffers (blist)
  13655. "Release all buffers in list, asking the user for confirmation when needed.
  13656. When a buffer is unmodified, it is just killed. When modified, it is saved
  13657. \(if the user agrees) and then killed."
  13658. (let (buf file)
  13659. (while (setq buf (pop blist))
  13660. (setq file (buffer-file-name buf))
  13661. (when (and (buffer-modified-p buf)
  13662. file
  13663. (y-or-n-p (format "Save file %s? " file)))
  13664. (with-current-buffer buf (save-buffer)))
  13665. (kill-buffer buf))))
  13666. (defun org-prepare-agenda-buffers (files)
  13667. "Create buffers for all agenda files, protect archived trees and comments."
  13668. (interactive)
  13669. (let ((pa '(:org-archived t))
  13670. (pc '(:org-comment t))
  13671. (pall '(:org-archived t :org-comment t))
  13672. (inhibit-read-only t)
  13673. (rea (concat ":" org-archive-tag ":"))
  13674. bmp file re)
  13675. (save-excursion
  13676. (save-restriction
  13677. (while (setq file (pop files))
  13678. (catch 'nextfile
  13679. (if (bufferp file)
  13680. (set-buffer file)
  13681. (org-check-agenda-file file)
  13682. (set-buffer (org-get-agenda-file-buffer file)))
  13683. (widen)
  13684. (setq bmp (buffer-modified-p))
  13685. (org-refresh-category-properties)
  13686. (setq org-todo-keywords-for-agenda
  13687. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13688. (setq org-done-keywords-for-agenda
  13689. (append org-done-keywords-for-agenda org-done-keywords))
  13690. (setq org-todo-keyword-alist-for-agenda
  13691. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13692. (setq org-drawers-for-agenda
  13693. (append org-drawers-for-agenda org-drawers))
  13694. (setq org-tag-alist-for-agenda
  13695. (append org-tag-alist-for-agenda org-tag-alist))
  13696. (save-excursion
  13697. (remove-text-properties (point-min) (point-max) pall)
  13698. (when org-agenda-skip-archived-trees
  13699. (goto-char (point-min))
  13700. (while (re-search-forward rea nil t)
  13701. (if (org-on-heading-p t)
  13702. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13703. (goto-char (point-min))
  13704. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13705. (while (re-search-forward re nil t)
  13706. (add-text-properties
  13707. (match-beginning 0) (org-end-of-subtree t) pc)))
  13708. (set-buffer-modified-p bmp)))))
  13709. (setq org-todo-keyword-alist-for-agenda
  13710. (org-uniquify org-todo-keyword-alist-for-agenda)
  13711. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13712. ;;;; Embedded LaTeX
  13713. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13714. "Keymap for the minor `org-cdlatex-mode'.")
  13715. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13716. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13717. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13718. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13719. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13720. (defvar org-cdlatex-texmathp-advice-is-done nil
  13721. "Flag remembering if we have applied the advice to texmathp already.")
  13722. (define-minor-mode org-cdlatex-mode
  13723. "Toggle the minor `org-cdlatex-mode'.
  13724. This mode supports entering LaTeX environment and math in LaTeX fragments
  13725. in Org-mode.
  13726. \\{org-cdlatex-mode-map}"
  13727. nil " OCDL" nil
  13728. (when org-cdlatex-mode (require 'cdlatex))
  13729. (unless org-cdlatex-texmathp-advice-is-done
  13730. (setq org-cdlatex-texmathp-advice-is-done t)
  13731. (defadvice texmathp (around org-math-always-on activate)
  13732. "Always return t in org-mode buffers.
  13733. This is because we want to insert math symbols without dollars even outside
  13734. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13735. an embedded LaTeX fragment, let texmathp do its job.
  13736. \\[org-cdlatex-mode-map]"
  13737. (interactive)
  13738. (let (p)
  13739. (cond
  13740. ((not (org-mode-p)) ad-do-it)
  13741. ((eq this-command 'cdlatex-math-symbol)
  13742. (setq ad-return-value t
  13743. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13744. (t
  13745. (let ((p (org-inside-LaTeX-fragment-p)))
  13746. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13747. (setq ad-return-value t
  13748. texmathp-why '("Org-mode embedded math" . 0))
  13749. (if p ad-do-it)))))))))
  13750. (defun turn-on-org-cdlatex ()
  13751. "Unconditionally turn on `org-cdlatex-mode'."
  13752. (org-cdlatex-mode 1))
  13753. (defun org-inside-LaTeX-fragment-p ()
  13754. "Test if point is inside a LaTeX fragment.
  13755. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13756. sequence appearing also before point.
  13757. Even though the matchers for math are configurable, this function assumes
  13758. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13759. delimiters are skipped when they have been removed by customization.
  13760. The return value is nil, or a cons cell with the delimiter and
  13761. and the position of this delimiter.
  13762. This function does a reasonably good job, but can locally be fooled by
  13763. for example currency specifications. For example it will assume being in
  13764. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13765. fragments that are properly closed, but during editing, we have to live
  13766. with the uncertainty caused by missing closing delimiters. This function
  13767. looks only before point, not after."
  13768. (catch 'exit
  13769. (let ((pos (point))
  13770. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13771. (lim (progn
  13772. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13773. (point)))
  13774. dd-on str (start 0) m re)
  13775. (goto-char pos)
  13776. (when dodollar
  13777. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13778. re (nth 1 (assoc "$" org-latex-regexps)))
  13779. (while (string-match re str start)
  13780. (cond
  13781. ((= (match-end 0) (length str))
  13782. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  13783. ((= (match-end 0) (- (length str) 5))
  13784. (throw 'exit nil))
  13785. (t (setq start (match-end 0))))))
  13786. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13787. (goto-char pos)
  13788. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13789. (and (match-beginning 2) (throw 'exit nil))
  13790. ;; count $$
  13791. (while (re-search-backward "\\$\\$" lim t)
  13792. (setq dd-on (not dd-on)))
  13793. (goto-char pos)
  13794. (if dd-on (cons "$$" m))))))
  13795. (defun org-inside-latex-macro-p ()
  13796. "Is point inside a LaTeX macro or its arguments?"
  13797. (save-match-data
  13798. (org-in-regexp
  13799. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  13800. (defun test ()
  13801. (interactive)
  13802. (message "%s" (org-inside-latex-macro-p)))
  13803. (defun org-try-cdlatex-tab ()
  13804. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13805. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13806. - inside a LaTeX fragment, or
  13807. - after the first word in a line, where an abbreviation expansion could
  13808. insert a LaTeX environment."
  13809. (when org-cdlatex-mode
  13810. (cond
  13811. ((save-excursion
  13812. (skip-chars-backward "a-zA-Z0-9*")
  13813. (skip-chars-backward " \t")
  13814. (bolp))
  13815. (cdlatex-tab) t)
  13816. ((org-inside-LaTeX-fragment-p)
  13817. (cdlatex-tab) t)
  13818. (t nil))))
  13819. (defun org-cdlatex-underscore-caret (&optional arg)
  13820. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13821. Revert to the normal definition outside of these fragments."
  13822. (interactive "P")
  13823. (if (org-inside-LaTeX-fragment-p)
  13824. (call-interactively 'cdlatex-sub-superscript)
  13825. (let (org-cdlatex-mode)
  13826. (call-interactively (key-binding (vector last-input-event))))))
  13827. (defun org-cdlatex-math-modify (&optional arg)
  13828. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13829. Revert to the normal definition outside of these fragments."
  13830. (interactive "P")
  13831. (if (org-inside-LaTeX-fragment-p)
  13832. (call-interactively 'cdlatex-math-modify)
  13833. (let (org-cdlatex-mode)
  13834. (call-interactively (key-binding (vector last-input-event))))))
  13835. (defvar org-latex-fragment-image-overlays nil
  13836. "List of overlays carrying the images of latex fragments.")
  13837. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13838. (defun org-remove-latex-fragment-image-overlays ()
  13839. "Remove all overlays with LaTeX fragment images in current buffer."
  13840. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  13841. (setq org-latex-fragment-image-overlays nil))
  13842. (defun org-preview-latex-fragment (&optional subtree)
  13843. "Preview the LaTeX fragment at point, or all locally or globally.
  13844. If the cursor is in a LaTeX fragment, create the image and overlay
  13845. it over the source code. If there is no fragment at point, display
  13846. all fragments in the current text, from one headline to the next. With
  13847. prefix SUBTREE, display all fragments in the current subtree. With a
  13848. double prefix `C-u C-u', or when the cursor is before the first headline,
  13849. display all fragments in the buffer.
  13850. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13851. (interactive "P")
  13852. (org-remove-latex-fragment-image-overlays)
  13853. (save-excursion
  13854. (save-restriction
  13855. (let (beg end at msg)
  13856. (cond
  13857. ((or (equal subtree '(16))
  13858. (not (save-excursion
  13859. (re-search-backward (concat "^" outline-regexp) nil t))))
  13860. (setq beg (point-min) end (point-max)
  13861. msg "Creating images for buffer...%s"))
  13862. ((equal subtree '(4))
  13863. (org-back-to-heading)
  13864. (setq beg (point) end (org-end-of-subtree t)
  13865. msg "Creating images for subtree...%s"))
  13866. (t
  13867. (if (setq at (org-inside-LaTeX-fragment-p))
  13868. (goto-char (max (point-min) (- (cdr at) 2)))
  13869. (org-back-to-heading))
  13870. (setq beg (point) end (progn (outline-next-heading) (point))
  13871. msg (if at "Creating image...%s"
  13872. "Creating images for entry...%s"))))
  13873. (message msg "")
  13874. (narrow-to-region beg end)
  13875. (goto-char beg)
  13876. (org-format-latex
  13877. (concat "ltxpng/" (file-name-sans-extension
  13878. (file-name-nondirectory
  13879. buffer-file-name)))
  13880. default-directory 'overlays msg at 'forbuffer)
  13881. (message msg "done. Use `C-c C-c' to remove images.")))))
  13882. (defvar org-latex-regexps
  13883. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13884. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13885. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13886. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13887. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13888. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13889. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13890. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13891. "Regular expressions for matching embedded LaTeX.")
  13892. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13893. "Replace LaTeX fragments with links to an image, and produce images.
  13894. Some of the options can be changed using the variable
  13895. `org-format-latex-options'."
  13896. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13897. (let* ((prefixnodir (file-name-nondirectory prefix))
  13898. (absprefix (expand-file-name prefix dir))
  13899. (todir (file-name-directory absprefix))
  13900. (opt org-format-latex-options)
  13901. (matchers (plist-get opt :matchers))
  13902. (re-list org-latex-regexps)
  13903. (org-format-latex-header-extra
  13904. (plist-get (org-infile-export-plist) :latex-header-extra))
  13905. (cnt 0) txt hash link beg end re e checkdir
  13906. executables-checked
  13907. m n block linkfile movefile ov)
  13908. ;; Check the different regular expressions
  13909. (while (setq e (pop re-list))
  13910. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13911. block (if (nth 3 e) "\n\n" ""))
  13912. (when (member m matchers)
  13913. (goto-char (point-min))
  13914. (while (re-search-forward re nil t)
  13915. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13916. (not (get-text-property (match-beginning n)
  13917. 'org-protected))
  13918. (or (not overlays)
  13919. (not (eq (get-char-property (match-beginning n)
  13920. 'org-overlay-type)
  13921. 'org-latex-overlay))))
  13922. (setq txt (match-string n)
  13923. beg (match-beginning n) end (match-end n)
  13924. cnt (1+ cnt))
  13925. (let (print-length print-level) ; make sure full list is printed
  13926. (setq hash (sha1 (prin1-to-string
  13927. (list org-format-latex-header
  13928. org-format-latex-header-extra
  13929. org-export-latex-default-packages-alist
  13930. org-export-latex-packages-alist
  13931. org-format-latex-options
  13932. forbuffer txt)))
  13933. linkfile (format "%s_%s.png" prefix hash)
  13934. movefile (format "%s_%s.png" absprefix hash)))
  13935. (setq link (concat block "[[file:" linkfile "]]" block))
  13936. (if msg (message msg cnt))
  13937. (goto-char beg)
  13938. (unless checkdir ; make sure the directory exists
  13939. (setq checkdir t)
  13940. (or (file-directory-p todir) (make-directory todir)))
  13941. (unless executables-checked
  13942. (org-check-external-command
  13943. "latex" "needed to convert LaTeX fragments to images")
  13944. (org-check-external-command
  13945. "dvipng" "needed to convert LaTeX fragments to images")
  13946. (setq executables-checked t))
  13947. (unless (file-exists-p movefile)
  13948. (org-create-formula-image
  13949. txt movefile opt forbuffer))
  13950. (if overlays
  13951. (progn
  13952. (mapc (lambda (o)
  13953. (if (eq (org-overlay-get o 'org-overlay-type)
  13954. 'org-latex-overlay)
  13955. (org-delete-overlay o)))
  13956. (org-overlays-in beg end))
  13957. (setq ov (org-make-overlay beg end))
  13958. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  13959. (if (featurep 'xemacs)
  13960. (progn
  13961. (org-overlay-put ov 'invisible t)
  13962. (org-overlay-put
  13963. ov 'end-glyph
  13964. (make-glyph (vector 'png :file movefile))))
  13965. (org-overlay-put
  13966. ov 'display
  13967. (list 'image :type 'png :file movefile :ascent 'center)))
  13968. (push ov org-latex-fragment-image-overlays)
  13969. (goto-char end))
  13970. (delete-region beg end)
  13971. (insert (org-add-props link
  13972. (list 'org-latex-src
  13973. (replace-regexp-in-string "\"" "" txt)))))))))))
  13974. ;; This function borrows from Ganesh Swami's latex2png.el
  13975. (defun org-create-formula-image (string tofile options buffer)
  13976. "This calls dvipng."
  13977. (require 'org-latex)
  13978. (let* ((tmpdir (if (featurep 'xemacs)
  13979. (temp-directory)
  13980. temporary-file-directory))
  13981. (texfilebase (make-temp-name
  13982. (expand-file-name "orgtex" tmpdir)))
  13983. (texfile (concat texfilebase ".tex"))
  13984. (dvifile (concat texfilebase ".dvi"))
  13985. (pngfile (concat texfilebase ".png"))
  13986. (fnh (if (featurep 'xemacs)
  13987. (font-height (get-face-font 'default))
  13988. (face-attribute 'default :height nil)))
  13989. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  13990. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  13991. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  13992. "Black"))
  13993. (bg (or (plist-get options (if buffer :background :html-background))
  13994. "Transparent")))
  13995. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  13996. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  13997. (with-temp-file texfile
  13998. (insert (org-splice-latex-header
  13999. org-format-latex-header
  14000. org-export-latex-default-packages-alist
  14001. org-export-latex-packages-alist
  14002. org-format-latex-header-extra))
  14003. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14004. (require 'org-latex)
  14005. (org-export-latex-fix-inputenc))
  14006. (let ((dir default-directory))
  14007. (condition-case nil
  14008. (progn
  14009. (cd tmpdir)
  14010. (call-process "latex" nil nil nil texfile))
  14011. (error nil))
  14012. (cd dir))
  14013. (if (not (file-exists-p dvifile))
  14014. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14015. (condition-case nil
  14016. (call-process "dvipng" nil nil nil
  14017. "-fg" fg "-bg" bg
  14018. "-D" dpi
  14019. ;;"-x" scale "-y" scale
  14020. "-T" "tight"
  14021. "-o" pngfile
  14022. dvifile)
  14023. (error nil))
  14024. (if (not (file-exists-p pngfile))
  14025. (if org-format-latex-signal-error
  14026. (error "Failed to create png file from %s" texfile)
  14027. (message "Failed to create png file from %s" texfile)
  14028. nil)
  14029. ;; Use the requested file name and clean up
  14030. (copy-file pngfile tofile 'replace)
  14031. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14032. (delete-file (concat texfilebase e)))
  14033. pngfile))))
  14034. (defun org-splice-latex-header (tpl def-pkg pkg &optional extra)
  14035. "Fill a LaTeX header template TPL.
  14036. In the template, the following place holders will be recognized:
  14037. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14038. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14039. [PACKAGES] \\usepackage statements for PKG
  14040. [NO-PACKAGES] do not include PKG
  14041. [EXTRA] the string EXTRA
  14042. [NO-EXTRA] do not include EXTRA
  14043. For backward compatibility, if both the positive and the negative place
  14044. holder is missing, the positive one (without the \"NO-\") will be
  14045. assumed to be present at the end of the template.
  14046. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14047. EXTRA is a string."
  14048. (let (rpl (end ""))
  14049. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14050. (setq rpl (if (or (match-end 1) (not def-pkg))
  14051. "" (org-latex-packages-to-string def-pkg t))
  14052. tpl (replace-match rpl t t tpl))
  14053. (if def-pkg (setq end (org-latex-packages-to-string def-pkg))))
  14054. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14055. (setq rpl (if (or (match-end 1) (not pkg))
  14056. "" (org-latex-packages-to-string pkg t))
  14057. tpl (replace-match rpl t t tpl))
  14058. (if pkg (setq end (concat end "\n" (org-latex-packages-to-string pkg)))))
  14059. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14060. (setq rpl (if (or (match-end 1) (not extra))
  14061. "" (concat extra "\n"))
  14062. tpl (replace-match rpl t t tpl))
  14063. (if (and extra (string-match "\\S-" extra))
  14064. (setq end (concat end "\n" extra))))
  14065. (if (string-match "\\S-" end)
  14066. (concat tpl "\n" end)
  14067. tpl)))
  14068. (defun org-latex-packages-to-string (pkg &optional newline)
  14069. "Turn an alist of packages into a string with the \\usepackage macros."
  14070. (setq pkg (mapconcat (lambda(p)
  14071. (cond
  14072. ((stringp p) p)
  14073. ((equal "" (car p))
  14074. (format "\\usepackage{%s}" (cadr p)))
  14075. (t
  14076. (format "\\usepackage[%s]{%s}"
  14077. (car p) (cadr p)))))
  14078. pkg
  14079. "\n"))
  14080. (if newline (concat pkg "\n") pkg))
  14081. (defun org-dvipng-color (attr)
  14082. "Return an rgb color specification for dvipng."
  14083. (apply 'format "rgb %s %s %s"
  14084. (mapcar 'org-normalize-color
  14085. (color-values (face-attribute 'default attr nil)))))
  14086. (defun org-normalize-color (value)
  14087. "Return string to be used as color value for an RGB component."
  14088. (format "%g" (/ value 65535.0)))
  14089. ;;;; Key bindings
  14090. ;; Make `C-c C-x' a prefix key
  14091. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14092. ;; TAB key with modifiers
  14093. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14094. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14095. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14096. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  14097. (org-defkey org-mode-map "\M-\t" 'org-complete)
  14098. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  14099. ;; The following line is necessary under Suse GNU/Linux
  14100. (unless (featurep 'xemacs)
  14101. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14102. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14103. (define-key org-mode-map [backtab] 'org-shifttab)
  14104. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14105. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14106. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14107. ;; Cursor keys with modifiers
  14108. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14109. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14110. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14111. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14112. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14113. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14114. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14115. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14116. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14117. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14118. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14119. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14120. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14121. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14122. ;;; Extra keys for tty access.
  14123. ;; We only set them when really needed because otherwise the
  14124. ;; menus don't show the simple keys
  14125. (when (or org-use-extra-keys
  14126. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14127. (not window-system))
  14128. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14129. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14130. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14131. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14132. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14133. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14134. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14135. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14136. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14137. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14138. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14139. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14140. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14141. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14142. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14143. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14144. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14145. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14146. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14147. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14148. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14149. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14150. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14151. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14152. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14153. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14154. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14155. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14156. ;; All the other keys
  14157. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14158. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14159. (if (boundp 'narrow-map)
  14160. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14161. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14162. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14163. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14164. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14165. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14166. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14167. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14168. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14169. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14170. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14171. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14172. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14173. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14174. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14175. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14176. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  14177. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14178. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14179. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14180. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14181. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14182. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14183. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14184. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14185. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14186. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14187. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14188. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14189. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14190. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14191. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14192. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14193. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14194. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14195. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14196. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14197. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14198. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14199. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14200. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14201. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14202. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14203. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14204. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14205. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14206. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14207. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14208. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14209. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  14210. (org-defkey org-mode-map "\C-m" 'org-return)
  14211. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  14212. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  14213. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  14214. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  14215. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  14216. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  14217. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  14218. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14219. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14220. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14221. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  14222. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  14223. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  14224. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  14225. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14226. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  14227. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  14228. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  14229. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  14230. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  14231. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  14232. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  14233. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14234. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14235. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14236. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  14237. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14238. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14239. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  14240. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14241. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14242. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14243. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14244. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14245. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14246. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  14247. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  14248. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  14249. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  14250. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  14251. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  14252. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  14253. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  14254. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  14255. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  14256. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  14257. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  14258. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  14259. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  14260. (when (featurep 'xemacs)
  14261. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  14262. (defconst org-speed-commands-default
  14263. '(
  14264. ("Outline Navigation")
  14265. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  14266. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  14267. ("f" . (org-speed-move-safe 'org-forward-same-level))
  14268. ("b" . (org-speed-move-safe 'org-backward-same-level))
  14269. ("u" . (org-speed-move-safe 'outline-up-heading))
  14270. ("j" . org-goto)
  14271. ("g" . (org-refile t))
  14272. ("Outline Visibility")
  14273. ("c" . org-cycle)
  14274. ("C" . org-shifttab)
  14275. (" " . org-display-outline-path)
  14276. ("Outline Structure Editing")
  14277. ("U" . org-shiftmetaup)
  14278. ("D" . org-shiftmetadown)
  14279. ("r" . org-metaright)
  14280. ("l" . org-metaleft)
  14281. ("R" . org-shiftmetaright)
  14282. ("L" . org-shiftmetaleft)
  14283. ("i" . (progn (forward-char 1) (call-interactively
  14284. 'org-insert-heading-respect-content)))
  14285. ("^" . org-sort)
  14286. ("w" . org-refile)
  14287. ("a" . org-archive-subtree-default-with-confirmation)
  14288. ("." . outline-mark-subtree)
  14289. ("Clock Commands")
  14290. ("I" . org-clock-in)
  14291. ("O" . org-clock-out)
  14292. ("Meta Data Editing")
  14293. ("t" . org-todo)
  14294. ("0" . (org-priority ?\ ))
  14295. ("1" . (org-priority ?A))
  14296. ("2" . (org-priority ?B))
  14297. ("3" . (org-priority ?C))
  14298. (";" . org-set-tags-command)
  14299. ("e" . org-set-effort)
  14300. ("Agenda Views etc")
  14301. ("v" . org-agenda)
  14302. ("/" . org-sparse-tree)
  14303. ("Misc")
  14304. ("o" . org-open-at-point)
  14305. ("?" . org-speed-command-help)
  14306. )
  14307. "The default speed commands.")
  14308. (defun org-print-speed-command (e)
  14309. (if (> (length (car e)) 1)
  14310. (progn
  14311. (princ "\n")
  14312. (princ (car e))
  14313. (princ "\n")
  14314. (princ (make-string (length (car e)) ?-))
  14315. (princ "\n"))
  14316. (princ (car e))
  14317. (princ " ")
  14318. (if (symbolp (cdr e))
  14319. (princ (symbol-name (cdr e)))
  14320. (prin1 (cdr e)))
  14321. (princ "\n")))
  14322. (defun org-speed-command-help ()
  14323. "Show the available speed commands."
  14324. (interactive)
  14325. (if (not org-use-speed-commands)
  14326. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  14327. (with-output-to-temp-buffer "*Help*"
  14328. (princ "User-defined Speed commands\n===========================\n")
  14329. (mapc 'org-print-speed-command org-speed-commands-user)
  14330. (princ "\n")
  14331. (princ "Built-in Speed commands\n=======================\n")
  14332. (mapc 'org-print-speed-command org-speed-commands-default))
  14333. (with-current-buffer "*Help*"
  14334. (setq truncate-lines t))))
  14335. (defun org-speed-move-safe (cmd)
  14336. "Execute CMD, but make sure that the cursor always ends up in a headline.
  14337. If not, return to the original position and throw an error."
  14338. (interactive)
  14339. (let ((pos (point)))
  14340. (call-interactively cmd)
  14341. (unless (and (bolp) (org-on-heading-p))
  14342. (goto-char pos)
  14343. (error "Boundary reached while executing %s" cmd))))
  14344. (defvar org-self-insert-command-undo-counter 0)
  14345. (defvar org-table-auto-blank-field) ; defined in org-table.el
  14346. (defvar org-speed-command nil)
  14347. (defun org-self-insert-command (N)
  14348. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14349. If the cursor is in a table looking at whitespace, the whitespace is
  14350. overwritten, and the table is not marked as requiring realignment."
  14351. (interactive "p")
  14352. (cond
  14353. ((and org-use-speed-commands
  14354. (or (and (bolp) (looking-at outline-regexp))
  14355. (and (functionp org-use-speed-commands)
  14356. (funcall org-use-speed-commands)))
  14357. (setq
  14358. org-speed-command
  14359. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  14360. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  14361. (cond
  14362. ((commandp org-speed-command)
  14363. (setq this-command org-speed-command)
  14364. (call-interactively org-speed-command))
  14365. ((functionp org-speed-command)
  14366. (funcall org-speed-command))
  14367. ((and org-speed-command (listp org-speed-command))
  14368. (eval org-speed-command))
  14369. (t (let (org-use-speed-commands)
  14370. (call-interactively 'org-self-insert-command)))))
  14371. ((and
  14372. (org-table-p)
  14373. (progn
  14374. ;; check if we blank the field, and if that triggers align
  14375. (and (featurep 'org-table) org-table-auto-blank-field
  14376. (member last-command
  14377. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  14378. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14379. ;; got extra space, this field does not determine column width
  14380. (let (org-table-may-need-update) (org-table-blank-field))
  14381. ;; no extra space, this field may determine column width
  14382. (org-table-blank-field)))
  14383. t)
  14384. (eq N 1)
  14385. (looking-at "[^|\n]* |"))
  14386. (let (org-table-may-need-update)
  14387. (goto-char (1- (match-end 0)))
  14388. (delete-backward-char 1)
  14389. (goto-char (match-beginning 0))
  14390. (self-insert-command N)))
  14391. (t
  14392. (setq org-table-may-need-update t)
  14393. (self-insert-command N)
  14394. (org-fix-tags-on-the-fly)
  14395. (if org-self-insert-cluster-for-undo
  14396. (if (not (eq last-command 'org-self-insert-command))
  14397. (setq org-self-insert-command-undo-counter 1)
  14398. (if (>= org-self-insert-command-undo-counter 20)
  14399. (setq org-self-insert-command-undo-counter 1)
  14400. (and (> org-self-insert-command-undo-counter 0)
  14401. buffer-undo-list
  14402. (not (cadr buffer-undo-list)) ; remove nil entry
  14403. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  14404. (setq org-self-insert-command-undo-counter
  14405. (1+ org-self-insert-command-undo-counter))))))))
  14406. (defun org-fix-tags-on-the-fly ()
  14407. (when (and (equal (char-after (point-at-bol)) ?*)
  14408. (org-on-heading-p))
  14409. (org-align-tags-here org-tags-column)))
  14410. (defun org-delete-backward-char (N)
  14411. "Like `delete-backward-char', insert whitespace at field end in tables.
  14412. When deleting backwards, in tables this function will insert whitespace in
  14413. front of the next \"|\" separator, to keep the table aligned. The table will
  14414. still be marked for re-alignment if the field did fill the entire column,
  14415. because, in this case the deletion might narrow the column."
  14416. (interactive "p")
  14417. (if (and (org-table-p)
  14418. (eq N 1)
  14419. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14420. (looking-at ".*?|"))
  14421. (let ((pos (point))
  14422. (noalign (looking-at "[^|\n\r]* |"))
  14423. (c org-table-may-need-update))
  14424. (backward-delete-char N)
  14425. (skip-chars-forward "^|")
  14426. (insert " ")
  14427. (goto-char (1- pos))
  14428. ;; noalign: if there were two spaces at the end, this field
  14429. ;; does not determine the width of the column.
  14430. (if noalign (setq org-table-may-need-update c)))
  14431. (backward-delete-char N)
  14432. (org-fix-tags-on-the-fly)))
  14433. (defun org-delete-char (N)
  14434. "Like `delete-char', but insert whitespace at field end in tables.
  14435. When deleting characters, in tables this function will insert whitespace in
  14436. front of the next \"|\" separator, to keep the table aligned. The table will
  14437. still be marked for re-alignment if the field did fill the entire column,
  14438. because, in this case the deletion might narrow the column."
  14439. (interactive "p")
  14440. (if (and (org-table-p)
  14441. (not (bolp))
  14442. (not (= (char-after) ?|))
  14443. (eq N 1))
  14444. (if (looking-at ".*?|")
  14445. (let ((pos (point))
  14446. (noalign (looking-at "[^|\n\r]* |"))
  14447. (c org-table-may-need-update))
  14448. (replace-match (concat
  14449. (substring (match-string 0) 1 -1)
  14450. " |"))
  14451. (goto-char pos)
  14452. ;; noalign: if there were two spaces at the end, this field
  14453. ;; does not determine the width of the column.
  14454. (if noalign (setq org-table-may-need-update c)))
  14455. (delete-char N))
  14456. (delete-char N)
  14457. (org-fix-tags-on-the-fly)))
  14458. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  14459. (put 'org-self-insert-command 'delete-selection t)
  14460. (put 'orgtbl-self-insert-command 'delete-selection t)
  14461. (put 'org-delete-char 'delete-selection 'supersede)
  14462. (put 'org-delete-backward-char 'delete-selection 'supersede)
  14463. (put 'org-yank 'delete-selection 'yank)
  14464. ;; Make `flyspell-mode' delay after some commands
  14465. (put 'org-self-insert-command 'flyspell-delayed t)
  14466. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  14467. (put 'org-delete-char 'flyspell-delayed t)
  14468. (put 'org-delete-backward-char 'flyspell-delayed t)
  14469. ;; Make pabbrev-mode expand after org-mode commands
  14470. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  14471. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  14472. ;; How to do this: Measure non-white length of current string
  14473. ;; If equal to column width, we should realign.
  14474. (defun org-remap (map &rest commands)
  14475. "In MAP, remap the functions given in COMMANDS.
  14476. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14477. (let (new old)
  14478. (while commands
  14479. (setq old (pop commands) new (pop commands))
  14480. (if (fboundp 'command-remapping)
  14481. (org-defkey map (vector 'remap old) new)
  14482. (substitute-key-definition old new map global-map)))))
  14483. (when (eq org-enable-table-editor 'optimized)
  14484. ;; If the user wants maximum table support, we need to hijack
  14485. ;; some standard editing functions
  14486. (org-remap org-mode-map
  14487. 'self-insert-command 'org-self-insert-command
  14488. 'delete-char 'org-delete-char
  14489. 'delete-backward-char 'org-delete-backward-char)
  14490. (org-defkey org-mode-map "|" 'org-force-self-insert))
  14491. (defvar org-ctrl-c-ctrl-c-hook nil
  14492. "Hook for functions attaching themselves to `C-c C-c'.
  14493. This can be used to add additional functionality to the C-c C-c key which
  14494. executes context-dependent commands.
  14495. Each function will be called with no arguments. The function must check
  14496. if the context is appropriate for it to act. If yes, it should do its
  14497. thing and then return a non-nil value. If the context is wrong,
  14498. just do nothing and return nil.")
  14499. (defvar org-tab-first-hook nil
  14500. "Hook for functions to attach themselves to TAB.
  14501. See `org-ctrl-c-ctrl-c-hook' for more information.
  14502. This hook runs as the first action when TAB is pressed, even before
  14503. `org-cycle' messes around with the `outline-regexp' to cater for
  14504. inline tasks and plain list item folding.
  14505. If any function in this hook returns t, not other actions like table
  14506. field motion visibility cycling will be done.")
  14507. (defvar org-tab-after-check-for-table-hook nil
  14508. "Hook for functions to attach themselves to TAB.
  14509. See `org-ctrl-c-ctrl-c-hook' for more information.
  14510. This hook runs after it has been established that the cursor is not in a
  14511. table, but before checking if the cursor is in a headline or if global cycling
  14512. should be done.
  14513. If any function in this hook returns t, not other actions like visibility
  14514. cycling will be done.")
  14515. (defvar org-tab-after-check-for-cycling-hook nil
  14516. "Hook for functions to attach themselves to TAB.
  14517. See `org-ctrl-c-ctrl-c-hook' for more information.
  14518. This hook runs after it has been established that not table field motion and
  14519. not visibility should be done because of current context. This is probably
  14520. the place where a package like yasnippets can hook in.")
  14521. (defvar org-tab-before-tab-emulation-hook nil
  14522. "Hook for functions to attach themselves to TAB.
  14523. See `org-ctrl-c-ctrl-c-hook' for more information.
  14524. This hook runs after every other options for TAB have been exhausted, but
  14525. before indentation and \t insertion takes place.")
  14526. (defvar org-metaleft-hook nil
  14527. "Hook for functions attaching themselves to `M-left'.
  14528. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14529. (defvar org-metaright-hook nil
  14530. "Hook for functions attaching themselves to `M-right'.
  14531. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14532. (defvar org-metaup-hook nil
  14533. "Hook for functions attaching themselves to `M-up'.
  14534. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14535. (defvar org-metadown-hook nil
  14536. "Hook for functions attaching themselves to `M-down'.
  14537. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14538. (defvar org-shiftmetaleft-hook nil
  14539. "Hook for functions attaching themselves to `M-S-left'.
  14540. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14541. (defvar org-shiftmetaright-hook nil
  14542. "Hook for functions attaching themselves to `M-S-right'.
  14543. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14544. (defvar org-shiftmetaup-hook nil
  14545. "Hook for functions attaching themselves to `M-S-up'.
  14546. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14547. (defvar org-shiftmetadown-hook nil
  14548. "Hook for functions attaching themselves to `M-S-down'.
  14549. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14550. (defvar org-metareturn-hook nil
  14551. "Hook for functions attaching themselves to `M-RET'.
  14552. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14553. (defun org-modifier-cursor-error ()
  14554. "Throw an error, a modified cursor command was applied in wrong context."
  14555. (error "This command is active in special context like tables, headlines or items"))
  14556. (defun org-shiftselect-error ()
  14557. "Throw an error because Shift-Cursor command was applied in wrong context."
  14558. (if (and (boundp 'shift-select-mode) shift-select-mode)
  14559. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  14560. (error "This command works only in special context like headlines or timestamps")))
  14561. (defun org-call-for-shift-select (cmd)
  14562. (let ((this-command-keys-shift-translated t))
  14563. (call-interactively cmd)))
  14564. (defun org-shifttab (&optional arg)
  14565. "Global visibility cycling or move to previous table field.
  14566. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14567. on context.
  14568. See the individual commands for more information."
  14569. (interactive "P")
  14570. (cond
  14571. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14572. ((integerp arg)
  14573. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  14574. (message "Content view to level: %d" arg)
  14575. (org-content (prefix-numeric-value arg2))
  14576. (setq org-cycle-global-status 'overview)))
  14577. (t (call-interactively 'org-global-cycle))))
  14578. (defun org-shiftmetaleft ()
  14579. "Promote subtree or delete table column.
  14580. Calls `org-promote-subtree', `org-outdent-item',
  14581. or `org-table-delete-column', depending on context.
  14582. See the individual commands for more information."
  14583. (interactive)
  14584. (cond
  14585. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  14586. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14587. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14588. ((org-at-item-p) (call-interactively 'org-outdent-item))
  14589. (t (org-modifier-cursor-error))))
  14590. (defun org-shiftmetaright ()
  14591. "Demote subtree or insert table column.
  14592. Calls `org-demote-subtree', `org-indent-item',
  14593. or `org-table-insert-column', depending on context.
  14594. See the individual commands for more information."
  14595. (interactive)
  14596. (cond
  14597. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  14598. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14599. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14600. ((org-at-item-p) (call-interactively 'org-indent-item))
  14601. (t (org-modifier-cursor-error))))
  14602. (defun org-shiftmetaup (&optional arg)
  14603. "Move subtree up or kill table row.
  14604. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14605. `org-move-item-up' depending on context. See the individual commands
  14606. for more information."
  14607. (interactive "P")
  14608. (cond
  14609. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  14610. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14611. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14612. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14613. (t (org-modifier-cursor-error))))
  14614. (defun org-shiftmetadown (&optional arg)
  14615. "Move subtree down or insert table row.
  14616. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14617. `org-move-item-down', depending on context. See the individual
  14618. commands for more information."
  14619. (interactive "P")
  14620. (cond
  14621. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  14622. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14623. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14624. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14625. (t (org-modifier-cursor-error))))
  14626. (defun org-metaleft (&optional arg)
  14627. "Promote heading or move table column to left.
  14628. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14629. With no specific context, calls the Emacs default `backward-word'.
  14630. See the individual commands for more information."
  14631. (interactive "P")
  14632. (cond
  14633. ((run-hook-with-args-until-success 'org-metaleft-hook))
  14634. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14635. ((or (org-on-heading-p)
  14636. (and (org-region-active-p)
  14637. (save-excursion
  14638. (goto-char (region-beginning))
  14639. (org-on-heading-p))))
  14640. (call-interactively 'org-do-promote))
  14641. ((or (org-at-item-p)
  14642. (and (org-region-active-p)
  14643. (save-excursion
  14644. (goto-char (region-beginning))
  14645. (org-at-item-p))))
  14646. (call-interactively 'org-outdent-item))
  14647. (t (call-interactively 'backward-word))))
  14648. (defun org-metaright (&optional arg)
  14649. "Demote subtree or move table column to right.
  14650. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14651. With no specific context, calls the Emacs default `forward-word'.
  14652. See the individual commands for more information."
  14653. (interactive "P")
  14654. (cond
  14655. ((run-hook-with-args-until-success 'org-metaright-hook))
  14656. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14657. ((or (org-on-heading-p)
  14658. (and (org-region-active-p)
  14659. (save-excursion
  14660. (goto-char (region-beginning))
  14661. (org-on-heading-p))))
  14662. (call-interactively 'org-do-demote))
  14663. ((or (org-at-item-p)
  14664. (and (org-region-active-p)
  14665. (save-excursion
  14666. (goto-char (region-beginning))
  14667. (org-at-item-p))))
  14668. (call-interactively 'org-indent-item))
  14669. (t (call-interactively 'forward-word))))
  14670. (defun org-metaup (&optional arg)
  14671. "Move subtree up or move table row up.
  14672. Calls `org-move-subtree-up' or `org-table-move-row' or
  14673. `org-move-item-up', depending on context. See the individual commands
  14674. for more information."
  14675. (interactive "P")
  14676. (cond
  14677. ((run-hook-with-args-until-success 'org-metaup-hook))
  14678. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14679. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14680. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14681. (t (transpose-lines 1) (beginning-of-line -1))))
  14682. (defun org-metadown (&optional arg)
  14683. "Move subtree down or move table row down.
  14684. Calls `org-move-subtree-down' or `org-table-move-row' or
  14685. `org-move-item-down', depending on context. See the individual
  14686. commands for more information."
  14687. (interactive "P")
  14688. (cond
  14689. ((run-hook-with-args-until-success 'org-metadown-hook))
  14690. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14691. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14692. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14693. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  14694. (defun org-shiftup (&optional arg)
  14695. "Increase item in timestamp or increase priority of current headline.
  14696. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  14697. depending on context. See the individual commands for more information."
  14698. (interactive "P")
  14699. (cond
  14700. ((and org-support-shift-select (org-region-active-p))
  14701. (org-call-for-shift-select 'previous-line))
  14702. ((org-at-timestamp-p t)
  14703. (call-interactively (if org-edit-timestamp-down-means-later
  14704. 'org-timestamp-down 'org-timestamp-up)))
  14705. ((and (not (eq org-support-shift-select 'always))
  14706. org-enable-priority-commands
  14707. (org-on-heading-p))
  14708. (call-interactively 'org-priority-up))
  14709. ((and (not org-support-shift-select) (org-at-item-p))
  14710. (call-interactively 'org-previous-item))
  14711. ((org-clocktable-try-shift 'up arg))
  14712. (org-support-shift-select
  14713. (org-call-for-shift-select 'previous-line))
  14714. (t (org-shiftselect-error))))
  14715. (defun org-shiftdown (&optional arg)
  14716. "Decrease item in timestamp or decrease priority of current headline.
  14717. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  14718. depending on context. See the individual commands for more information."
  14719. (interactive "P")
  14720. (cond
  14721. ((and org-support-shift-select (org-region-active-p))
  14722. (org-call-for-shift-select 'next-line))
  14723. ((org-at-timestamp-p t)
  14724. (call-interactively (if org-edit-timestamp-down-means-later
  14725. 'org-timestamp-up 'org-timestamp-down)))
  14726. ((and (not (eq org-support-shift-select 'always))
  14727. org-enable-priority-commands
  14728. (org-on-heading-p))
  14729. (call-interactively 'org-priority-down))
  14730. ((and (not org-support-shift-select) (org-at-item-p))
  14731. (call-interactively 'org-next-item))
  14732. ((org-clocktable-try-shift 'down arg))
  14733. (org-support-shift-select
  14734. (org-call-for-shift-select 'next-line))
  14735. (t (org-shiftselect-error))))
  14736. (defun org-shiftright (&optional arg)
  14737. "Cycle the thing at point or in the current line, depending on context.
  14738. Depending on context, this does one of the following:
  14739. - switch a timestamp at point one day into the future
  14740. - on a headline, switch to the next TODO keyword.
  14741. - on an item, switch entire list to the next bullet type
  14742. - on a property line, switch to the next allowed value
  14743. - on a clocktable definition line, move time block into the future"
  14744. (interactive "P")
  14745. (cond
  14746. ((and org-support-shift-select (org-region-active-p))
  14747. (org-call-for-shift-select 'forward-char))
  14748. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14749. ((and (not (eq org-support-shift-select 'always))
  14750. (org-on-heading-p))
  14751. (let ((org-inhibit-logging
  14752. (not org-treat-S-cursor-todo-selection-as-state-change))
  14753. (org-inhibit-blocking
  14754. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14755. (org-call-with-arg 'org-todo 'right)))
  14756. ((or (and org-support-shift-select
  14757. (not (eq org-support-shift-select 'always))
  14758. (org-at-item-bullet-p))
  14759. (and (not org-support-shift-select) (org-at-item-p)))
  14760. (org-call-with-arg 'org-cycle-list-bullet nil))
  14761. ((and (not (eq org-support-shift-select 'always))
  14762. (org-at-property-p))
  14763. (call-interactively 'org-property-next-allowed-value))
  14764. ((org-clocktable-try-shift 'right arg))
  14765. (org-support-shift-select
  14766. (org-call-for-shift-select 'forward-char))
  14767. (t (org-shiftselect-error))))
  14768. (defun org-shiftleft (&optional arg)
  14769. "Cycle the thing at point or in the current line, depending on context.
  14770. Depending on context, this does one of the following:
  14771. - switch a timestamp at point one day into the past
  14772. - on a headline, switch to the previous TODO keyword.
  14773. - on an item, switch entire list to the previous bullet type
  14774. - on a property line, switch to the previous allowed value
  14775. - on a clocktable definition line, move time block into the past"
  14776. (interactive "P")
  14777. (cond
  14778. ((and org-support-shift-select (org-region-active-p))
  14779. (org-call-for-shift-select 'backward-char))
  14780. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14781. ((and (not (eq org-support-shift-select 'always))
  14782. (org-on-heading-p))
  14783. (let ((org-inhibit-logging
  14784. (not org-treat-S-cursor-todo-selection-as-state-change))
  14785. (org-inhibit-blocking
  14786. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14787. (org-call-with-arg 'org-todo 'left)))
  14788. ((or (and org-support-shift-select
  14789. (not (eq org-support-shift-select 'always))
  14790. (org-at-item-bullet-p))
  14791. (and (not org-support-shift-select) (org-at-item-p)))
  14792. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  14793. ((and (not (eq org-support-shift-select 'always))
  14794. (org-at-property-p))
  14795. (call-interactively 'org-property-previous-allowed-value))
  14796. ((org-clocktable-try-shift 'left arg))
  14797. (org-support-shift-select
  14798. (org-call-for-shift-select 'backward-char))
  14799. (t (org-shiftselect-error))))
  14800. (defun org-shiftcontrolright ()
  14801. "Switch to next TODO set."
  14802. (interactive)
  14803. (cond
  14804. ((and org-support-shift-select (org-region-active-p))
  14805. (org-call-for-shift-select 'forward-word))
  14806. ((and (not (eq org-support-shift-select 'always))
  14807. (org-on-heading-p))
  14808. (org-call-with-arg 'org-todo 'nextset))
  14809. (org-support-shift-select
  14810. (org-call-for-shift-select 'forward-word))
  14811. (t (org-shiftselect-error))))
  14812. (defun org-shiftcontrolleft ()
  14813. "Switch to previous TODO set."
  14814. (interactive)
  14815. (cond
  14816. ((and org-support-shift-select (org-region-active-p))
  14817. (org-call-for-shift-select 'backward-word))
  14818. ((and (not (eq org-support-shift-select 'always))
  14819. (org-on-heading-p))
  14820. (org-call-with-arg 'org-todo 'previousset))
  14821. (org-support-shift-select
  14822. (org-call-for-shift-select 'backward-word))
  14823. (t (org-shiftselect-error))))
  14824. (defun org-ctrl-c-ret ()
  14825. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  14826. (interactive)
  14827. (cond
  14828. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  14829. (t (call-interactively 'org-insert-heading))))
  14830. (defun org-copy-special ()
  14831. "Copy region in table or copy current subtree.
  14832. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14833. See the individual commands for more information."
  14834. (interactive)
  14835. (call-interactively
  14836. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14837. (defun org-cut-special ()
  14838. "Cut region in table or cut current subtree.
  14839. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14840. See the individual commands for more information."
  14841. (interactive)
  14842. (call-interactively
  14843. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14844. (defun org-paste-special (arg)
  14845. "Paste rectangular region into table, or past subtree relative to level.
  14846. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14847. See the individual commands for more information."
  14848. (interactive "P")
  14849. (if (org-at-table-p)
  14850. (org-table-paste-rectangle)
  14851. (org-paste-subtree arg)))
  14852. (defun org-edit-special ()
  14853. "Call a special editor for the stuff at point.
  14854. When at a table, call the formula editor with `org-table-edit-formulas'.
  14855. When at the first line of an src example, call `org-edit-src-code'.
  14856. When in an #+include line, visit the include file. Otherwise call
  14857. `ffap' to visit the file at point."
  14858. (interactive)
  14859. (cond
  14860. ((org-at-table.el-p)
  14861. (org-edit-src-code))
  14862. ((org-at-table-p)
  14863. (call-interactively 'org-table-edit-formulas))
  14864. ((save-excursion
  14865. (beginning-of-line 1)
  14866. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  14867. (find-file (org-trim (match-string 1))))
  14868. ((org-edit-src-code))
  14869. ((org-edit-fixed-width-region))
  14870. (t (call-interactively 'ffap))))
  14871. (defun org-ctrl-c-ctrl-c (&optional arg)
  14872. "Set tags in headline, or update according to changed information at point.
  14873. This command does many different things, depending on context:
  14874. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  14875. this is what we do.
  14876. - If the cursor is on a statistics cookie, update it.
  14877. - If the cursor is in a headline, prompt for tags and insert them
  14878. into the current line, aligned to `org-tags-column'. When called
  14879. with prefix arg, realign all tags in the current buffer.
  14880. - If the cursor is in one of the special #+KEYWORD lines, this
  14881. triggers scanning the buffer for these lines and updating the
  14882. information.
  14883. - If the cursor is inside a table, realign the table. This command
  14884. works even if the automatic table editor has been turned off.
  14885. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14886. the entire table.
  14887. - If the cursor is at a footnote reference or definition, jump to
  14888. the corresponding definition or references, respectively.
  14889. - If the cursor is a the beginning of a dynamic block, update it.
  14890. - If the current buffer is a remember buffer, close note and file
  14891. it. A prefix argument of 1 files to the default location
  14892. without further interaction. A prefix argument of 2 files to
  14893. the currently clocking task.
  14894. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14895. links in this buffer.
  14896. - If the cursor is on a numbered item in a plain list, renumber the
  14897. ordered list.
  14898. - If the cursor is on a checkbox, toggle it."
  14899. (interactive "P")
  14900. (let ((org-enable-table-editor t))
  14901. (cond
  14902. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  14903. org-occur-highlights
  14904. org-latex-fragment-image-overlays)
  14905. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  14906. (org-remove-occur-highlights)
  14907. (org-remove-latex-fragment-image-overlays)
  14908. (message "Temporary highlights/overlays removed from current buffer"))
  14909. ((and (local-variable-p 'org-finish-function (current-buffer))
  14910. (fboundp org-finish-function))
  14911. (funcall org-finish-function))
  14912. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  14913. ((or (looking-at (org-re org-property-start-re))
  14914. (org-at-property-p))
  14915. (call-interactively 'org-property-action))
  14916. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14917. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  14918. (or (org-on-heading-p) (org-at-item-p)))
  14919. (call-interactively 'org-update-statistics-cookies))
  14920. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14921. ((org-at-table.el-p)
  14922. (message "Use C-c ' to edit table.el tables"))
  14923. ((org-at-table-p)
  14924. (org-table-maybe-eval-formula)
  14925. (if arg
  14926. (call-interactively 'org-table-recalculate)
  14927. (org-table-maybe-recalculate-line))
  14928. (call-interactively 'org-table-align))
  14929. ((or (org-footnote-at-reference-p)
  14930. (org-footnote-at-definition-p))
  14931. (call-interactively 'org-footnote-action))
  14932. ((org-at-item-checkbox-p)
  14933. (call-interactively 'org-toggle-checkbox))
  14934. ((org-at-item-p)
  14935. (if arg
  14936. (call-interactively 'org-toggle-checkbox)
  14937. (call-interactively 'org-maybe-renumber-ordered-list)))
  14938. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  14939. ;; Dynamic block
  14940. (beginning-of-line 1)
  14941. (save-excursion (org-update-dblock)))
  14942. ((save-excursion
  14943. (beginning-of-line 1)
  14944. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  14945. (cond
  14946. ((equal (match-string 1) "TBLFM")
  14947. ;; Recalculate the table before this line
  14948. (save-excursion
  14949. (beginning-of-line 1)
  14950. (skip-chars-backward " \r\n\t")
  14951. (if (org-at-table-p)
  14952. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  14953. (t
  14954. (let ((org-inhibit-startup-visibility-stuff t)
  14955. (org-startup-align-all-tables nil))
  14956. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  14957. (message "Local setup has been refreshed"))))
  14958. ((org-clock-update-time-maybe))
  14959. (t (error "C-c C-c can do nothing useful at this location")))))
  14960. (defun org-mode-restart ()
  14961. "Restart Org-mode, to scan again for special lines.
  14962. Also updates the keyword regular expressions."
  14963. (interactive)
  14964. (org-mode)
  14965. (message "Org-mode restarted"))
  14966. (defun org-kill-note-or-show-branches ()
  14967. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  14968. (interactive)
  14969. (if (not org-finish-function)
  14970. (call-interactively 'show-branches)
  14971. (let ((org-note-abort t))
  14972. (funcall org-finish-function))))
  14973. (defun org-return (&optional indent)
  14974. "Goto next table row or insert a newline.
  14975. Calls `org-table-next-row' or `newline', depending on context.
  14976. See the individual commands for more information."
  14977. (interactive)
  14978. (cond
  14979. ((bobp) (if indent (newline-and-indent) (newline)))
  14980. ((org-at-table-p)
  14981. (org-table-justify-field-maybe)
  14982. (call-interactively 'org-table-next-row))
  14983. ((and org-return-follows-link
  14984. (eq (get-text-property (point) 'face) 'org-link))
  14985. (call-interactively 'org-open-at-point))
  14986. ((and (org-at-heading-p)
  14987. (looking-at
  14988. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  14989. (org-show-entry)
  14990. (end-of-line 1)
  14991. (newline))
  14992. (t (if indent (newline-and-indent) (newline)))))
  14993. (defun org-return-indent ()
  14994. "Goto next table row or insert a newline and indent.
  14995. Calls `org-table-next-row' or `newline-and-indent', depending on
  14996. context. See the individual commands for more information."
  14997. (interactive)
  14998. (org-return t))
  14999. (defun org-ctrl-c-star ()
  15000. "Compute table, or change heading status of lines.
  15001. Calls `org-table-recalculate' or `org-toggle-heading',
  15002. depending on context."
  15003. (interactive)
  15004. (cond
  15005. ((org-at-table-p)
  15006. (call-interactively 'org-table-recalculate))
  15007. (t
  15008. ;; Convert all lines in region to list items
  15009. (call-interactively 'org-toggle-heading))))
  15010. (defun org-ctrl-c-minus ()
  15011. "Insert separator line in table or modify bullet status of line.
  15012. Also turns a plain line or a region of lines into list items.
  15013. Calls `org-table-insert-hline', `org-toggle-item', or
  15014. `org-cycle-list-bullet', depending on context."
  15015. (interactive)
  15016. (cond
  15017. ((org-at-table-p)
  15018. (call-interactively 'org-table-insert-hline))
  15019. ((org-region-active-p)
  15020. (call-interactively 'org-toggle-item))
  15021. ((org-in-item-p)
  15022. (call-interactively 'org-cycle-list-bullet))
  15023. (t
  15024. (call-interactively 'org-toggle-item))))
  15025. (defun org-toggle-item ()
  15026. "Convert headings or normal lines to items, items to normal lines.
  15027. If there is no active region, only the current line is considered.
  15028. If the first line in the region is a headline, convert all headlines to items.
  15029. If the first line in the region is an item, convert all items to normal lines.
  15030. If the first line is normal text, add an item bullet to each line."
  15031. (interactive)
  15032. (let (l2 l beg end)
  15033. (if (org-region-active-p)
  15034. (setq beg (region-beginning) end (region-end))
  15035. (setq beg (point-at-bol)
  15036. end (min (1+ (point-at-eol)) (point-max))))
  15037. (save-excursion
  15038. (goto-char end)
  15039. (setq l2 (org-current-line))
  15040. (goto-char beg)
  15041. (beginning-of-line 1)
  15042. (setq l (1- (org-current-line)))
  15043. (if (org-at-item-p)
  15044. ;; We already have items, de-itemize
  15045. (while (< (setq l (1+ l)) l2)
  15046. (when (org-at-item-p)
  15047. (goto-char (match-beginning 2))
  15048. (delete-region (match-beginning 2) (match-end 2))
  15049. (and (looking-at "[ \t]+") (replace-match "")))
  15050. (beginning-of-line 2))
  15051. (if (org-on-heading-p)
  15052. ;; Headings, convert to items
  15053. (while (< (setq l (1+ l)) l2)
  15054. (if (looking-at org-outline-regexp)
  15055. (replace-match "- " t t))
  15056. (beginning-of-line 2))
  15057. ;; normal lines, turn them into items
  15058. (while (< (setq l (1+ l)) l2)
  15059. (unless (org-at-item-p)
  15060. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15061. (replace-match "\\1- \\2")))
  15062. (beginning-of-line 2)))))))
  15063. (defun org-toggle-heading (&optional nstars)
  15064. "Convert headings to normal text, or items or text to headings.
  15065. If there is no active region, only the current line is considered.
  15066. If the first line is a heading, remove the stars from all headlines
  15067. in the region.
  15068. If the first line is a plain list item, turn all plain list items
  15069. into headings.
  15070. If the first line is a normal line, turn each and every line in the
  15071. region into a heading.
  15072. When converting a line into a heading, the number of stars is chosen
  15073. such that the lines become children of the current entry. However,
  15074. when a prefix argument is given, its value determines the number of
  15075. stars to add."
  15076. (interactive "P")
  15077. (let (l2 l itemp beg end)
  15078. (if (org-region-active-p)
  15079. (setq beg (region-beginning) end (region-end))
  15080. (setq beg (point-at-bol)
  15081. end (min (1+ (point-at-eol)) (point-max))))
  15082. (save-excursion
  15083. (goto-char end)
  15084. (setq l2 (org-current-line))
  15085. (goto-char beg)
  15086. (beginning-of-line 1)
  15087. (setq l (1- (org-current-line)))
  15088. (if (org-on-heading-p)
  15089. ;; We already have headlines, de-star them
  15090. (while (< (setq l (1+ l)) l2)
  15091. (when (org-on-heading-p t)
  15092. (and (looking-at outline-regexp) (replace-match "")))
  15093. (beginning-of-line 2))
  15094. (setq itemp (org-at-item-p))
  15095. (let* ((stars
  15096. (if nstars
  15097. (make-string (prefix-numeric-value current-prefix-arg)
  15098. ?*)
  15099. (save-excursion
  15100. (if (re-search-backward org-complex-heading-regexp nil t)
  15101. (match-string 1) ""))))
  15102. (add-stars (cond (nstars "")
  15103. ((equal stars "") "*")
  15104. (org-odd-levels-only "**")
  15105. (t "*")))
  15106. (rpl (concat stars add-stars " ")))
  15107. (while (< (setq l (1+ l)) l2)
  15108. (if itemp
  15109. (and (org-at-item-p) (replace-match rpl t t))
  15110. (unless (org-on-heading-p)
  15111. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15112. (replace-match (concat rpl (match-string 2))))))
  15113. (beginning-of-line 2)))))))
  15114. (defun org-meta-return (&optional arg)
  15115. "Insert a new heading or wrap a region in a table.
  15116. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15117. See the individual commands for more information."
  15118. (interactive "P")
  15119. (cond
  15120. ((run-hook-with-args-until-success 'org-metareturn-hook))
  15121. ((org-at-table-p)
  15122. (call-interactively 'org-table-wrap-region))
  15123. (t (call-interactively 'org-insert-heading))))
  15124. ;;; Menu entries
  15125. ;; Define the Org-mode menus
  15126. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15127. '("Tbl"
  15128. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  15129. ["Next Field" org-cycle (org-at-table-p)]
  15130. ["Previous Field" org-shifttab (org-at-table-p)]
  15131. ["Next Row" org-return (org-at-table-p)]
  15132. "--"
  15133. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15134. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15135. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15136. "--"
  15137. ("Column"
  15138. ["Move Column Left" org-metaleft (org-at-table-p)]
  15139. ["Move Column Right" org-metaright (org-at-table-p)]
  15140. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15141. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  15142. ("Row"
  15143. ["Move Row Up" org-metaup (org-at-table-p)]
  15144. ["Move Row Down" org-metadown (org-at-table-p)]
  15145. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15146. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15147. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15148. "--"
  15149. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  15150. ("Rectangle"
  15151. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15152. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15153. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15154. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15155. "--"
  15156. ("Calculate"
  15157. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  15158. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  15159. ["Edit Formulas" org-edit-special (org-at-table-p)]
  15160. "--"
  15161. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  15162. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  15163. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  15164. "--"
  15165. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  15166. "--"
  15167. ["Sum Column/Rectangle" org-table-sum
  15168. (or (org-at-table-p) (org-region-active-p))]
  15169. ["Which Column?" org-table-current-column (org-at-table-p)])
  15170. ["Debug Formulas"
  15171. org-table-toggle-formula-debugger
  15172. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  15173. ["Show Col/Row Numbers"
  15174. org-table-toggle-coordinate-overlays
  15175. :style toggle
  15176. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  15177. "--"
  15178. ["Create" org-table-create (and (not (org-at-table-p))
  15179. org-enable-table-editor)]
  15180. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  15181. ["Import from File" org-table-import (not (org-at-table-p))]
  15182. ["Export to File" org-table-export (org-at-table-p)]
  15183. "--"
  15184. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  15185. (easy-menu-define org-org-menu org-mode-map "Org menu"
  15186. '("Org"
  15187. ("Show/Hide"
  15188. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  15189. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  15190. ["Sparse Tree..." org-sparse-tree t]
  15191. ["Reveal Context" org-reveal t]
  15192. ["Show All" show-all t]
  15193. "--"
  15194. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  15195. "--"
  15196. ["New Heading" org-insert-heading t]
  15197. ("Navigate Headings"
  15198. ["Up" outline-up-heading t]
  15199. ["Next" outline-next-visible-heading t]
  15200. ["Previous" outline-previous-visible-heading t]
  15201. ["Next Same Level" outline-forward-same-level t]
  15202. ["Previous Same Level" outline-backward-same-level t]
  15203. "--"
  15204. ["Jump" org-goto t])
  15205. ("Edit Structure"
  15206. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  15207. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  15208. "--"
  15209. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  15210. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  15211. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  15212. "--"
  15213. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  15214. "--"
  15215. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  15216. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  15217. ["Demote Heading" org-metaright (not (org-at-table-p))]
  15218. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  15219. "--"
  15220. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  15221. "--"
  15222. ["Convert to odd levels" org-convert-to-odd-levels t]
  15223. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  15224. ("Editing"
  15225. ["Emphasis..." org-emphasize t]
  15226. ["Edit Source Example" org-edit-special t]
  15227. "--"
  15228. ["Footnote new/jump" org-footnote-action t]
  15229. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  15230. ("Archive"
  15231. ["Archive (default method)" org-archive-subtree-default t]
  15232. "--"
  15233. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  15234. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  15235. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  15236. )
  15237. "--"
  15238. ("Hyperlinks"
  15239. ["Store Link (Global)" org-store-link t]
  15240. ["Find existing link to here" org-occur-link-in-agenda-files t]
  15241. ["Insert Link" org-insert-link t]
  15242. ["Follow Link" org-open-at-point t]
  15243. "--"
  15244. ["Next link" org-next-link t]
  15245. ["Previous link" org-previous-link t]
  15246. "--"
  15247. ["Descriptive Links"
  15248. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  15249. :style radio
  15250. :selected (member '(org-link) buffer-invisibility-spec)]
  15251. ["Literal Links"
  15252. (progn
  15253. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  15254. :style radio
  15255. :selected (not (member '(org-link) buffer-invisibility-spec))])
  15256. "--"
  15257. ("TODO Lists"
  15258. ["TODO/DONE/-" org-todo t]
  15259. ("Select keyword"
  15260. ["Next keyword" org-shiftright (org-on-heading-p)]
  15261. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  15262. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  15263. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  15264. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  15265. ["Show TODO Tree" org-show-todo-tree t]
  15266. ["Global TODO list" org-todo-list t]
  15267. "--"
  15268. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  15269. :selected org-enforce-todo-dependencies :style toggle :active t]
  15270. "Settings for tree at point"
  15271. ["Do Children sequentially" org-toggle-ordered-property :style radio
  15272. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  15273. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15274. ["Do Children parallel" org-toggle-ordered-property :style radio
  15275. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  15276. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15277. "--"
  15278. ["Set Priority" org-priority t]
  15279. ["Priority Up" org-shiftup t]
  15280. ["Priority Down" org-shiftdown t]
  15281. "--"
  15282. ["Get news from all feeds" org-feed-update-all t]
  15283. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  15284. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  15285. ("TAGS and Properties"
  15286. ["Set Tags" org-set-tags-command t]
  15287. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  15288. "--"
  15289. ["Set property" org-set-property t]
  15290. ["Column view of properties" org-columns t]
  15291. ["Insert Column View DBlock" org-insert-columns-dblock t])
  15292. ("Dates and Scheduling"
  15293. ["Timestamp" org-time-stamp t]
  15294. ["Timestamp (inactive)" org-time-stamp-inactive t]
  15295. ("Change Date"
  15296. ["1 Day Later" org-shiftright t]
  15297. ["1 Day Earlier" org-shiftleft t]
  15298. ["1 ... Later" org-shiftup t]
  15299. ["1 ... Earlier" org-shiftdown t])
  15300. ["Compute Time Range" org-evaluate-time-range t]
  15301. ["Schedule Item" org-schedule t]
  15302. ["Deadline" org-deadline t]
  15303. "--"
  15304. ["Custom time format" org-toggle-time-stamp-overlays
  15305. :style radio :selected org-display-custom-times]
  15306. "--"
  15307. ["Goto Calendar" org-goto-calendar t]
  15308. ["Date from Calendar" org-date-from-calendar t]
  15309. "--"
  15310. ["Start/Restart Timer" org-timer-start t]
  15311. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  15312. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  15313. ["Insert Timer String" org-timer t]
  15314. ["Insert Timer Item" org-timer-item t])
  15315. ("Logging work"
  15316. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  15317. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  15318. ["Clock out" org-clock-out t]
  15319. ["Clock cancel" org-clock-cancel t]
  15320. "--"
  15321. ["Mark as default task" org-clock-mark-default-task t]
  15322. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  15323. ["Goto running clock" org-clock-goto t]
  15324. "--"
  15325. ["Display times" org-clock-display t]
  15326. ["Create clock table" org-clock-report t]
  15327. "--"
  15328. ["Record DONE time"
  15329. (progn (setq org-log-done (not org-log-done))
  15330. (message "Switching to %s will %s record a timestamp"
  15331. (car org-done-keywords)
  15332. (if org-log-done "automatically" "not")))
  15333. :style toggle :selected org-log-done])
  15334. "--"
  15335. ["Agenda Command..." org-agenda t]
  15336. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  15337. ("File List for Agenda")
  15338. ("Special views current file"
  15339. ["TODO Tree" org-show-todo-tree t]
  15340. ["Check Deadlines" org-check-deadlines t]
  15341. ["Timeline" org-timeline t]
  15342. ["Tags/Property tree" org-match-sparse-tree t])
  15343. "--"
  15344. ["Export/Publish..." org-export t]
  15345. ("LaTeX"
  15346. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15347. :selected org-cdlatex-mode]
  15348. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15349. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15350. ["Modify math symbol" org-cdlatex-math-modify
  15351. (org-inside-LaTeX-fragment-p)]
  15352. ["Insert citation" org-reftex-citation t]
  15353. "--"
  15354. ["Export LaTeX fragments as images"
  15355. (if (featurep 'org-exp)
  15356. (setq org-export-with-LaTeX-fragments
  15357. (not org-export-with-LaTeX-fragments))
  15358. (require 'org-exp))
  15359. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  15360. org-export-with-LaTeX-fragments)]
  15361. "--"
  15362. ["Template for BEAMER" org-beamer-settings-template t])
  15363. "--"
  15364. ("MobileOrg"
  15365. ["Push Files and Views" org-mobile-push t]
  15366. ["Get Captured and Flagged" org-mobile-pull t]
  15367. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  15368. "--"
  15369. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  15370. "--"
  15371. ("Documentation"
  15372. ["Show Version" org-version t]
  15373. ["Info Documentation" org-info t])
  15374. ("Customize"
  15375. ["Browse Org Group" org-customize t]
  15376. "--"
  15377. ["Expand This Menu" org-create-customize-menu
  15378. (fboundp 'customize-menu-create)])
  15379. ["Send bug report" org-submit-bug-report t]
  15380. "--"
  15381. ("Refresh/Reload"
  15382. ["Refresh setup current buffer" org-mode-restart t]
  15383. ["Reload Org (after update)" org-reload t]
  15384. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  15385. ))
  15386. (defun org-info (&optional node)
  15387. "Read documentation for Org-mode in the info system.
  15388. With optional NODE, go directly to that node."
  15389. (interactive)
  15390. (info (format "(org)%s" (or node ""))))
  15391. ;;;###autoload
  15392. (defun org-submit-bug-report ()
  15393. "Submit a bug report on Org-mode via mail.
  15394. Don't hesitate to report any problems or inaccurate documentation.
  15395. If you don't have setup sending mail from (X)Emacs, please copy the
  15396. output buffer into your mail program, as it gives us important
  15397. information about your Org-mode version and configuration."
  15398. (interactive)
  15399. (require 'reporter)
  15400. (org-load-modules-maybe)
  15401. (org-require-autoloaded-modules)
  15402. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  15403. (reporter-submit-bug-report
  15404. "emacs-orgmode@gnu.org"
  15405. (org-version)
  15406. (let (list)
  15407. (save-window-excursion
  15408. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  15409. (delete-other-windows)
  15410. (erase-buffer)
  15411. (insert "You are about to submit a bug report to the Org-mode mailing list.
  15412. We would like to add your full Org-mode and Outline configuration to the
  15413. bug report. This greatly simplifies the work of the maintainer and
  15414. other experts on the mailing list.
  15415. HOWEVER, some variables you have customized may contain private
  15416. information. The names of customers, colleagues, or friends, might
  15417. appear in the form of file names, tags, todo states, or search strings.
  15418. If you answer yes to the prompt, you might want to check and remove
  15419. such private information before sending the email.")
  15420. (add-text-properties (point-min) (point-max) '(face org-warning))
  15421. (when (yes-or-no-p "Include your Org-mode configuration ")
  15422. (mapatoms
  15423. (lambda (v)
  15424. (and (boundp v)
  15425. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  15426. (or (and (symbol-value v)
  15427. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  15428. (and
  15429. (get v 'custom-type) (get v 'standard-value)
  15430. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  15431. (push v list)))))
  15432. (kill-buffer (get-buffer "*Warn about privacy*"))
  15433. list))
  15434. nil nil
  15435. "Remember to cover the basics, that is, what you expected to happen and
  15436. what in fact did happen. You don't know how to make a good report? See
  15437. http://orgmode.org/manual/Feedback.html#Feedback
  15438. Your bug report will be posted to the Org-mode mailing list.
  15439. ------------------------------------------------------------------------")
  15440. (save-excursion
  15441. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  15442. (replace-match "\\1Bug: \\3 [\\2]")))))
  15443. (defun org-install-agenda-files-menu ()
  15444. (let ((bl (buffer-list)))
  15445. (save-excursion
  15446. (while bl
  15447. (set-buffer (pop bl))
  15448. (if (org-mode-p) (setq bl nil)))
  15449. (when (org-mode-p)
  15450. (easy-menu-change
  15451. '("Org") "File List for Agenda"
  15452. (append
  15453. (list
  15454. ["Edit File List" (org-edit-agenda-file-list) t]
  15455. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15456. ["Remove Current File from List" org-remove-file t]
  15457. ["Cycle through agenda files" org-cycle-agenda-files t]
  15458. ["Occur in all agenda files" org-occur-in-agenda-files t]
  15459. "--")
  15460. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15461. ;;;; Documentation
  15462. ;;;###autoload
  15463. (defun org-require-autoloaded-modules ()
  15464. (interactive)
  15465. (mapc 'require
  15466. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  15467. org-docbook org-exp org-html org-icalendar
  15468. org-id org-latex
  15469. org-publish org-remember org-table
  15470. org-timer org-xoxo)))
  15471. ;;;###autoload
  15472. (defun org-reload (&optional uncompiled)
  15473. "Reload all org lisp files.
  15474. With prefix arg UNCOMPILED, load the uncompiled versions."
  15475. (interactive "P")
  15476. (require 'find-func)
  15477. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  15478. (dir-org (file-name-directory (org-find-library-name "org")))
  15479. (dir-org-contrib (ignore-errors
  15480. (file-name-directory
  15481. (org-find-library-name "org-contribdir"))))
  15482. (files
  15483. (append (directory-files dir-org t file-re)
  15484. (and dir-org-contrib
  15485. (directory-files dir-org-contrib t file-re))))
  15486. (remove-re (concat (if (featurep 'xemacs)
  15487. "org-colview" "org-colview-xemacs")
  15488. "\\'")))
  15489. (setq files (mapcar 'file-name-sans-extension files))
  15490. (setq files (mapcar
  15491. (lambda (x) (if (string-match remove-re x) nil x))
  15492. files))
  15493. (setq files (delq nil files))
  15494. (mapc
  15495. (lambda (f)
  15496. (when (featurep (intern (file-name-nondirectory f)))
  15497. (if (and (not uncompiled)
  15498. (file-exists-p (concat f ".elc")))
  15499. (load (concat f ".elc") nil nil t)
  15500. (load (concat f ".el") nil nil t))))
  15501. files))
  15502. (org-version))
  15503. ;;;###autoload
  15504. (defun org-customize ()
  15505. "Call the customize function with org as argument."
  15506. (interactive)
  15507. (org-load-modules-maybe)
  15508. (org-require-autoloaded-modules)
  15509. (customize-browse 'org))
  15510. (defun org-create-customize-menu ()
  15511. "Create a full customization menu for Org-mode, insert it into the menu."
  15512. (interactive)
  15513. (org-load-modules-maybe)
  15514. (org-require-autoloaded-modules)
  15515. (if (fboundp 'customize-menu-create)
  15516. (progn
  15517. (easy-menu-change
  15518. '("Org") "Customize"
  15519. `(["Browse Org group" org-customize t]
  15520. "--"
  15521. ,(customize-menu-create 'org)
  15522. ["Set" Custom-set t]
  15523. ["Save" Custom-save t]
  15524. ["Reset to Current" Custom-reset-current t]
  15525. ["Reset to Saved" Custom-reset-saved t]
  15526. ["Reset to Standard Settings" Custom-reset-standard t]))
  15527. (message "\"Org\"-menu now contains full customization menu"))
  15528. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15529. ;;;; Miscellaneous stuff
  15530. ;;; Generally useful functions
  15531. (defun org-get-at-bol (property)
  15532. "Get text property PROPERTY at beginning of line."
  15533. (get-text-property (point-at-bol) property))
  15534. (defun org-find-text-property-in-string (prop s)
  15535. "Return the first non-nil value of property PROP in string S."
  15536. (or (get-text-property 0 prop s)
  15537. (get-text-property (or (next-single-property-change 0 prop s) 0)
  15538. prop s)))
  15539. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  15540. "Display the given MESSAGE as a warning."
  15541. (if (fboundp 'display-warning)
  15542. (display-warning 'org message
  15543. (if (featurep 'xemacs)
  15544. 'warning
  15545. :warning))
  15546. (let ((buf (get-buffer-create "*Org warnings*")))
  15547. (with-current-buffer buf
  15548. (goto-char (point-max))
  15549. (insert "Warning (Org): " message)
  15550. (unless (bolp)
  15551. (newline)))
  15552. (display-buffer buf)
  15553. (sit-for 0))))
  15554. (defun org-in-commented-line ()
  15555. "Is point in a line starting with `#'?"
  15556. (equal (char-after (point-at-bol)) ?#))
  15557. (defun org-in-verbatim-emphasis ()
  15558. (save-match-data
  15559. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  15560. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  15561. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  15562. (if (and marker (marker-buffer marker)
  15563. (buffer-live-p (marker-buffer marker)))
  15564. (progn
  15565. (switch-to-buffer (marker-buffer marker))
  15566. (if (or (> marker (point-max)) (< marker (point-min)))
  15567. (widen))
  15568. (goto-char marker)
  15569. (org-show-context 'org-goto))
  15570. (if bookmark
  15571. (bookmark-jump bookmark)
  15572. (error "Cannot find location"))))
  15573. (defun org-quote-csv-field (s)
  15574. "Quote field for inclusion in CSV material."
  15575. (if (string-match "[\",]" s)
  15576. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  15577. s))
  15578. (defun org-plist-delete (plist property)
  15579. "Delete PROPERTY from PLIST.
  15580. This is in contrast to merely setting it to 0."
  15581. (let (p)
  15582. (while plist
  15583. (if (not (eq property (car plist)))
  15584. (setq p (plist-put p (car plist) (nth 1 plist))))
  15585. (setq plist (cddr plist)))
  15586. p))
  15587. (defun org-force-self-insert (N)
  15588. "Needed to enforce self-insert under remapping."
  15589. (interactive "p")
  15590. (self-insert-command N))
  15591. (defun org-string-width (s)
  15592. "Compute width of string, ignoring invisible characters.
  15593. This ignores character with invisibility property `org-link', and also
  15594. characters with property `org-cwidth', because these will become invisible
  15595. upon the next fontification round."
  15596. (let (b l)
  15597. (when (or (eq t buffer-invisibility-spec)
  15598. (assq 'org-link buffer-invisibility-spec))
  15599. (while (setq b (text-property-any 0 (length s)
  15600. 'invisible 'org-link s))
  15601. (setq s (concat (substring s 0 b)
  15602. (substring s (or (next-single-property-change
  15603. b 'invisible s) (length s)))))))
  15604. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  15605. (setq s (concat (substring s 0 b)
  15606. (substring s (or (next-single-property-change
  15607. b 'org-cwidth s) (length s))))))
  15608. (setq l (string-width s) b -1)
  15609. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  15610. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  15611. l))
  15612. (defun org-get-indentation (&optional line)
  15613. "Get the indentation of the current line, interpreting tabs.
  15614. When LINE is given, assume it represents a line and compute its indentation."
  15615. (if line
  15616. (if (string-match "^ *" (org-remove-tabs line))
  15617. (match-end 0))
  15618. (save-excursion
  15619. (beginning-of-line 1)
  15620. (skip-chars-forward " \t")
  15621. (current-column))))
  15622. (defun org-remove-tabs (s &optional width)
  15623. "Replace tabulators in S with spaces.
  15624. Assumes that s is a single line, starting in column 0."
  15625. (setq width (or width tab-width))
  15626. (while (string-match "\t" s)
  15627. (setq s (replace-match
  15628. (make-string
  15629. (- (* width (/ (+ (match-beginning 0) width) width))
  15630. (match-beginning 0)) ?\ )
  15631. t t s)))
  15632. s)
  15633. (defun org-fix-indentation (line ind)
  15634. "Fix indentation in LINE.
  15635. IND is a cons cell with target and minimum indentation.
  15636. If the current indentation in LINE is smaller than the minimum,
  15637. leave it alone. If it is larger than ind, set it to the target."
  15638. (let* ((l (org-remove-tabs line))
  15639. (i (org-get-indentation l))
  15640. (i1 (car ind)) (i2 (cdr ind)))
  15641. (if (>= i i2) (setq l (substring line i2)))
  15642. (if (> i1 0)
  15643. (concat (make-string i1 ?\ ) l)
  15644. l)))
  15645. (defun org-remove-indentation (code &optional n)
  15646. "Remove the maximum common indentation from the lines in CODE.
  15647. N may optionally be the number of spaces to remove."
  15648. (with-temp-buffer
  15649. (insert code)
  15650. (org-do-remove-indentation n)
  15651. (buffer-string)))
  15652. (defun org-do-remove-indentation (&optional n)
  15653. "Remove the maximum common indentation from the buffer."
  15654. (untabify (point-min) (point-max))
  15655. (let ((min 10000) re)
  15656. (if n
  15657. (setq min n)
  15658. (goto-char (point-min))
  15659. (while (re-search-forward "^ *[^ \n]" nil t)
  15660. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  15661. (unless (or (= min 0) (= min 10000))
  15662. (setq re (format "^ \\{%d\\}" min))
  15663. (goto-char (point-min))
  15664. (while (re-search-forward re nil t)
  15665. (replace-match "")
  15666. (end-of-line 1))
  15667. min)))
  15668. (defun org-fill-template (template alist)
  15669. "Find each %key of ALIST in TEMPLATE and replace it."
  15670. (let ((case-fold-search nil)
  15671. entry key value)
  15672. (setq alist (sort (copy-sequence alist)
  15673. (lambda (a b) (< (length (car a)) (length (car b))))))
  15674. (while (setq entry (pop alist))
  15675. (setq template
  15676. (replace-regexp-in-string
  15677. (concat "%" (regexp-quote (car entry)))
  15678. (cdr entry) template t t)))
  15679. template))
  15680. (defun org-base-buffer (buffer)
  15681. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  15682. (if (not buffer)
  15683. buffer
  15684. (or (buffer-base-buffer buffer)
  15685. buffer)))
  15686. (defun org-trim (s)
  15687. "Remove whitespace at beginning and end of string."
  15688. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  15689. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  15690. s)
  15691. (defun org-wrap (string &optional width lines)
  15692. "Wrap string to either a number of lines, or a width in characters.
  15693. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  15694. that costs. If there is a word longer than WIDTH, the text is actually
  15695. wrapped to the length of that word.
  15696. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  15697. many lines, whatever width that takes.
  15698. The return value is a list of lines, without newlines at the end."
  15699. (let* ((words (org-split-string string "[ \t\n]+"))
  15700. (maxword (apply 'max (mapcar 'org-string-width words)))
  15701. w ll)
  15702. (cond (width
  15703. (org-do-wrap words (max maxword width)))
  15704. (lines
  15705. (setq w maxword)
  15706. (setq ll (org-do-wrap words maxword))
  15707. (if (<= (length ll) lines)
  15708. ll
  15709. (setq ll words)
  15710. (while (> (length ll) lines)
  15711. (setq w (1+ w))
  15712. (setq ll (org-do-wrap words w)))
  15713. ll))
  15714. (t (error "Cannot wrap this")))))
  15715. (defun org-do-wrap (words width)
  15716. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  15717. (let (lines line)
  15718. (while words
  15719. (setq line (pop words))
  15720. (while (and words (< (+ (length line) (length (car words))) width))
  15721. (setq line (concat line " " (pop words))))
  15722. (setq lines (push line lines)))
  15723. (nreverse lines)))
  15724. (defun org-split-string (string &optional separators)
  15725. "Splits STRING into substrings at SEPARATORS.
  15726. No empty strings are returned if there are matches at the beginning
  15727. and end of string."
  15728. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  15729. (start 0)
  15730. notfirst
  15731. (list nil))
  15732. (while (and (string-match rexp string
  15733. (if (and notfirst
  15734. (= start (match-beginning 0))
  15735. (< start (length string)))
  15736. (1+ start) start))
  15737. (< (match-beginning 0) (length string)))
  15738. (setq notfirst t)
  15739. (or (eq (match-beginning 0) 0)
  15740. (and (eq (match-beginning 0) (match-end 0))
  15741. (eq (match-beginning 0) start))
  15742. (setq list
  15743. (cons (substring string start (match-beginning 0))
  15744. list)))
  15745. (setq start (match-end 0)))
  15746. (or (eq start (length string))
  15747. (setq list
  15748. (cons (substring string start)
  15749. list)))
  15750. (nreverse list)))
  15751. (defun org-quote-vert (s)
  15752. "Replace \"|\" with \"\\vert\"."
  15753. (while (string-match "|" s)
  15754. (setq s (replace-match "\\vert" t t s)))
  15755. s)
  15756. (defun org-uuidgen-p (s)
  15757. "Is S an ID created by UUIDGEN?"
  15758. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  15759. (defun org-context ()
  15760. "Return a list of contexts of the current cursor position.
  15761. If several contexts apply, all are returned.
  15762. Each context entry is a list with a symbol naming the context, and
  15763. two positions indicating start and end of the context. Possible
  15764. contexts are:
  15765. :headline anywhere in a headline
  15766. :headline-stars on the leading stars in a headline
  15767. :todo-keyword on a TODO keyword (including DONE) in a headline
  15768. :tags on the TAGS in a headline
  15769. :priority on the priority cookie in a headline
  15770. :item on the first line of a plain list item
  15771. :item-bullet on the bullet/number of a plain list item
  15772. :checkbox on the checkbox in a plain list item
  15773. :table in an org-mode table
  15774. :table-special on a special filed in a table
  15775. :table-table in a table.el table
  15776. :link on a hyperlink
  15777. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15778. :target on a <<target>>
  15779. :radio-target on a <<<radio-target>>>
  15780. :latex-fragment on a LaTeX fragment
  15781. :latex-preview on a LaTeX fragment with overlayed preview image
  15782. This function expects the position to be visible because it uses font-lock
  15783. faces as a help to recognize the following contexts: :table-special, :link,
  15784. and :keyword."
  15785. (let* ((f (get-text-property (point) 'face))
  15786. (faces (if (listp f) f (list f)))
  15787. (p (point)) clist o)
  15788. ;; First the large context
  15789. (cond
  15790. ((org-on-heading-p t)
  15791. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15792. (when (progn
  15793. (beginning-of-line 1)
  15794. (looking-at org-todo-line-tags-regexp))
  15795. (push (org-point-in-group p 1 :headline-stars) clist)
  15796. (push (org-point-in-group p 2 :todo-keyword) clist)
  15797. (push (org-point-in-group p 4 :tags) clist))
  15798. (goto-char p)
  15799. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  15800. (if (looking-at "\\[#[A-Z0-9]\\]")
  15801. (push (org-point-in-group p 0 :priority) clist)))
  15802. ((org-at-item-p)
  15803. (push (org-point-in-group p 2 :item-bullet) clist)
  15804. (push (list :item (point-at-bol)
  15805. (save-excursion (org-end-of-item) (point)))
  15806. clist)
  15807. (and (org-at-item-checkbox-p)
  15808. (push (org-point-in-group p 0 :checkbox) clist)))
  15809. ((org-at-table-p)
  15810. (push (list :table (org-table-begin) (org-table-end)) clist)
  15811. (if (memq 'org-formula faces)
  15812. (push (list :table-special
  15813. (previous-single-property-change p 'face)
  15814. (next-single-property-change p 'face)) clist)))
  15815. ((org-at-table-p 'any)
  15816. (push (list :table-table) clist)))
  15817. (goto-char p)
  15818. ;; Now the small context
  15819. (cond
  15820. ((org-at-timestamp-p)
  15821. (push (org-point-in-group p 0 :timestamp) clist))
  15822. ((memq 'org-link faces)
  15823. (push (list :link
  15824. (previous-single-property-change p 'face)
  15825. (next-single-property-change p 'face)) clist))
  15826. ((memq 'org-special-keyword faces)
  15827. (push (list :keyword
  15828. (previous-single-property-change p 'face)
  15829. (next-single-property-change p 'face)) clist))
  15830. ((org-on-target-p)
  15831. (push (org-point-in-group p 0 :target) clist)
  15832. (goto-char (1- (match-beginning 0)))
  15833. (if (looking-at org-radio-target-regexp)
  15834. (push (org-point-in-group p 0 :radio-target) clist))
  15835. (goto-char p))
  15836. ((setq o (car (delq nil
  15837. (mapcar
  15838. (lambda (x)
  15839. (if (memq x org-latex-fragment-image-overlays) x))
  15840. (org-overlays-at (point))))))
  15841. (push (list :latex-fragment
  15842. (org-overlay-start o) (org-overlay-end o)) clist)
  15843. (push (list :latex-preview
  15844. (org-overlay-start o) (org-overlay-end o)) clist))
  15845. ((org-inside-LaTeX-fragment-p)
  15846. ;; FIXME: positions wrong.
  15847. (push (list :latex-fragment (point) (point)) clist)))
  15848. (setq clist (nreverse (delq nil clist)))
  15849. clist))
  15850. ;; FIXME: Compare with at-regexp-p Do we need both?
  15851. (defun org-in-regexp (re &optional nlines visually)
  15852. "Check if point is inside a match of regexp.
  15853. Normally only the current line is checked, but you can include NLINES extra
  15854. lines both before and after point into the search.
  15855. If VISUALLY is set, require that the cursor is not after the match but
  15856. really on, so that the block visually is on the match."
  15857. (catch 'exit
  15858. (let ((pos (point))
  15859. (eol (point-at-eol (+ 1 (or nlines 0))))
  15860. (inc (if visually 1 0)))
  15861. (save-excursion
  15862. (beginning-of-line (- 1 (or nlines 0)))
  15863. (while (re-search-forward re eol t)
  15864. (if (and (<= (match-beginning 0) pos)
  15865. (>= (+ inc (match-end 0)) pos))
  15866. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  15867. (defun org-at-regexp-p (regexp)
  15868. "Is point inside a match of REGEXP in the current line?"
  15869. (catch 'exit
  15870. (save-excursion
  15871. (let ((pos (point)) (end (point-at-eol)))
  15872. (beginning-of-line 1)
  15873. (while (re-search-forward regexp end t)
  15874. (if (and (<= (match-beginning 0) pos)
  15875. (>= (match-end 0) pos))
  15876. (throw 'exit t)))
  15877. nil))))
  15878. (defun org-in-regexps-block-p (start-re end-re)
  15879. "Returns t if the current point is between matches of START-RE and END-RE.
  15880. This will also return to if point is on one of the two matches."
  15881. (interactive)
  15882. (let ((p (point)))
  15883. (save-excursion
  15884. (and (or (org-at-regexp-p start-re)
  15885. (re-search-backward start-re nil t))
  15886. (re-search-forward end-re nil t)
  15887. (>= (point) p)))))
  15888. (defun org-occur-in-agenda-files (regexp &optional nlines)
  15889. "Call `multi-occur' with buffers for all agenda files."
  15890. (interactive "sOrg-files matching: \np")
  15891. (let* ((files (org-agenda-files))
  15892. (tnames (mapcar 'file-truename files))
  15893. (extra org-agenda-text-search-extra-files)
  15894. f)
  15895. (when (eq (car extra) 'agenda-archives)
  15896. (setq extra (cdr extra))
  15897. (setq files (org-add-archive-files files)))
  15898. (while (setq f (pop extra))
  15899. (unless (member (file-truename f) tnames)
  15900. (add-to-list 'files f 'append)
  15901. (add-to-list 'tnames (file-truename f) 'append)))
  15902. (multi-occur
  15903. (mapcar (lambda (x)
  15904. (with-current-buffer
  15905. (or (get-file-buffer x) (find-file-noselect x))
  15906. (widen)
  15907. (current-buffer)))
  15908. files)
  15909. regexp)))
  15910. (if (boundp 'occur-mode-find-occurrence-hook)
  15911. ;; Emacs 23
  15912. (add-hook 'occur-mode-find-occurrence-hook
  15913. (lambda ()
  15914. (when (org-mode-p)
  15915. (org-reveal))))
  15916. ;; Emacs 22
  15917. (defadvice occur-mode-goto-occurrence
  15918. (after org-occur-reveal activate)
  15919. (and (org-mode-p) (org-reveal)))
  15920. (defadvice occur-mode-goto-occurrence-other-window
  15921. (after org-occur-reveal activate)
  15922. (and (org-mode-p) (org-reveal)))
  15923. (defadvice occur-mode-display-occurrence
  15924. (after org-occur-reveal activate)
  15925. (when (org-mode-p)
  15926. (let ((pos (occur-mode-find-occurrence)))
  15927. (with-current-buffer (marker-buffer pos)
  15928. (save-excursion
  15929. (goto-char pos)
  15930. (org-reveal)))))))
  15931. (defun org-occur-link-in-agenda-files ()
  15932. "Create a link and search for it in the agendas.
  15933. The link is not stored in `org-stored-links', it is just created
  15934. for the search purpose."
  15935. (interactive)
  15936. (let ((link (condition-case nil
  15937. (org-store-link nil)
  15938. (error "Unable to create a link to here"))))
  15939. (org-occur-in-agenda-files (regexp-quote link))))
  15940. (defun org-uniquify (list)
  15941. "Remove duplicate elements from LIST."
  15942. (let (res)
  15943. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  15944. res))
  15945. (defun org-delete-all (elts list)
  15946. "Remove all elements in ELTS from LIST."
  15947. (while elts
  15948. (setq list (delete (pop elts) list)))
  15949. list)
  15950. (defun org-back-over-empty-lines ()
  15951. "Move backwards over whitespace, to the beginning of the first empty line.
  15952. Returns the number of empty lines passed."
  15953. (let ((pos (point)))
  15954. (skip-chars-backward " \t\n\r")
  15955. (beginning-of-line 2)
  15956. (goto-char (min (point) pos))
  15957. (count-lines (point) pos)))
  15958. (defun org-skip-whitespace ()
  15959. (skip-chars-forward " \t\n\r"))
  15960. (defun org-point-in-group (point group &optional context)
  15961. "Check if POINT is in match-group GROUP.
  15962. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15963. match. If the match group does ot exist or point is not inside it,
  15964. return nil."
  15965. (and (match-beginning group)
  15966. (>= point (match-beginning group))
  15967. (<= point (match-end group))
  15968. (if context
  15969. (list context (match-beginning group) (match-end group))
  15970. t)))
  15971. (defun org-switch-to-buffer-other-window (&rest args)
  15972. "Switch to buffer in a second window on the current frame.
  15973. In particular, do not allow pop-up frames."
  15974. (let (pop-up-frames special-display-buffer-names special-display-regexps
  15975. special-display-function)
  15976. (apply 'switch-to-buffer-other-window args)))
  15977. (defun org-combine-plists (&rest plists)
  15978. "Create a single property list from all plists in PLISTS.
  15979. The process starts by copying the first list, and then setting properties
  15980. from the other lists. Settings in the last list are the most significant
  15981. ones and overrule settings in the other lists."
  15982. (let ((rtn (copy-sequence (pop plists)))
  15983. p v ls)
  15984. (while plists
  15985. (setq ls (pop plists))
  15986. (while ls
  15987. (setq p (pop ls) v (pop ls))
  15988. (setq rtn (plist-put rtn p v))))
  15989. rtn))
  15990. (defun org-move-line-down (arg)
  15991. "Move the current line down. With prefix argument, move it past ARG lines."
  15992. (interactive "p")
  15993. (let ((col (current-column))
  15994. beg end pos)
  15995. (beginning-of-line 1) (setq beg (point))
  15996. (beginning-of-line 2) (setq end (point))
  15997. (beginning-of-line (+ 1 arg))
  15998. (setq pos (move-marker (make-marker) (point)))
  15999. (insert (delete-and-extract-region beg end))
  16000. (goto-char pos)
  16001. (org-move-to-column col)))
  16002. (defun org-move-line-up (arg)
  16003. "Move the current line up. With prefix argument, move it past ARG lines."
  16004. (interactive "p")
  16005. (let ((col (current-column))
  16006. beg end pos)
  16007. (beginning-of-line 1) (setq beg (point))
  16008. (beginning-of-line 2) (setq end (point))
  16009. (beginning-of-line (- arg))
  16010. (setq pos (move-marker (make-marker) (point)))
  16011. (insert (delete-and-extract-region beg end))
  16012. (goto-char pos)
  16013. (org-move-to-column col)))
  16014. (defun org-replace-escapes (string table)
  16015. "Replace %-escapes in STRING with values in TABLE.
  16016. TABLE is an association list with keys like \"%a\" and string values.
  16017. The sequences in STRING may contain normal field width and padding information,
  16018. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  16019. so values can contain further %-escapes if they are define later in TABLE."
  16020. (let ((case-fold-search nil)
  16021. e re rpl)
  16022. (while (setq e (pop table))
  16023. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  16024. (while (string-match re string)
  16025. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  16026. (cdr e)))
  16027. (setq string (replace-match rpl t t string))))
  16028. string))
  16029. (defun org-sublist (list start end)
  16030. "Return a section of LIST, from START to END.
  16031. Counting starts at 1."
  16032. (let (rtn (c start))
  16033. (setq list (nthcdr (1- start) list))
  16034. (while (and list (<= c end))
  16035. (push (pop list) rtn)
  16036. (setq c (1+ c)))
  16037. (nreverse rtn)))
  16038. (defun org-find-base-buffer-visiting (file)
  16039. "Like `find-buffer-visiting' but always return the base buffer and
  16040. not an indirect buffer."
  16041. (let ((buf (or (get-file-buffer file)
  16042. (find-buffer-visiting file))))
  16043. (if buf
  16044. (or (buffer-base-buffer buf) buf)
  16045. nil)))
  16046. (defun org-image-file-name-regexp (&optional extensions)
  16047. "Return regexp matching the file names of images.
  16048. If EXTENSIONS is given, only match these."
  16049. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  16050. (image-file-name-regexp)
  16051. (let ((image-file-name-extensions
  16052. (or extensions
  16053. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16054. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  16055. (concat "\\."
  16056. (regexp-opt (nconc (mapcar 'upcase
  16057. image-file-name-extensions)
  16058. image-file-name-extensions)
  16059. t)
  16060. "\\'"))))
  16061. (defun org-file-image-p (file &optional extensions)
  16062. "Return non-nil if FILE is an image."
  16063. (save-match-data
  16064. (string-match (org-image-file-name-regexp extensions) file)))
  16065. (defun org-get-cursor-date ()
  16066. "Return the date at cursor in as a time.
  16067. This works in the calendar and in the agenda, anywhere else it just
  16068. returns the current time."
  16069. (let (date day defd)
  16070. (cond
  16071. ((eq major-mode 'calendar-mode)
  16072. (setq date (calendar-cursor-to-date)
  16073. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16074. ((eq major-mode 'org-agenda-mode)
  16075. (setq day (get-text-property (point) 'day))
  16076. (if day
  16077. (setq date (calendar-gregorian-from-absolute day)
  16078. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  16079. (nth 2 date))))))
  16080. (or defd (current-time))))
  16081. (defvar org-agenda-action-marker (make-marker)
  16082. "Marker pointing to the entry for the next agenda action.")
  16083. (defun org-mark-entry-for-agenda-action ()
  16084. "Mark the current entry as target of an agenda action.
  16085. Agenda actions are actions executed from the agenda with the key `k',
  16086. which make use of the date at the cursor."
  16087. (interactive)
  16088. (move-marker org-agenda-action-marker
  16089. (save-excursion (org-back-to-heading t) (point))
  16090. (current-buffer))
  16091. (message
  16092. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  16093. ;;; Paragraph filling stuff.
  16094. ;; We want this to be just right, so use the full arsenal.
  16095. (defun org-indent-line-function ()
  16096. "Indent line like previous, but further if previous was headline or item."
  16097. (interactive)
  16098. (let* ((pos (point))
  16099. (itemp (org-at-item-p))
  16100. (case-fold-search t)
  16101. (org-drawer-regexp (or org-drawer-regexp "\000"))
  16102. column bpos bcol tpos tcol bullet btype bullet-type)
  16103. ;; Find the previous relevant line
  16104. (beginning-of-line 1)
  16105. (cond
  16106. ((looking-at "#") (setq column 0))
  16107. ((looking-at "\\*+ ") (setq column 0))
  16108. ((and (looking-at "[ \t]*:END:")
  16109. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  16110. (save-excursion
  16111. (goto-char (1- (match-beginning 1)))
  16112. (setq column (current-column))))
  16113. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  16114. (save-excursion
  16115. (re-search-backward
  16116. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  16117. (setq column (org-get-indentation (match-string 0))))
  16118. (t
  16119. (beginning-of-line 0)
  16120. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  16121. (not (looking-at "[ \t]*:END:"))
  16122. (not (looking-at org-drawer-regexp)))
  16123. (beginning-of-line 0))
  16124. (cond
  16125. ((looking-at "\\*+[ \t]+")
  16126. (if (not org-adapt-indentation)
  16127. (setq column 0)
  16128. (goto-char (match-end 0))
  16129. (setq column (current-column))))
  16130. ((looking-at org-drawer-regexp)
  16131. (goto-char (1- (match-beginning 1)))
  16132. (setq column (current-column)))
  16133. ((looking-at "\\([ \t]*\\):END:")
  16134. (goto-char (match-end 1))
  16135. (setq column (current-column)))
  16136. ((org-in-item-p)
  16137. (org-beginning-of-item)
  16138. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  16139. (setq bpos (match-beginning 1) tpos (match-end 0)
  16140. bcol (progn (goto-char bpos) (current-column))
  16141. tcol (progn (goto-char tpos) (current-column))
  16142. bullet (match-string 1)
  16143. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  16144. (if (> tcol (+ bcol org-description-max-indent))
  16145. (setq tcol (+ bcol 5)))
  16146. (if (not itemp)
  16147. (setq column tcol)
  16148. (goto-char pos)
  16149. (beginning-of-line 1)
  16150. (if (looking-at "\\S-")
  16151. (progn
  16152. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  16153. (setq bullet (match-string 1)
  16154. btype (if (string-match "[0-9]" bullet) "n" bullet))
  16155. (setq column (if (equal btype bullet-type) bcol tcol)))
  16156. (setq column (org-get-indentation)))))
  16157. (t (setq column (org-get-indentation))))))
  16158. (goto-char pos)
  16159. (if (<= (current-column) (current-indentation))
  16160. (org-indent-line-to column)
  16161. (save-excursion (org-indent-line-to column)))
  16162. (setq column (current-column))
  16163. (beginning-of-line 1)
  16164. (if (looking-at
  16165. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  16166. (replace-match (concat (match-string 1)
  16167. (format org-property-format
  16168. (match-string 2) (match-string 3)))
  16169. t t))
  16170. (org-move-to-column column)))
  16171. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  16172. "Variable to store copy of `adaptive-fill-regexp'.
  16173. Since `adaptive-fill-regexp' is set to never match, we need to
  16174. store a backup of its value before entering `org-mode' so that
  16175. the functionality can be provided as a fall-back.")
  16176. (defun org-set-autofill-regexps ()
  16177. (interactive)
  16178. ;; In the paragraph separator we include headlines, because filling
  16179. ;; text in a line directly attached to a headline would otherwise
  16180. ;; fill the headline as well.
  16181. (org-set-local 'comment-start-skip "^#+[ \t]*")
  16182. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  16183. ;; The paragraph starter includes hand-formatted lists.
  16184. (org-set-local
  16185. 'paragraph-start
  16186. (concat
  16187. "\f" "\\|"
  16188. "[ ]*$" "\\|"
  16189. "\\*+ " "\\|"
  16190. "[ \t]*#" "\\|"
  16191. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  16192. "[ \t]*[:|]" "\\|"
  16193. "\\$\\$" "\\|"
  16194. "\\\\\\(begin\\|end\\|[][]\\)"))
  16195. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  16196. ;; But only if the user has not turned off tables or fixed-width regions
  16197. (org-set-local
  16198. 'auto-fill-inhibit-regexp
  16199. (concat "\\*+ \\|#\\+"
  16200. "\\|[ \t]*" org-keyword-time-regexp
  16201. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  16202. (concat
  16203. "\\|[ \t]*["
  16204. (if org-enable-table-editor "|" "")
  16205. (if org-enable-fixed-width-editor ":" "")
  16206. "]"))))
  16207. ;; We use our own fill-paragraph function, to make sure that tables
  16208. ;; and fixed-width regions are not wrapped. That function will pass
  16209. ;; through to `fill-paragraph' when appropriate.
  16210. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  16211. ;; Adaptive filling: To get full control, first make sure that
  16212. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  16213. (unless (local-variable-p 'adaptive-fill-regexp)
  16214. (org-set-local 'org-adaptive-fill-regexp-backup
  16215. adaptive-fill-regexp))
  16216. (org-set-local 'adaptive-fill-regexp "\000")
  16217. (org-set-local 'adaptive-fill-function
  16218. 'org-adaptive-fill-function)
  16219. (org-set-local
  16220. 'align-mode-rules-list
  16221. '((org-in-buffer-settings
  16222. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  16223. (modes . '(org-mode))))))
  16224. (defun org-fill-paragraph (&optional justify)
  16225. "Re-align a table, pass through to fill-paragraph if no table."
  16226. (let ((table-p (org-at-table-p))
  16227. (table.el-p (org-at-table.el-p)))
  16228. (cond ((and (equal (char-after (point-at-bol)) ?*)
  16229. (save-excursion (goto-char (point-at-bol))
  16230. (looking-at outline-regexp)))
  16231. t) ; skip headlines
  16232. (table.el-p t) ; skip table.el tables
  16233. (table-p (org-table-align) t) ; align org-mode tables
  16234. (t nil)))) ; call paragraph-fill
  16235. ;; For reference, this is the default value of adaptive-fill-regexp
  16236. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  16237. (defun org-adaptive-fill-function ()
  16238. "Return a fill prefix for org-mode files.
  16239. In particular, this makes sure hanging paragraphs for hand-formatted lists
  16240. work correctly."
  16241. (cond
  16242. ;; Comment line
  16243. ((looking-at "#[ \t]+")
  16244. (match-string-no-properties 0))
  16245. ;; Description list
  16246. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  16247. (save-excursion
  16248. (if (> (match-end 1) (+ (match-beginning 1)
  16249. org-description-max-indent))
  16250. (goto-char (+ (match-beginning 1) 5))
  16251. (goto-char (match-end 0)))
  16252. (make-string (current-column) ?\ )))
  16253. ;; Ordered or unordered list
  16254. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  16255. (save-excursion
  16256. (goto-char (match-end 0))
  16257. (make-string (current-column) ?\ )))
  16258. ;; Other text
  16259. ((looking-at org-adaptive-fill-regexp-backup)
  16260. (match-string-no-properties 0))))
  16261. ;;; Other stuff.
  16262. (defun org-toggle-fixed-width-section (arg)
  16263. "Toggle the fixed-width export.
  16264. If there is no active region, the QUOTE keyword at the current headline is
  16265. inserted or removed. When present, it causes the text between this headline
  16266. and the next to be exported as fixed-width text, and unmodified.
  16267. If there is an active region, this command adds or removes a colon as the
  16268. first character of this line. If the first character of a line is a colon,
  16269. this line is also exported in fixed-width font."
  16270. (interactive "P")
  16271. (let* ((cc 0)
  16272. (regionp (org-region-active-p))
  16273. (beg (if regionp (region-beginning) (point)))
  16274. (end (if regionp (region-end)))
  16275. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  16276. (case-fold-search nil)
  16277. (re "[ \t]*\\(: \\)")
  16278. off)
  16279. (if regionp
  16280. (save-excursion
  16281. (goto-char beg)
  16282. (setq cc (current-column))
  16283. (beginning-of-line 1)
  16284. (setq off (looking-at re))
  16285. (while (> nlines 0)
  16286. (setq nlines (1- nlines))
  16287. (beginning-of-line 1)
  16288. (cond
  16289. (arg
  16290. (org-move-to-column cc t)
  16291. (insert ": \n")
  16292. (forward-line -1))
  16293. ((and off (looking-at re))
  16294. (replace-match "" t t nil 1))
  16295. ((not off) (org-move-to-column cc t) (insert ": ")))
  16296. (forward-line 1)))
  16297. (save-excursion
  16298. (org-back-to-heading)
  16299. (if (looking-at (concat outline-regexp
  16300. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  16301. (replace-match "" t t nil 1)
  16302. (if (looking-at outline-regexp)
  16303. (progn
  16304. (goto-char (match-end 0))
  16305. (insert org-quote-string " "))))))))
  16306. (defun org-reftex-citation ()
  16307. "Use reftex-citation to insert a citation into the buffer.
  16308. This looks for a line like
  16309. #+BIBLIOGRAPHY: foo plain option:-d
  16310. and derives from it that foo.bib is the bibliography file relevant
  16311. for this document. It then installs the necessary environment for RefTeX
  16312. to work in this buffer and calls `reftex-citation' to insert a citation
  16313. into the buffer.
  16314. Export of such citations to both LaTeX and HTML is handled by the contributed
  16315. package org-exp-bibtex by Taru Karttunen."
  16316. (interactive)
  16317. (let ((reftex-docstruct-symbol 'rds)
  16318. (reftex-cite-format "\\cite{%l}")
  16319. rds bib)
  16320. (save-excursion
  16321. (save-restriction
  16322. (widen)
  16323. (let ((case-fold-search t)
  16324. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  16325. (if (not (save-excursion
  16326. (or (re-search-forward re nil t)
  16327. (re-search-backward re nil t))))
  16328. (error "No bibliography defined in file")
  16329. (setq bib (concat (match-string 1) ".bib")
  16330. rds (list (list 'bib bib)))))))
  16331. (call-interactively 'reftex-citation)))
  16332. ;;;; Functions extending outline functionality
  16333. (defun org-beginning-of-line (&optional arg)
  16334. "Go to the beginning of the current line. If that is invisible, continue
  16335. to a visible line beginning. This makes the function of C-a more intuitive.
  16336. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16337. first attempt, and only move to after the tags when the cursor is already
  16338. beyond the end of the headline."
  16339. (interactive "P")
  16340. (let ((pos (point))
  16341. (special (if (consp org-special-ctrl-a/e)
  16342. (car org-special-ctrl-a/e)
  16343. org-special-ctrl-a/e))
  16344. refpos)
  16345. (if (org-bound-and-true-p line-move-visual)
  16346. (beginning-of-visual-line 1)
  16347. (beginning-of-line 1))
  16348. (if (and arg (fboundp 'move-beginning-of-line))
  16349. (call-interactively 'move-beginning-of-line)
  16350. (if (bobp)
  16351. nil
  16352. (backward-char 1)
  16353. (if (org-invisible-p)
  16354. (while (and (not (bobp)) (org-invisible-p))
  16355. (backward-char 1)
  16356. (beginning-of-line 1))
  16357. (forward-char 1))))
  16358. (when special
  16359. (cond
  16360. ((and (looking-at org-complex-heading-regexp)
  16361. (= (char-after (match-end 1)) ?\ ))
  16362. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  16363. (point-at-eol)))
  16364. (goto-char
  16365. (if (eq special t)
  16366. (cond ((> pos refpos) refpos)
  16367. ((= pos (point)) refpos)
  16368. (t (point)))
  16369. (cond ((> pos (point)) (point))
  16370. ((not (eq last-command this-command)) (point))
  16371. (t refpos)))))
  16372. ((org-at-item-p)
  16373. (goto-char
  16374. (if (eq special t)
  16375. (cond ((> pos (match-end 4)) (match-end 4))
  16376. ((= pos (point)) (match-end 4))
  16377. (t (point)))
  16378. (cond ((> pos (point)) (point))
  16379. ((not (eq last-command this-command)) (point))
  16380. (t (match-end 4))))))))
  16381. (org-no-warnings
  16382. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16383. (defun org-end-of-line (&optional arg)
  16384. "Go to the end of the line.
  16385. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16386. first attempt, and only move to after the tags when the cursor is already
  16387. beyond the end of the headline."
  16388. (interactive "P")
  16389. (let ((special (if (consp org-special-ctrl-a/e)
  16390. (cdr org-special-ctrl-a/e)
  16391. org-special-ctrl-a/e)))
  16392. (if (or (not special)
  16393. (not (org-on-heading-p))
  16394. arg)
  16395. (call-interactively
  16396. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  16397. ((fboundp 'move-end-of-line) 'move-end-of-line)
  16398. (t 'end-of-line)))
  16399. (let ((pos (point)))
  16400. (beginning-of-line 1)
  16401. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  16402. (if (eq special t)
  16403. (if (or (< pos (match-beginning 1))
  16404. (= pos (match-end 0)))
  16405. (goto-char (match-beginning 1))
  16406. (goto-char (match-end 0)))
  16407. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  16408. (goto-char (match-end 0))
  16409. (goto-char (match-beginning 1))))
  16410. (call-interactively (if (fboundp 'move-end-of-line)
  16411. 'move-end-of-line
  16412. 'end-of-line)))))
  16413. (org-no-warnings
  16414. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16415. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  16416. (define-key org-mode-map "\C-e" 'org-end-of-line)
  16417. (define-key org-mode-map [home] 'org-beginning-of-line)
  16418. (define-key org-mode-map [end] 'org-end-of-line)
  16419. (defun org-backward-sentence (&optional arg)
  16420. "Go to beginning of sentence, or beginning of table field.
  16421. This will call `backward-sentence' or `org-table-beginning-of-field',
  16422. depending on context."
  16423. (interactive "P")
  16424. (cond
  16425. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  16426. (t (call-interactively 'backward-sentence))))
  16427. (defun org-forward-sentence (&optional arg)
  16428. "Go to end of sentence, or end of table field.
  16429. This will call `forward-sentence' or `org-table-end-of-field',
  16430. depending on context."
  16431. (interactive "P")
  16432. (cond
  16433. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  16434. (t (call-interactively 'forward-sentence))))
  16435. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  16436. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  16437. (defun org-kill-line (&optional arg)
  16438. "Kill line, to tags or end of line."
  16439. (interactive "P")
  16440. (cond
  16441. ((or (not org-special-ctrl-k)
  16442. (bolp)
  16443. (not (org-on-heading-p)))
  16444. (call-interactively 'kill-line))
  16445. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  16446. (kill-region (point) (match-beginning 1))
  16447. (org-set-tags nil t))
  16448. (t (kill-region (point) (point-at-eol)))))
  16449. (define-key org-mode-map "\C-k" 'org-kill-line)
  16450. (defun org-yank (&optional arg)
  16451. "Yank. If the kill is a subtree, treat it specially.
  16452. This command will look at the current kill and check if is a single
  16453. subtree, or a series of subtrees[1]. If it passes the test, and if the
  16454. cursor is at the beginning of a line or after the stars of a currently
  16455. empty headline, then the yank is handled specially. How exactly depends
  16456. on the value of the following variables, both set by default.
  16457. org-yank-folded-subtrees
  16458. When set, the subtree(s) will be folded after insertion, but only
  16459. if doing so would now swallow text after the yanked text.
  16460. org-yank-adjusted-subtrees
  16461. When set, the subtree will be promoted or demoted in order to
  16462. fit into the local outline tree structure, which means that the level
  16463. will be adjusted so that it becomes the smaller one of the two
  16464. *visible* surrounding headings.
  16465. Any prefix to this command will cause `yank' to be called directly with
  16466. no special treatment. In particular, a simple `C-u' prefix will just
  16467. plainly yank the text as it is.
  16468. \[1] The test checks if the first non-white line is a heading
  16469. and if there are no other headings with fewer stars."
  16470. (interactive "P")
  16471. (org-yank-generic 'yank arg))
  16472. (defun org-yank-generic (command arg)
  16473. "Perform some yank-like command.
  16474. This function implements the behavior described in the `org-yank'
  16475. documentation. However, it has been generalized to work for any
  16476. interactive command with similar behavior."
  16477. ;; pretend to be command COMMAND
  16478. (setq this-command command)
  16479. (if arg
  16480. (call-interactively command)
  16481. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  16482. (and (org-kill-is-subtree-p)
  16483. (or (bolp)
  16484. (and (looking-at "[ \t]*$")
  16485. (string-match
  16486. "\\`\\*+\\'"
  16487. (buffer-substring (point-at-bol) (point)))))))
  16488. swallowp)
  16489. (cond
  16490. ((and subtreep org-yank-folded-subtrees)
  16491. (let ((beg (point))
  16492. end)
  16493. (if (and subtreep org-yank-adjusted-subtrees)
  16494. (org-paste-subtree nil nil 'for-yank)
  16495. (call-interactively command))
  16496. (setq end (point))
  16497. (goto-char beg)
  16498. (when (and (bolp) subtreep
  16499. (not (setq swallowp
  16500. (org-yank-folding-would-swallow-text beg end))))
  16501. (or (looking-at outline-regexp)
  16502. (re-search-forward (concat "^" outline-regexp) end t))
  16503. (while (and (< (point) end) (looking-at outline-regexp))
  16504. (hide-subtree)
  16505. (org-cycle-show-empty-lines 'folded)
  16506. (condition-case nil
  16507. (outline-forward-same-level 1)
  16508. (error (goto-char end)))))
  16509. (when swallowp
  16510. (message
  16511. "Inserted text not folded because that would swallow text"))
  16512. (goto-char end)
  16513. (skip-chars-forward " \t\n\r")
  16514. (beginning-of-line 1)
  16515. (push-mark beg 'nomsg)))
  16516. ((and subtreep org-yank-adjusted-subtrees)
  16517. (let ((beg (point-at-bol)))
  16518. (org-paste-subtree nil nil 'for-yank)
  16519. (push-mark beg 'nomsg)))
  16520. (t
  16521. (call-interactively command))))))
  16522. (defun org-yank-folding-would-swallow-text (beg end)
  16523. "Would hide-subtree at BEG swallow any text after END?"
  16524. (let (level)
  16525. (save-excursion
  16526. (goto-char beg)
  16527. (when (or (looking-at outline-regexp)
  16528. (re-search-forward (concat "^" outline-regexp) end t))
  16529. (setq level (org-outline-level)))
  16530. (goto-char end)
  16531. (skip-chars-forward " \t\r\n\v\f")
  16532. (if (or (eobp)
  16533. (and (bolp) (looking-at org-outline-regexp)
  16534. (<= (org-outline-level) level)))
  16535. nil ; Nothing would be swallowed
  16536. t)))) ; something would swallow
  16537. (define-key org-mode-map "\C-y" 'org-yank)
  16538. (defun org-invisible-p ()
  16539. "Check if point is at a character currently not visible."
  16540. ;; Early versions of noutline don't have `outline-invisible-p'.
  16541. (if (fboundp 'outline-invisible-p)
  16542. (outline-invisible-p)
  16543. (get-char-property (point) 'invisible)))
  16544. (defun org-invisible-p2 ()
  16545. "Check if point is at a character currently not visible."
  16546. (save-excursion
  16547. (if (and (eolp) (not (bobp))) (backward-char 1))
  16548. ;; Early versions of noutline don't have `outline-invisible-p'.
  16549. (if (fboundp 'outline-invisible-p)
  16550. (outline-invisible-p)
  16551. (get-char-property (point) 'invisible))))
  16552. (defun org-back-to-heading (&optional invisible-ok)
  16553. "Call `outline-back-to-heading', but provide a better error message."
  16554. (condition-case nil
  16555. (outline-back-to-heading invisible-ok)
  16556. (error (error "Before first headline at position %d in buffer %s"
  16557. (point) (current-buffer)))))
  16558. (defun org-before-first-heading-p ()
  16559. "Before first heading?"
  16560. (save-excursion
  16561. (null (re-search-backward "^\\*+ " nil t))))
  16562. (defun org-on-heading-p (&optional ignored)
  16563. (outline-on-heading-p t))
  16564. (defun org-at-heading-p (&optional ignored)
  16565. (outline-on-heading-p t))
  16566. (defun org-point-at-end-of-empty-headline ()
  16567. "If point is at the end of an empty headline, return t, else nil.
  16568. If the heading only contains a TODO keyword, it is still still considered
  16569. empty."
  16570. (and (looking-at "[ \t]*$")
  16571. (save-excursion
  16572. (beginning-of-line 1)
  16573. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  16574. "\\)?[ \t]*$")))))
  16575. (defun org-at-heading-or-item-p ()
  16576. (or (org-on-heading-p) (org-at-item-p)))
  16577. (defun org-on-target-p ()
  16578. (or (org-in-regexp org-radio-target-regexp)
  16579. (org-in-regexp org-target-regexp)))
  16580. (defun org-up-heading-all (arg)
  16581. "Move to the heading line of which the present line is a subheading.
  16582. This function considers both visible and invisible heading lines.
  16583. With argument, move up ARG levels."
  16584. (if (fboundp 'outline-up-heading-all)
  16585. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  16586. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  16587. (defun org-up-heading-safe ()
  16588. "Move to the heading line of which the present line is a subheading.
  16589. This version will not throw an error. It will return the level of the
  16590. headline found, or nil if no higher level is found.
  16591. Also, this function will be a lot faster than `outline-up-heading',
  16592. because it relies on stars being the outline starters. This can really
  16593. make a significant difference in outlines with very many siblings."
  16594. (let (start-level re)
  16595. (org-back-to-heading t)
  16596. (setq start-level (funcall outline-level))
  16597. (if (equal start-level 1)
  16598. nil
  16599. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  16600. (if (re-search-backward re nil t)
  16601. (funcall outline-level)))))
  16602. (defun org-first-sibling-p ()
  16603. "Is this heading the first child of its parents?"
  16604. (interactive)
  16605. (let ((re (concat "^" outline-regexp))
  16606. level l)
  16607. (unless (org-at-heading-p t)
  16608. (error "Not at a heading"))
  16609. (setq level (funcall outline-level))
  16610. (save-excursion
  16611. (if (not (re-search-backward re nil t))
  16612. t
  16613. (setq l (funcall outline-level))
  16614. (< l level)))))
  16615. (defun org-goto-sibling (&optional previous)
  16616. "Goto the next sibling, even if it is invisible.
  16617. When PREVIOUS is set, go to the previous sibling instead. Returns t
  16618. when a sibling was found. When none is found, return nil and don't
  16619. move point."
  16620. (let ((fun (if previous 're-search-backward 're-search-forward))
  16621. (pos (point))
  16622. (re (concat "^" outline-regexp))
  16623. level l)
  16624. (when (condition-case nil (org-back-to-heading t) (error nil))
  16625. (setq level (funcall outline-level))
  16626. (catch 'exit
  16627. (or previous (forward-char 1))
  16628. (while (funcall fun re nil t)
  16629. (setq l (funcall outline-level))
  16630. (when (< l level) (goto-char pos) (throw 'exit nil))
  16631. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  16632. (goto-char pos)
  16633. nil))))
  16634. (defun org-show-siblings ()
  16635. "Show all siblings of the current headline."
  16636. (save-excursion
  16637. (while (org-goto-sibling) (org-flag-heading nil)))
  16638. (save-excursion
  16639. (while (org-goto-sibling 'previous)
  16640. (org-flag-heading nil))))
  16641. (defun org-show-hidden-entry ()
  16642. "Show an entry where even the heading is hidden."
  16643. (save-excursion
  16644. (org-show-entry)))
  16645. (defun org-flag-heading (flag &optional entry)
  16646. "Flag the current heading. FLAG non-nil means make invisible.
  16647. When ENTRY is non-nil, show the entire entry."
  16648. (save-excursion
  16649. (org-back-to-heading t)
  16650. ;; Check if we should show the entire entry
  16651. (if entry
  16652. (progn
  16653. (org-show-entry)
  16654. (save-excursion
  16655. (and (outline-next-heading)
  16656. (org-flag-heading nil))))
  16657. (outline-flag-region (max (point-min) (1- (point)))
  16658. (save-excursion (outline-end-of-heading) (point))
  16659. flag))))
  16660. (defun org-get-next-sibling ()
  16661. "Move to next heading of the same level, and return point.
  16662. If there is no such heading, return nil.
  16663. This is like outline-next-sibling, but invisible headings are ok."
  16664. (let ((level (funcall outline-level)))
  16665. (outline-next-heading)
  16666. (while (and (not (eobp)) (> (funcall outline-level) level))
  16667. (outline-next-heading))
  16668. (if (or (eobp) (< (funcall outline-level) level))
  16669. nil
  16670. (point))))
  16671. (defun org-get-last-sibling ()
  16672. "Move to previous heading of the same level, and return point.
  16673. If there is no such heading, return nil."
  16674. (let ((opoint (point))
  16675. (level (funcall outline-level)))
  16676. (outline-previous-heading)
  16677. (when (and (/= (point) opoint) (outline-on-heading-p t))
  16678. (while (and (> (funcall outline-level) level)
  16679. (not (bobp)))
  16680. (outline-previous-heading))
  16681. (if (< (funcall outline-level) level)
  16682. nil
  16683. (point)))))
  16684. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  16685. ;; This contains an exact copy of the original function, but it uses
  16686. ;; `org-back-to-heading', to make it work also in invisible
  16687. ;; trees. And is uses an invisible-OK argument.
  16688. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  16689. ;; Furthermore, when used inside Org, finding the end of a large subtree
  16690. ;; with many children and grandchildren etc, this can be much faster
  16691. ;; than the outline version.
  16692. (org-back-to-heading invisible-OK)
  16693. (let ((first t)
  16694. (level (funcall outline-level)))
  16695. (if (and (org-mode-p) (< level 1000))
  16696. ;; A true heading (not a plain list item), in Org-mode
  16697. ;; This means we can easily find the end by looking
  16698. ;; only for the right number of stars. Using a regexp to do
  16699. ;; this is so much faster than using a Lisp loop.
  16700. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  16701. (forward-char 1)
  16702. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  16703. ;; something else, do it the slow way
  16704. (while (and (not (eobp))
  16705. (or first (> (funcall outline-level) level)))
  16706. (setq first nil)
  16707. (outline-next-heading)))
  16708. (unless to-heading
  16709. (if (memq (preceding-char) '(?\n ?\^M))
  16710. (progn
  16711. ;; Go to end of line before heading
  16712. (forward-char -1)
  16713. (if (memq (preceding-char) '(?\n ?\^M))
  16714. ;; leave blank line before heading
  16715. (forward-char -1))))))
  16716. (point))
  16717. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  16718. "Use Org version in org-mode, for dramatic speed-up."
  16719. (if (eq major-mode 'org-mode)
  16720. (progn
  16721. (org-end-of-subtree nil t)
  16722. (unless (eobp) (backward-char 1)))
  16723. ad-do-it))
  16724. (defun org-forward-same-level (arg &optional invisible-ok)
  16725. "Move forward to the arg'th subheading at same level as this one.
  16726. Stop at the first and last subheadings of a superior heading."
  16727. (interactive "p")
  16728. (org-back-to-heading invisible-ok)
  16729. (org-on-heading-p)
  16730. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16731. (re (format "^\\*\\{1,%d\\} " level))
  16732. l)
  16733. (forward-char 1)
  16734. (while (> arg 0)
  16735. (while (and (re-search-forward re nil 'move)
  16736. (setq l (- (match-end 0) (match-beginning 0) 1))
  16737. (= l level)
  16738. (not invisible-ok)
  16739. (progn (backward-char 1) (org-invisible-p)))
  16740. (if (< l level) (setq arg 1)))
  16741. (setq arg (1- arg)))
  16742. (beginning-of-line 1)))
  16743. (defun org-backward-same-level (arg &optional invisible-ok)
  16744. "Move backward to the arg'th subheading at same level as this one.
  16745. Stop at the first and last subheadings of a superior heading."
  16746. (interactive "p")
  16747. (org-back-to-heading)
  16748. (org-on-heading-p)
  16749. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16750. (re (format "^\\*\\{1,%d\\} " level))
  16751. l)
  16752. (while (> arg 0)
  16753. (while (and (re-search-backward re nil 'move)
  16754. (setq l (- (match-end 0) (match-beginning 0) 1))
  16755. (= l level)
  16756. (not invisible-ok)
  16757. (org-invisible-p))
  16758. (if (< l level) (setq arg 1)))
  16759. (setq arg (1- arg)))))
  16760. (defun org-show-subtree ()
  16761. "Show everything after this heading at deeper levels."
  16762. (outline-flag-region
  16763. (point)
  16764. (save-excursion
  16765. (org-end-of-subtree t t))
  16766. nil))
  16767. (defun org-show-entry ()
  16768. "Show the body directly following this heading.
  16769. Show the heading too, if it is currently invisible."
  16770. (interactive)
  16771. (save-excursion
  16772. (condition-case nil
  16773. (progn
  16774. (org-back-to-heading t)
  16775. (outline-flag-region
  16776. (max (point-min) (1- (point)))
  16777. (save-excursion
  16778. (if (re-search-forward
  16779. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  16780. (match-beginning 1)
  16781. (point-max)))
  16782. nil)
  16783. (org-cycle-hide-drawers 'children))
  16784. (error nil))))
  16785. (defun org-make-options-regexp (kwds &optional extra)
  16786. "Make a regular expression for keyword lines."
  16787. (concat
  16788. "^"
  16789. "#?[ \t]*\\+\\("
  16790. (mapconcat 'regexp-quote kwds "\\|")
  16791. (if extra (concat "\\|" extra))
  16792. "\\):[ \t]*"
  16793. "\\(.*\\)"))
  16794. ;; Make isearch reveal the necessary context
  16795. (defun org-isearch-end ()
  16796. "Reveal context after isearch exits."
  16797. (when isearch-success ; only if search was successful
  16798. (if (featurep 'xemacs)
  16799. ;; Under XEmacs, the hook is run in the correct place,
  16800. ;; we directly show the context.
  16801. (org-show-context 'isearch)
  16802. ;; In Emacs the hook runs *before* restoring the overlays.
  16803. ;; So we have to use a one-time post-command-hook to do this.
  16804. ;; (Emacs 22 has a special variable, see function `org-mode')
  16805. (unless (and (boundp 'isearch-mode-end-hook-quit)
  16806. isearch-mode-end-hook-quit)
  16807. ;; Only when the isearch was not quitted.
  16808. (org-add-hook 'post-command-hook 'org-isearch-post-command
  16809. 'append 'local)))))
  16810. (defun org-isearch-post-command ()
  16811. "Remove self from hook, and show context."
  16812. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  16813. (org-show-context 'isearch))
  16814. ;;;; Integration with and fixes for other packages
  16815. ;;; Imenu support
  16816. (defvar org-imenu-markers nil
  16817. "All markers currently used by Imenu.")
  16818. (make-variable-buffer-local 'org-imenu-markers)
  16819. (defun org-imenu-new-marker (&optional pos)
  16820. "Return a new marker for use by Imenu, and remember the marker."
  16821. (let ((m (make-marker)))
  16822. (move-marker m (or pos (point)))
  16823. (push m org-imenu-markers)
  16824. m))
  16825. (defun org-imenu-get-tree ()
  16826. "Produce the index for Imenu."
  16827. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  16828. (setq org-imenu-markers nil)
  16829. (let* ((n org-imenu-depth)
  16830. (re (concat "^" outline-regexp))
  16831. (subs (make-vector (1+ n) nil))
  16832. (last-level 0)
  16833. m level head)
  16834. (save-excursion
  16835. (save-restriction
  16836. (widen)
  16837. (goto-char (point-max))
  16838. (while (re-search-backward re nil t)
  16839. (setq level (org-reduced-level (funcall outline-level)))
  16840. (when (<= level n)
  16841. (looking-at org-complex-heading-regexp)
  16842. (setq head (org-link-display-format
  16843. (org-match-string-no-properties 4))
  16844. m (org-imenu-new-marker))
  16845. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  16846. (if (>= level last-level)
  16847. (push (cons head m) (aref subs level))
  16848. (push (cons head (aref subs (1+ level))) (aref subs level))
  16849. (loop for i from (1+ level) to n do (aset subs i nil)))
  16850. (setq last-level level)))))
  16851. (aref subs 1)))
  16852. (eval-after-load "imenu"
  16853. '(progn
  16854. (add-hook 'imenu-after-jump-hook
  16855. (lambda ()
  16856. (if (eq major-mode 'org-mode)
  16857. (org-show-context 'org-goto))))))
  16858. (defun org-link-display-format (link)
  16859. "Replace a link with either the description, or the link target
  16860. if no description is present"
  16861. (save-match-data
  16862. (if (string-match org-bracket-link-analytic-regexp link)
  16863. (replace-match (if (match-end 5)
  16864. (match-string 5 link)
  16865. (concat (match-string 1 link)
  16866. (match-string 3 link)))
  16867. nil t link)
  16868. link)))
  16869. ;; Speedbar support
  16870. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  16871. "Overlay marking the agenda restriction line in speedbar.")
  16872. (org-overlay-put org-speedbar-restriction-lock-overlay
  16873. 'face 'org-agenda-restriction-lock)
  16874. (org-overlay-put org-speedbar-restriction-lock-overlay
  16875. 'help-echo "Agendas are currently limited to this item.")
  16876. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  16877. (defun org-speedbar-set-agenda-restriction ()
  16878. "Restrict future agenda commands to the location at point in speedbar.
  16879. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  16880. (interactive)
  16881. (require 'org-agenda)
  16882. (let (p m tp np dir txt)
  16883. (cond
  16884. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16885. 'org-imenu t))
  16886. (setq m (get-text-property p 'org-imenu-marker))
  16887. (with-current-buffer (marker-buffer m)
  16888. (goto-char m)
  16889. (org-agenda-set-restriction-lock 'subtree)))
  16890. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16891. 'speedbar-function 'speedbar-find-file))
  16892. (setq tp (previous-single-property-change
  16893. (1+ p) 'speedbar-function)
  16894. np (next-single-property-change
  16895. tp 'speedbar-function)
  16896. dir (speedbar-line-directory)
  16897. txt (buffer-substring-no-properties (or tp (point-min))
  16898. (or np (point-max))))
  16899. (with-current-buffer (find-file-noselect
  16900. (let ((default-directory dir))
  16901. (expand-file-name txt)))
  16902. (unless (org-mode-p)
  16903. (error "Cannot restrict to non-Org-mode file"))
  16904. (org-agenda-set-restriction-lock 'file)))
  16905. (t (error "Don't know how to restrict Org-mode's agenda")))
  16906. (org-move-overlay org-speedbar-restriction-lock-overlay
  16907. (point-at-bol) (point-at-eol))
  16908. (setq current-prefix-arg nil)
  16909. (org-agenda-maybe-redo)))
  16910. (eval-after-load "speedbar"
  16911. '(progn
  16912. (speedbar-add-supported-extension ".org")
  16913. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  16914. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  16915. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  16916. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16917. (add-hook 'speedbar-visiting-tag-hook
  16918. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  16919. ;;; Fixes and Hacks for problems with other packages
  16920. ;; Make flyspell not check words in links, to not mess up our keymap
  16921. (defun org-mode-flyspell-verify ()
  16922. "Don't let flyspell put overlays at active buttons."
  16923. (and (not (get-text-property (point) 'keymap))
  16924. (not (get-text-property (point) 'org-no-flyspell))))
  16925. (defun org-remove-flyspell-overlays-in (beg end)
  16926. "Remove flyspell overlays in region."
  16927. (and (org-bound-and-true-p flyspell-mode)
  16928. (fboundp 'flyspell-delete-region-overlays)
  16929. (flyspell-delete-region-overlays beg end))
  16930. (add-text-properties beg end '(org-no-flyspell t)))
  16931. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  16932. (eval-after-load "bookmark"
  16933. '(if (boundp 'bookmark-after-jump-hook)
  16934. ;; We can use the hook
  16935. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  16936. ;; Hook not available, use advice
  16937. (defadvice bookmark-jump (after org-make-visible activate)
  16938. "Make the position visible."
  16939. (org-bookmark-jump-unhide))))
  16940. ;; Make sure saveplace shows the location if it was hidden
  16941. (eval-after-load "saveplace"
  16942. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  16943. "Make the position visible."
  16944. (org-bookmark-jump-unhide)))
  16945. ;; Make sure ecb shows the location if it was hidden
  16946. (eval-after-load "ecb"
  16947. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  16948. "Make hierarchy visible when jumping into location from ECB tree buffer."
  16949. (if (eq major-mode 'org-mode)
  16950. (org-show-context))))
  16951. (defun org-bookmark-jump-unhide ()
  16952. "Unhide the current position, to show the bookmark location."
  16953. (and (org-mode-p)
  16954. (or (org-invisible-p)
  16955. (save-excursion (goto-char (max (point-min) (1- (point))))
  16956. (org-invisible-p)))
  16957. (org-show-context 'bookmark-jump)))
  16958. ;; Make session.el ignore our circular variable
  16959. (eval-after-load "session"
  16960. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  16961. ;;;; Experimental code
  16962. (defun org-closed-in-range ()
  16963. "Sparse tree of items closed in a certain time range.
  16964. Still experimental, may disappear in the future."
  16965. (interactive)
  16966. ;; Get the time interval from the user.
  16967. (let* ((time1 (org-float-time
  16968. (org-read-date nil 'to-time nil "Starting date: ")))
  16969. (time2 (org-float-time
  16970. (org-read-date nil 'to-time nil "End date:")))
  16971. ;; callback function
  16972. (callback (lambda ()
  16973. (let ((time
  16974. (org-float-time
  16975. (apply 'encode-time
  16976. (org-parse-time-string
  16977. (match-string 1))))))
  16978. ;; check if time in interval
  16979. (and (>= time time1) (<= time time2))))))
  16980. ;; make tree, check each match with the callback
  16981. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  16982. ;;;; Finish up
  16983. (provide 'org)
  16984. (run-hooks 'org-load-hook)
  16985. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  16986. ;;; org.el ends here