org.el 690 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497
  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, 2010
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 6.35g
  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 "6.35g"
  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 name. For backward
  1279. compatibility, this can also be a string with only alphanumeric
  1280. characters, which is then interpreted as an extension.
  1281. `directory' Matches a directory
  1282. `remote' Matches a remote file, accessible through tramp or efs.
  1283. Remote files most likely should be visited through Emacs
  1284. because external applications cannot handle such paths.
  1285. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1286. so all files Emacs knows how to handle. Using this with
  1287. command `emacs' will open most files in Emacs. Beware that this
  1288. will also open html files inside Emacs, unless you add
  1289. (\"html\" . default) to the list as well.
  1290. t Default for files not matched by any of the other options.
  1291. `system' The system command to open files, like `open' on Windows
  1292. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1293. that will be selected if you call `C-c C-o' with a double
  1294. `C-u C-u' prefix.
  1295. Possible values for the command are:
  1296. `emacs' The file will be visited by the current Emacs process.
  1297. `default' Use the default application for this file type, which is the
  1298. association for t in the list, most likely in the system-specific
  1299. part.
  1300. This can be used to overrule an unwanted setting in the
  1301. system-specific variable.
  1302. `system' Use the system command for opening files, like \"open\".
  1303. This command is specified by the entry whose car is `system'.
  1304. Most likely, the system-specific version of this variable
  1305. does define this command, but you can overrule/replace it
  1306. here.
  1307. string A command to be executed by a shell; %s will be replaced
  1308. by the path to the file.
  1309. sexp A Lisp form which will be evaluated. The file path will
  1310. be available in the Lisp variable `file'.
  1311. For more examples, see the system specific constants
  1312. `org-file-apps-defaults-macosx'
  1313. `org-file-apps-defaults-windowsnt'
  1314. `org-file-apps-defaults-gnu'."
  1315. :group 'org-link-follow
  1316. :type '(repeat
  1317. (cons (choice :value ""
  1318. (string :tag "Extension")
  1319. (const :tag "System command to open files" system)
  1320. (const :tag "Default for unrecognized files" t)
  1321. (const :tag "Remote file" remote)
  1322. (const :tag "Links to a directory" directory)
  1323. (const :tag "Any files that have Emacs modes"
  1324. auto-mode))
  1325. (choice :value ""
  1326. (const :tag "Visit with Emacs" emacs)
  1327. (const :tag "Use default" default)
  1328. (const :tag "Use the system command" system)
  1329. (string :tag "Command")
  1330. (sexp :tag "Lisp form")))))
  1331. (defgroup org-refile nil
  1332. "Options concerning refiling entries in Org-mode."
  1333. :tag "Org Refile"
  1334. :group 'org)
  1335. (defcustom org-directory "~/org"
  1336. "Directory with org files.
  1337. This is just a default location to look for Org files. There is no need
  1338. at all to put your files into this directory. It is only used in the
  1339. following situations:
  1340. 1. When a remember template specifies a target file that is not an
  1341. absolute path. The path will then be interpreted relative to
  1342. `org-directory'
  1343. 2. When a remember note is filed away in an interactive way (when exiting the
  1344. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1345. with `org-directory' as the default path."
  1346. :group 'org-refile
  1347. :group 'org-remember
  1348. :type 'directory)
  1349. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1350. "Default target for storing notes.
  1351. Used by the hooks for remember.el. This can be a string, or nil to mean
  1352. the value of `remember-data-file'.
  1353. You can set this on a per-template basis with the variable
  1354. `org-remember-templates'."
  1355. :group 'org-refile
  1356. :group 'org-remember
  1357. :type '(choice
  1358. (const :tag "Default from remember-data-file" nil)
  1359. file))
  1360. (defcustom org-goto-interface 'outline
  1361. "The default interface to be used for `org-goto'.
  1362. Allowed values are:
  1363. outline The interface shows an outline of the relevant file
  1364. and the correct heading is found by moving through
  1365. the outline or by searching with incremental search.
  1366. outline-path-completion Headlines in the current buffer are offered via
  1367. completion. This is the interface also used by
  1368. the refile command."
  1369. :group 'org-refile
  1370. :type '(choice
  1371. (const :tag "Outline" outline)
  1372. (const :tag "Outline-path-completion" outline-path-completion)))
  1373. (defcustom org-goto-max-level 5
  1374. "Maximum level to be considered when running org-goto with refile interface."
  1375. :group 'org-refile
  1376. :type 'integer)
  1377. (defcustom org-reverse-note-order nil
  1378. "Non-nil means store new notes at the beginning of a file or entry.
  1379. When nil, new notes will be filed to the end of a file or entry.
  1380. This can also be a list with cons cells of regular expressions that
  1381. are matched against file names, and values."
  1382. :group 'org-remember
  1383. :group 'org-refile
  1384. :type '(choice
  1385. (const :tag "Reverse always" t)
  1386. (const :tag "Reverse never" nil)
  1387. (repeat :tag "By file name regexp"
  1388. (cons regexp boolean))))
  1389. (defcustom org-log-refile nil
  1390. "Information to record when a task is refiled.
  1391. Possible values are:
  1392. nil Don't add anything
  1393. time Add a time stamp to the task
  1394. note Prompt for a note and add it with template `org-log-note-headings'
  1395. This option can also be set with on a per-file-basis with
  1396. #+STARTUP: nologrefile
  1397. #+STARTUP: logrefile
  1398. #+STARTUP: lognoterefile
  1399. You can have local logging settings for a subtree by setting the LOGGING
  1400. property to one or more of these keywords.
  1401. When bulk-refiling from the agenda, the value `note' is forbidden and
  1402. will temporarily be changed to `time'."
  1403. :group 'org-refile
  1404. :group 'org-progress
  1405. :type '(choice
  1406. (const :tag "No logging" nil)
  1407. (const :tag "Record timestamp" time)
  1408. (const :tag "Record timestamp with note." note)))
  1409. (defcustom org-refile-targets nil
  1410. "Targets for refiling entries with \\[org-refile].
  1411. This is list of cons cells. Each cell contains:
  1412. - a specification of the files to be considered, either a list of files,
  1413. or a symbol whose function or variable value will be used to retrieve
  1414. a file name or a list of file names. If you use `org-agenda-files' for
  1415. that, all agenda files will be scanned for targets. Nil means consider
  1416. headings in the current buffer.
  1417. - A specification of how to find candidate refile targets. This may be
  1418. any of:
  1419. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1420. This tag has to be present in all target headlines, inheritance will
  1421. not be considered.
  1422. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1423. todo keyword.
  1424. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1425. headlines that are refiling targets.
  1426. - a cons cell (:level . N). Any headline of level N is considered a target.
  1427. Note that, when `org-odd-levels-only' is set, level corresponds to
  1428. order in hierarchy, not to the number of stars.
  1429. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1430. Note that, when `org-odd-levels-only' is set, level corresponds to
  1431. order in hierarchy, not to the number of stars.
  1432. You can set the variable `org-refile-target-verify-function' to a function
  1433. to verify each headline found by the simple critery above.
  1434. When this variable is nil, all top-level headlines in the current buffer
  1435. are used, equivalent to the value `((nil . (:level . 1))'."
  1436. :group 'org-refile
  1437. :type '(repeat
  1438. (cons
  1439. (choice :value org-agenda-files
  1440. (const :tag "All agenda files" org-agenda-files)
  1441. (const :tag "Current buffer" nil)
  1442. (function) (variable) (file))
  1443. (choice :tag "Identify target headline by"
  1444. (cons :tag "Specific tag" (const :value :tag) (string))
  1445. (cons :tag "TODO keyword" (const :value :todo) (string))
  1446. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1447. (cons :tag "Level number" (const :value :level) (integer))
  1448. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1449. (defcustom org-refile-target-verify-function nil
  1450. "Function to verify if the headline at point should be a refile target.
  1451. The function will be called without arguments, with point at the
  1452. beginning of the headline. It should return t and leave point
  1453. where it is if the headline is a valid target for refiling.
  1454. If the target should not be selected, the function must return nil.
  1455. In addition to this, it may move point to a place from where the search
  1456. should be continued. For example, the function may decide that the entire
  1457. subtree of the current entry should be excluded and move point to the end
  1458. of the subtree."
  1459. :group 'org-refile
  1460. :type 'function)
  1461. (defcustom org-refile-use-outline-path nil
  1462. "Non-nil means provide refile targets as paths.
  1463. So a level 3 headline will be available as level1/level2/level3.
  1464. When the value is `file', also include the file name (without directory)
  1465. into the path. In this case, you can also stop the completion after
  1466. the file name, to get entries inserted as top level in the file.
  1467. When `full-file-path', include the full file path."
  1468. :group 'org-refile
  1469. :type '(choice
  1470. (const :tag "Not" nil)
  1471. (const :tag "Yes" t)
  1472. (const :tag "Start with file name" file)
  1473. (const :tag "Start with full file path" full-file-path)))
  1474. (defcustom org-outline-path-complete-in-steps t
  1475. "Non-nil means complete the outline path in hierarchical steps.
  1476. When Org-mode uses the refile interface to select an outline path
  1477. \(see variable `org-refile-use-outline-path'), the completion of
  1478. the path can be done is a single go, or if can be done in steps down
  1479. the headline hierarchy. Going in steps is probably the best if you
  1480. do not use a special completion package like `ido' or `icicles'.
  1481. However, when using these packages, going in one step can be very
  1482. fast, while still showing the whole path to the entry."
  1483. :group 'org-refile
  1484. :type 'boolean)
  1485. (defcustom org-refile-allow-creating-parent-nodes nil
  1486. "Non-nil means allow to create new nodes as refile targets.
  1487. New nodes are then created by adding \"/new node name\" to the completion
  1488. of an existing node. When the value of this variable is `confirm',
  1489. new node creation must be confirmed by the user (recommended)
  1490. When nil, the completion must match an existing entry.
  1491. Note that, if the new heading is not seen by the criteria
  1492. listed in `org-refile-targets', multiple instances of the same
  1493. heading would be created by trying again to file under the new
  1494. heading."
  1495. :group 'org-refile
  1496. :type '(choice
  1497. (const :tag "Never" nil)
  1498. (const :tag "Always" t)
  1499. (const :tag "Prompt for confirmation" confirm)))
  1500. (defgroup org-todo nil
  1501. "Options concerning TODO items in Org-mode."
  1502. :tag "Org TODO"
  1503. :group 'org)
  1504. (defgroup org-progress nil
  1505. "Options concerning Progress logging in Org-mode."
  1506. :tag "Org Progress"
  1507. :group 'org-time)
  1508. (defvar org-todo-interpretation-widgets
  1509. '(
  1510. (:tag "Sequence (cycling hits every state)" sequence)
  1511. (:tag "Type (cycling directly to DONE)" type))
  1512. "The available interpretation symbols for customizing
  1513. `org-todo-keywords'.
  1514. Interested libraries should add to this list.")
  1515. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1516. "List of TODO entry keyword sequences and their interpretation.
  1517. \\<org-mode-map>This is a list of sequences.
  1518. Each sequence starts with a symbol, either `sequence' or `type',
  1519. indicating if the keywords should be interpreted as a sequence of
  1520. action steps, or as different types of TODO items. The first
  1521. keywords are states requiring action - these states will select a headline
  1522. for inclusion into the global TODO list Org-mode produces. If one of
  1523. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1524. signify that no further action is necessary. If \"|\" is not found,
  1525. the last keyword is treated as the only DONE state of the sequence.
  1526. The command \\[org-todo] cycles an entry through these states, and one
  1527. additional state where no keyword is present. For details about this
  1528. cycling, see the manual.
  1529. TODO keywords and interpretation can also be set on a per-file basis with
  1530. the special #+SEQ_TODO and #+TYP_TODO lines.
  1531. Each keyword can optionally specify a character for fast state selection
  1532. \(in combination with the variable `org-use-fast-todo-selection')
  1533. and specifiers for state change logging, using the same syntax
  1534. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1535. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1536. indicates to record a time stamp each time this state is selected.
  1537. Each keyword may also specify if a timestamp or a note should be
  1538. recorded when entering or leaving the state, by adding additional
  1539. characters in the parenthesis after the keyword. This looks like this:
  1540. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1541. record only the time of the state change. With X and Y being either
  1542. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1543. Y when leaving the state if and only if the *target* state does not
  1544. define X. You may omit any of the fast-selection key or X or /Y,
  1545. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1546. For backward compatibility, this variable may also be just a list
  1547. of keywords - in this case the interpretation (sequence or type) will be
  1548. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1549. :group 'org-todo
  1550. :group 'org-keywords
  1551. :type '(choice
  1552. (repeat :tag "Old syntax, just keywords"
  1553. (string :tag "Keyword"))
  1554. (repeat :tag "New syntax"
  1555. (cons
  1556. (choice
  1557. :tag "Interpretation"
  1558. ;;Quick and dirty way to see
  1559. ;;`org-todo-interpretations'. This takes the
  1560. ;;place of item arguments
  1561. :convert-widget
  1562. (lambda (widget)
  1563. (widget-put widget
  1564. :args (mapcar
  1565. #'(lambda (x)
  1566. (widget-convert
  1567. (cons 'const x)))
  1568. org-todo-interpretation-widgets))
  1569. widget))
  1570. (repeat
  1571. (string :tag "Keyword"))))))
  1572. (defvar org-todo-keywords-1 nil
  1573. "All TODO and DONE keywords active in a buffer.")
  1574. (make-variable-buffer-local 'org-todo-keywords-1)
  1575. (defvar org-todo-keywords-for-agenda nil)
  1576. (defvar org-done-keywords-for-agenda nil)
  1577. (defvar org-drawers-for-agenda nil)
  1578. (defvar org-todo-keyword-alist-for-agenda nil)
  1579. (defvar org-tag-alist-for-agenda nil)
  1580. (defvar org-agenda-contributing-files nil)
  1581. (defvar org-not-done-keywords nil)
  1582. (make-variable-buffer-local 'org-not-done-keywords)
  1583. (defvar org-done-keywords nil)
  1584. (make-variable-buffer-local 'org-done-keywords)
  1585. (defvar org-todo-heads nil)
  1586. (make-variable-buffer-local 'org-todo-heads)
  1587. (defvar org-todo-sets nil)
  1588. (make-variable-buffer-local 'org-todo-sets)
  1589. (defvar org-todo-log-states nil)
  1590. (make-variable-buffer-local 'org-todo-log-states)
  1591. (defvar org-todo-kwd-alist nil)
  1592. (make-variable-buffer-local 'org-todo-kwd-alist)
  1593. (defvar org-todo-key-alist nil)
  1594. (make-variable-buffer-local 'org-todo-key-alist)
  1595. (defvar org-todo-key-trigger nil)
  1596. (make-variable-buffer-local 'org-todo-key-trigger)
  1597. (defcustom org-todo-interpretation 'sequence
  1598. "Controls how TODO keywords are interpreted.
  1599. This variable is in principle obsolete and is only used for
  1600. backward compatibility, if the interpretation of todo keywords is
  1601. not given already in `org-todo-keywords'. See that variable for
  1602. more information."
  1603. :group 'org-todo
  1604. :group 'org-keywords
  1605. :type '(choice (const sequence)
  1606. (const type)))
  1607. (defcustom org-use-fast-todo-selection t
  1608. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1609. This variable describes if and under what circumstances the cycling
  1610. mechanism for TODO keywords will be replaced by a single-key, direct
  1611. selection scheme.
  1612. When nil, fast selection is never used.
  1613. When the symbol `prefix', it will be used when `org-todo' is called with
  1614. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1615. in an agenda buffer.
  1616. When t, fast selection is used by default. In this case, the prefix
  1617. argument forces cycling instead.
  1618. In all cases, the special interface is only used if access keys have actually
  1619. been assigned by the user, i.e. if keywords in the configuration are followed
  1620. by a letter in parenthesis, like TODO(t)."
  1621. :group 'org-todo
  1622. :type '(choice
  1623. (const :tag "Never" nil)
  1624. (const :tag "By default" t)
  1625. (const :tag "Only with C-u C-c C-t" prefix)))
  1626. (defcustom org-provide-todo-statistics t
  1627. "Non-nil means update todo statistics after insert and toggle.
  1628. ALL-HEADLINES means update todo statistics by including headlines
  1629. with no TODO keyword as well, counting them as not done.
  1630. A list of TODO keywords means the same, but skip keywords that are
  1631. not in this list.
  1632. When this is set, todo statistics is updated in the parent of the
  1633. current entry each time a todo state is changed."
  1634. :group 'org-todo
  1635. :type '(choice
  1636. (const :tag "Yes, only for TODO entries" t)
  1637. (const :tag "Yes, including all entries" 'all-headlines)
  1638. (repeat :tag "Yes, for TODOs in this list"
  1639. (string :tag "TODO keyword"))
  1640. (other :tag "No TODO statistics" nil)))
  1641. (defcustom org-hierarchical-todo-statistics t
  1642. "Non-nil means TODO statistics covers just direct children.
  1643. When nil, all entries in the subtree are considered.
  1644. This has only an effect if `org-provide-todo-statistics' is set.
  1645. To set this to nil for only a single subtree, use a COOKIE_DATA
  1646. property and include the word \"recursive\" into the value."
  1647. :group 'org-todo
  1648. :type 'boolean)
  1649. (defcustom org-after-todo-state-change-hook nil
  1650. "Hook which is run after the state of a TODO item was changed.
  1651. The new state (a string with a TODO keyword, or nil) is available in the
  1652. Lisp variable `state'."
  1653. :group 'org-todo
  1654. :type 'hook)
  1655. (defvar org-blocker-hook nil
  1656. "Hook for functions that are allowed to block a state change.
  1657. Each function gets as its single argument a property list, see
  1658. `org-trigger-hook' for more information about this list.
  1659. If any of the functions in this hook returns nil, the state change
  1660. is blocked.")
  1661. (defvar org-trigger-hook nil
  1662. "Hook for functions that are triggered by a state change.
  1663. Each function gets as its single argument a property list with at least
  1664. the following elements:
  1665. (:type type-of-change :position pos-at-entry-start
  1666. :from old-state :to new-state)
  1667. Depending on the type, more properties may be present.
  1668. This mechanism is currently implemented for:
  1669. TODO state changes
  1670. ------------------
  1671. :type todo-state-change
  1672. :from previous state (keyword as a string), or nil, or a symbol
  1673. 'todo' or 'done', to indicate the general type of state.
  1674. :to new state, like in :from")
  1675. (defcustom org-enforce-todo-dependencies nil
  1676. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1677. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1678. be blocked if any prior sibling is not yet done.
  1679. Finally, if the parent is blocked because of ordered siblings of its own,
  1680. the child will also be blocked.
  1681. This variable needs to be set before org.el is loaded, and you need to
  1682. restart Emacs after a change to make the change effective. The only way
  1683. to change is while Emacs is running is through the customize interface."
  1684. :set (lambda (var val)
  1685. (set var val)
  1686. (if val
  1687. (add-hook 'org-blocker-hook
  1688. 'org-block-todo-from-children-or-siblings-or-parent)
  1689. (remove-hook 'org-blocker-hook
  1690. 'org-block-todo-from-children-or-siblings-or-parent)))
  1691. :group 'org-todo
  1692. :type 'boolean)
  1693. (defcustom org-enforce-todo-checkbox-dependencies nil
  1694. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1695. When this is nil, checkboxes have no influence on switching TODO states.
  1696. When non-nil, you first need to check off all check boxes before the TODO
  1697. entry can be switched to DONE.
  1698. This variable needs to be set before org.el is loaded, and you need to
  1699. restart Emacs after a change to make the change effective. The only way
  1700. to change is while Emacs is running is through the customize interface."
  1701. :set (lambda (var val)
  1702. (set var val)
  1703. (if val
  1704. (add-hook 'org-blocker-hook
  1705. 'org-block-todo-from-checkboxes)
  1706. (remove-hook 'org-blocker-hook
  1707. 'org-block-todo-from-checkboxes)))
  1708. :group 'org-todo
  1709. :type 'boolean)
  1710. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1711. "Non-nil means inserting a TODO heading is treated as state change.
  1712. So when the command \\[org-insert-todo-heading] is used, state change
  1713. logging will apply if appropriate. When nil, the new TODO item will
  1714. be inserted directly, and no logging will take place."
  1715. :group 'org-todo
  1716. :type 'boolean)
  1717. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1718. "Non-nil means switching TODO states with S-cursor counts as state change.
  1719. This is the default behavior. However, setting this to nil allows a
  1720. convenient way to select a TODO state and bypass any logging associated
  1721. with that."
  1722. :group 'org-todo
  1723. :type 'boolean)
  1724. (defcustom org-todo-state-tags-triggers nil
  1725. "Tag changes that should be triggered by TODO state changes.
  1726. This is a list. Each entry is
  1727. (state-change (tag . flag) .......)
  1728. State-change can be a string with a state, and empty string to indicate the
  1729. state that has no TODO keyword, or it can be one of the symbols `todo'
  1730. or `done', meaning any not-done or done state, respectively."
  1731. :group 'org-todo
  1732. :group 'org-tags
  1733. :type '(repeat
  1734. (cons (choice :tag "When changing to"
  1735. (const :tag "Not-done state" todo)
  1736. (const :tag "Done state" done)
  1737. (string :tag "State"))
  1738. (repeat
  1739. (cons :tag "Tag action"
  1740. (string :tag "Tag")
  1741. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1742. (defcustom org-log-done nil
  1743. "Information to record when a task moves to the DONE state.
  1744. Possible values are:
  1745. nil Don't add anything, just change the keyword
  1746. time Add a time stamp to the task
  1747. note Prompt for a note and add it with template `org-log-note-headings'
  1748. This option can also be set with on a per-file-basis with
  1749. #+STARTUP: nologdone
  1750. #+STARTUP: logdone
  1751. #+STARTUP: lognotedone
  1752. You can have local logging settings for a subtree by setting the LOGGING
  1753. property to one or more of these keywords."
  1754. :group 'org-todo
  1755. :group 'org-progress
  1756. :type '(choice
  1757. (const :tag "No logging" nil)
  1758. (const :tag "Record CLOSED timestamp" time)
  1759. (const :tag "Record CLOSED timestamp with note." note)))
  1760. ;; Normalize old uses of org-log-done.
  1761. (cond
  1762. ((eq org-log-done t) (setq org-log-done 'time))
  1763. ((and (listp org-log-done) (memq 'done org-log-done))
  1764. (setq org-log-done 'note)))
  1765. (defcustom org-log-reschedule nil
  1766. "Information to record when the scheduling date of a tasks is modified.
  1767. Possible values are:
  1768. nil Don't add anything, just change the date
  1769. time Add a time stamp to the task
  1770. note Prompt for a note and add it with template `org-log-note-headings'
  1771. This option can also be set with on a per-file-basis with
  1772. #+STARTUP: nologreschedule
  1773. #+STARTUP: logreschedule
  1774. #+STARTUP: lognotereschedule"
  1775. :group 'org-todo
  1776. :group 'org-progress
  1777. :type '(choice
  1778. (const :tag "No logging" nil)
  1779. (const :tag "Record timestamp" time)
  1780. (const :tag "Record timestamp with note." note)))
  1781. (defcustom org-log-redeadline nil
  1782. "Information to record when the deadline date of a tasks is modified.
  1783. Possible values are:
  1784. nil Don't add anything, just change the date
  1785. time Add a time stamp to the task
  1786. note Prompt for a note and add it with template `org-log-note-headings'
  1787. This option can also be set with on a per-file-basis with
  1788. #+STARTUP: nologredeadline
  1789. #+STARTUP: logredeadline
  1790. #+STARTUP: lognoteredeadline
  1791. You can have local logging settings for a subtree by setting the LOGGING
  1792. property to one or more of these keywords."
  1793. :group 'org-todo
  1794. :group 'org-progress
  1795. :type '(choice
  1796. (const :tag "No logging" nil)
  1797. (const :tag "Record timestamp" time)
  1798. (const :tag "Record timestamp with note." note)))
  1799. (defcustom org-log-note-clock-out nil
  1800. "Non-nil means record a note when clocking out of an item.
  1801. This can also be configured on a per-file basis by adding one of
  1802. the following lines anywhere in the buffer:
  1803. #+STARTUP: lognoteclock-out
  1804. #+STARTUP: nolognoteclock-out"
  1805. :group 'org-todo
  1806. :group 'org-progress
  1807. :type 'boolean)
  1808. (defcustom org-log-done-with-time t
  1809. "Non-nil means the CLOSED time stamp will contain date and time.
  1810. When nil, only the date will be recorded."
  1811. :group 'org-progress
  1812. :type 'boolean)
  1813. (defcustom org-log-note-headings
  1814. '((done . "CLOSING NOTE %t")
  1815. (state . "State %-12s from %-12S %t")
  1816. (note . "Note taken on %t")
  1817. (reschedule . "Rescheduled from %S on %t")
  1818. (delschedule . "Not scheduled, was %S on %t")
  1819. (redeadline . "New deadline from %S on %t")
  1820. (deldeadline . "Removed deadline, was %S on %t")
  1821. (refile . "Refiled on %t")
  1822. (clock-out . ""))
  1823. "Headings for notes added to entries.
  1824. The value is an alist, with the car being a symbol indicating the note
  1825. context, and the cdr is the heading to be used. The heading may also be the
  1826. empty string.
  1827. %t in the heading will be replaced by a time stamp.
  1828. %s will be replaced by the new TODO state, in double quotes.
  1829. %S will be replaced by the old TODO state, in double quotes.
  1830. %u will be replaced by the user name.
  1831. %U will be replaced by the full user name.
  1832. In fact, it is not a good idea to change the `state' entry, because
  1833. agenda log mode depends on the format of these entries."
  1834. :group 'org-todo
  1835. :group 'org-progress
  1836. :type '(list :greedy t
  1837. (cons (const :tag "Heading when closing an item" done) string)
  1838. (cons (const :tag
  1839. "Heading when changing todo state (todo sequence only)"
  1840. state) string)
  1841. (cons (const :tag "Heading when just taking a note" note) string)
  1842. (cons (const :tag "Heading when clocking out" clock-out) string)
  1843. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  1844. (cons (const :tag "Heading when rescheduling" reschedule) string)
  1845. (cons (const :tag "Heading when changing deadline" redeadline) string)
  1846. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  1847. (cons (const :tag "Heading when refiling" refile) string)))
  1848. (unless (assq 'note org-log-note-headings)
  1849. (push '(note . "%t") org-log-note-headings))
  1850. (defcustom org-log-into-drawer nil
  1851. "Non-nil means insert state change notes and time stamps into a drawer.
  1852. When nil, state changes notes will be inserted after the headline and
  1853. any scheduling and clock lines, but not inside a drawer.
  1854. The value of this variable should be the name of the drawer to use.
  1855. LOGBOOK is proposed at the default drawer for this purpose, you can
  1856. also set this to a string to define the drawer of your choice.
  1857. A value of t is also allowed, representing \"LOGBOOK\".
  1858. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1859. will be ignored.
  1860. You can set the property LOG_INTO_DRAWER to overrule this setting for
  1861. a subtree."
  1862. :group 'org-todo
  1863. :group 'org-progress
  1864. :type '(choice
  1865. (const :tag "Not into a drawer" nil)
  1866. (const :tag "LOGBOOK" t)
  1867. (string :tag "Other")))
  1868. (if (fboundp 'defvaralias)
  1869. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1870. (defun org-log-into-drawer ()
  1871. "Return the value of `org-log-into-drawer', but let properties overrule.
  1872. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  1873. used instead of the default value."
  1874. (let ((p (ignore-errors (org-entry-get nil "LOG_INTO_DRAWER" 'inherit))))
  1875. (cond
  1876. ((or (not p) (equal p "nil")) org-log-into-drawer)
  1877. ((equal p "t") "LOGBOOK")
  1878. (t p))))
  1879. (defcustom org-log-state-notes-insert-after-drawers nil
  1880. "Non-nil means insert state change notes after any drawers in entry.
  1881. Only the drawers that *immediately* follow the headline and the
  1882. deadline/scheduled line are skipped.
  1883. When nil, insert notes right after the heading and perhaps the line
  1884. with deadline/scheduling if present.
  1885. This variable will have no effect if `org-log-into-drawer' is
  1886. set."
  1887. :group 'org-todo
  1888. :group 'org-progress
  1889. :type 'boolean)
  1890. (defcustom org-log-states-order-reversed t
  1891. "Non-nil means the latest state note will be directly after heading.
  1892. When nil, the state change notes will be ordered according to time."
  1893. :group 'org-todo
  1894. :group 'org-progress
  1895. :type 'boolean)
  1896. (defcustom org-log-repeat 'time
  1897. "Non-nil means record moving through the DONE state when triggering repeat.
  1898. An auto-repeating task is immediately switched back to TODO when
  1899. marked DONE. If you are not logging state changes (by adding \"@\"
  1900. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  1901. record a closing note, there will be no record of the task moving
  1902. through DONE. This variable forces taking a note anyway.
  1903. nil Don't force a record
  1904. time Record a time stamp
  1905. note Record a note
  1906. This option can also be set with on a per-file-basis with
  1907. #+STARTUP: logrepeat
  1908. #+STARTUP: lognoterepeat
  1909. #+STARTUP: nologrepeat
  1910. You can have local logging settings for a subtree by setting the LOGGING
  1911. property to one or more of these keywords."
  1912. :group 'org-todo
  1913. :group 'org-progress
  1914. :type '(choice
  1915. (const :tag "Don't force a record" nil)
  1916. (const :tag "Force recording the DONE state" time)
  1917. (const :tag "Force recording a note with the DONE state" note)))
  1918. (defgroup org-priorities nil
  1919. "Priorities in Org-mode."
  1920. :tag "Org Priorities"
  1921. :group 'org-todo)
  1922. (defcustom org-enable-priority-commands t
  1923. "Non-nil means priority commands are active.
  1924. When nil, these commands will be disabled, so that you never accidentally
  1925. set a priority."
  1926. :group 'org-priorities
  1927. :type 'boolean)
  1928. (defcustom org-highest-priority ?A
  1929. "The highest priority of TODO items. A character like ?A, ?B etc.
  1930. Must have a smaller ASCII number than `org-lowest-priority'."
  1931. :group 'org-priorities
  1932. :type 'character)
  1933. (defcustom org-lowest-priority ?C
  1934. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1935. Must have a larger ASCII number than `org-highest-priority'."
  1936. :group 'org-priorities
  1937. :type 'character)
  1938. (defcustom org-default-priority ?B
  1939. "The default priority of TODO items.
  1940. This is the priority an item get if no explicit priority is given."
  1941. :group 'org-priorities
  1942. :type 'character)
  1943. (defcustom org-priority-start-cycle-with-default t
  1944. "Non-nil means start with default priority when starting to cycle.
  1945. When this is nil, the first step in the cycle will be (depending on the
  1946. command used) one higher or lower that the default priority."
  1947. :group 'org-priorities
  1948. :type 'boolean)
  1949. (defgroup org-time nil
  1950. "Options concerning time stamps and deadlines in Org-mode."
  1951. :tag "Org Time"
  1952. :group 'org)
  1953. (defcustom org-insert-labeled-timestamps-at-point nil
  1954. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  1955. When nil, these labeled time stamps are forces into the second line of an
  1956. entry, just after the headline. When scheduling from the global TODO list,
  1957. the time stamp will always be forced into the second line."
  1958. :group 'org-time
  1959. :type 'boolean)
  1960. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1961. "Formats for `format-time-string' which are used for time stamps.
  1962. It is not recommended to change this constant.")
  1963. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1964. "Number of minutes to round time stamps to.
  1965. These are two values, the first applies when first creating a time stamp.
  1966. The second applies when changing it with the commands `S-up' and `S-down'.
  1967. When changing the time stamp, this means that it will change in steps
  1968. of N minutes, as given by the second value.
  1969. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1970. numbers should be factors of 60, so for example 5, 10, 15.
  1971. When this is larger than 1, you can still force an exact time-stamp by using
  1972. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1973. and by using a prefix arg to `S-up/down' to specify the exact number
  1974. of minutes to shift."
  1975. :group 'org-time
  1976. :get '(lambda (var) ; Make sure all entries have 5 elements
  1977. (if (integerp (default-value var))
  1978. (list (default-value var) 5)
  1979. (default-value var)))
  1980. :type '(list
  1981. (integer :tag "when inserting times")
  1982. (integer :tag "when modifying times")))
  1983. ;; Normalize old customizations of this variable.
  1984. (when (integerp org-time-stamp-rounding-minutes)
  1985. (setq org-time-stamp-rounding-minutes
  1986. (list org-time-stamp-rounding-minutes
  1987. org-time-stamp-rounding-minutes)))
  1988. (defcustom org-display-custom-times nil
  1989. "Non-nil means overlay custom formats over all time stamps.
  1990. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1991. To turn this on on a per-file basis, insert anywhere in the file:
  1992. #+STARTUP: customtime"
  1993. :group 'org-time
  1994. :set 'set-default
  1995. :type 'sexp)
  1996. (make-variable-buffer-local 'org-display-custom-times)
  1997. (defcustom org-time-stamp-custom-formats
  1998. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1999. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2000. These are overlayed over the default ISO format if the variable
  2001. `org-display-custom-times' is set. Time like %H:%M should be at the
  2002. end of the second format. The custom formats are also honored by export
  2003. commands, if custom time display is turned on at the time of export."
  2004. :group 'org-time
  2005. :type 'sexp)
  2006. (defun org-time-stamp-format (&optional long inactive)
  2007. "Get the right format for a time string."
  2008. (let ((f (if long (cdr org-time-stamp-formats)
  2009. (car org-time-stamp-formats))))
  2010. (if inactive
  2011. (concat "[" (substring f 1 -1) "]")
  2012. f)))
  2013. (defcustom org-time-clocksum-format "%d:%02d"
  2014. "The format string used when creating CLOCKSUM lines, or when
  2015. org-mode generates a time duration."
  2016. :group 'org-time
  2017. :type 'string)
  2018. (defcustom org-time-clocksum-use-fractional nil
  2019. "If non-nil, \\[org-clock-display] uses fractional times.
  2020. org-mode generates a time duration."
  2021. :group 'org-time
  2022. :type 'boolean)
  2023. (defcustom org-time-clocksum-fractional-format "%.2f"
  2024. "The format string used when creating CLOCKSUM lines, or when
  2025. org-mode generates a time duration."
  2026. :group 'org-time
  2027. :type 'string)
  2028. (defcustom org-deadline-warning-days 14
  2029. "No. of days before expiration during which a deadline becomes active.
  2030. This variable governs the display in sparse trees and in the agenda.
  2031. When 0 or negative, it means use this number (the absolute value of it)
  2032. even if a deadline has a different individual lead time specified.
  2033. Custom commands can set this variable in the options section."
  2034. :group 'org-time
  2035. :group 'org-agenda-daily/weekly
  2036. :type 'integer)
  2037. (defcustom org-read-date-prefer-future t
  2038. "Non-nil means assume future for incomplete date input from user.
  2039. This affects the following situations:
  2040. 1. The user gives a month but not a year.
  2041. For example, if it is april and you enter \"feb 2\", this will be read
  2042. as feb 2, *next* year. \"May 5\", however, will be this year.
  2043. 2. The user gives a day, but no month.
  2044. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2045. read this as the third of *next* month. However, if you enter \"17\",
  2046. it will be considered as *this* month.
  2047. If you set this variable to the symbol `time', then also the following
  2048. will work:
  2049. 3. If the user gives a time, but no day. If the time is before now,
  2050. to will be interpreted as tomorrow.
  2051. Currently none of this works for ISO week specifications.
  2052. When this option is nil, the current day, month and year will always be
  2053. used as defaults."
  2054. :group 'org-time
  2055. :type '(choice
  2056. (const :tag "Never" nil)
  2057. (const :tag "Check month and day" t)
  2058. (const :tag "Check month, day, and time" time)))
  2059. (defcustom org-read-date-display-live t
  2060. "Non-nil means display current interpretation of date prompt live.
  2061. This display will be in an overlay, in the minibuffer."
  2062. :group 'org-time
  2063. :type 'boolean)
  2064. (defcustom org-read-date-popup-calendar t
  2065. "Non-nil means pop up a calendar when prompting for a date.
  2066. In the calendar, the date can be selected with mouse-1. However, the
  2067. minibuffer will also be active, and you can simply enter the date as well.
  2068. When nil, only the minibuffer will be available."
  2069. :group 'org-time
  2070. :type 'boolean)
  2071. (if (fboundp 'defvaralias)
  2072. (defvaralias 'org-popup-calendar-for-date-prompt
  2073. 'org-read-date-popup-calendar))
  2074. (defcustom org-read-date-minibuffer-setup-hook nil
  2075. "Hook to be used to set up keys for the date/time interface.
  2076. Add key definitions to `minibuffer-local-map', which will be a temporary
  2077. copy."
  2078. :group 'org-time
  2079. :type 'hook)
  2080. (defcustom org-extend-today-until 0
  2081. "The hour when your day really ends. Must be an integer.
  2082. This has influence for the following applications:
  2083. - When switching the agenda to \"today\". It it is still earlier than
  2084. the time given here, the day recognized as TODAY is actually yesterday.
  2085. - When a date is read from the user and it is still before the time given
  2086. here, the current date and time will be assumed to be yesterday, 23:59.
  2087. Also, timestamps inserted in remember templates follow this rule.
  2088. IMPORTANT: This is a feature whose implementation is and likely will
  2089. remain incomplete. Really, it is only here because past midnight seems to
  2090. be the favorite working time of John Wiegley :-)"
  2091. :group 'org-time
  2092. :type 'integer)
  2093. (defcustom org-edit-timestamp-down-means-later nil
  2094. "Non-nil means S-down will increase the time in a time stamp.
  2095. When nil, S-up will increase."
  2096. :group 'org-time
  2097. :type 'boolean)
  2098. (defcustom org-calendar-follow-timestamp-change t
  2099. "Non-nil means make the calendar window follow timestamp changes.
  2100. When a timestamp is modified and the calendar window is visible, it will be
  2101. moved to the new date."
  2102. :group 'org-time
  2103. :type 'boolean)
  2104. (defgroup org-tags nil
  2105. "Options concerning tags in Org-mode."
  2106. :tag "Org Tags"
  2107. :group 'org)
  2108. (defcustom org-tag-alist nil
  2109. "List of tags allowed in Org-mode files.
  2110. When this list is nil, Org-mode will base TAG input on what is already in the
  2111. buffer.
  2112. The value of this variable is an alist, the car of each entry must be a
  2113. keyword as a string, the cdr may be a character that is used to select
  2114. that tag through the fast-tag-selection interface.
  2115. See the manual for details."
  2116. :group 'org-tags
  2117. :type '(repeat
  2118. (choice
  2119. (cons (string :tag "Tag name")
  2120. (character :tag "Access char"))
  2121. (list :tag "Start radio group"
  2122. (const :startgroup)
  2123. (option (string :tag "Group description")))
  2124. (list :tag "End radio group"
  2125. (const :endgroup)
  2126. (option (string :tag "Group description")))
  2127. (const :tag "New line" (:newline)))))
  2128. (defcustom org-tag-persistent-alist nil
  2129. "List of tags that will always appear in all Org-mode files.
  2130. This is in addition to any in buffer settings or customizations
  2131. of `org-tag-alist'.
  2132. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2133. The value of this variable is an alist, the car of each entry must be a
  2134. keyword as a string, the cdr may be a character that is used to select
  2135. that tag through the fast-tag-selection interface.
  2136. See the manual for details.
  2137. To disable these tags on a per-file basis, insert anywhere in the file:
  2138. #+STARTUP: noptag"
  2139. :group 'org-tags
  2140. :type '(repeat
  2141. (choice
  2142. (cons (string :tag "Tag name")
  2143. (character :tag "Access char"))
  2144. (const :tag "Start radio group" (:startgroup))
  2145. (const :tag "End radio group" (:endgroup))
  2146. (const :tag "New line" (:newline)))))
  2147. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2148. "If non-nil, always offer completion for all tags of all agenda files.
  2149. Instead of customizing this variable directly, you might want to
  2150. set it locally for remember buffers, because there no list of
  2151. tags in that file can be created dynamically (there are none).
  2152. (add-hook 'org-remember-mode-hook
  2153. (lambda ()
  2154. (set (make-local-variable
  2155. 'org-complete-tags-always-offer-all-agenda-tags)
  2156. t)))"
  2157. :group 'org-tags
  2158. :type 'boolean)
  2159. (defvar org-file-tags nil
  2160. "List of tags that can be inherited by all entries in the file.
  2161. The tags will be inherited if the variable `org-use-tag-inheritance'
  2162. says they should be.
  2163. This variable is populated from #+FILETAGS lines.")
  2164. (defcustom org-use-fast-tag-selection 'auto
  2165. "Non-nil means use fast tag selection scheme.
  2166. This is a special interface to select and deselect tags with single keys.
  2167. When nil, fast selection is never used.
  2168. When the symbol `auto', fast selection is used if and only if selection
  2169. characters for tags have been configured, either through the variable
  2170. `org-tag-alist' or through a #+TAGS line in the buffer.
  2171. When t, fast selection is always used and selection keys are assigned
  2172. automatically if necessary."
  2173. :group 'org-tags
  2174. :type '(choice
  2175. (const :tag "Always" t)
  2176. (const :tag "Never" nil)
  2177. (const :tag "When selection characters are configured" 'auto)))
  2178. (defcustom org-fast-tag-selection-single-key nil
  2179. "Non-nil means fast tag selection exits after first change.
  2180. When nil, you have to press RET to exit it.
  2181. During fast tag selection, you can toggle this flag with `C-c'.
  2182. This variable can also have the value `expert'. In this case, the window
  2183. displaying the tags menu is not even shown, until you press C-c again."
  2184. :group 'org-tags
  2185. :type '(choice
  2186. (const :tag "No" nil)
  2187. (const :tag "Yes" t)
  2188. (const :tag "Expert" expert)))
  2189. (defvar org-fast-tag-selection-include-todo nil
  2190. "Non-nil means fast tags selection interface will also offer TODO states.
  2191. This is an undocumented feature, you should not rely on it.")
  2192. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2193. "The column to which tags should be indented in a headline.
  2194. If this number is positive, it specifies the column. If it is negative,
  2195. it means that the tags should be flushright to that column. For example,
  2196. -80 works well for a normal 80 character screen."
  2197. :group 'org-tags
  2198. :type 'integer)
  2199. (defcustom org-auto-align-tags t
  2200. "Non-nil means realign tags after pro/demotion of TODO state change.
  2201. These operations change the length of a headline and therefore shift
  2202. the tags around. With this options turned on, after each such operation
  2203. the tags are again aligned to `org-tags-column'."
  2204. :group 'org-tags
  2205. :type 'boolean)
  2206. (defcustom org-use-tag-inheritance t
  2207. "Non-nil means tags in levels apply also for sublevels.
  2208. When nil, only the tags directly given in a specific line apply there.
  2209. This may also be a list of tags that should be inherited, or a regexp that
  2210. matches tags that should be inherited. Additional control is possible
  2211. with the variable `org-tags-exclude-from-inheritance' which gives an
  2212. explicit list of tags to be excluded from inheritance., even if the value of
  2213. `org-use-tag-inheritance' would select it for inheritance.
  2214. If this option is t, a match early-on in a tree can lead to a large
  2215. number of matches in the subtree when constructing the agenda or creating
  2216. a sparse tree. If you only want to see the first match in a tree during
  2217. a search, check out the variable `org-tags-match-list-sublevels'."
  2218. :group 'org-tags
  2219. :type '(choice
  2220. (const :tag "Not" nil)
  2221. (const :tag "Always" t)
  2222. (repeat :tag "Specific tags" (string :tag "Tag"))
  2223. (regexp :tag "Tags matched by regexp")))
  2224. (defcustom org-tags-exclude-from-inheritance nil
  2225. "List of tags that should never be inherited.
  2226. This is a way to exclude a few tags from inheritance. For way to do
  2227. the opposite, to actively allow inheritance for selected tags,
  2228. see the variable `org-use-tag-inheritance'."
  2229. :group 'org-tags
  2230. :type '(repeat (string :tag "Tag")))
  2231. (defun org-tag-inherit-p (tag)
  2232. "Check if TAG is one that should be inherited."
  2233. (cond
  2234. ((member tag org-tags-exclude-from-inheritance) nil)
  2235. ((eq org-use-tag-inheritance t) t)
  2236. ((not org-use-tag-inheritance) nil)
  2237. ((stringp org-use-tag-inheritance)
  2238. (string-match org-use-tag-inheritance tag))
  2239. ((listp org-use-tag-inheritance)
  2240. (member tag org-use-tag-inheritance))
  2241. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2242. (defcustom org-tags-match-list-sublevels t
  2243. "Non-nil means list also sublevels of headlines matching a search.
  2244. This variable applies to tags/property searches, and also to stuck
  2245. projects because this search is based on a tags match as well.
  2246. When set to the symbol `indented', sublevels are indented with
  2247. leading dots.
  2248. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2249. the sublevels of a headline matching a tag search often also match
  2250. the same search. Listing all of them can create very long lists.
  2251. Setting this variable to nil causes subtrees of a match to be skipped.
  2252. This variable is semi-obsolete and probably should always be true. It
  2253. is better to limit inheritance to certain tags using the variables
  2254. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2255. :group 'org-tags
  2256. :type '(choice
  2257. (const :tag "No, don't list them" nil)
  2258. (const :tag "Yes, do list them" t)
  2259. (const :tag "List them, indented with leading dots" indented)))
  2260. (defcustom org-tags-sort-function nil
  2261. "When set, tags are sorted using this function as a comparator"
  2262. :group 'org-tags
  2263. :type '(choice
  2264. (const :tag "No sorting" nil)
  2265. (const :tag "Alphabetical" string<)
  2266. (const :tag "Reverse alphabetical" string>)
  2267. (function :tag "Custom function" nil)))
  2268. (defvar org-tags-history nil
  2269. "History of minibuffer reads for tags.")
  2270. (defvar org-last-tags-completion-table nil
  2271. "The last used completion table for tags.")
  2272. (defvar org-after-tags-change-hook nil
  2273. "Hook that is run after the tags in a line have changed.")
  2274. (defgroup org-properties nil
  2275. "Options concerning properties in Org-mode."
  2276. :tag "Org Properties"
  2277. :group 'org)
  2278. (defcustom org-property-format "%-10s %s"
  2279. "How property key/value pairs should be formatted by `indent-line'.
  2280. When `indent-line' hits a property definition, it will format the line
  2281. according to this format, mainly to make sure that the values are
  2282. lined-up with respect to each other."
  2283. :group 'org-properties
  2284. :type 'string)
  2285. (defcustom org-use-property-inheritance nil
  2286. "Non-nil means properties apply also for sublevels.
  2287. This setting is chiefly used during property searches. Turning it on can
  2288. cause significant overhead when doing a search, which is why it is not
  2289. on by default.
  2290. When nil, only the properties directly given in the current entry count.
  2291. When t, every property is inherited. The value may also be a list of
  2292. properties that should have inheritance, or a regular expression matching
  2293. properties that should be inherited.
  2294. However, note that some special properties use inheritance under special
  2295. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2296. and the properties ending in \"_ALL\" when they are used as descriptor
  2297. for valid values of a property.
  2298. Note for programmers:
  2299. When querying an entry with `org-entry-get', you can control if inheritance
  2300. should be used. By default, `org-entry-get' looks only at the local
  2301. properties. You can request inheritance by setting the inherit argument
  2302. to t (to force inheritance) or to `selective' (to respect the setting
  2303. in this variable)."
  2304. :group 'org-properties
  2305. :type '(choice
  2306. (const :tag "Not" nil)
  2307. (const :tag "Always" t)
  2308. (repeat :tag "Specific properties" (string :tag "Property"))
  2309. (regexp :tag "Properties matched by regexp")))
  2310. (defun org-property-inherit-p (property)
  2311. "Check if PROPERTY is one that should be inherited."
  2312. (cond
  2313. ((eq org-use-property-inheritance t) t)
  2314. ((not org-use-property-inheritance) nil)
  2315. ((stringp org-use-property-inheritance)
  2316. (string-match org-use-property-inheritance property))
  2317. ((listp org-use-property-inheritance)
  2318. (member property org-use-property-inheritance))
  2319. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2320. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2321. "The default column format, if no other format has been defined.
  2322. This variable can be set on the per-file basis by inserting a line
  2323. #+COLUMNS: %25ITEM ....."
  2324. :group 'org-properties
  2325. :type 'string)
  2326. (defcustom org-columns-ellipses ".."
  2327. "The ellipses to be used when a field in column view is truncated.
  2328. When this is the empty string, as many characters as possible are shown,
  2329. but then there will be no visual indication that the field has been truncated.
  2330. When this is a string of length N, the last N characters of a truncated
  2331. field are replaced by this string. If the column is narrower than the
  2332. ellipses string, only part of the ellipses string will be shown."
  2333. :group 'org-properties
  2334. :type 'string)
  2335. (defcustom org-columns-modify-value-for-display-function nil
  2336. "Function that modifies values for display in column view.
  2337. For example, it can be used to cut out a certain part from a time stamp.
  2338. The function must take 2 arguments:
  2339. column-title The title of the column (*not* the property name)
  2340. value The value that should be modified.
  2341. The function should return the value that should be displayed,
  2342. or nil if the normal value should be used."
  2343. :group 'org-properties
  2344. :type 'function)
  2345. (defcustom org-effort-property "Effort"
  2346. "The property that is being used to keep track of effort estimates.
  2347. Effort estimates given in this property need to have the format H:MM."
  2348. :group 'org-properties
  2349. :group 'org-progress
  2350. :type '(string :tag "Property"))
  2351. (defconst org-global-properties-fixed
  2352. '(("VISIBILITY_ALL" . "folded children content all")
  2353. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2354. "List of property/value pairs that can be inherited by any entry.
  2355. These are fixed values, for the preset properties. The user variable
  2356. that can be used to add to this list is `org-global-properties'.
  2357. The entries in this list are cons cells where the car is a property
  2358. name and cdr is a string with the value. If the value represents
  2359. multiple items like an \"_ALL\" property, separate the items by
  2360. spaces.")
  2361. (defcustom org-global-properties nil
  2362. "List of property/value pairs that can be inherited by any entry.
  2363. This list will be combined with the constant `org-global-properties-fixed'.
  2364. The entries in this list are cons cells where the car is a property
  2365. name and cdr is a string with the value.
  2366. You can set buffer-local values for the same purpose in the variable
  2367. `org-file-properties' this by adding lines like
  2368. #+PROPERTY: NAME VALUE"
  2369. :group 'org-properties
  2370. :type '(repeat
  2371. (cons (string :tag "Property")
  2372. (string :tag "Value"))))
  2373. (defvar org-file-properties nil
  2374. "List of property/value pairs that can be inherited by any entry.
  2375. Valid for the current buffer.
  2376. This variable is populated from #+PROPERTY lines.")
  2377. (make-variable-buffer-local 'org-file-properties)
  2378. (defgroup org-agenda nil
  2379. "Options concerning agenda views in Org-mode."
  2380. :tag "Org Agenda"
  2381. :group 'org)
  2382. (defvar org-category nil
  2383. "Variable used by org files to set a category for agenda display.
  2384. Such files should use a file variable to set it, for example
  2385. # -*- mode: org; org-category: \"ELisp\"
  2386. or contain a special line
  2387. #+CATEGORY: ELisp
  2388. If the file does not specify a category, then file's base name
  2389. is used instead.")
  2390. (make-variable-buffer-local 'org-category)
  2391. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2392. (defcustom org-agenda-files nil
  2393. "The files to be used for agenda display.
  2394. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2395. \\[org-remove-file]. You can also use customize to edit the list.
  2396. If an entry is a directory, all files in that directory that are matched by
  2397. `org-agenda-file-regexp' will be part of the file list.
  2398. If the value of the variable is not a list but a single file name, then
  2399. the list of agenda files is actually stored and maintained in that file, one
  2400. agenda file per line. In this file paths can be given relative to
  2401. `org-directory'. Tilde expansion and environment variable substitution
  2402. are also made."
  2403. :group 'org-agenda
  2404. :type '(choice
  2405. (repeat :tag "List of files and directories" file)
  2406. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2407. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2408. "Regular expression to match files for `org-agenda-files'.
  2409. If any element in the list in that variable contains a directory instead
  2410. of a normal file, all files in that directory that are matched by this
  2411. regular expression will be included."
  2412. :group 'org-agenda
  2413. :type 'regexp)
  2414. (defcustom org-agenda-text-search-extra-files nil
  2415. "List of extra files to be searched by text search commands.
  2416. These files will be search in addition to the agenda files by the
  2417. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2418. Note that these files will only be searched for text search commands,
  2419. not for the other agenda views like todo lists, tag searches or the weekly
  2420. agenda. This variable is intended to list notes and possibly archive files
  2421. that should also be searched by these two commands.
  2422. In fact, if the first element in the list is the symbol `agenda-archives',
  2423. than all archive files of all agenda files will be added to the search
  2424. scope."
  2425. :group 'org-agenda
  2426. :type '(set :greedy t
  2427. (const :tag "Agenda Archives" agenda-archives)
  2428. (repeat :inline t (file))))
  2429. (if (fboundp 'defvaralias)
  2430. (defvaralias 'org-agenda-multi-occur-extra-files
  2431. 'org-agenda-text-search-extra-files))
  2432. (defcustom org-agenda-skip-unavailable-files nil
  2433. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2434. A nil value means to remove them, after a query, from the list."
  2435. :group 'org-agenda
  2436. :type 'boolean)
  2437. (defcustom org-calendar-to-agenda-key [?c]
  2438. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2439. The command `org-calendar-goto-agenda' will be bound to this key. The
  2440. default is the character `c' because then `c' can be used to switch back and
  2441. forth between agenda and calendar."
  2442. :group 'org-agenda
  2443. :type 'sexp)
  2444. (defcustom org-calendar-agenda-action-key [?k]
  2445. "The key to be installed in `calendar-mode-map' for agenda-action.
  2446. The command `org-agenda-action' will be bound to this key. The
  2447. default is the character `k' because we use the same key in the agenda."
  2448. :group 'org-agenda
  2449. :type 'sexp)
  2450. (defcustom org-calendar-insert-diary-entry-key [?i]
  2451. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2452. This option is irrelevant until `org-agenda-diary-file' has been configured
  2453. to point to an Org-mode file. When that is the case, the command
  2454. `org-agenda-diary-entry' will be bound to the key given here, by default
  2455. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2456. if you want to continue doing this, you need to change this to a different
  2457. key."
  2458. :group 'org-agenda
  2459. :type 'sexp)
  2460. (defcustom org-agenda-diary-file 'diary-file
  2461. "File to which to add new entries with the `i' key in agenda and calendar.
  2462. When this is the symbol `diary-file', the functionality in the Emacs
  2463. calendar will be used to add entries to the `diary-file'. But when this
  2464. points to a file, `org-agenda-diary-entry' will be used instead."
  2465. :group 'org-agenda
  2466. :type '(choice
  2467. (const :tag "The standard Emacs diary file" diary-file)
  2468. (file :tag "Special Org file diary entries")))
  2469. (eval-after-load "calendar"
  2470. '(progn
  2471. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2472. 'org-calendar-goto-agenda)
  2473. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2474. 'org-agenda-action)
  2475. (add-hook 'calendar-mode-hook
  2476. (lambda ()
  2477. (unless (eq org-agenda-diary-file 'diary-file)
  2478. (define-key calendar-mode-map
  2479. org-calendar-insert-diary-entry-key
  2480. 'org-agenda-diary-entry))))))
  2481. (defgroup org-latex nil
  2482. "Options for embedding LaTeX code into Org-mode."
  2483. :tag "Org LaTeX"
  2484. :group 'org)
  2485. (defcustom org-format-latex-options
  2486. '(:foreground default :background default :scale 1.0
  2487. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2488. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2489. "Options for creating images from LaTeX fragments.
  2490. This is a property list with the following properties:
  2491. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2492. `default' means use the foreground of the default face.
  2493. :background the background color, or \"Transparent\".
  2494. `default' means use the background of the default face.
  2495. :scale a scaling factor for the size of the images.
  2496. :html-foreground, :html-background, :html-scale
  2497. the same numbers for HTML export.
  2498. :matchers a list indicating which matchers should be used to
  2499. find LaTeX fragments. Valid members of this list are:
  2500. \"begin\" find environments
  2501. \"$1\" find single characters surrounded by $.$
  2502. \"$\" find math expressions surrounded by $...$
  2503. \"$$\" find math expressions surrounded by $$....$$
  2504. \"\\(\" find math expressions surrounded by \\(...\\)
  2505. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2506. :group 'org-latex
  2507. :type 'plist)
  2508. (defcustom org-format-latex-signal-error t
  2509. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2510. When nil, just push out a message."
  2511. :group 'org-latex
  2512. :type 'boolean)
  2513. (defcustom org-format-latex-header "\\documentclass{article}
  2514. \\usepackage[usenames]{color}
  2515. \\usepackage{amsmath}
  2516. \\usepackage[mathscr]{eucal}
  2517. \\pagestyle{empty} % do not remove
  2518. \[PACKAGES]
  2519. \[DEFAULT-PACKAGES]
  2520. % The settings below are copied from fullpage.sty
  2521. \\setlength{\\textwidth}{\\paperwidth}
  2522. \\addtolength{\\textwidth}{-3cm}
  2523. \\setlength{\\oddsidemargin}{1.5cm}
  2524. \\addtolength{\\oddsidemargin}{-2.54cm}
  2525. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2526. \\setlength{\\textheight}{\\paperheight}
  2527. \\addtolength{\\textheight}{-\\headheight}
  2528. \\addtolength{\\textheight}{-\\headsep}
  2529. \\addtolength{\\textheight}{-\\footskip}
  2530. \\addtolength{\\textheight}{-3cm}
  2531. \\setlength{\\topmargin}{1.5cm}
  2532. \\addtolength{\\topmargin}{-2.54cm}"
  2533. "The document header used for processing LaTeX fragments.
  2534. It is imperative that this header make sure that no page number
  2535. appears on the page. The package defined in the variables
  2536. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2537. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2538. will be appended."
  2539. :group 'org-latex
  2540. :type 'string)
  2541. (defvar org-format-latex-header-extra nil)
  2542. ;; The following variables are defined here because is it also used
  2543. ;; when formatting latex fragments. Originally it was part of the
  2544. ;; LaTeX exporter, which is why the name includes "export".
  2545. (defcustom org-export-latex-default-packages-alist
  2546. '(("AUTO" "inputenc")
  2547. ("T1" "fontenc")
  2548. ("" "fixltx2e")
  2549. ("" "graphicx")
  2550. ("" "longtable")
  2551. ("" "float")
  2552. ("" "wrapfig")
  2553. ("" "soul")
  2554. ("" "t1enc")
  2555. ("" "textcomp")
  2556. ("" "marvosym")
  2557. ("" "wasysym")
  2558. ("" "latexsym")
  2559. ("" "amssymb")
  2560. ("" "microtype")
  2561. ("" "hyperref")
  2562. "\\tolerance=1000"
  2563. )
  2564. "Alist of default packages to be inserted in the header.
  2565. Change this only if one of the packages here causes an incompatibility
  2566. with another package you are using.
  2567. The packages in this list are needed by one part or another of Org-mode
  2568. to function properly.
  2569. - inputenc, fontenc, t1enc: for basic font and character selection
  2570. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2571. for interpreting the entities in `org-entities'. You can skip some of these
  2572. packages if you don't use any of the symbols in it.
  2573. - graphicx: for including images
  2574. - float, wrapfig: for figure placement
  2575. - longtable: for long tables
  2576. - hyperref: for cross references
  2577. Therefore you should not modify this variable unless you know what you
  2578. are doing. The one reason to change it anyway is that you might be loading
  2579. some other package that conflicts with one of the default packages.
  2580. Each cell is of the format \( \"options\" \"package\" \)."
  2581. :group 'org-export-latex
  2582. :type '(repeat
  2583. (choice
  2584. (string :tag "A line of LaTeX")
  2585. (list :tag "options/package pair"
  2586. (string :tag "options")
  2587. (string :tag "package")))))
  2588. (defcustom org-export-latex-packages-alist nil
  2589. "Alist of packages to be inserted in every LaTeX the header.
  2590. These will be inserted after `org-export-latex-default-packages-alist'.
  2591. Each cell is of the format \( \"options\" \"package\" \).
  2592. Make sure that you only lis packages here which:
  2593. - you want in every file
  2594. - do not conflict with the default packages in
  2595. `org-export-latex-default-packages-alist'
  2596. - do not conflict with the setup in `org-format-latex-header'."
  2597. :group 'org-export-latex
  2598. :type '(repeat
  2599. (choice
  2600. (string :tag "A line of LaTeX")
  2601. (list :tag "options/package pair"
  2602. (string :tag "options")
  2603. (string :tag "package")))))
  2604. (defgroup org-appearance nil
  2605. "Settings for Org-mode appearance."
  2606. :tag "Org Appearance"
  2607. :group 'org)
  2608. (defcustom org-level-color-stars-only nil
  2609. "Non-nil means fontify only the stars in each headline.
  2610. When nil, the entire headline is fontified.
  2611. Changing it requires restart of `font-lock-mode' to become effective
  2612. also in regions already fontified."
  2613. :group 'org-appearance
  2614. :type 'boolean)
  2615. (defcustom org-hide-leading-stars nil
  2616. "Non-nil means hide the first N-1 stars in a headline.
  2617. This works by using the face `org-hide' for these stars. This
  2618. face is white for a light background, and black for a dark
  2619. background. You may have to customize the face `org-hide' to
  2620. make this work.
  2621. Changing it requires restart of `font-lock-mode' to become effective
  2622. also in regions already fontified.
  2623. You may also set this on a per-file basis by adding one of the following
  2624. lines to the buffer:
  2625. #+STARTUP: hidestars
  2626. #+STARTUP: showstars"
  2627. :group 'org-appearance
  2628. :type 'boolean)
  2629. (defcustom org-hidden-keywords nil
  2630. "List of keywords that should be hidden when typed in the org buffer.
  2631. For example, add #+TITLE to this list in order to make the
  2632. document title appear in the buffer without the initial #+TITLE:
  2633. keyword."
  2634. :group 'org-appearance
  2635. :type '(set (const :tag "#+AUTHOR" author)
  2636. (const :tag "#+DATE" date)
  2637. (const :tag "#+EMAIL" email)
  2638. (const :tag "#+TITLE" title)))
  2639. (defcustom org-fontify-done-headline nil
  2640. "Non-nil means change the face of a headline if it is marked DONE.
  2641. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2642. When this is non-nil, the headline after the keyword is set to the
  2643. `org-headline-done' as an additional indication."
  2644. :group 'org-appearance
  2645. :type 'boolean)
  2646. (defcustom org-fontify-emphasized-text t
  2647. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2648. Changing this variable requires a restart of Emacs to take effect."
  2649. :group 'org-appearance
  2650. :type 'boolean)
  2651. (defcustom org-fontify-whole-heading-line nil
  2652. "Non-nil means fontify the whole line for headings.
  2653. This is useful when setting a background color for the
  2654. org-level-* faces."
  2655. :group 'org-appearance
  2656. :type 'boolean)
  2657. (defcustom org-highlight-latex-fragments-and-specials nil
  2658. "Non-nil means fontify what is treated specially by the exporters."
  2659. :group 'org-appearance
  2660. :type 'boolean)
  2661. (defcustom org-hide-emphasis-markers nil
  2662. "Non-nil mean font-lock should hide the emphasis marker characters."
  2663. :group 'org-appearance
  2664. :type 'boolean)
  2665. (defvar org-emph-re nil
  2666. "Regular expression for matching emphasis.")
  2667. (defvar org-verbatim-re nil
  2668. "Regular expression for matching verbatim text.")
  2669. (defvar org-emphasis-regexp-components) ; defined just below
  2670. (defvar org-emphasis-alist) ; defined just below
  2671. (defun org-set-emph-re (var val)
  2672. "Set variable and compute the emphasis regular expression."
  2673. (set var val)
  2674. (when (and (boundp 'org-emphasis-alist)
  2675. (boundp 'org-emphasis-regexp-components)
  2676. org-emphasis-alist org-emphasis-regexp-components)
  2677. (let* ((e org-emphasis-regexp-components)
  2678. (pre (car e))
  2679. (post (nth 1 e))
  2680. (border (nth 2 e))
  2681. (body (nth 3 e))
  2682. (nl (nth 4 e))
  2683. (body1 (concat body "*?"))
  2684. (markers (mapconcat 'car org-emphasis-alist ""))
  2685. (vmarkers (mapconcat
  2686. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2687. org-emphasis-alist "")))
  2688. ;; make sure special characters appear at the right position in the class
  2689. (if (string-match "\\^" markers)
  2690. (setq markers (concat (replace-match "" t t markers) "^")))
  2691. (if (string-match "-" markers)
  2692. (setq markers (concat (replace-match "" t t markers) "-")))
  2693. (if (string-match "\\^" vmarkers)
  2694. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2695. (if (string-match "-" vmarkers)
  2696. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2697. (if (> nl 0)
  2698. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2699. (int-to-string nl) "\\}")))
  2700. ;; Make the regexp
  2701. (setq org-emph-re
  2702. (concat "\\([" pre "]\\|^\\)"
  2703. "\\("
  2704. "\\([" markers "]\\)"
  2705. "\\("
  2706. "[^" border "]\\|"
  2707. "[^" border "]"
  2708. body1
  2709. "[^" border "]"
  2710. "\\)"
  2711. "\\3\\)"
  2712. "\\([" post "]\\|$\\)"))
  2713. (setq org-verbatim-re
  2714. (concat "\\([" pre "]\\|^\\)"
  2715. "\\("
  2716. "\\([" vmarkers "]\\)"
  2717. "\\("
  2718. "[^" border "]\\|"
  2719. "[^" border "]"
  2720. body1
  2721. "[^" border "]"
  2722. "\\)"
  2723. "\\3\\)"
  2724. "\\([" post "]\\|$\\)")))))
  2725. (defcustom org-emphasis-regexp-components
  2726. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  2727. "Components used to build the regular expression for emphasis.
  2728. This is a list with 6 entries. Terminology: In an emphasis string
  2729. like \" *strong word* \", we call the initial space PREMATCH, the final
  2730. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2731. and \"trong wor\" is the body. The different components in this variable
  2732. specify what is allowed/forbidden in each part:
  2733. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2734. post Chars allowed as postmatch. End of line will be allowed too.
  2735. border The chars *forbidden* as border characters.
  2736. body-regexp A regexp like \".\" to match a body character. Don't use
  2737. non-shy groups here, and don't allow newline here.
  2738. newline The maximum number of newlines allowed in an emphasis exp.
  2739. Use customize to modify this, or restart Emacs after changing it."
  2740. :group 'org-appearance
  2741. :set 'org-set-emph-re
  2742. :type '(list
  2743. (sexp :tag "Allowed chars in pre ")
  2744. (sexp :tag "Allowed chars in post ")
  2745. (sexp :tag "Forbidden chars in border ")
  2746. (sexp :tag "Regexp for body ")
  2747. (integer :tag "number of newlines allowed")
  2748. (option (boolean :tag "Please ignore this button"))))
  2749. (defcustom org-emphasis-alist
  2750. `(("*" bold "<b>" "</b>")
  2751. ("/" italic "<i>" "</i>")
  2752. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2753. ("=" org-code "<code>" "</code>" verbatim)
  2754. ("~" org-verbatim "<code>" "</code>" verbatim)
  2755. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2756. "<del>" "</del>")
  2757. )
  2758. "Special syntax for emphasized text.
  2759. Text starting and ending with a special character will be emphasized, for
  2760. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2761. characters, the face to be used by font-lock for highlighting in Org-mode
  2762. Emacs buffers, and the HTML tags to be used for this.
  2763. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  2764. Use customize to modify this, or restart Emacs after changing it."
  2765. :group 'org-appearance
  2766. :set 'org-set-emph-re
  2767. :type '(repeat
  2768. (list
  2769. (string :tag "Marker character")
  2770. (choice
  2771. (face :tag "Font-lock-face")
  2772. (plist :tag "Face property list"))
  2773. (string :tag "HTML start tag")
  2774. (string :tag "HTML end tag")
  2775. (option (const verbatim)))))
  2776. (defvar org-protecting-blocks
  2777. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  2778. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  2779. This is needed for font-lock setup.")
  2780. ;;; Miscellaneous options
  2781. (defgroup org-completion nil
  2782. "Completion in Org-mode."
  2783. :tag "Org Completion"
  2784. :group 'org)
  2785. (defcustom org-completion-use-ido nil
  2786. "Non-nil means use ido completion wherever possible.
  2787. Note that `ido-mode' must be active for this variable to be relevant.
  2788. If you decide to turn this variable on, you might well want to turn off
  2789. `org-outline-path-complete-in-steps'.
  2790. See also `org-completion-use-iswitchb'."
  2791. :group 'org-completion
  2792. :type 'boolean)
  2793. (defcustom org-completion-use-iswitchb nil
  2794. "Non-nil means use iswitchb completion wherever possible.
  2795. Note that `iswitchb-mode' must be active for this variable to be relevant.
  2796. If you decide to turn this variable on, you might well want to turn off
  2797. `org-outline-path-complete-in-steps'.
  2798. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  2799. :group 'org-completion
  2800. :type 'boolean)
  2801. (defcustom org-completion-fallback-command 'hippie-expand
  2802. "The expansion command called by \\[org-complete] in normal context.
  2803. Normal means no org-mode-specific context."
  2804. :group 'org-completion
  2805. :type 'function)
  2806. ;;; Functions and variables from their packages
  2807. ;; Declared here to avoid compiler warnings
  2808. ;; XEmacs only
  2809. (defvar outline-mode-menu-heading)
  2810. (defvar outline-mode-menu-show)
  2811. (defvar outline-mode-menu-hide)
  2812. (defvar zmacs-regions) ; XEmacs regions
  2813. ;; Emacs only
  2814. (defvar mark-active)
  2815. ;; Various packages
  2816. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2817. (declare-function calendar-forward-day "cal-move" (arg))
  2818. (declare-function calendar-goto-date "cal-move" (date))
  2819. (declare-function calendar-goto-today "cal-move" ())
  2820. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2821. (defvar calc-embedded-close-formula)
  2822. (defvar calc-embedded-open-formula)
  2823. (declare-function cdlatex-tab "ext:cdlatex" ())
  2824. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2825. (defvar font-lock-unfontify-region-function)
  2826. (declare-function iswitchb-read-buffer "iswitchb"
  2827. (prompt &optional default require-match start matches-set))
  2828. (defvar iswitchb-temp-buflist)
  2829. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2830. (defvar org-agenda-tags-todo-honor-ignore-options)
  2831. (declare-function org-agenda-skip "org-agenda" ())
  2832. (declare-function
  2833. org-format-agenda-item "org-agenda"
  2834. (extra txt &optional category tags dotime noprefix remove-re habitp))
  2835. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2836. (declare-function org-agenda-change-all-lines "org-agenda"
  2837. (newhead hdmarker &optional fixface just-this))
  2838. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2839. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2840. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2841. (beg end))
  2842. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2843. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2844. "org-agenda" (&optional end))
  2845. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  2846. (declare-function org-indent-mode "org-indent" (&optional arg))
  2847. (declare-function parse-time-string "parse-time" (string))
  2848. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  2849. (defvar remember-data-file)
  2850. (defvar texmathp-why)
  2851. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2852. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2853. (defvar w3m-current-url)
  2854. (defvar w3m-current-title)
  2855. (defvar org-latex-regexps)
  2856. ;;; Autoload and prepare some org modules
  2857. ;; Some table stuff that needs to be defined here, because it is used
  2858. ;; by the functions setting up org-mode or checking for table context.
  2859. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2860. "Detects an org-type or table-type table.")
  2861. (defconst org-table-line-regexp "^[ \t]*|"
  2862. "Detects an org-type table line.")
  2863. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2864. "Detects an org-type table line.")
  2865. (defconst org-table-hline-regexp "^[ \t]*|-"
  2866. "Detects an org-type table hline.")
  2867. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2868. "Detects a table-type table hline.")
  2869. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2870. "Searching from within a table (any type) this finds the first line
  2871. outside the table.")
  2872. ;; Autoload the functions in org-table.el that are needed by functions here.
  2873. (eval-and-compile
  2874. (org-autoload "org-table"
  2875. '(org-table-align org-table-begin org-table-blank-field
  2876. org-table-convert org-table-convert-region org-table-copy-down
  2877. org-table-copy-region org-table-create
  2878. org-table-create-or-convert-from-region
  2879. org-table-create-with-table.el org-table-current-dline
  2880. org-table-cut-region org-table-delete-column org-table-edit-field
  2881. org-table-edit-formulas org-table-end org-table-eval-formula
  2882. org-table-export org-table-field-info
  2883. org-table-get-stored-formulas org-table-goto-column
  2884. org-table-hline-and-move org-table-import org-table-insert-column
  2885. org-table-insert-hline org-table-insert-row org-table-iterate
  2886. org-table-justify-field-maybe org-table-kill-row
  2887. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2888. org-table-move-column org-table-move-column-left
  2889. org-table-move-column-right org-table-move-row
  2890. org-table-move-row-down org-table-move-row-up
  2891. org-table-next-field org-table-next-row org-table-paste-rectangle
  2892. org-table-previous-field org-table-recalculate
  2893. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2894. org-table-toggle-coordinate-overlays
  2895. org-table-toggle-formula-debugger org-table-wrap-region
  2896. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2897. (defun org-at-table-p (&optional table-type)
  2898. "Return t if the cursor is inside an org-type table.
  2899. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2900. (if org-enable-table-editor
  2901. (save-excursion
  2902. (beginning-of-line 1)
  2903. (looking-at (if table-type org-table-any-line-regexp
  2904. org-table-line-regexp)))
  2905. nil))
  2906. (defsubst org-table-p () (org-at-table-p))
  2907. (defun org-at-table.el-p ()
  2908. "Return t if and only if we are at a table.el table."
  2909. (and (org-at-table-p 'any)
  2910. (save-excursion
  2911. (goto-char (org-table-begin 'any))
  2912. (looking-at org-table1-hline-regexp))))
  2913. (defun org-table-recognize-table.el ()
  2914. "If there is a table.el table nearby, recognize it and move into it."
  2915. (if org-table-tab-recognizes-table.el
  2916. (if (org-at-table.el-p)
  2917. (progn
  2918. (beginning-of-line 1)
  2919. (if (looking-at org-table-dataline-regexp)
  2920. nil
  2921. (if (looking-at org-table1-hline-regexp)
  2922. (progn
  2923. (beginning-of-line 2)
  2924. (if (looking-at org-table-any-border-regexp)
  2925. (beginning-of-line -1)))))
  2926. (if (re-search-forward "|" (org-table-end t) t)
  2927. (progn
  2928. (require 'table)
  2929. (if (table--at-cell-p (point))
  2930. t
  2931. (message "recognizing table.el table...")
  2932. (table-recognize-table)
  2933. (message "recognizing table.el table...done")))
  2934. (error "This should not happen..."))
  2935. t)
  2936. nil)
  2937. nil))
  2938. (defun org-at-table-hline-p ()
  2939. "Return t if the cursor is inside a hline in a table."
  2940. (if org-enable-table-editor
  2941. (save-excursion
  2942. (beginning-of-line 1)
  2943. (looking-at org-table-hline-regexp))
  2944. nil))
  2945. (defvar org-table-clean-did-remove-column nil)
  2946. (defun org-table-map-tables (function)
  2947. "Apply FUNCTION to the start of all tables in the buffer."
  2948. (save-excursion
  2949. (save-restriction
  2950. (widen)
  2951. (goto-char (point-min))
  2952. (while (re-search-forward org-table-any-line-regexp nil t)
  2953. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2954. (beginning-of-line 1)
  2955. (when (looking-at org-table-line-regexp)
  2956. (save-excursion (funcall function))
  2957. (or (looking-at org-table-line-regexp)
  2958. (forward-char 1)))
  2959. (re-search-forward org-table-any-border-regexp nil 1))))
  2960. (message "Mapping tables: done"))
  2961. ;; Declare and autoload functions from org-exp.el & Co
  2962. (declare-function org-default-export-plist "org-exp")
  2963. (declare-function org-infile-export-plist "org-exp")
  2964. (declare-function org-get-current-options "org-exp")
  2965. (eval-and-compile
  2966. (org-autoload "org-exp"
  2967. '(org-export org-export-visible
  2968. org-insert-export-options-template
  2969. org-table-clean-before-export))
  2970. (org-autoload "org-ascii"
  2971. '(org-export-as-ascii org-export-ascii-preprocess
  2972. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  2973. org-export-region-as-ascii))
  2974. (org-autoload "org-latex"
  2975. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  2976. org-replace-region-by-latex org-export-region-as-latex
  2977. org-export-as-latex org-export-as-pdf
  2978. org-export-as-pdf-and-open))
  2979. (org-autoload "org-html"
  2980. '(org-export-as-html-and-open
  2981. org-export-as-html-batch org-export-as-html-to-buffer
  2982. org-replace-region-by-html org-export-region-as-html
  2983. org-export-as-html))
  2984. (org-autoload "org-docbook"
  2985. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  2986. org-replace-region-by-docbook org-export-region-as-docbook
  2987. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  2988. org-export-as-docbook))
  2989. (org-autoload "org-icalendar"
  2990. '(org-export-icalendar-this-file
  2991. org-export-icalendar-all-agenda-files
  2992. org-export-icalendar-combine-agenda-files))
  2993. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  2994. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  2995. ;; Declare and autoload functions from org-agenda.el
  2996. (eval-and-compile
  2997. (org-autoload "org-agenda"
  2998. '(org-agenda org-agenda-list org-search-view
  2999. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3000. org-diary org-agenda-to-appt
  3001. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3002. ;; Autoload org-remember
  3003. (eval-and-compile
  3004. (org-autoload "org-remember"
  3005. '(org-remember-insinuate org-remember-annotation
  3006. org-remember-apply-template org-remember org-remember-handler)))
  3007. ;; Autoload org-clock.el
  3008. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3009. (beg end))
  3010. (declare-function org-clock-update-mode-line "org-clock" ())
  3011. (declare-function org-resolve-clocks "org-clock"
  3012. (&optional also-non-dangling-p prompt last-valid))
  3013. (defvar org-clock-start-time)
  3014. (defvar org-clock-marker (make-marker)
  3015. "Marker recording the last clock-in.")
  3016. (defvar org-clock-hd-marker (make-marker)
  3017. "Marker recording the last clock-in, but the headline position.")
  3018. (defvar org-clock-heading ""
  3019. "The heading of the current clock entry.")
  3020. (defun org-clock-is-active ()
  3021. "Return non-nil if clock is currently running.
  3022. The return value is actually the clock marker."
  3023. (marker-buffer org-clock-marker))
  3024. (eval-and-compile
  3025. (org-autoload
  3026. "org-clock"
  3027. '(org-clock-in org-clock-out org-clock-cancel
  3028. org-clock-goto org-clock-sum org-clock-display
  3029. org-clock-remove-overlays org-clock-report
  3030. org-clocktable-shift org-dblock-write:clocktable
  3031. org-get-clocktable org-resolve-clocks)))
  3032. (defun org-clock-update-time-maybe ()
  3033. "If this is a CLOCK line, update it and return t.
  3034. Otherwise, return nil."
  3035. (interactive)
  3036. (save-excursion
  3037. (beginning-of-line 1)
  3038. (skip-chars-forward " \t")
  3039. (when (looking-at org-clock-string)
  3040. (let ((re (concat "[ \t]*" org-clock-string
  3041. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3042. "\\([ \t]*=>.*\\)?\\)?"))
  3043. ts te h m s neg)
  3044. (cond
  3045. ((not (looking-at re))
  3046. nil)
  3047. ((not (match-end 2))
  3048. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3049. (> org-clock-marker (point))
  3050. (<= org-clock-marker (point-at-eol)))
  3051. ;; The clock is running here
  3052. (setq org-clock-start-time
  3053. (apply 'encode-time
  3054. (org-parse-time-string (match-string 1))))
  3055. (org-clock-update-mode-line)))
  3056. (t
  3057. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3058. (end-of-line 1)
  3059. (setq ts (match-string 1)
  3060. te (match-string 3))
  3061. (setq s (- (org-float-time
  3062. (apply 'encode-time (org-parse-time-string te)))
  3063. (org-float-time
  3064. (apply 'encode-time (org-parse-time-string ts))))
  3065. neg (< s 0)
  3066. s (abs s)
  3067. h (floor (/ s 3600))
  3068. s (- s (* 3600 h))
  3069. m (floor (/ s 60))
  3070. s (- s (* 60 s)))
  3071. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3072. t))))))
  3073. (defun org-check-running-clock ()
  3074. "Check if the current buffer contains the running clock.
  3075. If yes, offer to stop it and to save the buffer with the changes."
  3076. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3077. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3078. (buffer-name))))
  3079. (org-clock-out)
  3080. (when (y-or-n-p "Save changed buffer?")
  3081. (save-buffer))))
  3082. (defun org-clocktable-try-shift (dir n)
  3083. "Check if this line starts a clock table, if yes, shift the time block."
  3084. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3085. (org-clocktable-shift dir n)))
  3086. ;; Autoload org-timer.el
  3087. (eval-and-compile
  3088. (org-autoload
  3089. "org-timer"
  3090. '(org-timer-start org-timer org-timer-item
  3091. org-timer-change-times-in-region
  3092. org-timer-set-timer
  3093. org-timer-reset-timers
  3094. org-timer-show-remaining-time)))
  3095. ;; Autoload org-feed.el
  3096. (eval-and-compile
  3097. (org-autoload
  3098. "org-feed"
  3099. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3100. ;; Autoload org-indent.el
  3101. ;; Define the variable already here, to make sure we have it.
  3102. (defvar org-indent-mode nil
  3103. "Non-nil if Org-Indent mode is enabled.
  3104. Use the command `org-indent-mode' to change this variable.")
  3105. (eval-and-compile
  3106. (org-autoload
  3107. "org-indent"
  3108. '(org-indent-mode)))
  3109. ;; Autoload org-mobile.el
  3110. (eval-and-compile
  3111. (org-autoload
  3112. "org-mobile"
  3113. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3114. ;; Autoload archiving code
  3115. ;; The stuff that is needed for cycling and tags has to be defined here.
  3116. (defgroup org-archive nil
  3117. "Options concerning archiving in Org-mode."
  3118. :tag "Org Archive"
  3119. :group 'org-structure)
  3120. (defcustom org-archive-location "%s_archive::"
  3121. "The location where subtrees should be archived.
  3122. The value of this variable is a string, consisting of two parts,
  3123. separated by a double-colon. The first part is a filename and
  3124. the second part is a headline.
  3125. When the filename is omitted, archiving happens in the same file.
  3126. %s in the filename will be replaced by the current file
  3127. name (without the directory part). Archiving to a different file
  3128. is useful to keep archived entries from contributing to the
  3129. Org-mode Agenda.
  3130. The archived entries will be filed as subtrees of the specified
  3131. headline. When the headline is omitted, the subtrees are simply
  3132. filed away at the end of the file, as top-level entries. Also in
  3133. the heading you can use %s to represent the file name, this can be
  3134. useful when using the same archive for a number of different files.
  3135. Here are a few examples:
  3136. \"%s_archive::\"
  3137. If the current file is Projects.org, archive in file
  3138. Projects.org_archive, as top-level trees. This is the default.
  3139. \"::* Archived Tasks\"
  3140. Archive in the current file, under the top-level headline
  3141. \"* Archived Tasks\".
  3142. \"~/org/archive.org::\"
  3143. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3144. \"~/org/archive.org::From %s\"
  3145. Archive in file ~/org/archive.org (absolute path), under headlines
  3146. \"From FILENAME\" where file name is the current file name.
  3147. \"basement::** Finished Tasks\"
  3148. Archive in file ./basement (relative path), as level 3 trees
  3149. below the level 2 heading \"** Finished Tasks\".
  3150. You may set this option on a per-file basis by adding to the buffer a
  3151. line like
  3152. #+ARCHIVE: basement::** Finished Tasks
  3153. You may also define it locally for a subtree by setting an ARCHIVE property
  3154. in the entry. If such a property is found in an entry, or anywhere up
  3155. the hierarchy, it will be used."
  3156. :group 'org-archive
  3157. :type 'string)
  3158. (defcustom org-archive-tag "ARCHIVE"
  3159. "The tag that marks a subtree as archived.
  3160. An archived subtree does not open during visibility cycling, and does
  3161. not contribute to the agenda listings.
  3162. After changing this, font-lock must be restarted in the relevant buffers to
  3163. get the proper fontification."
  3164. :group 'org-archive
  3165. :group 'org-keywords
  3166. :type 'string)
  3167. (defcustom org-agenda-skip-archived-trees t
  3168. "Non-nil means the agenda will skip any items located in archived trees.
  3169. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3170. variable is no longer recommended, you should leave it at the value t.
  3171. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3172. :group 'org-archive
  3173. :group 'org-agenda-skip
  3174. :type 'boolean)
  3175. (defcustom org-columns-skip-archived-trees t
  3176. "Non-nil means ignore archived trees when creating column view."
  3177. :group 'org-archive
  3178. :group 'org-properties
  3179. :type 'boolean)
  3180. (defcustom org-cycle-open-archived-trees nil
  3181. "Non-nil means `org-cycle' will open archived trees.
  3182. An archived tree is a tree marked with the tag ARCHIVE.
  3183. When nil, archived trees will stay folded. You can still open them with
  3184. normal outline commands like `show-all', but not with the cycling commands."
  3185. :group 'org-archive
  3186. :group 'org-cycle
  3187. :type 'boolean)
  3188. (defcustom org-sparse-tree-open-archived-trees nil
  3189. "Non-nil means sparse tree construction shows matches in archived trees.
  3190. When nil, matches in these trees are highlighted, but the trees are kept in
  3191. collapsed state."
  3192. :group 'org-archive
  3193. :group 'org-sparse-trees
  3194. :type 'boolean)
  3195. (defun org-cycle-hide-archived-subtrees (state)
  3196. "Re-hide all archived subtrees after a visibility state change."
  3197. (when (and (not org-cycle-open-archived-trees)
  3198. (not (memq state '(overview folded))))
  3199. (save-excursion
  3200. (let* ((globalp (memq state '(contents all)))
  3201. (beg (if globalp (point-min) (point)))
  3202. (end (if globalp (point-max) (org-end-of-subtree t))))
  3203. (org-hide-archived-subtrees beg end)
  3204. (goto-char beg)
  3205. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3206. (message "%s" (substitute-command-keys
  3207. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3208. (defun org-force-cycle-archived ()
  3209. "Cycle subtree even if it is archived."
  3210. (interactive)
  3211. (setq this-command 'org-cycle)
  3212. (let ((org-cycle-open-archived-trees t))
  3213. (call-interactively 'org-cycle)))
  3214. (defun org-hide-archived-subtrees (beg end)
  3215. "Re-hide all archived subtrees after a visibility state change."
  3216. (save-excursion
  3217. (let* ((re (concat ":" org-archive-tag ":")))
  3218. (goto-char beg)
  3219. (while (re-search-forward re end t)
  3220. (when (org-on-heading-p)
  3221. (org-flag-subtree t)
  3222. (org-end-of-subtree t))))))
  3223. (defun org-flag-subtree (flag)
  3224. (save-excursion
  3225. (org-back-to-heading t)
  3226. (outline-end-of-heading)
  3227. (outline-flag-region (point)
  3228. (progn (org-end-of-subtree t) (point))
  3229. flag)))
  3230. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3231. (eval-and-compile
  3232. (org-autoload "org-archive"
  3233. '(org-add-archive-files org-archive-subtree
  3234. org-archive-to-archive-sibling org-toggle-archive-tag
  3235. org-archive-subtree-default
  3236. org-archive-subtree-default-with-confirmation)))
  3237. ;; Autoload Column View Code
  3238. (declare-function org-columns-number-to-string "org-colview")
  3239. (declare-function org-columns-get-format-and-top-level "org-colview")
  3240. (declare-function org-columns-compute "org-colview")
  3241. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3242. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3243. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3244. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3245. ;; Autoload ID code
  3246. (declare-function org-id-store-link "org-id")
  3247. (declare-function org-id-locations-load "org-id")
  3248. (declare-function org-id-locations-save "org-id")
  3249. (defvar org-id-track-globally)
  3250. (org-autoload "org-id"
  3251. '(org-id-get-create org-id-new org-id-copy org-id-get
  3252. org-id-get-with-outline-path-completion
  3253. org-id-get-with-outline-drilling
  3254. org-id-goto org-id-find org-id-store-link))
  3255. ;; Autoload Plotting Code
  3256. (org-autoload "org-plot"
  3257. '(org-plot/gnuplot))
  3258. ;;; Variables for pre-computed regular expressions, all buffer local
  3259. (defvar org-drawer-regexp nil
  3260. "Matches first line of a hidden block.")
  3261. (make-variable-buffer-local 'org-drawer-regexp)
  3262. (defvar org-todo-regexp nil
  3263. "Matches any of the TODO state keywords.")
  3264. (make-variable-buffer-local 'org-todo-regexp)
  3265. (defvar org-not-done-regexp nil
  3266. "Matches any of the TODO state keywords except the last one.")
  3267. (make-variable-buffer-local 'org-not-done-regexp)
  3268. (defvar org-not-done-heading-regexp nil
  3269. "Matches a TODO headline that is not done.")
  3270. (make-variable-buffer-local 'org-not-done-regexp)
  3271. (defvar org-todo-line-regexp nil
  3272. "Matches a headline and puts TODO state into group 2 if present.")
  3273. (make-variable-buffer-local 'org-todo-line-regexp)
  3274. (defvar org-complex-heading-regexp nil
  3275. "Matches a headline and puts everything into groups:
  3276. group 1: the stars
  3277. group 2: The todo keyword, maybe
  3278. group 3: Priority cookie
  3279. group 4: True headline
  3280. group 5: Tags")
  3281. (make-variable-buffer-local 'org-complex-heading-regexp)
  3282. (defvar org-complex-heading-regexp-format nil)
  3283. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3284. (defvar org-todo-line-tags-regexp nil
  3285. "Matches a headline and puts TODO state into group 2 if present.
  3286. Also put tags into group 4 if tags are present.")
  3287. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3288. (defvar org-nl-done-regexp nil
  3289. "Matches newline followed by a headline with the DONE keyword.")
  3290. (make-variable-buffer-local 'org-nl-done-regexp)
  3291. (defvar org-looking-at-done-regexp nil
  3292. "Matches the DONE keyword a point.")
  3293. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3294. (defvar org-ds-keyword-length 12
  3295. "Maximum length of the Deadline and SCHEDULED keywords.")
  3296. (make-variable-buffer-local 'org-ds-keyword-length)
  3297. (defvar org-deadline-regexp nil
  3298. "Matches the DEADLINE keyword.")
  3299. (make-variable-buffer-local 'org-deadline-regexp)
  3300. (defvar org-deadline-time-regexp nil
  3301. "Matches the DEADLINE keyword together with a time stamp.")
  3302. (make-variable-buffer-local 'org-deadline-time-regexp)
  3303. (defvar org-deadline-line-regexp nil
  3304. "Matches the DEADLINE keyword and the rest of the line.")
  3305. (make-variable-buffer-local 'org-deadline-line-regexp)
  3306. (defvar org-scheduled-regexp nil
  3307. "Matches the SCHEDULED keyword.")
  3308. (make-variable-buffer-local 'org-scheduled-regexp)
  3309. (defvar org-scheduled-time-regexp nil
  3310. "Matches the SCHEDULED keyword together with a time stamp.")
  3311. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3312. (defvar org-closed-time-regexp nil
  3313. "Matches the CLOSED keyword together with a time stamp.")
  3314. (make-variable-buffer-local 'org-closed-time-regexp)
  3315. (defvar org-keyword-time-regexp nil
  3316. "Matches any of the 4 keywords, together with the time stamp.")
  3317. (make-variable-buffer-local 'org-keyword-time-regexp)
  3318. (defvar org-keyword-time-not-clock-regexp nil
  3319. "Matches any of the 3 keywords, together with the time stamp.")
  3320. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3321. (defvar org-maybe-keyword-time-regexp nil
  3322. "Matches a timestamp, possibly preceeded by a keyword.")
  3323. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3324. (defvar org-planning-or-clock-line-re nil
  3325. "Matches a line with planning or clock info.")
  3326. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3327. (defvar org-all-time-keywords nil
  3328. "List of time keywords.")
  3329. (make-variable-buffer-local 'org-all-time-keywords)
  3330. (defconst org-plain-time-of-day-regexp
  3331. (concat
  3332. "\\(\\<[012]?[0-9]"
  3333. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3334. "\\(--?"
  3335. "\\(\\<[012]?[0-9]"
  3336. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3337. "\\)?")
  3338. "Regular expression to match a plain time or time range.
  3339. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3340. groups carry important information:
  3341. 0 the full match
  3342. 1 the first time, range or not
  3343. 8 the second time, if it is a range.")
  3344. (defconst org-plain-time-extension-regexp
  3345. (concat
  3346. "\\(\\<[012]?[0-9]"
  3347. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3348. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3349. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3350. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3351. groups carry important information:
  3352. 0 the full match
  3353. 7 hours of duration
  3354. 9 minutes of duration")
  3355. (defconst org-stamp-time-of-day-regexp
  3356. (concat
  3357. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3358. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3359. "\\(--?"
  3360. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3361. "Regular expression to match a timestamp time or time range.
  3362. After a match, the following groups carry important information:
  3363. 0 the full match
  3364. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3365. 2 the first time, range or not
  3366. 4 the second time, if it is a range.")
  3367. (defconst org-startup-options
  3368. '(("fold" org-startup-folded t)
  3369. ("overview" org-startup-folded t)
  3370. ("nofold" org-startup-folded nil)
  3371. ("showall" org-startup-folded nil)
  3372. ("showeverything" org-startup-folded showeverything)
  3373. ("content" org-startup-folded content)
  3374. ("indent" org-startup-indented t)
  3375. ("noindent" org-startup-indented nil)
  3376. ("hidestars" org-hide-leading-stars t)
  3377. ("showstars" org-hide-leading-stars nil)
  3378. ("odd" org-odd-levels-only t)
  3379. ("oddeven" org-odd-levels-only nil)
  3380. ("align" org-startup-align-all-tables t)
  3381. ("noalign" org-startup-align-all-tables nil)
  3382. ("customtime" org-display-custom-times t)
  3383. ("logdone" org-log-done time)
  3384. ("lognotedone" org-log-done note)
  3385. ("nologdone" org-log-done nil)
  3386. ("lognoteclock-out" org-log-note-clock-out t)
  3387. ("nolognoteclock-out" org-log-note-clock-out nil)
  3388. ("logrepeat" org-log-repeat state)
  3389. ("lognoterepeat" org-log-repeat note)
  3390. ("nologrepeat" org-log-repeat nil)
  3391. ("logreschedule" org-log-reschedule time)
  3392. ("lognotereschedule" org-log-reschedule note)
  3393. ("nologreschedule" org-log-reschedule nil)
  3394. ("logredeadline" org-log-redeadline time)
  3395. ("lognoteredeadline" org-log-redeadline note)
  3396. ("nologredeadline" org-log-redeadline nil)
  3397. ("logrefile" org-log-refile time)
  3398. ("lognoterefile" org-log-refile note)
  3399. ("nologrefile" org-log-refile nil)
  3400. ("fninline" org-footnote-define-inline t)
  3401. ("nofninline" org-footnote-define-inline nil)
  3402. ("fnlocal" org-footnote-section nil)
  3403. ("fnauto" org-footnote-auto-label t)
  3404. ("fnprompt" org-footnote-auto-label nil)
  3405. ("fnconfirm" org-footnote-auto-label confirm)
  3406. ("fnplain" org-footnote-auto-label plain)
  3407. ("fnadjust" org-footnote-auto-adjust t)
  3408. ("nofnadjust" org-footnote-auto-adjust nil)
  3409. ("constcgs" constants-unit-system cgs)
  3410. ("constSI" constants-unit-system SI)
  3411. ("noptag" org-tag-persistent-alist nil)
  3412. ("hideblocks" org-hide-block-startup t)
  3413. ("nohideblocks" org-hide-block-startup nil)
  3414. ("beamer" org-startup-with-beamer-mode t))
  3415. "Variable associated with STARTUP options for org-mode.
  3416. Each element is a list of three items: The startup options as written
  3417. in the #+STARTUP line, the corresponding variable, and the value to
  3418. set this variable to if the option is found. An optional forth element PUSH
  3419. means to push this value onto the list in the variable.")
  3420. (defun org-set-regexps-and-options ()
  3421. "Precompute regular expressions for current buffer."
  3422. (when (org-mode-p)
  3423. (org-set-local 'org-todo-kwd-alist nil)
  3424. (org-set-local 'org-todo-key-alist nil)
  3425. (org-set-local 'org-todo-key-trigger nil)
  3426. (org-set-local 'org-todo-keywords-1 nil)
  3427. (org-set-local 'org-done-keywords nil)
  3428. (org-set-local 'org-todo-heads nil)
  3429. (org-set-local 'org-todo-sets nil)
  3430. (org-set-local 'org-todo-log-states nil)
  3431. (org-set-local 'org-file-properties nil)
  3432. (org-set-local 'org-file-tags nil)
  3433. (let ((re (org-make-options-regexp
  3434. '("CATEGORY" "TODO" "COLUMNS"
  3435. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3436. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS")
  3437. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3438. (splitre "[ \t]+")
  3439. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3440. tail sep kws1 prio props ftags drawers beamer-p
  3441. ext-setup-or-nil setup-contents (start 0))
  3442. (save-excursion
  3443. (save-restriction
  3444. (widen)
  3445. (goto-char (point-min))
  3446. (while (or (and ext-setup-or-nil
  3447. (string-match re ext-setup-or-nil start)
  3448. (setq start (match-end 0)))
  3449. (and (setq ext-setup-or-nil nil start 0)
  3450. (re-search-forward re nil t)))
  3451. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3452. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3453. (cond
  3454. ((equal key "CATEGORY")
  3455. (if (string-match "[ \t]+$" value)
  3456. (setq value (replace-match "" t t value)))
  3457. (setq cat value))
  3458. ((member key '("SEQ_TODO" "TODO"))
  3459. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3460. ((equal key "TYP_TODO")
  3461. (push (cons 'type (org-split-string value splitre)) kwds))
  3462. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3463. ;; general TODO-like setup
  3464. (push (cons (intern (downcase (match-string 1 key)))
  3465. (org-split-string value splitre)) kwds))
  3466. ((equal key "TAGS")
  3467. (setq tags (append tags (if tags '("\\n") nil)
  3468. (org-split-string value splitre))))
  3469. ((equal key "COLUMNS")
  3470. (org-set-local 'org-columns-default-format value))
  3471. ((equal key "LINK")
  3472. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3473. (push (cons (match-string 1 value)
  3474. (org-trim (match-string 2 value)))
  3475. links)))
  3476. ((equal key "PRIORITIES")
  3477. (setq prio (org-split-string value " +")))
  3478. ((equal key "PROPERTY")
  3479. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3480. (push (cons (match-string 1 value) (match-string 2 value))
  3481. props)))
  3482. ((equal key "FILETAGS")
  3483. (when (string-match "\\S-" value)
  3484. (setq ftags
  3485. (append
  3486. ftags
  3487. (apply 'append
  3488. (mapcar (lambda (x) (org-split-string x ":"))
  3489. (org-split-string value)))))))
  3490. ((equal key "DRAWERS")
  3491. (setq drawers (org-split-string value splitre)))
  3492. ((equal key "CONSTANTS")
  3493. (setq const (append const (org-split-string value splitre))))
  3494. ((equal key "STARTUP")
  3495. (let ((opts (org-split-string value splitre))
  3496. l var val)
  3497. (while (setq l (pop opts))
  3498. (when (setq l (assoc l org-startup-options))
  3499. (setq var (nth 1 l) val (nth 2 l))
  3500. (if (not (nth 3 l))
  3501. (set (make-local-variable var) val)
  3502. (if (not (listp (symbol-value var)))
  3503. (set (make-local-variable var) nil))
  3504. (set (make-local-variable var) (symbol-value var))
  3505. (add-to-list var val))))))
  3506. ((equal key "ARCHIVE")
  3507. (string-match " *$" value)
  3508. (setq arch (replace-match "" t t value))
  3509. (remove-text-properties 0 (length arch)
  3510. '(face t fontified t) arch))
  3511. ((equal key "LATEX_CLASS")
  3512. (setq beamer-p (equal value "beamer")))
  3513. ((equal key "SETUPFILE")
  3514. (setq setup-contents (org-file-contents
  3515. (expand-file-name
  3516. (org-remove-double-quotes value))
  3517. 'noerror))
  3518. (if (not ext-setup-or-nil)
  3519. (setq ext-setup-or-nil setup-contents start 0)
  3520. (setq ext-setup-or-nil
  3521. (concat (substring ext-setup-or-nil 0 start)
  3522. "\n" setup-contents "\n"
  3523. (substring ext-setup-or-nil start)))))
  3524. ))))
  3525. (when cat
  3526. (org-set-local 'org-category (intern cat))
  3527. (push (cons "CATEGORY" cat) props))
  3528. (when prio
  3529. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3530. (setq prio (mapcar 'string-to-char prio))
  3531. (org-set-local 'org-highest-priority (nth 0 prio))
  3532. (org-set-local 'org-lowest-priority (nth 1 prio))
  3533. (org-set-local 'org-default-priority (nth 2 prio)))
  3534. (and props (org-set-local 'org-file-properties (nreverse props)))
  3535. (and ftags (org-set-local 'org-file-tags
  3536. (mapcar 'org-add-prop-inherited ftags)))
  3537. (and drawers (org-set-local 'org-drawers drawers))
  3538. (and arch (org-set-local 'org-archive-location arch))
  3539. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3540. ;; Process the TODO keywords
  3541. (unless kwds
  3542. ;; Use the global values as if they had been given locally.
  3543. (setq kwds (default-value 'org-todo-keywords))
  3544. (if (stringp (car kwds))
  3545. (setq kwds (list (cons org-todo-interpretation
  3546. (default-value 'org-todo-keywords)))))
  3547. (setq kwds (reverse kwds)))
  3548. (setq kwds (nreverse kwds))
  3549. (let (inter kws kw)
  3550. (while (setq kws (pop kwds))
  3551. (let ((kws (or
  3552. (run-hook-with-args-until-success
  3553. 'org-todo-setup-filter-hook kws)
  3554. kws)))
  3555. (setq inter (pop kws) sep (member "|" kws)
  3556. kws0 (delete "|" (copy-sequence kws))
  3557. kwsa nil
  3558. kws1 (mapcar
  3559. (lambda (x)
  3560. ;; 1 2
  3561. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3562. (progn
  3563. (setq kw (match-string 1 x)
  3564. key (and (match-end 2) (match-string 2 x))
  3565. log (org-extract-log-state-settings x))
  3566. (push (cons kw (and key (string-to-char key))) kwsa)
  3567. (and log (push log org-todo-log-states))
  3568. kw)
  3569. (error "Invalid TODO keyword %s" x)))
  3570. kws0)
  3571. kwsa (if kwsa (append '((:startgroup))
  3572. (nreverse kwsa)
  3573. '((:endgroup))))
  3574. hw (car kws1)
  3575. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3576. tail (list inter hw (car dws) (org-last dws))))
  3577. (add-to-list 'org-todo-heads hw 'append)
  3578. (push kws1 org-todo-sets)
  3579. (setq org-done-keywords (append org-done-keywords dws nil))
  3580. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3581. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3582. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3583. (setq org-todo-sets (nreverse org-todo-sets)
  3584. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3585. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3586. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3587. ;; Process the constants
  3588. (when const
  3589. (let (e cst)
  3590. (while (setq e (pop const))
  3591. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3592. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3593. (setq org-table-formula-constants-local cst)))
  3594. ;; Process the tags.
  3595. (when tags
  3596. (let (e tgs)
  3597. (while (setq e (pop tags))
  3598. (cond
  3599. ((equal e "{") (push '(:startgroup) tgs))
  3600. ((equal e "}") (push '(:endgroup) tgs))
  3601. ((equal e "\\n") (push '(:newline) tgs))
  3602. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3603. (push (cons (match-string 1 e)
  3604. (string-to-char (match-string 2 e)))
  3605. tgs))
  3606. (t (push (list e) tgs))))
  3607. (org-set-local 'org-tag-alist nil)
  3608. (while (setq e (pop tgs))
  3609. (or (and (stringp (car e))
  3610. (assoc (car e) org-tag-alist))
  3611. (push e org-tag-alist)))))
  3612. ;; Compute the regular expressions and other local variables
  3613. (if (not org-done-keywords)
  3614. (setq org-done-keywords (and org-todo-keywords-1
  3615. (list (org-last org-todo-keywords-1)))))
  3616. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3617. (length org-scheduled-string)
  3618. (length org-clock-string)
  3619. (length org-closed-string)))
  3620. org-drawer-regexp
  3621. (concat "^[ \t]*:\\("
  3622. (mapconcat 'regexp-quote org-drawers "\\|")
  3623. "\\):[ \t]*$")
  3624. org-not-done-keywords
  3625. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3626. org-todo-regexp
  3627. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3628. "\\|") "\\)\\>")
  3629. org-not-done-regexp
  3630. (concat "\\<\\("
  3631. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3632. "\\)\\>")
  3633. org-not-done-heading-regexp
  3634. (concat "^\\(\\*+\\)[ \t]+\\("
  3635. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3636. "\\)\\>")
  3637. org-todo-line-regexp
  3638. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3639. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3640. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3641. org-complex-heading-regexp
  3642. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3643. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3644. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3645. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3646. org-complex-heading-regexp-format
  3647. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3648. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3649. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(%s\\)"
  3650. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3651. org-nl-done-regexp
  3652. (concat "\n\\*+[ \t]+"
  3653. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3654. "\\)" "\\>")
  3655. org-todo-line-tags-regexp
  3656. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3657. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3658. (org-re
  3659. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3660. org-looking-at-done-regexp
  3661. (concat "^" "\\(?:"
  3662. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3663. "\\>")
  3664. org-deadline-regexp (concat "\\<" org-deadline-string)
  3665. org-deadline-time-regexp
  3666. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3667. org-deadline-line-regexp
  3668. (concat "\\<\\(" org-deadline-string "\\).*")
  3669. org-scheduled-regexp
  3670. (concat "\\<" org-scheduled-string)
  3671. org-scheduled-time-regexp
  3672. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3673. org-closed-time-regexp
  3674. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3675. org-keyword-time-regexp
  3676. (concat "\\<\\(" org-scheduled-string
  3677. "\\|" org-deadline-string
  3678. "\\|" org-closed-string
  3679. "\\|" org-clock-string "\\)"
  3680. " *[[<]\\([^]>]+\\)[]>]")
  3681. org-keyword-time-not-clock-regexp
  3682. (concat "\\<\\(" org-scheduled-string
  3683. "\\|" org-deadline-string
  3684. "\\|" org-closed-string
  3685. "\\)"
  3686. " *[[<]\\([^]>]+\\)[]>]")
  3687. org-maybe-keyword-time-regexp
  3688. (concat "\\(\\<\\(" org-scheduled-string
  3689. "\\|" org-deadline-string
  3690. "\\|" org-closed-string
  3691. "\\|" org-clock-string "\\)\\)?"
  3692. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3693. org-planning-or-clock-line-re
  3694. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3695. "\\|" org-deadline-string
  3696. "\\|" org-closed-string "\\|" org-clock-string
  3697. "\\)\\>\\)")
  3698. org-all-time-keywords
  3699. (mapcar (lambda (w) (substring w 0 -1))
  3700. (list org-scheduled-string org-deadline-string
  3701. org-clock-string org-closed-string))
  3702. )
  3703. (org-compute-latex-and-specials-regexp)
  3704. (org-set-font-lock-defaults))))
  3705. (defun org-file-contents (file &optional noerror)
  3706. "Return the contents of FILE, as a string."
  3707. (if (or (not file)
  3708. (not (file-readable-p file)))
  3709. (if noerror
  3710. (progn
  3711. (message "Cannot read file %s" file)
  3712. (ding) (sit-for 2)
  3713. "")
  3714. (error "Cannot read file %s" file))
  3715. (with-temp-buffer
  3716. (insert-file-contents file)
  3717. (buffer-string))))
  3718. (defun org-extract-log-state-settings (x)
  3719. "Extract the log state setting from a TODO keyword string.
  3720. This will extract info from a string like \"WAIT(w@/!)\"."
  3721. (let (kw key log1 log2)
  3722. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3723. (setq kw (match-string 1 x)
  3724. key (and (match-end 2) (match-string 2 x))
  3725. log1 (and (match-end 3) (match-string 3 x))
  3726. log2 (and (match-end 4) (match-string 4 x)))
  3727. (and (or log1 log2)
  3728. (list kw
  3729. (and log1 (if (equal log1 "!") 'time 'note))
  3730. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3731. (defun org-remove-keyword-keys (list)
  3732. "Remove a pair of parenthesis at the end of each string in LIST."
  3733. (mapcar (lambda (x)
  3734. (if (string-match "(.*)$" x)
  3735. (substring x 0 (match-beginning 0))
  3736. x))
  3737. list))
  3738. (defun org-assign-fast-keys (alist)
  3739. "Assign fast keys to a keyword-key alist.
  3740. Respect keys that are already there."
  3741. (let (new e (alt ?0))
  3742. (while (setq e (pop alist))
  3743. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  3744. (cdr e)) ;; Key already assigned.
  3745. (push e new)
  3746. (let ((clist (string-to-list (downcase (car e))))
  3747. (used (append new alist)))
  3748. (when (= (car clist) ?@)
  3749. (pop clist))
  3750. (while (and clist (rassoc (car clist) used))
  3751. (pop clist))
  3752. (unless clist
  3753. (while (rassoc alt used)
  3754. (incf alt)))
  3755. (push (cons (car e) (or (car clist) alt)) new))))
  3756. (nreverse new)))
  3757. ;;; Some variables used in various places
  3758. (defvar org-window-configuration nil
  3759. "Used in various places to store a window configuration.")
  3760. (defvar org-selected-window nil
  3761. "Used in various places to store a window configuration.")
  3762. (defvar org-finish-function nil
  3763. "Function to be called when `C-c C-c' is used.
  3764. This is for getting out of special buffers like remember.")
  3765. ;; FIXME: Occasionally check by commenting these, to make sure
  3766. ;; no other functions uses these, forgetting to let-bind them.
  3767. (defvar entry)
  3768. (defvar last-state)
  3769. (defvar date)
  3770. ;; Defined somewhere in this file, but used before definition.
  3771. (defvar org-entities) ;; defined in org-entities.el
  3772. (defvar org-struct-menu)
  3773. (defvar org-org-menu)
  3774. (defvar org-tbl-menu)
  3775. ;;;; Define the Org-mode
  3776. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3777. (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."))
  3778. ;; We use a before-change function to check if a table might need
  3779. ;; an update.
  3780. (defvar org-table-may-need-update t
  3781. "Indicates that a table might need an update.
  3782. This variable is set by `org-before-change-function'.
  3783. `org-table-align' sets it back to nil.")
  3784. (defun org-before-change-function (beg end)
  3785. "Every change indicates that a table might need an update."
  3786. (setq org-table-may-need-update t))
  3787. (defvar org-mode-map)
  3788. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3789. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  3790. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3791. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  3792. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  3793. (defvar org-table-buffer-is-an nil)
  3794. (defconst org-outline-regexp "\\*+ ")
  3795. ;;;###autoload
  3796. (define-derived-mode org-mode outline-mode "Org"
  3797. "Outline-based notes management and organizer, alias
  3798. \"Carsten's outline-mode for keeping track of everything.\"
  3799. Org-mode develops organizational tasks around a NOTES file which
  3800. contains information about projects as plain text. Org-mode is
  3801. implemented on top of outline-mode, which is ideal to keep the content
  3802. of large files well structured. It supports ToDo items, deadlines and
  3803. time stamps, which magically appear in the diary listing of the Emacs
  3804. calendar. Tables are easily created with a built-in table editor.
  3805. Plain text URL-like links connect to websites, emails (VM), Usenet
  3806. messages (Gnus), BBDB entries, and any files related to the project.
  3807. For printing and sharing of notes, an Org-mode file (or a part of it)
  3808. can be exported as a structured ASCII or HTML file.
  3809. The following commands are available:
  3810. \\{org-mode-map}"
  3811. ;; Get rid of Outline menus, they are not needed
  3812. ;; Need to do this here because define-derived-mode sets up
  3813. ;; the keymap so late. Still, it is a waste to call this each time
  3814. ;; we switch another buffer into org-mode.
  3815. (if (featurep 'xemacs)
  3816. (when (boundp 'outline-mode-menu-heading)
  3817. ;; Assume this is Greg's port, it used easymenu
  3818. (easy-menu-remove outline-mode-menu-heading)
  3819. (easy-menu-remove outline-mode-menu-show)
  3820. (easy-menu-remove outline-mode-menu-hide))
  3821. (define-key org-mode-map [menu-bar headings] 'undefined)
  3822. (define-key org-mode-map [menu-bar hide] 'undefined)
  3823. (define-key org-mode-map [menu-bar show] 'undefined))
  3824. (org-load-modules-maybe)
  3825. (easy-menu-add org-org-menu)
  3826. (easy-menu-add org-tbl-menu)
  3827. (org-install-agenda-files-menu)
  3828. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3829. (org-add-to-invisibility-spec '(org-cwidth))
  3830. (org-add-to-invisibility-spec '(org-hide-block . t))
  3831. (when (featurep 'xemacs)
  3832. (org-set-local 'line-move-ignore-invisible t))
  3833. (org-set-local 'outline-regexp org-outline-regexp)
  3834. (org-set-local 'outline-level 'org-outline-level)
  3835. (when (and org-ellipsis
  3836. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3837. (fboundp 'make-glyph-code))
  3838. (unless org-display-table
  3839. (setq org-display-table (make-display-table)))
  3840. (set-display-table-slot
  3841. org-display-table 4
  3842. (vconcat (mapcar
  3843. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3844. org-ellipsis)))
  3845. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3846. (setq buffer-display-table org-display-table))
  3847. (org-set-regexps-and-options)
  3848. (when (and org-tag-faces (not org-tags-special-faces-re))
  3849. ;; tag faces set outside customize.... force initialization.
  3850. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3851. ;; Calc embedded
  3852. (org-set-local 'calc-embedded-open-mode "# ")
  3853. (modify-syntax-entry ?# "<")
  3854. (modify-syntax-entry ?@ "w")
  3855. (if org-startup-truncated (setq truncate-lines t))
  3856. (org-set-local 'font-lock-unfontify-region-function
  3857. 'org-unfontify-region)
  3858. ;; Activate before-change-function
  3859. (org-set-local 'org-table-may-need-update t)
  3860. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3861. 'local)
  3862. ;; Check for running clock before killing a buffer
  3863. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3864. ;; Paragraphs and auto-filling
  3865. (org-set-autofill-regexps)
  3866. (setq indent-line-function 'org-indent-line-function)
  3867. (org-update-radio-target-regexp)
  3868. ;; Make sure dependence stuff works reliably, even for users who set it
  3869. ;; too late :-(
  3870. (if org-enforce-todo-dependencies
  3871. (add-hook 'org-blocker-hook
  3872. 'org-block-todo-from-children-or-siblings-or-parent)
  3873. (remove-hook 'org-blocker-hook
  3874. 'org-block-todo-from-children-or-siblings-or-parent))
  3875. (if org-enforce-todo-checkbox-dependencies
  3876. (add-hook 'org-blocker-hook
  3877. 'org-block-todo-from-checkboxes)
  3878. (remove-hook 'org-blocker-hook
  3879. 'org-block-todo-from-checkboxes))
  3880. ;; Comment characters
  3881. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3882. (org-set-local 'comment-padding " ")
  3883. ;; Align options lines
  3884. (org-set-local
  3885. 'align-mode-rules-list
  3886. '((org-in-buffer-settings
  3887. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3888. (modes . '(org-mode)))))
  3889. ;; Imenu
  3890. (org-set-local 'imenu-create-index-function
  3891. 'org-imenu-get-tree)
  3892. ;; Make isearch reveal context
  3893. (if (or (featurep 'xemacs)
  3894. (not (boundp 'outline-isearch-open-invisible-function)))
  3895. ;; Emacs 21 and XEmacs make use of the hook
  3896. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3897. ;; Emacs 22 deals with this through a special variable
  3898. (org-set-local 'outline-isearch-open-invisible-function
  3899. (lambda (&rest ignore) (org-show-context 'isearch))))
  3900. ;; Turn on org-beamer-mode?
  3901. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  3902. ;; If empty file that did not turn on org-mode automatically, make it to.
  3903. (if (and org-insert-mode-line-in-empty-file
  3904. (interactive-p)
  3905. (= (point-min) (point-max)))
  3906. (insert "# -*- mode: org -*-\n\n"))
  3907. (unless org-inhibit-startup
  3908. (when org-startup-align-all-tables
  3909. (let ((bmp (buffer-modified-p)))
  3910. (org-table-map-tables 'org-table-align)
  3911. (set-buffer-modified-p bmp)))
  3912. (when org-startup-indented
  3913. (require 'org-indent)
  3914. (org-indent-mode 1))
  3915. (unless org-inhibit-startup-visibility-stuff
  3916. (org-set-startup-visibility))))
  3917. (when (fboundp 'abbrev-table-put)
  3918. (abbrev-table-put org-mode-abbrev-table
  3919. :parents (list text-mode-abbrev-table)))
  3920. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3921. (defun org-current-time ()
  3922. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3923. (if (> (car org-time-stamp-rounding-minutes) 1)
  3924. (let ((r (car org-time-stamp-rounding-minutes))
  3925. (time (decode-time)))
  3926. (apply 'encode-time
  3927. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3928. (nthcdr 2 time))))
  3929. (current-time)))
  3930. ;;;; Font-Lock stuff, including the activators
  3931. (defvar org-mouse-map (make-sparse-keymap))
  3932. (org-defkey org-mouse-map
  3933. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3934. (org-defkey org-mouse-map
  3935. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3936. (when org-mouse-1-follows-link
  3937. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3938. (when org-tab-follows-link
  3939. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3940. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3941. (require 'font-lock)
  3942. (defconst org-non-link-chars "]\t\n\r<>")
  3943. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3944. "shell" "elisp"))
  3945. (defvar org-link-types-re nil
  3946. "Matches a link that has a url-like prefix like \"http:\"")
  3947. (defvar org-link-re-with-space nil
  3948. "Matches a link with spaces, optional angular brackets around it.")
  3949. (defvar org-link-re-with-space2 nil
  3950. "Matches a link with spaces, optional angular brackets around it.")
  3951. (defvar org-link-re-with-space3 nil
  3952. "Matches a link with spaces, only for internal part in bracket links.")
  3953. (defvar org-angle-link-re nil
  3954. "Matches link with angular brackets, spaces are allowed.")
  3955. (defvar org-plain-link-re nil
  3956. "Matches plain link, without spaces.")
  3957. (defvar org-bracket-link-regexp nil
  3958. "Matches a link in double brackets.")
  3959. (defvar org-bracket-link-analytic-regexp nil
  3960. "Regular expression used to analyze links.
  3961. Here is what the match groups contain after a match:
  3962. 1: http:
  3963. 2: http
  3964. 3: path
  3965. 4: [desc]
  3966. 5: desc")
  3967. (defvar org-bracket-link-analytic-regexp++ nil
  3968. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3969. (defvar org-any-link-re nil
  3970. "Regular expression matching any link.")
  3971. (defun org-make-link-regexps ()
  3972. "Update the link regular expressions.
  3973. This should be called after the variable `org-link-types' has changed."
  3974. (setq org-link-types-re
  3975. (concat
  3976. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  3977. org-link-re-with-space
  3978. (concat
  3979. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3980. "\\([^" org-non-link-chars " ]"
  3981. "[^" org-non-link-chars "]*"
  3982. "[^" org-non-link-chars " ]\\)>?")
  3983. org-link-re-with-space2
  3984. (concat
  3985. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3986. "\\([^" org-non-link-chars " ]"
  3987. "[^\t\n\r]*"
  3988. "[^" org-non-link-chars " ]\\)>?")
  3989. org-link-re-with-space3
  3990. (concat
  3991. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3992. "\\([^" org-non-link-chars " ]"
  3993. "[^\t\n\r]*\\)")
  3994. org-angle-link-re
  3995. (concat
  3996. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  3997. "\\([^" org-non-link-chars " ]"
  3998. "[^" org-non-link-chars "]*"
  3999. "\\)>")
  4000. org-plain-link-re
  4001. (concat
  4002. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4003. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4004. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4005. org-bracket-link-regexp
  4006. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4007. org-bracket-link-analytic-regexp
  4008. (concat
  4009. "\\[\\["
  4010. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4011. "\\([^]]+\\)"
  4012. "\\]"
  4013. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4014. "\\]")
  4015. org-bracket-link-analytic-regexp++
  4016. (concat
  4017. "\\[\\["
  4018. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4019. "\\([^]]+\\)"
  4020. "\\]"
  4021. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4022. "\\]")
  4023. org-any-link-re
  4024. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4025. org-angle-link-re "\\)\\|\\("
  4026. org-plain-link-re "\\)")))
  4027. (org-make-link-regexps)
  4028. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4029. "Regular expression for fast time stamp matching.")
  4030. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  4031. "Regular expression for fast time stamp matching.")
  4032. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4033. "Regular expression matching time strings for analysis.
  4034. This one does not require the space after the date, so it can be used
  4035. on a string that terminates immediately after the date.")
  4036. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4037. "Regular expression matching time strings for analysis.")
  4038. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4039. "Regular expression matching time stamps, with groups.")
  4040. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4041. "Regular expression matching time stamps (also [..]), with groups.")
  4042. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4043. "Regular expression matching a time stamp range.")
  4044. (defconst org-tr-regexp-both
  4045. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4046. "Regular expression matching a time stamp range.")
  4047. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4048. org-ts-regexp "\\)?")
  4049. "Regular expression matching a time stamp or time stamp range.")
  4050. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4051. org-ts-regexp-both "\\)?")
  4052. "Regular expression matching a time stamp or time stamp range.
  4053. The time stamps may be either active or inactive.")
  4054. (defvar org-emph-face nil)
  4055. (defun org-do-emphasis-faces (limit)
  4056. "Run through the buffer and add overlays to links."
  4057. (let (rtn a)
  4058. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4059. (if (not (= (char-after (match-beginning 3))
  4060. (char-after (match-beginning 4))))
  4061. (progn
  4062. (setq rtn t)
  4063. (setq a (assoc (match-string 3) org-emphasis-alist))
  4064. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4065. 'face
  4066. (nth 1 a))
  4067. (and (nth 4 a)
  4068. (org-remove-flyspell-overlays-in
  4069. (match-beginning 0) (match-end 0)))
  4070. (add-text-properties (match-beginning 2) (match-end 2)
  4071. '(font-lock-multiline t))
  4072. (when org-hide-emphasis-markers
  4073. (add-text-properties (match-end 4) (match-beginning 5)
  4074. '(invisible org-link))
  4075. (add-text-properties (match-beginning 3) (match-end 3)
  4076. '(invisible org-link)))))
  4077. (backward-char 1))
  4078. rtn))
  4079. (defun org-emphasize (&optional char)
  4080. "Insert or change an emphasis, i.e. a font like bold or italic.
  4081. If there is an active region, change that region to a new emphasis.
  4082. If there is no region, just insert the marker characters and position
  4083. the cursor between them.
  4084. CHAR should be either the marker character, or the first character of the
  4085. HTML tag associated with that emphasis. If CHAR is a space, the means
  4086. to remove the emphasis of the selected region.
  4087. If char is not given (for example in an interactive call) it
  4088. will be prompted for."
  4089. (interactive)
  4090. (let ((eal org-emphasis-alist) e det
  4091. (erc org-emphasis-regexp-components)
  4092. (prompt "")
  4093. (string "") beg end move tag c s)
  4094. (if (org-region-active-p)
  4095. (setq beg (region-beginning) end (region-end)
  4096. string (buffer-substring beg end))
  4097. (setq move t))
  4098. (while (setq e (pop eal))
  4099. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4100. c (aref tag 0))
  4101. (push (cons c (string-to-char (car e))) det)
  4102. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4103. (substring tag 1)))))
  4104. (setq det (nreverse det))
  4105. (unless char
  4106. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4107. (setq char (read-char-exclusive)))
  4108. (setq char (or (cdr (assoc char det)) char))
  4109. (if (equal char ?\ )
  4110. (setq s "" move nil)
  4111. (unless (assoc (char-to-string char) org-emphasis-alist)
  4112. (error "No such emphasis marker: \"%c\"" char))
  4113. (setq s (char-to-string char)))
  4114. (while (and (> (length string) 1)
  4115. (equal (substring string 0 1) (substring string -1))
  4116. (assoc (substring string 0 1) org-emphasis-alist))
  4117. (setq string (substring string 1 -1)))
  4118. (setq string (concat s string s))
  4119. (if beg (delete-region beg end))
  4120. (unless (or (bolp)
  4121. (string-match (concat "[" (nth 0 erc) "\n]")
  4122. (char-to-string (char-before (point)))))
  4123. (insert " "))
  4124. (unless (or (eobp)
  4125. (string-match (concat "[" (nth 1 erc) "\n]")
  4126. (char-to-string (char-after (point)))))
  4127. (insert " ") (backward-char 1))
  4128. (insert string)
  4129. (and move (backward-char 1))))
  4130. (defconst org-nonsticky-props
  4131. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4132. (defsubst org-rear-nonsticky-at (pos)
  4133. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4134. (defun org-activate-plain-links (limit)
  4135. "Run through the buffer and add overlays to links."
  4136. (catch 'exit
  4137. (let (f)
  4138. (if (re-search-forward org-plain-link-re limit t)
  4139. (progn
  4140. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4141. (setq f (get-text-property (match-beginning 0) 'face))
  4142. (if (or (eq f 'org-tag)
  4143. (and (listp f) (memq 'org-tag f)))
  4144. nil
  4145. (add-text-properties (match-beginning 0) (match-end 0)
  4146. (list 'mouse-face 'highlight
  4147. 'face 'org-link
  4148. 'keymap org-mouse-map))
  4149. (org-rear-nonsticky-at (match-end 0)))
  4150. t)))))
  4151. (defun org-activate-code (limit)
  4152. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4153. (progn
  4154. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4155. (remove-text-properties (match-beginning 0) (match-end 0)
  4156. '(display t invisible t intangible t))
  4157. t)))
  4158. (defun org-fontify-meta-lines-and-blocks (limit)
  4159. "Fontify #+ lines and blocks, in the correct ways."
  4160. (let ((case-fold-search t))
  4161. (if (re-search-forward
  4162. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)\\(.*\\)\\)"
  4163. limit t)
  4164. (let ((beg (match-beginning 0))
  4165. (beg1 (line-beginning-position 2))
  4166. (dc1 (downcase (match-string 2)))
  4167. (dc3 (downcase (match-string 3)))
  4168. end end1 quoting block-type)
  4169. (cond
  4170. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4171. ;; a single line of backend-specific content
  4172. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4173. (remove-text-properties (match-beginning 0) (match-end 0)
  4174. '(display t invisible t intangible t))
  4175. (add-text-properties (match-beginning 1) (match-end 3)
  4176. '(font-lock-fontified t face org-meta-line))
  4177. (add-text-properties (match-beginning 6) (match-end 6)
  4178. '(font-lock-fontified t face org-block))
  4179. t)
  4180. ((and (match-end 4) (equal dc3 "begin"))
  4181. ;; Truely a block
  4182. (setq block-type (downcase (match-string 5))
  4183. quoting (member block-type org-protecting-blocks))
  4184. (when (re-search-forward
  4185. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4186. nil t) ;; on purpose, we look further than LIMIT
  4187. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4188. (when quoting
  4189. (remove-text-properties beg end
  4190. '(display t invisible t intangible t)))
  4191. (add-text-properties
  4192. beg end
  4193. '(font-lock-fontified t font-lock-multiline t))
  4194. (add-text-properties beg beg1 '(face org-meta-line))
  4195. (add-text-properties end1 end '(face org-meta-line))
  4196. (cond
  4197. (quoting
  4198. (add-text-properties beg1 end1 '(face org-block)))
  4199. ((not org-fontify-quote-and-verse-blocks))
  4200. ((string= block-type "quote")
  4201. (add-text-properties beg1 end1 '(face org-quote)))
  4202. ((string= block-type "verse")
  4203. (add-text-properties beg1 end1 '(face org-verse))))
  4204. t))
  4205. ((member dc1 '("title:" "author:" "email:" "date:"))
  4206. (add-text-properties
  4207. beg (match-end 3)
  4208. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4209. '(font-lock-fontified t invisible t)
  4210. '(font-lock-fontified t face org-document-info-keyword)))
  4211. (add-text-properties
  4212. (match-beginning 6) (match-end 6)
  4213. (if (string-equal dc1 "title:")
  4214. '(font-lock-fontified t face org-document-title)
  4215. '(font-lock-fontified t face org-document-info))))
  4216. ((not (member (char-after beg) '(?\ ?\t)))
  4217. ;; just any other in-buffer setting, but not indented
  4218. (add-text-properties
  4219. beg (match-end 0)
  4220. '(font-lock-fontified t face org-meta-line))
  4221. t)
  4222. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4223. "orgtbl:" "tblfm:" "tblname:"))
  4224. (and (match-end 4) (equal dc3 "attr")))
  4225. (add-text-properties
  4226. beg (match-end 0)
  4227. '(font-lock-fontified t face org-meta-line))
  4228. t)
  4229. ((member dc3 '(" " ""))
  4230. (add-text-properties
  4231. beg (match-end 0)
  4232. '(font-lock-fontified t face font-lock-comment-face)))
  4233. (t nil))))))
  4234. (defun org-activate-angle-links (limit)
  4235. "Run through the buffer and add overlays to links."
  4236. (if (re-search-forward org-angle-link-re limit t)
  4237. (progn
  4238. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4239. (add-text-properties (match-beginning 0) (match-end 0)
  4240. (list 'mouse-face 'highlight
  4241. 'keymap org-mouse-map))
  4242. (org-rear-nonsticky-at (match-end 0))
  4243. t)))
  4244. (defun org-activate-footnote-links (limit)
  4245. "Run through the buffer and add overlays to links."
  4246. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4247. limit t)
  4248. (progn
  4249. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4250. (add-text-properties (match-beginning 2) (match-end 2)
  4251. (list 'mouse-face 'highlight
  4252. 'keymap org-mouse-map
  4253. 'help-echo
  4254. (if (= (point-at-bol) (match-beginning 2))
  4255. "Footnote definition"
  4256. "Footnote reference")
  4257. ))
  4258. (org-rear-nonsticky-at (match-end 2))
  4259. t)))
  4260. (defun org-activate-bracket-links (limit)
  4261. "Run through the buffer and add overlays to bracketed links."
  4262. (if (re-search-forward org-bracket-link-regexp limit t)
  4263. (let* ((help (concat "LINK: "
  4264. (org-match-string-no-properties 1)))
  4265. ;; FIXME: above we should remove the escapes.
  4266. ;; but that requires another match, protecting match data,
  4267. ;; a lot of overhead for font-lock.
  4268. (ip (org-maybe-intangible
  4269. (list 'invisible 'org-link
  4270. 'keymap org-mouse-map 'mouse-face 'highlight
  4271. 'font-lock-multiline t 'help-echo help)))
  4272. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4273. 'font-lock-multiline t 'help-echo help)))
  4274. ;; We need to remove the invisible property here. Table narrowing
  4275. ;; may have made some of this invisible.
  4276. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4277. (remove-text-properties (match-beginning 0) (match-end 0)
  4278. '(invisible nil))
  4279. (if (match-end 3)
  4280. (progn
  4281. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4282. (org-rear-nonsticky-at (match-beginning 3))
  4283. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4284. (org-rear-nonsticky-at (match-end 3))
  4285. (add-text-properties (match-end 3) (match-end 0) ip)
  4286. (org-rear-nonsticky-at (match-end 0)))
  4287. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4288. (org-rear-nonsticky-at (match-beginning 1))
  4289. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4290. (org-rear-nonsticky-at (match-end 1))
  4291. (add-text-properties (match-end 1) (match-end 0) ip)
  4292. (org-rear-nonsticky-at (match-end 0)))
  4293. t)))
  4294. (defun org-activate-dates (limit)
  4295. "Run through the buffer and add overlays to dates."
  4296. (if (re-search-forward org-tsr-regexp-both limit t)
  4297. (progn
  4298. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4299. (add-text-properties (match-beginning 0) (match-end 0)
  4300. (list 'mouse-face 'highlight
  4301. 'keymap org-mouse-map))
  4302. (org-rear-nonsticky-at (match-end 0))
  4303. (when org-display-custom-times
  4304. (if (match-end 3)
  4305. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4306. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4307. t)))
  4308. (defvar org-target-link-regexp nil
  4309. "Regular expression matching radio targets in plain text.")
  4310. (make-variable-buffer-local 'org-target-link-regexp)
  4311. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4312. "Regular expression matching a link target.")
  4313. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4314. "Regular expression matching a radio target.")
  4315. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4316. "Regular expression matching any target.")
  4317. (defun org-activate-target-links (limit)
  4318. "Run through the buffer and add overlays to target matches."
  4319. (when org-target-link-regexp
  4320. (let ((case-fold-search t))
  4321. (if (re-search-forward org-target-link-regexp limit t)
  4322. (progn
  4323. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4324. (add-text-properties (match-beginning 0) (match-end 0)
  4325. (list 'mouse-face 'highlight
  4326. 'keymap org-mouse-map
  4327. 'help-echo "Radio target link"
  4328. 'org-linked-text t))
  4329. (org-rear-nonsticky-at (match-end 0))
  4330. t)))))
  4331. (defun org-update-radio-target-regexp ()
  4332. "Find all radio targets in this file and update the regular expression."
  4333. (interactive)
  4334. (when (memq 'radio org-activate-links)
  4335. (setq org-target-link-regexp
  4336. (org-make-target-link-regexp (org-all-targets 'radio)))
  4337. (org-restart-font-lock)))
  4338. (defun org-hide-wide-columns (limit)
  4339. (let (s e)
  4340. (setq s (text-property-any (point) (or limit (point-max))
  4341. 'org-cwidth t))
  4342. (when s
  4343. (setq e (next-single-property-change s 'org-cwidth))
  4344. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4345. (goto-char e)
  4346. t)))
  4347. (defvar org-latex-and-specials-regexp nil
  4348. "Regular expression for highlighting export special stuff.")
  4349. (defvar org-match-substring-regexp)
  4350. (defvar org-match-substring-with-braces-regexp)
  4351. ;; This should be with the exporter code, but we also use if for font-locking
  4352. (defconst org-export-html-special-string-regexps
  4353. '(("\\\\-" . "&shy;")
  4354. ("---\\([^-]\\)" . "&mdash;\\1")
  4355. ("--\\([^-]\\)" . "&ndash;\\1")
  4356. ("\\.\\.\\." . "&hellip;"))
  4357. "Regular expressions for special string conversion.")
  4358. (defun org-compute-latex-and-specials-regexp ()
  4359. "Compute regular expression for stuff treated specially by exporters."
  4360. (if (not org-highlight-latex-fragments-and-specials)
  4361. (org-set-local 'org-latex-and-specials-regexp nil)
  4362. (require 'org-exp)
  4363. (let*
  4364. ((matchers (plist-get org-format-latex-options :matchers))
  4365. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4366. org-latex-regexps)))
  4367. (org-export-allow-BIND nil)
  4368. (options (org-combine-plists (org-default-export-plist)
  4369. (org-infile-export-plist)))
  4370. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4371. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4372. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4373. (org-export-html-expand (plist-get options :expand-quoted-html))
  4374. (org-export-with-special-strings (plist-get options :special-strings))
  4375. (re-sub
  4376. (cond
  4377. ((equal org-export-with-sub-superscripts '{})
  4378. (list org-match-substring-with-braces-regexp))
  4379. (org-export-with-sub-superscripts
  4380. (list org-match-substring-regexp))
  4381. (t nil)))
  4382. (re-latex
  4383. (if org-export-with-LaTeX-fragments
  4384. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4385. (re-macros
  4386. (if org-export-with-TeX-macros
  4387. (list (concat "\\\\"
  4388. (regexp-opt
  4389. (append (mapcar 'car (append org-entities-user
  4390. org-entities))
  4391. (if (boundp 'org-latex-entities)
  4392. (mapcar (lambda (x)
  4393. (or (car-safe x) x))
  4394. org-latex-entities)
  4395. nil))
  4396. 'words))) ; FIXME
  4397. ))
  4398. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4399. (re-special (if org-export-with-special-strings
  4400. (mapcar (lambda (x) (car x))
  4401. org-export-html-special-string-regexps)))
  4402. (re-rest
  4403. (delq nil
  4404. (list
  4405. (if org-export-html-expand "@<[^>\n]+>")
  4406. ))))
  4407. (org-set-local
  4408. 'org-latex-and-specials-regexp
  4409. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4410. re-rest) "\\|")))))
  4411. (defun org-do-latex-and-special-faces (limit)
  4412. "Run through the buffer and add overlays to links."
  4413. (when org-latex-and-specials-regexp
  4414. (let (rtn d)
  4415. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4416. limit t))
  4417. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4418. 'face))
  4419. '(org-code org-verbatim underline)))
  4420. (progn
  4421. (setq rtn t
  4422. d (cond ((member (char-after (1+ (match-beginning 0)))
  4423. '(?_ ?^)) 1)
  4424. (t 0)))
  4425. (font-lock-prepend-text-property
  4426. (+ d (match-beginning 0)) (match-end 0)
  4427. 'face 'org-latex-and-export-specials)
  4428. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4429. '(font-lock-multiline t)))))
  4430. rtn)))
  4431. (defun org-restart-font-lock ()
  4432. "Restart font-lock-mode, to force refontification."
  4433. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4434. (font-lock-mode -1)
  4435. (font-lock-mode 1)))
  4436. (defun org-all-targets (&optional radio)
  4437. "Return a list of all targets in this file.
  4438. With optional argument RADIO, only find radio targets."
  4439. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4440. rtn)
  4441. (save-excursion
  4442. (goto-char (point-min))
  4443. (while (re-search-forward re nil t)
  4444. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4445. rtn)))
  4446. (defun org-make-target-link-regexp (targets)
  4447. "Make regular expression matching all strings in TARGETS.
  4448. The regular expression finds the targets also if there is a line break
  4449. between words."
  4450. (and targets
  4451. (concat
  4452. "\\<\\("
  4453. (mapconcat
  4454. (lambda (x)
  4455. (while (string-match " +" x)
  4456. (setq x (replace-match "\\s-+" t t x)))
  4457. x)
  4458. targets
  4459. "\\|")
  4460. "\\)\\>")))
  4461. (defun org-activate-tags (limit)
  4462. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  4463. (progn
  4464. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4465. (add-text-properties (match-beginning 1) (match-end 1)
  4466. (list 'mouse-face 'highlight
  4467. 'keymap org-mouse-map))
  4468. (org-rear-nonsticky-at (match-end 1))
  4469. t)))
  4470. (defun org-outline-level ()
  4471. "Compute the outline level of the heading at point.
  4472. This function assumes that the cursor is at the beginning of a line matched
  4473. by outline-regexp. Otherwise it returns garbage.
  4474. If this is called at a normal headline, the level is the number of stars.
  4475. Use `org-reduced-level' to remove the effect of `org-odd-levels'.
  4476. For plain list items, if they are matched by `outline-regexp', this returns
  4477. 1000 plus the line indentation."
  4478. (save-excursion
  4479. (looking-at outline-regexp)
  4480. (if (match-beginning 1)
  4481. (+ (org-get-string-indentation (match-string 1)) 1000)
  4482. (1- (- (match-end 0) (match-beginning 0))))))
  4483. (defvar org-font-lock-keywords nil)
  4484. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4485. "Regular expression matching a property line.")
  4486. (defvar org-font-lock-hook nil
  4487. "Functions to be called for special font lock stuff.")
  4488. (defun org-font-lock-hook (limit)
  4489. (run-hook-with-args 'org-font-lock-hook limit))
  4490. (defun org-set-font-lock-defaults ()
  4491. (let* ((em org-fontify-emphasized-text)
  4492. (lk org-activate-links)
  4493. (org-font-lock-extra-keywords
  4494. (list
  4495. ;; Call the hook
  4496. '(org-font-lock-hook)
  4497. ;; Headlines
  4498. `(,(if org-fontify-whole-heading-line
  4499. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4500. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4501. (1 (org-get-level-face 1))
  4502. (2 (org-get-level-face 2))
  4503. (3 (org-get-level-face 3)))
  4504. ;; Table lines
  4505. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4506. (1 'org-table t))
  4507. ;; Table internals
  4508. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4509. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4510. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4511. '("| *\\(<[lr]?[0-9]*>\\)" (1 'org-formula t))
  4512. ;; Drawers
  4513. (list org-drawer-regexp '(0 'org-special-keyword t))
  4514. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4515. ;; Properties
  4516. (list org-property-re
  4517. '(1 'org-special-keyword t)
  4518. '(3 'org-property-value t))
  4519. ;; Links
  4520. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4521. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4522. (if (memq 'plain lk) '(org-activate-plain-links))
  4523. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4524. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4525. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4526. (if (memq 'footnote lk) '(org-activate-footnote-links
  4527. (2 'org-footnote t)))
  4528. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4529. '(org-hide-wide-columns (0 nil append))
  4530. ;; TODO lines
  4531. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4532. '(1 (org-get-todo-face 1) t))
  4533. ;; DONE
  4534. (if org-fontify-done-headline
  4535. (list (concat "^[*]+ +\\<\\("
  4536. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4537. "\\)\\(.*\\)")
  4538. '(2 'org-headline-done t))
  4539. nil)
  4540. ;; Priorities
  4541. '(org-font-lock-add-priority-faces)
  4542. ;; Tags
  4543. '(org-font-lock-add-tag-faces)
  4544. ;; Special keywords
  4545. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4546. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4547. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4548. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4549. ;; Emphasis
  4550. (if em
  4551. (if (featurep 'xemacs)
  4552. '(org-do-emphasis-faces (0 nil append))
  4553. '(org-do-emphasis-faces)))
  4554. ;; Checkboxes
  4555. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  4556. 2 'org-checkbox prepend)
  4557. (if org-provide-checkbox-statistics
  4558. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4559. (0 (org-get-checkbox-statistics-face) t)))
  4560. ;; Description list items
  4561. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  4562. 2 'bold prepend)
  4563. ;; ARCHIVEd headings
  4564. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4565. '(1 'org-archived prepend))
  4566. ;; Specials
  4567. '(org-do-latex-and-special-faces)
  4568. ;; Code
  4569. '(org-activate-code (1 'org-code t))
  4570. ;; COMMENT
  4571. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4572. "\\|" org-quote-string "\\)\\>")
  4573. '(1 'org-special-keyword t))
  4574. '("^#.*" (0 'font-lock-comment-face t))
  4575. ;; Blocks and meta lines
  4576. '(org-fontify-meta-lines-and-blocks)
  4577. )))
  4578. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4579. ;; Now set the full font-lock-keywords
  4580. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  4581. (org-set-local 'font-lock-defaults
  4582. '(org-font-lock-keywords t nil nil backward-paragraph))
  4583. (kill-local-variable 'font-lock-keywords) nil))
  4584. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  4585. "Fontify string S like in Org-mode"
  4586. (with-temp-buffer
  4587. (insert s)
  4588. (let ((org-odd-levels-only odd-levels))
  4589. (org-mode)
  4590. (font-lock-fontify-buffer)
  4591. (buffer-string))))
  4592. (defvar org-m nil)
  4593. (defvar org-l nil)
  4594. (defvar org-f nil)
  4595. (defun org-get-level-face (n)
  4596. "Get the right face for match N in font-lock matching of headlines."
  4597. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  4598. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  4599. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  4600. (cond
  4601. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  4602. ((eq n 2) org-f)
  4603. (t (if org-level-color-stars-only nil org-f))))
  4604. (defun org-get-todo-face (kwd)
  4605. "Get the right face for a TODO keyword KWD.
  4606. If KWD is a number, get the corresponding match group."
  4607. (if (numberp kwd) (setq kwd (match-string kwd)))
  4608. (or (org-face-from-face-or-color
  4609. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  4610. (and (member kwd org-done-keywords) 'org-done)
  4611. 'org-todo))
  4612. (defun org-face-from-face-or-color (context inherit face-or-color)
  4613. "Create a face list that inherits INHERIT, but sets the foreground color.
  4614. When FACE-OR-COLOR is not a string, just return it."
  4615. (if (stringp face-or-color)
  4616. (list :inherit inherit
  4617. (cdr (assoc context org-faces-easy-properties))
  4618. face-or-color)
  4619. face-or-color))
  4620. (defun org-font-lock-add-tag-faces (limit)
  4621. "Add the special tag faces."
  4622. (when (and org-tag-faces org-tags-special-faces-re)
  4623. (while (re-search-forward org-tags-special-faces-re limit t)
  4624. (add-text-properties (match-beginning 1) (match-end 1)
  4625. (list 'face (org-get-tag-face 1)
  4626. 'font-lock-fontified t))
  4627. (backward-char 1))))
  4628. (defun org-font-lock-add-priority-faces (limit)
  4629. "Add the special priority faces."
  4630. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  4631. (add-text-properties
  4632. (match-beginning 0) (match-end 0)
  4633. (list 'face (or (org-face-from-face-or-color
  4634. 'priority 'org-special-keyword
  4635. (cdr (assoc (char-after (match-beginning 1))
  4636. org-priority-faces)))
  4637. 'org-special-keyword)
  4638. 'font-lock-fontified t))))
  4639. (defun org-get-tag-face (kwd)
  4640. "Get the right face for a TODO keyword KWD.
  4641. If KWD is a number, get the corresponding match group."
  4642. (if (numberp kwd) (setq kwd (match-string kwd)))
  4643. (or (org-face-from-face-or-color
  4644. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  4645. 'org-tag))
  4646. (defun org-unfontify-region (beg end &optional maybe_loudly)
  4647. "Remove fontification and activation overlays from links."
  4648. (font-lock-default-unfontify-region beg end)
  4649. (let* ((buffer-undo-list t)
  4650. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  4651. (inhibit-modification-hooks t)
  4652. deactivate-mark buffer-file-name buffer-file-truename)
  4653. (remove-text-properties
  4654. beg end
  4655. (if org-indent-mode
  4656. ;; also remove line-prefix and wrap-prefix properties
  4657. '(mouse-face t keymap t org-linked-text t
  4658. invisible t intangible t
  4659. line-prefix t wrap-prefix t
  4660. org-no-flyspell t)
  4661. '(mouse-face t keymap t org-linked-text t
  4662. invisible t intangible t
  4663. org-no-flyspell t)))))
  4664. ;;;; Visibility cycling, including org-goto and indirect buffer
  4665. ;;; Cycling
  4666. (defvar org-cycle-global-status nil)
  4667. (make-variable-buffer-local 'org-cycle-global-status)
  4668. (defvar org-cycle-subtree-status nil)
  4669. (make-variable-buffer-local 'org-cycle-subtree-status)
  4670. ;;;###autoload
  4671. (defvar org-inlinetask-min-level)
  4672. (defun org-cycle (&optional arg)
  4673. "TAB-action and visibility cycling for Org-mode.
  4674. This is the command invoked in Org-mode by the TAB key. Its main purpose
  4675. is outline visibility cycling, but it also invokes other actions
  4676. in special contexts.
  4677. - When this function is called with a prefix argument, rotate the entire
  4678. buffer through 3 states (global cycling)
  4679. 1. OVERVIEW: Show only top-level headlines.
  4680. 2. CONTENTS: Show all headlines of all levels, but no body text.
  4681. 3. SHOW ALL: Show everything.
  4682. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  4683. determined by the variable `org-startup-folded', and by any VISIBILITY
  4684. properties in the buffer.
  4685. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  4686. including any drawers.
  4687. - When inside a table, re-align the table and move to the next field.
  4688. - When point is at the beginning of a headline, rotate the subtree started
  4689. by this line through 3 different states (local cycling)
  4690. 1. FOLDED: Only the main headline is shown.
  4691. 2. CHILDREN: The main headline and the direct children are shown.
  4692. From this state, you can move to one of the children
  4693. and zoom in further.
  4694. 3. SUBTREE: Show the entire subtree, including body text.
  4695. If there is no subtree, switch directly from CHILDREN to FOLDED.
  4696. - When point is at the beginning of an empty headline and the variable
  4697. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  4698. of the headline by demoting and promoting it to likely levels. This
  4699. speeds up creation document structure by presing TAB once or several
  4700. times right after creating a new headline.
  4701. - When there is a numeric prefix, go up to a heading with level ARG, do
  4702. a `show-subtree' and return to the previous cursor position. If ARG
  4703. is negative, go up that many levels.
  4704. - When point is not at the beginning of a headline, execute the global
  4705. binding for TAB, which is re-indenting the line. See the option
  4706. `org-cycle-emulate-tab' for details.
  4707. - Special case: if point is at the beginning of the buffer and there is
  4708. no headline in line 1, this function will act as if called with prefix arg.
  4709. But only if also the variable `org-cycle-global-at-bob' is t."
  4710. (interactive "P")
  4711. (org-load-modules-maybe)
  4712. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  4713. (and org-cycle-level-after-item/entry-creation
  4714. (or (org-cycle-level)
  4715. (org-cycle-item-indentation))))
  4716. (let* ((limit-level
  4717. (or org-cycle-max-level
  4718. (and (boundp 'org-inlinetask-min-level)
  4719. org-inlinetask-min-level
  4720. (1- org-inlinetask-min-level))))
  4721. (nstars (and limit-level
  4722. (if org-odd-levels-only
  4723. (and limit-level (1- (* limit-level 2)))
  4724. limit-level)))
  4725. (outline-regexp
  4726. (cond
  4727. ((not (org-mode-p)) outline-regexp)
  4728. ((or (eq org-cycle-include-plain-lists 'integrate)
  4729. (and org-cycle-include-plain-lists (org-at-item-p)))
  4730. (concat "\\(?:\\*"
  4731. (if nstars (format "\\{1,%d\\}" nstars) "+")
  4732. " \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"))
  4733. (t (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ ")))))
  4734. (bob-special (and org-cycle-global-at-bob (bobp)
  4735. (not (looking-at outline-regexp))))
  4736. (org-cycle-hook
  4737. (if bob-special
  4738. (delq 'org-optimize-window-after-visibility-change
  4739. (copy-sequence org-cycle-hook))
  4740. org-cycle-hook))
  4741. (pos (point)))
  4742. (if (or bob-special (equal arg '(4)))
  4743. ;; special case: use global cycling
  4744. (setq arg t))
  4745. (cond
  4746. ((equal arg '(16))
  4747. (org-set-startup-visibility)
  4748. (message "Startup visibility, plus VISIBILITY properties"))
  4749. ((equal arg '(64))
  4750. (show-all)
  4751. (message "Entire buffer visible, including drawers"))
  4752. ((org-at-table-p 'any)
  4753. ;; Enter the table or move to the next field in the table
  4754. (if (org-at-table.el-p)
  4755. (message "Use C-c ' to edit table.el tables")
  4756. (if arg (org-table-edit-field t)
  4757. (org-table-justify-field-maybe)
  4758. (call-interactively 'org-table-next-field))))
  4759. ((run-hook-with-args-until-success
  4760. 'org-tab-after-check-for-table-hook))
  4761. ((eq arg t) ;; Global cycling
  4762. (org-cycle-internal-global))
  4763. ((and org-drawers org-drawer-regexp
  4764. (save-excursion
  4765. (beginning-of-line 1)
  4766. (looking-at org-drawer-regexp)))
  4767. ;; Toggle block visibility
  4768. (org-flag-drawer
  4769. (not (get-char-property (match-end 0) 'invisible))))
  4770. ((integerp arg)
  4771. ;; Show-subtree, ARG levels up from here.
  4772. (save-excursion
  4773. (org-back-to-heading)
  4774. (outline-up-heading (if (< arg 0) (- arg)
  4775. (- (funcall outline-level) arg)))
  4776. (org-show-subtree)))
  4777. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4778. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4779. (org-cycle-internal-local))
  4780. ;; TAB emulation and template completion
  4781. (buffer-read-only (org-back-to-heading))
  4782. ((run-hook-with-args-until-success
  4783. 'org-tab-after-check-for-cycling-hook))
  4784. ((org-try-structure-completion))
  4785. ((org-try-cdlatex-tab))
  4786. ((run-hook-with-args-until-success
  4787. 'org-tab-before-tab-emulation-hook))
  4788. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4789. (or (not (bolp))
  4790. (not (looking-at outline-regexp))))
  4791. (call-interactively (global-key-binding "\t")))
  4792. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4793. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4794. (or (and (eq org-cycle-emulate-tab 'white)
  4795. (= (match-end 0) (point-at-eol)))
  4796. (and (eq org-cycle-emulate-tab 'whitestart)
  4797. (>= (match-end 0) pos))))
  4798. t
  4799. (eq org-cycle-emulate-tab t))
  4800. (call-interactively (global-key-binding "\t")))
  4801. (t (save-excursion
  4802. (org-back-to-heading)
  4803. (org-cycle)))))))
  4804. (defun org-cycle-internal-global ()
  4805. "Do the global cycling action."
  4806. (cond
  4807. ((and (eq last-command this-command)
  4808. (eq org-cycle-global-status 'overview))
  4809. ;; We just created the overview - now do table of contents
  4810. ;; This can be slow in very large buffers, so indicate action
  4811. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  4812. (message "CONTENTS...")
  4813. (org-content)
  4814. (message "CONTENTS...done")
  4815. (setq org-cycle-global-status 'contents)
  4816. (run-hook-with-args 'org-cycle-hook 'contents))
  4817. ((and (eq last-command this-command)
  4818. (eq org-cycle-global-status 'contents))
  4819. ;; We just showed the table of contents - now show everything
  4820. (run-hook-with-args 'org-pre-cycle-hook 'all)
  4821. (show-all)
  4822. (message "SHOW ALL")
  4823. (setq org-cycle-global-status 'all)
  4824. (run-hook-with-args 'org-cycle-hook 'all))
  4825. (t
  4826. ;; Default action: go to overview
  4827. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  4828. (org-overview)
  4829. (message "OVERVIEW")
  4830. (setq org-cycle-global-status 'overview)
  4831. (run-hook-with-args 'org-cycle-hook 'overview))))
  4832. (defun org-cycle-internal-local ()
  4833. "Do the local cycling action."
  4834. (org-back-to-heading)
  4835. (let ((goal-column 0) eoh eol eos level has-children children-skipped)
  4836. ;; First, some boundaries
  4837. (save-excursion
  4838. (org-back-to-heading)
  4839. (setq level (funcall outline-level))
  4840. (save-excursion
  4841. (beginning-of-line 2)
  4842. (if (or (featurep 'xemacs) (<= emacs-major-version 21))
  4843. ; XEmacs does not have `next-single-char-property-change'
  4844. ; I'm not sure about Emacs 21.
  4845. (while (and (not (eobp)) ;; this is like `next-line'
  4846. (get-char-property (1- (point)) 'invisible))
  4847. (beginning-of-line 2))
  4848. (while (and (not (eobp)) ;; this is like `next-line'
  4849. (get-char-property (1- (point)) 'invisible))
  4850. (goto-char (next-single-char-property-change (point) 'invisible))
  4851. ;;;??? (or (bolp) (beginning-of-line 2))))
  4852. (and (eolp) (beginning-of-line 2))))
  4853. (setq eol (point)))
  4854. (outline-end-of-heading) (setq eoh (point))
  4855. (save-excursion
  4856. (outline-next-heading)
  4857. (setq has-children (and (org-at-heading-p t)
  4858. (> (funcall outline-level) level))))
  4859. (org-end-of-subtree t)
  4860. (unless (eobp)
  4861. (skip-chars-forward " \t\n")
  4862. (beginning-of-line 1) ; in case this is an item
  4863. )
  4864. (setq eos (if (eobp) (point) (1- (point)))))
  4865. ;; Find out what to do next and set `this-command'
  4866. (cond
  4867. ((= eos eoh)
  4868. ;; Nothing is hidden behind this heading
  4869. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  4870. (message "EMPTY ENTRY")
  4871. (setq org-cycle-subtree-status nil)
  4872. (save-excursion
  4873. (goto-char eos)
  4874. (outline-next-heading)
  4875. (if (org-invisible-p) (org-flag-heading nil))))
  4876. ((and (or (>= eol eos)
  4877. (not (string-match "\\S-" (buffer-substring eol eos))))
  4878. (or has-children
  4879. (not (setq children-skipped
  4880. org-cycle-skip-children-state-if-no-children))))
  4881. ;; Entire subtree is hidden in one line: children view
  4882. (run-hook-with-args 'org-pre-cycle-hook 'children)
  4883. (org-show-entry)
  4884. (show-children)
  4885. (message "CHILDREN")
  4886. (save-excursion
  4887. (goto-char eos)
  4888. (outline-next-heading)
  4889. (if (org-invisible-p) (org-flag-heading nil)))
  4890. (setq org-cycle-subtree-status 'children)
  4891. (run-hook-with-args 'org-cycle-hook 'children))
  4892. ((or children-skipped
  4893. (and (eq last-command this-command)
  4894. (eq org-cycle-subtree-status 'children)))
  4895. ;; We just showed the children, or no children are there,
  4896. ;; now show everything.
  4897. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  4898. (org-show-subtree)
  4899. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  4900. (setq org-cycle-subtree-status 'subtree)
  4901. (run-hook-with-args 'org-cycle-hook 'subtree))
  4902. (t
  4903. ;; Default action: hide the subtree.
  4904. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  4905. (hide-subtree)
  4906. (message "FOLDED")
  4907. (setq org-cycle-subtree-status 'folded)
  4908. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4909. ;;;###autoload
  4910. (defun org-global-cycle (&optional arg)
  4911. "Cycle the global visibility. For details see `org-cycle'.
  4912. With C-u prefix arg, switch to startup visibility.
  4913. With a numeric prefix, show all headlines up to that level."
  4914. (interactive "P")
  4915. (let ((org-cycle-include-plain-lists
  4916. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4917. (cond
  4918. ((integerp arg)
  4919. (show-all)
  4920. (hide-sublevels arg)
  4921. (setq org-cycle-global-status 'contents))
  4922. ((equal arg '(4))
  4923. (org-set-startup-visibility)
  4924. (message "Startup visibility, plus VISIBILITY properties."))
  4925. (t
  4926. (org-cycle '(4))))))
  4927. (defun org-set-startup-visibility ()
  4928. "Set the visibility required by startup options and properties."
  4929. (cond
  4930. ((eq org-startup-folded t)
  4931. (org-cycle '(4)))
  4932. ((eq org-startup-folded 'content)
  4933. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4934. (org-cycle '(4)) (org-cycle '(4)))))
  4935. (unless (eq org-startup-folded 'showeverything)
  4936. (if org-hide-block-startup (org-hide-block-all))
  4937. (org-set-visibility-according-to-property 'no-cleanup)
  4938. (org-cycle-hide-archived-subtrees 'all)
  4939. (org-cycle-hide-drawers 'all)
  4940. (org-cycle-show-empty-lines 'all)))
  4941. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4942. "Switch subtree visibilities according to :VISIBILITY: property."
  4943. (interactive)
  4944. (let (org-show-entry-below state)
  4945. (save-excursion
  4946. (goto-char (point-min))
  4947. (while (re-search-forward
  4948. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4949. nil t)
  4950. (setq state (match-string 1))
  4951. (save-excursion
  4952. (org-back-to-heading t)
  4953. (hide-subtree)
  4954. (org-reveal)
  4955. (cond
  4956. ((equal state '("fold" "folded"))
  4957. (hide-subtree))
  4958. ((equal state "children")
  4959. (org-show-hidden-entry)
  4960. (show-children))
  4961. ((equal state "content")
  4962. (save-excursion
  4963. (save-restriction
  4964. (org-narrow-to-subtree)
  4965. (org-content))))
  4966. ((member state '("all" "showall"))
  4967. (show-subtree)))))
  4968. (unless no-cleanup
  4969. (org-cycle-hide-archived-subtrees 'all)
  4970. (org-cycle-hide-drawers 'all)
  4971. (org-cycle-show-empty-lines 'all)))))
  4972. (defun org-overview ()
  4973. "Switch to overview mode, showing only top-level headlines.
  4974. Really, this shows all headlines with level equal or greater than the level
  4975. of the first headline in the buffer. This is important, because if the
  4976. first headline is not level one, then (hide-sublevels 1) gives confusing
  4977. results."
  4978. (interactive)
  4979. (let ((level (save-excursion
  4980. (goto-char (point-min))
  4981. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4982. (progn
  4983. (goto-char (match-beginning 0))
  4984. (funcall outline-level))))))
  4985. (and level (hide-sublevels level))))
  4986. (defun org-content (&optional arg)
  4987. "Show all headlines in the buffer, like a table of contents.
  4988. With numerical argument N, show content up to level N."
  4989. (interactive "P")
  4990. (save-excursion
  4991. ;; Visit all headings and show their offspring
  4992. (and (integerp arg) (org-overview))
  4993. (goto-char (point-max))
  4994. (catch 'exit
  4995. (while (and (progn (condition-case nil
  4996. (outline-previous-visible-heading 1)
  4997. (error (goto-char (point-min))))
  4998. t)
  4999. (looking-at outline-regexp))
  5000. (if (integerp arg)
  5001. (show-children (1- arg))
  5002. (show-branches))
  5003. (if (bobp) (throw 'exit nil))))))
  5004. (defun org-optimize-window-after-visibility-change (state)
  5005. "Adjust the window after a change in outline visibility.
  5006. This function is the default value of the hook `org-cycle-hook'."
  5007. (when (get-buffer-window (current-buffer))
  5008. (cond
  5009. ((eq state 'content) nil)
  5010. ((eq state 'all) nil)
  5011. ((eq state 'folded) nil)
  5012. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5013. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5014. (defun org-remove-empty-overlays-at (pos)
  5015. "Remove outline overlays that do not contain non-white stuff."
  5016. (mapc
  5017. (lambda (o)
  5018. (and (eq 'outline (org-overlay-get o 'invisible))
  5019. (not (string-match "\\S-" (buffer-substring (org-overlay-start o)
  5020. (org-overlay-end o))))
  5021. (org-delete-overlay o)))
  5022. (org-overlays-at pos)))
  5023. (defun org-clean-visibility-after-subtree-move ()
  5024. "Fix visibility issues after moving a subtree."
  5025. ;; First, find a reasonable region to look at:
  5026. ;; Start two siblings above, end three below
  5027. (let* ((beg (save-excursion
  5028. (and (org-get-last-sibling)
  5029. (org-get-last-sibling))
  5030. (point)))
  5031. (end (save-excursion
  5032. (and (org-get-next-sibling)
  5033. (org-get-next-sibling)
  5034. (org-get-next-sibling))
  5035. (if (org-at-heading-p)
  5036. (point-at-eol)
  5037. (point))))
  5038. (level (looking-at "\\*+"))
  5039. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5040. (save-excursion
  5041. (save-restriction
  5042. (narrow-to-region beg end)
  5043. (when re
  5044. ;; Properly fold already folded siblings
  5045. (goto-char (point-min))
  5046. (while (re-search-forward re nil t)
  5047. (if (and (not (org-invisible-p))
  5048. (save-excursion
  5049. (goto-char (point-at-eol)) (org-invisible-p)))
  5050. (hide-entry))))
  5051. (org-cycle-show-empty-lines 'overview)
  5052. (org-cycle-hide-drawers 'overview)))))
  5053. (defun org-cycle-show-empty-lines (state)
  5054. "Show empty lines above all visible headlines.
  5055. The region to be covered depends on STATE when called through
  5056. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5057. entire buffer covered. Note that an empty line is only shown if there
  5058. are at least `org-cycle-separator-lines' empty lines before the headline."
  5059. (when (not (= org-cycle-separator-lines 0))
  5060. (save-excursion
  5061. (let* ((n (abs org-cycle-separator-lines))
  5062. (re (cond
  5063. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5064. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5065. (t (let ((ns (number-to-string (- n 2))))
  5066. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5067. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5068. beg end b e)
  5069. (cond
  5070. ((memq state '(overview contents t))
  5071. (setq beg (point-min) end (point-max)))
  5072. ((memq state '(children folded))
  5073. (setq beg (point) end (progn (org-end-of-subtree t t)
  5074. (beginning-of-line 2)
  5075. (point)))))
  5076. (when beg
  5077. (goto-char beg)
  5078. (while (re-search-forward re end t)
  5079. (unless (get-char-property (match-end 1) 'invisible)
  5080. (setq e (match-end 1))
  5081. (if (< org-cycle-separator-lines 0)
  5082. (setq b (save-excursion
  5083. (goto-char (match-beginning 0))
  5084. (org-back-over-empty-lines)
  5085. (if (save-excursion
  5086. (goto-char (max (point-min) (1- (point))))
  5087. (org-on-heading-p))
  5088. (1- (point))
  5089. (point))))
  5090. (setq b (match-beginning 1)))
  5091. (outline-flag-region b e nil)))))))
  5092. ;; Never hide empty lines at the end of the file.
  5093. (save-excursion
  5094. (goto-char (point-max))
  5095. (outline-previous-heading)
  5096. (outline-end-of-heading)
  5097. (if (and (looking-at "[ \t\n]+")
  5098. (= (match-end 0) (point-max)))
  5099. (outline-flag-region (point) (match-end 0) nil))))
  5100. (defun org-show-empty-lines-in-parent ()
  5101. "Move to the parent and re-show empty lines before visible headlines."
  5102. (save-excursion
  5103. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5104. (org-cycle-show-empty-lines context))))
  5105. (defun org-files-list ()
  5106. "Return `org-agenda-files' list, plus all open org-mode files.
  5107. This is useful for operations that need to scan all of a user's
  5108. open and agenda-wise Org files."
  5109. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5110. (dolist (buf (buffer-list))
  5111. (with-current-buffer buf
  5112. (if (and (eq major-mode 'org-mode) (buffer-file-name))
  5113. (let ((file (expand-file-name (buffer-file-name))))
  5114. (unless (member file files)
  5115. (push file files))))))
  5116. files))
  5117. (defsubst org-entry-beginning-position ()
  5118. "Return the beginning position of the current entry."
  5119. (save-excursion (outline-back-to-heading t) (point)))
  5120. (defsubst org-entry-end-position ()
  5121. "Return the end position of the current entry."
  5122. (save-excursion (outline-next-heading) (point)))
  5123. (defun org-cycle-hide-drawers (state)
  5124. "Re-hide all drawers after a visibility state change."
  5125. (when (and (org-mode-p)
  5126. (not (memq state '(overview folded contents))))
  5127. (save-excursion
  5128. (let* ((globalp (memq state '(contents all)))
  5129. (beg (if globalp (point-min) (point)))
  5130. (end (if globalp (point-max)
  5131. (if (eq state 'children)
  5132. (save-excursion (outline-next-heading) (point))
  5133. (org-end-of-subtree t)))))
  5134. (goto-char beg)
  5135. (while (re-search-forward org-drawer-regexp end t)
  5136. (org-flag-drawer t))))))
  5137. (defun org-flag-drawer (flag)
  5138. (save-excursion
  5139. (beginning-of-line 1)
  5140. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5141. (let ((b (match-end 0))
  5142. (outline-regexp org-outline-regexp))
  5143. (if (re-search-forward
  5144. "^[ \t]*:END:"
  5145. (save-excursion (outline-next-heading) (point)) t)
  5146. (outline-flag-region b (point-at-eol) flag)
  5147. (error ":END: line missing at position %s" b))))))
  5148. (defun org-subtree-end-visible-p ()
  5149. "Is the end of the current subtree visible?"
  5150. (pos-visible-in-window-p
  5151. (save-excursion (org-end-of-subtree t) (point))))
  5152. (defun org-first-headline-recenter (&optional N)
  5153. "Move cursor to the first headline and recenter the headline.
  5154. Optional argument N means put the headline into the Nth line of the window."
  5155. (goto-char (point-min))
  5156. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5157. (beginning-of-line)
  5158. (recenter (prefix-numeric-value N))))
  5159. ;;; Saving and restoring visibility
  5160. (defun org-outline-overlay-data (&optional use-markers)
  5161. "Return a list of the locations of all outline overlays.
  5162. The are overlays with the `invisible' property value `outline'.
  5163. The return valus is a list of cons cells, with start and stop
  5164. positions for each overlay.
  5165. If USE-MARKERS is set, return the positions as markers."
  5166. (let (beg end)
  5167. (save-excursion
  5168. (save-restriction
  5169. (widen)
  5170. (delq nil
  5171. (mapcar (lambda (o)
  5172. (when (eq (org-overlay-get o 'invisible) 'outline)
  5173. (setq beg (org-overlay-start o)
  5174. end (org-overlay-end o))
  5175. (and beg end (> end beg)
  5176. (if use-markers
  5177. (cons (move-marker (make-marker) beg)
  5178. (move-marker (make-marker) end))
  5179. (cons beg end)))))
  5180. (org-overlays-in (point-min) (point-max))))))))
  5181. (defun org-set-outline-overlay-data (data)
  5182. "Create visibility overlays for all positions in DATA.
  5183. DATA should have been made by `org-outline-overlay-data'."
  5184. (let (o)
  5185. (save-excursion
  5186. (save-restriction
  5187. (widen)
  5188. (show-all)
  5189. (mapc (lambda (c)
  5190. (setq o (org-make-overlay (car c) (cdr c)))
  5191. (org-overlay-put o 'invisible 'outline))
  5192. data)))))
  5193. (defmacro org-save-outline-visibility (use-markers &rest body)
  5194. "Save and restore outline visibility around BODY.
  5195. If USE-MARKERS is non-nil, use markers for the positions.
  5196. This means that the buffer may change while running BODY,
  5197. but it also means that the buffer should stay alive
  5198. during the operation, because otherwise all these markers will
  5199. point nowhere."
  5200. `(let ((data (org-outline-overlay-data ,use-markers)))
  5201. (unwind-protect
  5202. (progn
  5203. ,@body
  5204. (org-set-outline-overlay-data data))
  5205. (when ,use-markers
  5206. (mapc (lambda (c)
  5207. (and (markerp (car c)) (move-marker (car c) nil))
  5208. (and (markerp (cdr c)) (move-marker (cdr c) nil)))
  5209. data)))))
  5210. ;;; Folding of blocks
  5211. (defconst org-block-regexp
  5212. "^[ \t]*#\\+begin_\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_\\1[ \t]*$"
  5213. "Regular expression for hiding blocks.")
  5214. (defvar org-hide-block-overlays nil
  5215. "Overlays hiding blocks.")
  5216. (make-variable-buffer-local 'org-hide-block-overlays)
  5217. (defun org-block-map (function &optional start end)
  5218. "Call func at the head of all source blocks in the current
  5219. buffer. Optional arguments START and END can be used to limit
  5220. the range."
  5221. (let ((start (or start (point-min)))
  5222. (end (or end (point-max))))
  5223. (save-excursion
  5224. (goto-char start)
  5225. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5226. (save-excursion
  5227. (save-match-data
  5228. (goto-char (match-beginning 0))
  5229. (funcall function)))))))
  5230. (defun org-hide-block-toggle-all ()
  5231. "Toggle the visibility of all blocks in the current buffer."
  5232. (org-block-map #'org-hide-block-toggle))
  5233. (defun org-hide-block-all ()
  5234. "Fold all blocks in the current buffer."
  5235. (interactive)
  5236. (org-show-block-all)
  5237. (org-block-map #'org-hide-block-toggle-maybe))
  5238. (defun org-show-block-all ()
  5239. "Unfold all blocks in the current buffer."
  5240. (mapc 'org-delete-overlay org-hide-block-overlays)
  5241. (setq org-hide-block-overlays nil))
  5242. (defun org-hide-block-toggle-maybe ()
  5243. "Toggle visibility of block at point."
  5244. (interactive)
  5245. (let ((case-fold-search t))
  5246. (if (save-excursion
  5247. (beginning-of-line 1)
  5248. (looking-at org-block-regexp))
  5249. (progn (org-hide-block-toggle)
  5250. t) ;; to signal that we took action
  5251. nil))) ;; to signal that we did not
  5252. (defun org-hide-block-toggle (&optional force)
  5253. "Toggle the visibility of the current block."
  5254. (interactive)
  5255. (save-excursion
  5256. (beginning-of-line)
  5257. (if (re-search-forward org-block-regexp nil t)
  5258. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5259. (end (match-end 0)) ;; end of entire body
  5260. ov)
  5261. (if (memq t (mapcar (lambda (overlay)
  5262. (eq (org-overlay-get overlay 'invisible)
  5263. 'org-hide-block))
  5264. (org-overlays-at start)))
  5265. (if (or (not force) (eq force 'off))
  5266. (mapc (lambda (ov)
  5267. (when (member ov org-hide-block-overlays)
  5268. (setq org-hide-block-overlays
  5269. (delq ov org-hide-block-overlays)))
  5270. (when (eq (org-overlay-get ov 'invisible)
  5271. 'org-hide-block)
  5272. (org-delete-overlay ov)))
  5273. (org-overlays-at start)))
  5274. (setq ov (org-make-overlay start end))
  5275. (org-overlay-put ov 'invisible 'org-hide-block)
  5276. ;; make the block accessible to isearch
  5277. (org-overlay-put
  5278. ov 'isearch-open-invisible
  5279. (lambda (ov)
  5280. (when (member ov org-hide-block-overlays)
  5281. (setq org-hide-block-overlays
  5282. (delq ov org-hide-block-overlays)))
  5283. (when (eq (org-overlay-get ov 'invisible)
  5284. 'org-hide-block)
  5285. (org-delete-overlay ov))))
  5286. (push ov org-hide-block-overlays)))
  5287. (error "Not looking at a source block"))))
  5288. ;; org-tab-after-check-for-cycling-hook
  5289. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5290. ;; Remove overlays when changing major mode
  5291. (add-hook 'org-mode-hook
  5292. (lambda () (org-add-hook 'change-major-mode-hook
  5293. 'org-show-block-all 'append 'local)))
  5294. ;;; Org-goto
  5295. (defvar org-goto-window-configuration nil)
  5296. (defvar org-goto-marker nil)
  5297. (defvar org-goto-map
  5298. (let ((map (make-sparse-keymap)))
  5299. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5300. (while (setq cmd (pop cmds))
  5301. (substitute-key-definition cmd cmd map global-map)))
  5302. (suppress-keymap map)
  5303. (org-defkey map "\C-m" 'org-goto-ret)
  5304. (org-defkey map [(return)] 'org-goto-ret)
  5305. (org-defkey map [(left)] 'org-goto-left)
  5306. (org-defkey map [(right)] 'org-goto-right)
  5307. (org-defkey map [(control ?g)] 'org-goto-quit)
  5308. (org-defkey map "\C-i" 'org-cycle)
  5309. (org-defkey map [(tab)] 'org-cycle)
  5310. (org-defkey map [(down)] 'outline-next-visible-heading)
  5311. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5312. (if org-goto-auto-isearch
  5313. (if (fboundp 'define-key-after)
  5314. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5315. nil)
  5316. (org-defkey map "q" 'org-goto-quit)
  5317. (org-defkey map "n" 'outline-next-visible-heading)
  5318. (org-defkey map "p" 'outline-previous-visible-heading)
  5319. (org-defkey map "f" 'outline-forward-same-level)
  5320. (org-defkey map "b" 'outline-backward-same-level)
  5321. (org-defkey map "u" 'outline-up-heading))
  5322. (org-defkey map "/" 'org-occur)
  5323. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5324. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5325. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5326. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5327. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5328. map))
  5329. (defconst org-goto-help
  5330. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5331. RET=jump to location [Q]uit and return to previous location
  5332. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5333. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5334. ;; FIXME: Docstring does not mention both interfaces
  5335. (defun org-goto (&optional alternative-interface)
  5336. "Look up a different location in the current file, keeping current visibility.
  5337. When you want look-up or go to a different location in a document, the
  5338. fastest way is often to fold the entire buffer and then dive into the tree.
  5339. This method has the disadvantage, that the previous location will be folded,
  5340. which may not be what you want.
  5341. This command works around this by showing a copy of the current buffer
  5342. in an indirect buffer, in overview mode. You can dive into the tree in
  5343. that copy, use org-occur and incremental search to find a location.
  5344. When pressing RET or `Q', the command returns to the original buffer in
  5345. which the visibility is still unchanged. After RET is will also jump to
  5346. the location selected in the indirect buffer and expose the
  5347. the headline hierarchy above."
  5348. (interactive "P")
  5349. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5350. (org-refile-use-outline-path t)
  5351. (org-refile-target-verify-function nil)
  5352. (interface
  5353. (if (not alternative-interface)
  5354. org-goto-interface
  5355. (if (eq org-goto-interface 'outline)
  5356. 'outline-path-completion
  5357. 'outline)))
  5358. (org-goto-start-pos (point))
  5359. (selected-point
  5360. (if (eq interface 'outline)
  5361. (car (org-get-location (current-buffer) org-goto-help))
  5362. (nth 3 (org-refile-get-location "Goto: ")))))
  5363. (if selected-point
  5364. (progn
  5365. (org-mark-ring-push org-goto-start-pos)
  5366. (goto-char selected-point)
  5367. (if (or (org-invisible-p) (org-invisible-p2))
  5368. (org-show-context 'org-goto)))
  5369. (message "Quit"))))
  5370. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5371. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5372. (defvar org-goto-local-auto-isearch-map) ; defined below
  5373. (defun org-get-location (buf help)
  5374. "Let the user select a location in the Org-mode buffer BUF.
  5375. This function uses a recursive edit. It returns the selected position
  5376. or nil."
  5377. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5378. (isearch-hide-immediately nil)
  5379. (isearch-search-fun-function
  5380. (lambda () 'org-goto-local-search-headings))
  5381. (org-goto-selected-point org-goto-exit-command)
  5382. (pop-up-frames nil)
  5383. (special-display-buffer-names nil)
  5384. (special-display-regexps nil)
  5385. (special-display-function nil))
  5386. (save-excursion
  5387. (save-window-excursion
  5388. (delete-other-windows)
  5389. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5390. (switch-to-buffer
  5391. (condition-case nil
  5392. (make-indirect-buffer (current-buffer) "*org-goto*")
  5393. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5394. (with-output-to-temp-buffer "*Help*"
  5395. (princ help))
  5396. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5397. (setq buffer-read-only nil)
  5398. (let ((org-startup-truncated t)
  5399. (org-startup-folded nil)
  5400. (org-startup-align-all-tables nil))
  5401. (org-mode)
  5402. (org-overview))
  5403. (setq buffer-read-only t)
  5404. (if (and (boundp 'org-goto-start-pos)
  5405. (integer-or-marker-p org-goto-start-pos))
  5406. (let ((org-show-hierarchy-above t)
  5407. (org-show-siblings t)
  5408. (org-show-following-heading t))
  5409. (goto-char org-goto-start-pos)
  5410. (and (org-invisible-p) (org-show-context)))
  5411. (goto-char (point-min)))
  5412. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5413. (message "Select location and press RET")
  5414. (use-local-map org-goto-map)
  5415. (recursive-edit)
  5416. ))
  5417. (kill-buffer "*org-goto*")
  5418. (cons org-goto-selected-point org-goto-exit-command)))
  5419. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5420. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5421. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5422. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5423. (defun org-goto-local-search-headings (string bound noerror)
  5424. "Search and make sure that any matches are in headlines."
  5425. (catch 'return
  5426. (while (if isearch-forward
  5427. (search-forward string bound noerror)
  5428. (search-backward string bound noerror))
  5429. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5430. (and (member :headline context)
  5431. (not (member :tags context))))
  5432. (throw 'return (point))))))
  5433. (defun org-goto-local-auto-isearch ()
  5434. "Start isearch."
  5435. (interactive)
  5436. (goto-char (point-min))
  5437. (let ((keys (this-command-keys)))
  5438. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5439. (isearch-mode t)
  5440. (isearch-process-search-char (string-to-char keys)))))
  5441. (defun org-goto-ret (&optional arg)
  5442. "Finish `org-goto' by going to the new location."
  5443. (interactive "P")
  5444. (setq org-goto-selected-point (point)
  5445. org-goto-exit-command 'return)
  5446. (throw 'exit nil))
  5447. (defun org-goto-left ()
  5448. "Finish `org-goto' by going to the new location."
  5449. (interactive)
  5450. (if (org-on-heading-p)
  5451. (progn
  5452. (beginning-of-line 1)
  5453. (setq org-goto-selected-point (point)
  5454. org-goto-exit-command 'left)
  5455. (throw 'exit nil))
  5456. (error "Not on a heading")))
  5457. (defun org-goto-right ()
  5458. "Finish `org-goto' by going to the new location."
  5459. (interactive)
  5460. (if (org-on-heading-p)
  5461. (progn
  5462. (setq org-goto-selected-point (point)
  5463. org-goto-exit-command 'right)
  5464. (throw 'exit nil))
  5465. (error "Not on a heading")))
  5466. (defun org-goto-quit ()
  5467. "Finish `org-goto' without cursor motion."
  5468. (interactive)
  5469. (setq org-goto-selected-point nil)
  5470. (setq org-goto-exit-command 'quit)
  5471. (throw 'exit nil))
  5472. ;;; Indirect buffer display of subtrees
  5473. (defvar org-indirect-dedicated-frame nil
  5474. "This is the frame being used for indirect tree display.")
  5475. (defvar org-last-indirect-buffer nil)
  5476. (defun org-tree-to-indirect-buffer (&optional arg)
  5477. "Create indirect buffer and narrow it to current subtree.
  5478. With numerical prefix ARG, go up to this level and then take that tree.
  5479. If ARG is negative, go up that many levels.
  5480. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  5481. indirect buffer previously made with this command, to avoid proliferation of
  5482. indirect buffers. However, when you call the command with a `C-u' prefix, or
  5483. when `org-indirect-buffer-display' is `new-frame', the last buffer
  5484. is kept so that you can work with several indirect buffers at the same time.
  5485. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  5486. requests that a new frame be made for the new buffer, so that the dedicated
  5487. frame is not changed."
  5488. (interactive "P")
  5489. (let ((cbuf (current-buffer))
  5490. (cwin (selected-window))
  5491. (pos (point))
  5492. beg end level heading ibuf)
  5493. (save-excursion
  5494. (org-back-to-heading t)
  5495. (when (numberp arg)
  5496. (setq level (org-outline-level))
  5497. (if (< arg 0) (setq arg (+ level arg)))
  5498. (while (> (setq level (org-outline-level)) arg)
  5499. (outline-up-heading 1 t)))
  5500. (setq beg (point)
  5501. heading (org-get-heading))
  5502. (org-end-of-subtree t t)
  5503. (if (org-on-heading-p) (backward-char 1))
  5504. (setq end (point)))
  5505. (if (and (buffer-live-p org-last-indirect-buffer)
  5506. (not (eq org-indirect-buffer-display 'new-frame))
  5507. (not arg))
  5508. (kill-buffer org-last-indirect-buffer))
  5509. (setq ibuf (org-get-indirect-buffer cbuf)
  5510. org-last-indirect-buffer ibuf)
  5511. (cond
  5512. ((or (eq org-indirect-buffer-display 'new-frame)
  5513. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  5514. (select-frame (make-frame))
  5515. (delete-other-windows)
  5516. (switch-to-buffer ibuf)
  5517. (org-set-frame-title heading))
  5518. ((eq org-indirect-buffer-display 'dedicated-frame)
  5519. (raise-frame
  5520. (select-frame (or (and org-indirect-dedicated-frame
  5521. (frame-live-p org-indirect-dedicated-frame)
  5522. org-indirect-dedicated-frame)
  5523. (setq org-indirect-dedicated-frame (make-frame)))))
  5524. (delete-other-windows)
  5525. (switch-to-buffer ibuf)
  5526. (org-set-frame-title (concat "Indirect: " heading)))
  5527. ((eq org-indirect-buffer-display 'current-window)
  5528. (switch-to-buffer ibuf))
  5529. ((eq org-indirect-buffer-display 'other-window)
  5530. (pop-to-buffer ibuf))
  5531. (t (error "Invalid value")))
  5532. (if (featurep 'xemacs)
  5533. (save-excursion (org-mode) (turn-on-font-lock)))
  5534. (narrow-to-region beg end)
  5535. (show-all)
  5536. (goto-char pos)
  5537. (and (window-live-p cwin) (select-window cwin))))
  5538. (defun org-get-indirect-buffer (&optional buffer)
  5539. (setq buffer (or buffer (current-buffer)))
  5540. (let ((n 1) (base (buffer-name buffer)) bname)
  5541. (while (buffer-live-p
  5542. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  5543. (setq n (1+ n)))
  5544. (condition-case nil
  5545. (make-indirect-buffer buffer bname 'clone)
  5546. (error (make-indirect-buffer buffer bname)))))
  5547. (defun org-set-frame-title (title)
  5548. "Set the title of the current frame to the string TITLE."
  5549. ;; FIXME: how to name a single frame in XEmacs???
  5550. (unless (featurep 'xemacs)
  5551. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  5552. ;;;; Structure editing
  5553. ;;; Inserting headlines
  5554. (defun org-previous-line-empty-p ()
  5555. (save-excursion
  5556. (and (not (bobp))
  5557. (or (beginning-of-line 0) t)
  5558. (save-match-data
  5559. (looking-at "[ \t]*$")))))
  5560. (defun org-insert-heading (&optional force-heading invisible-ok)
  5561. "Insert a new heading or item with same depth at point.
  5562. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  5563. If point is at the beginning of a headline, insert a sibling before the
  5564. current headline. If point is not at the beginning, do not split the line,
  5565. but create the new headline after the current line.
  5566. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  5567. This is important for non-interactive uses of the command."
  5568. (interactive "P")
  5569. (if (or (= (buffer-size) 0)
  5570. (and (not (save-excursion (and (ignore-errors (org-back-to-heading invisible-ok))
  5571. (org-on-heading-p))))
  5572. (not (org-in-item-p))))
  5573. (insert "\n* ")
  5574. (when (or force-heading (not (org-insert-item)))
  5575. (let* ((empty-line-p nil)
  5576. (head (save-excursion
  5577. (condition-case nil
  5578. (progn
  5579. (org-back-to-heading invisible-ok)
  5580. (setq empty-line-p (org-previous-line-empty-p))
  5581. (match-string 0))
  5582. (error "*"))))
  5583. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  5584. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  5585. pos hide-previous previous-pos)
  5586. (cond
  5587. ((and (org-on-heading-p) (bolp)
  5588. (or (bobp)
  5589. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  5590. ;; insert before the current line
  5591. (open-line (if blank 2 1)))
  5592. ((and (bolp)
  5593. (not org-insert-heading-respect-content)
  5594. (or (bobp)
  5595. (save-excursion
  5596. (backward-char 1) (not (org-invisible-p)))))
  5597. ;; insert right here
  5598. nil)
  5599. (t
  5600. ;; somewhere in the line
  5601. (save-excursion
  5602. (setq previous-pos (point-at-bol))
  5603. (end-of-line)
  5604. (setq hide-previous (org-invisible-p)))
  5605. (and org-insert-heading-respect-content (org-show-subtree))
  5606. (let ((split
  5607. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  5608. (save-excursion
  5609. (let ((p (point)))
  5610. (goto-char (point-at-bol))
  5611. (and (looking-at org-complex-heading-regexp)
  5612. (> p (match-beginning 4)))))))
  5613. tags pos)
  5614. (cond
  5615. (org-insert-heading-respect-content
  5616. (org-end-of-subtree nil t)
  5617. (or (bolp) (newline))
  5618. (or (org-previous-line-empty-p)
  5619. (and blank (newline)))
  5620. (open-line 1))
  5621. ((org-on-heading-p)
  5622. (when hide-previous
  5623. (show-children)
  5624. (org-show-entry))
  5625. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  5626. (setq tags (and (match-end 2) (match-string 2)))
  5627. (and (match-end 1)
  5628. (delete-region (match-beginning 1) (match-end 1)))
  5629. (setq pos (point-at-bol))
  5630. (or split (end-of-line 1))
  5631. (delete-horizontal-space)
  5632. (if (string-match "\\`\\*+\\'"
  5633. (buffer-substring (point-at-bol) (point)))
  5634. (insert " "))
  5635. (newline (if blank 2 1))
  5636. (when tags
  5637. (save-excursion
  5638. (goto-char pos)
  5639. (end-of-line 1)
  5640. (insert " " tags)
  5641. (org-set-tags nil 'align))))
  5642. (t
  5643. (or split (end-of-line 1))
  5644. (newline (if blank 2 1)))))))
  5645. (insert head) (just-one-space)
  5646. (setq pos (point))
  5647. (end-of-line 1)
  5648. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  5649. (when (and org-insert-heading-respect-content hide-previous)
  5650. (save-excursion
  5651. (goto-char previous-pos)
  5652. (hide-subtree)))
  5653. (run-hooks 'org-insert-heading-hook)))))
  5654. (defun org-get-heading (&optional no-tags)
  5655. "Return the heading of the current entry, without the stars."
  5656. (save-excursion
  5657. (org-back-to-heading t)
  5658. (if (looking-at
  5659. (if no-tags
  5660. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  5661. "\\*+[ \t]+\\([^\r\n]*\\)"))
  5662. (match-string 1) "")))
  5663. (defun org-heading-components ()
  5664. "Return the components of the current heading.
  5665. This is a list with the following elements:
  5666. - the level as an integer
  5667. - the reduced level, different if `org-odd-levels-only' is set.
  5668. - the TODO keyword, or nil
  5669. - the priority character, like ?A, or nil if no priority is given
  5670. - the headline text itself, or the tags string if no headline text
  5671. - the tags string, or nil."
  5672. (save-excursion
  5673. (org-back-to-heading t)
  5674. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  5675. (list (length (match-string 1))
  5676. (org-reduced-level (length (match-string 1)))
  5677. (org-match-string-no-properties 2)
  5678. (and (match-end 3) (aref (match-string 3) 2))
  5679. (org-match-string-no-properties 4)
  5680. (org-match-string-no-properties 5)))))
  5681. (defun org-get-entry ()
  5682. "Get the entry text, after heading, entire subtree."
  5683. (save-excursion
  5684. (org-back-to-heading t)
  5685. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  5686. (defun org-insert-heading-after-current ()
  5687. "Insert a new heading with same level as current, after current subtree."
  5688. (interactive)
  5689. (org-back-to-heading)
  5690. (org-insert-heading)
  5691. (org-move-subtree-down)
  5692. (end-of-line 1))
  5693. (defun org-insert-heading-respect-content ()
  5694. (interactive)
  5695. (let ((org-insert-heading-respect-content t))
  5696. (org-insert-heading t)))
  5697. (defun org-insert-todo-heading-respect-content (&optional force-state)
  5698. (interactive "P")
  5699. (let ((org-insert-heading-respect-content t))
  5700. (org-insert-todo-heading force-state t)))
  5701. (defun org-insert-todo-heading (arg &optional force-heading)
  5702. "Insert a new heading with the same level and TODO state as current heading.
  5703. If the heading has no TODO state, or if the state is DONE, use the first
  5704. state (TODO by default). Also with prefix arg, force first state."
  5705. (interactive "P")
  5706. (when (or force-heading (not (org-insert-item 'checkbox)))
  5707. (org-insert-heading force-heading)
  5708. (save-excursion
  5709. (org-back-to-heading)
  5710. (outline-previous-heading)
  5711. (looking-at org-todo-line-regexp))
  5712. (let*
  5713. ((new-mark-x
  5714. (if (or arg
  5715. (not (match-beginning 2))
  5716. (member (match-string 2) org-done-keywords))
  5717. (car org-todo-keywords-1)
  5718. (match-string 2)))
  5719. (new-mark
  5720. (or
  5721. (run-hook-with-args-until-success
  5722. 'org-todo-get-default-hook new-mark-x nil)
  5723. new-mark-x)))
  5724. (beginning-of-line 1)
  5725. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  5726. (if org-treat-insert-todo-heading-as-state-change
  5727. (org-todo new-mark)
  5728. (insert new-mark " "))))
  5729. (when org-provide-todo-statistics
  5730. (org-update-parent-todo-statistics))))
  5731. (defun org-insert-subheading (arg)
  5732. "Insert a new subheading and demote it.
  5733. Works for outline headings and for plain lists alike."
  5734. (interactive "P")
  5735. (org-insert-heading arg)
  5736. (cond
  5737. ((org-on-heading-p) (org-do-demote))
  5738. ((org-at-item-p) (org-indent-item 1))))
  5739. (defun org-insert-todo-subheading (arg)
  5740. "Insert a new subheading with TODO keyword or checkbox and demote it.
  5741. Works for outline headings and for plain lists alike."
  5742. (interactive "P")
  5743. (org-insert-todo-heading arg)
  5744. (cond
  5745. ((org-on-heading-p) (org-do-demote))
  5746. ((org-at-item-p) (org-indent-item 1))))
  5747. ;;; Promotion and Demotion
  5748. (defvar org-after-demote-entry-hook nil
  5749. "Hook run after an entry has been demoted.
  5750. The cursor will be at the beginning of the entry.
  5751. When a subtree is being demoted, the hook will be called for each node.")
  5752. (defvar org-after-promote-entry-hook nil
  5753. "Hook run after an entry has been promoted.
  5754. The cursor will be at the beginning of the entry.
  5755. When a subtree is being promoted, the hook will be called for each node.")
  5756. (defun org-promote-subtree ()
  5757. "Promote the entire subtree.
  5758. See also `org-promote'."
  5759. (interactive)
  5760. (save-excursion
  5761. (org-map-tree 'org-promote))
  5762. (org-fix-position-after-promote))
  5763. (defun org-demote-subtree ()
  5764. "Demote the entire subtree. See `org-demote'.
  5765. See also `org-promote'."
  5766. (interactive)
  5767. (save-excursion
  5768. (org-map-tree 'org-demote))
  5769. (org-fix-position-after-promote))
  5770. (defun org-do-promote ()
  5771. "Promote the current heading higher up the tree.
  5772. If the region is active in `transient-mark-mode', promote all headings
  5773. in the region."
  5774. (interactive)
  5775. (save-excursion
  5776. (if (org-region-active-p)
  5777. (org-map-region 'org-promote (region-beginning) (region-end))
  5778. (org-promote)))
  5779. (org-fix-position-after-promote))
  5780. (defun org-do-demote ()
  5781. "Demote the current heading lower down the tree.
  5782. If the region is active in `transient-mark-mode', demote all headings
  5783. in the region."
  5784. (interactive)
  5785. (save-excursion
  5786. (if (org-region-active-p)
  5787. (org-map-region 'org-demote (region-beginning) (region-end))
  5788. (org-demote)))
  5789. (org-fix-position-after-promote))
  5790. (defun org-fix-position-after-promote ()
  5791. "Make sure that after pro/demotion cursor position is right."
  5792. (let ((pos (point)))
  5793. (when (save-excursion
  5794. (beginning-of-line 1)
  5795. (looking-at org-todo-line-regexp)
  5796. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  5797. (cond ((eobp) (insert " "))
  5798. ((eolp) (insert " "))
  5799. ((equal (char-after) ?\ ) (forward-char 1))))))
  5800. (defun org-current-level ()
  5801. "Return the level of the current entry, or nil if before the first headline.
  5802. The level is the number of stars at the beginning of the headline."
  5803. (save-excursion
  5804. (condition-case nil
  5805. (progn
  5806. (org-back-to-heading t)
  5807. (funcall outline-level))
  5808. (error nil))))
  5809. (defun org-get-previous-line-level ()
  5810. "Return the outline depth of the last headline before the current line.
  5811. Returns 0 for the first headline in the buffer, and nil if before the
  5812. first headline."
  5813. (let ((current-level (org-current-level))
  5814. (prev-level (when (> (line-number-at-pos) 1)
  5815. (save-excursion
  5816. (beginning-of-line 0)
  5817. (org-current-level)))))
  5818. (cond ((null current-level) nil) ; Before first headline
  5819. ((null prev-level) 0) ; At first headline
  5820. (prev-level))))
  5821. (defun org-reduced-level (l)
  5822. "Compute the effective level of a heading.
  5823. This takes into account the setting of `org-odd-levels-only'."
  5824. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  5825. (defun org-level-increment ()
  5826. "Return the number of stars that will be added or removed at a
  5827. time to headlines when structure editing, based on the value of
  5828. `org-odd-levels-only'."
  5829. (if org-odd-levels-only 2 1))
  5830. (defun org-get-valid-level (level &optional change)
  5831. "Rectify a level change under the influence of `org-odd-levels-only'
  5832. LEVEL is a current level, CHANGE is by how much the level should be
  5833. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  5834. even level numbers will become the next higher odd number."
  5835. (if org-odd-levels-only
  5836. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  5837. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  5838. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  5839. (max 1 (+ level (or change 0)))))
  5840. (if (boundp 'define-obsolete-function-alias)
  5841. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  5842. (define-obsolete-function-alias 'org-get-legal-level
  5843. 'org-get-valid-level)
  5844. (define-obsolete-function-alias 'org-get-legal-level
  5845. 'org-get-valid-level "23.1")))
  5846. (defun org-promote ()
  5847. "Promote the current heading higher up the tree.
  5848. If the region is active in `transient-mark-mode', promote all headings
  5849. in the region."
  5850. (org-back-to-heading t)
  5851. (let* ((level (save-match-data (funcall outline-level)))
  5852. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  5853. (diff (abs (- level (length up-head) -1))))
  5854. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  5855. (replace-match up-head nil t)
  5856. ;; Fixup tag positioning
  5857. (and org-auto-align-tags (org-set-tags nil t))
  5858. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  5859. (run-hooks 'org-after-promote-entry-hook)))
  5860. (defun org-demote ()
  5861. "Demote the current heading lower down the tree.
  5862. If the region is active in `transient-mark-mode', demote all headings
  5863. in the region."
  5864. (org-back-to-heading t)
  5865. (let* ((level (save-match-data (funcall outline-level)))
  5866. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  5867. (diff (abs (- level (length down-head) -1))))
  5868. (replace-match down-head nil t)
  5869. ;; Fixup tag positioning
  5870. (and org-auto-align-tags (org-set-tags nil t))
  5871. (if org-adapt-indentation (org-fixup-indentation diff))
  5872. (run-hooks 'org-after-demote-entry-hook)))
  5873. (defun org-cycle-level ()
  5874. "Cycle the level of an empty headline through possible states.
  5875. This goes first to child, then to parent, level, then up the hierarchy.
  5876. After top level, it switches back to sibling level."
  5877. (interactive)
  5878. (let ((org-adapt-indentation nil))
  5879. (when (org-point-at-end-of-empty-headline)
  5880. (setq this-command 'org-cycle-level) ; Only needed for caching
  5881. (let ((cur-level (org-current-level))
  5882. (prev-level (org-get-previous-line-level)))
  5883. (cond
  5884. ;; If first headline in file, promote to top-level.
  5885. ((= prev-level 0)
  5886. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5887. do (org-do-promote)))
  5888. ;; If same level as prev, demote one.
  5889. ((= prev-level cur-level)
  5890. (org-do-demote))
  5891. ;; If parent is top-level, promote to top level if not already.
  5892. ((= prev-level 1)
  5893. (loop repeat (/ (- cur-level 1) (org-level-increment))
  5894. do (org-do-promote)))
  5895. ;; If top-level, return to prev-level.
  5896. ((= cur-level 1)
  5897. (loop repeat (/ (- prev-level 1) (org-level-increment))
  5898. do (org-do-demote)))
  5899. ;; If less than prev-level, promote one.
  5900. ((< cur-level prev-level)
  5901. (org-do-promote))
  5902. ;; If deeper than prev-level, promote until higher than
  5903. ;; prev-level.
  5904. ((> cur-level prev-level)
  5905. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  5906. do (org-do-promote))))
  5907. t))))
  5908. (defun org-map-tree (fun)
  5909. "Call FUN for every heading underneath the current one."
  5910. (org-back-to-heading)
  5911. (let ((level (funcall outline-level)))
  5912. (save-excursion
  5913. (funcall fun)
  5914. (while (and (progn
  5915. (outline-next-heading)
  5916. (> (funcall outline-level) level))
  5917. (not (eobp)))
  5918. (funcall fun)))))
  5919. (defun org-map-region (fun beg end)
  5920. "Call FUN for every heading between BEG and END."
  5921. (let ((org-ignore-region t))
  5922. (save-excursion
  5923. (setq end (copy-marker end))
  5924. (goto-char beg)
  5925. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  5926. (< (point) end))
  5927. (funcall fun))
  5928. (while (and (progn
  5929. (outline-next-heading)
  5930. (< (point) end))
  5931. (not (eobp)))
  5932. (funcall fun)))))
  5933. (defun org-fixup-indentation (diff)
  5934. "Change the indentation in the current entry by DIFF
  5935. However, if any line in the current entry has no indentation, or if it
  5936. would end up with no indentation after the change, nothing at all is done."
  5937. (save-excursion
  5938. (let ((end (save-excursion (outline-next-heading)
  5939. (point-marker)))
  5940. (prohibit (if (> diff 0)
  5941. "^\\S-"
  5942. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  5943. col)
  5944. (unless (save-excursion (end-of-line 1)
  5945. (re-search-forward prohibit end t))
  5946. (while (and (< (point) end)
  5947. (re-search-forward "^[ \t]+" end t))
  5948. (goto-char (match-end 0))
  5949. (setq col (current-column))
  5950. (if (< diff 0) (replace-match ""))
  5951. (org-indent-to-column (+ diff col))))
  5952. (move-marker end nil))))
  5953. (defun org-convert-to-odd-levels ()
  5954. "Convert an org-mode file with all levels allowed to one with odd levels.
  5955. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  5956. level 5 etc."
  5957. (interactive)
  5958. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  5959. (let ((outline-regexp org-outline-regexp)
  5960. (outline-level 'org-outline-level)
  5961. (org-odd-levels-only nil) n)
  5962. (save-excursion
  5963. (goto-char (point-min))
  5964. (while (re-search-forward "^\\*\\*+ " nil t)
  5965. (setq n (- (length (match-string 0)) 2))
  5966. (while (>= (setq n (1- n)) 0)
  5967. (org-demote))
  5968. (end-of-line 1))))))
  5969. (defun org-convert-to-oddeven-levels ()
  5970. "Convert an org-mode file with only odd levels to one with odd and even levels.
  5971. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  5972. section with an even level, conversion would destroy the structure of the file. An error
  5973. is signaled in this case."
  5974. (interactive)
  5975. (goto-char (point-min))
  5976. ;; First check if there are no even levels
  5977. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  5978. (org-show-context t)
  5979. (error "Not all levels are odd in this file. Conversion not possible"))
  5980. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  5981. (let ((outline-regexp org-outline-regexp)
  5982. (outline-level 'org-outline-level)
  5983. (org-odd-levels-only nil) n)
  5984. (save-excursion
  5985. (goto-char (point-min))
  5986. (while (re-search-forward "^\\*\\*+ " nil t)
  5987. (setq n (/ (1- (length (match-string 0))) 2))
  5988. (while (>= (setq n (1- n)) 0)
  5989. (org-promote))
  5990. (end-of-line 1))))))
  5991. (defun org-tr-level (n)
  5992. "Make N odd if required."
  5993. (if org-odd-levels-only (1+ (/ n 2)) n))
  5994. ;;; Vertical tree motion, cutting and pasting of subtrees
  5995. (defun org-move-subtree-up (&optional arg)
  5996. "Move the current subtree up past ARG headlines of the same level."
  5997. (interactive "p")
  5998. (org-move-subtree-down (- (prefix-numeric-value arg))))
  5999. (defun org-move-subtree-down (&optional arg)
  6000. "Move the current subtree down past ARG headlines of the same level."
  6001. (interactive "p")
  6002. (setq arg (prefix-numeric-value arg))
  6003. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6004. 'org-get-last-sibling))
  6005. (ins-point (make-marker))
  6006. (cnt (abs arg))
  6007. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6008. ;; Select the tree
  6009. (org-back-to-heading)
  6010. (setq beg0 (point))
  6011. (save-excursion
  6012. (setq ne-beg (org-back-over-empty-lines))
  6013. (setq beg (point)))
  6014. (save-match-data
  6015. (save-excursion (outline-end-of-heading)
  6016. (setq folded (org-invisible-p)))
  6017. (outline-end-of-subtree))
  6018. (outline-next-heading)
  6019. (setq ne-end (org-back-over-empty-lines))
  6020. (setq end (point))
  6021. (goto-char beg0)
  6022. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6023. ;; include less whitespace
  6024. (save-excursion
  6025. (goto-char beg)
  6026. (forward-line (- ne-beg ne-end))
  6027. (setq beg (point))))
  6028. ;; Find insertion point, with error handling
  6029. (while (> cnt 0)
  6030. (or (and (funcall movfunc) (looking-at outline-regexp))
  6031. (progn (goto-char beg0)
  6032. (error "Cannot move past superior level or buffer limit")))
  6033. (setq cnt (1- cnt)))
  6034. (if (> arg 0)
  6035. ;; Moving forward - still need to move over subtree
  6036. (progn (org-end-of-subtree t t)
  6037. (save-excursion
  6038. (org-back-over-empty-lines)
  6039. (or (bolp) (newline)))))
  6040. (setq ne-ins (org-back-over-empty-lines))
  6041. (move-marker ins-point (point))
  6042. (setq txt (buffer-substring beg end))
  6043. (org-save-markers-in-region beg end)
  6044. (delete-region beg end)
  6045. (org-remove-empty-overlays-at beg)
  6046. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6047. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6048. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6049. (let ((bbb (point)))
  6050. (insert-before-markers txt)
  6051. (org-reinstall-markers-in-region bbb)
  6052. (move-marker ins-point bbb))
  6053. (or (bolp) (insert "\n"))
  6054. (setq ins-end (point))
  6055. (goto-char ins-point)
  6056. (org-skip-whitespace)
  6057. (when (and (< arg 0)
  6058. (org-first-sibling-p)
  6059. (> ne-ins ne-beg))
  6060. ;; Move whitespace back to beginning
  6061. (save-excursion
  6062. (goto-char ins-end)
  6063. (let ((kill-whole-line t))
  6064. (kill-line (- ne-ins ne-beg)) (point)))
  6065. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6066. (move-marker ins-point nil)
  6067. (if folded
  6068. (hide-subtree)
  6069. (org-show-entry)
  6070. (show-children)
  6071. (org-cycle-hide-drawers 'children))
  6072. (org-clean-visibility-after-subtree-move)))
  6073. (defvar org-subtree-clip ""
  6074. "Clipboard for cut and paste of subtrees.
  6075. This is actually only a copy of the kill, because we use the normal kill
  6076. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6077. (defvar org-subtree-clip-folded nil
  6078. "Was the last copied subtree folded?
  6079. This is used to fold the tree back after pasting.")
  6080. (defun org-cut-subtree (&optional n)
  6081. "Cut the current subtree into the clipboard.
  6082. With prefix arg N, cut this many sequential subtrees.
  6083. This is a short-hand for marking the subtree and then cutting it."
  6084. (interactive "p")
  6085. (org-copy-subtree n 'cut))
  6086. (defun org-copy-subtree (&optional n cut force-store-markers)
  6087. "Cut the current subtree into the clipboard.
  6088. With prefix arg N, cut this many sequential subtrees.
  6089. This is a short-hand for marking the subtree and then copying it.
  6090. If CUT is non-nil, actually cut the subtree.
  6091. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6092. of some markers in the region, even if CUT is non-nil. This is
  6093. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6094. (interactive "p")
  6095. (let (beg end folded (beg0 (point)))
  6096. (if (interactive-p)
  6097. (org-back-to-heading nil) ; take what looks like a subtree
  6098. (org-back-to-heading t)) ; take what is really there
  6099. (org-back-over-empty-lines)
  6100. (setq beg (point))
  6101. (skip-chars-forward " \t\r\n")
  6102. (save-match-data
  6103. (save-excursion (outline-end-of-heading)
  6104. (setq folded (org-invisible-p)))
  6105. (condition-case nil
  6106. (org-forward-same-level (1- n) t)
  6107. (error nil))
  6108. (org-end-of-subtree t t))
  6109. (org-back-over-empty-lines)
  6110. (setq end (point))
  6111. (goto-char beg0)
  6112. (when (> end beg)
  6113. (setq org-subtree-clip-folded folded)
  6114. (when (or cut force-store-markers)
  6115. (org-save-markers-in-region beg end))
  6116. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6117. (setq org-subtree-clip (current-kill 0))
  6118. (message "%s: Subtree(s) with %d characters"
  6119. (if cut "Cut" "Copied")
  6120. (length org-subtree-clip)))))
  6121. (defun org-paste-subtree (&optional level tree for-yank)
  6122. "Paste the clipboard as a subtree, with modification of headline level.
  6123. The entire subtree is promoted or demoted in order to match a new headline
  6124. level.
  6125. If the cursor is at the beginning of a headline, the same level as
  6126. that headline is used to paste the tree
  6127. If not, the new level is derived from the *visible* headings
  6128. before and after the insertion point, and taken to be the inferior headline
  6129. level of the two. So if the previous visible heading is level 3 and the
  6130. next is level 4 (or vice versa), level 4 will be used for insertion.
  6131. This makes sure that the subtree remains an independent subtree and does
  6132. not swallow low level entries.
  6133. You can also force a different level, either by using a numeric prefix
  6134. argument, or by inserting the heading marker by hand. For example, if the
  6135. cursor is after \"*****\", then the tree will be shifted to level 5.
  6136. If optional TREE is given, use this text instead of the kill ring.
  6137. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6138. move back over whitespace before inserting, and move point to the end of
  6139. the inserted text when done."
  6140. (interactive "P")
  6141. (setq tree (or tree (and kill-ring (current-kill 0))))
  6142. (unless (org-kill-is-subtree-p tree)
  6143. (error "%s"
  6144. (substitute-command-keys
  6145. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6146. (let* ((visp (not (org-invisible-p)))
  6147. (txt tree)
  6148. (^re (concat "^\\(" outline-regexp "\\)"))
  6149. (re (concat "\\(" outline-regexp "\\)"))
  6150. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6151. (old-level (if (string-match ^re txt)
  6152. (- (match-end 0) (match-beginning 0) 1)
  6153. -1))
  6154. (force-level (cond (level (prefix-numeric-value level))
  6155. ((and (looking-at "[ \t]*$")
  6156. (string-match
  6157. ^re_ (buffer-substring
  6158. (point-at-bol) (point))))
  6159. (- (match-end 1) (match-beginning 1)))
  6160. ((and (bolp)
  6161. (looking-at org-outline-regexp))
  6162. (- (match-end 0) (point) 1))
  6163. (t nil)))
  6164. (previous-level (save-excursion
  6165. (condition-case nil
  6166. (progn
  6167. (outline-previous-visible-heading 1)
  6168. (if (looking-at re)
  6169. (- (match-end 0) (match-beginning 0) 1)
  6170. 1))
  6171. (error 1))))
  6172. (next-level (save-excursion
  6173. (condition-case nil
  6174. (progn
  6175. (or (looking-at outline-regexp)
  6176. (outline-next-visible-heading 1))
  6177. (if (looking-at re)
  6178. (- (match-end 0) (match-beginning 0) 1)
  6179. 1))
  6180. (error 1))))
  6181. (new-level (or force-level (max previous-level next-level)))
  6182. (shift (if (or (= old-level -1)
  6183. (= new-level -1)
  6184. (= old-level new-level))
  6185. 0
  6186. (- new-level old-level)))
  6187. (delta (if (> shift 0) -1 1))
  6188. (func (if (> shift 0) 'org-demote 'org-promote))
  6189. (org-odd-levels-only nil)
  6190. beg end newend)
  6191. ;; Remove the forced level indicator
  6192. (if force-level
  6193. (delete-region (point-at-bol) (point)))
  6194. ;; Paste
  6195. (beginning-of-line 1)
  6196. (unless for-yank (org-back-over-empty-lines))
  6197. (setq beg (point))
  6198. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6199. (insert-before-markers txt)
  6200. (unless (string-match "\n\\'" txt) (insert "\n"))
  6201. (setq newend (point))
  6202. (org-reinstall-markers-in-region beg)
  6203. (setq end (point))
  6204. (goto-char beg)
  6205. (skip-chars-forward " \t\n\r")
  6206. (setq beg (point))
  6207. (if (and (org-invisible-p) visp)
  6208. (save-excursion (outline-show-heading)))
  6209. ;; Shift if necessary
  6210. (unless (= shift 0)
  6211. (save-restriction
  6212. (narrow-to-region beg end)
  6213. (while (not (= shift 0))
  6214. (org-map-region func (point-min) (point-max))
  6215. (setq shift (+ delta shift)))
  6216. (goto-char (point-min))
  6217. (setq newend (point-max))))
  6218. (when (or (interactive-p) for-yank)
  6219. (message "Clipboard pasted as level %d subtree" new-level))
  6220. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6221. kill-ring
  6222. (eq org-subtree-clip (current-kill 0))
  6223. org-subtree-clip-folded)
  6224. ;; The tree was folded before it was killed/copied
  6225. (hide-subtree))
  6226. (and for-yank (goto-char newend))))
  6227. (defun org-kill-is-subtree-p (&optional txt)
  6228. "Check if the current kill is an outline subtree, or a set of trees.
  6229. Returns nil if kill does not start with a headline, or if the first
  6230. headline level is not the largest headline level in the tree.
  6231. So this will actually accept several entries of equal levels as well,
  6232. which is OK for `org-paste-subtree'.
  6233. If optional TXT is given, check this string instead of the current kill."
  6234. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6235. (start-level (and kill
  6236. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6237. org-outline-regexp "\\)")
  6238. kill)
  6239. (- (match-end 2) (match-beginning 2) 1)))
  6240. (re (concat "^" org-outline-regexp))
  6241. (start (1+ (or (match-beginning 2) -1))))
  6242. (if (not start-level)
  6243. (progn
  6244. nil) ;; does not even start with a heading
  6245. (catch 'exit
  6246. (while (setq start (string-match re kill (1+ start)))
  6247. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6248. (throw 'exit nil)))
  6249. t))))
  6250. (defvar org-markers-to-move nil
  6251. "Markers that should be moved with a cut-and-paste operation.
  6252. Those markers are stored together with their positions relative to
  6253. the start of the region.")
  6254. (defun org-save-markers-in-region (beg end)
  6255. "Check markers in region.
  6256. If these markers are between BEG and END, record their position relative
  6257. to BEG, so that after moving the block of text, we can put the markers back
  6258. into place.
  6259. This function gets called just before an entry or tree gets cut from the
  6260. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6261. called immediately, to move the markers with the entries."
  6262. (setq org-markers-to-move nil)
  6263. (when (featurep 'org-clock)
  6264. (org-clock-save-markers-for-cut-and-paste beg end))
  6265. (when (featurep 'org-agenda)
  6266. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6267. (defun org-check-and-save-marker (marker beg end)
  6268. "Check if MARKER is between BEG and END.
  6269. If yes, remember the marker and the distance to BEG."
  6270. (when (and (marker-buffer marker)
  6271. (equal (marker-buffer marker) (current-buffer)))
  6272. (if (and (>= marker beg) (< marker end))
  6273. (push (cons marker (- marker beg)) org-markers-to-move))))
  6274. (defun org-reinstall-markers-in-region (beg)
  6275. "Move all remembered markers to their position relative to BEG."
  6276. (mapc (lambda (x)
  6277. (move-marker (car x) (+ beg (cdr x))))
  6278. org-markers-to-move)
  6279. (setq org-markers-to-move nil))
  6280. (defun org-narrow-to-subtree ()
  6281. "Narrow buffer to the current subtree."
  6282. (interactive)
  6283. (save-excursion
  6284. (save-match-data
  6285. (narrow-to-region
  6286. (progn (org-back-to-heading t) (point))
  6287. (progn (org-end-of-subtree t t)
  6288. (if (org-on-heading-p) (backward-char 1))
  6289. (point))))))
  6290. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6291. "Clone the task (subtree) at point N times.
  6292. The clones will be inserted as siblings.
  6293. In interactive use, the user will be prompted for the number of clones
  6294. to be produced, and for a time SHIFT, which may be a repeater as used
  6295. in time stamps, for example `+3d'.
  6296. When a valid repeater is given and the entry contains any time stamps,
  6297. the clones will become a sequence in time, with time stamps in the
  6298. subtree shifted for each clone produced. If SHIFT is nil or the
  6299. empty string, time stamps will be left alone.
  6300. If the original subtree did contain time stamps with a repeater,
  6301. the following will happen:
  6302. - the repeater will be removed in each clone
  6303. - an additional clone will be produced, with the current, unshifted
  6304. date(s) in the entry.
  6305. - the original entry will be placed *after* all the clones, with
  6306. repeater intact.
  6307. - the start days in the repeater in the original entry will be shifted
  6308. to past the last clone.
  6309. I this way you can spell out a number of instances of a repeating task,
  6310. and still retain the repeater to cover future instances of the task."
  6311. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6312. (let (beg end template task
  6313. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6314. (if (not (and (integerp n) (> n 0)))
  6315. (error "Invalid number of replications %s" n))
  6316. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6317. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6318. shift)))
  6319. (error "Invalid shift specification %s" shift))
  6320. (when doshift
  6321. (setq shift-n (string-to-number (match-string 1 shift))
  6322. shift-what (cdr (assoc (match-string 2 shift)
  6323. '(("d" . day) ("w" . week)
  6324. ("m" . month) ("y" . year))))))
  6325. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6326. (setq nmin 1 nmax n)
  6327. (org-back-to-heading t)
  6328. (setq beg (point))
  6329. (org-end-of-subtree t t)
  6330. (or (bolp) (insert "\n"))
  6331. (setq end (point))
  6332. (setq template (buffer-substring beg end))
  6333. (when (and doshift
  6334. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6335. (delete-region beg end)
  6336. (setq end beg)
  6337. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6338. (goto-char end)
  6339. (loop for n from nmin to nmax do
  6340. (if (not doshift)
  6341. (setq task template)
  6342. (with-temp-buffer
  6343. (insert template)
  6344. (org-mode)
  6345. (goto-char (point-min))
  6346. (while (re-search-forward org-ts-regexp-both nil t)
  6347. (org-timestamp-change (* n shift-n) shift-what))
  6348. (unless (= n n-no-remove)
  6349. (goto-char (point-min))
  6350. (while (re-search-forward org-ts-regexp nil t)
  6351. (save-excursion
  6352. (goto-char (match-beginning 0))
  6353. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6354. (delete-region (match-beginning 1) (match-end 1))))))
  6355. (setq task (buffer-string))))
  6356. (insert task))
  6357. (goto-char beg)))
  6358. ;;; Outline Sorting
  6359. (defun org-sort (with-case)
  6360. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  6361. Optional argument WITH-CASE means sort case-sensitively.
  6362. With a double prefix argument, also remove duplicate entries."
  6363. (interactive "P")
  6364. (if (org-at-table-p)
  6365. (org-call-with-arg 'org-table-sort-lines with-case)
  6366. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  6367. (defun org-sort-remove-invisible (s)
  6368. (remove-text-properties 0 (length s) org-rm-props s)
  6369. (while (string-match org-bracket-link-regexp s)
  6370. (setq s (replace-match (if (match-end 2)
  6371. (match-string 3 s)
  6372. (match-string 1 s)) t t s)))
  6373. s)
  6374. (defvar org-priority-regexp) ; defined later in the file
  6375. (defvar org-after-sorting-entries-or-items-hook nil
  6376. "Hook that is run after a bunch of entries or items have been sorted.
  6377. When children are sorted, the cursor is in the parent line when this
  6378. hook gets called. When a region or a plain list is sorted, the cursor
  6379. will be in the first entry of the sorted region/list.")
  6380. (defun org-sort-entries-or-items
  6381. (&optional with-case sorting-type getkey-func compare-func property)
  6382. "Sort entries on a certain level of an outline tree, or plain list items.
  6383. If there is an active region, the entries in the region are sorted.
  6384. Else, if the cursor is before the first entry, sort the top-level items.
  6385. Else, the children of the entry at point are sorted.
  6386. If the cursor is at the first item in a plain list, the list items will be
  6387. sorted.
  6388. Sorting can be alphabetically, numerically, by date/time as given by
  6389. a time stamp, by a property or by priority.
  6390. The command prompts for the sorting type unless it has been given to the
  6391. function through the SORTING-TYPE argument, which needs to a character,
  6392. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6393. precise meaning of each character:
  6394. n Numerically, by converting the beginning of the entry/item to a number.
  6395. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6396. t By date/time, either the first active time stamp in the entry, or, if
  6397. none exist, by the first inactive one.
  6398. In items, only the first line will be checked.
  6399. s By the scheduled date/time.
  6400. d By deadline date/time.
  6401. c By creation time, which is assumed to be the first inactive time stamp
  6402. at the beginning of a line.
  6403. p By priority according to the cookie.
  6404. r By the value of a property.
  6405. Capital letters will reverse the sort order.
  6406. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6407. called with point at the beginning of the record. It must return either
  6408. a string or a number that should serve as the sorting key for that record.
  6409. Comparing entries ignores case by default. However, with an optional argument
  6410. WITH-CASE, the sorting considers case as well."
  6411. (interactive "P")
  6412. (let ((case-func (if with-case 'identity 'downcase))
  6413. start beg end stars re re2
  6414. txt what tmp plain-list-p)
  6415. ;; Find beginning and end of region to sort
  6416. (cond
  6417. ((org-region-active-p)
  6418. ;; we will sort the region
  6419. (setq end (region-end)
  6420. what "region")
  6421. (goto-char (region-beginning))
  6422. (if (not (org-on-heading-p)) (outline-next-heading))
  6423. (setq start (point)))
  6424. ((org-at-item-p)
  6425. ;; we will sort this plain list
  6426. (org-beginning-of-item-list) (setq start (point))
  6427. (org-end-of-item-list)
  6428. (or (bolp) (insert "\n"))
  6429. (setq end (point))
  6430. (goto-char start)
  6431. (setq plain-list-p t
  6432. what "plain list"))
  6433. ((or (org-on-heading-p)
  6434. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  6435. ;; we will sort the children of the current headline
  6436. (org-back-to-heading)
  6437. (setq start (point)
  6438. end (progn (org-end-of-subtree t t)
  6439. (or (bolp) (insert "\n"))
  6440. (org-back-over-empty-lines)
  6441. (point))
  6442. what "children")
  6443. (goto-char start)
  6444. (show-subtree)
  6445. (outline-next-heading))
  6446. (t
  6447. ;; we will sort the top-level entries in this file
  6448. (goto-char (point-min))
  6449. (or (org-on-heading-p) (outline-next-heading))
  6450. (setq start (point))
  6451. (goto-char (point-max))
  6452. (beginning-of-line 1)
  6453. (when (looking-at ".*?\\S-")
  6454. ;; File ends in a non-white line
  6455. (end-of-line 1)
  6456. (insert "\n"))
  6457. (setq end (point-max))
  6458. (setq what "top-level")
  6459. (goto-char start)
  6460. (show-all)))
  6461. (setq beg (point))
  6462. (if (>= beg end) (error "Nothing to sort"))
  6463. (unless plain-list-p
  6464. (looking-at "\\(\\*+\\)")
  6465. (setq stars (match-string 1)
  6466. re (concat "^" (regexp-quote stars) " +")
  6467. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  6468. txt (buffer-substring beg end))
  6469. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  6470. (if (and (not (equal stars "*")) (string-match re2 txt))
  6471. (error "Region to sort contains a level above the first entry")))
  6472. (unless sorting-type
  6473. (message
  6474. (if plain-list-p
  6475. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  6476. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  6477. [t]ime [s]cheduled [d]eadline [c]reated
  6478. A/N/T/S/D/C/P/O/F means reversed:")
  6479. what)
  6480. (setq sorting-type (read-char-exclusive))
  6481. (and (= (downcase sorting-type) ?f)
  6482. (setq getkey-func
  6483. (org-icompleting-read "Sort using function: "
  6484. obarray 'fboundp t nil nil))
  6485. (setq getkey-func (intern getkey-func)))
  6486. (and (= (downcase sorting-type) ?r)
  6487. (setq property
  6488. (org-icompleting-read "Property: "
  6489. (mapcar 'list (org-buffer-property-keys t))
  6490. nil t))))
  6491. (message "Sorting entries...")
  6492. (save-restriction
  6493. (narrow-to-region start end)
  6494. (let ((dcst (downcase sorting-type))
  6495. (case-fold-search nil)
  6496. (now (current-time)))
  6497. (sort-subr
  6498. (/= dcst sorting-type)
  6499. ;; This function moves to the beginning character of the "record" to
  6500. ;; be sorted.
  6501. (if plain-list-p
  6502. (lambda nil
  6503. (if (org-at-item-p) t (goto-char (point-max))))
  6504. (lambda nil
  6505. (if (re-search-forward re nil t)
  6506. (goto-char (match-beginning 0))
  6507. (goto-char (point-max)))))
  6508. ;; This function moves to the last character of the "record" being
  6509. ;; sorted.
  6510. (if plain-list-p
  6511. 'org-end-of-item
  6512. (lambda nil
  6513. (save-match-data
  6514. (condition-case nil
  6515. (outline-forward-same-level 1)
  6516. (error
  6517. (goto-char (point-max)))))))
  6518. ;; This function returns the value that gets sorted against.
  6519. (if plain-list-p
  6520. (lambda nil
  6521. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  6522. (cond
  6523. ((= dcst ?n)
  6524. (string-to-number (buffer-substring (match-end 0)
  6525. (point-at-eol))))
  6526. ((= dcst ?a)
  6527. (buffer-substring (match-end 0) (point-at-eol)))
  6528. ((= dcst ?t)
  6529. (if (or (re-search-forward org-ts-regexp (point-at-eol) t)
  6530. (re-search-forward org-ts-regexp-both
  6531. (point-at-eol) t))
  6532. (org-time-string-to-seconds (match-string 0))
  6533. (org-float-time now)))
  6534. ((= dcst ?f)
  6535. (if getkey-func
  6536. (progn
  6537. (setq tmp (funcall getkey-func))
  6538. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6539. tmp)
  6540. (error "Invalid key function `%s'" getkey-func)))
  6541. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6542. (lambda nil
  6543. (cond
  6544. ((= dcst ?n)
  6545. (if (looking-at org-complex-heading-regexp)
  6546. (string-to-number (match-string 4))
  6547. nil))
  6548. ((= dcst ?a)
  6549. (if (looking-at org-complex-heading-regexp)
  6550. (funcall case-func (match-string 4))
  6551. nil))
  6552. ((= dcst ?t)
  6553. (let ((end (save-excursion (outline-next-heading) (point))))
  6554. (if (or (re-search-forward org-ts-regexp end t)
  6555. (re-search-forward org-ts-regexp-both end t))
  6556. (org-time-string-to-seconds (match-string 0))
  6557. (org-float-time now))))
  6558. ((= dcst ?c)
  6559. (let ((end (save-excursion (outline-next-heading) (point))))
  6560. (if (re-search-forward
  6561. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  6562. end t)
  6563. (org-time-string-to-seconds (match-string 0))
  6564. (org-float-time now))))
  6565. ((= dcst ?s)
  6566. (let ((end (save-excursion (outline-next-heading) (point))))
  6567. (if (re-search-forward org-scheduled-time-regexp end t)
  6568. (org-time-string-to-seconds (match-string 1))
  6569. (org-float-time now))))
  6570. ((= dcst ?d)
  6571. (let ((end (save-excursion (outline-next-heading) (point))))
  6572. (if (re-search-forward org-deadline-time-regexp end t)
  6573. (org-time-string-to-seconds (match-string 1))
  6574. (org-float-time now))))
  6575. ((= dcst ?p)
  6576. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  6577. (string-to-char (match-string 2))
  6578. org-default-priority))
  6579. ((= dcst ?r)
  6580. (or (org-entry-get nil property) ""))
  6581. ((= dcst ?o)
  6582. (if (looking-at org-complex-heading-regexp)
  6583. (- 9999 (length (member (match-string 2)
  6584. org-todo-keywords-1)))))
  6585. ((= dcst ?f)
  6586. (if getkey-func
  6587. (progn
  6588. (setq tmp (funcall getkey-func))
  6589. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  6590. tmp)
  6591. (error "Invalid key function `%s'" getkey-func)))
  6592. (t (error "Invalid sorting type `%c'" sorting-type)))))
  6593. nil
  6594. (cond
  6595. ((= dcst ?a) 'string<)
  6596. ((= dcst ?f) compare-func)
  6597. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  6598. (t nil)))))
  6599. (run-hooks 'org-after-sorting-entries-or-items-hook)
  6600. (message "Sorting entries...done")))
  6601. (defun org-do-sort (table what &optional with-case sorting-type)
  6602. "Sort TABLE of WHAT according to SORTING-TYPE.
  6603. The user will be prompted for the SORTING-TYPE if the call to this
  6604. function does not specify it. WHAT is only for the prompt, to indicate
  6605. what is being sorted. The sorting key will be extracted from
  6606. the car of the elements of the table.
  6607. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  6608. (unless sorting-type
  6609. (message
  6610. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  6611. what)
  6612. (setq sorting-type (read-char-exclusive)))
  6613. (let ((dcst (downcase sorting-type))
  6614. extractfun comparefun)
  6615. ;; Define the appropriate functions
  6616. (cond
  6617. ((= dcst ?n)
  6618. (setq extractfun 'string-to-number
  6619. comparefun (if (= dcst sorting-type) '< '>)))
  6620. ((= dcst ?a)
  6621. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  6622. (lambda(x) (downcase (org-sort-remove-invisible x))))
  6623. comparefun (if (= dcst sorting-type)
  6624. 'string<
  6625. (lambda (a b) (and (not (string< a b))
  6626. (not (string= a b)))))))
  6627. ((= dcst ?t)
  6628. (setq extractfun
  6629. (lambda (x)
  6630. (if (or (string-match org-ts-regexp x)
  6631. (string-match org-ts-regexp-both x))
  6632. (org-float-time
  6633. (org-time-string-to-time (match-string 0 x)))
  6634. 0))
  6635. comparefun (if (= dcst sorting-type) '< '>)))
  6636. (t (error "Invalid sorting type `%c'" sorting-type)))
  6637. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  6638. table)
  6639. (lambda (a b) (funcall comparefun (car a) (car b))))))
  6640. ;;; The orgstruct minor mode
  6641. ;; Define a minor mode which can be used in other modes in order to
  6642. ;; integrate the org-mode structure editing commands.
  6643. ;; This is really a hack, because the org-mode structure commands use
  6644. ;; keys which normally belong to the major mode. Here is how it
  6645. ;; works: The minor mode defines all the keys necessary to operate the
  6646. ;; structure commands, but wraps the commands into a function which
  6647. ;; tests if the cursor is currently at a headline or a plain list
  6648. ;; item. If that is the case, the structure command is used,
  6649. ;; temporarily setting many Org-mode variables like regular
  6650. ;; expressions for filling etc. However, when any of those keys is
  6651. ;; used at a different location, function uses `key-binding' to look
  6652. ;; up if the key has an associated command in another currently active
  6653. ;; keymap (minor modes, major mode, global), and executes that
  6654. ;; command. There might be problems if any of the keys is otherwise
  6655. ;; used as a prefix key.
  6656. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  6657. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  6658. ;; addresses this by checking explicitly for both bindings.
  6659. (defvar orgstruct-mode-map (make-sparse-keymap)
  6660. "Keymap for the minor `orgstruct-mode'.")
  6661. (defvar org-local-vars nil
  6662. "List of local variables, for use by `orgstruct-mode'")
  6663. ;;;###autoload
  6664. (define-minor-mode orgstruct-mode
  6665. "Toggle the minor more `orgstruct-mode'.
  6666. This mode is for using Org-mode structure commands in other modes.
  6667. The following key behave as if Org-mode was active, if the cursor
  6668. is on a headline, or on a plain list item (both in the definition
  6669. of Org-mode).
  6670. M-up Move entry/item up
  6671. M-down Move entry/item down
  6672. M-left Promote
  6673. M-right Demote
  6674. M-S-up Move entry/item up
  6675. M-S-down Move entry/item down
  6676. M-S-left Promote subtree
  6677. M-S-right Demote subtree
  6678. M-q Fill paragraph and items like in Org-mode
  6679. C-c ^ Sort entries
  6680. C-c - Cycle list bullet
  6681. TAB Cycle item visibility
  6682. M-RET Insert new heading/item
  6683. S-M-RET Insert new TODO heading / Checkbox item
  6684. C-c C-c Set tags / toggle checkbox"
  6685. nil " OrgStruct" nil
  6686. (org-load-modules-maybe)
  6687. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  6688. ;;;###autoload
  6689. (defun turn-on-orgstruct ()
  6690. "Unconditionally turn on `orgstruct-mode'."
  6691. (orgstruct-mode 1))
  6692. (defun orgstruct++-mode (&optional arg)
  6693. "Toggle `orgstruct-mode', the enhanced version of it.
  6694. In addition to setting orgstruct-mode, this also exports all indentation
  6695. and autofilling variables from org-mode into the buffer. It will also
  6696. recognize item context in multiline items.
  6697. Note that turning off orgstruct-mode will *not* remove the
  6698. indentation/paragraph settings. This can only be done by refreshing the
  6699. major mode, for example with \\[normal-mode]."
  6700. (interactive "P")
  6701. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  6702. (if (< arg 1)
  6703. (orgstruct-mode -1)
  6704. (orgstruct-mode 1)
  6705. (let (var val)
  6706. (mapc
  6707. (lambda (x)
  6708. (when (string-match
  6709. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6710. (symbol-name (car x)))
  6711. (setq var (car x) val (nth 1 x))
  6712. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  6713. org-local-vars)
  6714. (org-set-local 'orgstruct-is-++ t))))
  6715. (defvar orgstruct-is-++ nil
  6716. "Is orgstruct-mode in ++ version in the current-buffer?")
  6717. (make-variable-buffer-local 'orgstruct-is-++)
  6718. ;;;###autoload
  6719. (defun turn-on-orgstruct++ ()
  6720. "Unconditionally turn on `orgstruct++-mode'."
  6721. (orgstruct++-mode 1))
  6722. (defun orgstruct-error ()
  6723. "Error when there is no default binding for a structure key."
  6724. (interactive)
  6725. (error "This key has no function outside structure elements"))
  6726. (defun orgstruct-setup ()
  6727. "Setup orgstruct keymaps."
  6728. (let ((nfunc 0)
  6729. (bindings
  6730. (list
  6731. '([(meta up)] org-metaup)
  6732. '([(meta down)] org-metadown)
  6733. '([(meta left)] org-metaleft)
  6734. '([(meta right)] org-metaright)
  6735. '([(meta shift up)] org-shiftmetaup)
  6736. '([(meta shift down)] org-shiftmetadown)
  6737. '([(meta shift left)] org-shiftmetaleft)
  6738. '([(meta shift right)] org-shiftmetaright)
  6739. '([?\e (up)] org-metaup)
  6740. '([?\e (down)] org-metadown)
  6741. '([?\e (left)] org-metaleft)
  6742. '([?\e (right)] org-metaright)
  6743. '([?\e (shift up)] org-shiftmetaup)
  6744. '([?\e (shift down)] org-shiftmetadown)
  6745. '([?\e (shift left)] org-shiftmetaleft)
  6746. '([?\e (shift right)] org-shiftmetaright)
  6747. '([(shift up)] org-shiftup)
  6748. '([(shift down)] org-shiftdown)
  6749. '([(shift left)] org-shiftleft)
  6750. '([(shift right)] org-shiftright)
  6751. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  6752. '("\M-q" fill-paragraph)
  6753. '("\C-c^" org-sort)
  6754. '("\C-c-" org-cycle-list-bullet)))
  6755. elt key fun cmd)
  6756. (while (setq elt (pop bindings))
  6757. (setq nfunc (1+ nfunc))
  6758. (setq key (org-key (car elt))
  6759. fun (nth 1 elt)
  6760. cmd (orgstruct-make-binding fun nfunc key))
  6761. (org-defkey orgstruct-mode-map key cmd))
  6762. ;; Special treatment needed for TAB and RET
  6763. (org-defkey orgstruct-mode-map [(tab)]
  6764. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  6765. (org-defkey orgstruct-mode-map "\C-i"
  6766. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  6767. (org-defkey orgstruct-mode-map "\M-\C-m"
  6768. (orgstruct-make-binding 'org-insert-heading 105
  6769. "\M-\C-m" [(meta return)]))
  6770. (org-defkey orgstruct-mode-map [(meta return)]
  6771. (orgstruct-make-binding 'org-insert-heading 106
  6772. [(meta return)] "\M-\C-m"))
  6773. (org-defkey orgstruct-mode-map [(shift meta return)]
  6774. (orgstruct-make-binding 'org-insert-todo-heading 107
  6775. [(meta return)] "\M-\C-m"))
  6776. (org-defkey orgstruct-mode-map "\e\C-m"
  6777. (orgstruct-make-binding 'org-insert-heading 108
  6778. "\e\C-m" [?\e (return)]))
  6779. (org-defkey orgstruct-mode-map [?\e (return)]
  6780. (orgstruct-make-binding 'org-insert-heading 109
  6781. [?\e (return)] "\e\C-m"))
  6782. (org-defkey orgstruct-mode-map [?\e (shift return)]
  6783. (orgstruct-make-binding 'org-insert-todo-heading 110
  6784. [?\e (return)] "\e\C-m"))
  6785. (unless org-local-vars
  6786. (setq org-local-vars (org-get-local-variables)))
  6787. t))
  6788. (defun orgstruct-make-binding (fun n &rest keys)
  6789. "Create a function for binding in the structure minor mode.
  6790. FUN is the command to call inside a table. N is used to create a unique
  6791. command name. KEYS are keys that should be checked in for a command
  6792. to execute outside of tables."
  6793. (eval
  6794. (list 'defun
  6795. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  6796. '(arg)
  6797. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  6798. "Outside of structure, run the binding of `"
  6799. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  6800. "'.")
  6801. '(interactive "p")
  6802. (list 'if
  6803. `(org-context-p 'headline 'item
  6804. (and orgstruct-is-++
  6805. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  6806. 'item-body))
  6807. (list 'org-run-like-in-org-mode (list 'quote fun))
  6808. (list 'let '(orgstruct-mode)
  6809. (list 'call-interactively
  6810. (append '(or)
  6811. (mapcar (lambda (k)
  6812. (list 'key-binding k))
  6813. keys)
  6814. '('orgstruct-error))))))))
  6815. (defun org-context-p (&rest contexts)
  6816. "Check if local context is any of CONTEXTS.
  6817. Possible values in the list of contexts are `table', `headline', and `item'."
  6818. (let ((pos (point)))
  6819. (goto-char (point-at-bol))
  6820. (prog1 (or (and (memq 'table contexts)
  6821. (looking-at "[ \t]*|"))
  6822. (and (memq 'headline contexts)
  6823. ;;????????? (looking-at "\\*+"))
  6824. (looking-at outline-regexp))
  6825. (and (memq 'item contexts)
  6826. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  6827. (and (memq 'item-body contexts)
  6828. (org-in-item-p)))
  6829. (goto-char pos))))
  6830. (defun org-get-local-variables ()
  6831. "Return a list of all local variables in an org-mode buffer."
  6832. (let (varlist)
  6833. (with-current-buffer (get-buffer-create "*Org tmp*")
  6834. (erase-buffer)
  6835. (org-mode)
  6836. (setq varlist (buffer-local-variables)))
  6837. (kill-buffer "*Org tmp*")
  6838. (delq nil
  6839. (mapcar
  6840. (lambda (x)
  6841. (setq x
  6842. (if (symbolp x)
  6843. (list x)
  6844. (list (car x) (list 'quote (cdr x)))))
  6845. (if (string-match
  6846. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  6847. (symbol-name (car x)))
  6848. x nil))
  6849. varlist))))
  6850. ;;;###autoload
  6851. (defun org-run-like-in-org-mode (cmd)
  6852. "Run a command, pretending that the current buffer is in Org-mode.
  6853. This will temporarily bind local variables that are typically bound in
  6854. Org-mode to the values they have in Org-mode, and then interactively
  6855. call CMD."
  6856. (org-load-modules-maybe)
  6857. (unless org-local-vars
  6858. (setq org-local-vars (org-get-local-variables)))
  6859. (eval (list 'let org-local-vars
  6860. (list 'call-interactively (list 'quote cmd)))))
  6861. ;;;; Archiving
  6862. (defun org-get-category (&optional pos)
  6863. "Get the category applying to position POS."
  6864. (get-text-property (or pos (point)) 'org-category))
  6865. (defun org-refresh-category-properties ()
  6866. "Refresh category text properties in the buffer."
  6867. (let ((def-cat (cond
  6868. ((null org-category)
  6869. (if buffer-file-name
  6870. (file-name-sans-extension
  6871. (file-name-nondirectory buffer-file-name))
  6872. "???"))
  6873. ((symbolp org-category) (symbol-name org-category))
  6874. (t org-category)))
  6875. beg end cat pos optionp)
  6876. (org-unmodified
  6877. (save-excursion
  6878. (save-restriction
  6879. (widen)
  6880. (goto-char (point-min))
  6881. (put-text-property (point) (point-max) 'org-category def-cat)
  6882. (while (re-search-forward
  6883. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  6884. (setq pos (match-end 0)
  6885. optionp (equal (char-after (match-beginning 0)) ?#)
  6886. cat (org-trim (match-string 2)))
  6887. (if optionp
  6888. (setq beg (point-at-bol) end (point-max))
  6889. (org-back-to-heading t)
  6890. (setq beg (point) end (org-end-of-subtree t t)))
  6891. (put-text-property beg end 'org-category cat)
  6892. (goto-char pos)))))))
  6893. ;;;; Link Stuff
  6894. ;;; Link abbreviations
  6895. (defun org-link-expand-abbrev (link)
  6896. "Apply replacements as defined in `org-link-abbrev-alist."
  6897. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  6898. (let* ((key (match-string 1 link))
  6899. (as (or (assoc key org-link-abbrev-alist-local)
  6900. (assoc key org-link-abbrev-alist)))
  6901. (tag (and (match-end 2) (match-string 3 link)))
  6902. rpl)
  6903. (if (not as)
  6904. link
  6905. (setq rpl (cdr as))
  6906. (cond
  6907. ((symbolp rpl) (funcall rpl tag))
  6908. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  6909. ((string-match "%h" rpl)
  6910. (replace-match (url-hexify-string (or tag "")) t t rpl))
  6911. (t (concat rpl tag)))))
  6912. link))
  6913. ;;; Storing and inserting links
  6914. (defvar org-insert-link-history nil
  6915. "Minibuffer history for links inserted with `org-insert-link'.")
  6916. (defvar org-stored-links nil
  6917. "Contains the links stored with `org-store-link'.")
  6918. (defvar org-store-link-plist nil
  6919. "Plist with info about the most recently link created with `org-store-link'.")
  6920. (defvar org-link-protocols nil
  6921. "Link protocols added to Org-mode using `org-add-link-type'.")
  6922. (defvar org-store-link-functions nil
  6923. "List of functions that are called to create and store a link.
  6924. Each function will be called in turn until one returns a non-nil
  6925. value. Each function should check if it is responsible for creating
  6926. this link (for example by looking at the major mode).
  6927. If not, it must exit and return nil.
  6928. If yes, it should return a non-nil value after a calling
  6929. `org-store-link-props' with a list of properties and values.
  6930. Special properties are:
  6931. :type The link prefix. like \"http\". This must be given.
  6932. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  6933. This is obligatory as well.
  6934. :description Optional default description for the second pair
  6935. of brackets in an Org-mode link. The user can still change
  6936. this when inserting this link into an Org-mode buffer.
  6937. In addition to these, any additional properties can be specified
  6938. and then used in remember templates.")
  6939. (defun org-add-link-type (type &optional follow export)
  6940. "Add TYPE to the list of `org-link-types'.
  6941. Re-compute all regular expressions depending on `org-link-types'
  6942. FOLLOW and EXPORT are two functions.
  6943. FOLLOW should take the link path as the single argument and do whatever
  6944. is necessary to follow the link, for example find a file or display
  6945. a mail message.
  6946. EXPORT should format the link path for export to one of the export formats.
  6947. It should be a function accepting three arguments:
  6948. path the path of the link, the text after the prefix (like \"http:\")
  6949. desc the description of the link, if any, nil if there was no description
  6950. format the export format, a symbol like `html' or `latex'.
  6951. The function may use the FORMAT information to return different values
  6952. depending on the format. The return value will be put literally into
  6953. the exported file.
  6954. Org-mode has a built-in default for exporting links. If you are happy with
  6955. this default, there is no need to define an export function for the link
  6956. type. For a simple example of an export function, see `org-bbdb.el'."
  6957. (add-to-list 'org-link-types type t)
  6958. (org-make-link-regexps)
  6959. (if (assoc type org-link-protocols)
  6960. (setcdr (assoc type org-link-protocols) (list follow export))
  6961. (push (list type follow export) org-link-protocols)))
  6962. (defvar org-agenda-buffer-name)
  6963. ;;;###autoload
  6964. (defun org-store-link (arg)
  6965. "\\<org-mode-map>Store an org-link to the current location.
  6966. This link is added to `org-stored-links' and can later be inserted
  6967. into an org-buffer with \\[org-insert-link].
  6968. For some link types, a prefix arg is interpreted:
  6969. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  6970. For file links, arg negates `org-context-in-file-links'."
  6971. (interactive "P")
  6972. (org-load-modules-maybe)
  6973. (setq org-store-link-plist nil) ; reset
  6974. (let ((outline-regexp (org-get-limited-outline-regexp))
  6975. link cpltxt desc description search txt custom-id)
  6976. (cond
  6977. ((run-hook-with-args-until-success 'org-store-link-functions)
  6978. (setq link (plist-get org-store-link-plist :link)
  6979. desc (or (plist-get org-store-link-plist :description) link)))
  6980. ((equal (buffer-name) "*Org Edit Src Example*")
  6981. (let (label gc)
  6982. (while (or (not label)
  6983. (save-excursion
  6984. (save-restriction
  6985. (widen)
  6986. (goto-char (point-min))
  6987. (re-search-forward
  6988. (regexp-quote (format org-coderef-label-format label))
  6989. nil t))))
  6990. (when label (message "Label exists already") (sit-for 2))
  6991. (setq label (read-string "Code line label: " label)))
  6992. (end-of-line 1)
  6993. (setq link (format org-coderef-label-format label))
  6994. (setq gc (- 79 (length link)))
  6995. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  6996. (insert link)
  6997. (setq link (concat "(" label ")") desc nil)))
  6998. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  6999. ;; We are in the agenda, link to referenced location
  7000. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7001. (get-text-property (point) 'org-marker))))
  7002. (when m
  7003. (org-with-point-at m
  7004. (call-interactively 'org-store-link)))))
  7005. ((eq major-mode 'calendar-mode)
  7006. (let ((cd (calendar-cursor-to-date)))
  7007. (setq link
  7008. (format-time-string
  7009. (car org-time-stamp-formats)
  7010. (apply 'encode-time
  7011. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7012. nil nil nil))))
  7013. (org-store-link-props :type "calendar" :date cd)))
  7014. ((eq major-mode 'w3-mode)
  7015. (setq cpltxt (if (and (buffer-name)
  7016. (not (string-match "Untitled" (buffer-name))))
  7017. (buffer-name)
  7018. (url-view-url t))
  7019. link (org-make-link (url-view-url t)))
  7020. (org-store-link-props :type "w3" :url (url-view-url t)))
  7021. ((eq major-mode 'w3m-mode)
  7022. (setq cpltxt (or w3m-current-title w3m-current-url)
  7023. link (org-make-link w3m-current-url))
  7024. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7025. ((setq search (run-hook-with-args-until-success
  7026. 'org-create-file-search-functions))
  7027. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7028. "::" search))
  7029. (setq cpltxt (or description link)))
  7030. ((eq major-mode 'image-mode)
  7031. (setq cpltxt (concat "file:"
  7032. (abbreviate-file-name buffer-file-name))
  7033. link (org-make-link cpltxt))
  7034. (org-store-link-props :type "image" :file buffer-file-name))
  7035. ((eq major-mode 'dired-mode)
  7036. ;; link to the file in the current line
  7037. (let ((file (dired-get-filename nil t)))
  7038. (setq file (if file
  7039. (abbreviate-file-name
  7040. (expand-file-name (dired-get-filename nil t)))
  7041. ;; otherwise, no file so use current directory.
  7042. default-directory))
  7043. (setq cpltxt (concat "file:" file)
  7044. link (org-make-link cpltxt))))
  7045. ((and buffer-file-name (org-mode-p))
  7046. (setq custom-id (ignore-errors (org-entry-get nil "CUSTOM_ID")))
  7047. (cond
  7048. ((org-in-regexp "<<\\(.*?\\)>>")
  7049. (setq cpltxt
  7050. (concat "file:"
  7051. (abbreviate-file-name buffer-file-name)
  7052. "::" (match-string 1))
  7053. link (org-make-link cpltxt)))
  7054. ((and (featurep 'org-id)
  7055. (or (eq org-link-to-org-use-id t)
  7056. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7057. (interactive-p))
  7058. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7059. (interactive-p)
  7060. (not custom-id))
  7061. (and org-link-to-org-use-id
  7062. (condition-case nil
  7063. (org-entry-get nil "ID")
  7064. (error nil)))))
  7065. ;; We can make a link using the ID.
  7066. (setq link (condition-case nil
  7067. (prog1 (org-id-store-link)
  7068. (setq desc (plist-get org-store-link-plist
  7069. :description)))
  7070. (error
  7071. ;; probably before first headline, link to file only
  7072. (concat "file:"
  7073. (abbreviate-file-name buffer-file-name))))))
  7074. (t
  7075. ;; Just link to current headline
  7076. (setq cpltxt (concat "file:"
  7077. (abbreviate-file-name buffer-file-name)))
  7078. ;; Add a context search string
  7079. (when (org-xor org-context-in-file-links arg)
  7080. (setq txt (cond
  7081. ((org-on-heading-p) nil)
  7082. ((org-region-active-p)
  7083. (buffer-substring (region-beginning) (region-end)))
  7084. (t nil)))
  7085. (when (or (null txt) (string-match "\\S-" txt))
  7086. (setq cpltxt
  7087. (concat cpltxt "::"
  7088. (condition-case nil
  7089. (org-make-org-heading-search-string txt)
  7090. (error "")))
  7091. desc (or (nth 4 (ignore-errors
  7092. (org-heading-components))) "NONE"))))
  7093. (if (string-match "::\\'" cpltxt)
  7094. (setq cpltxt (substring cpltxt 0 -2)))
  7095. (setq link (org-make-link cpltxt)))))
  7096. ((buffer-file-name (buffer-base-buffer))
  7097. ;; Just link to this file here.
  7098. (setq cpltxt (concat "file:"
  7099. (abbreviate-file-name
  7100. (buffer-file-name (buffer-base-buffer)))))
  7101. ;; Add a context string
  7102. (when (org-xor org-context-in-file-links arg)
  7103. (setq txt (if (org-region-active-p)
  7104. (buffer-substring (region-beginning) (region-end))
  7105. (buffer-substring (point-at-bol) (point-at-eol))))
  7106. ;; Only use search option if there is some text.
  7107. (when (string-match "\\S-" txt)
  7108. (setq cpltxt
  7109. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7110. desc "NONE")))
  7111. (setq link (org-make-link cpltxt)))
  7112. ((interactive-p)
  7113. (error "Cannot link to a buffer which is not visiting a file"))
  7114. (t (setq link nil)))
  7115. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7116. (setq link (or link cpltxt)
  7117. desc (or desc cpltxt))
  7118. (if (equal desc "NONE") (setq desc nil))
  7119. (if (and (or (interactive-p) executing-kbd-macro) link)
  7120. (progn
  7121. (setq org-stored-links
  7122. (cons (list link desc) org-stored-links))
  7123. (message "Stored: %s" (or desc link))
  7124. (when custom-id
  7125. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7126. "::#" custom-id))
  7127. (setq org-stored-links
  7128. (cons (list link desc) org-stored-links))))
  7129. (and link (org-make-link-string link desc)))))
  7130. (defun org-store-link-props (&rest plist)
  7131. "Store link properties, extract names and addresses."
  7132. (let (x adr)
  7133. (when (setq x (plist-get plist :from))
  7134. (setq adr (mail-extract-address-components x))
  7135. (setq plist (plist-put plist :fromname (car adr)))
  7136. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7137. (when (setq x (plist-get plist :to))
  7138. (setq adr (mail-extract-address-components x))
  7139. (setq plist (plist-put plist :toname (car adr)))
  7140. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7141. (let ((from (plist-get plist :from))
  7142. (to (plist-get plist :to)))
  7143. (when (and from to org-from-is-user-regexp)
  7144. (setq plist
  7145. (plist-put plist :fromto
  7146. (if (string-match org-from-is-user-regexp from)
  7147. (concat "to %t")
  7148. (concat "from %f"))))))
  7149. (setq org-store-link-plist plist))
  7150. (defun org-add-link-props (&rest plist)
  7151. "Add these properties to the link property list."
  7152. (let (key value)
  7153. (while plist
  7154. (setq key (pop plist) value (pop plist))
  7155. (setq org-store-link-plist
  7156. (plist-put org-store-link-plist key value)))))
  7157. (defun org-email-link-description (&optional fmt)
  7158. "Return the description part of an email link.
  7159. This takes information from `org-store-link-plist' and formats it
  7160. according to FMT (default from `org-email-link-description-format')."
  7161. (setq fmt (or fmt org-email-link-description-format))
  7162. (let* ((p org-store-link-plist)
  7163. (to (plist-get p :toaddress))
  7164. (from (plist-get p :fromaddress))
  7165. (table
  7166. (list
  7167. (cons "%c" (plist-get p :fromto))
  7168. (cons "%F" (plist-get p :from))
  7169. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7170. (cons "%T" (plist-get p :to))
  7171. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7172. (cons "%s" (plist-get p :subject))
  7173. (cons "%m" (plist-get p :message-id)))))
  7174. (when (string-match "%c" fmt)
  7175. ;; Check if the user wrote this message
  7176. (if (and org-from-is-user-regexp from to
  7177. (save-match-data (string-match org-from-is-user-regexp from)))
  7178. (setq fmt (replace-match "to %t" t t fmt))
  7179. (setq fmt (replace-match "from %f" t t fmt))))
  7180. (org-replace-escapes fmt table)))
  7181. (defun org-make-org-heading-search-string (&optional string heading)
  7182. "Make search string for STRING or current headline."
  7183. (interactive)
  7184. (let ((s (or string (org-get-heading))))
  7185. (unless (and string (not heading))
  7186. ;; We are using a headline, clean up garbage in there.
  7187. (if (string-match org-todo-regexp s)
  7188. (setq s (replace-match "" t t s)))
  7189. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  7190. (setq s (replace-match "" t t s)))
  7191. (setq s (org-trim s))
  7192. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7193. org-comment-string "\\)") s)
  7194. (setq s (replace-match "" t t s)))
  7195. (while (string-match org-ts-regexp s)
  7196. (setq s (replace-match "" t t s))))
  7197. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7198. (setq s (replace-match " " t t s)))
  7199. (or string (setq s (concat "*" s))) ; Add * for headlines
  7200. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7201. (defun org-make-link (&rest strings)
  7202. "Concatenate STRINGS."
  7203. (apply 'concat strings))
  7204. (defun org-make-link-string (link &optional description)
  7205. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7206. (unless (string-match "\\S-" link)
  7207. (error "Empty link"))
  7208. (when (and description
  7209. (stringp description)
  7210. (not (string-match "\\S-" description)))
  7211. (setq description nil))
  7212. (when (stringp description)
  7213. ;; Remove brackets from the description, they are fatal.
  7214. (while (string-match "\\[" description)
  7215. (setq description (replace-match "{" t t description)))
  7216. (while (string-match "\\]" description)
  7217. (setq description (replace-match "}" t t description))))
  7218. (when (equal (org-link-escape link) description)
  7219. ;; No description needed, it is identical
  7220. (setq description nil))
  7221. (when (and (not description)
  7222. (not (equal link (org-link-escape link))))
  7223. (setq description (org-extract-attributes link)))
  7224. (concat "[[" (org-link-escape link) "]"
  7225. (if description (concat "[" description "]") "")
  7226. "]"))
  7227. (defconst org-link-escape-chars
  7228. '((?\ . "%20")
  7229. (?\[ . "%5B")
  7230. (?\] . "%5D")
  7231. (?\340 . "%E0") ; `a
  7232. (?\342 . "%E2") ; ^a
  7233. (?\347 . "%E7") ; ,c
  7234. (?\350 . "%E8") ; `e
  7235. (?\351 . "%E9") ; 'e
  7236. (?\352 . "%EA") ; ^e
  7237. (?\356 . "%EE") ; ^i
  7238. (?\364 . "%F4") ; ^o
  7239. (?\371 . "%F9") ; `u
  7240. (?\373 . "%FB") ; ^u
  7241. (?\; . "%3B")
  7242. ;; (?? . "%3F")
  7243. (?= . "%3D")
  7244. (?+ . "%2B")
  7245. )
  7246. "Association list of escapes for some characters problematic in links.
  7247. This is the list that is used for internal purposes.")
  7248. (defvar org-url-encoding-use-url-hexify nil)
  7249. (defconst org-link-escape-chars-browser
  7250. '((?\ . "%20")) ; 32 for the SPC char
  7251. "Association list of escapes for some characters problematic in links.
  7252. This is the list that is used before handing over to the browser.")
  7253. (defun org-link-escape (text &optional table)
  7254. "Escape characters in TEXT that are problematic for links."
  7255. (if (and org-url-encoding-use-url-hexify (not table))
  7256. (url-hexify-string text)
  7257. (setq table (or table org-link-escape-chars))
  7258. (when text
  7259. (let ((re (mapconcat (lambda (x) (regexp-quote
  7260. (char-to-string (car x))))
  7261. table "\\|")))
  7262. (while (string-match re text)
  7263. (setq text
  7264. (replace-match
  7265. (cdr (assoc (string-to-char (match-string 0 text))
  7266. table))
  7267. t t text)))
  7268. text))))
  7269. (defun org-link-unescape (text &optional table)
  7270. "Reverse the action of `org-link-escape'."
  7271. (if (and org-url-encoding-use-url-hexify (not table))
  7272. (url-unhex-string text)
  7273. (setq table (or table org-link-escape-chars))
  7274. (when text
  7275. (let ((case-fold-search t)
  7276. (re (mapconcat (lambda (x) (regexp-quote (downcase (cdr x))))
  7277. table "\\|")))
  7278. (while (string-match re text)
  7279. (setq text
  7280. (replace-match
  7281. (char-to-string (car (rassoc (upcase (match-string 0 text))
  7282. table)))
  7283. t t text)))
  7284. text))))
  7285. (defun org-xor (a b)
  7286. "Exclusive or."
  7287. (if a (not b) b))
  7288. (defun org-fixup-message-id-for-http (s)
  7289. "Replace special characters in a message id, so it can be used in an http query."
  7290. (while (string-match "<" s)
  7291. (setq s (replace-match "%3C" t t s)))
  7292. (while (string-match ">" s)
  7293. (setq s (replace-match "%3E" t t s)))
  7294. (while (string-match "@" s)
  7295. (setq s (replace-match "%40" t t s)))
  7296. s)
  7297. ;;;###autoload
  7298. (defun org-insert-link-global ()
  7299. "Insert a link like Org-mode does.
  7300. This command can be called in any mode to insert a link in Org-mode syntax."
  7301. (interactive)
  7302. (org-load-modules-maybe)
  7303. (org-run-like-in-org-mode 'org-insert-link))
  7304. (defun org-insert-link (&optional complete-file link-location)
  7305. "Insert a link. At the prompt, enter the link.
  7306. Completion can be used to insert any of the link protocol prefixes like
  7307. http or ftp in use.
  7308. The history can be used to select a link previously stored with
  7309. `org-store-link'. When the empty string is entered (i.e. if you just
  7310. press RET at the prompt), the link defaults to the most recently
  7311. stored link. As SPC triggers completion in the minibuffer, you need to
  7312. use M-SPC or C-q SPC to force the insertion of a space character.
  7313. You will also be prompted for a description, and if one is given, it will
  7314. be displayed in the buffer instead of the link.
  7315. If there is already a link at point, this command will allow you to edit link
  7316. and description parts.
  7317. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7318. be selected using completion. The path to the file will be relative to the
  7319. current directory if the file is in the current directory or a subdirectory.
  7320. Otherwise, the link will be the absolute path as completed in the minibuffer
  7321. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7322. option `org-link-file-path-type'.
  7323. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7324. the current directory or below.
  7325. With three \\[universal-argument] prefixes, negate the meaning of
  7326. `org-keep-stored-link-after-insertion'.
  7327. If `org-make-link-description-function' is non-nil, this function will be
  7328. called with the link target, and the result will be the default
  7329. link description.
  7330. If the LINK-LOCATION parameter is non-nil, this value will be
  7331. used as the link location instead of reading one interactively."
  7332. (interactive "P")
  7333. (let* ((wcf (current-window-configuration))
  7334. (region (if (org-region-active-p)
  7335. (buffer-substring (region-beginning) (region-end))))
  7336. (remove (and region (list (region-beginning) (region-end))))
  7337. (desc region)
  7338. tmphist ; byte-compile incorrectly complains about this
  7339. (link link-location)
  7340. entry file all-prefixes)
  7341. (cond
  7342. (link-location) ; specified by arg, just use it.
  7343. ((org-in-regexp org-bracket-link-regexp 1)
  7344. ;; We do have a link at point, and we are going to edit it.
  7345. (setq remove (list (match-beginning 0) (match-end 0)))
  7346. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7347. (setq link (read-string "Link: "
  7348. (org-link-unescape
  7349. (org-match-string-no-properties 1)))))
  7350. ((or (org-in-regexp org-angle-link-re)
  7351. (org-in-regexp org-plain-link-re))
  7352. ;; Convert to bracket link
  7353. (setq remove (list (match-beginning 0) (match-end 0))
  7354. link (read-string "Link: "
  7355. (org-remove-angle-brackets (match-string 0)))))
  7356. ((member complete-file '((4) (16)))
  7357. ;; Completing read for file names.
  7358. (setq link (org-file-complete-link complete-file)))
  7359. (t
  7360. ;; Read link, with completion for stored links.
  7361. (with-output-to-temp-buffer "*Org Links*"
  7362. (princ "Insert a link.
  7363. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7364. (when org-stored-links
  7365. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7366. (princ (mapconcat
  7367. (lambda (x)
  7368. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7369. (reverse org-stored-links) "\n"))))
  7370. (let ((cw (selected-window)))
  7371. (select-window (get-buffer-window "*Org Links*" 'visible))
  7372. (setq truncate-lines t)
  7373. (unless (pos-visible-in-window-p (point-max))
  7374. (org-fit-window-to-buffer))
  7375. (and (window-live-p cw) (select-window cw)))
  7376. ;; Fake a link history, containing the stored links.
  7377. (setq tmphist (append (mapcar 'car org-stored-links)
  7378. org-insert-link-history))
  7379. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7380. (mapcar 'car org-link-abbrev-alist)
  7381. org-link-types))
  7382. (unwind-protect
  7383. (progn
  7384. (setq link
  7385. (let ((org-completion-use-ido nil)
  7386. (org-completion-use-iswitchb nil))
  7387. (org-completing-read
  7388. "Link: "
  7389. (append
  7390. (mapcar (lambda (x) (list (concat x ":")))
  7391. all-prefixes)
  7392. (mapcar 'car org-stored-links))
  7393. nil nil nil
  7394. 'tmphist
  7395. (car (car org-stored-links)))))
  7396. (if (not (string-match "\\S-" link))
  7397. (error "No link selected"))
  7398. (if (or (member link all-prefixes)
  7399. (and (equal ":" (substring link -1))
  7400. (member (substring link 0 -1) all-prefixes)
  7401. (setq link (substring link 0 -1))))
  7402. (setq link (org-link-try-special-completion link))))
  7403. (set-window-configuration wcf)
  7404. (kill-buffer "*Org Links*"))
  7405. (setq entry (assoc link org-stored-links))
  7406. (or entry (push link org-insert-link-history))
  7407. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  7408. (not org-keep-stored-link-after-insertion))
  7409. (setq org-stored-links (delq (assoc link org-stored-links)
  7410. org-stored-links)))
  7411. (setq desc (or desc (nth 1 entry)))))
  7412. (if (string-match org-plain-link-re link)
  7413. ;; URL-like link, normalize the use of angular brackets.
  7414. (setq link (org-make-link (org-remove-angle-brackets link))))
  7415. ;; Check if we are linking to the current file with a search option
  7416. ;; If yes, simplify the link by using only the search option.
  7417. (when (and buffer-file-name
  7418. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  7419. (let* ((path (match-string 1 link))
  7420. (case-fold-search nil)
  7421. (search (match-string 2 link)))
  7422. (save-match-data
  7423. (if (equal (file-truename buffer-file-name) (file-truename path))
  7424. ;; We are linking to this same file, with a search option
  7425. (setq link search)))))
  7426. ;; Check if we can/should use a relative path. If yes, simplify the link
  7427. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  7428. (let* ((type (match-string 1 link))
  7429. (path (match-string 2 link))
  7430. (origpath path)
  7431. (case-fold-search nil))
  7432. (cond
  7433. ((or (eq org-link-file-path-type 'absolute)
  7434. (equal complete-file '(16)))
  7435. (setq path (abbreviate-file-name (expand-file-name path))))
  7436. ((eq org-link-file-path-type 'noabbrev)
  7437. (setq path (expand-file-name path)))
  7438. ((eq org-link-file-path-type 'relative)
  7439. (setq path (file-relative-name path)))
  7440. (t
  7441. (save-match-data
  7442. (if (string-match (concat "^" (regexp-quote
  7443. (file-name-as-directory
  7444. (expand-file-name "."))))
  7445. (expand-file-name path))
  7446. ;; We are linking a file with relative path name.
  7447. (setq path (substring (expand-file-name path)
  7448. (match-end 0)))
  7449. (setq path (abbreviate-file-name (expand-file-name path)))))))
  7450. (setq link (concat type path))
  7451. (if (equal desc origpath)
  7452. (setq desc path))))
  7453. (if org-make-link-description-function
  7454. (setq desc (funcall org-make-link-description-function link desc)))
  7455. (setq desc (read-string "Description: " desc))
  7456. (unless (string-match "\\S-" desc) (setq desc nil))
  7457. (if remove (apply 'delete-region remove))
  7458. (insert (org-make-link-string link desc))))
  7459. (defun org-link-try-special-completion (type)
  7460. "If there is completion support for link type TYPE, offer it."
  7461. (let ((fun (intern (concat "org-" type "-complete-link"))))
  7462. (if (functionp fun)
  7463. (funcall fun)
  7464. (read-string "Link (no completion support): " (concat type ":")))))
  7465. (defun org-file-complete-link (&optional arg)
  7466. "Create a file link using completion."
  7467. (let (file link)
  7468. (setq file (read-file-name "File: "))
  7469. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  7470. (pwd1 (file-name-as-directory (abbreviate-file-name
  7471. (expand-file-name ".")))))
  7472. (cond
  7473. ((equal arg '(16))
  7474. (setq link (org-make-link
  7475. "file:"
  7476. (abbreviate-file-name (expand-file-name file)))))
  7477. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  7478. (setq link (org-make-link "file:" (match-string 1 file))))
  7479. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7480. (expand-file-name file))
  7481. (setq link (org-make-link
  7482. "file:" (match-string 1 (expand-file-name file)))))
  7483. (t (setq link (org-make-link "file:" file)))))
  7484. link))
  7485. (defun org-completing-read (&rest args)
  7486. "Completing-read with SPACE being a normal character."
  7487. (let ((minibuffer-local-completion-map
  7488. (copy-keymap minibuffer-local-completion-map)))
  7489. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  7490. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  7491. (apply 'org-icompleting-read args)))
  7492. (defun org-completing-read-no-i (&rest args)
  7493. (let (org-completion-use-ido org-completion-use-iswitchb)
  7494. (apply 'org-completing-read args)))
  7495. (defun org-iswitchb-completing-read (prompt choices &rest args)
  7496. "Use iswitch as a completing-read replacement to choose from choices.
  7497. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  7498. from."
  7499. (let* ((iswitchb-use-virtual-buffers nil)
  7500. (iswitchb-make-buflist-hook
  7501. (lambda ()
  7502. (setq iswitchb-temp-buflist choices))))
  7503. (iswitchb-read-buffer prompt)))
  7504. (defun org-icompleting-read (&rest args)
  7505. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  7506. (org-without-partial-completion
  7507. (if (and org-completion-use-ido
  7508. (fboundp 'ido-completing-read)
  7509. (boundp 'ido-mode) ido-mode
  7510. (listp (second args)))
  7511. (let ((ido-enter-matching-directory nil))
  7512. (apply 'ido-completing-read (concat (car args))
  7513. (if (consp (car (nth 1 args)))
  7514. (mapcar (lambda (x) (car x)) (nth 1 args))
  7515. (nth 1 args))
  7516. (cddr args)))
  7517. (if (and org-completion-use-iswitchb
  7518. (boundp 'iswitchb-mode) iswitchb-mode
  7519. (listp (second args)))
  7520. (apply 'org-iswitchb-completing-read (concat (car args))
  7521. (if (consp (car (nth 1 args)))
  7522. (mapcar (lambda (x) (car x)) (nth 1 args))
  7523. (nth 1 args))
  7524. (cddr args))
  7525. (apply 'completing-read args)))))
  7526. (defun org-extract-attributes (s)
  7527. "Extract the attributes cookie from a string and set as text property."
  7528. (let (a attr (start 0) key value)
  7529. (save-match-data
  7530. (when (string-match "{{\\([^}]+\\)}}$" s)
  7531. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  7532. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  7533. (setq key (match-string 1 a) value (match-string 2 a)
  7534. start (match-end 0)
  7535. attr (plist-put attr (intern key) value))))
  7536. (org-add-props s nil 'org-attr attr))
  7537. s))
  7538. (defun org-extract-attributes-from-string (tag)
  7539. (let (key value attr)
  7540. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  7541. (setq key (match-string 1 tag) value (match-string 2 tag)
  7542. tag (replace-match "" t t tag)
  7543. attr (plist-put attr (intern key) value)))
  7544. (cons tag attr)))
  7545. (defun org-attributes-to-string (plist)
  7546. "Format a property list into an HTML attribute list."
  7547. (let ((s "") key value)
  7548. (while plist
  7549. (setq key (pop plist) value (pop plist))
  7550. (and value
  7551. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  7552. s))
  7553. ;;; Opening/following a link
  7554. (defvar org-link-search-failed nil)
  7555. (defvar org-open-link-functions nil
  7556. "Hook for functions finding a plain text link.
  7557. These functions must take a single argument, the link content.
  7558. They will be called for links that look like [[link text][description]]
  7559. when LINK TEXT does not have a protocol like \"http:\" and does not look
  7560. like a filename (e.g. \"./blue.png\").
  7561. These functions will be called *before* Org attempts to resolve the
  7562. link by doing text searches in the current buffer - so if you want a
  7563. link \"[[target]]\" to still find \"<<target>>\", your function should
  7564. handle this as a special case.
  7565. When the function does handle the link, it must return a non-nil value.
  7566. If it decides that it is not responsible for this link, it must return
  7567. nil to indicate that that Org-mode can continue with other options
  7568. like exact and fuzzy text search.")
  7569. (defun org-next-link ()
  7570. "Move forward to the next link.
  7571. If the link is in hidden text, expose it."
  7572. (interactive)
  7573. (when (and org-link-search-failed (eq this-command last-command))
  7574. (goto-char (point-min))
  7575. (message "Link search wrapped back to beginning of buffer"))
  7576. (setq org-link-search-failed nil)
  7577. (let* ((pos (point))
  7578. (ct (org-context))
  7579. (a (assoc :link ct)))
  7580. (if a (goto-char (nth 2 a)))
  7581. (if (re-search-forward org-any-link-re nil t)
  7582. (progn
  7583. (goto-char (match-beginning 0))
  7584. (if (org-invisible-p) (org-show-context)))
  7585. (goto-char pos)
  7586. (setq org-link-search-failed t)
  7587. (error "No further link found"))))
  7588. (defun org-previous-link ()
  7589. "Move backward to the previous link.
  7590. If the link is in hidden text, expose it."
  7591. (interactive)
  7592. (when (and org-link-search-failed (eq this-command last-command))
  7593. (goto-char (point-max))
  7594. (message "Link search wrapped back to end of buffer"))
  7595. (setq org-link-search-failed nil)
  7596. (let* ((pos (point))
  7597. (ct (org-context))
  7598. (a (assoc :link ct)))
  7599. (if a (goto-char (nth 1 a)))
  7600. (if (re-search-backward org-any-link-re nil t)
  7601. (progn
  7602. (goto-char (match-beginning 0))
  7603. (if (org-invisible-p) (org-show-context)))
  7604. (goto-char pos)
  7605. (setq org-link-search-failed t)
  7606. (error "No further link found"))))
  7607. (defun org-translate-link (s)
  7608. "Translate a link string if a translation function has been defined."
  7609. (if (and org-link-translation-function
  7610. (fboundp org-link-translation-function)
  7611. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  7612. (progn
  7613. (setq s (funcall org-link-translation-function
  7614. (match-string 1) (match-string 2)))
  7615. (concat (car s) ":" (cdr s)))
  7616. s))
  7617. (defun org-translate-link-from-planner (type path)
  7618. "Translate a link from Emacs Planner syntax so that Org can follow it.
  7619. This is still an experimental function, your mileage may vary."
  7620. (cond
  7621. ((member type '("http" "https" "news" "ftp"))
  7622. ;; standard Internet links are the same.
  7623. nil)
  7624. ((and (equal type "irc") (string-match "^//" path))
  7625. ;; Planner has two / at the beginning of an irc link, we have 1.
  7626. ;; We should have zero, actually....
  7627. (setq path (substring path 1)))
  7628. ((and (equal type "lisp") (string-match "^/" path))
  7629. ;; Planner has a slash, we do not.
  7630. (setq type "elisp" path (substring path 1)))
  7631. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  7632. ;; A typical message link. Planner has the id after the final slash,
  7633. ;; we separate it with a hash mark
  7634. (setq path (concat (match-string 1 path) "#"
  7635. (org-remove-angle-brackets (match-string 2 path)))))
  7636. )
  7637. (cons type path))
  7638. (defun org-find-file-at-mouse (ev)
  7639. "Open file link or URL at mouse."
  7640. (interactive "e")
  7641. (mouse-set-point ev)
  7642. (org-open-at-point 'in-emacs))
  7643. (defun org-open-at-mouse (ev)
  7644. "Open file link or URL at mouse."
  7645. (interactive "e")
  7646. (mouse-set-point ev)
  7647. (if (eq major-mode 'org-agenda-mode)
  7648. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  7649. (org-open-at-point))
  7650. (defvar org-window-config-before-follow-link nil
  7651. "The window configuration before following a link.
  7652. This is saved in case the need arises to restore it.")
  7653. (defvar org-open-link-marker (make-marker)
  7654. "Marker pointing to the location where `org-open-at-point; was called.")
  7655. ;;;###autoload
  7656. (defun org-open-at-point-global ()
  7657. "Follow a link like Org-mode does.
  7658. This command can be called in any mode to follow a link that has
  7659. Org-mode syntax."
  7660. (interactive)
  7661. (org-run-like-in-org-mode 'org-open-at-point))
  7662. ;;;###autoload
  7663. (defun org-open-link-from-string (s &optional arg reference-buffer)
  7664. "Open a link in the string S, as if it was in Org-mode."
  7665. (interactive "sLink: \nP")
  7666. (let ((reference-buffer (or reference-buffer (current-buffer))))
  7667. (with-temp-buffer
  7668. (let ((org-inhibit-startup t))
  7669. (org-mode)
  7670. (insert s)
  7671. (goto-char (point-min))
  7672. (when reference-buffer
  7673. (setq org-link-abbrev-alist-local
  7674. (with-current-buffer reference-buffer
  7675. org-link-abbrev-alist-local)))
  7676. (org-open-at-point arg reference-buffer)))))
  7677. (defun org-open-at-point (&optional in-emacs reference-buffer)
  7678. "Open link at or after point.
  7679. If there is no link at point, this function will search forward up to
  7680. the end of the current line.
  7681. Normally, files will be opened by an appropriate application. If the
  7682. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  7683. With a double prefix argument, try to open outside of Emacs, in the
  7684. application the system uses for this file type."
  7685. (interactive "P")
  7686. (org-load-modules-maybe)
  7687. (move-marker org-open-link-marker (point))
  7688. (setq org-window-config-before-follow-link (current-window-configuration))
  7689. (org-remove-occur-highlights nil nil t)
  7690. (cond
  7691. ((and (org-on-heading-p)
  7692. (not (org-in-regexp
  7693. (concat org-plain-link-re "\\|"
  7694. org-bracket-link-regexp "\\|"
  7695. org-angle-link-re "\\|"
  7696. "[ \t]:[^ \t\n]+:[ \t]*$")))
  7697. (not (get-text-property (point) 'org-linked-text)))
  7698. (or (org-offer-links-in-entry in-emacs)
  7699. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  7700. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  7701. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  7702. (org-footnote-action))
  7703. (t
  7704. (let (type path link line search (pos (point)))
  7705. (catch 'match
  7706. (save-excursion
  7707. (skip-chars-forward "^]\n\r")
  7708. (when (org-in-regexp org-bracket-link-regexp 1)
  7709. (setq link (org-extract-attributes
  7710. (org-link-unescape (org-match-string-no-properties 1))))
  7711. (while (string-match " *\n *" link)
  7712. (setq link (replace-match " " t t link)))
  7713. (setq link (org-link-expand-abbrev link))
  7714. (cond
  7715. ((or (file-name-absolute-p link)
  7716. (string-match "^\\.\\.?/" link))
  7717. (setq type "file" path link))
  7718. ((string-match org-link-re-with-space3 link)
  7719. (setq type (match-string 1 link) path (match-string 2 link)))
  7720. (t (setq type "thisfile" path link)))
  7721. (throw 'match t)))
  7722. (when (get-text-property (point) 'org-linked-text)
  7723. (setq type "thisfile"
  7724. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7725. (1+ (point)) (point))
  7726. path (buffer-substring
  7727. (previous-single-property-change pos 'org-linked-text)
  7728. (next-single-property-change pos 'org-linked-text)))
  7729. (throw 'match t))
  7730. (save-excursion
  7731. (when (or (org-in-regexp org-angle-link-re)
  7732. (org-in-regexp org-plain-link-re))
  7733. (setq type (match-string 1) path (match-string 2))
  7734. (throw 'match t)))
  7735. (save-excursion
  7736. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  7737. (setq type "tags"
  7738. path (match-string 1))
  7739. (while (string-match ":" path)
  7740. (setq path (replace-match "+" t t path)))
  7741. (throw 'match t)))
  7742. (when (org-in-regexp "<\\([^><\n]+\\)>")
  7743. (setq type "tree-match"
  7744. path (match-string 1))
  7745. (throw 'match t)))
  7746. (unless path
  7747. (error "No link found"))
  7748. ;; switch back to reference buffer
  7749. ;; needed when if called in a temporary buffer through
  7750. ;; org-open-link-from-string
  7751. (with-current-buffer (or reference-buffer (current-buffer))
  7752. ;; Remove any trailing spaces in path
  7753. (if (string-match " +\\'" path)
  7754. (setq path (replace-match "" t t path)))
  7755. (if (and org-link-translation-function
  7756. (fboundp org-link-translation-function))
  7757. ;; Check if we need to translate the link
  7758. (let ((tmp (funcall org-link-translation-function type path)))
  7759. (setq type (car tmp) path (cdr tmp))))
  7760. (cond
  7761. ((assoc type org-link-protocols)
  7762. (funcall (nth 1 (assoc type org-link-protocols)) path))
  7763. ((equal type "mailto")
  7764. (let ((cmd (car org-link-mailto-program))
  7765. (args (cdr org-link-mailto-program)) args1
  7766. (address path) (subject "") a)
  7767. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  7768. (setq address (match-string 1 path)
  7769. subject (org-link-escape (match-string 2 path))))
  7770. (while args
  7771. (cond
  7772. ((not (stringp (car args))) (push (pop args) args1))
  7773. (t (setq a (pop args))
  7774. (if (string-match "%a" a)
  7775. (setq a (replace-match address t t a)))
  7776. (if (string-match "%s" a)
  7777. (setq a (replace-match subject t t a)))
  7778. (push a args1))))
  7779. (apply cmd (nreverse args1))))
  7780. ((member type '("http" "https" "ftp" "news"))
  7781. (browse-url (concat type ":" (org-link-escape
  7782. path org-link-escape-chars-browser))))
  7783. ((member type '("message"))
  7784. (browse-url (concat type ":" path)))
  7785. ((string= type "tags")
  7786. (org-tags-view in-emacs path))
  7787. ((string= type "tree-match")
  7788. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  7789. ((string= type "file")
  7790. (if (string-match "::\\([0-9]+\\)\\'" path)
  7791. (setq line (string-to-number (match-string 1 path))
  7792. path (substring path 0 (match-beginning 0)))
  7793. (if (string-match "::\\(.+\\)\\'" path)
  7794. (setq search (match-string 1 path)
  7795. path (substring path 0 (match-beginning 0)))))
  7796. (if (string-match "[*?{]" (file-name-nondirectory path))
  7797. (dired path)
  7798. (org-open-file path in-emacs line search)))
  7799. ((string= type "news")
  7800. (require 'org-gnus)
  7801. (org-gnus-follow-link path))
  7802. ((string= type "shell")
  7803. (let ((cmd path))
  7804. (if (or (not org-confirm-shell-link-function)
  7805. (funcall org-confirm-shell-link-function
  7806. (format "Execute \"%s\" in shell? "
  7807. (org-add-props cmd nil
  7808. 'face 'org-warning))))
  7809. (progn
  7810. (message "Executing %s" cmd)
  7811. (shell-command cmd))
  7812. (error "Abort"))))
  7813. ((string= type "elisp")
  7814. (let ((cmd path))
  7815. (if (or (not org-confirm-elisp-link-function)
  7816. (funcall org-confirm-elisp-link-function
  7817. (format "Execute \"%s\" as elisp? "
  7818. (org-add-props cmd nil
  7819. 'face 'org-warning))))
  7820. (message "%s => %s" cmd
  7821. (if (equal (string-to-char cmd) ?\()
  7822. (eval (read cmd))
  7823. (call-interactively (read cmd))))
  7824. (error "Abort"))))
  7825. ((and (string= type "thisfile")
  7826. (run-hook-with-args-until-success
  7827. 'org-open-link-functions path)))
  7828. ((string= type "thisfile")
  7829. (if in-emacs
  7830. (switch-to-buffer-other-window
  7831. (org-get-buffer-for-internal-link (current-buffer)))
  7832. (org-mark-ring-push))
  7833. (let ((cmd `(org-link-search
  7834. ,path
  7835. ,(cond ((equal in-emacs '(4)) 'occur)
  7836. ((equal in-emacs '(16)) 'org-occur)
  7837. (t nil))
  7838. ,pos)))
  7839. (condition-case nil (eval cmd)
  7840. (error (progn (widen) (eval cmd))))))
  7841. (t
  7842. (browse-url-at-point)))))))
  7843. (move-marker org-open-link-marker nil)
  7844. (run-hook-with-args 'org-follow-link-hook))
  7845. (defun org-offer-links-in-entry (&optional nth zero)
  7846. "Offer links in the current entry and follow the selected link.
  7847. If there is only one link, follow it immediately as well.
  7848. If NTH is an integer, immediately pick the NTH link found.
  7849. If ZERO is a string, check also this string for a link, and if
  7850. there is one, offer it as link number zero."
  7851. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  7852. "\\(" org-angle-link-re "\\)\\|"
  7853. "\\(" org-plain-link-re "\\)"))
  7854. (cnt ?0)
  7855. (in-emacs (if (integerp nth) nil nth))
  7856. have-zero end links link c)
  7857. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  7858. (push (match-string 0 zero) links)
  7859. (setq cnt (1- cnt) have-zero t))
  7860. (save-excursion
  7861. (org-back-to-heading t)
  7862. (setq end (save-excursion (outline-next-heading) (point)))
  7863. (while (re-search-forward re end t)
  7864. (push (match-string 0) links))
  7865. (setq links (org-uniquify (reverse links))))
  7866. (cond
  7867. ((null links)
  7868. (message "No links"))
  7869. ((equal (length links) 1)
  7870. (setq link (list (car links))))
  7871. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  7872. (setq link (nth (if have-zero nth (1- nth)) links)))
  7873. (t ; we have to select a link
  7874. (save-excursion
  7875. (save-window-excursion
  7876. (delete-other-windows)
  7877. (with-output-to-temp-buffer "*Select Link*"
  7878. (mapc (lambda (l)
  7879. (if (not (string-match org-bracket-link-regexp l))
  7880. (princ (format "[%c] %s\n" (incf cnt)
  7881. (org-remove-angle-brackets l)))
  7882. (if (match-end 3)
  7883. (princ (format "[%c] %s (%s)\n" (incf cnt)
  7884. (match-string 3 l) (match-string 1 l)))
  7885. (princ (format "[%c] %s\n" (incf cnt)
  7886. (match-string 1 l))))))
  7887. links))
  7888. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  7889. (message "Select link to open, RET to open all:")
  7890. (setq c (read-char-exclusive))
  7891. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  7892. (when (equal c ?q) (error "Abort"))
  7893. (if (equal c ?\C-m)
  7894. (setq link links)
  7895. (setq nth (- c ?0))
  7896. (if have-zero (setq nth (1+ nth)))
  7897. (unless (and (integerp nth) (>= (length links) nth))
  7898. (error "Invalid link selection"))
  7899. (setq link (list (nth (1- nth) links))))))
  7900. (if link
  7901. (let ((buf (current-buffer)))
  7902. (dolist (l link)
  7903. (org-open-link-from-string l in-emacs buf))
  7904. t)
  7905. nil)))
  7906. ;; Add special file links that specify the way of opening
  7907. (org-add-link-type "file+sys" 'org-open-file-with-system)
  7908. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  7909. (defun org-open-file-with-system (path)
  7910. "Open file at PATH using the system way of opeing it."
  7911. (org-open-file path 'system))
  7912. (defun org-open-file-with-emacs (path)
  7913. "Open file at PATH in emacs."
  7914. (org-open-file path 'emacs))
  7915. (defun org-remove-file-link-modifiers ()
  7916. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  7917. (goto-char (point-min))
  7918. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  7919. (org-if-unprotected
  7920. (replace-match "file:" t t))))
  7921. (eval-after-load "org-exp"
  7922. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  7923. 'org-remove-file-link-modifiers))
  7924. ;;;; Time estimates
  7925. (defun org-get-effort (&optional pom)
  7926. "Get the effort estimate for the current entry."
  7927. (org-entry-get pom org-effort-property))
  7928. ;;; File search
  7929. (defvar org-create-file-search-functions nil
  7930. "List of functions to construct the right search string for a file link.
  7931. These functions are called in turn with point at the location to
  7932. which the link should point.
  7933. A function in the hook should first test if it would like to
  7934. handle this file type, for example by checking the major-mode or
  7935. the file extension. If it decides not to handle this file, it
  7936. should just return nil to give other functions a chance. If it
  7937. does handle the file, it must return the search string to be used
  7938. when following the link. The search string will be part of the
  7939. file link, given after a double colon, and `org-open-at-point'
  7940. will automatically search for it. If special measures must be
  7941. taken to make the search successful, another function should be
  7942. added to the companion hook `org-execute-file-search-functions',
  7943. which see.
  7944. A function in this hook may also use `setq' to set the variable
  7945. `description' to provide a suggestion for the descriptive text to
  7946. be used for this link when it gets inserted into an Org-mode
  7947. buffer with \\[org-insert-link].")
  7948. (defvar org-execute-file-search-functions nil
  7949. "List of functions to execute a file search triggered by a link.
  7950. Functions added to this hook must accept a single argument, the
  7951. search string that was part of the file link, the part after the
  7952. double colon. The function must first check if it would like to
  7953. handle this search, for example by checking the major-mode or the
  7954. file extension. If it decides not to handle this search, it
  7955. should just return nil to give other functions a chance. If it
  7956. does handle the search, it must return a non-nil value to keep
  7957. other functions from trying.
  7958. Each function can access the current prefix argument through the
  7959. variable `current-prefix-argument'. Note that a single prefix is
  7960. used to force opening a link in Emacs, so it may be good to only
  7961. use a numeric or double prefix to guide the search function.
  7962. In case this is needed, a function in this hook can also restore
  7963. the window configuration before `org-open-at-point' was called using:
  7964. (set-window-configuration org-window-config-before-follow-link)")
  7965. (defun org-link-search (s &optional type avoid-pos)
  7966. "Search for a link search option.
  7967. If S is surrounded by forward slashes, it is interpreted as a
  7968. regular expression. In org-mode files, this will create an `org-occur'
  7969. sparse tree. In ordinary files, `occur' will be used to list matches.
  7970. If the current buffer is in `dired-mode', grep will be used to search
  7971. in all files. If AVOID-POS is given, ignore matches near that position."
  7972. (let ((case-fold-search t)
  7973. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7974. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  7975. (append '(("") (" ") ("\t") ("\n"))
  7976. org-emphasis-alist)
  7977. "\\|") "\\)"))
  7978. (pos (point))
  7979. (pre nil) (post nil)
  7980. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  7981. (cond
  7982. ;; First check if there are any special
  7983. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7984. ;; Now try the builtin stuff
  7985. ((and (equal (string-to-char s0) ?#)
  7986. (> (length s0) 1)
  7987. (save-excursion
  7988. (goto-char (point-min))
  7989. (and
  7990. (re-search-forward
  7991. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  7992. (setq type 'dedicated
  7993. pos (match-beginning 0))))
  7994. ;; There is an exact target for this
  7995. (goto-char pos)
  7996. (org-back-to-heading t)))
  7997. ((save-excursion
  7998. (goto-char (point-min))
  7999. (and
  8000. (re-search-forward
  8001. (concat "<<" (regexp-quote s0) ">>") nil t)
  8002. (setq type 'dedicated
  8003. pos (match-beginning 0))))
  8004. ;; There is an exact target for this
  8005. (goto-char pos))
  8006. ((and (string-match "^(\\(.*\\))$" s0)
  8007. (save-excursion
  8008. (goto-char (point-min))
  8009. (and
  8010. (re-search-forward
  8011. (concat "[^[]" (regexp-quote
  8012. (format org-coderef-label-format
  8013. (match-string 1 s0))))
  8014. nil t)
  8015. (setq type 'dedicated
  8016. pos (1+ (match-beginning 0))))))
  8017. ;; There is a coderef target for this
  8018. (goto-char pos))
  8019. ((string-match "^/\\(.*\\)/$" s)
  8020. ;; A regular expression
  8021. (cond
  8022. ((org-mode-p)
  8023. (org-occur (match-string 1 s)))
  8024. ;;((eq major-mode 'dired-mode)
  8025. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8026. (t (org-do-occur (match-string 1 s)))))
  8027. (t
  8028. ;; A normal search strings
  8029. (when (equal (string-to-char s) ?*)
  8030. ;; Anchor on headlines, post may include tags.
  8031. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8032. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  8033. s (substring s 1)))
  8034. (remove-text-properties
  8035. 0 (length s)
  8036. '(face nil mouse-face nil keymap nil fontified nil) s)
  8037. ;; Make a series of regular expressions to find a match
  8038. (setq words (org-split-string s "[ \n\r\t]+")
  8039. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8040. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8041. "\\)" markers)
  8042. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8043. re2a (concat "[ \t\r\n]" re2a_)
  8044. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8045. re4 (concat "[^a-zA-Z_]" re4_)
  8046. re1 (concat pre re2 post)
  8047. re3 (concat pre (if pre re4_ re4) post)
  8048. re5 (concat pre ".*" re4)
  8049. re2 (concat pre re2)
  8050. re2a (concat pre (if pre re2a_ re2a))
  8051. re4 (concat pre (if pre re4_ re4))
  8052. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8053. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8054. re5 "\\)"
  8055. ))
  8056. (cond
  8057. ((eq type 'org-occur) (org-occur reall))
  8058. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8059. (t (goto-char (point-min))
  8060. (setq type 'fuzzy)
  8061. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8062. (org-search-not-self 1 re1 nil t)
  8063. (org-search-not-self 1 re2 nil t)
  8064. (org-search-not-self 1 re2a nil t)
  8065. (org-search-not-self 1 re3 nil t)
  8066. (org-search-not-self 1 re4 nil t)
  8067. (org-search-not-self 1 re5 nil t)
  8068. )
  8069. (goto-char (match-beginning 1))
  8070. (goto-char pos)
  8071. (error "No match")))))
  8072. (t
  8073. ;; Normal string-search
  8074. (goto-char (point-min))
  8075. (if (search-forward s nil t)
  8076. (goto-char (match-beginning 0))
  8077. (error "No match"))))
  8078. (and (org-mode-p) (org-show-context 'link-search))
  8079. type))
  8080. (defun org-search-not-self (group &rest args)
  8081. "Execute `re-search-forward', but only accept matches that do not
  8082. enclose the position of `org-open-link-marker'."
  8083. (let ((m org-open-link-marker))
  8084. (catch 'exit
  8085. (while (apply 're-search-forward args)
  8086. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8087. (goto-char (match-end group))
  8088. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8089. (> (match-beginning 0) (marker-position m))
  8090. (< (match-end 0) (marker-position m)))
  8091. (save-match-data
  8092. (or (not (org-in-regexp
  8093. org-bracket-link-analytic-regexp 1))
  8094. (not (match-end 4)) ; no description
  8095. (and (<= (match-beginning 4) (point))
  8096. (>= (match-end 4) (point))))))
  8097. (throw 'exit (point))))))))
  8098. (defun org-get-buffer-for-internal-link (buffer)
  8099. "Return a buffer to be used for displaying the link target of internal links."
  8100. (cond
  8101. ((not org-display-internal-link-with-indirect-buffer)
  8102. buffer)
  8103. ((string-match "(Clone)$" (buffer-name buffer))
  8104. (message "Buffer is already a clone, not making another one")
  8105. ;; we also do not modify visibility in this case
  8106. buffer)
  8107. (t ; make a new indirect buffer for displaying the link
  8108. (let* ((bn (buffer-name buffer))
  8109. (ibn (concat bn "(Clone)"))
  8110. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8111. (with-current-buffer ib (org-overview))
  8112. ib))))
  8113. (defun org-do-occur (regexp &optional cleanup)
  8114. "Call the Emacs command `occur'.
  8115. If CLEANUP is non-nil, remove the printout of the regular expression
  8116. in the *Occur* buffer. This is useful if the regex is long and not useful
  8117. to read."
  8118. (occur regexp)
  8119. (when cleanup
  8120. (let ((cwin (selected-window)) win beg end)
  8121. (when (setq win (get-buffer-window "*Occur*"))
  8122. (select-window win))
  8123. (goto-char (point-min))
  8124. (when (re-search-forward "match[a-z]+" nil t)
  8125. (setq beg (match-end 0))
  8126. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8127. (setq end (1- (match-beginning 0)))))
  8128. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8129. (goto-char (point-min))
  8130. (select-window cwin))))
  8131. ;;; The mark ring for links jumps
  8132. (defvar org-mark-ring nil
  8133. "Mark ring for positions before jumps in Org-mode.")
  8134. (defvar org-mark-ring-last-goto nil
  8135. "Last position in the mark ring used to go back.")
  8136. ;; Fill and close the ring
  8137. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8138. (loop for i from 1 to org-mark-ring-length do
  8139. (push (make-marker) org-mark-ring))
  8140. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8141. org-mark-ring)
  8142. (defun org-mark-ring-push (&optional pos buffer)
  8143. "Put the current position or POS into the mark ring and rotate it."
  8144. (interactive)
  8145. (setq pos (or pos (point)))
  8146. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8147. (move-marker (car org-mark-ring)
  8148. (or pos (point))
  8149. (or buffer (current-buffer)))
  8150. (message "%s"
  8151. (substitute-command-keys
  8152. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8153. (defun org-mark-ring-goto (&optional n)
  8154. "Jump to the previous position in the mark ring.
  8155. With prefix arg N, jump back that many stored positions. When
  8156. called several times in succession, walk through the entire ring.
  8157. Org-mode commands jumping to a different position in the current file,
  8158. or to another Org-mode file, automatically push the old position
  8159. onto the ring."
  8160. (interactive "p")
  8161. (let (p m)
  8162. (if (eq last-command this-command)
  8163. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8164. (setq p org-mark-ring))
  8165. (setq org-mark-ring-last-goto p)
  8166. (setq m (car p))
  8167. (switch-to-buffer (marker-buffer m))
  8168. (goto-char m)
  8169. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8170. (defun org-remove-angle-brackets (s)
  8171. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8172. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8173. s)
  8174. (defun org-add-angle-brackets (s)
  8175. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8176. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8177. s)
  8178. (defun org-remove-double-quotes (s)
  8179. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8180. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8181. s)
  8182. ;;; Following specific links
  8183. (defun org-follow-timestamp-link ()
  8184. (cond
  8185. ((org-at-date-range-p t)
  8186. (let ((org-agenda-start-on-weekday)
  8187. (t1 (match-string 1))
  8188. (t2 (match-string 2)))
  8189. (setq t1 (time-to-days (org-time-string-to-time t1))
  8190. t2 (time-to-days (org-time-string-to-time t2)))
  8191. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8192. ((org-at-timestamp-p t)
  8193. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8194. (substring (match-string 1) 0 10)))
  8195. 1))
  8196. (t (error "This should not happen"))))
  8197. ;;; Following file links
  8198. (defvar org-wait nil)
  8199. (defun org-open-file (path &optional in-emacs line search)
  8200. "Open the file at PATH.
  8201. First, this expands any special file name abbreviations. Then the
  8202. configuration variable `org-file-apps' is checked if it contains an
  8203. entry for this file type, and if yes, the corresponding command is launched.
  8204. If no application is found, Emacs simply visits the file.
  8205. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8206. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  8207. and to use an external application to visit the file.
  8208. Optional LINE specifies a line to go to, optional SEARCH a string to
  8209. search for. If LINE or SEARCH is given, the file will always be
  8210. opened in Emacs.
  8211. If the file does not exist, an error is thrown."
  8212. (setq in-emacs (or in-emacs line search))
  8213. (let* ((file (if (equal path "")
  8214. buffer-file-name
  8215. (substitute-in-file-name (expand-file-name path))))
  8216. (apps (append org-file-apps (org-default-apps)))
  8217. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8218. (dirp (if remp nil (file-directory-p file)))
  8219. (file (if (and dirp org-open-directory-means-index-dot-org)
  8220. (concat (file-name-as-directory file) "index.org")
  8221. file))
  8222. (a-m-a-p (assq 'auto-mode apps))
  8223. (dfile (downcase file))
  8224. (old-buffer (current-buffer))
  8225. (old-pos (point))
  8226. (old-mode major-mode)
  8227. ext cmd)
  8228. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8229. (setq ext (match-string 1 dfile))
  8230. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8231. (setq ext (match-string 1 dfile))))
  8232. (cond
  8233. ((member in-emacs '((16) system))
  8234. (setq cmd (cdr (assoc 'system apps))))
  8235. (in-emacs (setq cmd 'emacs))
  8236. (t
  8237. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8238. (and dirp (cdr (assoc 'directory apps)))
  8239. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8240. 'string-match)
  8241. (cdr (assoc ext apps))
  8242. (cdr (assoc t apps))))))
  8243. (when (eq cmd 'system)
  8244. (setq cmd (cdr (assoc 'system apps))))
  8245. (when (eq cmd 'default)
  8246. (setq cmd (cdr (assoc t apps))))
  8247. (when (eq cmd 'mailcap)
  8248. (require 'mailcap)
  8249. (mailcap-parse-mailcaps)
  8250. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8251. (command (mailcap-mime-info mime-type)))
  8252. (if (stringp command)
  8253. (setq cmd command)
  8254. (setq cmd 'emacs))))
  8255. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8256. (not (file-exists-p file))
  8257. (not org-open-non-existing-files))
  8258. (error "No such file: %s" file))
  8259. (cond
  8260. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8261. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8262. (while (string-match "['\"]%s['\"]" cmd)
  8263. (setq cmd (replace-match "%s" t t cmd)))
  8264. (while (string-match "%s" cmd)
  8265. (setq cmd (replace-match
  8266. (save-match-data
  8267. (shell-quote-argument
  8268. (convert-standard-filename file)))
  8269. t t cmd)))
  8270. (save-window-excursion
  8271. (start-process-shell-command cmd nil cmd)
  8272. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8273. ))
  8274. ((or (stringp cmd)
  8275. (eq cmd 'emacs))
  8276. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8277. (widen)
  8278. (if line (org-goto-line line)
  8279. (if search (org-link-search search))))
  8280. ((consp cmd)
  8281. (let ((file (convert-standard-filename file)))
  8282. (eval cmd)))
  8283. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8284. (and (org-mode-p) (eq old-mode 'org-mode)
  8285. (or (not (equal old-buffer (current-buffer)))
  8286. (not (equal old-pos (point))))
  8287. (org-mark-ring-push old-pos old-buffer))))
  8288. (defun org-default-apps ()
  8289. "Return the default applications for this operating system."
  8290. (cond
  8291. ((eq system-type 'darwin)
  8292. org-file-apps-defaults-macosx)
  8293. ((eq system-type 'windows-nt)
  8294. org-file-apps-defaults-windowsnt)
  8295. (t org-file-apps-defaults-gnu)))
  8296. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8297. "Convert extensions to regular expressions in the cars of LIST.
  8298. Also, weed out any non-string entries, because the return value is used
  8299. only for regexp matching.
  8300. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8301. point to the symbol `emacs', indicating that the file should
  8302. be opened in Emacs."
  8303. (append
  8304. (delq nil
  8305. (mapcar (lambda (x)
  8306. (if (not (stringp (car x)))
  8307. nil
  8308. (if (string-match "\\W" (car x))
  8309. x
  8310. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  8311. list))
  8312. (if add-auto-mode
  8313. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  8314. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  8315. (defun org-file-remote-p (file)
  8316. "Test whether FILE specifies a location on a remote system.
  8317. Return non-nil if the location is indeed remote.
  8318. For example, the filename \"/user@host:/foo\" specifies a location
  8319. on the system \"/user@host:\"."
  8320. (cond ((fboundp 'file-remote-p)
  8321. (file-remote-p file))
  8322. ((fboundp 'tramp-handle-file-remote-p)
  8323. (tramp-handle-file-remote-p file))
  8324. ((and (boundp 'ange-ftp-name-format)
  8325. (string-match (car ange-ftp-name-format) file))
  8326. t)
  8327. (t nil)))
  8328. ;;;; Refiling
  8329. (defun org-get-org-file ()
  8330. "Read a filename, with default directory `org-directory'."
  8331. (let ((default (or org-default-notes-file remember-data-file)))
  8332. (read-file-name (format "File name [%s]: " default)
  8333. (file-name-as-directory org-directory)
  8334. default)))
  8335. (defun org-notes-order-reversed-p ()
  8336. "Check if the current file should receive notes in reversed order."
  8337. (cond
  8338. ((not org-reverse-note-order) nil)
  8339. ((eq t org-reverse-note-order) t)
  8340. ((not (listp org-reverse-note-order)) nil)
  8341. (t (catch 'exit
  8342. (let ((all org-reverse-note-order)
  8343. entry)
  8344. (while (setq entry (pop all))
  8345. (if (string-match (car entry) buffer-file-name)
  8346. (throw 'exit (cdr entry))))
  8347. nil)))))
  8348. (defvar org-refile-target-table nil
  8349. "The list of refile targets, created by `org-refile'.")
  8350. (defvar org-agenda-new-buffers nil
  8351. "Buffers created to visit agenda files.")
  8352. (defun org-get-refile-targets (&optional default-buffer)
  8353. "Produce a table with refile targets."
  8354. (let ((case-fold-search nil)
  8355. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  8356. (entries (or org-refile-targets '((nil . (:level . 1)))))
  8357. targets txt re files f desc descre fast-path-p level pos0)
  8358. (message "Getting targets...")
  8359. (with-current-buffer (or default-buffer (current-buffer))
  8360. (while (setq entry (pop entries))
  8361. (setq files (car entry) desc (cdr entry))
  8362. (setq fast-path-p nil)
  8363. (cond
  8364. ((null files) (setq files (list (current-buffer))))
  8365. ((eq files 'org-agenda-files)
  8366. (setq files (org-agenda-files 'unrestricted)))
  8367. ((and (symbolp files) (fboundp files))
  8368. (setq files (funcall files)))
  8369. ((and (symbolp files) (boundp files))
  8370. (setq files (symbol-value files))))
  8371. (if (stringp files) (setq files (list files)))
  8372. (cond
  8373. ((eq (car desc) :tag)
  8374. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  8375. ((eq (car desc) :todo)
  8376. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  8377. ((eq (car desc) :regexp)
  8378. (setq descre (cdr desc)))
  8379. ((eq (car desc) :level)
  8380. (setq descre (concat "^\\*\\{" (number-to-string
  8381. (if org-odd-levels-only
  8382. (1- (* 2 (cdr desc)))
  8383. (cdr desc)))
  8384. "\\}[ \t]")))
  8385. ((eq (car desc) :maxlevel)
  8386. (setq fast-path-p t)
  8387. (setq descre (concat "^\\*\\{1," (number-to-string
  8388. (if org-odd-levels-only
  8389. (1- (* 2 (cdr desc)))
  8390. (cdr desc)))
  8391. "\\}[ \t]")))
  8392. (t (error "Bad refiling target description %s" desc)))
  8393. (while (setq f (pop files))
  8394. (with-current-buffer
  8395. (if (bufferp f) f (org-get-agenda-file-buffer f))
  8396. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  8397. (setq f (and f (expand-file-name f)))
  8398. (if (eq org-refile-use-outline-path 'file)
  8399. (push (list (file-name-nondirectory f) f nil nil) targets))
  8400. (save-excursion
  8401. (save-restriction
  8402. (widen)
  8403. (goto-char (point-min))
  8404. (while (re-search-forward descre nil t)
  8405. (goto-char (setq pos0 (point-at-bol)))
  8406. (catch 'next
  8407. (when org-refile-target-verify-function
  8408. (save-match-data
  8409. (or (funcall org-refile-target-verify-function)
  8410. (throw 'next t))))
  8411. (when (looking-at org-complex-heading-regexp)
  8412. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  8413. txt (org-link-display-format (match-string 4))
  8414. re (concat "^" (regexp-quote
  8415. (buffer-substring (match-beginning 1)
  8416. (match-end 4)))))
  8417. (if (match-end 5) (setq re (concat re "[ \t]+"
  8418. (regexp-quote
  8419. (match-string 5)))))
  8420. (setq re (concat re "[ \t]*$"))
  8421. (when org-refile-use-outline-path
  8422. (setq txt (mapconcat 'org-protect-slash
  8423. (append
  8424. (if (eq org-refile-use-outline-path 'file)
  8425. (list (file-name-nondirectory
  8426. (buffer-file-name (buffer-base-buffer))))
  8427. (if (eq org-refile-use-outline-path 'full-file-path)
  8428. (list (buffer-file-name (buffer-base-buffer)))))
  8429. (org-get-outline-path fast-path-p level txt)
  8430. (list txt))
  8431. "/")))
  8432. (push (list txt f re (point)) targets)))
  8433. (when (= (point) pos0)
  8434. ;; verification function has not moved point
  8435. (goto-char (point-at-eol))))))))))
  8436. (message "Getting targets...done")
  8437. (nreverse targets)))
  8438. (defun org-protect-slash (s)
  8439. (while (string-match "/" s)
  8440. (setq s (replace-match "\\" t t s)))
  8441. s)
  8442. (defvar org-olpa (make-vector 20 nil))
  8443. (defun org-get-outline-path (&optional fastp level heading)
  8444. "Return the outline path to the current entry, as a list.
  8445. The parameters FASTP, LEVEL, and HEADING are for use be a scanner
  8446. routine which makes outline path derivations for an entire file,
  8447. avoiding backtracing."
  8448. (if fastp
  8449. (progn
  8450. (if (> level 19)
  8451. (error "Outline path failure, more than 19 levels."))
  8452. (loop for i from level upto 19 do
  8453. (aset org-olpa i nil))
  8454. (prog1
  8455. (delq nil (append org-olpa nil))
  8456. (aset org-olpa level heading)))
  8457. (let (rtn case-fold-search)
  8458. (save-excursion
  8459. (save-restriction
  8460. (widen)
  8461. (while (org-up-heading-safe)
  8462. (when (looking-at org-complex-heading-regexp)
  8463. (push (org-match-string-no-properties 4) rtn)))
  8464. rtn)))))
  8465. (defun org-format-outline-path (path &optional width prefix)
  8466. "Format the outlie path PATH for display.
  8467. Width is the maximum number of characters that is available.
  8468. Prefix is a prefix to be included in the returned string,
  8469. such as the file name."
  8470. (setq width (or width 79))
  8471. (if prefix (setq width (- width (length prefix))))
  8472. (if (not path)
  8473. (or prefix "")
  8474. (let* ((nsteps (length path))
  8475. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  8476. (maxwidth (if (<= total-width width)
  8477. 10000 ;; everything fits
  8478. ;; we need to shorten the level headings
  8479. (/ (- width nsteps) nsteps)))
  8480. (org-odd-levels-only nil)
  8481. (n 0)
  8482. (total (1+ (length prefix))))
  8483. (setq maxwidth (max maxwidth 10))
  8484. (concat prefix
  8485. (mapconcat
  8486. (lambda (h)
  8487. (setq n (1+ n))
  8488. (if (and (= n nsteps) (< maxwidth 10000))
  8489. (setq maxwidth (- total-width total)))
  8490. (if (< (length h) maxwidth)
  8491. (progn (setq total (+ total (length h) 1)) h)
  8492. (setq h (substring h 0 (- maxwidth 2))
  8493. total (+ total maxwidth 1))
  8494. (if (string-match "[ \t]+\\'" h)
  8495. (setq h (substring h 0 (match-beginning 0))))
  8496. (setq h (concat h "..")))
  8497. (org-add-props h nil 'face
  8498. (nth (% (1- n) org-n-level-faces)
  8499. org-level-faces))
  8500. h)
  8501. path "/")))))
  8502. (defun org-display-outline-path (&optional file current)
  8503. "Display the current outline path in the echo area."
  8504. (interactive "P")
  8505. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  8506. (case-fold-search nil)
  8507. (path (and (org-mode-p) (org-get-outline-path))))
  8508. (if current (setq path (append path
  8509. (save-excursion
  8510. (org-back-to-heading t)
  8511. (if (looking-at org-complex-heading-regexp)
  8512. (list (match-string 4)))))))
  8513. (message "%s"
  8514. (org-format-outline-path
  8515. path
  8516. (1- (frame-width))
  8517. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  8518. (defvar org-refile-history nil
  8519. "History for refiling operations.")
  8520. (defvar org-after-refile-insert-hook nil
  8521. "Hook run after `org-refile' has inserted its stuff at the new location.
  8522. Note that this is still *before* the stuff will be removed from
  8523. the *old* location.")
  8524. (defun org-refile (&optional goto default-buffer rfloc)
  8525. "Move the entry at point to another heading.
  8526. The list of target headings is compiled using the information in
  8527. `org-refile-targets', which see. This list is created before each use
  8528. and will therefore always be up-to-date.
  8529. At the target location, the entry is filed as a subitem of the target heading.
  8530. Depending on `org-reverse-note-order', the new subitem will either be the
  8531. first or the last subitem.
  8532. If there is an active region, all entries in that region will be moved.
  8533. However, the region must fulfil the requirement that the first heading
  8534. is the first one sets the top-level of the moved text - at most siblings
  8535. below it are allowed.
  8536. With prefix arg GOTO, the command will only visit the target location,
  8537. not actually move anything.
  8538. With a double prefix `C-u C-u', go to the location where the last refiling
  8539. operation has put the subtree.
  8540. With a prefix argument of `2', refile to the running clock.
  8541. RFLOC can be a refile location obtained in a different way.
  8542. See also `org-refile-use-outline-path' and `org-completion-use-ido'"
  8543. (interactive "P")
  8544. (let* ((cbuf (current-buffer))
  8545. (regionp (org-region-active-p))
  8546. (region-start (and regionp (region-beginning)))
  8547. (region-end (and regionp (region-end)))
  8548. (region-length (and regionp (- region-end region-start)))
  8549. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  8550. pos it nbuf file re level reversed)
  8551. (setq last-command nil)
  8552. (when regionp
  8553. (goto-char region-start)
  8554. (or (bolp) (goto-char (point-at-bol)))
  8555. (setq region-start (point))
  8556. (unless (org-kill-is-subtree-p
  8557. (buffer-substring region-start region-end))
  8558. (error "The region is not a (sequence of) subtree(s)")))
  8559. (if (equal goto '(16))
  8560. (org-refile-goto-last-stored)
  8561. (when (or
  8562. (and (equal goto 2)
  8563. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  8564. (prog1
  8565. (setq it (list (or org-clock-heading "running clock")
  8566. (buffer-file-name
  8567. (marker-buffer org-clock-hd-marker))
  8568. ""
  8569. (marker-position org-clock-hd-marker)))
  8570. (setq goto nil)))
  8571. (setq it (or rfloc
  8572. (save-excursion
  8573. (org-refile-get-location
  8574. (if goto "Goto: " "Refile to: ") default-buffer
  8575. org-refile-allow-creating-parent-nodes)))))
  8576. (setq file (nth 1 it)
  8577. re (nth 2 it)
  8578. pos (nth 3 it))
  8579. (if (and (not goto)
  8580. pos
  8581. (equal (buffer-file-name) file)
  8582. (if regionp
  8583. (and (>= pos region-start)
  8584. (<= pos region-end))
  8585. (and (>= pos (point))
  8586. (< pos (save-excursion
  8587. (org-end-of-subtree t t))))))
  8588. (error "Cannot refile to position inside the tree or region"))
  8589. (setq nbuf (or (find-buffer-visiting file)
  8590. (find-file-noselect file)))
  8591. (if goto
  8592. (progn
  8593. (switch-to-buffer nbuf)
  8594. (goto-char pos)
  8595. (org-show-context 'org-goto))
  8596. (if regionp
  8597. (progn
  8598. (org-kill-new (buffer-substring region-start region-end))
  8599. (org-save-markers-in-region region-start region-end))
  8600. (org-copy-subtree 1 nil t))
  8601. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  8602. (find-file-noselect file)))
  8603. (setq reversed (org-notes-order-reversed-p))
  8604. (save-excursion
  8605. (save-restriction
  8606. (widen)
  8607. (if pos
  8608. (progn
  8609. (goto-char pos)
  8610. (looking-at outline-regexp)
  8611. (setq level (org-get-valid-level (funcall outline-level) 1))
  8612. (goto-char
  8613. (if reversed
  8614. (or (outline-next-heading) (point-max))
  8615. (or (save-excursion (org-get-next-sibling))
  8616. (org-end-of-subtree t t)
  8617. (point-max)))))
  8618. (setq level 1)
  8619. (if (not reversed)
  8620. (goto-char (point-max))
  8621. (goto-char (point-min))
  8622. (or (outline-next-heading) (goto-char (point-max)))))
  8623. (if (not (bolp)) (newline))
  8624. (org-paste-subtree level)
  8625. (when org-log-refile
  8626. (org-add-log-setup 'refile nil nil 'findpos
  8627. org-log-refile)
  8628. (unless (eq org-log-refile 'note)
  8629. (save-excursion (org-add-log-note))))
  8630. (and org-auto-align-tags (org-set-tags nil t))
  8631. (bookmark-set "org-refile-last-stored")
  8632. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8633. (run-hooks 'org-after-refile-insert-hook))))
  8634. (if regionp
  8635. (delete-region (point) (+ (point) region-length))
  8636. (org-cut-subtree))
  8637. (when (featurep 'org-inlinetask)
  8638. (org-inlinetask-remove-END-maybe))
  8639. (setq org-markers-to-move nil)
  8640. (message "Refiled to \"%s\"" (car it))))))
  8641. (org-reveal))
  8642. (defun org-refile-goto-last-stored ()
  8643. "Go to the location where the last refile was stored."
  8644. (interactive)
  8645. (bookmark-jump "org-refile-last-stored")
  8646. (message "This is the location of the last refile"))
  8647. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  8648. "Prompt the user for a refile location, using PROMPT."
  8649. (let ((org-refile-targets org-refile-targets)
  8650. (org-refile-use-outline-path org-refile-use-outline-path))
  8651. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  8652. (unless org-refile-target-table
  8653. (error "No refile targets"))
  8654. (let* ((cbuf (current-buffer))
  8655. (partial-completion-mode nil)
  8656. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  8657. (cfunc (if (and org-refile-use-outline-path
  8658. org-outline-path-complete-in-steps)
  8659. 'org-olpath-completing-read
  8660. 'org-icompleting-read))
  8661. (extra (if org-refile-use-outline-path "/" ""))
  8662. (filename (and cfn (expand-file-name cfn)))
  8663. (tbl (mapcar
  8664. (lambda (x)
  8665. (if (and (not (member org-refile-use-outline-path
  8666. '(file full-file-path)))
  8667. (not (equal filename (nth 1 x))))
  8668. (cons (concat (car x) extra " ("
  8669. (file-name-nondirectory (nth 1 x)) ")")
  8670. (cdr x))
  8671. (cons (concat (car x) extra) (cdr x))))
  8672. org-refile-target-table))
  8673. (completion-ignore-case t)
  8674. pa answ parent-target child parent old-hist)
  8675. (setq old-hist org-refile-history)
  8676. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  8677. nil 'org-refile-history))
  8678. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  8679. (if pa
  8680. (progn
  8681. (when (or (not org-refile-history)
  8682. (not (eq old-hist org-refile-history))
  8683. (not (equal (car pa) (car org-refile-history))))
  8684. (setq org-refile-history
  8685. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  8686. org-refile-history
  8687. (cdr org-refile-history))))
  8688. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  8689. (pop org-refile-history)))
  8690. pa)
  8691. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  8692. (progn
  8693. (setq parent (match-string 1 answ)
  8694. child (match-string 2 answ))
  8695. (setq parent-target (or (assoc parent tbl)
  8696. (assoc (concat parent "/") tbl)))
  8697. (when (and parent-target
  8698. (or (eq new-nodes t)
  8699. (and (eq new-nodes 'confirm)
  8700. (y-or-n-p (format "Create new node \"%s\"? "
  8701. child)))))
  8702. (org-refile-new-child parent-target child)))
  8703. (error "Invalid target location")))))
  8704. (defun org-refile-new-child (parent-target child)
  8705. "Use refile target PARENT-TARGET to add new CHILD below it."
  8706. (unless parent-target
  8707. (error "Cannot find parent for new node"))
  8708. (let ((file (nth 1 parent-target))
  8709. (pos (nth 3 parent-target))
  8710. level)
  8711. (with-current-buffer (or (find-buffer-visiting file)
  8712. (find-file-noselect file))
  8713. (save-excursion
  8714. (save-restriction
  8715. (widen)
  8716. (if pos
  8717. (goto-char pos)
  8718. (goto-char (point-max))
  8719. (if (not (bolp)) (newline)))
  8720. (when (looking-at outline-regexp)
  8721. (setq level (funcall outline-level))
  8722. (org-end-of-subtree t t))
  8723. (org-back-over-empty-lines)
  8724. (insert "\n" (make-string
  8725. (if pos (org-get-valid-level level 1) 1) ?*)
  8726. " " child "\n")
  8727. (beginning-of-line 0)
  8728. (list (concat (car parent-target) "/" child) file "" (point)))))))
  8729. (defun org-olpath-completing-read (prompt collection &rest args)
  8730. "Read an outline path like a file name."
  8731. (let ((thetable collection)
  8732. (org-completion-use-ido nil) ; does not work with ido.
  8733. (org-completion-use-iswitchb nil)) ; or iswitchb
  8734. (apply
  8735. 'org-icompleting-read prompt
  8736. (lambda (string predicate &optional flag)
  8737. (let (rtn r f (l (length string)))
  8738. (cond
  8739. ((eq flag nil)
  8740. ;; try completion
  8741. (try-completion string thetable))
  8742. ((eq flag t)
  8743. ;; all-completions
  8744. (setq rtn (all-completions string thetable predicate))
  8745. (mapcar
  8746. (lambda (x)
  8747. (setq r (substring x l))
  8748. (if (string-match " ([^)]*)$" x)
  8749. (setq f (match-string 0 x))
  8750. (setq f ""))
  8751. (if (string-match "/" r)
  8752. (concat string (substring r 0 (match-end 0)) f)
  8753. x))
  8754. rtn))
  8755. ((eq flag 'lambda)
  8756. ;; exact match?
  8757. (assoc string thetable)))
  8758. ))
  8759. args)))
  8760. ;;;; Dynamic blocks
  8761. (defun org-find-dblock (name)
  8762. "Find the first dynamic block with name NAME in the buffer.
  8763. If not found, stay at current position and return nil."
  8764. (let (pos)
  8765. (save-excursion
  8766. (goto-char (point-min))
  8767. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  8768. nil t)
  8769. (match-beginning 0))))
  8770. (if pos (goto-char pos))
  8771. pos))
  8772. (defconst org-dblock-start-re
  8773. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  8774. "Matches the start line of a dynamic block, with parameters.")
  8775. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  8776. "Matches the end of a dynamic block.")
  8777. (defun org-create-dblock (plist)
  8778. "Create a dynamic block section, with parameters taken from PLIST.
  8779. PLIST must contain a :name entry which is used as name of the block."
  8780. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  8781. (end-of-line 1)
  8782. (newline))
  8783. (let ((col (current-column))
  8784. (name (plist-get plist :name)))
  8785. (insert "#+BEGIN: " name)
  8786. (while plist
  8787. (if (eq (car plist) :name)
  8788. (setq plist (cddr plist))
  8789. (insert " " (prin1-to-string (pop plist)))))
  8790. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  8791. (beginning-of-line -2)))
  8792. (defun org-prepare-dblock ()
  8793. "Prepare dynamic block for refresh.
  8794. This empties the block, puts the cursor at the insert position and returns
  8795. the property list including an extra property :name with the block name."
  8796. (unless (looking-at org-dblock-start-re)
  8797. (error "Not at a dynamic block"))
  8798. (let* ((begdel (1+ (match-end 0)))
  8799. (name (org-no-properties (match-string 1)))
  8800. (params (append (list :name name)
  8801. (read (concat "(" (match-string 3) ")")))))
  8802. (save-excursion
  8803. (beginning-of-line 1)
  8804. (skip-chars-forward " \t")
  8805. (setq params (plist-put params :indentation-column (current-column))))
  8806. (unless (re-search-forward org-dblock-end-re nil t)
  8807. (error "Dynamic block not terminated"))
  8808. (setq params
  8809. (append params
  8810. (list :content (buffer-substring
  8811. begdel (match-beginning 0)))))
  8812. (delete-region begdel (match-beginning 0))
  8813. (goto-char begdel)
  8814. (open-line 1)
  8815. params))
  8816. (defun org-map-dblocks (&optional command)
  8817. "Apply COMMAND to all dynamic blocks in the current buffer.
  8818. If COMMAND is not given, use `org-update-dblock'."
  8819. (let ((cmd (or command 'org-update-dblock)))
  8820. (save-excursion
  8821. (goto-char (point-min))
  8822. (while (re-search-forward org-dblock-start-re nil t)
  8823. (goto-char (match-beginning 0))
  8824. (save-excursion
  8825. (condition-case nil
  8826. (funcall cmd)
  8827. (error (message "Error during update of dynamic block"))))
  8828. (unless (re-search-forward org-dblock-end-re nil t)
  8829. (error "Dynamic block not terminated"))))))
  8830. (defun org-dblock-update (&optional arg)
  8831. "User command for updating dynamic blocks.
  8832. Update the dynamic block at point. With prefix ARG, update all dynamic
  8833. blocks in the buffer."
  8834. (interactive "P")
  8835. (if arg
  8836. (org-update-all-dblocks)
  8837. (or (looking-at org-dblock-start-re)
  8838. (org-beginning-of-dblock))
  8839. (org-update-dblock)))
  8840. (defun org-update-dblock ()
  8841. "Update the dynamic block at point
  8842. This means to empty the block, parse for parameters and then call
  8843. the correct writing function."
  8844. (save-window-excursion
  8845. (let* ((pos (point))
  8846. (line (org-current-line))
  8847. (params (org-prepare-dblock))
  8848. (name (plist-get params :name))
  8849. (indent (plist-get params :indentation-column))
  8850. (cmd (intern (concat "org-dblock-write:" name))))
  8851. (message "Updating dynamic block `%s' at line %d..." name line)
  8852. (funcall cmd params)
  8853. (message "Updating dynamic block `%s' at line %d...done" name line)
  8854. (goto-char pos)
  8855. (when (and indent (> indent 0))
  8856. (setq indent (make-string indent ?\ ))
  8857. (save-excursion
  8858. (org-beginning-of-dblock)
  8859. (forward-line 1)
  8860. (while (not (looking-at org-dblock-end-re))
  8861. (insert indent)
  8862. (beginning-of-line 2))
  8863. (when (looking-at org-dblock-end-re)
  8864. (and (looking-at "[ \t]+")
  8865. (replace-match ""))
  8866. (insert indent)))))))
  8867. (defun org-beginning-of-dblock ()
  8868. "Find the beginning of the dynamic block at point.
  8869. Error if there is no such block at point."
  8870. (let ((pos (point))
  8871. beg)
  8872. (end-of-line 1)
  8873. (if (and (re-search-backward org-dblock-start-re nil t)
  8874. (setq beg (match-beginning 0))
  8875. (re-search-forward org-dblock-end-re nil t)
  8876. (> (match-end 0) pos))
  8877. (goto-char beg)
  8878. (goto-char pos)
  8879. (error "Not in a dynamic block"))))
  8880. (defun org-update-all-dblocks ()
  8881. "Update all dynamic blocks in the buffer.
  8882. This function can be used in a hook."
  8883. (when (org-mode-p)
  8884. (org-map-dblocks 'org-update-dblock)))
  8885. ;;;; Completion
  8886. (defconst org-additional-option-like-keywords
  8887. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML"
  8888. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook"
  8889. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  8890. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX"
  8891. "BEGIN:" "END:"
  8892. "ORGTBL" "TBLFM:" "TBLNAME:"
  8893. "BEGIN_EXAMPLE" "END_EXAMPLE"
  8894. "BEGIN_QUOTE" "END_QUOTE"
  8895. "BEGIN_VERSE" "END_VERSE"
  8896. "BEGIN_CENTER" "END_CENTER"
  8897. "BEGIN_SRC" "END_SRC"
  8898. "CATEGORY" "COLUMNS"
  8899. "CAPTION" "LABEL"
  8900. "SETUPFILE"
  8901. "BIND"
  8902. "MACRO"))
  8903. (defcustom org-structure-template-alist
  8904. '(
  8905. ("s" "#+begin_src ?\n\n#+end_src"
  8906. "<src lang=\"?\">\n\n</src>")
  8907. ("e" "#+begin_example\n?\n#+end_example"
  8908. "<example>\n?\n</example>")
  8909. ("q" "#+begin_quote\n?\n#+end_quote"
  8910. "<quote>\n?\n</quote>")
  8911. ("v" "#+begin_verse\n?\n#+end_verse"
  8912. "<verse>\n?\n/verse>")
  8913. ("c" "#+begin_center\n?\n#+end_center"
  8914. "<center>\n?\n/center>")
  8915. ("l" "#+begin_latex\n?\n#+end_latex"
  8916. "<literal style=\"latex\">\n?\n</literal>")
  8917. ("L" "#+latex: "
  8918. "<literal style=\"latex\">?</literal>")
  8919. ("h" "#+begin_html\n?\n#+end_html"
  8920. "<literal style=\"html\">\n?\n</literal>")
  8921. ("H" "#+html: "
  8922. "<literal style=\"html\">?</literal>")
  8923. ("a" "#+begin_ascii\n?\n#+end_ascii")
  8924. ("A" "#+ascii: ")
  8925. ("i" "#+include %file ?"
  8926. "<include file=%file markup=\"?\">")
  8927. )
  8928. "Structure completion elements.
  8929. This is a list of abbreviation keys and values. The value gets inserted
  8930. it you type @samp{.} followed by the key and then the completion key,
  8931. usually `M-TAB'. %file will be replaced by a file name after prompting
  8932. for the file using completion.
  8933. There are two templates for each key, the first uses the original Org syntax,
  8934. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  8935. the default when the /org-mtags.el/ module has been loaded. See also the
  8936. variable `org-mtags-prefer-muse-templates'.
  8937. This is an experimental feature, it is undecided if it is going to stay in."
  8938. :group 'org-completion
  8939. :type '(repeat
  8940. (string :tag "Key")
  8941. (string :tag "Template")
  8942. (string :tag "Muse Template")))
  8943. (defun org-try-structure-completion ()
  8944. "Try to complete a structure template before point.
  8945. This looks for strings like \"<e\" on an otherwise empty line and
  8946. expands them."
  8947. (let ((l (buffer-substring (point-at-bol) (point)))
  8948. a)
  8949. (when (and (looking-at "[ \t]*$")
  8950. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  8951. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  8952. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  8953. (match-beginning 1)) a)
  8954. t)))
  8955. (defun org-complete-expand-structure-template (start cell)
  8956. "Expand a structure template."
  8957. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  8958. (rpl (nth (if musep 2 1) cell))
  8959. (ind ""))
  8960. (delete-region start (point))
  8961. (when (string-match "\\`#\\+" rpl)
  8962. (cond
  8963. ((bolp))
  8964. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  8965. (setq ind (buffer-substring (point-at-bol) (point))))
  8966. (t (newline))))
  8967. (setq start (point))
  8968. (if (string-match "%file" rpl)
  8969. (setq rpl (replace-match
  8970. (concat
  8971. "\""
  8972. (save-match-data
  8973. (abbreviate-file-name (read-file-name "Include file: ")))
  8974. "\"")
  8975. t t rpl)))
  8976. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  8977. (concat "\n" ind)))
  8978. (insert rpl)
  8979. (if (re-search-backward "\\?" start t) (delete-char 1))))
  8980. (defun org-complete (&optional arg)
  8981. "Perform completion on word at point.
  8982. At the beginning of a headline, this completes TODO keywords as given in
  8983. `org-todo-keywords'.
  8984. If the current word is preceded by a backslash, completes the TeX symbols
  8985. that are supported for HTML support.
  8986. If the current word is preceded by \"#+\", completes special words for
  8987. setting file options.
  8988. In the line after \"#+STARTUP:, complete valid keywords.\"
  8989. At all other locations, this simply calls the value of
  8990. `org-completion-fallback-command'."
  8991. (interactive "P")
  8992. (org-without-partial-completion
  8993. (catch 'exit
  8994. (let* ((a nil)
  8995. (end (point))
  8996. (beg1 (save-excursion
  8997. (skip-chars-backward (org-re "[:alnum:]_@"))
  8998. (point)))
  8999. (beg (save-excursion
  9000. (skip-chars-backward "a-zA-Z0-9_:$")
  9001. (point)))
  9002. (confirm (lambda (x) (stringp (car x))))
  9003. (searchhead (equal (char-before beg) ?*))
  9004. (struct
  9005. (when (and (member (char-before beg1) '(?. ?<))
  9006. (setq a (assoc (buffer-substring beg1 (point))
  9007. org-structure-template-alist)))
  9008. (org-complete-expand-structure-template (1- beg1) a)
  9009. (throw 'exit t)))
  9010. (tag (and (equal (char-before beg1) ?:)
  9011. (equal (char-after (point-at-bol)) ?*)))
  9012. (prop (and (equal (char-before beg1) ?:)
  9013. (not (equal (char-after (point-at-bol)) ?*))))
  9014. (texp (equal (char-before beg) ?\\))
  9015. (link (equal (char-before beg) ?\[))
  9016. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  9017. beg)
  9018. "#+"))
  9019. (startup (string-match "^#\\+STARTUP:.*"
  9020. (buffer-substring (point-at-bol) (point))))
  9021. (completion-ignore-case opt)
  9022. (type nil)
  9023. (tbl nil)
  9024. (table (cond
  9025. (opt
  9026. (setq type :opt)
  9027. (require 'org-exp)
  9028. (append
  9029. (delq nil
  9030. (mapcar
  9031. (lambda (x)
  9032. (if (string-match
  9033. "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  9034. (cons (match-string 2 x)
  9035. (match-string 1 x))))
  9036. (org-split-string (org-get-current-options) "\n")))
  9037. (mapcar 'list org-additional-option-like-keywords)))
  9038. (startup
  9039. (setq type :startup)
  9040. org-startup-options)
  9041. (link (append org-link-abbrev-alist-local
  9042. org-link-abbrev-alist))
  9043. (texp
  9044. (setq type :tex)
  9045. (append org-entities-user org-entities))
  9046. ((string-match "\\`\\*+[ \t]+\\'"
  9047. (buffer-substring (point-at-bol) beg))
  9048. (setq type :todo)
  9049. (mapcar 'list org-todo-keywords-1))
  9050. (searchhead
  9051. (setq type :searchhead)
  9052. (save-excursion
  9053. (goto-char (point-min))
  9054. (while (re-search-forward org-todo-line-regexp nil t)
  9055. (push (list
  9056. (org-make-org-heading-search-string
  9057. (match-string 3) t))
  9058. tbl)))
  9059. tbl)
  9060. (tag (setq type :tag beg beg1)
  9061. (or org-tag-alist (org-get-buffer-tags)))
  9062. (prop (setq type :prop beg beg1)
  9063. (mapcar 'list (org-buffer-property-keys nil t t)))
  9064. (t (progn
  9065. (call-interactively org-completion-fallback-command)
  9066. (throw 'exit nil)))))
  9067. (pattern (buffer-substring-no-properties beg end))
  9068. (completion (try-completion pattern table confirm)))
  9069. (cond ((eq completion t)
  9070. (if (not (assoc (upcase pattern) table))
  9071. (message "Already complete")
  9072. (if (and (equal type :opt)
  9073. (not (member (car (assoc (upcase pattern) table))
  9074. org-additional-option-like-keywords)))
  9075. (insert (substring (cdr (assoc (upcase pattern) table))
  9076. (length pattern)))
  9077. (if (memq type '(:tag :prop)) (insert ":")))))
  9078. ((null completion)
  9079. (message "Can't find completion for \"%s\"" pattern)
  9080. (ding))
  9081. ((not (string= pattern completion))
  9082. (delete-region beg end)
  9083. (if (string-match " +$" completion)
  9084. (setq completion (replace-match "" t t completion)))
  9085. (insert completion)
  9086. (if (get-buffer-window "*Completions*")
  9087. (delete-window (get-buffer-window "*Completions*")))
  9088. (if (assoc completion table)
  9089. (if (eq type :todo) (insert " ")
  9090. (if (memq type '(:tag :prop)) (insert ":"))))
  9091. (if (and (equal type :opt) (assoc completion table))
  9092. (message "%s" (substitute-command-keys
  9093. "Press \\[org-complete] again to insert example settings"))))
  9094. (t
  9095. (message "Making completion list...")
  9096. (let ((list (sort (all-completions pattern table confirm)
  9097. 'string<)))
  9098. (with-output-to-temp-buffer "*Completions*"
  9099. (condition-case nil
  9100. ;; Protection needed for XEmacs and emacs 21
  9101. (display-completion-list list pattern)
  9102. (error (display-completion-list list)))))
  9103. (message "Making completion list...%s" "done")))))))
  9104. ;;;; TODO, DEADLINE, Comments
  9105. (defun org-toggle-comment ()
  9106. "Change the COMMENT state of an entry."
  9107. (interactive)
  9108. (save-excursion
  9109. (org-back-to-heading)
  9110. (let (case-fold-search)
  9111. (if (looking-at (concat outline-regexp
  9112. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9113. (replace-match "" t t nil 1)
  9114. (if (looking-at outline-regexp)
  9115. (progn
  9116. (goto-char (match-end 0))
  9117. (insert org-comment-string " ")))))))
  9118. (defvar org-last-todo-state-is-todo nil
  9119. "This is non-nil when the last TODO state change led to a TODO state.
  9120. If the last change removed the TODO tag or switched to DONE, then
  9121. this is nil.")
  9122. (defvar org-setting-tags nil) ; dynamically skipped
  9123. (defun org-parse-local-options (string var)
  9124. "Parse STRING for startup setting relevant for variable VAR."
  9125. (let ((rtn (symbol-value var))
  9126. e opts)
  9127. (save-match-data
  9128. (if (or (not string) (not (string-match "\\S-" string)))
  9129. rtn
  9130. (setq opts (delq nil (mapcar (lambda (x)
  9131. (setq e (assoc x org-startup-options))
  9132. (if (eq (nth 1 e) var) e nil))
  9133. (org-split-string string "[ \t]+"))))
  9134. (if (not opts)
  9135. rtn
  9136. (setq rtn nil)
  9137. (while (setq e (pop opts))
  9138. (if (not (nth 3 e))
  9139. (setq rtn (nth 2 e))
  9140. (if (not (listp rtn)) (setq rtn nil))
  9141. (push (nth 2 e) rtn)))
  9142. rtn)))))
  9143. (defvar org-todo-setup-filter-hook nil
  9144. "Hook for functions that pre-filter todo specs.
  9145. Each function takes a todo spec and returns either `nil' or the spec
  9146. transformed into canonical form." )
  9147. (defvar org-todo-get-default-hook nil
  9148. "Hook for functions that get a default item for todo.
  9149. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9150. `nil' or a string to be used for the todo mark." )
  9151. (defvar org-agenda-headline-snapshot-before-repeat)
  9152. (defun org-todo (&optional arg)
  9153. "Change the TODO state of an item.
  9154. The state of an item is given by a keyword at the start of the heading,
  9155. like
  9156. *** TODO Write paper
  9157. *** DONE Call mom
  9158. The different keywords are specified in the variable `org-todo-keywords'.
  9159. By default the available states are \"TODO\" and \"DONE\".
  9160. So for this example: when the item starts with TODO, it is changed to DONE.
  9161. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9162. DONE are present, add TODO at the beginning of the heading.
  9163. With C-u prefix arg, use completion to determine the new state.
  9164. With numeric prefix arg, switch to that state.
  9165. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  9166. With a triple C-u prefix, circumvent any state blocking.
  9167. For calling through lisp, arg is also interpreted in the following way:
  9168. 'none -> empty state
  9169. \"\"(empty string) -> switch to empty state
  9170. 'done -> switch to DONE
  9171. 'nextset -> switch to the next set of keywords
  9172. 'previousset -> switch to the previous set of keywords
  9173. \"WAITING\" -> switch to the specified keyword, but only if it
  9174. really is a member of `org-todo-keywords'."
  9175. (interactive "P")
  9176. (if (equal arg '(16)) (setq arg 'nextset))
  9177. (let ((org-blocker-hook org-blocker-hook)
  9178. (case-fold-search nil))
  9179. (when (equal arg '(64))
  9180. (setq arg nil org-blocker-hook nil))
  9181. (when (and org-blocker-hook
  9182. (or org-inhibit-blocking
  9183. (org-entry-get nil "NOBLOCKING")))
  9184. (setq org-blocker-hook nil))
  9185. (save-excursion
  9186. (catch 'exit
  9187. (org-back-to-heading t)
  9188. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9189. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9190. (looking-at " *"))
  9191. (let* ((match-data (match-data))
  9192. (startpos (point-at-bol))
  9193. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  9194. (org-log-done org-log-done)
  9195. (org-log-repeat org-log-repeat)
  9196. (org-todo-log-states org-todo-log-states)
  9197. (this (match-string 1))
  9198. (hl-pos (match-beginning 0))
  9199. (head (org-get-todo-sequence-head this))
  9200. (ass (assoc head org-todo-kwd-alist))
  9201. (interpret (nth 1 ass))
  9202. (done-word (nth 3 ass))
  9203. (final-done-word (nth 4 ass))
  9204. (last-state (or this ""))
  9205. (completion-ignore-case t)
  9206. (member (member this org-todo-keywords-1))
  9207. (tail (cdr member))
  9208. (state (cond
  9209. ((and org-todo-key-trigger
  9210. (or (and (equal arg '(4))
  9211. (eq org-use-fast-todo-selection 'prefix))
  9212. (and (not arg) org-use-fast-todo-selection
  9213. (not (eq org-use-fast-todo-selection
  9214. 'prefix)))))
  9215. ;; Use fast selection
  9216. (org-fast-todo-selection))
  9217. ((and (equal arg '(4))
  9218. (or (not org-use-fast-todo-selection)
  9219. (not org-todo-key-trigger)))
  9220. ;; Read a state with completion
  9221. (org-icompleting-read
  9222. "State: " (mapcar (lambda(x) (list x))
  9223. org-todo-keywords-1)
  9224. nil t))
  9225. ((eq arg 'right)
  9226. (if this
  9227. (if tail (car tail) nil)
  9228. (car org-todo-keywords-1)))
  9229. ((eq arg 'left)
  9230. (if (equal member org-todo-keywords-1)
  9231. nil
  9232. (if this
  9233. (nth (- (length org-todo-keywords-1)
  9234. (length tail) 2)
  9235. org-todo-keywords-1)
  9236. (org-last org-todo-keywords-1))))
  9237. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9238. (setq arg nil))) ; hack to fall back to cycling
  9239. (arg
  9240. ;; user or caller requests a specific state
  9241. (cond
  9242. ((equal arg "") nil)
  9243. ((eq arg 'none) nil)
  9244. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9245. ((eq arg 'nextset)
  9246. (or (car (cdr (member head org-todo-heads)))
  9247. (car org-todo-heads)))
  9248. ((eq arg 'previousset)
  9249. (let ((org-todo-heads (reverse org-todo-heads)))
  9250. (or (car (cdr (member head org-todo-heads)))
  9251. (car org-todo-heads))))
  9252. ((car (member arg org-todo-keywords-1)))
  9253. ((stringp arg)
  9254. (error "State `%s' not valid in this file" arg))
  9255. ((nth (1- (prefix-numeric-value arg))
  9256. org-todo-keywords-1))))
  9257. ((null member) (or head (car org-todo-keywords-1)))
  9258. ((equal this final-done-word) nil) ;; -> make empty
  9259. ((null tail) nil) ;; -> first entry
  9260. ((memq interpret '(type priority))
  9261. (if (eq this-command last-command)
  9262. (car tail)
  9263. (if (> (length tail) 0)
  9264. (or done-word (car org-done-keywords))
  9265. nil)))
  9266. (t
  9267. (car tail))))
  9268. (state (or
  9269. (run-hook-with-args-until-success
  9270. 'org-todo-get-default-hook state last-state)
  9271. state))
  9272. (next (if state (concat " " state " ") " "))
  9273. (change-plist (list :type 'todo-state-change :from this :to state
  9274. :position startpos))
  9275. dolog now-done-p)
  9276. (when org-blocker-hook
  9277. (setq org-last-todo-state-is-todo
  9278. (not (member this org-done-keywords)))
  9279. (unless (save-excursion
  9280. (save-match-data
  9281. (run-hook-with-args-until-failure
  9282. 'org-blocker-hook change-plist)))
  9283. (if (interactive-p)
  9284. (error "TODO state change from %s to %s blocked" this state)
  9285. ;; fail silently
  9286. (message "TODO state change from %s to %s blocked" this state)
  9287. (throw 'exit nil))))
  9288. (store-match-data match-data)
  9289. (replace-match next t t)
  9290. (unless (pos-visible-in-window-p hl-pos)
  9291. (message "TODO state changed to %s" (org-trim next)))
  9292. (unless head
  9293. (setq head (org-get-todo-sequence-head state)
  9294. ass (assoc head org-todo-kwd-alist)
  9295. interpret (nth 1 ass)
  9296. done-word (nth 3 ass)
  9297. final-done-word (nth 4 ass)))
  9298. (when (memq arg '(nextset previousset))
  9299. (message "Keyword-Set %d/%d: %s"
  9300. (- (length org-todo-sets) -1
  9301. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9302. (length org-todo-sets)
  9303. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9304. (setq org-last-todo-state-is-todo
  9305. (not (member state org-done-keywords)))
  9306. (setq now-done-p (and (member state org-done-keywords)
  9307. (not (member this org-done-keywords))))
  9308. (and logging (org-local-logging logging))
  9309. (when (and (or org-todo-log-states org-log-done)
  9310. (not (eq org-inhibit-logging t))
  9311. (not (memq arg '(nextset previousset))))
  9312. ;; we need to look at recording a time and note
  9313. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9314. (nth 2 (assoc this org-todo-log-states))))
  9315. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9316. (setq dolog 'time))
  9317. (when (and state
  9318. (member state org-not-done-keywords)
  9319. (not (member this org-not-done-keywords)))
  9320. ;; This is now a todo state and was not one before
  9321. ;; If there was a CLOSED time stamp, get rid of it.
  9322. (org-add-planning-info nil nil 'closed))
  9323. (when (and now-done-p org-log-done)
  9324. ;; It is now done, and it was not done before
  9325. (org-add-planning-info 'closed (org-current-time))
  9326. (if (and (not dolog) (eq 'note org-log-done))
  9327. (org-add-log-setup 'done state this 'findpos 'note)))
  9328. (when (and state dolog)
  9329. ;; This is a non-nil state, and we need to log it
  9330. (org-add-log-setup 'state state this 'findpos dolog)))
  9331. ;; Fixup tag positioning
  9332. (org-todo-trigger-tag-changes state)
  9333. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9334. (when org-provide-todo-statistics
  9335. (org-update-parent-todo-statistics))
  9336. (run-hooks 'org-after-todo-state-change-hook)
  9337. (if (and arg (not (member state org-done-keywords)))
  9338. (setq head (org-get-todo-sequence-head state)))
  9339. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9340. ;; Do we need to trigger a repeat?
  9341. (when now-done-p
  9342. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9343. ;; This is for the agenda, take a snapshot of the headline.
  9344. (save-match-data
  9345. (setq org-agenda-headline-snapshot-before-repeat
  9346. (org-get-heading))))
  9347. (org-auto-repeat-maybe state))
  9348. ;; Fixup cursor location if close to the keyword
  9349. (if (and (outline-on-heading-p)
  9350. (not (bolp))
  9351. (save-excursion (beginning-of-line 1)
  9352. (looking-at org-todo-line-regexp))
  9353. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  9354. (progn
  9355. (goto-char (or (match-end 2) (match-end 1)))
  9356. (and (looking-at " ") (just-one-space))))
  9357. (when org-trigger-hook
  9358. (save-excursion
  9359. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  9360. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  9361. "Block turning an entry into a TODO, using the hierarchy.
  9362. This checks whether the current task should be blocked from state
  9363. changes. Such blocking occurs when:
  9364. 1. The task has children which are not all in a completed state.
  9365. 2. A task has a parent with the property :ORDERED:, and there
  9366. are siblings prior to the current task with incomplete
  9367. status.
  9368. 3. The parent of the task is blocked because it has siblings that should
  9369. be done first, or is child of a block grandparent TODO entry."
  9370. (if (not org-enforce-todo-dependencies)
  9371. t ; if locally turned off don't block
  9372. (catch 'dont-block
  9373. ;; If this is not a todo state change, or if this entry is already DONE,
  9374. ;; do not block
  9375. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9376. (member (plist-get change-plist :from)
  9377. (cons 'done org-done-keywords))
  9378. (member (plist-get change-plist :to)
  9379. (cons 'todo org-not-done-keywords))
  9380. (not (plist-get change-plist :to)))
  9381. (throw 'dont-block t))
  9382. ;; If this task has children, and any are undone, it's blocked
  9383. (save-excursion
  9384. (org-back-to-heading t)
  9385. (let ((this-level (funcall outline-level)))
  9386. (outline-next-heading)
  9387. (let ((child-level (funcall outline-level)))
  9388. (while (and (not (eobp))
  9389. (> child-level this-level))
  9390. ;; this todo has children, check whether they are all
  9391. ;; completed
  9392. (if (and (not (org-entry-is-done-p))
  9393. (org-entry-is-todo-p))
  9394. (throw 'dont-block nil))
  9395. (outline-next-heading)
  9396. (setq child-level (funcall outline-level))))))
  9397. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  9398. ;; any previous siblings are undone, it's blocked
  9399. (save-excursion
  9400. (org-back-to-heading t)
  9401. (let* ((pos (point))
  9402. (parent-pos (and (org-up-heading-safe) (point))))
  9403. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9404. (when (and (org-entry-get (point) "ORDERED")
  9405. (forward-line 1)
  9406. (re-search-forward org-not-done-heading-regexp pos t))
  9407. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  9408. ;; Search further up the hierarchy, to see if an anchestor is blocked
  9409. (while t
  9410. (goto-char parent-pos)
  9411. (if (not (looking-at org-not-done-heading-regexp))
  9412. (throw 'dont-block t)) ; do not block, parent is not a TODO
  9413. (setq pos (point))
  9414. (setq parent-pos (and (org-up-heading-safe) (point)))
  9415. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  9416. (when (and (org-entry-get (point) "ORDERED")
  9417. (forward-line 1)
  9418. (re-search-forward org-not-done-heading-regexp pos t))
  9419. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  9420. (defcustom org-track-ordered-property-with-tag nil
  9421. "Should the ORDERED property also be shown as a tag?
  9422. The ORDERED property decides if an entry should require subtasks to be
  9423. completed in sequence. Since a property is not very visible, setting
  9424. this option means that toggling the ORDERED property with the command
  9425. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  9426. not relevant for the behavior, but it makes things more visible.
  9427. Note that toggling the tag with tags commands will not change the property
  9428. and therefore not influence behavior!
  9429. This can be t, meaning the tag ORDERED should be used, It can also be a
  9430. string to select a different tag for this task."
  9431. :group 'org-todo
  9432. :type '(choice
  9433. (const :tag "No tracking" nil)
  9434. (const :tag "Track with ORDERED tag" t)
  9435. (string :tag "Use other tag")))
  9436. (defun org-toggle-ordered-property ()
  9437. "Toggle the ORDERED property of the current entry.
  9438. For better visibility, you can track the value of this property with a tag.
  9439. See variable `org-track-ordered-property-with-tag'."
  9440. (interactive)
  9441. (let* ((t1 org-track-ordered-property-with-tag)
  9442. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  9443. (save-excursion
  9444. (org-back-to-heading)
  9445. (if (org-entry-get nil "ORDERED")
  9446. (progn
  9447. (org-delete-property "ORDERED")
  9448. (and tag (org-toggle-tag tag 'off))
  9449. (message "Subtasks can be completed in arbitrary order"))
  9450. (org-entry-put nil "ORDERED" "t")
  9451. (and tag (org-toggle-tag tag 'on))
  9452. (message "Subtasks must be completed in sequence")))))
  9453. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  9454. (defun org-block-todo-from-checkboxes (change-plist)
  9455. "Block turning an entry into a TODO, using checkboxes.
  9456. This checks whether the current task should be blocked from state
  9457. changes because there are unchecked boxes in this entry."
  9458. (if (not org-enforce-todo-checkbox-dependencies)
  9459. t ; if locally turned off don't block
  9460. (catch 'dont-block
  9461. ;; If this is not a todo state change, or if this entry is already DONE,
  9462. ;; do not block
  9463. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  9464. (member (plist-get change-plist :from)
  9465. (cons 'done org-done-keywords))
  9466. (member (plist-get change-plist :to)
  9467. (cons 'todo org-not-done-keywords))
  9468. (not (plist-get change-plist :to)))
  9469. (throw 'dont-block t))
  9470. ;; If this task has checkboxes that are not checked, it's blocked
  9471. (save-excursion
  9472. (org-back-to-heading t)
  9473. (let ((beg (point)) end)
  9474. (outline-next-heading)
  9475. (setq end (point))
  9476. (goto-char beg)
  9477. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  9478. end t)
  9479. (progn
  9480. (if (boundp 'org-blocked-by-checkboxes)
  9481. (setq org-blocked-by-checkboxes t))
  9482. (throw 'dont-block nil)))))
  9483. t))) ; do not block
  9484. (defun org-entry-blocked-p ()
  9485. "Is the current entry blocked?"
  9486. (if (org-entry-get nil "NOBLOCKING")
  9487. nil ;; Never block this entry
  9488. (not
  9489. (run-hook-with-args-until-failure
  9490. 'org-blocker-hook
  9491. (list :type 'todo-state-change
  9492. :position (point)
  9493. :from 'todo
  9494. :to 'done)))))
  9495. (defun org-update-statistics-cookies (all)
  9496. "Update the statistics cookie, either from TODO or from checkboxes.
  9497. This should be called with the cursor in a line with a statistics cookie."
  9498. (interactive "P")
  9499. (if all
  9500. (progn
  9501. (org-update-checkbox-count 'all)
  9502. (org-map-entries 'org-update-parent-todo-statistics))
  9503. (if (not (org-on-heading-p))
  9504. (org-update-checkbox-count)
  9505. (let ((pos (move-marker (make-marker) (point)))
  9506. end l1 l2)
  9507. (ignore-errors (org-back-to-heading t))
  9508. (if (not (org-on-heading-p))
  9509. (org-update-checkbox-count)
  9510. (setq l1 (org-outline-level))
  9511. (setq end (save-excursion
  9512. (outline-next-heading)
  9513. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  9514. (point)))
  9515. (if (and (save-excursion
  9516. (re-search-forward
  9517. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  9518. (not (save-excursion (re-search-forward
  9519. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  9520. (org-update-checkbox-count)
  9521. (if (and l2 (> l2 l1))
  9522. (progn
  9523. (goto-char end)
  9524. (org-update-parent-todo-statistics))
  9525. (goto-char pos)
  9526. (beginning-of-line 1)
  9527. (while (re-search-forward
  9528. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  9529. (point-at-eol) t)
  9530. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  9531. (goto-char pos)
  9532. (move-marker pos nil)))))
  9533. (defvar org-entry-property-inherited-from) ;; defined below
  9534. (defun org-update-parent-todo-statistics ()
  9535. "Update any statistics cookie in the parent of the current headline.
  9536. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  9537. the entire subtree and this will travel up the hierarchy and update
  9538. statistics everywhere."
  9539. (interactive)
  9540. (let* ((lim 0) prop
  9541. (recursive (or (not org-hierarchical-todo-statistics)
  9542. (string-match
  9543. "\\<recursive\\>"
  9544. (or (setq prop (org-entry-get
  9545. nil "COOKIE_DATA" 'inherit)) ""))))
  9546. (lim (or (and prop (marker-position
  9547. org-entry-property-inherited-from))
  9548. lim))
  9549. (first t)
  9550. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  9551. level ltoggle l1 new ndel
  9552. (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  9553. (catch 'exit
  9554. (save-excursion
  9555. (beginning-of-line 1)
  9556. (if (org-at-heading-p)
  9557. (setq ltoggle (funcall outline-level))
  9558. (error "This should not happen"))
  9559. (while (and (setq level (org-up-heading-safe))
  9560. (or recursive first)
  9561. (>= (point) lim))
  9562. (setq first nil cookie-present nil)
  9563. (unless (and level
  9564. (not (string-match
  9565. "\\<checkbox\\>"
  9566. (downcase
  9567. (or (org-entry-get
  9568. nil "COOKIE_DATA")
  9569. "")))))
  9570. (throw 'exit nil))
  9571. (while (re-search-forward box-re (point-at-eol) t)
  9572. (setq cnt-all 0 cnt-done 0 cookie-present t)
  9573. (setq is-percent (match-end 2))
  9574. (save-match-data
  9575. (unless (outline-next-heading) (throw 'exit nil))
  9576. (while (and (looking-at org-complex-heading-regexp)
  9577. (> (setq l1 (length (match-string 1))) level))
  9578. (setq kwd (and (or recursive (= l1 ltoggle))
  9579. (match-string 2)))
  9580. (if (or (eq org-provide-todo-statistics 'all-headlines)
  9581. (and (listp org-provide-todo-statistics)
  9582. (or (member kwd org-provide-todo-statistics)
  9583. (member kwd org-done-keywords))))
  9584. (setq cnt-all (1+ cnt-all))
  9585. (if (eq org-provide-todo-statistics t)
  9586. (and kwd (setq cnt-all (1+ cnt-all)))))
  9587. (and (member kwd org-done-keywords)
  9588. (setq cnt-done (1+ cnt-done)))
  9589. (outline-next-heading)))
  9590. (setq new
  9591. (if is-percent
  9592. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  9593. (format "[%d/%d]" cnt-done cnt-all))
  9594. ndel (- (match-end 0) (match-beginning 0)))
  9595. (goto-char (match-beginning 0))
  9596. (insert new)
  9597. (delete-region (point) (+ (point) ndel)))
  9598. (when cookie-present
  9599. (run-hook-with-args 'org-after-todo-statistics-hook
  9600. cnt-done (- cnt-all cnt-done))))))
  9601. (run-hooks 'org-todo-statistics-hook)))
  9602. (defvar org-after-todo-statistics-hook nil
  9603. "Hook that is called after a TODO statistics cookie has been updated.
  9604. Each function is called with two arguments: the number of not-done entries
  9605. and the number of done entries.
  9606. For example, the following function, when added to this hook, will switch
  9607. an entry to DONE when all children are done, and back to TODO when new
  9608. entries are set to a TODO status. Note that this hook is only called
  9609. when there is a statistics cookie in the headline!
  9610. (defun org-summary-todo (n-done n-not-done)
  9611. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  9612. (let (org-log-done org-log-states) ; turn off logging
  9613. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  9614. ")
  9615. (defvar org-todo-statistics-hook nil
  9616. "Hook that is run whenever Org thinks TODO statistics should be updated.
  9617. This hook runs even if there is no statistics cookie present, in which case
  9618. `org-after-todo-statistics-hook' would not run.")
  9619. (defun org-todo-trigger-tag-changes (state)
  9620. "Apply the changes defined in `org-todo-state-tags-triggers'."
  9621. (let ((l org-todo-state-tags-triggers)
  9622. changes)
  9623. (when (or (not state) (equal state ""))
  9624. (setq changes (append changes (cdr (assoc "" l)))))
  9625. (when (and (stringp state) (> (length state) 0))
  9626. (setq changes (append changes (cdr (assoc state l)))))
  9627. (when (member state org-not-done-keywords)
  9628. (setq changes (append changes (cdr (assoc 'todo l)))))
  9629. (when (member state org-done-keywords)
  9630. (setq changes (append changes (cdr (assoc 'done l)))))
  9631. (dolist (c changes)
  9632. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  9633. (defun org-local-logging (value)
  9634. "Get logging settings from a property VALUE."
  9635. (let* (words w a)
  9636. ;; directly set the variables, they are already local.
  9637. (setq org-log-done nil
  9638. org-log-repeat nil
  9639. org-todo-log-states nil)
  9640. (setq words (org-split-string value))
  9641. (while (setq w (pop words))
  9642. (cond
  9643. ((setq a (assoc w org-startup-options))
  9644. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  9645. (set (nth 1 a) (nth 2 a))))
  9646. ((setq a (org-extract-log-state-settings w))
  9647. (and (member (car a) org-todo-keywords-1)
  9648. (push a org-todo-log-states)))))))
  9649. (defun org-get-todo-sequence-head (kwd)
  9650. "Return the head of the TODO sequence to which KWD belongs.
  9651. If KWD is not set, check if there is a text property remembering the
  9652. right sequence."
  9653. (let (p)
  9654. (cond
  9655. ((not kwd)
  9656. (or (get-text-property (point-at-bol) 'org-todo-head)
  9657. (progn
  9658. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  9659. nil (point-at-eol)))
  9660. (get-text-property p 'org-todo-head))))
  9661. ((not (member kwd org-todo-keywords-1))
  9662. (car org-todo-keywords-1))
  9663. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  9664. (defun org-fast-todo-selection ()
  9665. "Fast TODO keyword selection with single keys.
  9666. Returns the new TODO keyword, or nil if no state change should occur."
  9667. (let* ((fulltable org-todo-key-alist)
  9668. (done-keywords org-done-keywords) ;; needed for the faces.
  9669. (maxlen (apply 'max (mapcar
  9670. (lambda (x)
  9671. (if (stringp (car x)) (string-width (car x)) 0))
  9672. fulltable)))
  9673. (expert nil)
  9674. (fwidth (+ maxlen 3 1 3))
  9675. (ncol (/ (- (window-width) 4) fwidth))
  9676. tg cnt e c tbl
  9677. groups ingroup)
  9678. (save-excursion
  9679. (save-window-excursion
  9680. (if expert
  9681. (set-buffer (get-buffer-create " *Org todo*"))
  9682. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  9683. (erase-buffer)
  9684. (org-set-local 'org-done-keywords done-keywords)
  9685. (setq tbl fulltable cnt 0)
  9686. (while (setq e (pop tbl))
  9687. (cond
  9688. ((equal e '(:startgroup))
  9689. (push '() groups) (setq ingroup t)
  9690. (when (not (= cnt 0))
  9691. (setq cnt 0)
  9692. (insert "\n"))
  9693. (insert "{ "))
  9694. ((equal e '(:endgroup))
  9695. (setq ingroup nil cnt 0)
  9696. (insert "}\n"))
  9697. ((equal e '(:newline))
  9698. (when (not (= cnt 0))
  9699. (setq cnt 0)
  9700. (insert "\n")
  9701. (setq e (car tbl))
  9702. (while (equal (car tbl) '(:newline))
  9703. (insert "\n")
  9704. (setq tbl (cdr tbl)))))
  9705. (t
  9706. (setq tg (car e) c (cdr e))
  9707. (if ingroup (push tg (car groups)))
  9708. (setq tg (org-add-props tg nil 'face
  9709. (org-get-todo-face tg)))
  9710. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9711. (insert "[" c "] " tg (make-string
  9712. (- fwidth 4 (length tg)) ?\ ))
  9713. (when (= (setq cnt (1+ cnt)) ncol)
  9714. (insert "\n")
  9715. (if ingroup (insert " "))
  9716. (setq cnt 0)))))
  9717. (insert "\n")
  9718. (goto-char (point-min))
  9719. (if (not expert) (org-fit-window-to-buffer))
  9720. (message "[a-z..]:Set [SPC]:clear")
  9721. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9722. (cond
  9723. ((or (= c ?\C-g)
  9724. (and (= c ?q) (not (rassoc c fulltable))))
  9725. (setq quit-flag t))
  9726. ((= c ?\ ) nil)
  9727. ((setq e (rassoc c fulltable) tg (car e))
  9728. tg)
  9729. (t (setq quit-flag t)))))))
  9730. (defun org-entry-is-todo-p ()
  9731. (member (org-get-todo-state) org-not-done-keywords))
  9732. (defun org-entry-is-done-p ()
  9733. (member (org-get-todo-state) org-done-keywords))
  9734. (defun org-get-todo-state ()
  9735. (save-excursion
  9736. (org-back-to-heading t)
  9737. (and (looking-at org-todo-line-regexp)
  9738. (match-end 2)
  9739. (match-string 2))))
  9740. (defun org-at-date-range-p (&optional inactive-ok)
  9741. "Is the cursor inside a date range?"
  9742. (interactive)
  9743. (save-excursion
  9744. (catch 'exit
  9745. (let ((pos (point)))
  9746. (skip-chars-backward "^[<\r\n")
  9747. (skip-chars-backward "<[")
  9748. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9749. (>= (match-end 0) pos)
  9750. (throw 'exit t))
  9751. (skip-chars-backward "^<[\r\n")
  9752. (skip-chars-backward "<[")
  9753. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  9754. (>= (match-end 0) pos)
  9755. (throw 'exit t)))
  9756. nil)))
  9757. (defun org-get-repeat (&optional tagline)
  9758. "Check if there is a deadline/schedule with repeater in this entry."
  9759. (save-match-data
  9760. (save-excursion
  9761. (org-back-to-heading t)
  9762. (and (re-search-forward (if tagline
  9763. (concat tagline "\\s-*" org-repeat-re)
  9764. org-repeat-re)
  9765. (org-entry-end-position) t)
  9766. (match-string-no-properties 1)))))
  9767. (defvar org-last-changed-timestamp)
  9768. (defvar org-last-inserted-timestamp)
  9769. (defvar org-log-post-message)
  9770. (defvar org-log-note-purpose)
  9771. (defvar org-log-note-how)
  9772. (defvar org-log-note-extra)
  9773. (defun org-auto-repeat-maybe (done-word)
  9774. "Check if the current headline contains a repeated deadline/schedule.
  9775. If yes, set TODO state back to what it was and change the base date
  9776. of repeating deadline/scheduled time stamps to new date.
  9777. This function is run automatically after each state change to a DONE state."
  9778. ;; last-state is dynamically scoped into this function
  9779. (let* ((repeat (org-get-repeat))
  9780. (aa (assoc last-state org-todo-kwd-alist))
  9781. (interpret (nth 1 aa))
  9782. (head (nth 2 aa))
  9783. (whata '(("d" . day) ("m" . month) ("y" . year)))
  9784. (msg "Entry repeats: ")
  9785. (org-log-done nil)
  9786. (org-todo-log-states nil)
  9787. (nshiftmax 10) (nshift 0)
  9788. re type n what ts time)
  9789. (when repeat
  9790. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  9791. (org-todo (if (eq interpret 'type) last-state head))
  9792. (org-entry-put nil "LAST_REPEAT" (format-time-string
  9793. (org-time-stamp-format t t)))
  9794. (when org-log-repeat
  9795. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  9796. (memq 'org-add-log-note post-command-hook))
  9797. ;; OK, we are already setup for some record
  9798. (if (eq org-log-repeat 'note)
  9799. ;; make sure we take a note, not only a time stamp
  9800. (setq org-log-note-how 'note))
  9801. ;; Set up for taking a record
  9802. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  9803. last-state
  9804. 'findpos org-log-repeat)))
  9805. (org-back-to-heading t)
  9806. (org-add-planning-info nil nil 'closed)
  9807. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  9808. org-deadline-time-regexp "\\)\\|\\("
  9809. org-ts-regexp "\\)"))
  9810. (while (re-search-forward
  9811. re (save-excursion (outline-next-heading) (point)) t)
  9812. (setq type (if (match-end 1) org-scheduled-string
  9813. (if (match-end 3) org-deadline-string "Plain:"))
  9814. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  9815. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  9816. (setq n (string-to-number (match-string 2 ts))
  9817. what (match-string 3 ts))
  9818. (if (equal what "w") (setq n (* n 7) what "d"))
  9819. ;; Preparation, see if we need to modify the start date for the change
  9820. (when (match-end 1)
  9821. (setq time (save-match-data (org-time-string-to-time ts)))
  9822. (cond
  9823. ((equal (match-string 1 ts) ".")
  9824. ;; Shift starting date to today
  9825. (org-timestamp-change
  9826. (- (time-to-days (current-time)) (time-to-days time))
  9827. 'day))
  9828. ((equal (match-string 1 ts) "+")
  9829. (while (or (= nshift 0)
  9830. (<= (time-to-days time) (time-to-days (current-time))))
  9831. (when (= (incf nshift) nshiftmax)
  9832. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  9833. (error "Abort")))
  9834. (org-timestamp-change n (cdr (assoc what whata)))
  9835. (org-at-timestamp-p t)
  9836. (setq ts (match-string 1))
  9837. (setq time (save-match-data (org-time-string-to-time ts))))
  9838. (org-timestamp-change (- n) (cdr (assoc what whata)))
  9839. ;; rematch, so that we have everything in place for the real shift
  9840. (org-at-timestamp-p t)
  9841. (setq ts (match-string 1))
  9842. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  9843. (org-timestamp-change n (cdr (assoc what whata)))
  9844. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  9845. (setq org-log-post-message msg)
  9846. (message "%s" msg))))
  9847. (defun org-show-todo-tree (arg)
  9848. "Make a compact tree which shows all headlines marked with TODO.
  9849. The tree will show the lines where the regexp matches, and all higher
  9850. headlines above the match.
  9851. With a \\[universal-argument] prefix, prompt for a regexp to match.
  9852. With a numeric prefix N, construct a sparse tree for the Nth element
  9853. of `org-todo-keywords-1'."
  9854. (interactive "P")
  9855. (let ((case-fold-search nil)
  9856. (kwd-re
  9857. (cond ((null arg) org-not-done-regexp)
  9858. ((equal arg '(4))
  9859. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  9860. (mapcar 'list org-todo-keywords-1))))
  9861. (concat "\\("
  9862. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  9863. "\\)\\>")))
  9864. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  9865. (regexp-quote (nth (1- (prefix-numeric-value arg))
  9866. org-todo-keywords-1)))
  9867. (t (error "Invalid prefix argument: %s" arg)))))
  9868. (message "%d TODO entries found"
  9869. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  9870. (defun org-deadline (&optional remove time)
  9871. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  9872. With argument REMOVE, remove any deadline from the item.
  9873. When TIME is set, it should be an internal time specification, and the
  9874. scheduling will use the corresponding date."
  9875. (interactive "P")
  9876. (let* ((old-date (org-entry-get nil "DEADLINE"))
  9877. (repeater (and old-date
  9878. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  9879. (match-string 1 old-date))))
  9880. (if remove
  9881. (progn
  9882. (when (and old-date org-log-redeadline)
  9883. (org-add-log-setup 'deldeadline nil old-date 'findpos
  9884. org-log-redeadline))
  9885. (org-remove-timestamp-with-keyword org-deadline-string)
  9886. (message "Item no longer has a deadline."))
  9887. (org-add-planning-info 'deadline time 'closed)
  9888. (when (and old-date org-log-redeadline
  9889. (not (equal old-date
  9890. (substring org-last-inserted-timestamp 1 -1))))
  9891. (org-add-log-setup 'redeadline nil old-date 'findpos
  9892. org-log-redeadline))
  9893. (when repeater
  9894. (save-excursion
  9895. (org-back-to-heading t)
  9896. (when (re-search-forward (concat org-deadline-string " "
  9897. org-last-inserted-timestamp)
  9898. (save-excursion
  9899. (outline-next-heading) (point)) t)
  9900. (goto-char (1- (match-end 0)))
  9901. (insert " " repeater)
  9902. (setq org-last-inserted-timestamp
  9903. (concat (substring org-last-inserted-timestamp 0 -1)
  9904. " " repeater
  9905. (substring org-last-inserted-timestamp -1))))))
  9906. (message "Deadline on %s" org-last-inserted-timestamp))))
  9907. (defun org-schedule (&optional remove time)
  9908. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  9909. With argument REMOVE, remove any scheduling date from the item.
  9910. When TIME is set, it should be an internal time specification, and the
  9911. scheduling will use the corresponding date."
  9912. (interactive "P")
  9913. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  9914. (repeater (and old-date
  9915. (string-match "\\([.+]+[0-9]+[dwmy]\\) ?" old-date)
  9916. (match-string 1 old-date))))
  9917. (if remove
  9918. (progn
  9919. (when (and old-date org-log-reschedule)
  9920. (org-add-log-setup 'delschedule nil old-date 'findpos
  9921. org-log-reschedule))
  9922. (org-remove-timestamp-with-keyword org-scheduled-string)
  9923. (message "Item is no longer scheduled."))
  9924. (org-add-planning-info 'scheduled time 'closed)
  9925. (when (and old-date org-log-reschedule
  9926. (not (equal old-date
  9927. (substring org-last-inserted-timestamp 1 -1))))
  9928. (org-add-log-setup 'reschedule nil old-date 'findpos
  9929. org-log-reschedule))
  9930. (when repeater
  9931. (save-excursion
  9932. (org-back-to-heading t)
  9933. (when (re-search-forward (concat org-scheduled-string " "
  9934. org-last-inserted-timestamp)
  9935. (save-excursion
  9936. (outline-next-heading) (point)) t)
  9937. (goto-char (1- (match-end 0)))
  9938. (insert " " repeater)
  9939. (setq org-last-inserted-timestamp
  9940. (concat (substring org-last-inserted-timestamp 0 -1)
  9941. " " repeater
  9942. (substring org-last-inserted-timestamp -1))))))
  9943. (message "Scheduled to %s" org-last-inserted-timestamp))))
  9944. (defun org-get-scheduled-time (pom &optional inherit)
  9945. "Get the scheduled time as a time tuple, of a format suitable
  9946. for calling org-schedule with, or if there is no scheduling,
  9947. returns nil."
  9948. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  9949. (when time
  9950. (apply 'encode-time (org-parse-time-string time)))))
  9951. (defun org-get-deadline-time (pom &optional inherit)
  9952. "Get the deadine as a time tuple, of a format suitable for
  9953. calling org-deadline with, or if there is no scheduling, returns
  9954. nil."
  9955. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  9956. (when time
  9957. (apply 'encode-time (org-parse-time-string time)))))
  9958. (defun org-remove-timestamp-with-keyword (keyword)
  9959. "Remove all time stamps with KEYWORD in the current entry."
  9960. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  9961. beg)
  9962. (save-excursion
  9963. (org-back-to-heading t)
  9964. (setq beg (point))
  9965. (outline-next-heading)
  9966. (while (re-search-backward re beg t)
  9967. (replace-match "")
  9968. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  9969. (equal (char-before) ?\ ))
  9970. (backward-delete-char 1)
  9971. (if (string-match "^[ \t]*$" (buffer-substring
  9972. (point-at-bol) (point-at-eol)))
  9973. (delete-region (point-at-bol)
  9974. (min (point-max) (1+ (point-at-eol))))))))))
  9975. (defun org-add-planning-info (what &optional time &rest remove)
  9976. "Insert new timestamp with keyword in the line directly after the headline.
  9977. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  9978. If non is given, the user is prompted for a date.
  9979. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  9980. be removed."
  9981. (interactive)
  9982. (let (org-time-was-given org-end-time-was-given ts
  9983. end default-time default-input)
  9984. (catch 'exit
  9985. (when (and (not time) (memq what '(scheduled deadline)))
  9986. ;; Try to get a default date/time from existing timestamp
  9987. (save-excursion
  9988. (org-back-to-heading t)
  9989. (setq end (save-excursion (outline-next-heading) (point)))
  9990. (when (re-search-forward (if (eq what 'scheduled)
  9991. org-scheduled-time-regexp
  9992. org-deadline-time-regexp)
  9993. end t)
  9994. (setq ts (match-string 1)
  9995. default-time
  9996. (apply 'encode-time (org-parse-time-string ts))
  9997. default-input (and ts (org-get-compact-tod ts))))))
  9998. (when what
  9999. ;; If necessary, get the time from the user
  10000. (setq time (or time (org-read-date nil 'to-time nil nil
  10001. default-time default-input))))
  10002. (when (and org-insert-labeled-timestamps-at-point
  10003. (member what '(scheduled deadline)))
  10004. (insert
  10005. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10006. (org-insert-time-stamp time org-time-was-given
  10007. nil nil nil (list org-end-time-was-given))
  10008. (setq what nil))
  10009. (save-excursion
  10010. (save-restriction
  10011. (let (col list elt ts buffer-invisibility-spec)
  10012. (org-back-to-heading t)
  10013. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10014. (goto-char (match-end 1))
  10015. (setq col (current-column))
  10016. (goto-char (match-end 0))
  10017. (if (eobp) (insert "\n") (forward-char 1))
  10018. (when (and (not what)
  10019. (not (looking-at
  10020. (concat "[ \t]*"
  10021. org-keyword-time-not-clock-regexp))))
  10022. ;; Nothing to add, nothing to remove...... :-)
  10023. (throw 'exit nil))
  10024. (if (and (not (looking-at outline-regexp))
  10025. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10026. "[^\r\n]*"))
  10027. (not (equal (match-string 1) org-clock-string)))
  10028. (narrow-to-region (match-beginning 0) (match-end 0))
  10029. (insert-before-markers "\n")
  10030. (backward-char 1)
  10031. (narrow-to-region (point) (point))
  10032. (and org-adapt-indentation (org-indent-to-column col)))
  10033. ;; Check if we have to remove something.
  10034. (setq list (cons what remove))
  10035. (while list
  10036. (setq elt (pop list))
  10037. (goto-char (point-min))
  10038. (when (or (and (eq elt 'scheduled)
  10039. (re-search-forward org-scheduled-time-regexp nil t))
  10040. (and (eq elt 'deadline)
  10041. (re-search-forward org-deadline-time-regexp nil t))
  10042. (and (eq elt 'closed)
  10043. (re-search-forward org-closed-time-regexp nil t)))
  10044. (replace-match "")
  10045. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10046. (skip-chars-backward " ")
  10047. (if (looking-at " +") (replace-match ""))))
  10048. (goto-char (point-max))
  10049. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10050. (when what
  10051. (insert
  10052. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10053. (cond ((eq what 'scheduled) org-scheduled-string)
  10054. ((eq what 'deadline) org-deadline-string)
  10055. ((eq what 'closed) org-closed-string))
  10056. " ")
  10057. (setq ts (org-insert-time-stamp
  10058. time
  10059. (or org-time-was-given
  10060. (and (eq what 'closed) org-log-done-with-time))
  10061. (eq what 'closed)
  10062. nil nil (list org-end-time-was-given)))
  10063. (end-of-line 1))
  10064. (goto-char (point-min))
  10065. (widen)
  10066. (if (and (looking-at "[ \t]+\n")
  10067. (equal (char-before) ?\n))
  10068. (delete-region (1- (point)) (point-at-eol)))
  10069. ts))))))
  10070. (defvar org-log-note-marker (make-marker))
  10071. (defvar org-log-note-purpose nil)
  10072. (defvar org-log-note-state nil)
  10073. (defvar org-log-note-previous-state nil)
  10074. (defvar org-log-note-how nil)
  10075. (defvar org-log-note-extra nil)
  10076. (defvar org-log-note-window-configuration nil)
  10077. (defvar org-log-note-return-to (make-marker))
  10078. (defvar org-log-post-message nil
  10079. "Message to be displayed after a log note has been stored.
  10080. The auto-repeater uses this.")
  10081. (defun org-add-note ()
  10082. "Add a note to the current entry.
  10083. This is done in the same way as adding a state change note."
  10084. (interactive)
  10085. (org-add-log-setup 'note nil nil 'findpos nil))
  10086. (defvar org-property-end-re)
  10087. (defun org-add-log-setup (&optional purpose state prev-state
  10088. findpos how &optional extra)
  10089. "Set up the post command hook to take a note.
  10090. If this is about to TODO state change, the new state is expected in STATE.
  10091. When FINDPOS is non-nil, find the correct position for the note in
  10092. the current entry. If not, assume that it can be inserted at point.
  10093. HOW is an indicator what kind of note should be created.
  10094. EXTRA is additional text that will be inserted into the notes buffer."
  10095. (let* ((org-log-into-drawer (org-log-into-drawer))
  10096. (drawer (cond ((stringp org-log-into-drawer)
  10097. org-log-into-drawer)
  10098. (org-log-into-drawer "LOGBOOK")
  10099. (t nil))))
  10100. (save-restriction
  10101. (save-excursion
  10102. (when findpos
  10103. (org-back-to-heading t)
  10104. (narrow-to-region (point) (save-excursion
  10105. (outline-next-heading) (point)))
  10106. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10107. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10108. "[^\r\n]*\\)?"))
  10109. (goto-char (match-end 0))
  10110. (cond
  10111. (drawer
  10112. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10113. nil t)
  10114. (progn
  10115. (goto-char (match-end 0))
  10116. (or org-log-states-order-reversed
  10117. (and (re-search-forward org-property-end-re nil t)
  10118. (goto-char (1- (match-beginning 0))))))
  10119. (insert "\n:" drawer ":\n:END:")
  10120. (beginning-of-line 0)
  10121. (org-indent-line-function)
  10122. (beginning-of-line 2)
  10123. (org-indent-line-function)
  10124. (end-of-line 0)))
  10125. ((and org-log-state-notes-insert-after-drawers
  10126. (save-excursion
  10127. (forward-line) (looking-at org-drawer-regexp)))
  10128. (forward-line)
  10129. (while (looking-at org-drawer-regexp)
  10130. (goto-char (match-end 0))
  10131. (re-search-forward org-property-end-re (point-max) t)
  10132. (forward-line))
  10133. (forward-line -1)))
  10134. (unless org-log-states-order-reversed
  10135. (and (= (char-after) ?\n) (forward-char 1))
  10136. (org-skip-over-state-notes)
  10137. (skip-chars-backward " \t\n\r")))
  10138. (move-marker org-log-note-marker (point))
  10139. (setq org-log-note-purpose purpose
  10140. org-log-note-state state
  10141. org-log-note-previous-state prev-state
  10142. org-log-note-how how
  10143. org-log-note-extra extra)
  10144. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10145. (defun org-skip-over-state-notes ()
  10146. "Skip past the list of State notes in an entry."
  10147. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10148. (while (looking-at "[ \t]*- State")
  10149. (condition-case nil
  10150. (org-next-item)
  10151. (error (org-end-of-item)))))
  10152. (defun org-add-log-note (&optional purpose)
  10153. "Pop up a window for taking a note, and add this note later at point."
  10154. (remove-hook 'post-command-hook 'org-add-log-note)
  10155. (setq org-log-note-window-configuration (current-window-configuration))
  10156. (delete-other-windows)
  10157. (move-marker org-log-note-return-to (point))
  10158. (switch-to-buffer (marker-buffer org-log-note-marker))
  10159. (goto-char org-log-note-marker)
  10160. (org-switch-to-buffer-other-window "*Org Note*")
  10161. (erase-buffer)
  10162. (if (memq org-log-note-how '(time state))
  10163. (let (current-prefix-arg) (org-store-log-note))
  10164. (let ((org-inhibit-startup t)) (org-mode))
  10165. (insert (format "# Insert note for %s.
  10166. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10167. (cond
  10168. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10169. ((eq org-log-note-purpose 'done) "closed todo item")
  10170. ((eq org-log-note-purpose 'state)
  10171. (format "state change from \"%s\" to \"%s\""
  10172. (or org-log-note-previous-state "")
  10173. (or org-log-note-state "")))
  10174. ((eq org-log-note-purpose 'reschedule)
  10175. "rescheduling")
  10176. ((eq org-log-note-purpose 'delschedule)
  10177. "no longer scheduled")
  10178. ((eq org-log-note-purpose 'redeadline)
  10179. "changing deadline")
  10180. ((eq org-log-note-purpose 'deldeadline)
  10181. "removing deadline")
  10182. ((eq org-log-note-purpose 'refile)
  10183. "refiling")
  10184. ((eq org-log-note-purpose 'note)
  10185. "this entry")
  10186. (t (error "This should not happen")))))
  10187. (if org-log-note-extra (insert org-log-note-extra))
  10188. (org-set-local 'org-finish-function 'org-store-log-note)))
  10189. (defvar org-note-abort nil) ; dynamically scoped
  10190. (defun org-store-log-note ()
  10191. "Finish taking a log note, and insert it to where it belongs."
  10192. (let ((txt (buffer-string))
  10193. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10194. lines ind)
  10195. (kill-buffer (current-buffer))
  10196. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10197. (setq txt (replace-match "" t t txt)))
  10198. (if (string-match "\\s-+\\'" txt)
  10199. (setq txt (replace-match "" t t txt)))
  10200. (setq lines (org-split-string txt "\n"))
  10201. (when (and note (string-match "\\S-" note))
  10202. (setq note
  10203. (org-replace-escapes
  10204. note
  10205. (list (cons "%u" (user-login-name))
  10206. (cons "%U" user-full-name)
  10207. (cons "%t" (format-time-string
  10208. (org-time-stamp-format 'long 'inactive)
  10209. (current-time)))
  10210. (cons "%s" (if org-log-note-state
  10211. (concat "\"" org-log-note-state "\"")
  10212. ""))
  10213. (cons "%S" (if org-log-note-previous-state
  10214. (concat "\"" org-log-note-previous-state "\"")
  10215. "\"\"")))))
  10216. (if lines (setq note (concat note " \\\\")))
  10217. (push note lines))
  10218. (when (or current-prefix-arg org-note-abort)
  10219. (when org-log-into-drawer
  10220. (org-remove-empty-drawer-at
  10221. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10222. org-log-note-marker))
  10223. (setq lines nil))
  10224. (when lines
  10225. (with-current-buffer (marker-buffer org-log-note-marker)
  10226. (save-excursion
  10227. (goto-char org-log-note-marker)
  10228. (move-marker org-log-note-marker nil)
  10229. (end-of-line 1)
  10230. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10231. (insert "- " (pop lines))
  10232. (org-indent-line-function)
  10233. (beginning-of-line 1)
  10234. (looking-at "[ \t]*")
  10235. (setq ind (concat (match-string 0) " "))
  10236. (end-of-line 1)
  10237. (while lines (insert "\n" ind (pop lines)))
  10238. (message "Note stored")
  10239. (org-back-to-heading t)
  10240. (org-cycle-hide-drawers 'children)))))
  10241. (set-window-configuration org-log-note-window-configuration)
  10242. (with-current-buffer (marker-buffer org-log-note-return-to)
  10243. (goto-char org-log-note-return-to))
  10244. (move-marker org-log-note-return-to nil)
  10245. (and org-log-post-message (message "%s" org-log-post-message)))
  10246. (defun org-remove-empty-drawer-at (drawer pos)
  10247. "Remove an empty drawer DRAWER at position POS.
  10248. POS may also be a marker."
  10249. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10250. (save-excursion
  10251. (save-restriction
  10252. (widen)
  10253. (goto-char pos)
  10254. (if (org-in-regexp
  10255. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10256. (replace-match ""))))))
  10257. (defun org-sparse-tree (&optional arg)
  10258. "Create a sparse tree, prompt for the details.
  10259. This command can create sparse trees. You first need to select the type
  10260. of match used to create the tree:
  10261. t Show entries with a specific TODO keyword.
  10262. m Show entries selected by a tags/property match.
  10263. p Enter a property name and its value (both with completion on existing
  10264. names/values) and show entries with that property.
  10265. / Show entries matching a regular expression (`r' can be used as well)
  10266. d Show deadlines due within `org-deadline-warning-days'.
  10267. b Show deadlines and scheduled items before a date.
  10268. a Show deadlines and scheduled items after a date."
  10269. (interactive "P")
  10270. (let (ans kwd value)
  10271. (message "Sparse tree: [/]regexp [t]odo-kwd [m]atch [p]roperty [d]eadlines [b]efore-date [a]fter-date")
  10272. (setq ans (read-char-exclusive))
  10273. (cond
  10274. ((equal ans ?d)
  10275. (call-interactively 'org-check-deadlines))
  10276. ((equal ans ?b)
  10277. (call-interactively 'org-check-before-date))
  10278. ((equal ans ?a)
  10279. (call-interactively 'org-check-after-date))
  10280. ((equal ans ?t)
  10281. (org-show-todo-tree '(4)))
  10282. ((member ans '(?T ?m))
  10283. (call-interactively 'org-match-sparse-tree))
  10284. ((member ans '(?p ?P))
  10285. (setq kwd (org-icompleting-read "Property: "
  10286. (mapcar 'list (org-buffer-property-keys))))
  10287. (setq value (org-icompleting-read "Value: "
  10288. (mapcar 'list (org-property-values kwd))))
  10289. (unless (string-match "\\`{.*}\\'" value)
  10290. (setq value (concat "\"" value "\"")))
  10291. (org-match-sparse-tree arg (concat kwd "=" value)))
  10292. ((member ans '(?r ?R ?/))
  10293. (call-interactively 'org-occur))
  10294. (t (error "No such sparse tree command \"%c\"" ans)))))
  10295. (defvar org-occur-highlights nil
  10296. "List of overlays used for occur matches.")
  10297. (make-variable-buffer-local 'org-occur-highlights)
  10298. (defvar org-occur-parameters nil
  10299. "Parameters of the active org-occur calls.
  10300. This is a list, each call to org-occur pushes as cons cell,
  10301. containing the regular expression and the callback, onto the list.
  10302. The list can contain several entries if `org-occur' has been called
  10303. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10304. will only contain one set of parameters. When the highlights are
  10305. removed (for example with `C-c C-c', or with the next edit (depending
  10306. on `org-remove-highlights-with-change'), this variable is emptied
  10307. as well.")
  10308. (make-variable-buffer-local 'org-occur-parameters)
  10309. (defun org-occur (regexp &optional keep-previous callback)
  10310. "Make a compact tree which shows all matches of REGEXP.
  10311. The tree will show the lines where the regexp matches, and all higher
  10312. headlines above the match. It will also show the heading after the match,
  10313. to make sure editing the matching entry is easy.
  10314. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  10315. call to `org-occur' will be kept, to allow stacking of calls to this
  10316. command.
  10317. If CALLBACK is non-nil, it is a function which is called to confirm
  10318. that the match should indeed be shown."
  10319. (interactive "sRegexp: \nP")
  10320. (when (equal regexp "")
  10321. (error "Regexp cannot be empty"))
  10322. (unless keep-previous
  10323. (org-remove-occur-highlights nil nil t))
  10324. (push (cons regexp callback) org-occur-parameters)
  10325. (let ((cnt 0))
  10326. (save-excursion
  10327. (goto-char (point-min))
  10328. (if (or (not keep-previous) ; do not want to keep
  10329. (not org-occur-highlights)) ; no previous matches
  10330. ;; hide everything
  10331. (org-overview))
  10332. (while (re-search-forward regexp nil t)
  10333. (when (or (not callback)
  10334. (save-match-data (funcall callback)))
  10335. (setq cnt (1+ cnt))
  10336. (when org-highlight-sparse-tree-matches
  10337. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  10338. (org-show-context 'occur-tree))))
  10339. (when org-remove-highlights-with-change
  10340. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  10341. nil 'local))
  10342. (unless org-sparse-tree-open-archived-trees
  10343. (org-hide-archived-subtrees (point-min) (point-max)))
  10344. (run-hooks 'org-occur-hook)
  10345. (if (interactive-p)
  10346. (message "%d match(es) for regexp %s" cnt regexp))
  10347. cnt))
  10348. (defun org-show-context (&optional key)
  10349. "Make sure point and context and visible.
  10350. How much context is shown depends upon the variables
  10351. `org-show-hierarchy-above', `org-show-following-heading'. and
  10352. `org-show-siblings'."
  10353. (let ((heading-p (org-on-heading-p t))
  10354. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  10355. (following-p (org-get-alist-option org-show-following-heading key))
  10356. (entry-p (org-get-alist-option org-show-entry-below key))
  10357. (siblings-p (org-get-alist-option org-show-siblings key)))
  10358. (catch 'exit
  10359. ;; Show heading or entry text
  10360. (if (and heading-p (not entry-p))
  10361. (org-flag-heading nil) ; only show the heading
  10362. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  10363. (org-show-hidden-entry))) ; show entire entry
  10364. (when following-p
  10365. ;; Show next sibling, or heading below text
  10366. (save-excursion
  10367. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  10368. (org-flag-heading nil))))
  10369. (when siblings-p (org-show-siblings))
  10370. (when hierarchy-p
  10371. ;; show all higher headings, possibly with siblings
  10372. (save-excursion
  10373. (while (and (condition-case nil
  10374. (progn (org-up-heading-all 1) t)
  10375. (error nil))
  10376. (not (bobp)))
  10377. (org-flag-heading nil)
  10378. (when siblings-p (org-show-siblings))))))))
  10379. (defvar org-reveal-start-hook nil
  10380. "Hook run before revealing a location.")
  10381. (defun org-reveal (&optional siblings)
  10382. "Show current entry, hierarchy above it, and the following headline.
  10383. This can be used to show a consistent set of context around locations
  10384. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  10385. not t for the search context.
  10386. With optional argument SIBLINGS, on each level of the hierarchy all
  10387. siblings are shown. This repairs the tree structure to what it would
  10388. look like when opened with hierarchical calls to `org-cycle'.
  10389. With double optional argument `C-u C-u', go to the parent and show the
  10390. entire tree."
  10391. (interactive "P")
  10392. (run-hooks 'org-reveal-start-hook)
  10393. (let ((org-show-hierarchy-above t)
  10394. (org-show-following-heading t)
  10395. (org-show-siblings (if siblings t org-show-siblings)))
  10396. (org-show-context nil))
  10397. (when (equal siblings '(16))
  10398. (save-excursion
  10399. (when (org-up-heading-safe)
  10400. (org-show-subtree)
  10401. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  10402. (defun org-highlight-new-match (beg end)
  10403. "Highlight from BEG to END and mark the highlight is an occur headline."
  10404. (let ((ov (org-make-overlay beg end)))
  10405. (org-overlay-put ov 'face 'secondary-selection)
  10406. (push ov org-occur-highlights)))
  10407. (defun org-remove-occur-highlights (&optional beg end noremove)
  10408. "Remove the occur highlights from the buffer.
  10409. BEG and END are ignored. If NOREMOVE is nil, remove this function
  10410. from the `before-change-functions' in the current buffer."
  10411. (interactive)
  10412. (unless org-inhibit-highlight-removal
  10413. (mapc 'org-delete-overlay org-occur-highlights)
  10414. (setq org-occur-highlights nil)
  10415. (setq org-occur-parameters nil)
  10416. (unless noremove
  10417. (remove-hook 'before-change-functions
  10418. 'org-remove-occur-highlights 'local))))
  10419. ;;;; Priorities
  10420. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  10421. "Regular expression matching the priority indicator.")
  10422. (defvar org-remove-priority-next-time nil)
  10423. (defun org-priority-up ()
  10424. "Increase the priority of the current item."
  10425. (interactive)
  10426. (org-priority 'up))
  10427. (defun org-priority-down ()
  10428. "Decrease the priority of the current item."
  10429. (interactive)
  10430. (org-priority 'down))
  10431. (defun org-priority (&optional action)
  10432. "Change the priority of an item by ARG.
  10433. ACTION can be `set', `up', `down', or a character."
  10434. (interactive)
  10435. (unless org-enable-priority-commands
  10436. (error "Priority commands are disabled"))
  10437. (setq action (or action 'set))
  10438. (let (current new news have remove)
  10439. (save-excursion
  10440. (org-back-to-heading t)
  10441. (if (looking-at org-priority-regexp)
  10442. (setq current (string-to-char (match-string 2))
  10443. have t)
  10444. (setq current org-default-priority))
  10445. (cond
  10446. ((eq action 'remove)
  10447. (setq remove t new ?\ ))
  10448. ((or (eq action 'set)
  10449. (if (featurep 'xemacs) (characterp action) (integerp action)))
  10450. (if (not (eq action 'set))
  10451. (setq new action)
  10452. (message "Priority %c-%c, SPC to remove: "
  10453. org-highest-priority org-lowest-priority)
  10454. (setq new (read-char-exclusive)))
  10455. (if (and (= (upcase org-highest-priority) org-highest-priority)
  10456. (= (upcase org-lowest-priority) org-lowest-priority))
  10457. (setq new (upcase new)))
  10458. (cond ((equal new ?\ ) (setq remove t))
  10459. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  10460. (error "Priority must be between `%c' and `%c'"
  10461. org-highest-priority org-lowest-priority))))
  10462. ((eq action 'up)
  10463. (if (and (not have) (eq last-command this-command))
  10464. (setq new org-lowest-priority)
  10465. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10466. org-default-priority (1- current)))))
  10467. ((eq action 'down)
  10468. (if (and (not have) (eq last-command this-command))
  10469. (setq new org-highest-priority)
  10470. (setq new (if (and org-priority-start-cycle-with-default (not have))
  10471. org-default-priority (1+ current)))))
  10472. (t (error "Invalid action")))
  10473. (if (or (< (upcase new) org-highest-priority)
  10474. (> (upcase new) org-lowest-priority))
  10475. (setq remove t))
  10476. (setq news (format "%c" new))
  10477. (if have
  10478. (if remove
  10479. (replace-match "" t t nil 1)
  10480. (replace-match news t t nil 2))
  10481. (if remove
  10482. (error "No priority cookie found in line")
  10483. (let ((case-fold-search nil))
  10484. (looking-at org-todo-line-regexp))
  10485. (if (match-end 2)
  10486. (progn
  10487. (goto-char (match-end 2))
  10488. (insert " [#" news "]"))
  10489. (goto-char (match-beginning 3))
  10490. (insert "[#" news "] "))))
  10491. (org-preserve-lc (org-set-tags nil 'align)))
  10492. (if remove
  10493. (message "Priority removed")
  10494. (message "Priority of current item set to %s" news))))
  10495. (defun org-get-priority (s)
  10496. "Find priority cookie and return priority."
  10497. (save-match-data
  10498. (if (not (string-match org-priority-regexp s))
  10499. (* 1000 (- org-lowest-priority org-default-priority))
  10500. (* 1000 (- org-lowest-priority
  10501. (string-to-char (match-string 2 s)))))))
  10502. ;;;; Tags
  10503. (defvar org-agenda-archives-mode)
  10504. (defvar org-map-continue-from nil
  10505. "Position from where mapping should continue.
  10506. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  10507. (defvar org-scanner-tags nil
  10508. "The current tag list while the tags scanner is running.")
  10509. (defvar org-trust-scanner-tags nil
  10510. "Should `org-get-tags-at' use the tags fro the scanner.
  10511. This is for internal dynamical scoping only.
  10512. When this is non-nil, the function `org-get-tags-at' will return the value
  10513. of `org-scanner-tags' instead of building the list by itself. This
  10514. can lead to large speed-ups when the tags scanner is used in a file with
  10515. many entries, and when the list of tags is retrieved, for example to
  10516. obtain a list of properties. Building the tags list for each entry in such
  10517. a file becomes an N^2 operation - but with this variable set, it scales
  10518. as N.")
  10519. (defun org-scan-tags (action matcher &optional todo-only)
  10520. "Scan headline tags with inheritance and produce output ACTION.
  10521. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  10522. or `agenda' to produce an entry list for an agenda view. It can also be
  10523. a Lisp form or a function that should be called at each matched headline, in
  10524. this case the return value is a list of all return values from these calls.
  10525. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  10526. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  10527. only lines with a TODO keyword are included in the output."
  10528. (require 'org-agenda)
  10529. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  10530. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  10531. (org-re
  10532. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  10533. (props (list 'face 'default
  10534. 'done-face 'org-agenda-done
  10535. 'undone-face 'default
  10536. 'mouse-face 'highlight
  10537. 'org-not-done-regexp org-not-done-regexp
  10538. 'org-todo-regexp org-todo-regexp
  10539. 'help-echo
  10540. (format "mouse-2 or RET jump to org file %s"
  10541. (abbreviate-file-name
  10542. (or (buffer-file-name (buffer-base-buffer))
  10543. (buffer-name (buffer-base-buffer)))))))
  10544. (case-fold-search nil)
  10545. (org-map-continue-from nil)
  10546. lspos tags tags-list
  10547. (tags-alist (list (cons 0 org-file-tags)))
  10548. (llast 0) rtn rtn1 level category i txt
  10549. todo marker entry priority)
  10550. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  10551. (setq action (list 'lambda nil action)))
  10552. (save-excursion
  10553. (goto-char (point-min))
  10554. (when (eq action 'sparse-tree)
  10555. (org-overview)
  10556. (org-remove-occur-highlights))
  10557. (while (re-search-forward re nil t)
  10558. (catch :skip
  10559. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  10560. tags (if (match-end 4) (org-match-string-no-properties 4)))
  10561. (goto-char (setq lspos (match-beginning 0)))
  10562. (setq level (org-reduced-level (funcall outline-level))
  10563. category (org-get-category))
  10564. (setq i llast llast level)
  10565. ;; remove tag lists from same and sublevels
  10566. (while (>= i level)
  10567. (when (setq entry (assoc i tags-alist))
  10568. (setq tags-alist (delete entry tags-alist)))
  10569. (setq i (1- i)))
  10570. ;; add the next tags
  10571. (when tags
  10572. (setq tags (org-split-string tags ":")
  10573. tags-alist
  10574. (cons (cons level tags) tags-alist)))
  10575. ;; compile tags for current headline
  10576. (setq tags-list
  10577. (if org-use-tag-inheritance
  10578. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  10579. tags)
  10580. org-scanner-tags tags-list)
  10581. (when org-use-tag-inheritance
  10582. (setcdr (car tags-alist)
  10583. (mapcar (lambda (x)
  10584. (setq x (copy-sequence x))
  10585. (org-add-prop-inherited x))
  10586. (cdar tags-alist))))
  10587. (when (and tags org-use-tag-inheritance
  10588. (or (not (eq t org-use-tag-inheritance))
  10589. org-tags-exclude-from-inheritance))
  10590. ;; selective inheritance, remove uninherited ones
  10591. (setcdr (car tags-alist)
  10592. (org-remove-uniherited-tags (cdar tags-alist))))
  10593. (when (and (or (not todo-only)
  10594. (and (member todo org-not-done-keywords)
  10595. (or (not org-agenda-tags-todo-honor-ignore-options)
  10596. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  10597. (let ((case-fold-search t)) (eval matcher))
  10598. (or
  10599. (not (member org-archive-tag tags-list))
  10600. ;; we have an archive tag, should we use this anyway?
  10601. (or (not org-agenda-skip-archived-trees)
  10602. (and (eq action 'agenda) org-agenda-archives-mode))))
  10603. (unless (eq action 'sparse-tree) (org-agenda-skip))
  10604. ;; select this headline
  10605. (cond
  10606. ((eq action 'sparse-tree)
  10607. (and org-highlight-sparse-tree-matches
  10608. (org-get-heading) (match-end 0)
  10609. (org-highlight-new-match
  10610. (match-beginning 0) (match-beginning 1)))
  10611. (org-show-context 'tags-tree))
  10612. ((eq action 'agenda)
  10613. (setq txt (org-format-agenda-item
  10614. ""
  10615. (concat
  10616. (if (eq org-tags-match-list-sublevels 'indented)
  10617. (make-string (1- level) ?.) "")
  10618. (org-get-heading))
  10619. category
  10620. tags-list
  10621. )
  10622. priority (org-get-priority txt))
  10623. (goto-char lspos)
  10624. (setq marker (org-agenda-new-marker))
  10625. (org-add-props txt props
  10626. 'org-marker marker 'org-hd-marker marker 'org-category category
  10627. 'todo-state todo
  10628. 'priority priority 'type "tagsmatch")
  10629. (push txt rtn))
  10630. ((functionp action)
  10631. (setq org-map-continue-from nil)
  10632. (save-excursion
  10633. (setq rtn1 (funcall action))
  10634. (push rtn1 rtn)))
  10635. (t (error "Invalid action")))
  10636. ;; if we are to skip sublevels, jump to end of subtree
  10637. (unless org-tags-match-list-sublevels
  10638. (org-end-of-subtree t)
  10639. (backward-char 1))))
  10640. ;; Get the correct position from where to continue
  10641. (if org-map-continue-from
  10642. (goto-char org-map-continue-from)
  10643. (and (= (point) lspos) (end-of-line 1)))))
  10644. (when (and (eq action 'sparse-tree)
  10645. (not org-sparse-tree-open-archived-trees))
  10646. (org-hide-archived-subtrees (point-min) (point-max)))
  10647. (nreverse rtn)))
  10648. (defun org-remove-uniherited-tags (tags)
  10649. "Remove all tags that are not inherited from the list TAGS."
  10650. (cond
  10651. ((eq org-use-tag-inheritance t)
  10652. (if org-tags-exclude-from-inheritance
  10653. (org-delete-all org-tags-exclude-from-inheritance tags)
  10654. tags))
  10655. ((not org-use-tag-inheritance) nil)
  10656. ((stringp org-use-tag-inheritance)
  10657. (delq nil (mapcar
  10658. (lambda (x)
  10659. (if (and (string-match org-use-tag-inheritance x)
  10660. (not (member x org-tags-exclude-from-inheritance)))
  10661. x nil))
  10662. tags)))
  10663. ((listp org-use-tag-inheritance)
  10664. (delq nil (mapcar
  10665. (lambda (x)
  10666. (if (member x org-use-tag-inheritance) x nil))
  10667. tags)))))
  10668. (defvar todo-only) ;; dynamically scoped
  10669. (defun org-match-sparse-tree (&optional todo-only match)
  10670. "Create a sparse tree according to tags string MATCH.
  10671. MATCH can contain positive and negative selection of tags, like
  10672. \"+WORK+URGENT-WITHBOSS\".
  10673. If optional argument TODO-ONLY is non-nil, only select lines that are
  10674. also TODO lines."
  10675. (interactive "P")
  10676. (org-prepare-agenda-buffers (list (current-buffer)))
  10677. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  10678. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  10679. (defvar org-cached-props nil)
  10680. (defun org-cached-entry-get (pom property)
  10681. (if (or (eq t org-use-property-inheritance)
  10682. (and (stringp org-use-property-inheritance)
  10683. (string-match org-use-property-inheritance property))
  10684. (and (listp org-use-property-inheritance)
  10685. (member property org-use-property-inheritance)))
  10686. ;; Caching is not possible, check it directly
  10687. (org-entry-get pom property 'inherit)
  10688. ;; Get all properties, so that we can do complicated checks easily
  10689. (cdr (assoc property (or org-cached-props
  10690. (setq org-cached-props
  10691. (org-entry-properties pom)))))))
  10692. (defun org-global-tags-completion-table (&optional files)
  10693. "Return the list of all tags in all agenda buffer/files."
  10694. (save-excursion
  10695. (org-uniquify
  10696. (delq nil
  10697. (apply 'append
  10698. (mapcar
  10699. (lambda (file)
  10700. (set-buffer (find-file-noselect file))
  10701. (append (org-get-buffer-tags)
  10702. (mapcar (lambda (x) (if (stringp (car-safe x))
  10703. (list (car-safe x)) nil))
  10704. org-tag-alist)))
  10705. (if (and files (car files))
  10706. files
  10707. (org-agenda-files))))))))
  10708. (defun org-make-tags-matcher (match)
  10709. "Create the TAGS//TODO matcher form for the selection string MATCH."
  10710. ;; todo-only is scoped dynamically into this function, and the function
  10711. ;; may change it if the matcher asks for it.
  10712. (unless match
  10713. ;; Get a new match request, with completion
  10714. (let ((org-last-tags-completion-table
  10715. (org-global-tags-completion-table)))
  10716. (setq match (org-completing-read-no-i
  10717. "Match: " 'org-tags-completion-function nil nil nil
  10718. 'org-tags-history))))
  10719. ;; Parse the string and create a lisp form
  10720. (let ((match0 match)
  10721. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  10722. minus tag mm
  10723. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  10724. orterms term orlist re-p str-p level-p level-op time-p
  10725. prop-p pn pv po cat-p gv rest)
  10726. (if (string-match "/+" match)
  10727. ;; match contains also a todo-matching request
  10728. (progn
  10729. (setq tagsmatch (substring match 0 (match-beginning 0))
  10730. todomatch (substring match (match-end 0)))
  10731. (if (string-match "^!" todomatch)
  10732. (setq todo-only t todomatch (substring todomatch 1)))
  10733. (if (string-match "^\\s-*$" todomatch)
  10734. (setq todomatch nil)))
  10735. ;; only matching tags
  10736. (setq tagsmatch match todomatch nil))
  10737. ;; Make the tags matcher
  10738. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  10739. (setq tagsmatcher t)
  10740. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  10741. (while (setq term (pop orterms))
  10742. (while (and (equal (substring term -1) "\\") orterms)
  10743. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  10744. (while (string-match re term)
  10745. (setq rest (substring term (match-end 0))
  10746. minus (and (match-end 1)
  10747. (equal (match-string 1 term) "-"))
  10748. tag (match-string 2 term)
  10749. re-p (equal (string-to-char tag) ?{)
  10750. level-p (match-end 4)
  10751. prop-p (match-end 5)
  10752. mm (cond
  10753. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  10754. (level-p
  10755. (setq level-op (org-op-to-function (match-string 3 term)))
  10756. `(,level-op level ,(string-to-number
  10757. (match-string 4 term))))
  10758. (prop-p
  10759. (setq pn (match-string 5 term)
  10760. po (match-string 6 term)
  10761. pv (match-string 7 term)
  10762. cat-p (equal pn "CATEGORY")
  10763. re-p (equal (string-to-char pv) ?{)
  10764. str-p (equal (string-to-char pv) ?\")
  10765. time-p (save-match-data
  10766. (string-match "^\"[[<].*[]>]\"$" pv))
  10767. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  10768. (if time-p (setq pv (org-matcher-time pv)))
  10769. (setq po (org-op-to-function po (if time-p 'time str-p)))
  10770. (cond
  10771. ((equal pn "CATEGORY")
  10772. (setq gv '(get-text-property (point) 'org-category)))
  10773. ((equal pn "TODO")
  10774. (setq gv 'todo))
  10775. (t
  10776. (setq gv `(org-cached-entry-get nil ,pn))))
  10777. (if re-p
  10778. (if (eq po 'org<>)
  10779. `(not (string-match ,pv (or ,gv "")))
  10780. `(string-match ,pv (or ,gv "")))
  10781. (if str-p
  10782. `(,po (or ,gv "") ,pv)
  10783. `(,po (string-to-number (or ,gv ""))
  10784. ,(string-to-number pv) ))))
  10785. (t `(member ,tag tags-list)))
  10786. mm (if minus (list 'not mm) mm)
  10787. term rest)
  10788. (push mm tagsmatcher))
  10789. (push (if (> (length tagsmatcher) 1)
  10790. (cons 'and tagsmatcher)
  10791. (car tagsmatcher))
  10792. orlist)
  10793. (setq tagsmatcher nil))
  10794. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  10795. (setq tagsmatcher
  10796. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  10797. ;; Make the todo matcher
  10798. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  10799. (setq todomatcher t)
  10800. (setq orterms (org-split-string todomatch "|") orlist nil)
  10801. (while (setq term (pop orterms))
  10802. (while (string-match re term)
  10803. (setq minus (and (match-end 1)
  10804. (equal (match-string 1 term) "-"))
  10805. kwd (match-string 2 term)
  10806. re-p (equal (string-to-char kwd) ?{)
  10807. term (substring term (match-end 0))
  10808. mm (if re-p
  10809. `(string-match ,(substring kwd 1 -1) todo)
  10810. (list 'equal 'todo kwd))
  10811. mm (if minus (list 'not mm) mm))
  10812. (push mm todomatcher))
  10813. (push (if (> (length todomatcher) 1)
  10814. (cons 'and todomatcher)
  10815. (car todomatcher))
  10816. orlist)
  10817. (setq todomatcher nil))
  10818. (setq todomatcher (if (> (length orlist) 1)
  10819. (cons 'or orlist) (car orlist))))
  10820. ;; Return the string and lisp forms of the matcher
  10821. (setq matcher (if todomatcher
  10822. (list 'and tagsmatcher todomatcher)
  10823. tagsmatcher))
  10824. (cons match0 matcher)))
  10825. (defun org-op-to-function (op &optional stringp)
  10826. "Turn an operator into the appropriate function."
  10827. (setq op
  10828. (cond
  10829. ((equal op "<" ) '(< string< org-time<))
  10830. ((equal op ">" ) '(> org-string> org-time>))
  10831. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  10832. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  10833. ((member op '("=" "==")) '(= string= org-time=))
  10834. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  10835. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  10836. (defun org<> (a b) (not (= a b)))
  10837. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  10838. (defun org-string>= (a b) (not (string< a b)))
  10839. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  10840. (defun org-string<> (a b) (not (string= a b)))
  10841. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  10842. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  10843. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  10844. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  10845. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  10846. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  10847. (defun org-2ft (s)
  10848. "Convert S to a floating point time.
  10849. If S is already a number, just return it. If it is a string, parse
  10850. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  10851. (cond
  10852. ((numberp s) s)
  10853. ((stringp s)
  10854. (condition-case nil
  10855. (float-time (apply 'encode-time (org-parse-time-string s)))
  10856. (error 0.)))
  10857. (t 0.)))
  10858. (defun org-time-today ()
  10859. "Time in seconds today at 0:00.
  10860. Returns the float number of seconds since the beginning of the
  10861. epoch to the beginning of today (00:00)."
  10862. (float-time (apply 'encode-time
  10863. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  10864. (defun org-matcher-time (s)
  10865. "Interpret a time comparison value."
  10866. (save-match-data
  10867. (cond
  10868. ((string= s "<now>") (float-time))
  10869. ((string= s "<today>") (org-time-today))
  10870. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  10871. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  10872. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  10873. (+ (org-time-today)
  10874. (* (string-to-number (match-string 1 s))
  10875. (cdr (assoc (match-string 2 s)
  10876. '(("d" . 86400.0) ("w" . 604800.0)
  10877. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  10878. (t (org-2ft s)))))
  10879. (defun org-match-any-p (re list)
  10880. "Does re match any element of list?"
  10881. (setq list (mapcar (lambda (x) (string-match re x)) list))
  10882. (delq nil list))
  10883. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  10884. (defvar org-tags-overlay (org-make-overlay 1 1))
  10885. (org-detach-overlay org-tags-overlay)
  10886. (defun org-get-local-tags-at (&optional pos)
  10887. "Get a list of tags defined in the current headline."
  10888. (org-get-tags-at pos 'local))
  10889. (defun org-get-local-tags ()
  10890. "Get a list of tags defined in the current headline."
  10891. (org-get-tags-at nil 'local))
  10892. (defun org-get-tags-at (&optional pos local)
  10893. "Get a list of all headline tags applicable at POS.
  10894. POS defaults to point. If tags are inherited, the list contains
  10895. the targets in the same sequence as the headlines appear, i.e.
  10896. the tags of the current headline come last.
  10897. When LOCAL is non-nil, only return tags from the current headline,
  10898. ignore inherited ones."
  10899. (interactive)
  10900. (if (and org-trust-scanner-tags
  10901. (or (not pos) (equal pos (point)))
  10902. (not local))
  10903. org-scanner-tags
  10904. (let (tags ltags lastpos parent)
  10905. (save-excursion
  10906. (save-restriction
  10907. (widen)
  10908. (goto-char (or pos (point)))
  10909. (save-match-data
  10910. (catch 'done
  10911. (condition-case nil
  10912. (progn
  10913. (org-back-to-heading t)
  10914. (while (not (equal lastpos (point)))
  10915. (setq lastpos (point))
  10916. (when (looking-at
  10917. (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  10918. (setq ltags (org-split-string
  10919. (org-match-string-no-properties 1) ":"))
  10920. (when parent
  10921. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  10922. (setq tags (append
  10923. (if parent
  10924. (org-remove-uniherited-tags ltags)
  10925. ltags)
  10926. tags)))
  10927. (or org-use-tag-inheritance (throw 'done t))
  10928. (if local (throw 'done t))
  10929. (or (org-up-heading-safe) (error nil))
  10930. (setq parent t)))
  10931. (error nil)))))
  10932. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  10933. (defun org-add-prop-inherited (s)
  10934. (add-text-properties 0 (length s) '(inherited t) s)
  10935. s)
  10936. (defun org-toggle-tag (tag &optional onoff)
  10937. "Toggle the tag TAG for the current line.
  10938. If ONOFF is `on' or `off', don't toggle but set to this state."
  10939. (let (res current)
  10940. (save-excursion
  10941. (org-back-to-heading t)
  10942. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  10943. (point-at-eol) t)
  10944. (progn
  10945. (setq current (match-string 1))
  10946. (replace-match ""))
  10947. (setq current ""))
  10948. (setq current (nreverse (org-split-string current ":")))
  10949. (cond
  10950. ((eq onoff 'on)
  10951. (setq res t)
  10952. (or (member tag current) (push tag current)))
  10953. ((eq onoff 'off)
  10954. (or (not (member tag current)) (setq current (delete tag current))))
  10955. (t (if (member tag current)
  10956. (setq current (delete tag current))
  10957. (setq res t)
  10958. (push tag current))))
  10959. (end-of-line 1)
  10960. (if current
  10961. (progn
  10962. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  10963. (org-set-tags nil t))
  10964. (delete-horizontal-space))
  10965. (run-hooks 'org-after-tags-change-hook))
  10966. res))
  10967. (defun org-align-tags-here (to-col)
  10968. ;; Assumes that this is a headline
  10969. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  10970. (beginning-of-line 1)
  10971. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  10972. (< pos (match-beginning 2)))
  10973. (progn
  10974. (setq tags-l (- (match-end 2) (match-beginning 2)))
  10975. (goto-char (match-beginning 1))
  10976. (insert " ")
  10977. (delete-region (point) (1+ (match-beginning 2)))
  10978. (setq ncol (max (1+ (current-column))
  10979. (1+ col)
  10980. (if (> to-col 0)
  10981. to-col
  10982. (- (abs to-col) tags-l))))
  10983. (setq p (point))
  10984. (insert (make-string (- ncol (current-column)) ?\ ))
  10985. (setq ncol (current-column))
  10986. (when indent-tabs-mode (tabify p (point-at-eol)))
  10987. (org-move-to-column (min ncol col) t))
  10988. (goto-char pos))))
  10989. (defun org-set-tags-command (&optional arg just-align)
  10990. "Call the set-tags command for the current entry."
  10991. (interactive "P")
  10992. (if (org-on-heading-p)
  10993. (org-set-tags arg just-align)
  10994. (save-excursion
  10995. (org-back-to-heading t)
  10996. (org-set-tags arg just-align))))
  10997. (defun org-set-tags-to (data)
  10998. "Set the tags of the current entry to DATA, replacing the current tags.
  10999. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11000. If DATA is nil or the empty string, any tags will be removed."
  11001. (interactive "sTags: ")
  11002. (setq data
  11003. (cond
  11004. ((eq data nil) "")
  11005. ((equal data "") "")
  11006. ((stringp data)
  11007. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11008. ":"))
  11009. ((listp data)
  11010. (concat ":" (mapconcat 'identity data ":") ":"))
  11011. (t nil)))
  11012. (when data
  11013. (save-excursion
  11014. (org-back-to-heading t)
  11015. (when (looking-at org-complex-heading-regexp)
  11016. (if (match-end 5)
  11017. (progn
  11018. (goto-char (match-beginning 5))
  11019. (insert data)
  11020. (delete-region (point) (point-at-eol))
  11021. (org-set-tags nil 'align))
  11022. (goto-char (point-at-eol))
  11023. (insert " " data)
  11024. (org-set-tags nil 'align)))
  11025. (beginning-of-line 1)
  11026. (if (looking-at ".*?\\([ \t]+\\)$")
  11027. (delete-region (match-beginning 1) (match-end 1))))))
  11028. (defun org-set-tags (&optional arg just-align)
  11029. "Set the tags for the current headline.
  11030. With prefix ARG, realign all tags in headings in the current buffer."
  11031. (interactive "P")
  11032. (let* ((re (concat "^" outline-regexp))
  11033. (current (org-get-tags-string))
  11034. (col (current-column))
  11035. (org-setting-tags t)
  11036. table current-tags inherited-tags ; computed below when needed
  11037. tags p0 c0 c1 rpl)
  11038. (if arg
  11039. (save-excursion
  11040. (goto-char (point-min))
  11041. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11042. (while (re-search-forward re nil t)
  11043. (org-set-tags nil t)
  11044. (end-of-line 1)))
  11045. (message "All tags realigned to column %d" org-tags-column))
  11046. (if just-align
  11047. (setq tags current)
  11048. ;; Get a new set of tags from the user
  11049. (save-excursion
  11050. (setq table (append org-tag-persistent-alist
  11051. (or org-tag-alist (org-get-buffer-tags))
  11052. (and org-complete-tags-always-offer-all-agenda-tags
  11053. (org-global-tags-completion-table (org-agenda-files))))
  11054. org-last-tags-completion-table table
  11055. current-tags (org-split-string current ":")
  11056. inherited-tags (nreverse
  11057. (nthcdr (length current-tags)
  11058. (nreverse (org-get-tags-at))))
  11059. tags
  11060. (if (or (eq t org-use-fast-tag-selection)
  11061. (and org-use-fast-tag-selection
  11062. (delq nil (mapcar 'cdr table))))
  11063. (org-fast-tag-selection
  11064. current-tags inherited-tags table
  11065. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  11066. (let ((org-add-colon-after-tag-completion t))
  11067. (org-trim
  11068. (org-without-partial-completion
  11069. (org-icompleting-read "Tags: " 'org-tags-completion-function
  11070. nil nil current 'org-tags-history)))))))
  11071. (while (string-match "[-+&]+" tags)
  11072. ;; No boolean logic, just a list
  11073. (setq tags (replace-match ":" t t tags))))
  11074. (if org-tags-sort-function
  11075. (setq tags (mapconcat 'identity
  11076. (sort (org-split-string tags (org-re "[^[:alnum:]_@]+"))
  11077. org-tags-sort-function) ":")))
  11078. (if (string-match "\\`[\t ]*\\'" tags)
  11079. (setq tags "")
  11080. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11081. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11082. ;; Insert new tags at the correct column
  11083. (beginning-of-line 1)
  11084. (cond
  11085. ((and (equal current "") (equal tags "")))
  11086. ((re-search-forward
  11087. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11088. (point-at-eol) t)
  11089. (if (equal tags "")
  11090. (setq rpl "")
  11091. (goto-char (match-beginning 0))
  11092. (setq c0 (current-column) p0 (if (equal (char-before) ?*)
  11093. (1+ (point)) (point))
  11094. c1 (max (1+ c0) (if (> org-tags-column 0)
  11095. org-tags-column
  11096. (- (- org-tags-column) (length tags))))
  11097. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11098. (replace-match rpl t t)
  11099. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11100. tags)
  11101. (t (error "Tags alignment failed")))
  11102. (org-move-to-column col)
  11103. (unless just-align
  11104. (run-hooks 'org-after-tags-change-hook)))))
  11105. (defun org-change-tag-in-region (beg end tag off)
  11106. "Add or remove TAG for each entry in the region.
  11107. This works in the agenda, and also in an org-mode buffer."
  11108. (interactive
  11109. (list (region-beginning) (region-end)
  11110. (let ((org-last-tags-completion-table
  11111. (if (org-mode-p)
  11112. (org-get-buffer-tags)
  11113. (org-global-tags-completion-table))))
  11114. (org-icompleting-read
  11115. "Tag: " 'org-tags-completion-function nil nil nil
  11116. 'org-tags-history))
  11117. (progn
  11118. (message "[s]et or [r]emove? ")
  11119. (equal (read-char-exclusive) ?r))))
  11120. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11121. (let ((agendap (equal major-mode 'org-agenda-mode))
  11122. l1 l2 m buf pos newhead (cnt 0))
  11123. (goto-char end)
  11124. (setq l2 (1- (org-current-line)))
  11125. (goto-char beg)
  11126. (setq l1 (org-current-line))
  11127. (loop for l from l1 to l2 do
  11128. (org-goto-line l)
  11129. (setq m (get-text-property (point) 'org-hd-marker))
  11130. (when (or (and (org-mode-p) (org-on-heading-p))
  11131. (and agendap m))
  11132. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11133. pos (if agendap m (point)))
  11134. (with-current-buffer buf
  11135. (save-excursion
  11136. (save-restriction
  11137. (goto-char pos)
  11138. (setq cnt (1+ cnt))
  11139. (org-toggle-tag tag (if off 'off 'on))
  11140. (setq newhead (org-get-heading)))))
  11141. (and agendap (org-agenda-change-all-lines newhead m))))
  11142. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11143. (defun org-tags-completion-function (string predicate &optional flag)
  11144. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11145. (confirm (lambda (x) (stringp (car x)))))
  11146. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  11147. (setq s1 (match-string 1 string)
  11148. s2 (match-string 2 string))
  11149. (setq s1 "" s2 string))
  11150. (cond
  11151. ((eq flag nil)
  11152. ;; try completion
  11153. (setq rtn (try-completion s2 ctable confirm))
  11154. (if (stringp rtn)
  11155. (setq rtn
  11156. (concat s1 s2 (substring rtn (length s2))
  11157. (if (and org-add-colon-after-tag-completion
  11158. (assoc rtn ctable))
  11159. ":" ""))))
  11160. rtn)
  11161. ((eq flag t)
  11162. ;; all-completions
  11163. (all-completions s2 ctable confirm)
  11164. )
  11165. ((eq flag 'lambda)
  11166. ;; exact match?
  11167. (assoc s2 ctable)))
  11168. ))
  11169. (defun org-fast-tag-insert (kwd tags face &optional end)
  11170. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11171. (insert (format "%-12s" (concat kwd ":"))
  11172. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11173. (or end "")))
  11174. (defun org-fast-tag-show-exit (flag)
  11175. (save-excursion
  11176. (org-goto-line 3)
  11177. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11178. (replace-match ""))
  11179. (when flag
  11180. (end-of-line 1)
  11181. (org-move-to-column (- (window-width) 19) t)
  11182. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11183. (defun org-set-current-tags-overlay (current prefix)
  11184. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11185. (if (featurep 'xemacs)
  11186. (org-overlay-display org-tags-overlay (concat prefix s)
  11187. 'secondary-selection)
  11188. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11189. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11190. (defvar org-last-tag-selection-key nil)
  11191. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11192. "Fast tag selection with single keys.
  11193. CURRENT is the current list of tags in the headline, INHERITED is the
  11194. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11195. possibly with grouping information. TODO-TABLE is a similar table with
  11196. TODO keywords, should these have keys assigned to them.
  11197. If the keys are nil, a-z are automatically assigned.
  11198. Returns the new tags string, or nil to not change the current settings."
  11199. (let* ((fulltable (append table todo-table))
  11200. (maxlen (apply 'max (mapcar
  11201. (lambda (x)
  11202. (if (stringp (car x)) (string-width (car x)) 0))
  11203. fulltable)))
  11204. (buf (current-buffer))
  11205. (expert (eq org-fast-tag-selection-single-key 'expert))
  11206. (buffer-tags nil)
  11207. (fwidth (+ maxlen 3 1 3))
  11208. (ncol (/ (- (window-width) 4) fwidth))
  11209. (i-face 'org-done)
  11210. (c-face 'org-todo)
  11211. tg cnt e c char c1 c2 ntable tbl rtn
  11212. ov-start ov-end ov-prefix
  11213. (exit-after-next org-fast-tag-selection-single-key)
  11214. (done-keywords org-done-keywords)
  11215. groups ingroup)
  11216. (save-excursion
  11217. (beginning-of-line 1)
  11218. (if (looking-at
  11219. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11220. (setq ov-start (match-beginning 1)
  11221. ov-end (match-end 1)
  11222. ov-prefix "")
  11223. (setq ov-start (1- (point-at-eol))
  11224. ov-end (1+ ov-start))
  11225. (skip-chars-forward "^\n\r")
  11226. (setq ov-prefix
  11227. (concat
  11228. (buffer-substring (1- (point)) (point))
  11229. (if (> (current-column) org-tags-column)
  11230. " "
  11231. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11232. (org-move-overlay org-tags-overlay ov-start ov-end)
  11233. (save-window-excursion
  11234. (if expert
  11235. (set-buffer (get-buffer-create " *Org tags*"))
  11236. (delete-other-windows)
  11237. (split-window-vertically)
  11238. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11239. (erase-buffer)
  11240. (org-set-local 'org-done-keywords done-keywords)
  11241. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11242. (org-fast-tag-insert "Current" current c-face "\n\n")
  11243. (org-fast-tag-show-exit exit-after-next)
  11244. (org-set-current-tags-overlay current ov-prefix)
  11245. (setq tbl fulltable char ?a cnt 0)
  11246. (while (setq e (pop tbl))
  11247. (cond
  11248. ((equal (car e) :startgroup)
  11249. (push '() groups) (setq ingroup t)
  11250. (when (not (= cnt 0))
  11251. (setq cnt 0)
  11252. (insert "\n"))
  11253. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11254. ((equal (car e) :endgroup)
  11255. (setq ingroup nil cnt 0)
  11256. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  11257. ((equal e '(:newline))
  11258. (when (not (= cnt 0))
  11259. (setq cnt 0)
  11260. (insert "\n")
  11261. (setq e (car tbl))
  11262. (while (equal (car tbl) '(:newline))
  11263. (insert "\n")
  11264. (setq tbl (cdr tbl)))))
  11265. (t
  11266. (setq tg (copy-sequence (car e)) c2 nil)
  11267. (if (cdr e)
  11268. (setq c (cdr e))
  11269. ;; automatically assign a character.
  11270. (setq c1 (string-to-char
  11271. (downcase (substring
  11272. tg (if (= (string-to-char tg) ?@) 1 0)))))
  11273. (if (or (rassoc c1 ntable) (rassoc c1 table))
  11274. (while (or (rassoc char ntable) (rassoc char table))
  11275. (setq char (1+ char)))
  11276. (setq c2 c1))
  11277. (setq c (or c2 char)))
  11278. (if ingroup (push tg (car groups)))
  11279. (setq tg (org-add-props tg nil 'face
  11280. (cond
  11281. ((not (assoc tg table))
  11282. (org-get-todo-face tg))
  11283. ((member tg current) c-face)
  11284. ((member tg inherited) i-face)
  11285. (t nil))))
  11286. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11287. (insert "[" c "] " tg (make-string
  11288. (- fwidth 4 (length tg)) ?\ ))
  11289. (push (cons tg c) ntable)
  11290. (when (= (setq cnt (1+ cnt)) ncol)
  11291. (insert "\n")
  11292. (if ingroup (insert " "))
  11293. (setq cnt 0)))))
  11294. (setq ntable (nreverse ntable))
  11295. (insert "\n")
  11296. (goto-char (point-min))
  11297. (if (not expert) (org-fit-window-to-buffer))
  11298. (setq rtn
  11299. (catch 'exit
  11300. (while t
  11301. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  11302. (if (not groups) "no " "")
  11303. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  11304. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11305. (setq org-last-tag-selection-key c)
  11306. (cond
  11307. ((= c ?\r) (throw 'exit t))
  11308. ((= c ?!)
  11309. (setq groups (not groups))
  11310. (goto-char (point-min))
  11311. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  11312. ((= c ?\C-c)
  11313. (if (not expert)
  11314. (org-fast-tag-show-exit
  11315. (setq exit-after-next (not exit-after-next)))
  11316. (setq expert nil)
  11317. (delete-other-windows)
  11318. (split-window-vertically)
  11319. (org-switch-to-buffer-other-window " *Org tags*")
  11320. (org-fit-window-to-buffer)))
  11321. ((or (= c ?\C-g)
  11322. (and (= c ?q) (not (rassoc c ntable))))
  11323. (org-detach-overlay org-tags-overlay)
  11324. (setq quit-flag t))
  11325. ((= c ?\ )
  11326. (setq current nil)
  11327. (if exit-after-next (setq exit-after-next 'now)))
  11328. ((= c ?\t)
  11329. (condition-case nil
  11330. (setq tg (org-icompleting-read
  11331. "Tag: "
  11332. (or buffer-tags
  11333. (with-current-buffer buf
  11334. (org-get-buffer-tags)))))
  11335. (quit (setq tg "")))
  11336. (when (string-match "\\S-" tg)
  11337. (add-to-list 'buffer-tags (list tg))
  11338. (if (member tg current)
  11339. (setq current (delete tg current))
  11340. (push tg current)))
  11341. (if exit-after-next (setq exit-after-next 'now)))
  11342. ((setq e (rassoc c todo-table) tg (car e))
  11343. (with-current-buffer buf
  11344. (save-excursion (org-todo tg)))
  11345. (if exit-after-next (setq exit-after-next 'now)))
  11346. ((setq e (rassoc c ntable) tg (car e))
  11347. (if (member tg current)
  11348. (setq current (delete tg current))
  11349. (loop for g in groups do
  11350. (if (member tg g)
  11351. (mapc (lambda (x)
  11352. (setq current (delete x current)))
  11353. g)))
  11354. (push tg current))
  11355. (if exit-after-next (setq exit-after-next 'now))))
  11356. ;; Create a sorted list
  11357. (setq current
  11358. (sort current
  11359. (lambda (a b)
  11360. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  11361. (if (eq exit-after-next 'now) (throw 'exit t))
  11362. (goto-char (point-min))
  11363. (beginning-of-line 2)
  11364. (delete-region (point) (point-at-eol))
  11365. (org-fast-tag-insert "Current" current c-face)
  11366. (org-set-current-tags-overlay current ov-prefix)
  11367. (while (re-search-forward
  11368. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  11369. (setq tg (match-string 1))
  11370. (add-text-properties
  11371. (match-beginning 1) (match-end 1)
  11372. (list 'face
  11373. (cond
  11374. ((member tg current) c-face)
  11375. ((member tg inherited) i-face)
  11376. (t (get-text-property (match-beginning 1) 'face))))))
  11377. (goto-char (point-min)))))
  11378. (org-detach-overlay org-tags-overlay)
  11379. (if rtn
  11380. (mapconcat 'identity current ":")
  11381. nil))))
  11382. (defun org-get-tags-string ()
  11383. "Get the TAGS string in the current headline."
  11384. (unless (org-on-heading-p t)
  11385. (error "Not on a heading"))
  11386. (save-excursion
  11387. (beginning-of-line 1)
  11388. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  11389. (org-match-string-no-properties 1)
  11390. "")))
  11391. (defun org-get-tags ()
  11392. "Get the list of tags specified in the current headline."
  11393. (org-split-string (org-get-tags-string) ":"))
  11394. (defun org-get-buffer-tags ()
  11395. "Get a table of all tags used in the buffer, for completion."
  11396. (let (tags)
  11397. (save-excursion
  11398. (goto-char (point-min))
  11399. (while (re-search-forward
  11400. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  11401. (when (equal (char-after (point-at-bol 0)) ?*)
  11402. (mapc (lambda (x) (add-to-list 'tags x))
  11403. (org-split-string (org-match-string-no-properties 1) ":")))))
  11404. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  11405. (mapcar 'list tags)))
  11406. ;;;; The mapping API
  11407. ;;;###autoload
  11408. (defun org-map-entries (func &optional match scope &rest skip)
  11409. "Call FUNC at each headline selected by MATCH in SCOPE.
  11410. FUNC is a function or a lisp form. The function will be called without
  11411. arguments, with the cursor positioned at the beginning of the headline.
  11412. The return values of all calls to the function will be collected and
  11413. returned as a list.
  11414. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  11415. does not need to preserve point. After evaluation, the cursor will be
  11416. moved to the end of the line (presumably of the headline of the
  11417. processed entry) and search continues from there. Under some
  11418. circumstances, this may not produce the wanted results. For example,
  11419. if you have removed (e.g. archived) the current (sub)tree it could
  11420. mean that the next entry will be skipped entirely. In such cases, you
  11421. can specify the position from where search should continue by making
  11422. FUNC set the variable `org-map-continue-from' to the desired buffer
  11423. position.
  11424. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  11425. Only headlines that are matched by this query will be considered during
  11426. the iteration. When MATCH is nil or t, all headlines will be
  11427. visited by the iteration.
  11428. SCOPE determines the scope of this command. It can be any of:
  11429. nil The current buffer, respecting the restriction if any
  11430. tree The subtree started with the entry at point
  11431. file The current buffer, without restriction
  11432. file-with-archives
  11433. The current buffer, and any archives associated with it
  11434. agenda All agenda files
  11435. agenda-with-archives
  11436. All agenda files with any archive files associated with them
  11437. \(file1 file2 ...)
  11438. If this is a list, all files in the list will be scanned
  11439. The remaining args are treated as settings for the skipping facilities of
  11440. the scanner. The following items can be given here:
  11441. archive skip trees with the archive tag.
  11442. comment skip trees with the COMMENT keyword
  11443. function or Emacs Lisp form:
  11444. will be used as value for `org-agenda-skip-function', so whenever
  11445. the function returns t, FUNC will not be called for that
  11446. entry and search will continue from the point where the
  11447. function leaves it.
  11448. If your function needs to retrieve the tags including inherited tags
  11449. at the *current* entry, you can use the value of the variable
  11450. `org-scanner-tags' which will be much faster than getting the value
  11451. with `org-get-tags-at'. If your function gets properties with
  11452. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  11453. to t around the call to `org-entry-properties' to get the same speedup.
  11454. Note that if your function moves around to retrieve tags and properties at
  11455. a *different* entry, you cannot use these techniques."
  11456. (let* ((org-agenda-archives-mode nil) ; just to make sure
  11457. (org-agenda-skip-archived-trees (memq 'archive skip))
  11458. (org-agenda-skip-comment-trees (memq 'comment skip))
  11459. (org-agenda-skip-function
  11460. (car (org-delete-all '(comment archive) skip)))
  11461. (org-tags-match-list-sublevels t)
  11462. matcher file res
  11463. org-todo-keywords-for-agenda
  11464. org-done-keywords-for-agenda
  11465. org-todo-keyword-alist-for-agenda
  11466. org-drawers-for-agenda
  11467. org-tag-alist-for-agenda)
  11468. (cond
  11469. ((eq match t) (setq matcher t))
  11470. ((eq match nil) (setq matcher t))
  11471. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  11472. (save-excursion
  11473. (save-restriction
  11474. (when (eq scope 'tree)
  11475. (org-back-to-heading t)
  11476. (org-narrow-to-subtree)
  11477. (setq scope nil))
  11478. (if (not scope)
  11479. (progn
  11480. (org-prepare-agenda-buffers
  11481. (list (buffer-file-name (current-buffer))))
  11482. (setq res (org-scan-tags func matcher)))
  11483. ;; Get the right scope
  11484. (cond
  11485. ((and scope (listp scope) (symbolp (car scope)))
  11486. (setq scope (eval scope)))
  11487. ((eq scope 'agenda)
  11488. (setq scope (org-agenda-files t)))
  11489. ((eq scope 'agenda-with-archives)
  11490. (setq scope (org-agenda-files t))
  11491. (setq scope (org-add-archive-files scope)))
  11492. ((eq scope 'file)
  11493. (setq scope (list (buffer-file-name))))
  11494. ((eq scope 'file-with-archives)
  11495. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  11496. (org-prepare-agenda-buffers scope)
  11497. (while (setq file (pop scope))
  11498. (with-current-buffer (org-find-base-buffer-visiting file)
  11499. (save-excursion
  11500. (save-restriction
  11501. (widen)
  11502. (goto-char (point-min))
  11503. (setq res (append res (org-scan-tags func matcher))))))))))
  11504. res))
  11505. ;;;; Properties
  11506. ;;; Setting and retrieving properties
  11507. (defconst org-special-properties
  11508. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  11509. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED")
  11510. "The special properties valid in Org-mode.
  11511. These are properties that are not defined in the property drawer,
  11512. but in some other way.")
  11513. (defconst org-default-properties
  11514. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  11515. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  11516. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  11517. "EXPORT_FILE_NAME" "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  11518. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER"
  11519. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  11520. "Some properties that are used by Org-mode for various purposes.
  11521. Being in this list makes sure that they are offered for completion.")
  11522. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  11523. "Regular expression matching the first line of a property drawer.")
  11524. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  11525. "Regular expression matching the last line of a property drawer.")
  11526. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  11527. "Regular expression matching the first line of a property drawer.")
  11528. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  11529. "Regular expression matching the first line of a property drawer.")
  11530. (defconst org-property-drawer-re
  11531. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  11532. org-property-end-re "\\)\n?")
  11533. "Matches an entire property drawer.")
  11534. (defconst org-clock-drawer-re
  11535. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  11536. org-property-end-re "\\)\n?")
  11537. "Matches an entire clock drawer.")
  11538. (defun org-property-action ()
  11539. "Do an action on properties."
  11540. (interactive)
  11541. (let (c)
  11542. (org-at-property-p)
  11543. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  11544. (setq c (read-char-exclusive))
  11545. (cond
  11546. ((equal c ?s)
  11547. (call-interactively 'org-set-property))
  11548. ((equal c ?d)
  11549. (call-interactively 'org-delete-property))
  11550. ((equal c ?D)
  11551. (call-interactively 'org-delete-property-globally))
  11552. ((equal c ?c)
  11553. (call-interactively 'org-compute-property-at-point))
  11554. (t (error "No such property action %c" c)))))
  11555. (defun org-set-effort (&optional value)
  11556. "Set the effort property of the current entry.
  11557. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  11558. allowed value."
  11559. (interactive "P")
  11560. (if (equal value 0) (setq value 10))
  11561. (let* ((completion-ignore-case t)
  11562. (prop org-effort-property)
  11563. (cur (org-entry-get nil prop))
  11564. (allowed (org-property-get-allowed-values nil prop 'table))
  11565. (existing (mapcar 'list (org-property-values prop)))
  11566. rpl
  11567. (val (cond
  11568. ((stringp value) value)
  11569. ((and allowed (integerp value))
  11570. (or (car (nth (1- value) allowed))
  11571. (car (org-last allowed))))
  11572. (allowed
  11573. (message "Select 1-9,0, [RET%s]: %s"
  11574. (if cur (concat "=" cur) "")
  11575. (mapconcat 'car allowed " "))
  11576. (setq rpl (read-char-exclusive))
  11577. (if (equal rpl ?\r)
  11578. cur
  11579. (setq rpl (- rpl ?0))
  11580. (if (equal rpl 0) (setq rpl 10))
  11581. (if (and (> rpl 0) (<= rpl (length allowed)))
  11582. (car (nth (1- rpl) allowed))
  11583. (org-completing-read "Effort: " allowed nil))))
  11584. (t
  11585. (let (org-completion-use-ido org-completion-use-iswitchb)
  11586. (org-completing-read
  11587. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  11588. (concat "[" cur "]") "")
  11589. ": ")
  11590. existing nil nil "" nil cur))))))
  11591. (unless (equal (org-entry-get nil prop) val)
  11592. (org-entry-put nil prop val))
  11593. (message "%s is now %s" prop val)))
  11594. (defun org-at-property-p ()
  11595. "Is cursor inside a property drawer?"
  11596. (save-excursion
  11597. (beginning-of-line 1)
  11598. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  11599. (let ((match (match-data)) ;; Keep match-data for use by calling
  11600. (p (point)) ;; procedures.
  11601. (range (unless (org-before-first-heading-p)
  11602. (org-get-property-block))))
  11603. (prog1 (and range (<= (car range) p) (< p (cdr range)))
  11604. (set-match-data match))))))
  11605. (defun org-get-property-block (&optional beg end force)
  11606. "Return the (beg . end) range of the body of the property drawer.
  11607. BEG and END can be beginning and end of subtree, if not given
  11608. they will be found.
  11609. If the drawer does not exist and FORCE is non-nil, create the drawer."
  11610. (catch 'exit
  11611. (save-excursion
  11612. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  11613. (end (or end (progn (outline-next-heading) (point)))))
  11614. (goto-char beg)
  11615. (if (re-search-forward org-property-start-re end t)
  11616. (setq beg (1+ (match-end 0)))
  11617. (if force
  11618. (save-excursion
  11619. (org-insert-property-drawer)
  11620. (setq end (progn (outline-next-heading) (point))))
  11621. (throw 'exit nil))
  11622. (goto-char beg)
  11623. (if (re-search-forward org-property-start-re end t)
  11624. (setq beg (1+ (match-end 0)))))
  11625. (if (re-search-forward org-property-end-re end t)
  11626. (setq end (match-beginning 0))
  11627. (or force (throw 'exit nil))
  11628. (goto-char beg)
  11629. (setq end beg)
  11630. (org-indent-line-function)
  11631. (insert ":END:\n"))
  11632. (cons beg end)))))
  11633. (defun org-entry-properties (&optional pom which specific)
  11634. "Get all properties of the entry at point-or-marker POM.
  11635. This includes the TODO keyword, the tags, time strings for deadline,
  11636. scheduled, and clocking, and any additional properties defined in the
  11637. entry. The return value is an alist, keys may occur multiple times
  11638. if the property key was used several times.
  11639. POM may also be nil, in which case the current entry is used.
  11640. If WHICH is nil or `all', get all properties. If WHICH is
  11641. `special' or `standard', only get that subclass. If WHICH
  11642. is a string only get exactly this property. Specific can be a string, the
  11643. specific property we are interested in. Specifying it can speed
  11644. things up because then unnecessary parsing is avoided."
  11645. (setq which (or which 'all))
  11646. (org-with-point-at pom
  11647. (let ((clockstr (substring org-clock-string 0 -1))
  11648. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  11649. (case-fold-search nil)
  11650. beg end range props sum-props key value string clocksum)
  11651. (save-excursion
  11652. (when (condition-case nil
  11653. (and (org-mode-p) (org-back-to-heading t))
  11654. (error nil))
  11655. (setq beg (point))
  11656. (setq sum-props (get-text-property (point) 'org-summaries))
  11657. (setq clocksum (get-text-property (point) :org-clock-minutes))
  11658. (outline-next-heading)
  11659. (setq end (point))
  11660. (when (memq which '(all special))
  11661. ;; Get the special properties, like TODO and tags
  11662. (goto-char beg)
  11663. (when (and (or (not specific) (string= specific "TODO"))
  11664. (looking-at org-todo-line-regexp) (match-end 2))
  11665. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  11666. (when (and (or (not specific) (string= specific "PRIORITY"))
  11667. (looking-at org-priority-regexp))
  11668. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  11669. (when (and (or (not specific) (string= specific "TAGS"))
  11670. (setq value (org-get-tags-string))
  11671. (string-match "\\S-" value))
  11672. (push (cons "TAGS" value) props))
  11673. (when (and (or (not specific) (string= specific "ALLTAGS"))
  11674. (setq value (org-get-tags-at)))
  11675. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  11676. ":"))
  11677. props))
  11678. (when (or (not specific) (string= specific "BLOCKED"))
  11679. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  11680. (when (or (not specific)
  11681. (member specific org-all-time-keywords)
  11682. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  11683. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  11684. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  11685. string (if (equal key clockstr)
  11686. (org-no-properties
  11687. (org-trim
  11688. (buffer-substring
  11689. (match-beginning 3) (goto-char (point-at-eol)))))
  11690. (substring (org-match-string-no-properties 3) 1 -1)))
  11691. (unless key
  11692. (if (= (char-after (match-beginning 3)) ?\[)
  11693. (setq key "TIMESTAMP_IA")
  11694. (setq key "TIMESTAMP")))
  11695. (when (or (equal key clockstr) (not (assoc key props)))
  11696. (push (cons key string) props))))
  11697. )
  11698. (when (memq which '(all standard))
  11699. ;; Get the standard properties, like :PROP: ...
  11700. (setq range (org-get-property-block beg end))
  11701. (when range
  11702. (goto-char (car range))
  11703. (while (re-search-forward
  11704. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  11705. (cdr range) t)
  11706. (setq key (org-match-string-no-properties 1)
  11707. value (org-trim (or (org-match-string-no-properties 2) "")))
  11708. (unless (member key excluded)
  11709. (push (cons key (or value "")) props)))))
  11710. (if clocksum
  11711. (push (cons "CLOCKSUM"
  11712. (org-columns-number-to-string (/ (float clocksum) 60.)
  11713. 'add_times))
  11714. props))
  11715. (unless (assoc "CATEGORY" props)
  11716. (setq value (or (org-get-category)
  11717. (progn (org-refresh-category-properties)
  11718. (org-get-category))))
  11719. (push (cons "CATEGORY" value) props))
  11720. (append sum-props (nreverse props)))))))
  11721. (defun org-entry-get (pom property &optional inherit)
  11722. "Get value of PROPERTY for entry at point-or-marker POM.
  11723. If INHERIT is non-nil and the entry does not have the property,
  11724. then also check higher levels of the hierarchy.
  11725. If INHERIT is the symbol `selective', use inheritance only if the setting
  11726. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  11727. If the property is present but empty, the return value is the empty string.
  11728. If the property is not present at all, nil is returned."
  11729. (org-with-point-at pom
  11730. (if (and inherit (if (eq inherit 'selective)
  11731. (org-property-inherit-p property)
  11732. t))
  11733. (org-entry-get-with-inheritance property)
  11734. (if (member property org-special-properties)
  11735. ;; We need a special property. Use `org-entry-properties' to
  11736. ;; retrieve it, but specify the wanted property
  11737. (cdr (assoc property (org-entry-properties nil 'special property)))
  11738. (let ((range (org-get-property-block)))
  11739. (if (and range
  11740. (goto-char (car range))
  11741. (re-search-forward
  11742. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  11743. (cdr range) t))
  11744. ;; Found the property, return it.
  11745. (if (match-end 1)
  11746. (org-match-string-no-properties 1)
  11747. "")))))))
  11748. (defun org-property-or-variable-value (var &optional inherit)
  11749. "Check if there is a property fixing the value of VAR.
  11750. If yes, return this value. If not, return the current value of the variable."
  11751. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  11752. (if (and prop (stringp prop) (string-match "\\S-" prop))
  11753. (read prop)
  11754. (symbol-value var))))
  11755. (defun org-entry-delete (pom property)
  11756. "Delete the property PROPERTY from entry at point-or-marker POM."
  11757. (org-with-point-at pom
  11758. (if (member property org-special-properties)
  11759. nil ; cannot delete these properties.
  11760. (let ((range (org-get-property-block)))
  11761. (if (and range
  11762. (goto-char (car range))
  11763. (re-search-forward
  11764. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  11765. (cdr range) t))
  11766. (progn
  11767. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  11768. t)
  11769. nil)))))
  11770. ;; Multi-values properties are properties that contain multiple values
  11771. ;; These values are assumed to be single words, separated by whitespace.
  11772. (defun org-entry-add-to-multivalued-property (pom property value)
  11773. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  11774. (let* ((old (org-entry-get pom property))
  11775. (values (and old (org-split-string old "[ \t]"))))
  11776. (setq value (org-entry-protect-space value))
  11777. (unless (member value values)
  11778. (setq values (cons value values))
  11779. (org-entry-put pom property
  11780. (mapconcat 'identity values " ")))))
  11781. (defun org-entry-remove-from-multivalued-property (pom property value)
  11782. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  11783. (let* ((old (org-entry-get pom property))
  11784. (values (and old (org-split-string old "[ \t]"))))
  11785. (setq value (org-entry-protect-space value))
  11786. (when (member value values)
  11787. (setq values (delete value values))
  11788. (org-entry-put pom property
  11789. (mapconcat 'identity values " ")))))
  11790. (defun org-entry-member-in-multivalued-property (pom property value)
  11791. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  11792. (let* ((old (org-entry-get pom property))
  11793. (values (and old (org-split-string old "[ \t]"))))
  11794. (setq value (org-entry-protect-space value))
  11795. (member value values)))
  11796. (defun org-entry-get-multivalued-property (pom property)
  11797. "Return a list of values in a multivalued property."
  11798. (let* ((value (org-entry-get pom property))
  11799. (values (and value (org-split-string value "[ \t]"))))
  11800. (mapcar 'org-entry-restore-space values)))
  11801. (defun org-entry-put-multivalued-property (pom property &rest values)
  11802. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  11803. VALUES should be a list of strings. Spaces will be protected."
  11804. (org-entry-put pom property
  11805. (mapconcat 'org-entry-protect-space values " "))
  11806. (let* ((value (org-entry-get pom property))
  11807. (values (and value (org-split-string value "[ \t]"))))
  11808. (mapcar 'org-entry-restore-space values)))
  11809. (defun org-entry-protect-space (s)
  11810. "Protect spaces and newline in string S."
  11811. (while (string-match " " s)
  11812. (setq s (replace-match "%20" t t s)))
  11813. (while (string-match "\n" s)
  11814. (setq s (replace-match "%0A" t t s)))
  11815. s)
  11816. (defun org-entry-restore-space (s)
  11817. "Restore spaces and newline in string S."
  11818. (while (string-match "%20" s)
  11819. (setq s (replace-match " " t t s)))
  11820. (while (string-match "%0A" s)
  11821. (setq s (replace-match "\n" t t s)))
  11822. s)
  11823. (defvar org-entry-property-inherited-from (make-marker)
  11824. "Marker pointing to the entry from where a property was inherited.
  11825. Each call to `org-entry-get-with-inheritance' will set this marker to the
  11826. location of the entry where the inheritance search matched. If there was
  11827. no match, the marker will point nowhere.
  11828. Note that also `org-entry-get' calls this function, if the INHERIT flag
  11829. is set.")
  11830. (defun org-entry-get-with-inheritance (property)
  11831. "Get entry property, and search higher levels if not present."
  11832. (move-marker org-entry-property-inherited-from nil)
  11833. (let (tmp)
  11834. (save-excursion
  11835. (save-restriction
  11836. (widen)
  11837. (catch 'ex
  11838. (while t
  11839. (when (setq tmp (org-entry-get nil property))
  11840. (org-back-to-heading t)
  11841. (move-marker org-entry-property-inherited-from (point))
  11842. (throw 'ex tmp))
  11843. (or (org-up-heading-safe) (throw 'ex nil)))))
  11844. (or tmp
  11845. (cdr (assoc property org-file-properties))
  11846. (cdr (assoc property org-global-properties))
  11847. (cdr (assoc property org-global-properties-fixed))))))
  11848. (defvar org-property-changed-functions nil
  11849. "Hook called when the value of a property has changed.
  11850. Each hook function should accept two arguments, the name of the property
  11851. and the new value.")
  11852. (defun org-entry-put (pom property value)
  11853. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  11854. (org-with-point-at pom
  11855. (org-back-to-heading t)
  11856. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  11857. range)
  11858. (cond
  11859. ((equal property "TODO")
  11860. (when (and (stringp value) (string-match "\\S-" value)
  11861. (not (member value org-todo-keywords-1)))
  11862. (error "\"%s\" is not a valid TODO state" value))
  11863. (if (or (not value)
  11864. (not (string-match "\\S-" value)))
  11865. (setq value 'none))
  11866. (org-todo value)
  11867. (org-set-tags nil 'align))
  11868. ((equal property "PRIORITY")
  11869. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  11870. (string-to-char value) ?\ ))
  11871. (org-set-tags nil 'align))
  11872. ((equal property "SCHEDULED")
  11873. (if (re-search-forward org-scheduled-time-regexp end t)
  11874. (cond
  11875. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11876. ((eq value 'later) (org-timestamp-change 1 'day))
  11877. (t (call-interactively 'org-schedule)))
  11878. (call-interactively 'org-schedule)))
  11879. ((equal property "DEADLINE")
  11880. (if (re-search-forward org-deadline-time-regexp end t)
  11881. (cond
  11882. ((eq value 'earlier) (org-timestamp-change -1 'day))
  11883. ((eq value 'later) (org-timestamp-change 1 'day))
  11884. (t (call-interactively 'org-deadline)))
  11885. (call-interactively 'org-deadline)))
  11886. ((member property org-special-properties)
  11887. (error "The %s property can not yet be set with `org-entry-put'"
  11888. property))
  11889. (t ; a non-special property
  11890. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  11891. (setq range (org-get-property-block beg end 'force))
  11892. (goto-char (car range))
  11893. (if (re-search-forward
  11894. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  11895. (progn
  11896. (delete-region (match-beginning 1) (match-end 1))
  11897. (goto-char (match-beginning 1)))
  11898. (goto-char (cdr range))
  11899. (insert "\n")
  11900. (backward-char 1)
  11901. (org-indent-line-function)
  11902. (insert ":" property ":"))
  11903. (and value (insert " " value))
  11904. (org-indent-line-function)))))
  11905. (run-hook-with-args 'org-property-changed-functions property value)))
  11906. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  11907. "Get all property keys in the current buffer.
  11908. With INCLUDE-SPECIALS, also list the special properties that reflect things
  11909. like tags and TODO state.
  11910. With INCLUDE-DEFAULTS, also include properties that has special meaning
  11911. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  11912. With INCLUDE-COLUMNS, also include property names given in COLUMN
  11913. formats in the current buffer."
  11914. (let (rtn range cfmt s p)
  11915. (save-excursion
  11916. (save-restriction
  11917. (widen)
  11918. (goto-char (point-min))
  11919. (while (re-search-forward org-property-start-re nil t)
  11920. (setq range (org-get-property-block))
  11921. (goto-char (car range))
  11922. (while (re-search-forward
  11923. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  11924. (cdr range) t)
  11925. (add-to-list 'rtn (org-match-string-no-properties 1)))
  11926. (outline-next-heading))))
  11927. (when include-specials
  11928. (setq rtn (append org-special-properties rtn)))
  11929. (when include-defaults
  11930. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  11931. (add-to-list 'rtn org-effort-property))
  11932. (when include-columns
  11933. (save-excursion
  11934. (save-restriction
  11935. (widen)
  11936. (goto-char (point-min))
  11937. (while (re-search-forward
  11938. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  11939. nil t)
  11940. (setq cfmt (match-string 2) s 0)
  11941. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  11942. cfmt s)
  11943. (setq s (match-end 0)
  11944. p (match-string 1 cfmt))
  11945. (unless (or (equal p "ITEM")
  11946. (member p org-special-properties))
  11947. (add-to-list 'rtn (match-string 1 cfmt))))))))
  11948. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  11949. (defun org-property-values (key)
  11950. "Return a list of all values of property KEY."
  11951. (save-excursion
  11952. (save-restriction
  11953. (widen)
  11954. (goto-char (point-min))
  11955. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  11956. values)
  11957. (while (re-search-forward re nil t)
  11958. (add-to-list 'values (org-trim (match-string 1))))
  11959. (delete "" values)))))
  11960. (defun org-insert-property-drawer ()
  11961. "Insert a property drawer into the current entry."
  11962. (interactive)
  11963. (org-back-to-heading t)
  11964. (looking-at outline-regexp)
  11965. (let ((indent (if org-adapt-indentation
  11966. (- (match-end 0)(match-beginning 0))
  11967. 0))
  11968. (beg (point))
  11969. (re (concat "^[ \t]*" org-keyword-time-regexp))
  11970. end hiddenp)
  11971. (outline-next-heading)
  11972. (setq end (point))
  11973. (goto-char beg)
  11974. (while (re-search-forward re end t))
  11975. (setq hiddenp (org-invisible-p))
  11976. (end-of-line 1)
  11977. (and (equal (char-after) ?\n) (forward-char 1))
  11978. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  11979. (if (member (match-string 1) '("CLOCK:" ":END:"))
  11980. ;; just skip this line
  11981. (beginning-of-line 2)
  11982. ;; Drawer start, find the end
  11983. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  11984. (beginning-of-line 1)))
  11985. (org-skip-over-state-notes)
  11986. (skip-chars-backward " \t\n\r")
  11987. (if (eq (char-before) ?*) (forward-char 1))
  11988. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  11989. (beginning-of-line 0)
  11990. (org-indent-to-column indent)
  11991. (beginning-of-line 2)
  11992. (org-indent-to-column indent)
  11993. (beginning-of-line 0)
  11994. (if hiddenp
  11995. (save-excursion
  11996. (org-back-to-heading t)
  11997. (hide-entry))
  11998. (org-flag-drawer t))))
  11999. (defun org-set-property (property value)
  12000. "In the current entry, set PROPERTY to VALUE.
  12001. When called interactively, this will prompt for a property name, offering
  12002. completion on existing and default properties. And then it will prompt
  12003. for a value, offering completion either on allowed values (via an inherited
  12004. xxx_ALL property) or on existing values in other instances of this property
  12005. in the current file."
  12006. (interactive
  12007. (let* ((completion-ignore-case t)
  12008. (keys (org-buffer-property-keys nil t t))
  12009. (prop0 (org-icompleting-read "Property: " (mapcar 'list keys)))
  12010. (prop (if (member prop0 keys)
  12011. prop0
  12012. (or (cdr (assoc (downcase prop0)
  12013. (mapcar (lambda (x) (cons (downcase x) x))
  12014. keys)))
  12015. prop0)))
  12016. (cur (org-entry-get nil prop))
  12017. (prompt (concat prop " value"
  12018. (if (and cur (string-match "\\S-" cur))
  12019. (concat " [" cur "]") "") ": "))
  12020. (allowed (org-property-get-allowed-values nil prop 'table))
  12021. (existing (mapcar 'list (org-property-values prop)))
  12022. (val (if allowed
  12023. (org-completing-read prompt allowed nil
  12024. (not (get-text-property 0 'org-unrestricted
  12025. (caar allowed))))
  12026. (let (org-completion-use-ido org-completion-use-iswitchb)
  12027. (org-completing-read prompt existing nil nil "" nil cur)))))
  12028. (list prop (if (equal val "") cur val))))
  12029. (unless (equal (org-entry-get nil property) value)
  12030. (org-entry-put nil property value)))
  12031. (defun org-delete-property (property)
  12032. "In the current entry, delete PROPERTY."
  12033. (interactive
  12034. (let* ((completion-ignore-case t)
  12035. (prop (org-icompleting-read "Property: " (org-entry-properties nil 'standard))))
  12036. (list prop)))
  12037. (message "Property %s %s" property
  12038. (if (org-entry-delete nil property)
  12039. "deleted"
  12040. "was not present in the entry")))
  12041. (defun org-delete-property-globally (property)
  12042. "Remove PROPERTY globally, from all entries."
  12043. (interactive
  12044. (let* ((completion-ignore-case t)
  12045. (prop (org-icompleting-read
  12046. "Globally remove property: "
  12047. (mapcar 'list (org-buffer-property-keys)))))
  12048. (list prop)))
  12049. (save-excursion
  12050. (save-restriction
  12051. (widen)
  12052. (goto-char (point-min))
  12053. (let ((cnt 0))
  12054. (while (re-search-forward
  12055. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  12056. nil t)
  12057. (setq cnt (1+ cnt))
  12058. (replace-match ""))
  12059. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12060. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12061. (defun org-compute-property-at-point ()
  12062. "Compute the property at point.
  12063. This looks for an enclosing column format, extracts the operator and
  12064. then applies it to the property in the column format's scope."
  12065. (interactive)
  12066. (unless (org-at-property-p)
  12067. (error "Not at a property"))
  12068. (let ((prop (org-match-string-no-properties 2)))
  12069. (org-columns-get-format-and-top-level)
  12070. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12071. (error "No operator defined for property %s" prop))
  12072. (org-columns-compute prop)))
  12073. (defvar org-property-allowed-value-functions nil
  12074. "Hook for functions supplying allowed values for a specific property.
  12075. The functions must take a single argument, the name of the property, and
  12076. return a flat list of allowed values. If \":ETC\" is one of
  12077. the values, this means that these values are intended as defaults for
  12078. completion, but that other values should be allowed too.
  12079. The functions must return nil if they are not responsible for this
  12080. property.")
  12081. (defun org-property-get-allowed-values (pom property &optional table)
  12082. "Get allowed values for the property PROPERTY.
  12083. When TABLE is non-nil, return an alist that can directly be used for
  12084. completion."
  12085. (let (vals)
  12086. (cond
  12087. ((equal property "TODO")
  12088. (setq vals (org-with-point-at pom
  12089. (append org-todo-keywords-1 '("")))))
  12090. ((equal property "PRIORITY")
  12091. (let ((n org-lowest-priority))
  12092. (while (>= n org-highest-priority)
  12093. (push (char-to-string n) vals)
  12094. (setq n (1- n)))))
  12095. ((member property org-special-properties))
  12096. ((setq vals (run-hook-with-args-until-success
  12097. 'org-property-allowed-value-functions property)))
  12098. (t
  12099. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12100. (when (and vals (string-match "\\S-" vals))
  12101. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12102. (setq vals (mapcar (lambda (x)
  12103. (cond ((stringp x) x)
  12104. ((numberp x) (number-to-string x))
  12105. ((symbolp x) (symbol-name x))
  12106. (t "???")))
  12107. vals)))))
  12108. (when (member ":ETC" vals)
  12109. (setq vals (remove ":ETC" vals))
  12110. (org-add-props (car vals) '(org-unrestricted t)))
  12111. (if table (mapcar 'list vals) vals)))
  12112. (defun org-property-previous-allowed-value (&optional previous)
  12113. "Switch to the next allowed value for this property."
  12114. (interactive)
  12115. (org-property-next-allowed-value t))
  12116. (defun org-property-next-allowed-value (&optional previous)
  12117. "Switch to the next allowed value for this property."
  12118. (interactive)
  12119. (unless (org-at-property-p)
  12120. (error "Not at a property"))
  12121. (let* ((key (match-string 2))
  12122. (value (match-string 3))
  12123. (allowed (or (org-property-get-allowed-values (point) key)
  12124. (and (member value '("[ ]" "[-]" "[X]"))
  12125. '("[ ]" "[X]"))))
  12126. nval)
  12127. (unless allowed
  12128. (error "Allowed values for this property have not been defined"))
  12129. (if previous (setq allowed (reverse allowed)))
  12130. (if (member value allowed)
  12131. (setq nval (car (cdr (member value allowed)))))
  12132. (setq nval (or nval (car allowed)))
  12133. (if (equal nval value)
  12134. (error "Only one allowed value for this property"))
  12135. (org-at-property-p)
  12136. (replace-match (concat " :" key ": " nval) t t)
  12137. (org-indent-line-function)
  12138. (beginning-of-line 1)
  12139. (skip-chars-forward " \t")
  12140. (run-hook-with-args 'org-property-changed-functions key nval)))
  12141. (defun org-find-entry-with-id (ident)
  12142. "Locate the entry that contains the ID property with exact value IDENT.
  12143. IDENT can be a string, a symbol or a number, this function will search for
  12144. the string representation of it.
  12145. Return the position where this entry starts, or nil if there is no such entry."
  12146. (interactive "sID: ")
  12147. (let ((id (cond
  12148. ((stringp ident) ident)
  12149. ((symbol-name ident) (symbol-name ident))
  12150. ((numberp ident) (number-to-string ident))
  12151. (t (error "IDENT %s must be a string, symbol or number" ident))))
  12152. (case-fold-search nil))
  12153. (save-excursion
  12154. (save-restriction
  12155. (widen)
  12156. (goto-char (point-min))
  12157. (when (re-search-forward
  12158. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  12159. nil t)
  12160. (org-back-to-heading t)
  12161. (point))))))
  12162. ;;;; Timestamps
  12163. (defvar org-last-changed-timestamp nil)
  12164. (defvar org-last-inserted-timestamp nil
  12165. "The last time stamp inserted with `org-insert-time-stamp'.")
  12166. (defvar org-time-was-given) ; dynamically scoped parameter
  12167. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12168. (defvar org-ts-what) ; dynamically scoped parameter
  12169. (defun org-time-stamp (arg &optional inactive)
  12170. "Prompt for a date/time and insert a time stamp.
  12171. If the user specifies a time like HH:MM, or if this command is called
  12172. with a prefix argument, the time stamp will contain date and time.
  12173. Otherwise, only the date will be included. All parts of a date not
  12174. specified by the user will be filled in from the current date/time.
  12175. So if you press just return without typing anything, the time stamp
  12176. will represent the current date/time. If there is already a timestamp
  12177. at the cursor, it will be modified."
  12178. (interactive "P")
  12179. (let* ((ts nil)
  12180. (default-time
  12181. ;; Default time is either today, or, when entering a range,
  12182. ;; the range start.
  12183. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  12184. (save-excursion
  12185. (re-search-backward
  12186. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  12187. (- (point) 20) t)))
  12188. (apply 'encode-time (org-parse-time-string (match-string 1)))
  12189. (current-time)))
  12190. (default-input (and ts (org-get-compact-tod ts)))
  12191. org-time-was-given org-end-time-was-given time)
  12192. (cond
  12193. ((and (org-at-timestamp-p t)
  12194. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  12195. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  12196. (insert "--")
  12197. (setq time (let ((this-command this-command))
  12198. (org-read-date arg 'totime nil nil
  12199. default-time default-input)))
  12200. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  12201. ((org-at-timestamp-p t)
  12202. (setq time (let ((this-command this-command))
  12203. (org-read-date arg 'totime nil nil default-time default-input)))
  12204. (when (org-at-timestamp-p t) ; just to get the match data
  12205. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  12206. (replace-match "")
  12207. (setq org-last-changed-timestamp
  12208. (org-insert-time-stamp
  12209. time (or org-time-was-given arg)
  12210. inactive nil nil (list org-end-time-was-given))))
  12211. (message "Timestamp updated"))
  12212. (t
  12213. (setq time (let ((this-command this-command))
  12214. (org-read-date arg 'totime nil nil default-time default-input)))
  12215. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  12216. nil nil (list org-end-time-was-given))))))
  12217. ;; FIXME: can we use this for something else, like computing time differences?
  12218. (defun org-get-compact-tod (s)
  12219. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  12220. (let* ((t1 (match-string 1 s))
  12221. (h1 (string-to-number (match-string 2 s)))
  12222. (m1 (string-to-number (match-string 3 s)))
  12223. (t2 (and (match-end 4) (match-string 5 s)))
  12224. (h2 (and t2 (string-to-number (match-string 6 s))))
  12225. (m2 (and t2 (string-to-number (match-string 7 s))))
  12226. dh dm)
  12227. (if (not t2)
  12228. t1
  12229. (setq dh (- h2 h1) dm (- m2 m1))
  12230. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  12231. (concat t1 "+" (number-to-string dh)
  12232. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  12233. (defun org-time-stamp-inactive (&optional arg)
  12234. "Insert an inactive time stamp.
  12235. An inactive time stamp is enclosed in square brackets instead of angle
  12236. brackets. It is inactive in the sense that it does not trigger agenda entries,
  12237. does not link to the calendar and cannot be changed with the S-cursor keys.
  12238. So these are more for recording a certain time/date."
  12239. (interactive "P")
  12240. (org-time-stamp arg 'inactive))
  12241. (defvar org-date-ovl (org-make-overlay 1 1))
  12242. (org-overlay-put org-date-ovl 'face 'org-warning)
  12243. (org-detach-overlay org-date-ovl)
  12244. (defvar org-ans1) ; dynamically scoped parameter
  12245. (defvar org-ans2) ; dynamically scoped parameter
  12246. (defvar org-plain-time-of-day-regexp) ; defined below
  12247. (defvar org-overriding-default-time nil) ; dynamically scoped
  12248. (defvar org-read-date-overlay nil)
  12249. (defvar org-dcst nil) ; dynamically scoped
  12250. (defvar org-read-date-history nil)
  12251. (defvar org-read-date-final-answer nil)
  12252. (defun org-read-date (&optional with-time to-time from-string prompt
  12253. default-time default-input)
  12254. "Read a date, possibly a time, and make things smooth for the user.
  12255. The prompt will suggest to enter an ISO date, but you can also enter anything
  12256. which will at least partially be understood by `parse-time-string'.
  12257. Unrecognized parts of the date will default to the current day, month, year,
  12258. hour and minute. If this command is called to replace a timestamp at point,
  12259. of to enter the second timestamp of a range, the default time is taken from the
  12260. existing stamp. For example,
  12261. 3-2-5 --> 2003-02-05
  12262. feb 15 --> currentyear-02-15
  12263. sep 12 9 --> 2009-09-12
  12264. 12:45 --> today 12:45
  12265. 22 sept 0:34 --> currentyear-09-22 0:34
  12266. 12 --> currentyear-currentmonth-12
  12267. Fri --> nearest Friday (today or later)
  12268. etc.
  12269. Furthermore you can specify a relative date by giving, as the *first* thing
  12270. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  12271. change in days weeks, months, years.
  12272. With a single plus or minus, the date is relative to today. With a double
  12273. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  12274. +4d --> four days from today
  12275. +4 --> same as above
  12276. +2w --> two weeks from today
  12277. ++5 --> five days from default date
  12278. The function understands only English month and weekday abbreviations,
  12279. but this can be configured with the variables `parse-time-months' and
  12280. `parse-time-weekdays'.
  12281. While prompting, a calendar is popped up - you can also select the
  12282. date with the mouse (button 1). The calendar shows a period of three
  12283. months. To scroll it to other months, use the keys `>' and `<'.
  12284. If you don't like the calendar, turn it off with
  12285. \(setq org-read-date-popup-calendar nil)
  12286. With optional argument TO-TIME, the date will immediately be converted
  12287. to an internal time.
  12288. With an optional argument WITH-TIME, the prompt will suggest to also
  12289. insert a time. Note that when WITH-TIME is not set, you can still
  12290. enter a time, and this function will inform the calling routine about
  12291. this change. The calling routine may then choose to change the format
  12292. used to insert the time stamp into the buffer to include the time.
  12293. With optional argument FROM-STRING, read from this string instead from
  12294. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  12295. the time/date that is used for everything that is not specified by the
  12296. user."
  12297. (require 'parse-time)
  12298. (let* ((org-time-stamp-rounding-minutes
  12299. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  12300. (org-dcst org-display-custom-times)
  12301. (ct (org-current-time))
  12302. (def (or org-overriding-default-time default-time ct))
  12303. (defdecode (decode-time def))
  12304. (dummy (progn
  12305. (when (< (nth 2 defdecode) org-extend-today-until)
  12306. (setcar (nthcdr 2 defdecode) -1)
  12307. (setcar (nthcdr 1 defdecode) 59)
  12308. (setq def (apply 'encode-time defdecode)
  12309. defdecode (decode-time def)))))
  12310. (calendar-frame-setup nil)
  12311. (calendar-move-hook nil)
  12312. (calendar-view-diary-initially-flag nil)
  12313. (view-diary-entries-initially nil)
  12314. (calendar-view-holidays-initially-flag nil)
  12315. (view-calendar-holidays-initially nil)
  12316. (timestr (format-time-string
  12317. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  12318. (prompt (concat (if prompt (concat prompt " ") "")
  12319. (format "Date+time [%s]: " timestr)))
  12320. ans (org-ans0 "") org-ans1 org-ans2 final)
  12321. (cond
  12322. (from-string (setq ans from-string))
  12323. (org-read-date-popup-calendar
  12324. (save-excursion
  12325. (save-window-excursion
  12326. (calendar)
  12327. (calendar-forward-day (- (time-to-days def)
  12328. (calendar-absolute-from-gregorian
  12329. (calendar-current-date))))
  12330. (org-eval-in-calendar nil t)
  12331. (let* ((old-map (current-local-map))
  12332. (map (copy-keymap calendar-mode-map))
  12333. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  12334. (org-defkey map (kbd "RET") 'org-calendar-select)
  12335. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  12336. 'org-calendar-select-mouse)
  12337. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  12338. 'org-calendar-select-mouse)
  12339. (org-defkey minibuffer-local-map [(meta shift left)]
  12340. (lambda () (interactive)
  12341. (org-eval-in-calendar '(calendar-backward-month 1))))
  12342. (org-defkey minibuffer-local-map [(meta shift right)]
  12343. (lambda () (interactive)
  12344. (org-eval-in-calendar '(calendar-forward-month 1))))
  12345. (org-defkey minibuffer-local-map [(meta shift up)]
  12346. (lambda () (interactive)
  12347. (org-eval-in-calendar '(calendar-backward-year 1))))
  12348. (org-defkey minibuffer-local-map [(meta shift down)]
  12349. (lambda () (interactive)
  12350. (org-eval-in-calendar '(calendar-forward-year 1))))
  12351. (org-defkey minibuffer-local-map [?\e (shift left)]
  12352. (lambda () (interactive)
  12353. (org-eval-in-calendar '(calendar-backward-month 1))))
  12354. (org-defkey minibuffer-local-map [?\e (shift right)]
  12355. (lambda () (interactive)
  12356. (org-eval-in-calendar '(calendar-forward-month 1))))
  12357. (org-defkey minibuffer-local-map [?\e (shift up)]
  12358. (lambda () (interactive)
  12359. (org-eval-in-calendar '(calendar-backward-year 1))))
  12360. (org-defkey minibuffer-local-map [?\e (shift down)]
  12361. (lambda () (interactive)
  12362. (org-eval-in-calendar '(calendar-forward-year 1))))
  12363. (org-defkey minibuffer-local-map [(shift up)]
  12364. (lambda () (interactive)
  12365. (org-eval-in-calendar '(calendar-backward-week 1))))
  12366. (org-defkey minibuffer-local-map [(shift down)]
  12367. (lambda () (interactive)
  12368. (org-eval-in-calendar '(calendar-forward-week 1))))
  12369. (org-defkey minibuffer-local-map [(shift left)]
  12370. (lambda () (interactive)
  12371. (org-eval-in-calendar '(calendar-backward-day 1))))
  12372. (org-defkey minibuffer-local-map [(shift right)]
  12373. (lambda () (interactive)
  12374. (org-eval-in-calendar '(calendar-forward-day 1))))
  12375. (org-defkey minibuffer-local-map ">"
  12376. (lambda () (interactive)
  12377. (org-eval-in-calendar '(scroll-calendar-left 1))))
  12378. (org-defkey minibuffer-local-map "<"
  12379. (lambda () (interactive)
  12380. (org-eval-in-calendar '(scroll-calendar-right 1))))
  12381. (run-hooks 'org-read-date-minibuffer-setup-hook)
  12382. (unwind-protect
  12383. (progn
  12384. (use-local-map map)
  12385. (add-hook 'post-command-hook 'org-read-date-display)
  12386. (setq org-ans0 (read-string prompt default-input
  12387. 'org-read-date-history nil))
  12388. ;; org-ans0: from prompt
  12389. ;; org-ans1: from mouse click
  12390. ;; org-ans2: from calendar motion
  12391. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  12392. (remove-hook 'post-command-hook 'org-read-date-display)
  12393. (use-local-map old-map)
  12394. (when org-read-date-overlay
  12395. (org-delete-overlay org-read-date-overlay)
  12396. (setq org-read-date-overlay nil)))))))
  12397. (t ; Naked prompt only
  12398. (unwind-protect
  12399. (setq ans (read-string prompt default-input
  12400. 'org-read-date-history timestr))
  12401. (when org-read-date-overlay
  12402. (org-delete-overlay org-read-date-overlay)
  12403. (setq org-read-date-overlay nil)))))
  12404. (setq final (org-read-date-analyze ans def defdecode))
  12405. (setq org-read-date-final-answer ans)
  12406. (if to-time
  12407. (apply 'encode-time final)
  12408. (if (and (boundp 'org-time-was-given) org-time-was-given)
  12409. (format "%04d-%02d-%02d %02d:%02d"
  12410. (nth 5 final) (nth 4 final) (nth 3 final)
  12411. (nth 2 final) (nth 1 final))
  12412. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  12413. (defvar def)
  12414. (defvar defdecode)
  12415. (defvar with-time)
  12416. (defvar org-read-date-analyze-futurep nil)
  12417. (defun org-read-date-display ()
  12418. "Display the current date prompt interpretation in the minibuffer."
  12419. (when org-read-date-display-live
  12420. (when org-read-date-overlay
  12421. (org-delete-overlay org-read-date-overlay))
  12422. (let ((p (point)))
  12423. (end-of-line 1)
  12424. (while (not (equal (buffer-substring
  12425. (max (point-min) (- (point) 4)) (point))
  12426. " "))
  12427. (insert " "))
  12428. (goto-char p))
  12429. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  12430. " " (or org-ans1 org-ans2)))
  12431. (org-end-time-was-given nil)
  12432. (f (org-read-date-analyze ans def defdecode))
  12433. (fmts (if org-dcst
  12434. org-time-stamp-custom-formats
  12435. org-time-stamp-formats))
  12436. (fmt (if (or with-time
  12437. (and (boundp 'org-time-was-given) org-time-was-given))
  12438. (cdr fmts)
  12439. (car fmts)))
  12440. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  12441. (when (and org-end-time-was-given
  12442. (string-match org-plain-time-of-day-regexp txt))
  12443. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  12444. org-end-time-was-given
  12445. (substring txt (match-end 0)))))
  12446. (when org-read-date-analyze-futurep
  12447. (setq txt (concat txt " (=>F)")))
  12448. (setq org-read-date-overlay
  12449. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  12450. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  12451. (defun org-read-date-analyze (ans def defdecode)
  12452. "Analyse the combined answer of the date prompt."
  12453. ;; FIXME: cleanup and comment
  12454. (let ((nowdecode (decode-time (current-time)))
  12455. delta deltan deltaw deltadef year month day
  12456. hour minute second wday pm h2 m2 tl wday1
  12457. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  12458. (setq org-read-date-analyze-futurep nil)
  12459. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  12460. (setq ans "+0"))
  12461. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  12462. (setq ans (replace-match "" t t ans)
  12463. deltan (car delta)
  12464. deltaw (nth 1 delta)
  12465. deltadef (nth 2 delta)))
  12466. ;; Check if there is an iso week date in there
  12467. ;; If yes, store the info and postpone interpreting it until the rest
  12468. ;; of the parsing is done
  12469. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  12470. (setq iso-year (if (match-end 1)
  12471. (org-small-year-to-year
  12472. (string-to-number (match-string 1 ans))))
  12473. iso-weekday (if (match-end 3)
  12474. (string-to-number (match-string 3 ans)))
  12475. iso-week (string-to-number (match-string 2 ans)))
  12476. (setq ans (replace-match "" t t ans)))
  12477. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  12478. (when (string-match
  12479. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  12480. (setq year (if (match-end 2)
  12481. (string-to-number (match-string 2 ans))
  12482. (progn (setq kill-year t)
  12483. (string-to-number (format-time-string "%Y"))))
  12484. month (string-to-number (match-string 3 ans))
  12485. day (string-to-number (match-string 4 ans)))
  12486. (if (< year 100) (setq year (+ 2000 year)))
  12487. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12488. t nil ans)))
  12489. ;; Help matching american dates, like 5/30 or 5/30/7
  12490. (when (string-match
  12491. "^ *\\([0-3]?[0-9]\\)/\\([0-1]?[0-9]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  12492. (setq year (if (match-end 4)
  12493. (string-to-number (match-string 4 ans))
  12494. (progn (setq kill-year t)
  12495. (string-to-number (format-time-string "%Y"))))
  12496. month (string-to-number (match-string 1 ans))
  12497. day (string-to-number (match-string 2 ans)))
  12498. (if (< year 100) (setq year (+ 2000 year)))
  12499. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  12500. t nil ans)))
  12501. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  12502. ;; If there is a time with am/pm, and *no* time without it, we convert
  12503. ;; so that matching will be successful.
  12504. (loop for i from 1 to 2 do ; twice, for end time as well
  12505. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  12506. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  12507. (setq hour (string-to-number (match-string 1 ans))
  12508. minute (if (match-end 3)
  12509. (string-to-number (match-string 3 ans))
  12510. 0)
  12511. pm (equal ?p
  12512. (string-to-char (downcase (match-string 4 ans)))))
  12513. (if (and (= hour 12) (not pm))
  12514. (setq hour 0)
  12515. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  12516. (setq ans (replace-match (format "%02d:%02d" hour minute)
  12517. t t ans))))
  12518. ;; Check if a time range is given as a duration
  12519. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  12520. (setq hour (string-to-number (match-string 1 ans))
  12521. h2 (+ hour (string-to-number (match-string 3 ans)))
  12522. minute (string-to-number (match-string 2 ans))
  12523. m2 (+ minute (if (match-end 5) (string-to-number
  12524. (match-string 5 ans))0)))
  12525. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  12526. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  12527. t t ans)))
  12528. ;; Check if there is a time range
  12529. (when (boundp 'org-end-time-was-given)
  12530. (setq org-time-was-given nil)
  12531. (when (and (string-match org-plain-time-of-day-regexp ans)
  12532. (match-end 8))
  12533. (setq org-end-time-was-given (match-string 8 ans))
  12534. (setq ans (concat (substring ans 0 (match-beginning 7))
  12535. (substring ans (match-end 7))))))
  12536. (setq tl (parse-time-string ans)
  12537. day (or (nth 3 tl) (nth 3 defdecode))
  12538. month (or (nth 4 tl)
  12539. (if (and org-read-date-prefer-future
  12540. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  12541. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  12542. (nth 4 defdecode)))
  12543. year (or (and (not kill-year) (nth 5 tl))
  12544. (if (and org-read-date-prefer-future
  12545. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  12546. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  12547. (nth 5 defdecode)))
  12548. hour (or (nth 2 tl) (nth 2 defdecode))
  12549. minute (or (nth 1 tl) (nth 1 defdecode))
  12550. second (or (nth 0 tl) 0)
  12551. wday (nth 6 tl))
  12552. (when (and (eq org-read-date-prefer-future 'time)
  12553. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  12554. (equal day (nth 3 nowdecode))
  12555. (equal month (nth 4 nowdecode))
  12556. (equal year (nth 5 nowdecode))
  12557. (nth 2 tl)
  12558. (or (< (nth 2 tl) (nth 2 nowdecode))
  12559. (and (= (nth 2 tl) (nth 2 nowdecode))
  12560. (nth 1 tl)
  12561. (< (nth 1 tl) (nth 1 nowdecode)))))
  12562. (setq day (1+ day)
  12563. futurep t))
  12564. ;; Special date definitions below
  12565. (cond
  12566. (iso-week
  12567. ;; There was an iso week
  12568. (require 'cal-iso)
  12569. (setq futurep nil)
  12570. (setq year (or iso-year year)
  12571. day (or iso-weekday wday 1)
  12572. wday nil ; to make sure that the trigger below does not match
  12573. iso-date (calendar-gregorian-from-absolute
  12574. (calendar-absolute-from-iso
  12575. (list iso-week day year))))
  12576. ; FIXME: Should we also push ISO weeks into the future?
  12577. ; (when (and org-read-date-prefer-future
  12578. ; (not iso-year)
  12579. ; (< (calendar-absolute-from-gregorian iso-date)
  12580. ; (time-to-days (current-time))))
  12581. ; (setq year (1+ year)
  12582. ; iso-date (calendar-gregorian-from-absolute
  12583. ; (calendar-absolute-from-iso
  12584. ; (list iso-week day year)))))
  12585. (setq month (car iso-date)
  12586. year (nth 2 iso-date)
  12587. day (nth 1 iso-date)))
  12588. (deltan
  12589. (setq futurep nil)
  12590. (unless deltadef
  12591. (let ((now (decode-time (current-time))))
  12592. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  12593. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  12594. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  12595. ((equal deltaw "m") (setq month (+ month deltan)))
  12596. ((equal deltaw "y") (setq year (+ year deltan)))))
  12597. ((and wday (not (nth 3 tl)))
  12598. (setq futurep nil)
  12599. ;; Weekday was given, but no day, so pick that day in the week
  12600. ;; on or after the derived date.
  12601. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  12602. (unless (equal wday wday1)
  12603. (setq day (+ day (% (- wday wday1 -7) 7))))))
  12604. (if (and (boundp 'org-time-was-given)
  12605. (nth 2 tl))
  12606. (setq org-time-was-given t))
  12607. (if (< year 100) (setq year (+ 2000 year)))
  12608. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  12609. (setq org-read-date-analyze-futurep futurep)
  12610. (list second minute hour day month year)))
  12611. (defvar parse-time-weekdays)
  12612. (defun org-read-date-get-relative (s today default)
  12613. "Check string S for special relative date string.
  12614. TODAY and DEFAULT are internal times, for today and for a default.
  12615. Return shift list (N what def-flag)
  12616. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  12617. N is the number of WHATs to shift.
  12618. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  12619. the DEFAULT date rather than TODAY."
  12620. (when (and
  12621. (string-match
  12622. (concat
  12623. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  12624. "\\([0-9]+\\)?"
  12625. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  12626. "\\([ \t]\\|$\\)") s)
  12627. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  12628. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  12629. (string-to-char (substring (match-string 1 s) -1))
  12630. ?+))
  12631. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  12632. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  12633. (what (if (match-end 3) (match-string 3 s) "d"))
  12634. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  12635. (date (if rel default today))
  12636. (wday (nth 6 (decode-time date)))
  12637. delta)
  12638. (if wday1
  12639. (progn
  12640. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  12641. (if (= dir ?-) (setq delta (- delta 7)))
  12642. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  12643. (list delta "d" rel))
  12644. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  12645. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  12646. "Turn a user-specified date into the internal representation.
  12647. The internal representation needed by the calendar is (month day year).
  12648. This is a wrapper to handle the brain-dead convention in calendar that
  12649. user function argument order change dependent on argument order."
  12650. (if (boundp 'calendar-date-style)
  12651. (cond
  12652. ((eq calendar-date-style 'american)
  12653. (list arg1 arg2 arg3))
  12654. ((eq calendar-date-style 'european)
  12655. (list arg2 arg1 arg3))
  12656. ((eq calendar-date-style 'iso)
  12657. (list arg2 arg3 arg1)))
  12658. (if (org-bound-and-true-p european-calendar-style)
  12659. (list arg2 arg1 arg3)
  12660. (list arg1 arg2 arg3))))
  12661. (defun org-eval-in-calendar (form &optional keepdate)
  12662. "Eval FORM in the calendar window and return to current window.
  12663. Also, store the cursor date in variable org-ans2."
  12664. (let ((sf (selected-frame))
  12665. (sw (selected-window)))
  12666. (select-window (get-buffer-window "*Calendar*" t))
  12667. (eval form)
  12668. (when (and (not keepdate) (calendar-cursor-to-date))
  12669. (let* ((date (calendar-cursor-to-date))
  12670. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12671. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  12672. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  12673. (select-window sw)
  12674. (org-select-frame-set-input-focus sf)))
  12675. (defun org-calendar-select ()
  12676. "Return to `org-read-date' with the date currently selected.
  12677. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12678. (interactive)
  12679. (when (calendar-cursor-to-date)
  12680. (let* ((date (calendar-cursor-to-date))
  12681. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12682. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12683. (if (active-minibuffer-window) (exit-minibuffer))))
  12684. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  12685. "Insert a date stamp for the date given by the internal TIME.
  12686. WITH-HM means use the stamp format that includes the time of the day.
  12687. INACTIVE means use square brackets instead of angular ones, so that the
  12688. stamp will not contribute to the agenda.
  12689. PRE and POST are optional strings to be inserted before and after the
  12690. stamp.
  12691. The command returns the inserted time stamp."
  12692. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  12693. stamp)
  12694. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  12695. (insert-before-markers (or pre ""))
  12696. (insert-before-markers (setq stamp (format-time-string fmt time)))
  12697. (when (listp extra)
  12698. (setq extra (car extra))
  12699. (if (and (stringp extra)
  12700. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  12701. (setq extra (format "-%02d:%02d"
  12702. (string-to-number (match-string 1 extra))
  12703. (string-to-number (match-string 2 extra))))
  12704. (setq extra nil)))
  12705. (when extra
  12706. (backward-char 1)
  12707. (insert-before-markers extra)
  12708. (forward-char 1))
  12709. (insert-before-markers (or post ""))
  12710. (setq org-last-inserted-timestamp stamp)))
  12711. (defun org-toggle-time-stamp-overlays ()
  12712. "Toggle the use of custom time stamp formats."
  12713. (interactive)
  12714. (setq org-display-custom-times (not org-display-custom-times))
  12715. (unless org-display-custom-times
  12716. (let ((p (point-min)) (bmp (buffer-modified-p)))
  12717. (while (setq p (next-single-property-change p 'display))
  12718. (if (and (get-text-property p 'display)
  12719. (eq (get-text-property p 'face) 'org-date))
  12720. (remove-text-properties
  12721. p (setq p (next-single-property-change p 'display))
  12722. '(display t))))
  12723. (set-buffer-modified-p bmp)))
  12724. (if (featurep 'xemacs)
  12725. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  12726. (org-restart-font-lock)
  12727. (setq org-table-may-need-update t)
  12728. (if org-display-custom-times
  12729. (message "Time stamps are overlayed with custom format")
  12730. (message "Time stamp overlays removed")))
  12731. (defun org-display-custom-time (beg end)
  12732. "Overlay modified time stamp format over timestamp between BEG and END."
  12733. (let* ((ts (buffer-substring beg end))
  12734. t1 w1 with-hm tf time str w2 (off 0))
  12735. (save-match-data
  12736. (setq t1 (org-parse-time-string ts t))
  12737. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  12738. (setq off (- (match-end 0) (match-beginning 0)))))
  12739. (setq end (- end off))
  12740. (setq w1 (- end beg)
  12741. with-hm (and (nth 1 t1) (nth 2 t1))
  12742. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  12743. time (org-fix-decoded-time t1)
  12744. str (org-add-props
  12745. (format-time-string
  12746. (substring tf 1 -1) (apply 'encode-time time))
  12747. nil 'mouse-face 'highlight)
  12748. w2 (length str))
  12749. (if (not (= w2 w1))
  12750. (add-text-properties (1+ beg) (+ 2 beg)
  12751. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  12752. (if (featurep 'xemacs)
  12753. (progn
  12754. (put-text-property beg end 'invisible t)
  12755. (put-text-property beg end 'end-glyph (make-glyph str)))
  12756. (put-text-property beg end 'display str))))
  12757. (defun org-translate-time (string)
  12758. "Translate all timestamps in STRING to custom format.
  12759. But do this only if the variable `org-display-custom-times' is set."
  12760. (when org-display-custom-times
  12761. (save-match-data
  12762. (let* ((start 0)
  12763. (re org-ts-regexp-both)
  12764. t1 with-hm inactive tf time str beg end)
  12765. (while (setq start (string-match re string start))
  12766. (setq beg (match-beginning 0)
  12767. end (match-end 0)
  12768. t1 (save-match-data
  12769. (org-parse-time-string (substring string beg end) t))
  12770. with-hm (and (nth 1 t1) (nth 2 t1))
  12771. inactive (equal (substring string beg (1+ beg)) "[")
  12772. tf (funcall (if with-hm 'cdr 'car)
  12773. org-time-stamp-custom-formats)
  12774. time (org-fix-decoded-time t1)
  12775. str (format-time-string
  12776. (concat
  12777. (if inactive "[" "<") (substring tf 1 -1)
  12778. (if inactive "]" ">"))
  12779. (apply 'encode-time time))
  12780. string (replace-match str t t string)
  12781. start (+ start (length str)))))))
  12782. string)
  12783. (defun org-fix-decoded-time (time)
  12784. "Set 0 instead of nil for the first 6 elements of time.
  12785. Don't touch the rest."
  12786. (let ((n 0))
  12787. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  12788. (defun org-days-to-time (timestamp-string)
  12789. "Difference between TIMESTAMP-STRING and now in days."
  12790. (- (time-to-days (org-time-string-to-time timestamp-string))
  12791. (time-to-days (current-time))))
  12792. (defun org-deadline-close (timestamp-string &optional ndays)
  12793. "Is the time in TIMESTAMP-STRING close to the current date?"
  12794. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  12795. (and (< (org-days-to-time timestamp-string) ndays)
  12796. (not (org-entry-is-done-p))))
  12797. (defun org-get-wdays (ts)
  12798. "Get the deadline lead time appropriate for timestring TS."
  12799. (cond
  12800. ((<= org-deadline-warning-days 0)
  12801. ;; 0 or negative, enforce this value no matter what
  12802. (- org-deadline-warning-days))
  12803. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  12804. ;; lead time is specified.
  12805. (floor (* (string-to-number (match-string 1 ts))
  12806. (cdr (assoc (match-string 2 ts)
  12807. '(("d" . 1) ("w" . 7)
  12808. ("m" . 30.4) ("y" . 365.25)))))))
  12809. ;; go for the default.
  12810. (t org-deadline-warning-days)))
  12811. (defun org-calendar-select-mouse (ev)
  12812. "Return to `org-read-date' with the date currently selected.
  12813. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  12814. (interactive "e")
  12815. (mouse-set-point ev)
  12816. (when (calendar-cursor-to-date)
  12817. (let* ((date (calendar-cursor-to-date))
  12818. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12819. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  12820. (if (active-minibuffer-window) (exit-minibuffer))))
  12821. (defun org-check-deadlines (ndays)
  12822. "Check if there are any deadlines due or past due.
  12823. A deadline is considered due if it happens within `org-deadline-warning-days'
  12824. days from today's date. If the deadline appears in an entry marked DONE,
  12825. it is not shown. The prefix arg NDAYS can be used to test that many
  12826. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  12827. (interactive "P")
  12828. (let* ((org-warn-days
  12829. (cond
  12830. ((equal ndays '(4)) 100000)
  12831. (ndays (prefix-numeric-value ndays))
  12832. (t (abs org-deadline-warning-days))))
  12833. (case-fold-search nil)
  12834. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  12835. (callback
  12836. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  12837. (message "%d deadlines past-due or due within %d days"
  12838. (org-occur regexp nil callback)
  12839. org-warn-days)))
  12840. (defun org-check-before-date (date)
  12841. "Check if there are deadlines or scheduled entries before DATE."
  12842. (interactive (list (org-read-date)))
  12843. (let ((case-fold-search nil)
  12844. (regexp (concat "\\<\\(" org-deadline-string
  12845. "\\|" org-scheduled-string
  12846. "\\) *<\\([^>]+\\)>"))
  12847. (callback
  12848. (lambda () (time-less-p
  12849. (org-time-string-to-time (match-string 2))
  12850. (org-time-string-to-time date)))))
  12851. (message "%d entries before %s"
  12852. (org-occur regexp nil callback) date)))
  12853. (defun org-check-after-date (date)
  12854. "Check if there are deadlines or scheduled entries after DATE."
  12855. (interactive (list (org-read-date)))
  12856. (let ((case-fold-search nil)
  12857. (regexp (concat "\\<\\(" org-deadline-string
  12858. "\\|" org-scheduled-string
  12859. "\\) *<\\([^>]+\\)>"))
  12860. (callback
  12861. (lambda () (not
  12862. (time-less-p
  12863. (org-time-string-to-time (match-string 2))
  12864. (org-time-string-to-time date))))))
  12865. (message "%d entries after %s"
  12866. (org-occur regexp nil callback) date)))
  12867. (defun org-evaluate-time-range (&optional to-buffer)
  12868. "Evaluate a time range by computing the difference between start and end.
  12869. Normally the result is just printed in the echo area, but with prefix arg
  12870. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  12871. If the time range is actually in a table, the result is inserted into the
  12872. next column.
  12873. For time difference computation, a year is assumed to be exactly 365
  12874. days in order to avoid rounding problems."
  12875. (interactive "P")
  12876. (or
  12877. (org-clock-update-time-maybe)
  12878. (save-excursion
  12879. (unless (org-at-date-range-p t)
  12880. (goto-char (point-at-bol))
  12881. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  12882. (if (not (org-at-date-range-p t))
  12883. (error "Not at a time-stamp range, and none found in current line")))
  12884. (let* ((ts1 (match-string 1))
  12885. (ts2 (match-string 2))
  12886. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  12887. (match-end (match-end 0))
  12888. (time1 (org-time-string-to-time ts1))
  12889. (time2 (org-time-string-to-time ts2))
  12890. (t1 (org-float-time time1))
  12891. (t2 (org-float-time time2))
  12892. (diff (abs (- t2 t1)))
  12893. (negative (< (- t2 t1) 0))
  12894. ;; (ys (floor (* 365 24 60 60)))
  12895. (ds (* 24 60 60))
  12896. (hs (* 60 60))
  12897. (fy "%dy %dd %02d:%02d")
  12898. (fy1 "%dy %dd")
  12899. (fd "%dd %02d:%02d")
  12900. (fd1 "%dd")
  12901. (fh "%02d:%02d")
  12902. y d h m align)
  12903. (if havetime
  12904. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12905. y 0
  12906. d (floor (/ diff ds)) diff (mod diff ds)
  12907. h (floor (/ diff hs)) diff (mod diff hs)
  12908. m (floor (/ diff 60)))
  12909. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  12910. y 0
  12911. d (floor (+ (/ diff ds) 0.5))
  12912. h 0 m 0))
  12913. (if (not to-buffer)
  12914. (message "%s" (org-make-tdiff-string y d h m))
  12915. (if (org-at-table-p)
  12916. (progn
  12917. (goto-char match-end)
  12918. (setq align t)
  12919. (and (looking-at " *|") (goto-char (match-end 0))))
  12920. (goto-char match-end))
  12921. (if (looking-at
  12922. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  12923. (replace-match ""))
  12924. (if negative (insert " -"))
  12925. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  12926. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  12927. (insert " " (format fh h m))))
  12928. (if align (org-table-align))
  12929. (message "Time difference inserted")))))
  12930. (defun org-make-tdiff-string (y d h m)
  12931. (let ((fmt "")
  12932. (l nil))
  12933. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  12934. l (push y l)))
  12935. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  12936. l (push d l)))
  12937. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  12938. l (push h l)))
  12939. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  12940. l (push m l)))
  12941. (apply 'format fmt (nreverse l))))
  12942. (defun org-time-string-to-time (s)
  12943. (apply 'encode-time (org-parse-time-string s)))
  12944. (defun org-time-string-to-seconds (s)
  12945. (org-float-time (org-time-string-to-time s)))
  12946. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  12947. "Convert a time stamp to an absolute day number.
  12948. If there is a specifyer for a cyclic time stamp, get the closest date to
  12949. DAYNR.
  12950. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  12951. the variable date is bound by the calendar when this is called."
  12952. (cond
  12953. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  12954. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  12955. daynr
  12956. (+ daynr 1000)))
  12957. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  12958. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  12959. (time-to-days (current-time))) (match-string 0 s)
  12960. prefer show-all))
  12961. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  12962. (defun org-days-to-iso-week (days)
  12963. "Return the iso week number."
  12964. (require 'cal-iso)
  12965. (car (calendar-iso-from-absolute days)))
  12966. (defun org-small-year-to-year (year)
  12967. "Convert 2-digit years into 4-digit years.
  12968. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  12969. The year 2000 cannot be abbreviated. Any year larger than 99
  12970. is returned unchanged."
  12971. (if (< year 38)
  12972. (setq year (+ 2000 year))
  12973. (if (< year 100)
  12974. (setq year (+ 1900 year))))
  12975. year)
  12976. (defun org-time-from-absolute (d)
  12977. "Return the time corresponding to date D.
  12978. D may be an absolute day number, or a calendar-type list (month day year)."
  12979. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  12980. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  12981. (defun org-calendar-holiday ()
  12982. "List of holidays, for Diary display in Org-mode."
  12983. (require 'holidays)
  12984. (let ((hl (funcall
  12985. (if (fboundp 'calendar-check-holidays)
  12986. 'calendar-check-holidays 'check-calendar-holidays) date)))
  12987. (if hl (mapconcat 'identity hl "; "))))
  12988. (defun org-diary-sexp-entry (sexp entry date)
  12989. "Process a SEXP diary ENTRY for DATE."
  12990. (require 'diary-lib)
  12991. (let ((result (if calendar-debug-sexp
  12992. (let ((stack-trace-on-error t))
  12993. (eval (car (read-from-string sexp))))
  12994. (condition-case nil
  12995. (eval (car (read-from-string sexp)))
  12996. (error
  12997. (beep)
  12998. (message "Bad sexp at line %d in %s: %s"
  12999. (org-current-line)
  13000. (buffer-file-name) sexp)
  13001. (sleep-for 2))))))
  13002. (cond ((stringp result) result)
  13003. ((and (consp result)
  13004. (stringp (cdr result))) (cdr result))
  13005. (result entry)
  13006. (t nil))))
  13007. (defun org-diary-to-ical-string (frombuf)
  13008. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13009. This uses the icalendar.el library."
  13010. (let* ((tmpdir (if (featurep 'xemacs)
  13011. (temp-directory)
  13012. temporary-file-directory))
  13013. (tmpfile (make-temp-name
  13014. (expand-file-name "orgics" tmpdir)))
  13015. buf rtn b e)
  13016. (with-current-buffer frombuf
  13017. (icalendar-export-region (point-min) (point-max) tmpfile)
  13018. (setq buf (find-buffer-visiting tmpfile))
  13019. (set-buffer buf)
  13020. (goto-char (point-min))
  13021. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13022. (setq b (match-beginning 0)))
  13023. (goto-char (point-max))
  13024. (if (re-search-backward "^END:VEVENT" nil t)
  13025. (setq e (match-end 0)))
  13026. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13027. (kill-buffer buf)
  13028. (delete-file tmpfile)
  13029. rtn))
  13030. (defun org-closest-date (start current change prefer show-all)
  13031. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13032. When PREFER is `past' return a date that is either CURRENT or past.
  13033. When PREFER is `future', return a date that is either CURRENT or future.
  13034. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13035. ;; Make the proper lists from the dates
  13036. (catch 'exit
  13037. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13038. dn dw sday cday n1 n2 n0
  13039. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13040. (setq start (org-date-to-gregorian start)
  13041. current (org-date-to-gregorian
  13042. (if show-all
  13043. current
  13044. (time-to-days (current-time))))
  13045. sday (calendar-absolute-from-gregorian start)
  13046. cday (calendar-absolute-from-gregorian current))
  13047. (if (<= cday sday) (throw 'exit sday))
  13048. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13049. (setq dn (string-to-number (match-string 1 change))
  13050. dw (cdr (assoc (match-string 2 change) a1)))
  13051. (error "Invalid change specifyer: %s" change))
  13052. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13053. (cond
  13054. ((eq dw 'day)
  13055. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13056. n2 (+ n1 dn)))
  13057. ((eq dw 'year)
  13058. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13059. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13060. (setq date1 (list m d y1)
  13061. n1 (calendar-absolute-from-gregorian date1)
  13062. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13063. n2 (calendar-absolute-from-gregorian date2)))
  13064. ((eq dw 'month)
  13065. ;; approx number of month between the two dates
  13066. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13067. ;; How often does dn fit in there?
  13068. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13069. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13070. m (+ m nm)
  13071. ny (floor (/ m 12))
  13072. y (+ y ny)
  13073. m (- m (* ny 12)))
  13074. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13075. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13076. (setq m2 (+ m dn) y2 y)
  13077. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13078. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13079. (while (<= n2 cday)
  13080. (setq n1 n2 m m2 y y2)
  13081. (setq m2 (+ m dn) y2 y)
  13082. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13083. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  13084. ;; Make sure n1 is the earlier date
  13085. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  13086. (if show-all
  13087. (cond
  13088. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13089. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13090. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  13091. (cond
  13092. ((eq prefer 'past) (if (= cday n2) n2 n1))
  13093. ((eq prefer 'future) (if (= cday n1) n1 n2))
  13094. (t (if (= cday n1) n1 n2)))))))
  13095. (defun org-date-to-gregorian (date)
  13096. "Turn any specification of DATE into a gregorian date for the calendar."
  13097. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  13098. ((and (listp date) (= (length date) 3)) date)
  13099. ((stringp date)
  13100. (setq date (org-parse-time-string date))
  13101. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  13102. ((listp date)
  13103. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  13104. (defun org-parse-time-string (s &optional nodefault)
  13105. "Parse the standard Org-mode time string.
  13106. This should be a lot faster than the normal `parse-time-string'.
  13107. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  13108. hour and minute fields will be nil if not given."
  13109. (if (string-match org-ts-regexp0 s)
  13110. (list 0
  13111. (if (or (match-beginning 8) (not nodefault))
  13112. (string-to-number (or (match-string 8 s) "0")))
  13113. (if (or (match-beginning 7) (not nodefault))
  13114. (string-to-number (or (match-string 7 s) "0")))
  13115. (string-to-number (match-string 4 s))
  13116. (string-to-number (match-string 3 s))
  13117. (string-to-number (match-string 2 s))
  13118. nil nil nil)
  13119. (error "Not a standard Org-mode time string: %s" s)))
  13120. (defun org-timestamp-up (&optional arg)
  13121. "Increase the date item at the cursor by one.
  13122. If the cursor is on the year, change the year. If it is on the month or
  13123. the day, change that.
  13124. With prefix ARG, change by that many units."
  13125. (interactive "p")
  13126. (org-timestamp-change (prefix-numeric-value arg)))
  13127. (defun org-timestamp-down (&optional arg)
  13128. "Decrease the date item at the cursor by one.
  13129. If the cursor is on the year, change the year. If it is on the month or
  13130. the day, change that.
  13131. With prefix ARG, change by that many units."
  13132. (interactive "p")
  13133. (org-timestamp-change (- (prefix-numeric-value arg))))
  13134. (defun org-timestamp-up-day (&optional arg)
  13135. "Increase the date in the time stamp by one day.
  13136. With prefix ARG, change that many days."
  13137. (interactive "p")
  13138. (if (and (not (org-at-timestamp-p t))
  13139. (org-on-heading-p))
  13140. (org-todo 'up)
  13141. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  13142. (defun org-timestamp-down-day (&optional arg)
  13143. "Decrease the date in the time stamp by one day.
  13144. With prefix ARG, change that many days."
  13145. (interactive "p")
  13146. (if (and (not (org-at-timestamp-p t))
  13147. (org-on-heading-p))
  13148. (org-todo 'down)
  13149. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  13150. (defun org-at-timestamp-p (&optional inactive-ok)
  13151. "Determine if the cursor is in or at a timestamp."
  13152. (interactive)
  13153. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  13154. (pos (point))
  13155. (ans (or (looking-at tsr)
  13156. (save-excursion
  13157. (skip-chars-backward "^[<\n\r\t")
  13158. (if (> (point) (point-min)) (backward-char 1))
  13159. (and (looking-at tsr)
  13160. (> (- (match-end 0) pos) -1))))))
  13161. (and ans
  13162. (boundp 'org-ts-what)
  13163. (setq org-ts-what
  13164. (cond
  13165. ((= pos (match-beginning 0)) 'bracket)
  13166. ((= pos (1- (match-end 0))) 'bracket)
  13167. ((org-pos-in-match-range pos 2) 'year)
  13168. ((org-pos-in-match-range pos 3) 'month)
  13169. ((org-pos-in-match-range pos 7) 'hour)
  13170. ((org-pos-in-match-range pos 8) 'minute)
  13171. ((or (org-pos-in-match-range pos 4)
  13172. (org-pos-in-match-range pos 5)) 'day)
  13173. ((and (> pos (or (match-end 8) (match-end 5)))
  13174. (< pos (match-end 0)))
  13175. (- pos (or (match-end 8) (match-end 5))))
  13176. (t 'day))))
  13177. ans))
  13178. (defun org-toggle-timestamp-type ()
  13179. "Toggle the type (<active> or [inactive]) of a time stamp."
  13180. (interactive)
  13181. (when (org-at-timestamp-p t)
  13182. (let ((beg (match-beginning 0)) (end (match-end 0))
  13183. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  13184. (save-excursion
  13185. (goto-char beg)
  13186. (while (re-search-forward "[][<>]" end t)
  13187. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  13188. t t)))
  13189. (message "Timestamp is now %sactive"
  13190. (if (equal (char-after beg) ?<) "" "in")))))
  13191. (defun org-timestamp-change (n &optional what)
  13192. "Change the date in the time stamp at point.
  13193. The date will be changed by N times WHAT. WHAT can be `day', `month',
  13194. `year', `minute', `second'. If WHAT is not given, the cursor position
  13195. in the timestamp determines what will be changed."
  13196. (let ((pos (point))
  13197. with-hm inactive
  13198. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  13199. org-ts-what
  13200. extra rem
  13201. ts time time0)
  13202. (if (not (org-at-timestamp-p t))
  13203. (error "Not at a timestamp"))
  13204. (if (and (not what) (eq org-ts-what 'bracket))
  13205. (org-toggle-timestamp-type)
  13206. (if (and (not what) (not (eq org-ts-what 'day))
  13207. org-display-custom-times
  13208. (get-text-property (point) 'display)
  13209. (not (get-text-property (1- (point)) 'display)))
  13210. (setq org-ts-what 'day))
  13211. (setq org-ts-what (or what org-ts-what)
  13212. inactive (= (char-after (match-beginning 0)) ?\[)
  13213. ts (match-string 0))
  13214. (replace-match "")
  13215. (if (string-match
  13216. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  13217. ts)
  13218. (setq extra (match-string 1 ts)))
  13219. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  13220. (setq with-hm t))
  13221. (setq time0 (org-parse-time-string ts))
  13222. (when (and (eq org-ts-what 'minute)
  13223. (eq current-prefix-arg nil))
  13224. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  13225. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  13226. (setcar (cdr time0) (+ (nth 1 time0)
  13227. (if (> n 0) (- rem) (- dm rem))))))
  13228. (setq time
  13229. (encode-time (or (car time0) 0)
  13230. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  13231. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  13232. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  13233. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  13234. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  13235. (nthcdr 6 time0)))
  13236. (when (and (member org-ts-what '(hour minute))
  13237. extra
  13238. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  13239. (setq extra (org-modify-ts-extra
  13240. extra
  13241. (if (eq org-ts-what 'hour) 2 5)
  13242. n dm)))
  13243. (when (integerp org-ts-what)
  13244. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  13245. (if (eq what 'calendar)
  13246. (let ((cal-date (org-get-date-from-calendar)))
  13247. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  13248. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  13249. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  13250. (setcar time0 (or (car time0) 0))
  13251. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  13252. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  13253. (setq time (apply 'encode-time time0))))
  13254. (setq org-last-changed-timestamp
  13255. (org-insert-time-stamp time with-hm inactive nil nil extra))
  13256. (org-clock-update-time-maybe)
  13257. (goto-char pos)
  13258. ;; Try to recenter the calendar window, if any
  13259. (if (and org-calendar-follow-timestamp-change
  13260. (get-buffer-window "*Calendar*" t)
  13261. (memq org-ts-what '(day month year)))
  13262. (org-recenter-calendar (time-to-days time))))))
  13263. (defun org-modify-ts-extra (s pos n dm)
  13264. "Change the different parts of the lead-time and repeat fields in timestamp."
  13265. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  13266. ng h m new rem)
  13267. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  13268. (cond
  13269. ((or (org-pos-in-match-range pos 2)
  13270. (org-pos-in-match-range pos 3))
  13271. (setq m (string-to-number (match-string 3 s))
  13272. h (string-to-number (match-string 2 s)))
  13273. (if (org-pos-in-match-range pos 2)
  13274. (setq h (+ h n))
  13275. (setq n (* dm (org-no-warnings (signum n))))
  13276. (when (not (= 0 (setq rem (% m dm))))
  13277. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  13278. (setq m (+ m n)))
  13279. (if (< m 0) (setq m (+ m 60) h (1- h)))
  13280. (if (> m 59) (setq m (- m 60) h (1+ h)))
  13281. (setq h (min 24 (max 0 h)))
  13282. (setq ng 1 new (format "-%02d:%02d" h m)))
  13283. ((org-pos-in-match-range pos 6)
  13284. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  13285. ((org-pos-in-match-range pos 5)
  13286. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  13287. ((org-pos-in-match-range pos 9)
  13288. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  13289. ((org-pos-in-match-range pos 8)
  13290. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  13291. (when ng
  13292. (setq s (concat
  13293. (substring s 0 (match-beginning ng))
  13294. new
  13295. (substring s (match-end ng))))))
  13296. s))
  13297. (defun org-recenter-calendar (date)
  13298. "If the calendar is visible, recenter it to DATE."
  13299. (let* ((win (selected-window))
  13300. (cwin (get-buffer-window "*Calendar*" t))
  13301. (calendar-move-hook nil))
  13302. (when cwin
  13303. (select-window cwin)
  13304. (calendar-goto-date (if (listp date) date
  13305. (calendar-gregorian-from-absolute date)))
  13306. (select-window win))))
  13307. (defun org-goto-calendar (&optional arg)
  13308. "Go to the Emacs calendar at the current date.
  13309. If there is a time stamp in the current line, go to that date.
  13310. A prefix ARG can be used to force the current date."
  13311. (interactive "P")
  13312. (let ((tsr org-ts-regexp) diff
  13313. (calendar-move-hook nil)
  13314. (calendar-view-holidays-initially-flag nil)
  13315. (view-calendar-holidays-initially nil)
  13316. (calendar-view-diary-initially-flag nil)
  13317. (view-diary-entries-initially nil))
  13318. (if (or (org-at-timestamp-p)
  13319. (save-excursion
  13320. (beginning-of-line 1)
  13321. (looking-at (concat ".*" tsr))))
  13322. (let ((d1 (time-to-days (current-time)))
  13323. (d2 (time-to-days
  13324. (org-time-string-to-time (match-string 1)))))
  13325. (setq diff (- d2 d1))))
  13326. (calendar)
  13327. (calendar-goto-today)
  13328. (if (and diff (not arg)) (calendar-forward-day diff))))
  13329. (defun org-get-date-from-calendar ()
  13330. "Return a list (month day year) of date at point in calendar."
  13331. (with-current-buffer "*Calendar*"
  13332. (save-match-data
  13333. (calendar-cursor-to-date))))
  13334. (defun org-date-from-calendar ()
  13335. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  13336. If there is already a time stamp at the cursor position, update it."
  13337. (interactive)
  13338. (if (org-at-timestamp-p t)
  13339. (org-timestamp-change 0 'calendar)
  13340. (let ((cal-date (org-get-date-from-calendar)))
  13341. (org-insert-time-stamp
  13342. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  13343. (defun org-minutes-to-hh:mm-string (m)
  13344. "Compute H:MM from a number of minutes."
  13345. (let ((h (/ m 60)))
  13346. (setq m (- m (* 60 h)))
  13347. (format org-time-clocksum-format h m)))
  13348. (defun org-hh:mm-string-to-minutes (s)
  13349. "Convert a string H:MM to a number of minutes.
  13350. If the string is just a number, interpret it as minutes.
  13351. In fact, the first hh:mm or number in the string will be taken,
  13352. there can be extra stuff in the string.
  13353. If no number is found, the return value is 0."
  13354. (cond
  13355. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  13356. (+ (* (string-to-number (match-string 1 s)) 60)
  13357. (string-to-number (match-string 2 s))))
  13358. ((string-match "\\([0-9]+\\)" s)
  13359. (string-to-number (match-string 1 s)))
  13360. (t 0)))
  13361. ;;;; Files
  13362. (defun org-save-all-org-buffers ()
  13363. "Save all Org-mode buffers without user confirmation."
  13364. (interactive)
  13365. (message "Saving all Org-mode buffers...")
  13366. (save-some-buffers t 'org-mode-p)
  13367. (when (featurep 'org-id) (org-id-locations-save))
  13368. (message "Saving all Org-mode buffers... done"))
  13369. (defun org-revert-all-org-buffers ()
  13370. "Revert all Org-mode buffers.
  13371. Prompt for confirmation when there are unsaved changes.
  13372. Be sure you know what you are doing before letting this function
  13373. overwrite your changes.
  13374. This function is useful in a setup where one tracks org files
  13375. with a version control system, to revert on one machine after pulling
  13376. changes from another. I believe the procedure must be like this:
  13377. 1. M-x org-save-all-org-buffers
  13378. 2. Pull changes from the other machine, resolve conflicts
  13379. 3. M-x org-revert-all-org-buffers"
  13380. (interactive)
  13381. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  13382. (error "Abort"))
  13383. (save-excursion
  13384. (save-window-excursion
  13385. (mapc
  13386. (lambda (b)
  13387. (when (and (with-current-buffer b (org-mode-p))
  13388. (with-current-buffer b buffer-file-name))
  13389. (switch-to-buffer b)
  13390. (revert-buffer t 'no-confirm)))
  13391. (buffer-list))
  13392. (when (and (featurep 'org-id) org-id-track-globally)
  13393. (org-id-locations-load)))))
  13394. ;;;; Agenda files
  13395. ;;;###autoload
  13396. (defun org-iswitchb (&optional arg)
  13397. "Use `org-icompleting-read' to prompt for an Org buffer to switch to.
  13398. With a prefix argument, restrict available to files.
  13399. With two prefix arguments, restrict available buffers to agenda files."
  13400. (interactive "P")
  13401. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  13402. ((equal arg '(16)) (org-buffer-list 'agenda))
  13403. (t (org-buffer-list)))))
  13404. (switch-to-buffer
  13405. (org-icompleting-read "Org buffer: "
  13406. (mapcar 'list (mapcar 'buffer-name blist))
  13407. nil t))))
  13408. ;;;###autoload
  13409. (defalias 'org-ido-switchb 'org-iswitchb)
  13410. (defun org-buffer-list (&optional predicate exclude-tmp)
  13411. "Return a list of Org buffers.
  13412. PREDICATE can be `export', `files' or `agenda'.
  13413. export restrict the list to Export buffers.
  13414. files restrict the list to buffers visiting Org files.
  13415. agenda restrict the list to buffers visiting agenda files.
  13416. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  13417. (let* ((bfn nil)
  13418. (agenda-files (and (eq predicate 'agenda)
  13419. (mapcar 'file-truename (org-agenda-files t))))
  13420. (filter
  13421. (cond
  13422. ((eq predicate 'files)
  13423. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  13424. ((eq predicate 'export)
  13425. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  13426. ((eq predicate 'agenda)
  13427. (lambda (b)
  13428. (with-current-buffer b
  13429. (and (eq major-mode 'org-mode)
  13430. (setq bfn (buffer-file-name b))
  13431. (member (file-truename bfn) agenda-files)))))
  13432. (t (lambda (b) (with-current-buffer b
  13433. (or (eq major-mode 'org-mode)
  13434. (string-match "\*Org .*Export"
  13435. (buffer-name b)))))))))
  13436. (delq nil
  13437. (mapcar
  13438. (lambda(b)
  13439. (if (and (funcall filter b)
  13440. (or (not exclude-tmp)
  13441. (not (string-match "tmp" (buffer-name b)))))
  13442. b
  13443. nil))
  13444. (buffer-list)))))
  13445. (defun org-agenda-files (&optional unrestricted archives)
  13446. "Get the list of agenda files.
  13447. Optional UNRESTRICTED means return the full list even if a restriction
  13448. is currently in place.
  13449. When ARCHIVES is t, include all archive files that are really being
  13450. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  13451. `org-agenda-archives-mode' is t."
  13452. (let ((files
  13453. (cond
  13454. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  13455. ((stringp org-agenda-files) (org-read-agenda-file-list))
  13456. ((listp org-agenda-files) org-agenda-files)
  13457. (t (error "Invalid value of `org-agenda-files'")))))
  13458. (setq files (apply 'append
  13459. (mapcar (lambda (f)
  13460. (if (file-directory-p f)
  13461. (directory-files
  13462. f t org-agenda-file-regexp)
  13463. (list f)))
  13464. files)))
  13465. (when org-agenda-skip-unavailable-files
  13466. (setq files (delq nil
  13467. (mapcar (function
  13468. (lambda (file)
  13469. (and (file-readable-p file) file)))
  13470. files))))
  13471. (when (or (eq archives t)
  13472. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  13473. (setq files (org-add-archive-files files)))
  13474. files))
  13475. (defun org-edit-agenda-file-list ()
  13476. "Edit the list of agenda files.
  13477. Depending on setup, this either uses customize to edit the variable
  13478. `org-agenda-files', or it visits the file that is holding the list. In the
  13479. latter case, the buffer is set up in a way that saving it automatically kills
  13480. the buffer and restores the previous window configuration."
  13481. (interactive)
  13482. (if (stringp org-agenda-files)
  13483. (let ((cw (current-window-configuration)))
  13484. (find-file org-agenda-files)
  13485. (org-set-local 'org-window-configuration cw)
  13486. (org-add-hook 'after-save-hook
  13487. (lambda ()
  13488. (set-window-configuration
  13489. (prog1 org-window-configuration
  13490. (kill-buffer (current-buffer))))
  13491. (org-install-agenda-files-menu)
  13492. (message "New agenda file list installed"))
  13493. nil 'local)
  13494. (message "%s" (substitute-command-keys
  13495. "Edit list and finish with \\[save-buffer]")))
  13496. (customize-variable 'org-agenda-files)))
  13497. (defun org-store-new-agenda-file-list (list)
  13498. "Set new value for the agenda file list and save it correctly."
  13499. (if (stringp org-agenda-files)
  13500. (let ((fe (org-read-agenda-file-list t)) b u)
  13501. (while (setq b (find-buffer-visiting org-agenda-files))
  13502. (kill-buffer b))
  13503. (with-temp-file org-agenda-files
  13504. (insert
  13505. (mapconcat
  13506. (lambda (f) ;; Keep un-expanded entries.
  13507. (if (setq u (assoc f fe))
  13508. (cdr u)
  13509. f))
  13510. list "\n")
  13511. "\n")))
  13512. (let ((org-mode-hook nil) (org-inhibit-startup t)
  13513. (org-insert-mode-line-in-empty-file nil))
  13514. (setq org-agenda-files list)
  13515. (customize-save-variable 'org-agenda-files org-agenda-files))))
  13516. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  13517. "Read the list of agenda files from a file.
  13518. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  13519. filenames, used by `org-store-new-agenda-file-list' to write back
  13520. un-expanded file names."
  13521. (when (file-directory-p org-agenda-files)
  13522. (error "`org-agenda-files' cannot be a single directory"))
  13523. (when (stringp org-agenda-files)
  13524. (with-temp-buffer
  13525. (insert-file-contents org-agenda-files)
  13526. (mapcar
  13527. (lambda (f)
  13528. (let ((e (expand-file-name (substitute-in-file-name f)
  13529. org-directory)))
  13530. (if pair-with-expansion
  13531. (cons e f)
  13532. e)))
  13533. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  13534. ;;;###autoload
  13535. (defun org-cycle-agenda-files ()
  13536. "Cycle through the files in `org-agenda-files'.
  13537. If the current buffer visits an agenda file, find the next one in the list.
  13538. If the current buffer does not, find the first agenda file."
  13539. (interactive)
  13540. (let* ((fs (org-agenda-files t))
  13541. (files (append fs (list (car fs))))
  13542. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  13543. file)
  13544. (unless files (error "No agenda files"))
  13545. (catch 'exit
  13546. (while (setq file (pop files))
  13547. (if (equal (file-truename file) tcf)
  13548. (when (car files)
  13549. (find-file (car files))
  13550. (throw 'exit t))))
  13551. (find-file (car fs)))
  13552. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  13553. (defun org-agenda-file-to-front (&optional to-end)
  13554. "Move/add the current file to the top of the agenda file list.
  13555. If the file is not present in the list, it is added to the front. If it is
  13556. present, it is moved there. With optional argument TO-END, add/move to the
  13557. end of the list."
  13558. (interactive "P")
  13559. (let ((org-agenda-skip-unavailable-files nil)
  13560. (file-alist (mapcar (lambda (x)
  13561. (cons (file-truename x) x))
  13562. (org-agenda-files t)))
  13563. (ctf (file-truename buffer-file-name))
  13564. x had)
  13565. (setq x (assoc ctf file-alist) had x)
  13566. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  13567. (if to-end
  13568. (setq file-alist (append (delq x file-alist) (list x)))
  13569. (setq file-alist (cons x (delq x file-alist))))
  13570. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  13571. (org-install-agenda-files-menu)
  13572. (message "File %s to %s of agenda file list"
  13573. (if had "moved" "added") (if to-end "end" "front"))))
  13574. (defun org-remove-file (&optional file)
  13575. "Remove current file from the list of files in variable `org-agenda-files'.
  13576. These are the files which are being checked for agenda entries.
  13577. Optional argument FILE means use this file instead of the current."
  13578. (interactive)
  13579. (let* ((org-agenda-skip-unavailable-files nil)
  13580. (file (or file buffer-file-name))
  13581. (true-file (file-truename file))
  13582. (afile (abbreviate-file-name file))
  13583. (files (delq nil (mapcar
  13584. (lambda (x)
  13585. (if (equal true-file
  13586. (file-truename x))
  13587. nil x))
  13588. (org-agenda-files t)))))
  13589. (if (not (= (length files) (length (org-agenda-files t))))
  13590. (progn
  13591. (org-store-new-agenda-file-list files)
  13592. (org-install-agenda-files-menu)
  13593. (message "Removed file: %s" afile))
  13594. (message "File was not in list: %s (not removed)" afile))))
  13595. (defun org-file-menu-entry (file)
  13596. (vector file (list 'find-file file) t))
  13597. (defun org-check-agenda-file (file)
  13598. "Make sure FILE exists. If not, ask user what to do."
  13599. (when (not (file-exists-p file))
  13600. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  13601. (abbreviate-file-name file))
  13602. (let ((r (downcase (read-char-exclusive))))
  13603. (cond
  13604. ((equal r ?r)
  13605. (org-remove-file file)
  13606. (throw 'nextfile t))
  13607. (t (error "Abort"))))))
  13608. (defun org-get-agenda-file-buffer (file)
  13609. "Get a buffer visiting FILE. If the buffer needs to be created, add
  13610. it to the list of buffers which might be released later."
  13611. (let ((buf (org-find-base-buffer-visiting file)))
  13612. (if buf
  13613. buf ; just return it
  13614. ;; Make a new buffer and remember it
  13615. (setq buf (find-file-noselect file))
  13616. (if buf (push buf org-agenda-new-buffers))
  13617. buf)))
  13618. (defun org-release-buffers (blist)
  13619. "Release all buffers in list, asking the user for confirmation when needed.
  13620. When a buffer is unmodified, it is just killed. When modified, it is saved
  13621. \(if the user agrees) and then killed."
  13622. (let (buf file)
  13623. (while (setq buf (pop blist))
  13624. (setq file (buffer-file-name buf))
  13625. (when (and (buffer-modified-p buf)
  13626. file
  13627. (y-or-n-p (format "Save file %s? " file)))
  13628. (with-current-buffer buf (save-buffer)))
  13629. (kill-buffer buf))))
  13630. (defun org-prepare-agenda-buffers (files)
  13631. "Create buffers for all agenda files, protect archived trees and comments."
  13632. (interactive)
  13633. (let ((pa '(:org-archived t))
  13634. (pc '(:org-comment t))
  13635. (pall '(:org-archived t :org-comment t))
  13636. (inhibit-read-only t)
  13637. (rea (concat ":" org-archive-tag ":"))
  13638. bmp file re)
  13639. (save-excursion
  13640. (save-restriction
  13641. (while (setq file (pop files))
  13642. (catch 'nextfile
  13643. (if (bufferp file)
  13644. (set-buffer file)
  13645. (org-check-agenda-file file)
  13646. (set-buffer (org-get-agenda-file-buffer file)))
  13647. (widen)
  13648. (setq bmp (buffer-modified-p))
  13649. (org-refresh-category-properties)
  13650. (setq org-todo-keywords-for-agenda
  13651. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  13652. (setq org-done-keywords-for-agenda
  13653. (append org-done-keywords-for-agenda org-done-keywords))
  13654. (setq org-todo-keyword-alist-for-agenda
  13655. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  13656. (setq org-drawers-for-agenda
  13657. (append org-drawers-for-agenda org-drawers))
  13658. (setq org-tag-alist-for-agenda
  13659. (append org-tag-alist-for-agenda org-tag-alist))
  13660. (save-excursion
  13661. (remove-text-properties (point-min) (point-max) pall)
  13662. (when org-agenda-skip-archived-trees
  13663. (goto-char (point-min))
  13664. (while (re-search-forward rea nil t)
  13665. (if (org-on-heading-p t)
  13666. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  13667. (goto-char (point-min))
  13668. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  13669. (while (re-search-forward re nil t)
  13670. (add-text-properties
  13671. (match-beginning 0) (org-end-of-subtree t) pc)))
  13672. (set-buffer-modified-p bmp)))))
  13673. (setq org-todo-keywords-for-agenda
  13674. (org-uniquify org-todo-keywords-for-agenda))
  13675. (setq org-todo-keyword-alist-for-agenda
  13676. (org-uniquify org-todo-keyword-alist-for-agenda)
  13677. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  13678. ;;;; Embedded LaTeX
  13679. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  13680. "Keymap for the minor `org-cdlatex-mode'.")
  13681. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  13682. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  13683. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  13684. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  13685. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  13686. (defvar org-cdlatex-texmathp-advice-is-done nil
  13687. "Flag remembering if we have applied the advice to texmathp already.")
  13688. (define-minor-mode org-cdlatex-mode
  13689. "Toggle the minor `org-cdlatex-mode'.
  13690. This mode supports entering LaTeX environment and math in LaTeX fragments
  13691. in Org-mode.
  13692. \\{org-cdlatex-mode-map}"
  13693. nil " OCDL" nil
  13694. (when org-cdlatex-mode (require 'cdlatex))
  13695. (unless org-cdlatex-texmathp-advice-is-done
  13696. (setq org-cdlatex-texmathp-advice-is-done t)
  13697. (defadvice texmathp (around org-math-always-on activate)
  13698. "Always return t in org-mode buffers.
  13699. This is because we want to insert math symbols without dollars even outside
  13700. the LaTeX math segments. If Orgmode thinks that point is actually inside
  13701. an embedded LaTeX fragment, let texmathp do its job.
  13702. \\[org-cdlatex-mode-map]"
  13703. (interactive)
  13704. (let (p)
  13705. (cond
  13706. ((not (org-mode-p)) ad-do-it)
  13707. ((eq this-command 'cdlatex-math-symbol)
  13708. (setq ad-return-value t
  13709. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  13710. (t
  13711. (let ((p (org-inside-LaTeX-fragment-p)))
  13712. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  13713. (setq ad-return-value t
  13714. texmathp-why '("Org-mode embedded math" . 0))
  13715. (if p ad-do-it)))))))))
  13716. (defun turn-on-org-cdlatex ()
  13717. "Unconditionally turn on `org-cdlatex-mode'."
  13718. (org-cdlatex-mode 1))
  13719. (defun org-inside-LaTeX-fragment-p ()
  13720. "Test if point is inside a LaTeX fragment.
  13721. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  13722. sequence appearing also before point.
  13723. Even though the matchers for math are configurable, this function assumes
  13724. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  13725. delimiters are skipped when they have been removed by customization.
  13726. The return value is nil, or a cons cell with the delimiter and
  13727. and the position of this delimiter.
  13728. This function does a reasonably good job, but can locally be fooled by
  13729. for example currency specifications. For example it will assume being in
  13730. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  13731. fragments that are properly closed, but during editing, we have to live
  13732. with the uncertainty caused by missing closing delimiters. This function
  13733. looks only before point, not after."
  13734. (catch 'exit
  13735. (let ((pos (point))
  13736. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  13737. (lim (progn
  13738. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  13739. (point)))
  13740. dd-on str (start 0) m re)
  13741. (goto-char pos)
  13742. (when dodollar
  13743. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  13744. re (nth 1 (assoc "$" org-latex-regexps)))
  13745. (while (string-match re str start)
  13746. (cond
  13747. ((= (match-end 0) (length str))
  13748. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  13749. ((= (match-end 0) (- (length str) 5))
  13750. (throw 'exit nil))
  13751. (t (setq start (match-end 0))))))
  13752. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  13753. (goto-char pos)
  13754. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  13755. (and (match-beginning 2) (throw 'exit nil))
  13756. ;; count $$
  13757. (while (re-search-backward "\\$\\$" lim t)
  13758. (setq dd-on (not dd-on)))
  13759. (goto-char pos)
  13760. (if dd-on (cons "$$" m))))))
  13761. (defun org-inside-latex-macro-p ()
  13762. "Is point inside a LaTeX macro or its arguments?"
  13763. (save-match-data
  13764. (org-in-regexp
  13765. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  13766. (defun test ()
  13767. (interactive)
  13768. (message "%s" (org-inside-latex-macro-p)))
  13769. (defun org-try-cdlatex-tab ()
  13770. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  13771. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  13772. - inside a LaTeX fragment, or
  13773. - after the first word in a line, where an abbreviation expansion could
  13774. insert a LaTeX environment."
  13775. (when org-cdlatex-mode
  13776. (cond
  13777. ((save-excursion
  13778. (skip-chars-backward "a-zA-Z0-9*")
  13779. (skip-chars-backward " \t")
  13780. (bolp))
  13781. (cdlatex-tab) t)
  13782. ((org-inside-LaTeX-fragment-p)
  13783. (cdlatex-tab) t)
  13784. (t nil))))
  13785. (defun org-cdlatex-underscore-caret (&optional arg)
  13786. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  13787. Revert to the normal definition outside of these fragments."
  13788. (interactive "P")
  13789. (if (org-inside-LaTeX-fragment-p)
  13790. (call-interactively 'cdlatex-sub-superscript)
  13791. (let (org-cdlatex-mode)
  13792. (call-interactively (key-binding (vector last-input-event))))))
  13793. (defun org-cdlatex-math-modify (&optional arg)
  13794. "Execute `cdlatex-math-modify' in LaTeX fragments.
  13795. Revert to the normal definition outside of these fragments."
  13796. (interactive "P")
  13797. (if (org-inside-LaTeX-fragment-p)
  13798. (call-interactively 'cdlatex-math-modify)
  13799. (let (org-cdlatex-mode)
  13800. (call-interactively (key-binding (vector last-input-event))))))
  13801. (defvar org-latex-fragment-image-overlays nil
  13802. "List of overlays carrying the images of latex fragments.")
  13803. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  13804. (defun org-remove-latex-fragment-image-overlays ()
  13805. "Remove all overlays with LaTeX fragment images in current buffer."
  13806. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  13807. (setq org-latex-fragment-image-overlays nil))
  13808. (defun org-preview-latex-fragment (&optional subtree)
  13809. "Preview the LaTeX fragment at point, or all locally or globally.
  13810. If the cursor is in a LaTeX fragment, create the image and overlay
  13811. it over the source code. If there is no fragment at point, display
  13812. all fragments in the current text, from one headline to the next. With
  13813. prefix SUBTREE, display all fragments in the current subtree. With a
  13814. double prefix `C-u C-u', or when the cursor is before the first headline,
  13815. display all fragments in the buffer.
  13816. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  13817. (interactive "P")
  13818. (org-remove-latex-fragment-image-overlays)
  13819. (save-excursion
  13820. (save-restriction
  13821. (let (beg end at msg)
  13822. (cond
  13823. ((or (equal subtree '(16))
  13824. (not (save-excursion
  13825. (re-search-backward (concat "^" outline-regexp) nil t))))
  13826. (setq beg (point-min) end (point-max)
  13827. msg "Creating images for buffer...%s"))
  13828. ((equal subtree '(4))
  13829. (org-back-to-heading)
  13830. (setq beg (point) end (org-end-of-subtree t)
  13831. msg "Creating images for subtree...%s"))
  13832. (t
  13833. (if (setq at (org-inside-LaTeX-fragment-p))
  13834. (goto-char (max (point-min) (- (cdr at) 2)))
  13835. (org-back-to-heading))
  13836. (setq beg (point) end (progn (outline-next-heading) (point))
  13837. msg (if at "Creating image...%s"
  13838. "Creating images for entry...%s"))))
  13839. (message msg "")
  13840. (narrow-to-region beg end)
  13841. (goto-char beg)
  13842. (org-format-latex
  13843. (concat "ltxpng/" (file-name-sans-extension
  13844. (file-name-nondirectory
  13845. buffer-file-name)))
  13846. default-directory 'overlays msg at 'forbuffer)
  13847. (message msg "done. Use `C-c C-c' to remove images.")))))
  13848. (defvar org-latex-regexps
  13849. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  13850. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  13851. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  13852. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13853. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  13854. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  13855. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  13856. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  13857. "Regular expressions for matching embedded LaTeX.")
  13858. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  13859. "Replace LaTeX fragments with links to an image, and produce images.
  13860. Some of the options can be changed using the variable
  13861. `org-format-latex-options'."
  13862. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  13863. (let* ((prefixnodir (file-name-nondirectory prefix))
  13864. (absprefix (expand-file-name prefix dir))
  13865. (todir (file-name-directory absprefix))
  13866. (opt org-format-latex-options)
  13867. (matchers (plist-get opt :matchers))
  13868. (re-list org-latex-regexps)
  13869. (org-format-latex-header-extra
  13870. (plist-get (org-infile-export-plist) :latex-header-extra))
  13871. (cnt 0) txt hash link beg end re e checkdir
  13872. executables-checked
  13873. m n block linkfile movefile ov)
  13874. ;; Check the different regular expressions
  13875. (while (setq e (pop re-list))
  13876. (setq m (car e) re (nth 1 e) n (nth 2 e)
  13877. block (if (nth 3 e) "\n\n" ""))
  13878. (when (member m matchers)
  13879. (goto-char (point-min))
  13880. (while (re-search-forward re nil t)
  13881. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  13882. (not (get-text-property (match-beginning n)
  13883. 'org-protected))
  13884. (or (not overlays)
  13885. (not (eq (get-char-property (match-beginning n)
  13886. 'org-overlay-type)
  13887. 'org-latex-overlay))))
  13888. (setq txt (match-string n)
  13889. beg (match-beginning n) end (match-end n)
  13890. cnt (1+ cnt))
  13891. (let (print-length print-level) ; make sure full list is printed
  13892. (setq hash (sha1 (prin1-to-string
  13893. (list org-format-latex-header
  13894. org-format-latex-header-extra
  13895. org-export-latex-default-packages-alist
  13896. org-export-latex-packages-alist
  13897. org-format-latex-options
  13898. forbuffer txt)))
  13899. linkfile (format "%s_%s.png" prefix hash)
  13900. movefile (format "%s_%s.png" absprefix hash)))
  13901. (setq link (concat block "[[file:" linkfile "]]" block))
  13902. (if msg (message msg cnt))
  13903. (goto-char beg)
  13904. (unless checkdir ; make sure the directory exists
  13905. (setq checkdir t)
  13906. (or (file-directory-p todir) (make-directory todir)))
  13907. (unless executables-checked
  13908. (org-check-external-command
  13909. "latex" "needed to convert LaTeX fragments to images")
  13910. (org-check-external-command
  13911. "dvipng" "needed to convert LaTeX fragments to images")
  13912. (setq executables-checked t))
  13913. (unless (file-exists-p movefile)
  13914. (org-create-formula-image
  13915. txt movefile opt forbuffer))
  13916. (if overlays
  13917. (progn
  13918. (mapc (lambda (o)
  13919. (if (eq (org-overlay-get o 'org-overlay-type)
  13920. 'org-latex-overlay)
  13921. (org-delete-overlay o)))
  13922. (org-overlays-in beg end))
  13923. (setq ov (org-make-overlay beg end))
  13924. (org-overlay-put ov 'org-overlay-type 'org-latex-overlay)
  13925. (if (featurep 'xemacs)
  13926. (progn
  13927. (org-overlay-put ov 'invisible t)
  13928. (org-overlay-put
  13929. ov 'end-glyph
  13930. (make-glyph (vector 'png :file movefile))))
  13931. (org-overlay-put
  13932. ov 'display
  13933. (list 'image :type 'png :file movefile :ascent 'center)))
  13934. (push ov org-latex-fragment-image-overlays)
  13935. (goto-char end))
  13936. (delete-region beg end)
  13937. (insert (org-add-props link
  13938. (list 'org-latex-src
  13939. (replace-regexp-in-string "\"" "" txt)))))))))))
  13940. ;; This function borrows from Ganesh Swami's latex2png.el
  13941. (defun org-create-formula-image (string tofile options buffer)
  13942. "This calls dvipng."
  13943. (require 'org-latex)
  13944. (let* ((tmpdir (if (featurep 'xemacs)
  13945. (temp-directory)
  13946. temporary-file-directory))
  13947. (texfilebase (make-temp-name
  13948. (expand-file-name "orgtex" tmpdir)))
  13949. (texfile (concat texfilebase ".tex"))
  13950. (dvifile (concat texfilebase ".dvi"))
  13951. (pngfile (concat texfilebase ".png"))
  13952. (fnh (if (featurep 'xemacs)
  13953. (font-height (get-face-font 'default))
  13954. (face-attribute 'default :height nil)))
  13955. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  13956. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  13957. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  13958. "Black"))
  13959. (bg (or (plist-get options (if buffer :background :html-background))
  13960. "Transparent")))
  13961. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  13962. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  13963. (with-temp-file texfile
  13964. (insert (org-splice-latex-header
  13965. org-format-latex-header
  13966. org-export-latex-default-packages-alist
  13967. org-export-latex-packages-alist
  13968. org-format-latex-header-extra))
  13969. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  13970. (require 'org-latex)
  13971. (org-export-latex-fix-inputenc))
  13972. (let ((dir default-directory))
  13973. (condition-case nil
  13974. (progn
  13975. (cd tmpdir)
  13976. (call-process "latex" nil nil nil texfile))
  13977. (error nil))
  13978. (cd dir))
  13979. (if (not (file-exists-p dvifile))
  13980. (progn (message "Failed to create dvi file from %s" texfile) nil)
  13981. (condition-case nil
  13982. (call-process "dvipng" nil nil nil
  13983. "-fg" fg "-bg" bg
  13984. "-D" dpi
  13985. ;;"-x" scale "-y" scale
  13986. "-T" "tight"
  13987. "-o" pngfile
  13988. dvifile)
  13989. (error nil))
  13990. (if (not (file-exists-p pngfile))
  13991. (if org-format-latex-signal-error
  13992. (error "Failed to create png file from %s" texfile)
  13993. (message "Failed to create png file from %s" texfile)
  13994. nil)
  13995. ;; Use the requested file name and clean up
  13996. (copy-file pngfile tofile 'replace)
  13997. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  13998. (delete-file (concat texfilebase e)))
  13999. pngfile))))
  14000. (defun org-splice-latex-header (tpl def-pkg pkg &optional extra)
  14001. "Fill a LaTeX header template TPL.
  14002. In the template, the following place holders will be recognized:
  14003. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14004. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14005. [PACKAGES] \\usepackage statements for PKG
  14006. [NO-PACKAGES] do not include PKG
  14007. [EXTRA] the string EXTRA
  14008. [NO-EXTRA] do not include EXTRA
  14009. For backward compatibility, if both the positive and the negative place
  14010. holder is missing, the positive one (without the \"NO-\") will be
  14011. assumed to be present at the end of the template.
  14012. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  14013. EXTRA is a string."
  14014. (let (rpl (end ""))
  14015. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  14016. (setq rpl (if (or (match-end 1) (not def-pkg))
  14017. "" (org-latex-packages-to-string def-pkg t))
  14018. tpl (replace-match rpl t t tpl))
  14019. (if def-pkg (setq end (org-latex-packages-to-string def-pkg))))
  14020. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  14021. (setq rpl (if (or (match-end 1) (not pkg))
  14022. "" (org-latex-packages-to-string pkg t))
  14023. tpl (replace-match rpl t t tpl))
  14024. (if pkg (setq end (concat end "\n" (org-latex-packages-to-string pkg)))))
  14025. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  14026. (setq rpl (if (or (match-end 1) (not extra))
  14027. "" (concat extra "\n"))
  14028. tpl (replace-match rpl t t tpl))
  14029. (if (and extra (string-match "\\S-" extra))
  14030. (setq end (concat end "\n" extra))))
  14031. (if (string-match "\\S-" end)
  14032. (concat tpl "\n" end)
  14033. tpl)))
  14034. (defun org-latex-packages-to-string (pkg &optional newline)
  14035. "Turn an alist of packages into a string with the \\usepackage macros."
  14036. (setq pkg (mapconcat (lambda(p)
  14037. (cond
  14038. ((stringp p) p)
  14039. ((equal "" (car p))
  14040. (format "\\usepackage{%s}" (cadr p)))
  14041. (t
  14042. (format "\\usepackage[%s]{%s}"
  14043. (car p) (cadr p)))))
  14044. pkg
  14045. "\n"))
  14046. (if newline (concat pkg "\n") pkg))
  14047. (defun org-dvipng-color (attr)
  14048. "Return an rgb color specification for dvipng."
  14049. (apply 'format "rgb %s %s %s"
  14050. (mapcar 'org-normalize-color
  14051. (color-values (face-attribute 'default attr nil)))))
  14052. (defun org-normalize-color (value)
  14053. "Return string to be used as color value for an RGB component."
  14054. (format "%g" (/ value 65535.0)))
  14055. ;;;; Key bindings
  14056. ;; Make `C-c C-x' a prefix key
  14057. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14058. ;; TAB key with modifiers
  14059. (org-defkey org-mode-map "\C-i" 'org-cycle)
  14060. (org-defkey org-mode-map [(tab)] 'org-cycle)
  14061. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  14062. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  14063. (org-defkey org-mode-map "\M-\t" 'org-complete)
  14064. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  14065. ;; The following line is necessary under Suse GNU/Linux
  14066. (unless (featurep 'xemacs)
  14067. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  14068. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  14069. (define-key org-mode-map [backtab] 'org-shifttab)
  14070. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  14071. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14072. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  14073. ;; Cursor keys with modifiers
  14074. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  14075. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  14076. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  14077. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  14078. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14079. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14080. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14081. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14082. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  14083. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  14084. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  14085. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  14086. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  14087. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  14088. ;;; Extra keys for tty access.
  14089. ;; We only set them when really needed because otherwise the
  14090. ;; menus don't show the simple keys
  14091. (when (or org-use-extra-keys
  14092. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  14093. (not window-system))
  14094. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  14095. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  14096. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  14097. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  14098. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  14099. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  14100. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  14101. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  14102. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  14103. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  14104. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  14105. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  14106. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  14107. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  14108. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  14109. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  14110. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  14111. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  14112. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  14113. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  14114. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  14115. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  14116. (org-defkey org-mode-map [?\e (tab)] 'org-complete)
  14117. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  14118. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  14119. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  14120. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  14121. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  14122. ;; All the other keys
  14123. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14124. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  14125. (if (boundp 'narrow-map)
  14126. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  14127. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  14128. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  14129. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  14130. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  14131. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  14132. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  14133. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  14134. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  14135. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  14136. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  14137. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  14138. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  14139. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  14140. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  14141. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  14142. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  14143. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  14144. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  14145. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  14146. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  14147. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  14148. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  14149. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  14150. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  14151. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  14152. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  14153. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  14154. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  14155. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  14156. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  14157. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  14158. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14159. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14160. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14161. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14162. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  14163. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  14164. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14165. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  14166. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14167. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  14168. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  14169. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14170. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  14171. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  14172. (org-defkey org-mode-map "\C-c^" 'org-sort)
  14173. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14174. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  14175. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  14176. (org-defkey org-mode-map "\C-m" 'org-return)
  14177. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  14178. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  14179. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  14180. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  14181. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  14182. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  14183. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  14184. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14185. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14186. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14187. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  14188. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  14189. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  14190. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  14191. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14192. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  14193. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  14194. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  14195. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  14196. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  14197. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  14198. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  14199. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14200. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14201. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14202. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  14203. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14204. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14205. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  14206. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14207. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14208. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14209. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14210. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14211. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14212. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  14213. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  14214. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  14215. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  14216. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  14217. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  14218. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  14219. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  14220. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  14221. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  14222. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  14223. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  14224. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  14225. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  14226. (when (featurep 'xemacs)
  14227. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  14228. (defconst org-speed-commands-default
  14229. '(
  14230. ("Outline Navigation")
  14231. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  14232. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  14233. ("f" . (org-speed-move-safe 'org-forward-same-level))
  14234. ("b" . (org-speed-move-safe 'org-backward-same-level))
  14235. ("u" . (org-speed-move-safe 'outline-up-heading))
  14236. ("j" . org-goto)
  14237. ("g" . (org-refile t))
  14238. ("Outline Visibility")
  14239. ("c" . org-cycle)
  14240. ("C" . org-shifttab)
  14241. (" " . org-display-outline-path)
  14242. ("Outline Structure Editing")
  14243. ("U" . org-shiftmetaup)
  14244. ("D" . org-shiftmetadown)
  14245. ("r" . org-metaright)
  14246. ("l" . org-metaleft)
  14247. ("R" . org-shiftmetaright)
  14248. ("L" . org-shiftmetaleft)
  14249. ("i" . (progn (forward-char 1) (call-interactively
  14250. 'org-insert-heading-respect-content)))
  14251. ("^" . org-sort)
  14252. ("w" . org-refile)
  14253. ("a" . org-archive-subtree-default-with-confirmation)
  14254. ("." . outline-mark-subtree)
  14255. ("Clock Commands")
  14256. ("I" . org-clock-in)
  14257. ("O" . org-clock-out)
  14258. ("Meta Data Editing")
  14259. ("t" . org-todo)
  14260. ("0" . (org-priority ?\ ))
  14261. ("1" . (org-priority ?A))
  14262. ("2" . (org-priority ?B))
  14263. ("3" . (org-priority ?C))
  14264. (";" . org-set-tags-command)
  14265. ("e" . org-set-effort)
  14266. ("Agenda Views etc")
  14267. ("v" . org-agenda)
  14268. ("/" . org-sparse-tree)
  14269. ("Misc")
  14270. ("o" . org-open-at-point)
  14271. ("?" . org-speed-command-help)
  14272. )
  14273. "The default speed commands.")
  14274. (defun org-print-speed-command (e)
  14275. (if (> (length (car e)) 1)
  14276. (progn
  14277. (princ "\n")
  14278. (princ (car e))
  14279. (princ "\n")
  14280. (princ (make-string (length (car e)) ?-))
  14281. (princ "\n"))
  14282. (princ (car e))
  14283. (princ " ")
  14284. (if (symbolp (cdr e))
  14285. (princ (symbol-name (cdr e)))
  14286. (prin1 (cdr e)))
  14287. (princ "\n")))
  14288. (defun org-speed-command-help ()
  14289. "Show the available speed commands."
  14290. (interactive)
  14291. (if (not org-use-speed-commands)
  14292. (error "Speed commands are not activated, customize `org-use-speed-commands'.")
  14293. (with-output-to-temp-buffer "*Help*"
  14294. (princ "User-defined Speed commands\n===========================\n")
  14295. (mapc 'org-print-speed-command org-speed-commands-user)
  14296. (princ "\n")
  14297. (princ "Built-in Speed commands\n=======================\n")
  14298. (mapc 'org-print-speed-command org-speed-commands-default))
  14299. (with-current-buffer "*Help*"
  14300. (setq truncate-lines t))))
  14301. (defun org-speed-move-safe (cmd)
  14302. "Execute CMD, but make sure that the cursor always ends up in a headline.
  14303. If not, return to the original position and throw an error."
  14304. (interactive)
  14305. (let ((pos (point)))
  14306. (call-interactively cmd)
  14307. (unless (and (bolp) (org-on-heading-p))
  14308. (goto-char pos)
  14309. (error "Boundary reached while executing %s" cmd))))
  14310. (defvar org-self-insert-command-undo-counter 0)
  14311. (defvar org-table-auto-blank-field) ; defined in org-table.el
  14312. (defvar org-speed-command nil)
  14313. (defun org-self-insert-command (N)
  14314. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14315. If the cursor is in a table looking at whitespace, the whitespace is
  14316. overwritten, and the table is not marked as requiring realignment."
  14317. (interactive "p")
  14318. (cond
  14319. ((and org-use-speed-commands
  14320. (or (and (bolp) (looking-at outline-regexp))
  14321. (and (functionp org-use-speed-commands)
  14322. (funcall org-use-speed-commands)))
  14323. (setq
  14324. org-speed-command
  14325. (or (cdr (assoc (this-command-keys) org-speed-commands-user))
  14326. (cdr (assoc (this-command-keys) org-speed-commands-default)))))
  14327. (cond
  14328. ((commandp org-speed-command)
  14329. (setq this-command org-speed-command)
  14330. (call-interactively org-speed-command))
  14331. ((functionp org-speed-command)
  14332. (funcall org-speed-command))
  14333. ((and org-speed-command (listp org-speed-command))
  14334. (eval org-speed-command))
  14335. (t (let (org-use-speed-commands)
  14336. (call-interactively 'org-self-insert-command)))))
  14337. ((and
  14338. (org-table-p)
  14339. (progn
  14340. ;; check if we blank the field, and if that triggers align
  14341. (and (featurep 'org-table) org-table-auto-blank-field
  14342. (member last-command
  14343. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  14344. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14345. ;; got extra space, this field does not determine column width
  14346. (let (org-table-may-need-update) (org-table-blank-field))
  14347. ;; no extra space, this field may determine column width
  14348. (org-table-blank-field)))
  14349. t)
  14350. (eq N 1)
  14351. (looking-at "[^|\n]* |"))
  14352. (let (org-table-may-need-update)
  14353. (goto-char (1- (match-end 0)))
  14354. (delete-backward-char 1)
  14355. (goto-char (match-beginning 0))
  14356. (self-insert-command N)))
  14357. (t
  14358. (setq org-table-may-need-update t)
  14359. (self-insert-command N)
  14360. (org-fix-tags-on-the-fly)
  14361. (if org-self-insert-cluster-for-undo
  14362. (if (not (eq last-command 'org-self-insert-command))
  14363. (setq org-self-insert-command-undo-counter 1)
  14364. (if (>= org-self-insert-command-undo-counter 20)
  14365. (setq org-self-insert-command-undo-counter 1)
  14366. (and (> org-self-insert-command-undo-counter 0)
  14367. buffer-undo-list
  14368. (not (cadr buffer-undo-list)) ; remove nil entry
  14369. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  14370. (setq org-self-insert-command-undo-counter
  14371. (1+ org-self-insert-command-undo-counter))))))))
  14372. (defun org-fix-tags-on-the-fly ()
  14373. (when (and (equal (char-after (point-at-bol)) ?*)
  14374. (org-on-heading-p))
  14375. (org-align-tags-here org-tags-column)))
  14376. (defun org-delete-backward-char (N)
  14377. "Like `delete-backward-char', insert whitespace at field end in tables.
  14378. When deleting backwards, in tables this function will insert whitespace in
  14379. front of the next \"|\" separator, to keep the table aligned. The table will
  14380. still be marked for re-alignment if the field did fill the entire column,
  14381. because, in this case the deletion might narrow the column."
  14382. (interactive "p")
  14383. (if (and (org-table-p)
  14384. (eq N 1)
  14385. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14386. (looking-at ".*?|"))
  14387. (let ((pos (point))
  14388. (noalign (looking-at "[^|\n\r]* |"))
  14389. (c org-table-may-need-update))
  14390. (backward-delete-char N)
  14391. (skip-chars-forward "^|")
  14392. (insert " ")
  14393. (goto-char (1- pos))
  14394. ;; noalign: if there were two spaces at the end, this field
  14395. ;; does not determine the width of the column.
  14396. (if noalign (setq org-table-may-need-update c)))
  14397. (backward-delete-char N)
  14398. (org-fix-tags-on-the-fly)))
  14399. (defun org-delete-char (N)
  14400. "Like `delete-char', but insert whitespace at field end in tables.
  14401. When deleting characters, in tables this function will insert whitespace in
  14402. front of the next \"|\" separator, to keep the table aligned. The table will
  14403. still be marked for re-alignment if the field did fill the entire column,
  14404. because, in this case the deletion might narrow the column."
  14405. (interactive "p")
  14406. (if (and (org-table-p)
  14407. (not (bolp))
  14408. (not (= (char-after) ?|))
  14409. (eq N 1))
  14410. (if (looking-at ".*?|")
  14411. (let ((pos (point))
  14412. (noalign (looking-at "[^|\n\r]* |"))
  14413. (c org-table-may-need-update))
  14414. (replace-match (concat
  14415. (substring (match-string 0) 1 -1)
  14416. " |"))
  14417. (goto-char pos)
  14418. ;; noalign: if there were two spaces at the end, this field
  14419. ;; does not determine the width of the column.
  14420. (if noalign (setq org-table-may-need-update c)))
  14421. (delete-char N))
  14422. (delete-char N)
  14423. (org-fix-tags-on-the-fly)))
  14424. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  14425. (put 'org-self-insert-command 'delete-selection t)
  14426. (put 'orgtbl-self-insert-command 'delete-selection t)
  14427. (put 'org-delete-char 'delete-selection 'supersede)
  14428. (put 'org-delete-backward-char 'delete-selection 'supersede)
  14429. (put 'org-yank 'delete-selection 'yank)
  14430. ;; Make `flyspell-mode' delay after some commands
  14431. (put 'org-self-insert-command 'flyspell-delayed t)
  14432. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  14433. (put 'org-delete-char 'flyspell-delayed t)
  14434. (put 'org-delete-backward-char 'flyspell-delayed t)
  14435. ;; Make pabbrev-mode expand after org-mode commands
  14436. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  14437. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  14438. ;; How to do this: Measure non-white length of current string
  14439. ;; If equal to column width, we should realign.
  14440. (defun org-remap (map &rest commands)
  14441. "In MAP, remap the functions given in COMMANDS.
  14442. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14443. (let (new old)
  14444. (while commands
  14445. (setq old (pop commands) new (pop commands))
  14446. (if (fboundp 'command-remapping)
  14447. (org-defkey map (vector 'remap old) new)
  14448. (substitute-key-definition old new map global-map)))))
  14449. (when (eq org-enable-table-editor 'optimized)
  14450. ;; If the user wants maximum table support, we need to hijack
  14451. ;; some standard editing functions
  14452. (org-remap org-mode-map
  14453. 'self-insert-command 'org-self-insert-command
  14454. 'delete-char 'org-delete-char
  14455. 'delete-backward-char 'org-delete-backward-char)
  14456. (org-defkey org-mode-map "|" 'org-force-self-insert))
  14457. (defvar org-ctrl-c-ctrl-c-hook nil
  14458. "Hook for functions attaching themselves to `C-c C-c'.
  14459. This can be used to add additional functionality to the C-c C-c key which
  14460. executes context-dependent commands.
  14461. Each function will be called with no arguments. The function must check
  14462. if the context is appropriate for it to act. If yes, it should do its
  14463. thing and then return a non-nil value. If the context is wrong,
  14464. just do nothing and return nil.")
  14465. (defvar org-tab-first-hook nil
  14466. "Hook for functions to attach themselves to TAB.
  14467. See `org-ctrl-c-ctrl-c-hook' for more information.
  14468. This hook runs as the first action when TAB is pressed, even before
  14469. `org-cycle' messes around with the `outline-regexp' to cater for
  14470. inline tasks and plain list item folding.
  14471. If any function in this hook returns t, not other actions like table
  14472. field motion visibility cycling will be done.")
  14473. (defvar org-tab-after-check-for-table-hook nil
  14474. "Hook for functions to attach themselves to TAB.
  14475. See `org-ctrl-c-ctrl-c-hook' for more information.
  14476. This hook runs after it has been established that the cursor is not in a
  14477. table, but before checking if the cursor is in a headline or if global cycling
  14478. should be done.
  14479. If any function in this hook returns t, not other actions like visibility
  14480. cycling will be done.")
  14481. (defvar org-tab-after-check-for-cycling-hook nil
  14482. "Hook for functions to attach themselves to TAB.
  14483. See `org-ctrl-c-ctrl-c-hook' for more information.
  14484. This hook runs after it has been established that not table field motion and
  14485. not visibility should be done because of current context. This is probably
  14486. the place where a package like yasnippets can hook in.")
  14487. (defvar org-tab-before-tab-emulation-hook nil
  14488. "Hook for functions to attach themselves to TAB.
  14489. See `org-ctrl-c-ctrl-c-hook' for more information.
  14490. This hook runs after every other options for TAB have been exhausted, but
  14491. before indentation and \t insertion takes place.")
  14492. (defvar org-metaleft-hook nil
  14493. "Hook for functions attaching themselves to `M-left'.
  14494. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14495. (defvar org-metaright-hook nil
  14496. "Hook for functions attaching themselves to `M-right'.
  14497. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14498. (defvar org-metaup-hook nil
  14499. "Hook for functions attaching themselves to `M-up'.
  14500. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14501. (defvar org-metadown-hook nil
  14502. "Hook for functions attaching themselves to `M-down'.
  14503. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14504. (defvar org-shiftmetaleft-hook nil
  14505. "Hook for functions attaching themselves to `M-S-left'.
  14506. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14507. (defvar org-shiftmetaright-hook nil
  14508. "Hook for functions attaching themselves to `M-S-right'.
  14509. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14510. (defvar org-shiftmetaup-hook nil
  14511. "Hook for functions attaching themselves to `M-S-up'.
  14512. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14513. (defvar org-shiftmetadown-hook nil
  14514. "Hook for functions attaching themselves to `M-S-down'.
  14515. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14516. (defvar org-metareturn-hook nil
  14517. "Hook for functions attaching themselves to `M-RET'.
  14518. See `org-ctrl-c-ctrl-c-hook' for more information.")
  14519. (defun org-modifier-cursor-error ()
  14520. "Throw an error, a modified cursor command was applied in wrong context."
  14521. (error "This command is active in special context like tables, headlines or items"))
  14522. (defun org-shiftselect-error ()
  14523. "Throw an error because Shift-Cursor command was applied in wrong context."
  14524. (if (and (boundp 'shift-select-mode) shift-select-mode)
  14525. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  14526. (error "This command works only in special context like headlines or timestamps")))
  14527. (defun org-call-for-shift-select (cmd)
  14528. (let ((this-command-keys-shift-translated t))
  14529. (call-interactively cmd)))
  14530. (defun org-shifttab (&optional arg)
  14531. "Global visibility cycling or move to previous table field.
  14532. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14533. on context.
  14534. See the individual commands for more information."
  14535. (interactive "P")
  14536. (cond
  14537. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14538. ((integerp arg)
  14539. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  14540. (message "Content view to level: %d" arg)
  14541. (org-content (prefix-numeric-value arg2))
  14542. (setq org-cycle-global-status 'overview)))
  14543. (t (call-interactively 'org-global-cycle))))
  14544. (defun org-shiftmetaleft ()
  14545. "Promote subtree or delete table column.
  14546. Calls `org-promote-subtree', `org-outdent-item',
  14547. or `org-table-delete-column', depending on context.
  14548. See the individual commands for more information."
  14549. (interactive)
  14550. (cond
  14551. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  14552. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14553. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14554. ((org-at-item-p) (call-interactively 'org-outdent-item))
  14555. (t (org-modifier-cursor-error))))
  14556. (defun org-shiftmetaright ()
  14557. "Demote subtree or insert table column.
  14558. Calls `org-demote-subtree', `org-indent-item',
  14559. or `org-table-insert-column', depending on context.
  14560. See the individual commands for more information."
  14561. (interactive)
  14562. (cond
  14563. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  14564. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14565. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14566. ((org-at-item-p) (call-interactively 'org-indent-item))
  14567. (t (org-modifier-cursor-error))))
  14568. (defun org-shiftmetaup (&optional arg)
  14569. "Move subtree up or kill table row.
  14570. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14571. `org-move-item-up' depending on context. See the individual commands
  14572. for more information."
  14573. (interactive "P")
  14574. (cond
  14575. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  14576. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14577. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14578. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14579. (t (org-modifier-cursor-error))))
  14580. (defun org-shiftmetadown (&optional arg)
  14581. "Move subtree down or insert table row.
  14582. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14583. `org-move-item-down', depending on context. See the individual
  14584. commands for more information."
  14585. (interactive "P")
  14586. (cond
  14587. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  14588. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14589. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14590. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14591. (t (org-modifier-cursor-error))))
  14592. (defun org-metaleft (&optional arg)
  14593. "Promote heading or move table column to left.
  14594. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14595. With no specific context, calls the Emacs default `backward-word'.
  14596. See the individual commands for more information."
  14597. (interactive "P")
  14598. (cond
  14599. ((run-hook-with-args-until-success 'org-metaleft-hook))
  14600. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14601. ((or (org-on-heading-p)
  14602. (and (org-region-active-p)
  14603. (save-excursion
  14604. (goto-char (region-beginning))
  14605. (org-on-heading-p))))
  14606. (call-interactively 'org-do-promote))
  14607. ((or (org-at-item-p)
  14608. (and (org-region-active-p)
  14609. (save-excursion
  14610. (goto-char (region-beginning))
  14611. (org-at-item-p))))
  14612. (call-interactively 'org-outdent-item))
  14613. (t (call-interactively 'backward-word))))
  14614. (defun org-metaright (&optional arg)
  14615. "Demote subtree or move table column to right.
  14616. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14617. With no specific context, calls the Emacs default `forward-word'.
  14618. See the individual commands for more information."
  14619. (interactive "P")
  14620. (cond
  14621. ((run-hook-with-args-until-success 'org-metaright-hook))
  14622. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14623. ((or (org-on-heading-p)
  14624. (and (org-region-active-p)
  14625. (save-excursion
  14626. (goto-char (region-beginning))
  14627. (org-on-heading-p))))
  14628. (call-interactively 'org-do-demote))
  14629. ((or (org-at-item-p)
  14630. (and (org-region-active-p)
  14631. (save-excursion
  14632. (goto-char (region-beginning))
  14633. (org-at-item-p))))
  14634. (call-interactively 'org-indent-item))
  14635. (t (call-interactively 'forward-word))))
  14636. (defun org-metaup (&optional arg)
  14637. "Move subtree up or move table row up.
  14638. Calls `org-move-subtree-up' or `org-table-move-row' or
  14639. `org-move-item-up', depending on context. See the individual commands
  14640. for more information."
  14641. (interactive "P")
  14642. (cond
  14643. ((run-hook-with-args-until-success 'org-metaup-hook))
  14644. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14645. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14646. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14647. (t (transpose-lines 1) (beginning-of-line -1))))
  14648. (defun org-metadown (&optional arg)
  14649. "Move subtree down or move table row down.
  14650. Calls `org-move-subtree-down' or `org-table-move-row' or
  14651. `org-move-item-down', depending on context. See the individual
  14652. commands for more information."
  14653. (interactive "P")
  14654. (cond
  14655. ((run-hook-with-args-until-success 'org-metadown-hook))
  14656. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14657. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14658. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14659. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  14660. (defun org-shiftup (&optional arg)
  14661. "Increase item in timestamp or increase priority of current headline.
  14662. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  14663. depending on context. See the individual commands for more information."
  14664. (interactive "P")
  14665. (cond
  14666. ((and org-support-shift-select (org-region-active-p))
  14667. (org-call-for-shift-select 'previous-line))
  14668. ((org-at-timestamp-p t)
  14669. (call-interactively (if org-edit-timestamp-down-means-later
  14670. 'org-timestamp-down 'org-timestamp-up)))
  14671. ((and (not (eq org-support-shift-select 'always))
  14672. org-enable-priority-commands
  14673. (org-on-heading-p))
  14674. (call-interactively 'org-priority-up))
  14675. ((and (not org-support-shift-select) (org-at-item-p))
  14676. (call-interactively 'org-previous-item))
  14677. ((org-clocktable-try-shift 'up arg))
  14678. (org-support-shift-select
  14679. (org-call-for-shift-select 'previous-line))
  14680. (t (org-shiftselect-error))))
  14681. (defun org-shiftdown (&optional arg)
  14682. "Decrease item in timestamp or decrease priority of current headline.
  14683. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  14684. depending on context. See the individual commands for more information."
  14685. (interactive "P")
  14686. (cond
  14687. ((and org-support-shift-select (org-region-active-p))
  14688. (org-call-for-shift-select 'next-line))
  14689. ((org-at-timestamp-p t)
  14690. (call-interactively (if org-edit-timestamp-down-means-later
  14691. 'org-timestamp-up 'org-timestamp-down)))
  14692. ((and (not (eq org-support-shift-select 'always))
  14693. org-enable-priority-commands
  14694. (org-on-heading-p))
  14695. (call-interactively 'org-priority-down))
  14696. ((and (not org-support-shift-select) (org-at-item-p))
  14697. (call-interactively 'org-next-item))
  14698. ((org-clocktable-try-shift 'down arg))
  14699. (org-support-shift-select
  14700. (org-call-for-shift-select 'next-line))
  14701. (t (org-shiftselect-error))))
  14702. (defun org-shiftright (&optional arg)
  14703. "Cycle the thing at point or in the current line, depending on context.
  14704. Depending on context, this does one of the following:
  14705. - switch a timestamp at point one day into the future
  14706. - on a headline, switch to the next TODO keyword.
  14707. - on an item, switch entire list to the next bullet type
  14708. - on a property line, switch to the next allowed value
  14709. - on a clocktable definition line, move time block into the future"
  14710. (interactive "P")
  14711. (cond
  14712. ((and org-support-shift-select (org-region-active-p))
  14713. (org-call-for-shift-select 'forward-char))
  14714. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14715. ((and (not (eq org-support-shift-select 'always))
  14716. (org-on-heading-p))
  14717. (let ((org-inhibit-logging
  14718. (not org-treat-S-cursor-todo-selection-as-state-change))
  14719. (org-inhibit-blocking
  14720. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14721. (org-call-with-arg 'org-todo 'right)))
  14722. ((or (and org-support-shift-select
  14723. (not (eq org-support-shift-select 'always))
  14724. (org-at-item-bullet-p))
  14725. (and (not org-support-shift-select) (org-at-item-p)))
  14726. (org-call-with-arg 'org-cycle-list-bullet nil))
  14727. ((and (not (eq org-support-shift-select 'always))
  14728. (org-at-property-p))
  14729. (call-interactively 'org-property-next-allowed-value))
  14730. ((org-clocktable-try-shift 'right arg))
  14731. (org-support-shift-select
  14732. (org-call-for-shift-select 'forward-char))
  14733. (t (org-shiftselect-error))))
  14734. (defun org-shiftleft (&optional arg)
  14735. "Cycle the thing at point or in the current line, depending on context.
  14736. Depending on context, this does one of the following:
  14737. - switch a timestamp at point one day into the past
  14738. - on a headline, switch to the previous TODO keyword.
  14739. - on an item, switch entire list to the previous bullet type
  14740. - on a property line, switch to the previous allowed value
  14741. - on a clocktable definition line, move time block into the past"
  14742. (interactive "P")
  14743. (cond
  14744. ((and org-support-shift-select (org-region-active-p))
  14745. (org-call-for-shift-select 'backward-char))
  14746. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14747. ((and (not (eq org-support-shift-select 'always))
  14748. (org-on-heading-p))
  14749. (let ((org-inhibit-logging
  14750. (not org-treat-S-cursor-todo-selection-as-state-change))
  14751. (org-inhibit-blocking
  14752. (not org-treat-S-cursor-todo-selection-as-state-change)))
  14753. (org-call-with-arg 'org-todo 'left)))
  14754. ((or (and org-support-shift-select
  14755. (not (eq org-support-shift-select 'always))
  14756. (org-at-item-bullet-p))
  14757. (and (not org-support-shift-select) (org-at-item-p)))
  14758. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  14759. ((and (not (eq org-support-shift-select 'always))
  14760. (org-at-property-p))
  14761. (call-interactively 'org-property-previous-allowed-value))
  14762. ((org-clocktable-try-shift 'left arg))
  14763. (org-support-shift-select
  14764. (org-call-for-shift-select 'backward-char))
  14765. (t (org-shiftselect-error))))
  14766. (defun org-shiftcontrolright ()
  14767. "Switch to next TODO set."
  14768. (interactive)
  14769. (cond
  14770. ((and org-support-shift-select (org-region-active-p))
  14771. (org-call-for-shift-select 'forward-word))
  14772. ((and (not (eq org-support-shift-select 'always))
  14773. (org-on-heading-p))
  14774. (org-call-with-arg 'org-todo 'nextset))
  14775. (org-support-shift-select
  14776. (org-call-for-shift-select 'forward-word))
  14777. (t (org-shiftselect-error))))
  14778. (defun org-shiftcontrolleft ()
  14779. "Switch to previous TODO set."
  14780. (interactive)
  14781. (cond
  14782. ((and org-support-shift-select (org-region-active-p))
  14783. (org-call-for-shift-select 'backward-word))
  14784. ((and (not (eq org-support-shift-select 'always))
  14785. (org-on-heading-p))
  14786. (org-call-with-arg 'org-todo 'previousset))
  14787. (org-support-shift-select
  14788. (org-call-for-shift-select 'backward-word))
  14789. (t (org-shiftselect-error))))
  14790. (defun org-ctrl-c-ret ()
  14791. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  14792. (interactive)
  14793. (cond
  14794. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  14795. (t (call-interactively 'org-insert-heading))))
  14796. (defun org-copy-special ()
  14797. "Copy region in table or copy current subtree.
  14798. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14799. See the individual commands for more information."
  14800. (interactive)
  14801. (call-interactively
  14802. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14803. (defun org-cut-special ()
  14804. "Cut region in table or cut current subtree.
  14805. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14806. See the individual commands for more information."
  14807. (interactive)
  14808. (call-interactively
  14809. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14810. (defun org-paste-special (arg)
  14811. "Paste rectangular region into table, or past subtree relative to level.
  14812. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14813. See the individual commands for more information."
  14814. (interactive "P")
  14815. (if (org-at-table-p)
  14816. (org-table-paste-rectangle)
  14817. (org-paste-subtree arg)))
  14818. (defun org-edit-special ()
  14819. "Call a special editor for the stuff at point.
  14820. When at a table, call the formula editor with `org-table-edit-formulas'.
  14821. When at the first line of an src example, call `org-edit-src-code'.
  14822. When in an #+include line, visit the include file. Otherwise call
  14823. `ffap' to visit the file at point."
  14824. (interactive)
  14825. (cond
  14826. ((org-at-table.el-p)
  14827. (org-edit-src-code))
  14828. ((org-at-table-p)
  14829. (call-interactively 'org-table-edit-formulas))
  14830. ((save-excursion
  14831. (beginning-of-line 1)
  14832. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  14833. (find-file (org-trim (match-string 1))))
  14834. ((org-edit-src-code))
  14835. ((org-edit-fixed-width-region))
  14836. (t (call-interactively 'ffap))))
  14837. (defun org-ctrl-c-ctrl-c (&optional arg)
  14838. "Set tags in headline, or update according to changed information at point.
  14839. This command does many different things, depending on context:
  14840. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  14841. this is what we do.
  14842. - If the cursor is on a statistics cookie, update it.
  14843. - If the cursor is in a headline, prompt for tags and insert them
  14844. into the current line, aligned to `org-tags-column'. When called
  14845. with prefix arg, realign all tags in the current buffer.
  14846. - If the cursor is in one of the special #+KEYWORD lines, this
  14847. triggers scanning the buffer for these lines and updating the
  14848. information.
  14849. - If the cursor is inside a table, realign the table. This command
  14850. works even if the automatic table editor has been turned off.
  14851. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14852. the entire table.
  14853. - If the cursor is at a footnote reference or definition, jump to
  14854. the corresponding definition or references, respectively.
  14855. - If the cursor is a the beginning of a dynamic block, update it.
  14856. - If the current buffer is a remember buffer, close note and file
  14857. it. A prefix argument of 1 files to the default location
  14858. without further interaction. A prefix argument of 2 files to
  14859. the currently clocking task.
  14860. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14861. links in this buffer.
  14862. - If the cursor is on a numbered item in a plain list, renumber the
  14863. ordered list.
  14864. - If the cursor is on a checkbox, toggle it."
  14865. (interactive "P")
  14866. (let ((org-enable-table-editor t))
  14867. (cond
  14868. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  14869. org-occur-highlights
  14870. org-latex-fragment-image-overlays)
  14871. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  14872. (org-remove-occur-highlights)
  14873. (org-remove-latex-fragment-image-overlays)
  14874. (message "Temporary highlights/overlays removed from current buffer"))
  14875. ((and (local-variable-p 'org-finish-function (current-buffer))
  14876. (fboundp org-finish-function))
  14877. (funcall org-finish-function))
  14878. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  14879. ((or (looking-at (org-re org-property-start-re))
  14880. (org-at-property-p))
  14881. (call-interactively 'org-property-action))
  14882. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14883. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  14884. (or (org-on-heading-p) (org-at-item-p)))
  14885. (call-interactively 'org-update-statistics-cookies))
  14886. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14887. ((org-at-table.el-p)
  14888. (message "Use C-c ' to edit table.el tables"))
  14889. ((org-at-table-p)
  14890. (org-table-maybe-eval-formula)
  14891. (if arg
  14892. (call-interactively 'org-table-recalculate)
  14893. (org-table-maybe-recalculate-line))
  14894. (call-interactively 'org-table-align))
  14895. ((or (org-footnote-at-reference-p)
  14896. (org-footnote-at-definition-p))
  14897. (call-interactively 'org-footnote-action))
  14898. ((org-at-item-checkbox-p)
  14899. (call-interactively 'org-toggle-checkbox))
  14900. ((org-at-item-p)
  14901. (if arg
  14902. (call-interactively 'org-toggle-checkbox)
  14903. (call-interactively 'org-maybe-renumber-ordered-list)))
  14904. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  14905. ;; Dynamic block
  14906. (beginning-of-line 1)
  14907. (save-excursion (org-update-dblock)))
  14908. ((save-excursion
  14909. (beginning-of-line 1)
  14910. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  14911. (cond
  14912. ((equal (match-string 1) "TBLFM")
  14913. ;; Recalculate the table before this line
  14914. (save-excursion
  14915. (beginning-of-line 1)
  14916. (skip-chars-backward " \r\n\t")
  14917. (if (org-at-table-p)
  14918. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  14919. (t
  14920. (let ((org-inhibit-startup-visibility-stuff t)
  14921. (org-startup-align-all-tables nil))
  14922. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  14923. (message "Local setup has been refreshed"))))
  14924. ((org-clock-update-time-maybe))
  14925. (t (error "C-c C-c can do nothing useful at this location")))))
  14926. (defun org-mode-restart ()
  14927. "Restart Org-mode, to scan again for special lines.
  14928. Also updates the keyword regular expressions."
  14929. (interactive)
  14930. (org-mode)
  14931. (message "Org-mode restarted"))
  14932. (defun org-kill-note-or-show-branches ()
  14933. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  14934. (interactive)
  14935. (if (not org-finish-function)
  14936. (call-interactively 'show-branches)
  14937. (let ((org-note-abort t))
  14938. (funcall org-finish-function))))
  14939. (defun org-return (&optional indent)
  14940. "Goto next table row or insert a newline.
  14941. Calls `org-table-next-row' or `newline', depending on context.
  14942. See the individual commands for more information."
  14943. (interactive)
  14944. (cond
  14945. ((bobp) (if indent (newline-and-indent) (newline)))
  14946. ((org-at-table-p)
  14947. (org-table-justify-field-maybe)
  14948. (call-interactively 'org-table-next-row))
  14949. ((and org-return-follows-link
  14950. (eq (get-text-property (point) 'face) 'org-link))
  14951. (call-interactively 'org-open-at-point))
  14952. ((and (org-at-heading-p)
  14953. (looking-at
  14954. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  14955. (org-show-entry)
  14956. (end-of-line 1)
  14957. (newline))
  14958. (t (if indent (newline-and-indent) (newline)))))
  14959. (defun org-return-indent ()
  14960. "Goto next table row or insert a newline and indent.
  14961. Calls `org-table-next-row' or `newline-and-indent', depending on
  14962. context. See the individual commands for more information."
  14963. (interactive)
  14964. (org-return t))
  14965. (defun org-ctrl-c-star ()
  14966. "Compute table, or change heading status of lines.
  14967. Calls `org-table-recalculate' or `org-toggle-heading',
  14968. depending on context."
  14969. (interactive)
  14970. (cond
  14971. ((org-at-table-p)
  14972. (call-interactively 'org-table-recalculate))
  14973. (t
  14974. ;; Convert all lines in region to list items
  14975. (call-interactively 'org-toggle-heading))))
  14976. (defun org-ctrl-c-minus ()
  14977. "Insert separator line in table or modify bullet status of line.
  14978. Also turns a plain line or a region of lines into list items.
  14979. Calls `org-table-insert-hline', `org-toggle-item', or
  14980. `org-cycle-list-bullet', depending on context."
  14981. (interactive)
  14982. (cond
  14983. ((org-at-table-p)
  14984. (call-interactively 'org-table-insert-hline))
  14985. ((org-region-active-p)
  14986. (call-interactively 'org-toggle-item))
  14987. ((org-in-item-p)
  14988. (call-interactively 'org-cycle-list-bullet))
  14989. (t
  14990. (call-interactively 'org-toggle-item))))
  14991. (defun org-toggle-item ()
  14992. "Convert headings or normal lines to items, items to normal lines.
  14993. If there is no active region, only the current line is considered.
  14994. If the first line in the region is a headline, convert all headlines to items.
  14995. If the first line in the region is an item, convert all items to normal lines.
  14996. If the first line is normal text, add an item bullet to each line."
  14997. (interactive)
  14998. (let (l2 l beg end)
  14999. (if (org-region-active-p)
  15000. (setq beg (region-beginning) end (region-end))
  15001. (setq beg (point-at-bol)
  15002. end (min (1+ (point-at-eol)) (point-max))))
  15003. (save-excursion
  15004. (goto-char end)
  15005. (setq l2 (org-current-line))
  15006. (goto-char beg)
  15007. (beginning-of-line 1)
  15008. (setq l (1- (org-current-line)))
  15009. (if (org-at-item-p)
  15010. ;; We already have items, de-itemize
  15011. (while (< (setq l (1+ l)) l2)
  15012. (when (org-at-item-p)
  15013. (goto-char (match-beginning 2))
  15014. (delete-region (match-beginning 2) (match-end 2))
  15015. (and (looking-at "[ \t]+") (replace-match "")))
  15016. (beginning-of-line 2))
  15017. (if (org-on-heading-p)
  15018. ;; Headings, convert to items
  15019. (while (< (setq l (1+ l)) l2)
  15020. (if (looking-at org-outline-regexp)
  15021. (replace-match "- " t t))
  15022. (beginning-of-line 2))
  15023. ;; normal lines, turn them into items
  15024. (while (< (setq l (1+ l)) l2)
  15025. (unless (org-at-item-p)
  15026. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15027. (replace-match "\\1- \\2")))
  15028. (beginning-of-line 2)))))))
  15029. (defun org-toggle-heading (&optional nstars)
  15030. "Convert headings to normal text, or items or text to headings.
  15031. If there is no active region, only the current line is considered.
  15032. If the first line is a heading, remove the stars from all headlines
  15033. in the region.
  15034. If the first line is a plain list item, turn all plain list items
  15035. into headings.
  15036. If the first line is a normal line, turn each and every line in the
  15037. region into a heading.
  15038. When converting a line into a heading, the number of stars is chosen
  15039. such that the lines become children of the current entry. However,
  15040. when a prefix argument is given, its value determines the number of
  15041. stars to add."
  15042. (interactive "P")
  15043. (let (l2 l itemp beg end)
  15044. (if (org-region-active-p)
  15045. (setq beg (region-beginning) end (region-end))
  15046. (setq beg (point-at-bol)
  15047. end (min (1+ (point-at-eol)) (point-max))))
  15048. (save-excursion
  15049. (goto-char end)
  15050. (setq l2 (org-current-line))
  15051. (goto-char beg)
  15052. (beginning-of-line 1)
  15053. (setq l (1- (org-current-line)))
  15054. (if (org-on-heading-p)
  15055. ;; We already have headlines, de-star them
  15056. (while (< (setq l (1+ l)) l2)
  15057. (when (org-on-heading-p t)
  15058. (and (looking-at outline-regexp) (replace-match "")))
  15059. (beginning-of-line 2))
  15060. (setq itemp (org-at-item-p))
  15061. (let* ((stars
  15062. (if nstars
  15063. (make-string (prefix-numeric-value current-prefix-arg)
  15064. ?*)
  15065. (save-excursion
  15066. (if (re-search-backward org-complex-heading-regexp nil t)
  15067. (match-string 1) ""))))
  15068. (add-stars (cond (nstars "")
  15069. ((equal stars "") "*")
  15070. (org-odd-levels-only "**")
  15071. (t "*")))
  15072. (rpl (concat stars add-stars " ")))
  15073. (while (< (setq l (1+ l)) l2)
  15074. (if itemp
  15075. (and (org-at-item-p) (replace-match rpl t t))
  15076. (unless (org-on-heading-p)
  15077. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  15078. (replace-match (concat rpl (match-string 2))))))
  15079. (beginning-of-line 2)))))))
  15080. (defun org-meta-return (&optional arg)
  15081. "Insert a new heading or wrap a region in a table.
  15082. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  15083. See the individual commands for more information."
  15084. (interactive "P")
  15085. (cond
  15086. ((run-hook-with-args-until-success 'org-metareturn-hook))
  15087. ((org-at-table-p)
  15088. (call-interactively 'org-table-wrap-region))
  15089. (t (call-interactively 'org-insert-heading))))
  15090. ;;; Menu entries
  15091. ;; Define the Org-mode menus
  15092. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  15093. '("Tbl"
  15094. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  15095. ["Next Field" org-cycle (org-at-table-p)]
  15096. ["Previous Field" org-shifttab (org-at-table-p)]
  15097. ["Next Row" org-return (org-at-table-p)]
  15098. "--"
  15099. ["Blank Field" org-table-blank-field (org-at-table-p)]
  15100. ["Edit Field" org-table-edit-field (org-at-table-p)]
  15101. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  15102. "--"
  15103. ("Column"
  15104. ["Move Column Left" org-metaleft (org-at-table-p)]
  15105. ["Move Column Right" org-metaright (org-at-table-p)]
  15106. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  15107. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  15108. ("Row"
  15109. ["Move Row Up" org-metaup (org-at-table-p)]
  15110. ["Move Row Down" org-metadown (org-at-table-p)]
  15111. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  15112. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  15113. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  15114. "--"
  15115. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  15116. ("Rectangle"
  15117. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  15118. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  15119. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  15120. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  15121. "--"
  15122. ("Calculate"
  15123. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  15124. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  15125. ["Edit Formulas" org-edit-special (org-at-table-p)]
  15126. "--"
  15127. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  15128. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  15129. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  15130. "--"
  15131. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  15132. "--"
  15133. ["Sum Column/Rectangle" org-table-sum
  15134. (or (org-at-table-p) (org-region-active-p))]
  15135. ["Which Column?" org-table-current-column (org-at-table-p)])
  15136. ["Debug Formulas"
  15137. org-table-toggle-formula-debugger
  15138. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  15139. ["Show Col/Row Numbers"
  15140. org-table-toggle-coordinate-overlays
  15141. :style toggle
  15142. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  15143. "--"
  15144. ["Create" org-table-create (and (not (org-at-table-p))
  15145. org-enable-table-editor)]
  15146. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  15147. ["Import from File" org-table-import (not (org-at-table-p))]
  15148. ["Export to File" org-table-export (org-at-table-p)]
  15149. "--"
  15150. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  15151. (easy-menu-define org-org-menu org-mode-map "Org menu"
  15152. '("Org"
  15153. ("Show/Hide"
  15154. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  15155. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  15156. ["Sparse Tree..." org-sparse-tree t]
  15157. ["Reveal Context" org-reveal t]
  15158. ["Show All" show-all t]
  15159. "--"
  15160. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  15161. "--"
  15162. ["New Heading" org-insert-heading t]
  15163. ("Navigate Headings"
  15164. ["Up" outline-up-heading t]
  15165. ["Next" outline-next-visible-heading t]
  15166. ["Previous" outline-previous-visible-heading t]
  15167. ["Next Same Level" outline-forward-same-level t]
  15168. ["Previous Same Level" outline-backward-same-level t]
  15169. "--"
  15170. ["Jump" org-goto t])
  15171. ("Edit Structure"
  15172. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  15173. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  15174. "--"
  15175. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  15176. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  15177. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  15178. "--"
  15179. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  15180. "--"
  15181. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  15182. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  15183. ["Demote Heading" org-metaright (not (org-at-table-p))]
  15184. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  15185. "--"
  15186. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  15187. "--"
  15188. ["Convert to odd levels" org-convert-to-odd-levels t]
  15189. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  15190. ("Editing"
  15191. ["Emphasis..." org-emphasize t]
  15192. ["Edit Source Example" org-edit-special t]
  15193. "--"
  15194. ["Footnote new/jump" org-footnote-action t]
  15195. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  15196. ("Archive"
  15197. ["Archive (default method)" org-archive-subtree-default t]
  15198. "--"
  15199. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  15200. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  15201. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  15202. )
  15203. "--"
  15204. ("Hyperlinks"
  15205. ["Store Link (Global)" org-store-link t]
  15206. ["Find existing link to here" org-occur-link-in-agenda-files t]
  15207. ["Insert Link" org-insert-link t]
  15208. ["Follow Link" org-open-at-point t]
  15209. "--"
  15210. ["Next link" org-next-link t]
  15211. ["Previous link" org-previous-link t]
  15212. "--"
  15213. ["Descriptive Links"
  15214. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  15215. :style radio
  15216. :selected (member '(org-link) buffer-invisibility-spec)]
  15217. ["Literal Links"
  15218. (progn
  15219. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  15220. :style radio
  15221. :selected (not (member '(org-link) buffer-invisibility-spec))])
  15222. "--"
  15223. ("TODO Lists"
  15224. ["TODO/DONE/-" org-todo t]
  15225. ("Select keyword"
  15226. ["Next keyword" org-shiftright (org-on-heading-p)]
  15227. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  15228. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  15229. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  15230. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  15231. ["Show TODO Tree" org-show-todo-tree t]
  15232. ["Global TODO list" org-todo-list t]
  15233. "--"
  15234. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  15235. :selected org-enforce-todo-dependencies :style toggle :active t]
  15236. "Settings for tree at point"
  15237. ["Do Children sequentially" org-toggle-ordered-property :style radio
  15238. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  15239. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15240. ["Do Children parallel" org-toggle-ordered-property :style radio
  15241. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  15242. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  15243. "--"
  15244. ["Set Priority" org-priority t]
  15245. ["Priority Up" org-shiftup t]
  15246. ["Priority Down" org-shiftdown t]
  15247. "--"
  15248. ["Get news from all feeds" org-feed-update-all t]
  15249. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  15250. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  15251. ("TAGS and Properties"
  15252. ["Set Tags" org-set-tags-command t]
  15253. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  15254. "--"
  15255. ["Set property" org-set-property t]
  15256. ["Column view of properties" org-columns t]
  15257. ["Insert Column View DBlock" org-insert-columns-dblock t])
  15258. ("Dates and Scheduling"
  15259. ["Timestamp" org-time-stamp t]
  15260. ["Timestamp (inactive)" org-time-stamp-inactive t]
  15261. ("Change Date"
  15262. ["1 Day Later" org-shiftright t]
  15263. ["1 Day Earlier" org-shiftleft t]
  15264. ["1 ... Later" org-shiftup t]
  15265. ["1 ... Earlier" org-shiftdown t])
  15266. ["Compute Time Range" org-evaluate-time-range t]
  15267. ["Schedule Item" org-schedule t]
  15268. ["Deadline" org-deadline t]
  15269. "--"
  15270. ["Custom time format" org-toggle-time-stamp-overlays
  15271. :style radio :selected org-display-custom-times]
  15272. "--"
  15273. ["Goto Calendar" org-goto-calendar t]
  15274. ["Date from Calendar" org-date-from-calendar t]
  15275. "--"
  15276. ["Start/Restart Timer" org-timer-start t]
  15277. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  15278. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  15279. ["Insert Timer String" org-timer t]
  15280. ["Insert Timer Item" org-timer-item t])
  15281. ("Logging work"
  15282. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  15283. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  15284. ["Clock out" org-clock-out t]
  15285. ["Clock cancel" org-clock-cancel t]
  15286. "--"
  15287. ["Mark as default task" org-clock-mark-default-task t]
  15288. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  15289. ["Goto running clock" org-clock-goto t]
  15290. "--"
  15291. ["Display times" org-clock-display t]
  15292. ["Create clock table" org-clock-report t]
  15293. "--"
  15294. ["Record DONE time"
  15295. (progn (setq org-log-done (not org-log-done))
  15296. (message "Switching to %s will %s record a timestamp"
  15297. (car org-done-keywords)
  15298. (if org-log-done "automatically" "not")))
  15299. :style toggle :selected org-log-done])
  15300. "--"
  15301. ["Agenda Command..." org-agenda t]
  15302. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  15303. ("File List for Agenda")
  15304. ("Special views current file"
  15305. ["TODO Tree" org-show-todo-tree t]
  15306. ["Check Deadlines" org-check-deadlines t]
  15307. ["Timeline" org-timeline t]
  15308. ["Tags/Property tree" org-match-sparse-tree t])
  15309. "--"
  15310. ["Export/Publish..." org-export t]
  15311. ("LaTeX"
  15312. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15313. :selected org-cdlatex-mode]
  15314. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15315. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15316. ["Modify math symbol" org-cdlatex-math-modify
  15317. (org-inside-LaTeX-fragment-p)]
  15318. ["Insert citation" org-reftex-citation t]
  15319. "--"
  15320. ["Export LaTeX fragments as images"
  15321. (if (featurep 'org-exp)
  15322. (setq org-export-with-LaTeX-fragments
  15323. (not org-export-with-LaTeX-fragments))
  15324. (require 'org-exp))
  15325. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  15326. org-export-with-LaTeX-fragments)]
  15327. "--"
  15328. ["Template for BEAMER" org-beamer-settings-template t])
  15329. "--"
  15330. ("MobileOrg"
  15331. ["Push Files and Views" org-mobile-push t]
  15332. ["Get Captured and Flagged" org-mobile-pull t]
  15333. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  15334. "--"
  15335. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  15336. "--"
  15337. ("Documentation"
  15338. ["Show Version" org-version t]
  15339. ["Info Documentation" org-info t])
  15340. ("Customize"
  15341. ["Browse Org Group" org-customize t]
  15342. "--"
  15343. ["Expand This Menu" org-create-customize-menu
  15344. (fboundp 'customize-menu-create)])
  15345. ["Send bug report" org-submit-bug-report t]
  15346. "--"
  15347. ("Refresh/Reload"
  15348. ["Refresh setup current buffer" org-mode-restart t]
  15349. ["Reload Org (after update)" org-reload t]
  15350. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  15351. ))
  15352. (defun org-info (&optional node)
  15353. "Read documentation for Org-mode in the info system.
  15354. With optional NODE, go directly to that node."
  15355. (interactive)
  15356. (info (format "(org)%s" (or node ""))))
  15357. ;;;###autoload
  15358. (defun org-submit-bug-report ()
  15359. "Submit a bug report on Org-mode via mail.
  15360. Don't hesitate to report any problems or inaccurate documentation.
  15361. If you don't have setup sending mail from (X)Emacs, please copy the
  15362. output buffer into your mail program, as it gives us important
  15363. information about your Org-mode version and configuration."
  15364. (interactive)
  15365. (require 'reporter)
  15366. (org-load-modules-maybe)
  15367. (org-require-autoloaded-modules)
  15368. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  15369. (reporter-submit-bug-report
  15370. "emacs-orgmode@gnu.org"
  15371. (org-version)
  15372. (let (list)
  15373. (save-window-excursion
  15374. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  15375. (delete-other-windows)
  15376. (erase-buffer)
  15377. (insert "You are about to submit a bug report to the Org-mode mailing list.
  15378. We would like to add your full Org-mode and Outline configuration to the
  15379. bug report. This greatly simplifies the work of the maintainer and
  15380. other experts on the mailing list.
  15381. HOWEVER, some variables you have customized may contain private
  15382. information. The names of customers, colleagues, or friends, might
  15383. appear in the form of file names, tags, todo states, or search strings.
  15384. If you answer yes to the prompt, you might want to check and remove
  15385. such private information before sending the email.")
  15386. (add-text-properties (point-min) (point-max) '(face org-warning))
  15387. (when (yes-or-no-p "Include your Org-mode configuration ")
  15388. (mapatoms
  15389. (lambda (v)
  15390. (and (boundp v)
  15391. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  15392. (or (and (symbol-value v)
  15393. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  15394. (and
  15395. (get v 'custom-type) (get v 'standard-value)
  15396. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  15397. (push v list)))))
  15398. (kill-buffer (get-buffer "*Warn about privacy*"))
  15399. list))
  15400. nil nil
  15401. "Remember to cover the basics, that is, what you expected to happen and
  15402. what in fact did happen. You don't know how to make a good report? See
  15403. http://orgmode.org/manual/Feedback.html#Feedback
  15404. Your bug report will be posted to the Org-mode mailing list.
  15405. ------------------------------------------------------------------------")
  15406. (save-excursion
  15407. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  15408. (replace-match "\\1Bug: \\3 [\\2]")))))
  15409. (defun org-install-agenda-files-menu ()
  15410. (let ((bl (buffer-list)))
  15411. (save-excursion
  15412. (while bl
  15413. (set-buffer (pop bl))
  15414. (if (org-mode-p) (setq bl nil)))
  15415. (when (org-mode-p)
  15416. (easy-menu-change
  15417. '("Org") "File List for Agenda"
  15418. (append
  15419. (list
  15420. ["Edit File List" (org-edit-agenda-file-list) t]
  15421. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15422. ["Remove Current File from List" org-remove-file t]
  15423. ["Cycle through agenda files" org-cycle-agenda-files t]
  15424. ["Occur in all agenda files" org-occur-in-agenda-files t]
  15425. "--")
  15426. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15427. ;;;; Documentation
  15428. ;;;###autoload
  15429. (defun org-require-autoloaded-modules ()
  15430. (interactive)
  15431. (mapc 'require
  15432. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  15433. org-docbook org-exp org-html org-icalendar
  15434. org-id org-latex
  15435. org-publish org-remember org-table
  15436. org-timer org-xoxo)))
  15437. ;;;###autoload
  15438. (defun org-reload (&optional uncompiled)
  15439. "Reload all org lisp files.
  15440. With prefix arg UNCOMPILED, load the uncompiled versions."
  15441. (interactive "P")
  15442. (require 'find-func)
  15443. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  15444. (dir-org (file-name-directory (org-find-library-name "org")))
  15445. (dir-org-contrib (ignore-errors
  15446. (file-name-directory
  15447. (org-find-library-name "org-contribdir"))))
  15448. (files
  15449. (append (directory-files dir-org t file-re)
  15450. (and dir-org-contrib
  15451. (directory-files dir-org-contrib t file-re))))
  15452. (remove-re (concat (if (featurep 'xemacs)
  15453. "org-colview" "org-colview-xemacs")
  15454. "\\'")))
  15455. (setq files (mapcar 'file-name-sans-extension files))
  15456. (setq files (mapcar
  15457. (lambda (x) (if (string-match remove-re x) nil x))
  15458. files))
  15459. (setq files (delq nil files))
  15460. (mapc
  15461. (lambda (f)
  15462. (when (featurep (intern (file-name-nondirectory f)))
  15463. (if (and (not uncompiled)
  15464. (file-exists-p (concat f ".elc")))
  15465. (load (concat f ".elc") nil nil t)
  15466. (load (concat f ".el") nil nil t))))
  15467. files))
  15468. (org-version))
  15469. ;;;###autoload
  15470. (defun org-customize ()
  15471. "Call the customize function with org as argument."
  15472. (interactive)
  15473. (org-load-modules-maybe)
  15474. (org-require-autoloaded-modules)
  15475. (customize-browse 'org))
  15476. (defun org-create-customize-menu ()
  15477. "Create a full customization menu for Org-mode, insert it into the menu."
  15478. (interactive)
  15479. (org-load-modules-maybe)
  15480. (org-require-autoloaded-modules)
  15481. (if (fboundp 'customize-menu-create)
  15482. (progn
  15483. (easy-menu-change
  15484. '("Org") "Customize"
  15485. `(["Browse Org group" org-customize t]
  15486. "--"
  15487. ,(customize-menu-create 'org)
  15488. ["Set" Custom-set t]
  15489. ["Save" Custom-save t]
  15490. ["Reset to Current" Custom-reset-current t]
  15491. ["Reset to Saved" Custom-reset-saved t]
  15492. ["Reset to Standard Settings" Custom-reset-standard t]))
  15493. (message "\"Org\"-menu now contains full customization menu"))
  15494. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15495. ;;;; Miscellaneous stuff
  15496. ;;; Generally useful functions
  15497. (defun org-get-at-bol (property)
  15498. "Get text property PROPERTY at beginning of line."
  15499. (get-text-property (point-at-bol) property))
  15500. (defun org-find-text-property-in-string (prop s)
  15501. "Return the first non-nil value of property PROP in string S."
  15502. (or (get-text-property 0 prop s)
  15503. (get-text-property (or (next-single-property-change 0 prop s) 0)
  15504. prop s)))
  15505. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  15506. "Display the given MESSAGE as a warning."
  15507. (if (fboundp 'display-warning)
  15508. (display-warning 'org message
  15509. (if (featurep 'xemacs)
  15510. 'warning
  15511. :warning))
  15512. (let ((buf (get-buffer-create "*Org warnings*")))
  15513. (with-current-buffer buf
  15514. (goto-char (point-max))
  15515. (insert "Warning (Org): " message)
  15516. (unless (bolp)
  15517. (newline)))
  15518. (display-buffer buf)
  15519. (sit-for 0))))
  15520. (defun org-in-commented-line ()
  15521. "Is point in a line starting with `#'?"
  15522. (equal (char-after (point-at-bol)) ?#))
  15523. (defun org-in-verbatim-emphasis ()
  15524. (save-match-data
  15525. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  15526. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  15527. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  15528. (if (and marker (marker-buffer marker)
  15529. (buffer-live-p (marker-buffer marker)))
  15530. (progn
  15531. (switch-to-buffer (marker-buffer marker))
  15532. (if (or (> marker (point-max)) (< marker (point-min)))
  15533. (widen))
  15534. (goto-char marker)
  15535. (org-show-context 'org-goto))
  15536. (if bookmark
  15537. (bookmark-jump bookmark)
  15538. (error "Cannot find location"))))
  15539. (defun org-quote-csv-field (s)
  15540. "Quote field for inclusion in CSV material."
  15541. (if (string-match "[\",]" s)
  15542. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  15543. s))
  15544. (defun org-plist-delete (plist property)
  15545. "Delete PROPERTY from PLIST.
  15546. This is in contrast to merely setting it to 0."
  15547. (let (p)
  15548. (while plist
  15549. (if (not (eq property (car plist)))
  15550. (setq p (plist-put p (car plist) (nth 1 plist))))
  15551. (setq plist (cddr plist)))
  15552. p))
  15553. (defun org-force-self-insert (N)
  15554. "Needed to enforce self-insert under remapping."
  15555. (interactive "p")
  15556. (self-insert-command N))
  15557. (defun org-string-width (s)
  15558. "Compute width of string, ignoring invisible characters.
  15559. This ignores character with invisibility property `org-link', and also
  15560. characters with property `org-cwidth', because these will become invisible
  15561. upon the next fontification round."
  15562. (let (b l)
  15563. (when (or (eq t buffer-invisibility-spec)
  15564. (assq 'org-link buffer-invisibility-spec))
  15565. (while (setq b (text-property-any 0 (length s)
  15566. 'invisible 'org-link s))
  15567. (setq s (concat (substring s 0 b)
  15568. (substring s (or (next-single-property-change
  15569. b 'invisible s) (length s)))))))
  15570. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  15571. (setq s (concat (substring s 0 b)
  15572. (substring s (or (next-single-property-change
  15573. b 'org-cwidth s) (length s))))))
  15574. (setq l (string-width s) b -1)
  15575. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  15576. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  15577. l))
  15578. (defun org-get-indentation (&optional line)
  15579. "Get the indentation of the current line, interpreting tabs.
  15580. When LINE is given, assume it represents a line and compute its indentation."
  15581. (if line
  15582. (if (string-match "^ *" (org-remove-tabs line))
  15583. (match-end 0))
  15584. (save-excursion
  15585. (beginning-of-line 1)
  15586. (skip-chars-forward " \t")
  15587. (current-column))))
  15588. (defun org-remove-tabs (s &optional width)
  15589. "Replace tabulators in S with spaces.
  15590. Assumes that s is a single line, starting in column 0."
  15591. (setq width (or width tab-width))
  15592. (while (string-match "\t" s)
  15593. (setq s (replace-match
  15594. (make-string
  15595. (- (* width (/ (+ (match-beginning 0) width) width))
  15596. (match-beginning 0)) ?\ )
  15597. t t s)))
  15598. s)
  15599. (defun org-fix-indentation (line ind)
  15600. "Fix indentation in LINE.
  15601. IND is a cons cell with target and minimum indentation.
  15602. If the current indentation in LINE is smaller than the minimum,
  15603. leave it alone. If it is larger than ind, set it to the target."
  15604. (let* ((l (org-remove-tabs line))
  15605. (i (org-get-indentation l))
  15606. (i1 (car ind)) (i2 (cdr ind)))
  15607. (if (>= i i2) (setq l (substring line i2)))
  15608. (if (> i1 0)
  15609. (concat (make-string i1 ?\ ) l)
  15610. l)))
  15611. (defun org-remove-indentation (code &optional n)
  15612. "Remove the maximum common indentation from the lines in CODE.
  15613. N may optionally be the number of spaces to remove."
  15614. (with-temp-buffer
  15615. (insert code)
  15616. (org-do-remove-indentation n)
  15617. (buffer-string)))
  15618. (defun org-do-remove-indentation (&optional n)
  15619. "Remove the maximum common indentation from the buffer."
  15620. (untabify (point-min) (point-max))
  15621. (let ((min 10000) re)
  15622. (if n
  15623. (setq min n)
  15624. (goto-char (point-min))
  15625. (while (re-search-forward "^ *[^ \n]" nil t)
  15626. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  15627. (unless (or (= min 0) (= min 10000))
  15628. (setq re (format "^ \\{%d\\}" min))
  15629. (goto-char (point-min))
  15630. (while (re-search-forward re nil t)
  15631. (replace-match "")
  15632. (end-of-line 1))
  15633. min)))
  15634. (defun org-fill-template (template alist)
  15635. "Find each %key of ALIST in TEMPLATE and replace it."
  15636. (let ((case-fold-search nil)
  15637. entry key value)
  15638. (setq alist (sort (copy-sequence alist)
  15639. (lambda (a b) (< (length (car a)) (length (car b))))))
  15640. (while (setq entry (pop alist))
  15641. (setq template
  15642. (replace-regexp-in-string
  15643. (concat "%" (regexp-quote (car entry)))
  15644. (cdr entry) template t t)))
  15645. template))
  15646. (defun org-base-buffer (buffer)
  15647. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  15648. (if (not buffer)
  15649. buffer
  15650. (or (buffer-base-buffer buffer)
  15651. buffer)))
  15652. (defun org-trim (s)
  15653. "Remove whitespace at beginning and end of string."
  15654. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  15655. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  15656. s)
  15657. (defun org-wrap (string &optional width lines)
  15658. "Wrap string to either a number of lines, or a width in characters.
  15659. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  15660. that costs. If there is a word longer than WIDTH, the text is actually
  15661. wrapped to the length of that word.
  15662. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  15663. many lines, whatever width that takes.
  15664. The return value is a list of lines, without newlines at the end."
  15665. (let* ((words (org-split-string string "[ \t\n]+"))
  15666. (maxword (apply 'max (mapcar 'org-string-width words)))
  15667. w ll)
  15668. (cond (width
  15669. (org-do-wrap words (max maxword width)))
  15670. (lines
  15671. (setq w maxword)
  15672. (setq ll (org-do-wrap words maxword))
  15673. (if (<= (length ll) lines)
  15674. ll
  15675. (setq ll words)
  15676. (while (> (length ll) lines)
  15677. (setq w (1+ w))
  15678. (setq ll (org-do-wrap words w)))
  15679. ll))
  15680. (t (error "Cannot wrap this")))))
  15681. (defun org-do-wrap (words width)
  15682. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  15683. (let (lines line)
  15684. (while words
  15685. (setq line (pop words))
  15686. (while (and words (< (+ (length line) (length (car words))) width))
  15687. (setq line (concat line " " (pop words))))
  15688. (setq lines (push line lines)))
  15689. (nreverse lines)))
  15690. (defun org-split-string (string &optional separators)
  15691. "Splits STRING into substrings at SEPARATORS.
  15692. No empty strings are returned if there are matches at the beginning
  15693. and end of string."
  15694. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  15695. (start 0)
  15696. notfirst
  15697. (list nil))
  15698. (while (and (string-match rexp string
  15699. (if (and notfirst
  15700. (= start (match-beginning 0))
  15701. (< start (length string)))
  15702. (1+ start) start))
  15703. (< (match-beginning 0) (length string)))
  15704. (setq notfirst t)
  15705. (or (eq (match-beginning 0) 0)
  15706. (and (eq (match-beginning 0) (match-end 0))
  15707. (eq (match-beginning 0) start))
  15708. (setq list
  15709. (cons (substring string start (match-beginning 0))
  15710. list)))
  15711. (setq start (match-end 0)))
  15712. (or (eq start (length string))
  15713. (setq list
  15714. (cons (substring string start)
  15715. list)))
  15716. (nreverse list)))
  15717. (defun org-quote-vert (s)
  15718. "Replace \"|\" with \"\\vert\"."
  15719. (while (string-match "|" s)
  15720. (setq s (replace-match "\\vert" t t s)))
  15721. s)
  15722. (defun org-uuidgen-p (s)
  15723. "Is S an ID created by UUIDGEN?"
  15724. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  15725. (defun org-context ()
  15726. "Return a list of contexts of the current cursor position.
  15727. If several contexts apply, all are returned.
  15728. Each context entry is a list with a symbol naming the context, and
  15729. two positions indicating start and end of the context. Possible
  15730. contexts are:
  15731. :headline anywhere in a headline
  15732. :headline-stars on the leading stars in a headline
  15733. :todo-keyword on a TODO keyword (including DONE) in a headline
  15734. :tags on the TAGS in a headline
  15735. :priority on the priority cookie in a headline
  15736. :item on the first line of a plain list item
  15737. :item-bullet on the bullet/number of a plain list item
  15738. :checkbox on the checkbox in a plain list item
  15739. :table in an org-mode table
  15740. :table-special on a special filed in a table
  15741. :table-table in a table.el table
  15742. :link on a hyperlink
  15743. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15744. :target on a <<target>>
  15745. :radio-target on a <<<radio-target>>>
  15746. :latex-fragment on a LaTeX fragment
  15747. :latex-preview on a LaTeX fragment with overlayed preview image
  15748. This function expects the position to be visible because it uses font-lock
  15749. faces as a help to recognize the following contexts: :table-special, :link,
  15750. and :keyword."
  15751. (let* ((f (get-text-property (point) 'face))
  15752. (faces (if (listp f) f (list f)))
  15753. (p (point)) clist o)
  15754. ;; First the large context
  15755. (cond
  15756. ((org-on-heading-p t)
  15757. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15758. (when (progn
  15759. (beginning-of-line 1)
  15760. (looking-at org-todo-line-tags-regexp))
  15761. (push (org-point-in-group p 1 :headline-stars) clist)
  15762. (push (org-point-in-group p 2 :todo-keyword) clist)
  15763. (push (org-point-in-group p 4 :tags) clist))
  15764. (goto-char p)
  15765. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  15766. (if (looking-at "\\[#[A-Z0-9]\\]")
  15767. (push (org-point-in-group p 0 :priority) clist)))
  15768. ((org-at-item-p)
  15769. (push (org-point-in-group p 2 :item-bullet) clist)
  15770. (push (list :item (point-at-bol)
  15771. (save-excursion (org-end-of-item) (point)))
  15772. clist)
  15773. (and (org-at-item-checkbox-p)
  15774. (push (org-point-in-group p 0 :checkbox) clist)))
  15775. ((org-at-table-p)
  15776. (push (list :table (org-table-begin) (org-table-end)) clist)
  15777. (if (memq 'org-formula faces)
  15778. (push (list :table-special
  15779. (previous-single-property-change p 'face)
  15780. (next-single-property-change p 'face)) clist)))
  15781. ((org-at-table-p 'any)
  15782. (push (list :table-table) clist)))
  15783. (goto-char p)
  15784. ;; Now the small context
  15785. (cond
  15786. ((org-at-timestamp-p)
  15787. (push (org-point-in-group p 0 :timestamp) clist))
  15788. ((memq 'org-link faces)
  15789. (push (list :link
  15790. (previous-single-property-change p 'face)
  15791. (next-single-property-change p 'face)) clist))
  15792. ((memq 'org-special-keyword faces)
  15793. (push (list :keyword
  15794. (previous-single-property-change p 'face)
  15795. (next-single-property-change p 'face)) clist))
  15796. ((org-on-target-p)
  15797. (push (org-point-in-group p 0 :target) clist)
  15798. (goto-char (1- (match-beginning 0)))
  15799. (if (looking-at org-radio-target-regexp)
  15800. (push (org-point-in-group p 0 :radio-target) clist))
  15801. (goto-char p))
  15802. ((setq o (car (delq nil
  15803. (mapcar
  15804. (lambda (x)
  15805. (if (memq x org-latex-fragment-image-overlays) x))
  15806. (org-overlays-at (point))))))
  15807. (push (list :latex-fragment
  15808. (org-overlay-start o) (org-overlay-end o)) clist)
  15809. (push (list :latex-preview
  15810. (org-overlay-start o) (org-overlay-end o)) clist))
  15811. ((org-inside-LaTeX-fragment-p)
  15812. ;; FIXME: positions wrong.
  15813. (push (list :latex-fragment (point) (point)) clist)))
  15814. (setq clist (nreverse (delq nil clist)))
  15815. clist))
  15816. ;; FIXME: Compare with at-regexp-p Do we need both?
  15817. (defun org-in-regexp (re &optional nlines visually)
  15818. "Check if point is inside a match of regexp.
  15819. Normally only the current line is checked, but you can include NLINES extra
  15820. lines both before and after point into the search.
  15821. If VISUALLY is set, require that the cursor is not after the match but
  15822. really on, so that the block visually is on the match."
  15823. (catch 'exit
  15824. (let ((pos (point))
  15825. (eol (point-at-eol (+ 1 (or nlines 0))))
  15826. (inc (if visually 1 0)))
  15827. (save-excursion
  15828. (beginning-of-line (- 1 (or nlines 0)))
  15829. (while (re-search-forward re eol t)
  15830. (if (and (<= (match-beginning 0) pos)
  15831. (>= (+ inc (match-end 0)) pos))
  15832. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  15833. (defun org-at-regexp-p (regexp)
  15834. "Is point inside a match of REGEXP in the current line?"
  15835. (catch 'exit
  15836. (save-excursion
  15837. (let ((pos (point)) (end (point-at-eol)))
  15838. (beginning-of-line 1)
  15839. (while (re-search-forward regexp end t)
  15840. (if (and (<= (match-beginning 0) pos)
  15841. (>= (match-end 0) pos))
  15842. (throw 'exit t)))
  15843. nil))))
  15844. (defun org-in-regexps-block-p (start-re end-re)
  15845. "Returns t if the current point is between matches of START-RE and END-RE.
  15846. This will also return to if point is on one of the two matches."
  15847. (interactive)
  15848. (let ((p (point)))
  15849. (save-excursion
  15850. (and (or (org-at-regexp-p start-re)
  15851. (re-search-backward start-re nil t))
  15852. (re-search-forward end-re nil t)
  15853. (>= (point) p)))))
  15854. (defun org-occur-in-agenda-files (regexp &optional nlines)
  15855. "Call `multi-occur' with buffers for all agenda files."
  15856. (interactive "sOrg-files matching: \np")
  15857. (let* ((files (org-agenda-files))
  15858. (tnames (mapcar 'file-truename files))
  15859. (extra org-agenda-text-search-extra-files)
  15860. f)
  15861. (when (eq (car extra) 'agenda-archives)
  15862. (setq extra (cdr extra))
  15863. (setq files (org-add-archive-files files)))
  15864. (while (setq f (pop extra))
  15865. (unless (member (file-truename f) tnames)
  15866. (add-to-list 'files f 'append)
  15867. (add-to-list 'tnames (file-truename f) 'append)))
  15868. (multi-occur
  15869. (mapcar (lambda (x)
  15870. (with-current-buffer
  15871. (or (get-file-buffer x) (find-file-noselect x))
  15872. (widen)
  15873. (current-buffer)))
  15874. files)
  15875. regexp)))
  15876. (if (boundp 'occur-mode-find-occurrence-hook)
  15877. ;; Emacs 23
  15878. (add-hook 'occur-mode-find-occurrence-hook
  15879. (lambda ()
  15880. (when (org-mode-p)
  15881. (org-reveal))))
  15882. ;; Emacs 22
  15883. (defadvice occur-mode-goto-occurrence
  15884. (after org-occur-reveal activate)
  15885. (and (org-mode-p) (org-reveal)))
  15886. (defadvice occur-mode-goto-occurrence-other-window
  15887. (after org-occur-reveal activate)
  15888. (and (org-mode-p) (org-reveal)))
  15889. (defadvice occur-mode-display-occurrence
  15890. (after org-occur-reveal activate)
  15891. (when (org-mode-p)
  15892. (let ((pos (occur-mode-find-occurrence)))
  15893. (with-current-buffer (marker-buffer pos)
  15894. (save-excursion
  15895. (goto-char pos)
  15896. (org-reveal)))))))
  15897. (defun org-occur-link-in-agenda-files ()
  15898. "Create a link and search for it in the agendas.
  15899. The link is not stored in `org-stored-links', it is just created
  15900. for the search purpose."
  15901. (interactive)
  15902. (let ((link (condition-case nil
  15903. (org-store-link nil)
  15904. (error "Unable to create a link to here"))))
  15905. (org-occur-in-agenda-files (regexp-quote link))))
  15906. (defun org-uniquify (list)
  15907. "Remove duplicate elements from LIST."
  15908. (let (res)
  15909. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  15910. res))
  15911. (defun org-delete-all (elts list)
  15912. "Remove all elements in ELTS from LIST."
  15913. (while elts
  15914. (setq list (delete (pop elts) list)))
  15915. list)
  15916. (defun org-back-over-empty-lines ()
  15917. "Move backwards over whitespace, to the beginning of the first empty line.
  15918. Returns the number of empty lines passed."
  15919. (let ((pos (point)))
  15920. (skip-chars-backward " \t\n\r")
  15921. (beginning-of-line 2)
  15922. (goto-char (min (point) pos))
  15923. (count-lines (point) pos)))
  15924. (defun org-skip-whitespace ()
  15925. (skip-chars-forward " \t\n\r"))
  15926. (defun org-point-in-group (point group &optional context)
  15927. "Check if POINT is in match-group GROUP.
  15928. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15929. match. If the match group does ot exist or point is not inside it,
  15930. return nil."
  15931. (and (match-beginning group)
  15932. (>= point (match-beginning group))
  15933. (<= point (match-end group))
  15934. (if context
  15935. (list context (match-beginning group) (match-end group))
  15936. t)))
  15937. (defun org-switch-to-buffer-other-window (&rest args)
  15938. "Switch to buffer in a second window on the current frame.
  15939. In particular, do not allow pop-up frames."
  15940. (let (pop-up-frames special-display-buffer-names special-display-regexps
  15941. special-display-function)
  15942. (apply 'switch-to-buffer-other-window args)))
  15943. (defun org-combine-plists (&rest plists)
  15944. "Create a single property list from all plists in PLISTS.
  15945. The process starts by copying the first list, and then setting properties
  15946. from the other lists. Settings in the last list are the most significant
  15947. ones and overrule settings in the other lists."
  15948. (let ((rtn (copy-sequence (pop plists)))
  15949. p v ls)
  15950. (while plists
  15951. (setq ls (pop plists))
  15952. (while ls
  15953. (setq p (pop ls) v (pop ls))
  15954. (setq rtn (plist-put rtn p v))))
  15955. rtn))
  15956. (defun org-move-line-down (arg)
  15957. "Move the current line down. With prefix argument, move it past ARG lines."
  15958. (interactive "p")
  15959. (let ((col (current-column))
  15960. beg end pos)
  15961. (beginning-of-line 1) (setq beg (point))
  15962. (beginning-of-line 2) (setq end (point))
  15963. (beginning-of-line (+ 1 arg))
  15964. (setq pos (move-marker (make-marker) (point)))
  15965. (insert (delete-and-extract-region beg end))
  15966. (goto-char pos)
  15967. (org-move-to-column col)))
  15968. (defun org-move-line-up (arg)
  15969. "Move the current line up. With prefix argument, move it past ARG lines."
  15970. (interactive "p")
  15971. (let ((col (current-column))
  15972. beg end pos)
  15973. (beginning-of-line 1) (setq beg (point))
  15974. (beginning-of-line 2) (setq end (point))
  15975. (beginning-of-line (- arg))
  15976. (setq pos (move-marker (make-marker) (point)))
  15977. (insert (delete-and-extract-region beg end))
  15978. (goto-char pos)
  15979. (org-move-to-column col)))
  15980. (defun org-replace-escapes (string table)
  15981. "Replace %-escapes in STRING with values in TABLE.
  15982. TABLE is an association list with keys like \"%a\" and string values.
  15983. The sequences in STRING may contain normal field width and padding information,
  15984. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  15985. so values can contain further %-escapes if they are define later in TABLE."
  15986. (let ((case-fold-search nil)
  15987. e re rpl)
  15988. (while (setq e (pop table))
  15989. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  15990. (while (string-match re string)
  15991. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  15992. (cdr e)))
  15993. (setq string (replace-match rpl t t string))))
  15994. string))
  15995. (defun org-sublist (list start end)
  15996. "Return a section of LIST, from START to END.
  15997. Counting starts at 1."
  15998. (let (rtn (c start))
  15999. (setq list (nthcdr (1- start) list))
  16000. (while (and list (<= c end))
  16001. (push (pop list) rtn)
  16002. (setq c (1+ c)))
  16003. (nreverse rtn)))
  16004. (defun org-find-base-buffer-visiting (file)
  16005. "Like `find-buffer-visiting' but always return the base buffer and
  16006. not an indirect buffer."
  16007. (let ((buf (or (get-file-buffer file)
  16008. (find-buffer-visiting file))))
  16009. (if buf
  16010. (or (buffer-base-buffer buf) buf)
  16011. nil)))
  16012. (defun org-image-file-name-regexp (&optional extensions)
  16013. "Return regexp matching the file names of images.
  16014. If EXTENSIONS is given, only match these."
  16015. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  16016. (image-file-name-regexp)
  16017. (let ((image-file-name-extensions
  16018. (or extensions
  16019. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  16020. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  16021. (concat "\\."
  16022. (regexp-opt (nconc (mapcar 'upcase
  16023. image-file-name-extensions)
  16024. image-file-name-extensions)
  16025. t)
  16026. "\\'"))))
  16027. (defun org-file-image-p (file &optional extensions)
  16028. "Return non-nil if FILE is an image."
  16029. (save-match-data
  16030. (string-match (org-image-file-name-regexp extensions) file)))
  16031. (defun org-get-cursor-date ()
  16032. "Return the date at cursor in as a time.
  16033. This works in the calendar and in the agenda, anywhere else it just
  16034. returns the current time."
  16035. (let (date day defd)
  16036. (cond
  16037. ((eq major-mode 'calendar-mode)
  16038. (setq date (calendar-cursor-to-date)
  16039. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16040. ((eq major-mode 'org-agenda-mode)
  16041. (setq day (get-text-property (point) 'day))
  16042. (if day
  16043. (setq date (calendar-gregorian-from-absolute day)
  16044. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  16045. (nth 2 date))))))
  16046. (or defd (current-time))))
  16047. (defvar org-agenda-action-marker (make-marker)
  16048. "Marker pointing to the entry for the next agenda action.")
  16049. (defun org-mark-entry-for-agenda-action ()
  16050. "Mark the current entry as target of an agenda action.
  16051. Agenda actions are actions executed from the agenda with the key `k',
  16052. which make use of the date at the cursor."
  16053. (interactive)
  16054. (move-marker org-agenda-action-marker
  16055. (save-excursion (org-back-to-heading t) (point))
  16056. (current-buffer))
  16057. (message
  16058. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  16059. ;;; Paragraph filling stuff.
  16060. ;; We want this to be just right, so use the full arsenal.
  16061. (defun org-indent-line-function ()
  16062. "Indent line like previous, but further if previous was headline or item."
  16063. (interactive)
  16064. (let* ((pos (point))
  16065. (itemp (org-at-item-p))
  16066. (case-fold-search t)
  16067. (org-drawer-regexp (or org-drawer-regexp "\000"))
  16068. column bpos bcol tpos tcol bullet btype bullet-type)
  16069. ;; Find the previous relevant line
  16070. (beginning-of-line 1)
  16071. (cond
  16072. ((looking-at "#") (setq column 0))
  16073. ((looking-at "\\*+ ") (setq column 0))
  16074. ((and (looking-at "[ \t]*:END:")
  16075. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  16076. (save-excursion
  16077. (goto-char (1- (match-beginning 1)))
  16078. (setq column (current-column))))
  16079. ((and (looking-at "[ \t]+#\\+end_\\([a-z]+\\)")
  16080. (save-excursion
  16081. (re-search-backward
  16082. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  16083. (setq column (org-get-indentation (match-string 0))))
  16084. (t
  16085. (beginning-of-line 0)
  16086. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  16087. (not (looking-at "[ \t]*:END:"))
  16088. (not (looking-at org-drawer-regexp)))
  16089. (beginning-of-line 0))
  16090. (cond
  16091. ((looking-at "\\*+[ \t]+")
  16092. (if (not org-adapt-indentation)
  16093. (setq column 0)
  16094. (goto-char (match-end 0))
  16095. (setq column (current-column))))
  16096. ((looking-at org-drawer-regexp)
  16097. (goto-char (1- (match-beginning 1)))
  16098. (setq column (current-column)))
  16099. ((looking-at "\\([ \t]*\\):END:")
  16100. (goto-char (match-end 1))
  16101. (setq column (current-column)))
  16102. ((org-in-item-p)
  16103. (org-beginning-of-item)
  16104. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  16105. (setq bpos (match-beginning 1) tpos (match-end 0)
  16106. bcol (progn (goto-char bpos) (current-column))
  16107. tcol (progn (goto-char tpos) (current-column))
  16108. bullet (match-string 1)
  16109. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  16110. (if (> tcol (+ bcol org-description-max-indent))
  16111. (setq tcol (+ bcol 5)))
  16112. (if (not itemp)
  16113. (setq column tcol)
  16114. (goto-char pos)
  16115. (beginning-of-line 1)
  16116. (if (looking-at "\\S-")
  16117. (progn
  16118. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  16119. (setq bullet (match-string 1)
  16120. btype (if (string-match "[0-9]" bullet) "n" bullet))
  16121. (setq column (if (equal btype bullet-type) bcol tcol)))
  16122. (setq column (org-get-indentation)))))
  16123. (t (setq column (org-get-indentation))))))
  16124. (goto-char pos)
  16125. (if (<= (current-column) (current-indentation))
  16126. (org-indent-line-to column)
  16127. (save-excursion (org-indent-line-to column)))
  16128. (setq column (current-column))
  16129. (beginning-of-line 1)
  16130. (if (looking-at
  16131. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  16132. (replace-match (concat (match-string 1)
  16133. (format org-property-format
  16134. (match-string 2) (match-string 3)))
  16135. t t))
  16136. (org-move-to-column column)))
  16137. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  16138. "Variable to store copy of `adaptive-fill-regexp'.
  16139. Since `adaptive-fill-regexp' is set to never match, we need to
  16140. store a backup of its value before entering `org-mode' so that
  16141. the functionality can be provided as a fall-back.")
  16142. (defun org-set-autofill-regexps ()
  16143. (interactive)
  16144. ;; In the paragraph separator we include headlines, because filling
  16145. ;; text in a line directly attached to a headline would otherwise
  16146. ;; fill the headline as well.
  16147. (org-set-local 'comment-start-skip "^#+[ \t]*")
  16148. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  16149. ;; The paragraph starter includes hand-formatted lists.
  16150. (org-set-local
  16151. 'paragraph-start
  16152. (concat
  16153. "\f" "\\|"
  16154. "[ ]*$" "\\|"
  16155. "\\*+ " "\\|"
  16156. "[ \t]*#" "\\|"
  16157. "[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)" "\\|"
  16158. "[ \t]*[:|]" "\\|"
  16159. "\\$\\$" "\\|"
  16160. "\\\\\\(begin\\|end\\|[][]\\)"))
  16161. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  16162. ;; But only if the user has not turned off tables or fixed-width regions
  16163. (org-set-local
  16164. 'auto-fill-inhibit-regexp
  16165. (concat "\\*+ \\|#\\+"
  16166. "\\|[ \t]*" org-keyword-time-regexp
  16167. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  16168. (concat
  16169. "\\|[ \t]*["
  16170. (if org-enable-table-editor "|" "")
  16171. (if org-enable-fixed-width-editor ":" "")
  16172. "]"))))
  16173. ;; We use our own fill-paragraph function, to make sure that tables
  16174. ;; and fixed-width regions are not wrapped. That function will pass
  16175. ;; through to `fill-paragraph' when appropriate.
  16176. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  16177. ;; Adaptive filling: To get full control, first make sure that
  16178. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  16179. (unless (local-variable-p 'adaptive-fill-regexp)
  16180. (org-set-local 'org-adaptive-fill-regexp-backup
  16181. adaptive-fill-regexp))
  16182. (org-set-local 'adaptive-fill-regexp "\000")
  16183. (org-set-local 'adaptive-fill-function
  16184. 'org-adaptive-fill-function)
  16185. (org-set-local
  16186. 'align-mode-rules-list
  16187. '((org-in-buffer-settings
  16188. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  16189. (modes . '(org-mode))))))
  16190. (defun org-fill-paragraph (&optional justify)
  16191. "Re-align a table, pass through to fill-paragraph if no table."
  16192. (let ((table-p (org-at-table-p))
  16193. (table.el-p (org-at-table.el-p)))
  16194. (cond ((and (equal (char-after (point-at-bol)) ?*)
  16195. (save-excursion (goto-char (point-at-bol))
  16196. (looking-at outline-regexp)))
  16197. t) ; skip headlines
  16198. (table.el-p t) ; skip table.el tables
  16199. (table-p (org-table-align) t) ; align org-mode tables
  16200. (t nil)))) ; call paragraph-fill
  16201. ;; For reference, this is the default value of adaptive-fill-regexp
  16202. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  16203. (defun org-adaptive-fill-function ()
  16204. "Return a fill prefix for org-mode files.
  16205. In particular, this makes sure hanging paragraphs for hand-formatted lists
  16206. work correctly."
  16207. (cond
  16208. ;; Comment line
  16209. ((looking-at "#[ \t]+")
  16210. (match-string-no-properties 0))
  16211. ;; Description list
  16212. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  16213. (save-excursion
  16214. (if (> (match-end 1) (+ (match-beginning 1)
  16215. org-description-max-indent))
  16216. (goto-char (+ (match-beginning 1) 5))
  16217. (goto-char (match-end 0)))
  16218. (make-string (current-column) ?\ )))
  16219. ;; Ordered or unordered list
  16220. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)")
  16221. (save-excursion
  16222. (goto-char (match-end 0))
  16223. (make-string (current-column) ?\ )))
  16224. ;; Other text
  16225. ((looking-at org-adaptive-fill-regexp-backup)
  16226. (match-string-no-properties 0))))
  16227. ;;; Other stuff.
  16228. (defun org-toggle-fixed-width-section (arg)
  16229. "Toggle the fixed-width export.
  16230. If there is no active region, the QUOTE keyword at the current headline is
  16231. inserted or removed. When present, it causes the text between this headline
  16232. and the next to be exported as fixed-width text, and unmodified.
  16233. If there is an active region, this command adds or removes a colon as the
  16234. first character of this line. If the first character of a line is a colon,
  16235. this line is also exported in fixed-width font."
  16236. (interactive "P")
  16237. (let* ((cc 0)
  16238. (regionp (org-region-active-p))
  16239. (beg (if regionp (region-beginning) (point)))
  16240. (end (if regionp (region-end)))
  16241. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  16242. (case-fold-search nil)
  16243. (re "[ \t]*\\(: \\)")
  16244. off)
  16245. (if regionp
  16246. (save-excursion
  16247. (goto-char beg)
  16248. (setq cc (current-column))
  16249. (beginning-of-line 1)
  16250. (setq off (looking-at re))
  16251. (while (> nlines 0)
  16252. (setq nlines (1- nlines))
  16253. (beginning-of-line 1)
  16254. (cond
  16255. (arg
  16256. (org-move-to-column cc t)
  16257. (insert ": \n")
  16258. (forward-line -1))
  16259. ((and off (looking-at re))
  16260. (replace-match "" t t nil 1))
  16261. ((not off) (org-move-to-column cc t) (insert ": ")))
  16262. (forward-line 1)))
  16263. (save-excursion
  16264. (org-back-to-heading)
  16265. (if (looking-at (concat outline-regexp
  16266. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  16267. (replace-match "" t t nil 1)
  16268. (if (looking-at outline-regexp)
  16269. (progn
  16270. (goto-char (match-end 0))
  16271. (insert org-quote-string " "))))))))
  16272. (defun org-reftex-citation ()
  16273. "Use reftex-citation to insert a citation into the buffer.
  16274. This looks for a line like
  16275. #+BIBLIOGRAPHY: foo plain option:-d
  16276. and derives from it that foo.bib is the bibliography file relevant
  16277. for this document. It then installs the necessary environment for RefTeX
  16278. to work in this buffer and calls `reftex-citation' to insert a citation
  16279. into the buffer.
  16280. Export of such citations to both LaTeX and HTML is handled by the contributed
  16281. package org-exp-bibtex by Taru Karttunen."
  16282. (interactive)
  16283. (let ((reftex-docstruct-symbol 'rds)
  16284. (reftex-cite-format "\\cite{%l}")
  16285. rds bib)
  16286. (save-excursion
  16287. (save-restriction
  16288. (widen)
  16289. (let ((case-fold-search t)
  16290. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  16291. (if (not (save-excursion
  16292. (or (re-search-forward re nil t)
  16293. (re-search-backward re nil t))))
  16294. (error "No bibliography defined in file")
  16295. (setq bib (concat (match-string 1) ".bib")
  16296. rds (list (list 'bib bib)))))))
  16297. (call-interactively 'reftex-citation)))
  16298. ;;;; Functions extending outline functionality
  16299. (defun org-beginning-of-line (&optional arg)
  16300. "Go to the beginning of the current line. If that is invisible, continue
  16301. to a visible line beginning. This makes the function of C-a more intuitive.
  16302. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16303. first attempt, and only move to after the tags when the cursor is already
  16304. beyond the end of the headline."
  16305. (interactive "P")
  16306. (let ((pos (point))
  16307. (special (if (consp org-special-ctrl-a/e)
  16308. (car org-special-ctrl-a/e)
  16309. org-special-ctrl-a/e))
  16310. refpos)
  16311. (if (org-bound-and-true-p line-move-visual)
  16312. (beginning-of-visual-line 1)
  16313. (beginning-of-line 1))
  16314. (if (and arg (fboundp 'move-beginning-of-line))
  16315. (call-interactively 'move-beginning-of-line)
  16316. (if (bobp)
  16317. nil
  16318. (backward-char 1)
  16319. (if (org-invisible-p)
  16320. (while (and (not (bobp)) (org-invisible-p))
  16321. (backward-char 1)
  16322. (beginning-of-line 1))
  16323. (forward-char 1))))
  16324. (when special
  16325. (cond
  16326. ((and (looking-at org-complex-heading-regexp)
  16327. (= (char-after (match-end 1)) ?\ ))
  16328. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  16329. (point-at-eol)))
  16330. (goto-char
  16331. (if (eq special t)
  16332. (cond ((> pos refpos) refpos)
  16333. ((= pos (point)) refpos)
  16334. (t (point)))
  16335. (cond ((> pos (point)) (point))
  16336. ((not (eq last-command this-command)) (point))
  16337. (t refpos)))))
  16338. ((org-at-item-p)
  16339. (goto-char
  16340. (if (eq special t)
  16341. (cond ((> pos (match-end 4)) (match-end 4))
  16342. ((= pos (point)) (match-end 4))
  16343. (t (point)))
  16344. (cond ((> pos (point)) (point))
  16345. ((not (eq last-command this-command)) (point))
  16346. (t (match-end 4))))))))
  16347. (org-no-warnings
  16348. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16349. (defun org-end-of-line (&optional arg)
  16350. "Go to the end of the line.
  16351. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  16352. first attempt, and only move to after the tags when the cursor is already
  16353. beyond the end of the headline."
  16354. (interactive "P")
  16355. (let ((special (if (consp org-special-ctrl-a/e)
  16356. (cdr org-special-ctrl-a/e)
  16357. org-special-ctrl-a/e)))
  16358. (if (or (not special)
  16359. (not (org-on-heading-p))
  16360. arg)
  16361. (call-interactively
  16362. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  16363. ((fboundp 'move-end-of-line) 'move-end-of-line)
  16364. (t 'end-of-line)))
  16365. (let ((pos (point)))
  16366. (beginning-of-line 1)
  16367. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*\\)?$"))
  16368. (if (eq special t)
  16369. (if (or (< pos (match-beginning 1))
  16370. (= pos (match-end 0)))
  16371. (goto-char (match-beginning 1))
  16372. (goto-char (match-end 0)))
  16373. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  16374. (goto-char (match-end 0))
  16375. (goto-char (match-beginning 1))))
  16376. (call-interactively (if (fboundp 'move-end-of-line)
  16377. 'move-end-of-line
  16378. 'end-of-line)))))
  16379. (org-no-warnings
  16380. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  16381. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  16382. (define-key org-mode-map "\C-e" 'org-end-of-line)
  16383. (define-key org-mode-map [home] 'org-beginning-of-line)
  16384. (define-key org-mode-map [end] 'org-end-of-line)
  16385. (defun org-backward-sentence (&optional arg)
  16386. "Go to beginning of sentence, or beginning of table field.
  16387. This will call `backward-sentence' or `org-table-beginning-of-field',
  16388. depending on context."
  16389. (interactive "P")
  16390. (cond
  16391. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  16392. (t (call-interactively 'backward-sentence))))
  16393. (defun org-forward-sentence (&optional arg)
  16394. "Go to end of sentence, or end of table field.
  16395. This will call `forward-sentence' or `org-table-end-of-field',
  16396. depending on context."
  16397. (interactive "P")
  16398. (cond
  16399. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  16400. (t (call-interactively 'forward-sentence))))
  16401. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  16402. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  16403. (defun org-kill-line (&optional arg)
  16404. "Kill line, to tags or end of line."
  16405. (interactive "P")
  16406. (cond
  16407. ((or (not org-special-ctrl-k)
  16408. (bolp)
  16409. (not (org-on-heading-p)))
  16410. (call-interactively 'kill-line))
  16411. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  16412. (kill-region (point) (match-beginning 1))
  16413. (org-set-tags nil t))
  16414. (t (kill-region (point) (point-at-eol)))))
  16415. (define-key org-mode-map "\C-k" 'org-kill-line)
  16416. (defun org-yank (&optional arg)
  16417. "Yank. If the kill is a subtree, treat it specially.
  16418. This command will look at the current kill and check if is a single
  16419. subtree, or a series of subtrees[1]. If it passes the test, and if the
  16420. cursor is at the beginning of a line or after the stars of a currently
  16421. empty headline, then the yank is handled specially. How exactly depends
  16422. on the value of the following variables, both set by default.
  16423. org-yank-folded-subtrees
  16424. When set, the subtree(s) will be folded after insertion, but only
  16425. if doing so would now swallow text after the yanked text.
  16426. org-yank-adjusted-subtrees
  16427. When set, the subtree will be promoted or demoted in order to
  16428. fit into the local outline tree structure, which means that the level
  16429. will be adjusted so that it becomes the smaller one of the two
  16430. *visible* surrounding headings.
  16431. Any prefix to this command will cause `yank' to be called directly with
  16432. no special treatment. In particular, a simple `C-u' prefix will just
  16433. plainly yank the text as it is.
  16434. \[1] The test checks if the first non-white line is a heading
  16435. and if there are no other headings with fewer stars."
  16436. (interactive "P")
  16437. (org-yank-generic 'yank arg))
  16438. (defun org-yank-generic (command arg)
  16439. "Perform some yank-like command.
  16440. This function implements the behavior described in the `org-yank'
  16441. documentation. However, it has been generalized to work for any
  16442. interactive command with similar behavior."
  16443. ;; pretend to be command COMMAND
  16444. (setq this-command command)
  16445. (if arg
  16446. (call-interactively command)
  16447. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  16448. (and (org-kill-is-subtree-p)
  16449. (or (bolp)
  16450. (and (looking-at "[ \t]*$")
  16451. (string-match
  16452. "\\`\\*+\\'"
  16453. (buffer-substring (point-at-bol) (point)))))))
  16454. swallowp)
  16455. (cond
  16456. ((and subtreep org-yank-folded-subtrees)
  16457. (let ((beg (point))
  16458. end)
  16459. (if (and subtreep org-yank-adjusted-subtrees)
  16460. (org-paste-subtree nil nil 'for-yank)
  16461. (call-interactively command))
  16462. (setq end (point))
  16463. (goto-char beg)
  16464. (when (and (bolp) subtreep
  16465. (not (setq swallowp
  16466. (org-yank-folding-would-swallow-text beg end))))
  16467. (or (looking-at outline-regexp)
  16468. (re-search-forward (concat "^" outline-regexp) end t))
  16469. (while (and (< (point) end) (looking-at outline-regexp))
  16470. (hide-subtree)
  16471. (org-cycle-show-empty-lines 'folded)
  16472. (condition-case nil
  16473. (outline-forward-same-level 1)
  16474. (error (goto-char end)))))
  16475. (when swallowp
  16476. (message
  16477. "Inserted text not folded because that would swallow text"))
  16478. (goto-char end)
  16479. (skip-chars-forward " \t\n\r")
  16480. (beginning-of-line 1)
  16481. (push-mark beg 'nomsg)))
  16482. ((and subtreep org-yank-adjusted-subtrees)
  16483. (let ((beg (point-at-bol)))
  16484. (org-paste-subtree nil nil 'for-yank)
  16485. (push-mark beg 'nomsg)))
  16486. (t
  16487. (call-interactively command))))))
  16488. (defun org-yank-folding-would-swallow-text (beg end)
  16489. "Would hide-subtree at BEG swallow any text after END?"
  16490. (let (level)
  16491. (save-excursion
  16492. (goto-char beg)
  16493. (when (or (looking-at outline-regexp)
  16494. (re-search-forward (concat "^" outline-regexp) end t))
  16495. (setq level (org-outline-level)))
  16496. (goto-char end)
  16497. (skip-chars-forward " \t\r\n\v\f")
  16498. (if (or (eobp)
  16499. (and (bolp) (looking-at org-outline-regexp)
  16500. (<= (org-outline-level) level)))
  16501. nil ; Nothing would be swallowed
  16502. t)))) ; something would swallow
  16503. (define-key org-mode-map "\C-y" 'org-yank)
  16504. (defun org-invisible-p ()
  16505. "Check if point is at a character currently not visible."
  16506. ;; Early versions of noutline don't have `outline-invisible-p'.
  16507. (if (fboundp 'outline-invisible-p)
  16508. (outline-invisible-p)
  16509. (get-char-property (point) 'invisible)))
  16510. (defun org-invisible-p2 ()
  16511. "Check if point is at a character currently not visible."
  16512. (save-excursion
  16513. (if (and (eolp) (not (bobp))) (backward-char 1))
  16514. ;; Early versions of noutline don't have `outline-invisible-p'.
  16515. (if (fboundp 'outline-invisible-p)
  16516. (outline-invisible-p)
  16517. (get-char-property (point) 'invisible))))
  16518. (defun org-back-to-heading (&optional invisible-ok)
  16519. "Call `outline-back-to-heading', but provide a better error message."
  16520. (condition-case nil
  16521. (outline-back-to-heading invisible-ok)
  16522. (error (error "Before first headline at position %d in buffer %s"
  16523. (point) (current-buffer)))))
  16524. (defun org-before-first-heading-p ()
  16525. "Before first heading?"
  16526. (save-excursion
  16527. (null (re-search-backward "^\\*+ " nil t))))
  16528. (defun org-on-heading-p (&optional ignored)
  16529. (outline-on-heading-p t))
  16530. (defun org-at-heading-p (&optional ignored)
  16531. (outline-on-heading-p t))
  16532. (defun org-point-at-end-of-empty-headline ()
  16533. "If point is at the end of an empty headline, return t, else nil.
  16534. If the heading only contains a TODO keyword, it is still still considered
  16535. empty."
  16536. (and (looking-at "[ \t]*$")
  16537. (save-excursion
  16538. (beginning-of-line 1)
  16539. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  16540. "\\)?[ \t]*$")))))
  16541. (defun org-at-heading-or-item-p ()
  16542. (or (org-on-heading-p) (org-at-item-p)))
  16543. (defun org-on-target-p ()
  16544. (or (org-in-regexp org-radio-target-regexp)
  16545. (org-in-regexp org-target-regexp)))
  16546. (defun org-up-heading-all (arg)
  16547. "Move to the heading line of which the present line is a subheading.
  16548. This function considers both visible and invisible heading lines.
  16549. With argument, move up ARG levels."
  16550. (if (fboundp 'outline-up-heading-all)
  16551. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  16552. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  16553. (defun org-up-heading-safe ()
  16554. "Move to the heading line of which the present line is a subheading.
  16555. This version will not throw an error. It will return the level of the
  16556. headline found, or nil if no higher level is found.
  16557. Also, this function will be a lot faster than `outline-up-heading',
  16558. because it relies on stars being the outline starters. This can really
  16559. make a significant difference in outlines with very many siblings."
  16560. (let (start-level re)
  16561. (org-back-to-heading t)
  16562. (setq start-level (funcall outline-level))
  16563. (if (equal start-level 1)
  16564. nil
  16565. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  16566. (if (re-search-backward re nil t)
  16567. (funcall outline-level)))))
  16568. (defun org-first-sibling-p ()
  16569. "Is this heading the first child of its parents?"
  16570. (interactive)
  16571. (let ((re (concat "^" outline-regexp))
  16572. level l)
  16573. (unless (org-at-heading-p t)
  16574. (error "Not at a heading"))
  16575. (setq level (funcall outline-level))
  16576. (save-excursion
  16577. (if (not (re-search-backward re nil t))
  16578. t
  16579. (setq l (funcall outline-level))
  16580. (< l level)))))
  16581. (defun org-goto-sibling (&optional previous)
  16582. "Goto the next sibling, even if it is invisible.
  16583. When PREVIOUS is set, go to the previous sibling instead. Returns t
  16584. when a sibling was found. When none is found, return nil and don't
  16585. move point."
  16586. (let ((fun (if previous 're-search-backward 're-search-forward))
  16587. (pos (point))
  16588. (re (concat "^" outline-regexp))
  16589. level l)
  16590. (when (condition-case nil (org-back-to-heading t) (error nil))
  16591. (setq level (funcall outline-level))
  16592. (catch 'exit
  16593. (or previous (forward-char 1))
  16594. (while (funcall fun re nil t)
  16595. (setq l (funcall outline-level))
  16596. (when (< l level) (goto-char pos) (throw 'exit nil))
  16597. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  16598. (goto-char pos)
  16599. nil))))
  16600. (defun org-show-siblings ()
  16601. "Show all siblings of the current headline."
  16602. (save-excursion
  16603. (while (org-goto-sibling) (org-flag-heading nil)))
  16604. (save-excursion
  16605. (while (org-goto-sibling 'previous)
  16606. (org-flag-heading nil))))
  16607. (defun org-show-hidden-entry ()
  16608. "Show an entry where even the heading is hidden."
  16609. (save-excursion
  16610. (org-show-entry)))
  16611. (defun org-flag-heading (flag &optional entry)
  16612. "Flag the current heading. FLAG non-nil means make invisible.
  16613. When ENTRY is non-nil, show the entire entry."
  16614. (save-excursion
  16615. (org-back-to-heading t)
  16616. ;; Check if we should show the entire entry
  16617. (if entry
  16618. (progn
  16619. (org-show-entry)
  16620. (save-excursion
  16621. (and (outline-next-heading)
  16622. (org-flag-heading nil))))
  16623. (outline-flag-region (max (point-min) (1- (point)))
  16624. (save-excursion (outline-end-of-heading) (point))
  16625. flag))))
  16626. (defun org-get-next-sibling ()
  16627. "Move to next heading of the same level, and return point.
  16628. If there is no such heading, return nil.
  16629. This is like outline-next-sibling, but invisible headings are ok."
  16630. (let ((level (funcall outline-level)))
  16631. (outline-next-heading)
  16632. (while (and (not (eobp)) (> (funcall outline-level) level))
  16633. (outline-next-heading))
  16634. (if (or (eobp) (< (funcall outline-level) level))
  16635. nil
  16636. (point))))
  16637. (defun org-get-last-sibling ()
  16638. "Move to previous heading of the same level, and return point.
  16639. If there is no such heading, return nil."
  16640. (let ((opoint (point))
  16641. (level (funcall outline-level)))
  16642. (outline-previous-heading)
  16643. (when (and (/= (point) opoint) (outline-on-heading-p t))
  16644. (while (and (> (funcall outline-level) level)
  16645. (not (bobp)))
  16646. (outline-previous-heading))
  16647. (if (< (funcall outline-level) level)
  16648. nil
  16649. (point)))))
  16650. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  16651. ;; This contains an exact copy of the original function, but it uses
  16652. ;; `org-back-to-heading', to make it work also in invisible
  16653. ;; trees. And is uses an invisible-OK argument.
  16654. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  16655. ;; Furthermore, when used inside Org, finding the end of a large subtree
  16656. ;; with many children and grandchildren etc, this can be much faster
  16657. ;; than the outline version.
  16658. (org-back-to-heading invisible-OK)
  16659. (let ((first t)
  16660. (level (funcall outline-level)))
  16661. (if (and (org-mode-p) (< level 1000))
  16662. ;; A true heading (not a plain list item), in Org-mode
  16663. ;; This means we can easily find the end by looking
  16664. ;; only for the right number of stars. Using a regexp to do
  16665. ;; this is so much faster than using a Lisp loop.
  16666. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  16667. (forward-char 1)
  16668. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  16669. ;; something else, do it the slow way
  16670. (while (and (not (eobp))
  16671. (or first (> (funcall outline-level) level)))
  16672. (setq first nil)
  16673. (outline-next-heading)))
  16674. (unless to-heading
  16675. (if (memq (preceding-char) '(?\n ?\^M))
  16676. (progn
  16677. ;; Go to end of line before heading
  16678. (forward-char -1)
  16679. (if (memq (preceding-char) '(?\n ?\^M))
  16680. ;; leave blank line before heading
  16681. (forward-char -1))))))
  16682. (point))
  16683. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  16684. "Use Org version in org-mode, for dramatic speed-up."
  16685. (if (eq major-mode 'org-mode)
  16686. (progn
  16687. (org-end-of-subtree nil t)
  16688. (unless (eobp) (backward-char 1)))
  16689. ad-do-it))
  16690. (defun org-forward-same-level (arg &optional invisible-ok)
  16691. "Move forward to the arg'th subheading at same level as this one.
  16692. Stop at the first and last subheadings of a superior heading."
  16693. (interactive "p")
  16694. (org-back-to-heading invisible-ok)
  16695. (org-on-heading-p)
  16696. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16697. (re (format "^\\*\\{1,%d\\} " level))
  16698. l)
  16699. (forward-char 1)
  16700. (while (> arg 0)
  16701. (while (and (re-search-forward re nil 'move)
  16702. (setq l (- (match-end 0) (match-beginning 0) 1))
  16703. (= l level)
  16704. (not invisible-ok)
  16705. (progn (backward-char 1) (org-invisible-p)))
  16706. (if (< l level) (setq arg 1)))
  16707. (setq arg (1- arg)))
  16708. (beginning-of-line 1)))
  16709. (defun org-backward-same-level (arg &optional invisible-ok)
  16710. "Move backward to the arg'th subheading at same level as this one.
  16711. Stop at the first and last subheadings of a superior heading."
  16712. (interactive "p")
  16713. (org-back-to-heading)
  16714. (org-on-heading-p)
  16715. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  16716. (re (format "^\\*\\{1,%d\\} " level))
  16717. l)
  16718. (while (> arg 0)
  16719. (while (and (re-search-backward re nil 'move)
  16720. (setq l (- (match-end 0) (match-beginning 0) 1))
  16721. (= l level)
  16722. (not invisible-ok)
  16723. (org-invisible-p))
  16724. (if (< l level) (setq arg 1)))
  16725. (setq arg (1- arg)))))
  16726. (defun org-show-subtree ()
  16727. "Show everything after this heading at deeper levels."
  16728. (outline-flag-region
  16729. (point)
  16730. (save-excursion
  16731. (org-end-of-subtree t t))
  16732. nil))
  16733. (defun org-show-entry ()
  16734. "Show the body directly following this heading.
  16735. Show the heading too, if it is currently invisible."
  16736. (interactive)
  16737. (save-excursion
  16738. (condition-case nil
  16739. (progn
  16740. (org-back-to-heading t)
  16741. (outline-flag-region
  16742. (max (point-min) (1- (point)))
  16743. (save-excursion
  16744. (if (re-search-forward
  16745. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  16746. (match-beginning 1)
  16747. (point-max)))
  16748. nil)
  16749. (org-cycle-hide-drawers 'children))
  16750. (error nil))))
  16751. (defun org-make-options-regexp (kwds &optional extra)
  16752. "Make a regular expression for keyword lines."
  16753. (concat
  16754. "^"
  16755. "#?[ \t]*\\+\\("
  16756. (mapconcat 'regexp-quote kwds "\\|")
  16757. (if extra (concat "\\|" extra))
  16758. "\\):[ \t]*"
  16759. "\\(.*\\)"))
  16760. ;; Make isearch reveal the necessary context
  16761. (defun org-isearch-end ()
  16762. "Reveal context after isearch exits."
  16763. (when isearch-success ; only if search was successful
  16764. (if (featurep 'xemacs)
  16765. ;; Under XEmacs, the hook is run in the correct place,
  16766. ;; we directly show the context.
  16767. (org-show-context 'isearch)
  16768. ;; In Emacs the hook runs *before* restoring the overlays.
  16769. ;; So we have to use a one-time post-command-hook to do this.
  16770. ;; (Emacs 22 has a special variable, see function `org-mode')
  16771. (unless (and (boundp 'isearch-mode-end-hook-quit)
  16772. isearch-mode-end-hook-quit)
  16773. ;; Only when the isearch was not quitted.
  16774. (org-add-hook 'post-command-hook 'org-isearch-post-command
  16775. 'append 'local)))))
  16776. (defun org-isearch-post-command ()
  16777. "Remove self from hook, and show context."
  16778. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  16779. (org-show-context 'isearch))
  16780. ;;;; Integration with and fixes for other packages
  16781. ;;; Imenu support
  16782. (defvar org-imenu-markers nil
  16783. "All markers currently used by Imenu.")
  16784. (make-variable-buffer-local 'org-imenu-markers)
  16785. (defun org-imenu-new-marker (&optional pos)
  16786. "Return a new marker for use by Imenu, and remember the marker."
  16787. (let ((m (make-marker)))
  16788. (move-marker m (or pos (point)))
  16789. (push m org-imenu-markers)
  16790. m))
  16791. (defun org-imenu-get-tree ()
  16792. "Produce the index for Imenu."
  16793. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  16794. (setq org-imenu-markers nil)
  16795. (let* ((n org-imenu-depth)
  16796. (re (concat "^" outline-regexp))
  16797. (subs (make-vector (1+ n) nil))
  16798. (last-level 0)
  16799. m level head)
  16800. (save-excursion
  16801. (save-restriction
  16802. (widen)
  16803. (goto-char (point-max))
  16804. (while (re-search-backward re nil t)
  16805. (setq level (org-reduced-level (funcall outline-level)))
  16806. (when (<= level n)
  16807. (looking-at org-complex-heading-regexp)
  16808. (setq head (org-link-display-format
  16809. (org-match-string-no-properties 4))
  16810. m (org-imenu-new-marker))
  16811. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  16812. (if (>= level last-level)
  16813. (push (cons head m) (aref subs level))
  16814. (push (cons head (aref subs (1+ level))) (aref subs level))
  16815. (loop for i from (1+ level) to n do (aset subs i nil)))
  16816. (setq last-level level)))))
  16817. (aref subs 1)))
  16818. (eval-after-load "imenu"
  16819. '(progn
  16820. (add-hook 'imenu-after-jump-hook
  16821. (lambda ()
  16822. (if (eq major-mode 'org-mode)
  16823. (org-show-context 'org-goto))))))
  16824. (defun org-link-display-format (link)
  16825. "Replace a link with either the description, or the link target
  16826. if no description is present"
  16827. (save-match-data
  16828. (if (string-match org-bracket-link-analytic-regexp link)
  16829. (replace-match (if (match-end 5)
  16830. (match-string 5 link)
  16831. (concat (match-string 1 link)
  16832. (match-string 3 link)))
  16833. nil t link)
  16834. link)))
  16835. ;; Speedbar support
  16836. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  16837. "Overlay marking the agenda restriction line in speedbar.")
  16838. (org-overlay-put org-speedbar-restriction-lock-overlay
  16839. 'face 'org-agenda-restriction-lock)
  16840. (org-overlay-put org-speedbar-restriction-lock-overlay
  16841. 'help-echo "Agendas are currently limited to this item.")
  16842. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  16843. (defun org-speedbar-set-agenda-restriction ()
  16844. "Restrict future agenda commands to the location at point in speedbar.
  16845. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  16846. (interactive)
  16847. (require 'org-agenda)
  16848. (let (p m tp np dir txt)
  16849. (cond
  16850. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16851. 'org-imenu t))
  16852. (setq m (get-text-property p 'org-imenu-marker))
  16853. (with-current-buffer (marker-buffer m)
  16854. (goto-char m)
  16855. (org-agenda-set-restriction-lock 'subtree)))
  16856. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  16857. 'speedbar-function 'speedbar-find-file))
  16858. (setq tp (previous-single-property-change
  16859. (1+ p) 'speedbar-function)
  16860. np (next-single-property-change
  16861. tp 'speedbar-function)
  16862. dir (speedbar-line-directory)
  16863. txt (buffer-substring-no-properties (or tp (point-min))
  16864. (or np (point-max))))
  16865. (with-current-buffer (find-file-noselect
  16866. (let ((default-directory dir))
  16867. (expand-file-name txt)))
  16868. (unless (org-mode-p)
  16869. (error "Cannot restrict to non-Org-mode file"))
  16870. (org-agenda-set-restriction-lock 'file)))
  16871. (t (error "Don't know how to restrict Org-mode's agenda")))
  16872. (org-move-overlay org-speedbar-restriction-lock-overlay
  16873. (point-at-bol) (point-at-eol))
  16874. (setq current-prefix-arg nil)
  16875. (org-agenda-maybe-redo)))
  16876. (eval-after-load "speedbar"
  16877. '(progn
  16878. (speedbar-add-supported-extension ".org")
  16879. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  16880. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  16881. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  16882. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16883. (add-hook 'speedbar-visiting-tag-hook
  16884. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  16885. ;;; Fixes and Hacks for problems with other packages
  16886. ;; Make flyspell not check words in links, to not mess up our keymap
  16887. (defun org-mode-flyspell-verify ()
  16888. "Don't let flyspell put overlays at active buttons."
  16889. (and (not (get-text-property (point) 'keymap))
  16890. (not (get-text-property (point) 'org-no-flyspell))))
  16891. (defun org-remove-flyspell-overlays-in (beg end)
  16892. "Remove flyspell overlays in region."
  16893. (and (org-bound-and-true-p flyspell-mode)
  16894. (fboundp 'flyspell-delete-region-overlays)
  16895. (flyspell-delete-region-overlays beg end))
  16896. (add-text-properties beg end '(org-no-flyspell t)))
  16897. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  16898. (eval-after-load "bookmark"
  16899. '(if (boundp 'bookmark-after-jump-hook)
  16900. ;; We can use the hook
  16901. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  16902. ;; Hook not available, use advice
  16903. (defadvice bookmark-jump (after org-make-visible activate)
  16904. "Make the position visible."
  16905. (org-bookmark-jump-unhide))))
  16906. ;; Make sure saveplace shows the location if it was hidden
  16907. (eval-after-load "saveplace"
  16908. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  16909. "Make the position visible."
  16910. (org-bookmark-jump-unhide)))
  16911. ;; Make sure ecb shows the location if it was hidden
  16912. (eval-after-load "ecb"
  16913. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  16914. "Make hierarchy visible when jumping into location from ECB tree buffer."
  16915. (if (eq major-mode 'org-mode)
  16916. (org-show-context))))
  16917. (defun org-bookmark-jump-unhide ()
  16918. "Unhide the current position, to show the bookmark location."
  16919. (and (org-mode-p)
  16920. (or (org-invisible-p)
  16921. (save-excursion (goto-char (max (point-min) (1- (point))))
  16922. (org-invisible-p)))
  16923. (org-show-context 'bookmark-jump)))
  16924. ;; Make session.el ignore our circular variable
  16925. (eval-after-load "session"
  16926. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  16927. ;;;; Experimental code
  16928. (defun org-closed-in-range ()
  16929. "Sparse tree of items closed in a certain time range.
  16930. Still experimental, may disappear in the future."
  16931. (interactive)
  16932. ;; Get the time interval from the user.
  16933. (let* ((time1 (org-float-time
  16934. (org-read-date nil 'to-time nil "Starting date: ")))
  16935. (time2 (org-float-time
  16936. (org-read-date nil 'to-time nil "End date:")))
  16937. ;; callback function
  16938. (callback (lambda ()
  16939. (let ((time
  16940. (org-float-time
  16941. (apply 'encode-time
  16942. (org-parse-time-string
  16943. (match-string 1))))))
  16944. ;; check if time in interval
  16945. (and (>= time time1) (<= time time2))))))
  16946. ;; make tree, check each match with the callback
  16947. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  16948. ;;;; Finish up
  16949. (provide 'org)
  16950. (run-hooks 'org-load-hook)
  16951. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  16952. ;;; org.el ends here