org.el 763 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345
  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, 2011
  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: 7.5
  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. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (when (fboundp 'defvaralias)
  74. (unless (boundp 'calendar-view-holidays-initially-flag)
  75. (defvaralias 'calendar-view-holidays-initially-flag
  76. 'view-calendar-holidays-initially))
  77. (unless (boundp 'calendar-view-diary-initially-flag)
  78. (defvaralias 'calendar-view-diary-initially-flag
  79. 'view-diary-entries-initially))
  80. (unless (boundp 'diary-fancy-buffer)
  81. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  82. (require 'outline) (require 'noutline)
  83. ;; Other stuff we need.
  84. (require 'time-date)
  85. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  86. (require 'easymenu)
  87. (require 'overlay)
  88. (require 'org-macs)
  89. (require 'org-entities)
  90. (require 'org-compat)
  91. (require 'org-faces)
  92. (require 'org-list)
  93. (require 'org-pcomplete)
  94. (require 'org-src)
  95. (require 'org-footnote)
  96. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  97. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  98. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  99. ;; babel
  100. (require 'ob)
  101. (require 'ob-table)
  102. (require 'ob-lob)
  103. (require 'ob-ref)
  104. (require 'ob-tangle)
  105. (require 'ob-comint)
  106. (require 'ob-keys)
  107. ;; load languages based on value of `org-babel-load-languages'
  108. (defvar org-babel-load-languages)
  109. ;;;###autoload
  110. (defun org-babel-do-load-languages (sym value)
  111. "Load the languages defined in `org-babel-load-languages'."
  112. (set-default sym value)
  113. (mapc (lambda (pair)
  114. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  115. (if active
  116. (progn
  117. (require (intern (concat "ob-" lang))))
  118. (progn
  119. (funcall 'fmakunbound
  120. (intern (concat "org-babel-execute:" lang)))
  121. (funcall 'fmakunbound
  122. (intern (concat "org-babel-expand-body:" lang)))))))
  123. org-babel-load-languages))
  124. (defcustom org-babel-load-languages '((emacs-lisp . t))
  125. "Languages which can be evaluated in Org-mode buffers.
  126. This list can be used to load support for any of the languages
  127. below, note that each language will depend on a different set of
  128. system executables and/or Emacs modes. When a language is
  129. \"loaded\", then code blocks in that language can be evaluated
  130. with `org-babel-execute-src-block' bound by default to C-c
  131. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  132. be set to remove code block evaluation from the C-c C-c
  133. keybinding. By default only Emacs Lisp (which has no
  134. requirements) is loaded."
  135. :group 'org-babel
  136. :set 'org-babel-do-load-languages
  137. :type '(alist :tag "Babel Languages"
  138. :key-type
  139. (choice
  140. (const :tag "Awk" awk)
  141. (const :tag "C" C)
  142. (const :tag "R" R)
  143. (const :tag "Asymptote" asymptote)
  144. (const :tag "Calc" calc)
  145. (const :tag "Clojure" clojure)
  146. (const :tag "CSS" css)
  147. (const :tag "Ditaa" ditaa)
  148. (const :tag "Dot" dot)
  149. (const :tag "Emacs Lisp" emacs-lisp)
  150. (const :tag "Gnuplot" gnuplot)
  151. (const :tag "Haskell" haskell)
  152. (const :tag "Javascript" js)
  153. (const :tag "Latex" latex)
  154. (const :tag "Ledger" ledger)
  155. (const :tag "Lilypond" lilypond)
  156. (const :tag "Maxima" maxima)
  157. (const :tag "Matlab" matlab)
  158. (const :tag "Mscgen" mscgen)
  159. (const :tag "Ocaml" ocaml)
  160. (const :tag "Octave" octave)
  161. (const :tag "Org" org)
  162. (const :tag "Perl" perl)
  163. (const :tag "PlantUML" plantuml)
  164. (const :tag "Python" python)
  165. (const :tag "Ruby" ruby)
  166. (const :tag "Sass" sass)
  167. (const :tag "Scheme" scheme)
  168. (const :tag "Screen" screen)
  169. (const :tag "Shell Script" sh)
  170. (const :tag "Sql" sql)
  171. (const :tag "Sqlite" sqlite))
  172. :value-type (boolean :tag "Activate" :value t)))
  173. ;;;; Customization variables
  174. (defcustom org-clone-delete-id nil
  175. "Remove ID property of clones of a subtree.
  176. When non-nil, clones of a subtree don't inherit the ID property.
  177. Otherwise they inherit the ID property with a new unique
  178. identifier."
  179. :type 'boolean
  180. :group 'org-id)
  181. ;;; Version
  182. (defconst org-version "7.5"
  183. "The version number of the file org.el.")
  184. (defun org-version (&optional here)
  185. "Show the org-mode version in the echo area.
  186. With prefix arg HERE, insert it at point."
  187. (interactive "P")
  188. (let* ((origin default-directory)
  189. (version org-version)
  190. (git-version)
  191. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  192. (when (and (file-exists-p (expand-file-name ".git" dir))
  193. (executable-find "git"))
  194. (unwind-protect
  195. (progn
  196. (cd dir)
  197. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  198. (with-current-buffer "*Shell Command Output*"
  199. (goto-char (point-min))
  200. (setq git-version (buffer-substring (point) (point-at-eol))))
  201. (subst-char-in-string ?- ?. git-version t)
  202. (when (string-match "\\S-"
  203. (shell-command-to-string
  204. "git diff-index --name-only HEAD --"))
  205. (setq git-version (concat git-version ".dirty")))
  206. (setq version (concat version " (" git-version ")"))))
  207. (cd origin)))
  208. (setq version (format "Org-mode version %s" version))
  209. (if here (insert version))
  210. (message version)))
  211. ;;; Compatibility constants
  212. ;;; The custom variables
  213. (defgroup org nil
  214. "Outline-based notes management and organizer."
  215. :tag "Org"
  216. :group 'outlines
  217. :group 'calendar)
  218. (defcustom org-mode-hook nil
  219. "Mode hook for Org-mode, run after the mode was turned on."
  220. :group 'org
  221. :type 'hook)
  222. (defcustom org-load-hook nil
  223. "Hook that is run after org.el has been loaded."
  224. :group 'org
  225. :type 'hook)
  226. (defvar org-modules) ; defined below
  227. (defvar org-modules-loaded nil
  228. "Have the modules been loaded already?")
  229. (defun org-load-modules-maybe (&optional force)
  230. "Load all extensions listed in `org-modules'."
  231. (when (or force (not org-modules-loaded))
  232. (mapc (lambda (ext)
  233. (condition-case nil (require ext)
  234. (error (message "Problems while trying to load feature `%s'" ext))))
  235. org-modules)
  236. (setq org-modules-loaded t)))
  237. (defun org-set-modules (var value)
  238. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  239. (set var value)
  240. (when (featurep 'org)
  241. (org-load-modules-maybe 'force)))
  242. (when (org-bound-and-true-p org-modules)
  243. (let ((a (member 'org-infojs org-modules)))
  244. (and a (setcar a 'org-jsinfo))))
  245. (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)
  246. "Modules that should always be loaded together with org.el.
  247. If a description starts with <C>, the file is not part of Emacs
  248. and loading it will require that you have downloaded and properly installed
  249. the org-mode distribution.
  250. You can also use this system to load external packages (i.e. neither Org
  251. core modules, nor modules from the CONTRIB directory). Just add symbols
  252. to the end of the list. If the package is called org-xyz.el, then you need
  253. to add the symbol `xyz', and the package must have a call to
  254. (provide 'org-xyz)"
  255. :group 'org
  256. :set 'org-set-modules
  257. :type
  258. '(set :greedy t
  259. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  260. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  261. (const :tag " crypt: Encryption of subtrees" org-crypt)
  262. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  263. (const :tag " docview: Links to doc-view buffers" org-docview)
  264. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  265. (const :tag " id: Global IDs for identifying entries" org-id)
  266. (const :tag " info: Links to Info nodes" org-info)
  267. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  268. (const :tag " habit: Track your consistency with habits" org-habit)
  269. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  270. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  271. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  272. (const :tag " mew Links to Mew folders/messages" org-mew)
  273. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  274. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  275. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  276. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  277. (const :tag " vm: Links to VM folders/messages" org-vm)
  278. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  279. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  280. (const :tag " mouse: Additional mouse support" org-mouse)
  281. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  282. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  283. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  284. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  285. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  286. (const :tag "C collector: Collect properties into tables" org-collector)
  287. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  288. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  289. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  290. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  291. (const :tag "C eval: Include command output as text" org-eval)
  292. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  293. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  294. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  295. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  296. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  297. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  298. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  299. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  300. (const :tag "C lparse: Library for building custom exporters" org-lparse)
  301. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  302. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  303. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  304. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  305. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  306. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  307. (const :tag "C odt: OpenDocumentText exporter for Org-mode" org-odt)
  308. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  309. (const :tag "C registry: A registry for Org-mode links" org-registry)
  310. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  311. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  312. (const :tag "C secretary: Team management with org-mode" org-secretary)
  313. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  314. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  315. (const :tag "C track: Keep up with Org-mode development" org-track)
  316. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  317. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  318. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  319. (defcustom org-support-shift-select nil
  320. "Non-nil means make shift-cursor commands select text when possible.
  321. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  322. selecting a region, or enlarge regions started in this way.
  323. In Org-mode, in special contexts, these same keys are used for other
  324. purposes, important enough to compete with shift selection. Org tries
  325. to balance these needs by supporting `shift-select-mode' outside these
  326. special contexts, under control of this variable.
  327. The default of this variable is nil, to avoid confusing behavior. Shifted
  328. cursor keys will then execute Org commands in the following contexts:
  329. - on a headline, changing TODO state (left/right) and priority (up/down)
  330. - on a time stamp, changing the time
  331. - in a plain list item, changing the bullet type
  332. - in a property definition line, switching between allowed values
  333. - in the BEGIN line of a clock table (changing the time block).
  334. Outside these contexts, the commands will throw an error.
  335. When this variable is t and the cursor is not in a special context,
  336. Org-mode will support shift-selection for making and enlarging regions.
  337. To make this more effective, the bullet cycling will no longer happen
  338. anywhere in an item line, but only if the cursor is exactly on the bullet.
  339. If you set this variable to the symbol `always', then the keys
  340. will not be special in headlines, property lines, and item lines, to make
  341. shift selection work there as well. If this is what you want, you can
  342. use the following alternative commands: `C-c C-t' and `C-c ,' to
  343. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  344. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  345. edited by hand or in column view.
  346. However, when the cursor is on a timestamp, shift-cursor commands
  347. will still edit the time stamp - this is just too good to give up.
  348. XEmacs user should have this variable set to nil, because shift-select-mode
  349. is Emacs 23 only."
  350. :group 'org
  351. :type '(choice
  352. (const :tag "Never" nil)
  353. (const :tag "When outside special context" t)
  354. (const :tag "Everywhere except timestamps" always)))
  355. (defgroup org-startup nil
  356. "Options concerning startup of Org-mode."
  357. :tag "Org Startup"
  358. :group 'org)
  359. (defcustom org-startup-folded t
  360. "Non-nil means entering Org-mode will switch to OVERVIEW.
  361. This can also be configured on a per-file basis by adding one of
  362. the following lines anywhere in the buffer:
  363. #+STARTUP: fold (or `overview', this is equivalent)
  364. #+STARTUP: nofold (or `showall', this is equivalent)
  365. #+STARTUP: content
  366. #+STARTUP: showeverything"
  367. :group 'org-startup
  368. :type '(choice
  369. (const :tag "nofold: show all" nil)
  370. (const :tag "fold: overview" t)
  371. (const :tag "content: all headlines" content)
  372. (const :tag "show everything, even drawers" showeverything)))
  373. (defcustom org-startup-truncated t
  374. "Non-nil means entering Org-mode will set `truncate-lines'.
  375. This is useful since some lines containing links can be very long and
  376. uninteresting. Also tables look terrible when wrapped."
  377. :group 'org-startup
  378. :type 'boolean)
  379. (defcustom org-startup-indented nil
  380. "Non-nil means turn on `org-indent-mode' on startup.
  381. This can also be configured on a per-file basis by adding one of
  382. the following lines anywhere in the buffer:
  383. #+STARTUP: indent
  384. #+STARTUP: noindent"
  385. :group 'org-structure
  386. :type '(choice
  387. (const :tag "Not" nil)
  388. (const :tag "Globally (slow on startup in large files)" t)))
  389. (defcustom org-use-sub-superscripts t
  390. "Non-nil means interpret \"_\" and \"^\" for export.
  391. When this option is turned on, you can use TeX-like syntax for sub- and
  392. superscripts. Several characters after \"_\" or \"^\" will be
  393. considered as a single item - so grouping with {} is normally not
  394. needed. For example, the following things will be parsed as single
  395. sub- or superscripts.
  396. 10^24 or 10^tau several digits will be considered 1 item.
  397. 10^-12 or 10^-tau a leading sign with digits or a word
  398. x^2-y^3 will be read as x^2 - y^3, because items are
  399. terminated by almost any nonword/nondigit char.
  400. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  401. Still, ambiguity is possible - so when in doubt use {} to enclose the
  402. sub/superscript. If you set this variable to the symbol `{}',
  403. the braces are *required* in order to trigger interpretations as
  404. sub/superscript. This can be helpful in documents that need \"_\"
  405. frequently in plain text.
  406. Not all export backends support this, but HTML does.
  407. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  408. :group 'org-startup
  409. :group 'org-export-translation
  410. :type '(choice
  411. (const :tag "Always interpret" t)
  412. (const :tag "Only with braces" {})
  413. (const :tag "Never interpret" nil)))
  414. (if (fboundp 'defvaralias)
  415. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  416. (defcustom org-startup-with-beamer-mode nil
  417. "Non-nil means turn on `org-beamer-mode' on startup.
  418. This can also be configured on a per-file basis by adding one of
  419. the following lines anywhere in the buffer:
  420. #+STARTUP: beamer"
  421. :group 'org-startup
  422. :type 'boolean)
  423. (defcustom org-startup-align-all-tables nil
  424. "Non-nil means align all tables when visiting a file.
  425. This is useful when the column width in tables is forced with <N> cookies
  426. in table fields. Such tables will look correct only after the first re-align.
  427. This can also be configured on a per-file basis by adding one of
  428. the following lines anywhere in the buffer:
  429. #+STARTUP: align
  430. #+STARTUP: noalign"
  431. :group 'org-startup
  432. :type 'boolean)
  433. (defcustom org-startup-with-inline-images nil
  434. "Non-nil means show inline images when loading a new Org file.
  435. This can also be configured on a per-file basis by adding one of
  436. the following lines anywhere in the buffer:
  437. #+STARTUP: inlineimages
  438. #+STARTUP: noinlineimages"
  439. :group 'org-startup
  440. :type 'boolean)
  441. (defcustom org-insert-mode-line-in-empty-file nil
  442. "Non-nil means insert the first line setting Org-mode in empty files.
  443. When the function `org-mode' is called interactively in an empty file, this
  444. normally means that the file name does not automatically trigger Org-mode.
  445. To ensure that the file will always be in Org-mode in the future, a
  446. line enforcing Org-mode will be inserted into the buffer, if this option
  447. has been set."
  448. :group 'org-startup
  449. :type 'boolean)
  450. (defcustom org-replace-disputed-keys nil
  451. "Non-nil means use alternative key bindings for some keys.
  452. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  453. These keys are also used by other packages like shift-selection-mode'
  454. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  455. If you want to use Org-mode together with one of these other modes,
  456. or more generally if you would like to move some Org-mode commands to
  457. other keys, set this variable and configure the keys with the variable
  458. `org-disputed-keys'.
  459. This option is only relevant at load-time of Org-mode, and must be set
  460. *before* org.el is loaded. Changing it requires a restart of Emacs to
  461. become effective."
  462. :group 'org-startup
  463. :type 'boolean)
  464. (defcustom org-use-extra-keys nil
  465. "Non-nil means use extra key sequence definitions for certain commands.
  466. This happens automatically if you run XEmacs or if `window-system'
  467. is nil. This variable lets you do the same manually. You must
  468. set it before loading org.
  469. Example: on Carbon Emacs 22 running graphically, with an external
  470. keyboard on a Powerbook, the default way of setting M-left might
  471. not work for either Alt or ESC. Setting this variable will make
  472. it work for ESC."
  473. :group 'org-startup
  474. :type 'boolean)
  475. (if (fboundp 'defvaralias)
  476. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  477. (defcustom org-disputed-keys
  478. '(([(shift up)] . [(meta p)])
  479. ([(shift down)] . [(meta n)])
  480. ([(shift left)] . [(meta -)])
  481. ([(shift right)] . [(meta +)])
  482. ([(control shift right)] . [(meta shift +)])
  483. ([(control shift left)] . [(meta shift -)]))
  484. "Keys for which Org-mode and other modes compete.
  485. This is an alist, cars are the default keys, second element specifies
  486. the alternative to use when `org-replace-disputed-keys' is t.
  487. Keys can be specified in any syntax supported by `define-key'.
  488. The value of this option takes effect only at Org-mode's startup,
  489. therefore you'll have to restart Emacs to apply it after changing."
  490. :group 'org-startup
  491. :type 'alist)
  492. (defun org-key (key)
  493. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  494. Or return the original if not disputed.
  495. Also apply the translations defined in `org-xemacs-key-equivalents'."
  496. (when org-replace-disputed-keys
  497. (let* ((nkey (key-description key))
  498. (x (org-find-if (lambda (x)
  499. (equal (key-description (car x)) nkey))
  500. org-disputed-keys)))
  501. (setq key (if x (cdr x) key))))
  502. (when (featurep 'xemacs)
  503. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  504. key)
  505. (defun org-find-if (predicate seq)
  506. (catch 'exit
  507. (while seq
  508. (if (funcall predicate (car seq))
  509. (throw 'exit (car seq))
  510. (pop seq)))))
  511. (defun org-defkey (keymap key def)
  512. "Define a key, possibly translated, as returned by `org-key'."
  513. (define-key keymap (org-key key) def))
  514. (defcustom org-ellipsis nil
  515. "The ellipsis to use in the Org-mode outline.
  516. When nil, just use the standard three dots. When a string, use that instead,
  517. When a face, use the standard 3 dots, but with the specified face.
  518. The change affects only Org-mode (which will then use its own display table).
  519. Changing this requires executing `M-x org-mode' in a buffer to become
  520. effective."
  521. :group 'org-startup
  522. :type '(choice (const :tag "Default" nil)
  523. (face :tag "Face" :value org-warning)
  524. (string :tag "String" :value "...#")))
  525. (defvar org-display-table nil
  526. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  527. (defgroup org-keywords nil
  528. "Keywords in Org-mode."
  529. :tag "Org Keywords"
  530. :group 'org)
  531. (defcustom org-deadline-string "DEADLINE:"
  532. "String to mark deadline entries.
  533. A deadline is this string, followed by a time stamp. Should be a word,
  534. terminated by a colon. You can insert a schedule keyword and
  535. a timestamp with \\[org-deadline].
  536. Changes become only effective after restarting Emacs."
  537. :group 'org-keywords
  538. :type 'string)
  539. (defcustom org-scheduled-string "SCHEDULED:"
  540. "String to mark scheduled TODO entries.
  541. A schedule is this string, followed by a time stamp. Should be a word,
  542. terminated by a colon. You can insert a schedule keyword and
  543. a timestamp with \\[org-schedule].
  544. Changes become only effective after restarting Emacs."
  545. :group 'org-keywords
  546. :type 'string)
  547. (defcustom org-closed-string "CLOSED:"
  548. "String used as the prefix for timestamps logging closing a TODO entry."
  549. :group 'org-keywords
  550. :type 'string)
  551. (defcustom org-clock-string "CLOCK:"
  552. "String used as prefix for timestamps clocking work hours on an item."
  553. :group 'org-keywords
  554. :type 'string)
  555. (defcustom org-comment-string "COMMENT"
  556. "Entries starting with this keyword will never be exported.
  557. An entry can be toggled between COMMENT and normal with
  558. \\[org-toggle-comment].
  559. Changes become only effective after restarting Emacs."
  560. :group 'org-keywords
  561. :type 'string)
  562. (defcustom org-quote-string "QUOTE"
  563. "Entries starting with this keyword will be exported in fixed-width font.
  564. Quoting applies only to the text in the entry following the headline, and does
  565. not extend beyond the next headline, even if that is lower level.
  566. An entry can be toggled between QUOTE and normal with
  567. \\[org-toggle-fixed-width-section]."
  568. :group 'org-keywords
  569. :type 'string)
  570. (defconst org-repeat-re
  571. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  572. "Regular expression for specifying repeated events.
  573. After a match, group 1 contains the repeat expression.")
  574. (defgroup org-structure nil
  575. "Options concerning the general structure of Org-mode files."
  576. :tag "Org Structure"
  577. :group 'org)
  578. (defgroup org-reveal-location nil
  579. "Options about how to make context of a location visible."
  580. :tag "Org Reveal Location"
  581. :group 'org-structure)
  582. (defconst org-context-choice
  583. '(choice
  584. (const :tag "Always" t)
  585. (const :tag "Never" nil)
  586. (repeat :greedy t :tag "Individual contexts"
  587. (cons
  588. (choice :tag "Context"
  589. (const agenda)
  590. (const org-goto)
  591. (const occur-tree)
  592. (const tags-tree)
  593. (const link-search)
  594. (const mark-goto)
  595. (const bookmark-jump)
  596. (const isearch)
  597. (const default))
  598. (boolean))))
  599. "Contexts for the reveal options.")
  600. (defcustom org-show-hierarchy-above '((default . t))
  601. "Non-nil means show full hierarchy when revealing a location.
  602. Org-mode often shows locations in an org-mode file which might have
  603. been invisible before. When this is set, the hierarchy of headings
  604. above the exposed location is shown.
  605. Turning this off for example for sparse trees makes them very compact.
  606. Instead of t, this can also be an alist specifying this option for different
  607. contexts. Valid contexts are
  608. agenda when exposing an entry from the agenda
  609. org-goto when using the command `org-goto' on key C-c C-j
  610. occur-tree when using the command `org-occur' on key C-c /
  611. tags-tree when constructing a sparse tree based on tags matches
  612. link-search when exposing search matches associated with a link
  613. mark-goto when exposing the jump goal of a mark
  614. bookmark-jump when exposing a bookmark location
  615. isearch when exiting from an incremental search
  616. default default for all contexts not set explicitly"
  617. :group 'org-reveal-location
  618. :type org-context-choice)
  619. (defcustom org-show-following-heading '((default . nil))
  620. "Non-nil means show following heading when revealing a location.
  621. Org-mode often shows locations in an org-mode file which might have
  622. been invisible before. When this is set, the heading following the
  623. match is shown.
  624. Turning this off for example for sparse trees makes them very compact,
  625. but makes it harder to edit the location of the match. In such a case,
  626. use the command \\[org-reveal] to show more context.
  627. Instead of t, this can also be an alist specifying this option for different
  628. contexts. See `org-show-hierarchy-above' for valid contexts."
  629. :group 'org-reveal-location
  630. :type org-context-choice)
  631. (defcustom org-show-siblings '((default . nil) (isearch t))
  632. "Non-nil means show all sibling heading when revealing a location.
  633. Org-mode often shows locations in an org-mode file which might have
  634. been invisible before. When this is set, the sibling of the current entry
  635. heading are all made visible. If `org-show-hierarchy-above' is t,
  636. the same happens on each level of the hierarchy above the current entry.
  637. By default this is on for the isearch context, off for all other contexts.
  638. Turning this off for example for sparse trees makes them very compact,
  639. but makes it harder to edit the location of the match. In such a case,
  640. use the command \\[org-reveal] to show more context.
  641. Instead of t, this can also be an alist specifying this option for different
  642. contexts. See `org-show-hierarchy-above' for valid contexts."
  643. :group 'org-reveal-location
  644. :type org-context-choice)
  645. (defcustom org-show-entry-below '((default . nil))
  646. "Non-nil means show the entry below a headline when revealing a location.
  647. Org-mode often shows locations in an org-mode file which might have
  648. been invisible before. When this is set, the text below the headline that is
  649. exposed is also shown.
  650. By default this is off for all contexts.
  651. Instead of t, this can also be an alist specifying this option for different
  652. contexts. See `org-show-hierarchy-above' for valid contexts."
  653. :group 'org-reveal-location
  654. :type org-context-choice)
  655. (defcustom org-indirect-buffer-display 'other-window
  656. "How should indirect tree buffers be displayed?
  657. This applies to indirect buffers created with the commands
  658. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  659. Valid values are:
  660. current-window Display in the current window
  661. other-window Just display in another window.
  662. dedicated-frame Create one new frame, and re-use it each time.
  663. new-frame Make a new frame each time. Note that in this case
  664. previously-made indirect buffers are kept, and you need to
  665. kill these buffers yourself."
  666. :group 'org-structure
  667. :group 'org-agenda-windows
  668. :type '(choice
  669. (const :tag "In current window" current-window)
  670. (const :tag "In current frame, other window" other-window)
  671. (const :tag "Each time a new frame" new-frame)
  672. (const :tag "One dedicated frame" dedicated-frame)))
  673. (defcustom org-use-speed-commands nil
  674. "Non-nil means activate single letter commands at beginning of a headline.
  675. This may also be a function to test for appropriate locations where speed
  676. commands should be active."
  677. :group 'org-structure
  678. :type '(choice
  679. (const :tag "Never" nil)
  680. (const :tag "At beginning of headline stars" t)
  681. (function)))
  682. (defcustom org-speed-commands-user nil
  683. "Alist of additional speed commands.
  684. This list will be checked before `org-speed-commands-default'
  685. when the variable `org-use-speed-commands' is non-nil
  686. and when the cursor is at the beginning of a headline.
  687. The car if each entry is a string with a single letter, which must
  688. be assigned to `self-insert-command' in the global map.
  689. The cdr is either a command to be called interactively, a function
  690. to be called, or a form to be evaluated.
  691. An entry that is just a list with a single string will be interpreted
  692. as a descriptive headline that will be added when listing the speed
  693. commands in the Help buffer using the `?' speed command."
  694. :group 'org-structure
  695. :type '(repeat :value ("k" . ignore)
  696. (choice :value ("k" . ignore)
  697. (list :tag "Descriptive Headline" (string :tag "Headline"))
  698. (cons :tag "Letter and Command"
  699. (string :tag "Command letter")
  700. (choice
  701. (function)
  702. (sexp))))))
  703. (defgroup org-cycle nil
  704. "Options concerning visibility cycling in Org-mode."
  705. :tag "Org Cycle"
  706. :group 'org-structure)
  707. (defcustom org-cycle-skip-children-state-if-no-children t
  708. "Non-nil means skip CHILDREN state in entries that don't have any."
  709. :group 'org-cycle
  710. :type 'boolean)
  711. (defcustom org-cycle-max-level nil
  712. "Maximum level which should still be subject to visibility cycling.
  713. Levels higher than this will, for cycling, be treated as text, not a headline.
  714. When `org-odd-levels-only' is set, a value of N in this variable actually
  715. means 2N-1 stars as the limiting headline.
  716. When nil, cycle all levels.
  717. Note that the limiting level of cycling is also influenced by
  718. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  719. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  720. than its value."
  721. :group 'org-cycle
  722. :type '(choice
  723. (const :tag "No limit" nil)
  724. (integer :tag "Maximum level")))
  725. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  726. "Names of drawers. Drawers are not opened by cycling on the headline above.
  727. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  728. this:
  729. :DRAWERNAME:
  730. .....
  731. :END:
  732. The drawer \"PROPERTIES\" is special for capturing properties through
  733. the property API.
  734. Drawers can be defined on the per-file basis with a line like:
  735. #+DRAWERS: HIDDEN STATE PROPERTIES"
  736. :group 'org-structure
  737. :group 'org-cycle
  738. :type '(repeat (string :tag "Drawer Name")))
  739. (defcustom org-hide-block-startup nil
  740. "Non-nil means entering Org-mode will fold all blocks.
  741. This can also be set in on a per-file basis with
  742. #+STARTUP: hideblocks
  743. #+STARTUP: showblocks"
  744. :group 'org-startup
  745. :group 'org-cycle
  746. :type 'boolean)
  747. (defcustom org-cycle-global-at-bob nil
  748. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  749. This makes it possible to do global cycling without having to use S-TAB or
  750. \\[universal-argument] TAB. For this special case to work, the first line \
  751. of the buffer
  752. must not be a headline - it may be empty or some other text. When used in
  753. this way, `org-cycle-hook' is disables temporarily, to make sure the
  754. cursor stays at the beginning of the buffer.
  755. When this option is nil, don't do anything special at the beginning
  756. of the buffer."
  757. :group 'org-cycle
  758. :type 'boolean)
  759. (defcustom org-cycle-level-after-item/entry-creation t
  760. "Non-nil means cycle entry level or item indentation in new empty entries.
  761. When the cursor is at the end of an empty headline, i.e with only stars
  762. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  763. and then all possible ancestor states, before returning to the original state.
  764. This makes data entry extremely fast: M-RET to create a new headline,
  765. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  766. When the cursor is at the end of an empty plain list item, one TAB will
  767. make it a subitem, two or more tabs will back up to make this an item
  768. higher up in the item hierarchy."
  769. :group 'org-cycle
  770. :type 'boolean)
  771. (defcustom org-cycle-emulate-tab t
  772. "Where should `org-cycle' emulate TAB.
  773. nil Never
  774. white Only in completely white lines
  775. whitestart Only at the beginning of lines, before the first non-white char
  776. t Everywhere except in headlines
  777. exc-hl-bol Everywhere except at the start of a headline
  778. If TAB is used in a place where it does not emulate TAB, the current subtree
  779. visibility is cycled."
  780. :group 'org-cycle
  781. :type '(choice (const :tag "Never" nil)
  782. (const :tag "Only in completely white lines" white)
  783. (const :tag "Before first char in a line" whitestart)
  784. (const :tag "Everywhere except in headlines" t)
  785. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  786. ))
  787. (defcustom org-cycle-separator-lines 2
  788. "Number of empty lines needed to keep an empty line between collapsed trees.
  789. If you leave an empty line between the end of a subtree and the following
  790. headline, this empty line is hidden when the subtree is folded.
  791. Org-mode will leave (exactly) one empty line visible if the number of
  792. empty lines is equal or larger to the number given in this variable.
  793. So the default 2 means at least 2 empty lines after the end of a subtree
  794. are needed to produce free space between a collapsed subtree and the
  795. following headline.
  796. If the number is negative, and the number of empty lines is at least -N,
  797. all empty lines are shown.
  798. Special case: when 0, never leave empty lines in collapsed view."
  799. :group 'org-cycle
  800. :type 'integer)
  801. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  802. (defcustom org-pre-cycle-hook nil
  803. "Hook that is run before visibility cycling is happening.
  804. The function(s) in this hook must accept a single argument which indicates
  805. the new state that will be set right after running this hook. The
  806. argument is a symbol. Before a global state change, it can have the values
  807. `overview', `content', or `all'. Before a local state change, it can have
  808. the values `folded', `children', or `subtree'."
  809. :group 'org-cycle
  810. :type 'hook)
  811. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  812. org-cycle-hide-drawers
  813. org-cycle-show-empty-lines
  814. org-optimize-window-after-visibility-change)
  815. "Hook that is run after `org-cycle' has changed the buffer visibility.
  816. The function(s) in this hook must accept a single argument which indicates
  817. the new state that was set by the most recent `org-cycle' command. The
  818. argument is a symbol. After a global state change, it can have the values
  819. `overview', `content', or `all'. After a local state change, it can have
  820. the values `folded', `children', or `subtree'."
  821. :group 'org-cycle
  822. :type 'hook)
  823. (defgroup org-edit-structure nil
  824. "Options concerning structure editing in Org-mode."
  825. :tag "Org Edit Structure"
  826. :group 'org-structure)
  827. (defcustom org-odd-levels-only nil
  828. "Non-nil means skip even levels and only use odd levels for the outline.
  829. This has the effect that two stars are being added/taken away in
  830. promotion/demotion commands. It also influences how levels are
  831. handled by the exporters.
  832. Changing it requires restart of `font-lock-mode' to become effective
  833. for fontification also in regions already fontified.
  834. You may also set this on a per-file basis by adding one of the following
  835. lines to the buffer:
  836. #+STARTUP: odd
  837. #+STARTUP: oddeven"
  838. :group 'org-edit-structure
  839. :group 'org-appearance
  840. :type 'boolean)
  841. (defcustom org-adapt-indentation t
  842. "Non-nil means adapt indentation to outline node level.
  843. When this variable is set, Org assumes that you write outlines by
  844. indenting text in each node to align with the headline (after the stars).
  845. The following issues are influenced by this variable:
  846. - When this is set and the *entire* text in an entry is indented, the
  847. indentation is increased by one space in a demotion command, and
  848. decreased by one in a promotion command. If any line in the entry
  849. body starts with text at column 0, indentation is not changed at all.
  850. - Property drawers and planning information is inserted indented when
  851. this variable s set. When nil, they will not be indented.
  852. - TAB indents a line relative to context. The lines below a headline
  853. will be indented when this variable is set.
  854. Note that this is all about true indentation, by adding and removing
  855. space characters. See also `org-indent.el' which does level-dependent
  856. indentation in a virtual way, i.e. at display time in Emacs."
  857. :group 'org-edit-structure
  858. :type 'boolean)
  859. (defcustom org-special-ctrl-a/e nil
  860. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  861. When t, `C-a' will bring back the cursor to the beginning of the
  862. headline text, i.e. after the stars and after a possible TODO keyword.
  863. In an item, this will be the position after the bullet.
  864. When the cursor is already at that position, another `C-a' will bring
  865. it to the beginning of the line.
  866. `C-e' will jump to the end of the headline, ignoring the presence of tags
  867. in the headline. A second `C-e' will then jump to the true end of the
  868. line, after any tags. This also means that, when this variable is
  869. non-nil, `C-e' also will never jump beyond the end of the heading of a
  870. folded section, i.e. not after the ellipses.
  871. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  872. going to the true line boundary first. Only a directly following, identical
  873. keypress will bring the cursor to the special positions.
  874. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  875. set separately."
  876. :group 'org-edit-structure
  877. :type '(choice
  878. (const :tag "off" nil)
  879. (const :tag "on: after stars/bullet and before tags first" t)
  880. (const :tag "reversed: true line boundary first" reversed)
  881. (cons :tag "Set C-a and C-e separately"
  882. (choice :tag "Special C-a"
  883. (const :tag "off" nil)
  884. (const :tag "on: after stars/bullet first" t)
  885. (const :tag "reversed: before stars/bullet first" reversed))
  886. (choice :tag "Special C-e"
  887. (const :tag "off" nil)
  888. (const :tag "on: before tags first" t)
  889. (const :tag "reversed: after tags first" reversed)))))
  890. (if (fboundp 'defvaralias)
  891. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  892. (defcustom org-special-ctrl-k nil
  893. "Non-nil means `C-k' will behave specially in headlines.
  894. When nil, `C-k' will call the default `kill-line' command.
  895. When t, the following will happen while the cursor is in the headline:
  896. - When the cursor is at the beginning of a headline, kill the entire
  897. line and possible the folded subtree below the line.
  898. - When in the middle of the headline text, kill the headline up to the tags.
  899. - When after the headline text, kill the tags."
  900. :group 'org-edit-structure
  901. :type 'boolean)
  902. (defcustom org-ctrl-k-protect-subtree nil
  903. "Non-nil means, do not delete a hidden subtree with C-k.
  904. When set to the symbol `error', simply throw an error when C-k is
  905. used to kill (part-of) a headline that has hidden text behind it.
  906. Any other non-nil value will result in a query to the user, if it is
  907. OK to kill that hidden subtree. When nil, kill without remorse."
  908. :group 'org-edit-structure
  909. :type '(choice
  910. (const :tag "Do not protect hidden subtrees" nil)
  911. (const :tag "Protect hidden subtrees with a security query" t)
  912. (const :tag "Never kill a hidden subtree with C-k" error)))
  913. (defcustom org-yank-folded-subtrees t
  914. "Non-nil means when yanking subtrees, fold them.
  915. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  916. it starts with a heading and all other headings in it are either children
  917. or siblings, then fold all the subtrees. However, do this only if no
  918. text after the yank would be swallowed into a folded tree by this action."
  919. :group 'org-edit-structure
  920. :type 'boolean)
  921. (defcustom org-yank-adjusted-subtrees nil
  922. "Non-nil means when yanking subtrees, adjust the level.
  923. With this setting, `org-paste-subtree' is used to insert the subtree, see
  924. this function for details."
  925. :group 'org-edit-structure
  926. :type 'boolean)
  927. (defcustom org-M-RET-may-split-line '((default . t))
  928. "Non-nil means M-RET will split the line at the cursor position.
  929. When nil, it will go to the end of the line before making a
  930. new line.
  931. You may also set this option in a different way for different
  932. contexts. Valid contexts are:
  933. headline when creating a new headline
  934. item when creating a new item
  935. table in a table field
  936. default the value to be used for all contexts not explicitly
  937. customized"
  938. :group 'org-structure
  939. :group 'org-table
  940. :type '(choice
  941. (const :tag "Always" t)
  942. (const :tag "Never" nil)
  943. (repeat :greedy t :tag "Individual contexts"
  944. (cons
  945. (choice :tag "Context"
  946. (const headline)
  947. (const item)
  948. (const table)
  949. (const default))
  950. (boolean)))))
  951. (defcustom org-insert-heading-respect-content nil
  952. "Non-nil means insert new headings after the current subtree.
  953. When nil, the new heading is created directly after the current line.
  954. The commands \\[org-insert-heading-respect-content] and
  955. \\[org-insert-todo-heading-respect-content] turn this variable on
  956. for the duration of the command."
  957. :group 'org-structure
  958. :type 'boolean)
  959. (defcustom org-blank-before-new-entry '((heading . auto)
  960. (plain-list-item . auto))
  961. "Should `org-insert-heading' leave a blank line before new heading/item?
  962. The value is an alist, with `heading' and `plain-list-item' as car,
  963. and a boolean flag as cdr. The cdr may also be the symbol `auto', and then
  964. Org will look at the surrounding headings/items and try to make an
  965. intelligent decision whether to insert a blank line or not.
  966. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  967. set, the setting here is ignored and no empty line is inserted, to avoid
  968. breaking the list structure."
  969. :group 'org-edit-structure
  970. :type '(list
  971. (cons (const heading)
  972. (choice (const :tag "Never" nil)
  973. (const :tag "Always" t)
  974. (const :tag "Auto" auto)))
  975. (cons (const plain-list-item)
  976. (choice (const :tag "Never" nil)
  977. (const :tag "Always" t)
  978. (const :tag "Auto" auto)))))
  979. (defcustom org-insert-heading-hook nil
  980. "Hook being run after inserting a new heading."
  981. :group 'org-edit-structure
  982. :type 'hook)
  983. (defcustom org-enable-fixed-width-editor t
  984. "Non-nil means lines starting with \":\" are treated as fixed-width.
  985. This currently only means they are never auto-wrapped.
  986. When nil, such lines will be treated like ordinary lines.
  987. See also the QUOTE keyword."
  988. :group 'org-edit-structure
  989. :type 'boolean)
  990. (defcustom org-goto-auto-isearch t
  991. "Non-nil means typing characters in `org-goto' starts incremental search."
  992. :group 'org-edit-structure
  993. :type 'boolean)
  994. (defgroup org-sparse-trees nil
  995. "Options concerning sparse trees in Org-mode."
  996. :tag "Org Sparse Trees"
  997. :group 'org-structure)
  998. (defcustom org-highlight-sparse-tree-matches t
  999. "Non-nil means highlight all matches that define a sparse tree.
  1000. The highlights will automatically disappear the next time the buffer is
  1001. changed by an edit command."
  1002. :group 'org-sparse-trees
  1003. :type 'boolean)
  1004. (defcustom org-remove-highlights-with-change t
  1005. "Non-nil means any change to the buffer will remove temporary highlights.
  1006. Such highlights are created by `org-occur' and `org-clock-display'.
  1007. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1008. The highlights created by `org-preview-latex-fragment' always need
  1009. `C-c C-c' to be removed."
  1010. :group 'org-sparse-trees
  1011. :group 'org-time
  1012. :type 'boolean)
  1013. (defcustom org-occur-hook '(org-first-headline-recenter)
  1014. "Hook that is run after `org-occur' has constructed a sparse tree.
  1015. This can be used to recenter the window to show as much of the structure
  1016. as possible."
  1017. :group 'org-sparse-trees
  1018. :type 'hook)
  1019. (defgroup org-imenu-and-speedbar nil
  1020. "Options concerning imenu and speedbar in Org-mode."
  1021. :tag "Org Imenu and Speedbar"
  1022. :group 'org-structure)
  1023. (defcustom org-imenu-depth 2
  1024. "The maximum level for Imenu access to Org-mode headlines.
  1025. This also applied for speedbar access."
  1026. :group 'org-imenu-and-speedbar
  1027. :type 'integer)
  1028. (defgroup org-table nil
  1029. "Options concerning tables in Org-mode."
  1030. :tag "Org Table"
  1031. :group 'org)
  1032. (defcustom org-enable-table-editor 'optimized
  1033. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1034. When nil, such lines will be treated like ordinary lines.
  1035. When equal to the symbol `optimized', the table editor will be optimized to
  1036. do the following:
  1037. - Automatic overwrite mode in front of whitespace in table fields.
  1038. This makes the structure of the table stay in tact as long as the edited
  1039. field does not exceed the column width.
  1040. - Minimize the number of realigns. Normally, the table is aligned each time
  1041. TAB or RET are pressed to move to another field. With optimization this
  1042. happens only if changes to a field might have changed the column width.
  1043. Optimization requires replacing the functions `self-insert-command',
  1044. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1045. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1046. very good at guessing when a re-align will be necessary, but you can always
  1047. force one with \\[org-ctrl-c-ctrl-c].
  1048. If you would like to use the optimized version in Org-mode, but the
  1049. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1050. This variable can be used to turn on and off the table editor during a session,
  1051. but in order to toggle optimization, a restart is required.
  1052. See also the variable `org-table-auto-blank-field'."
  1053. :group 'org-table
  1054. :type '(choice
  1055. (const :tag "off" nil)
  1056. (const :tag "on" t)
  1057. (const :tag "on, optimized" optimized)))
  1058. (defcustom org-self-insert-cluster-for-undo t
  1059. "Non-nil means cluster self-insert commands for undo when possible.
  1060. If this is set, then, like in the Emacs command loop, 20 consecutive
  1061. characters will be undone together.
  1062. This is configurable, because there is some impact on typing performance."
  1063. :group 'org-table
  1064. :type 'boolean)
  1065. (defcustom org-table-tab-recognizes-table.el t
  1066. "Non-nil means TAB will automatically notice a table.el table.
  1067. When it sees such a table, it moves point into it and - if necessary -
  1068. calls `table-recognize-table'."
  1069. :group 'org-table-editing
  1070. :type 'boolean)
  1071. (defgroup org-link nil
  1072. "Options concerning links in Org-mode."
  1073. :tag "Org Link"
  1074. :group 'org)
  1075. (defvar org-link-abbrev-alist-local nil
  1076. "Buffer-local version of `org-link-abbrev-alist', which see.
  1077. The value of this is taken from the #+LINK lines.")
  1078. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1079. (defcustom org-link-abbrev-alist nil
  1080. "Alist of link abbreviations.
  1081. The car of each element is a string, to be replaced at the start of a link.
  1082. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1083. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1084. [[linkkey:tag][description]]
  1085. The 'linkkey' must be a word word, starting with a letter, followed
  1086. by letters, numbers, '-' or '_'.
  1087. If REPLACE is a string, the tag will simply be appended to create the link.
  1088. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1089. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1090. be inserted at that point (see the function `url-hexify-string').
  1091. REPLACE may also be a function that will be called with the tag as the
  1092. only argument to create the link, which should be returned as a string.
  1093. See the manual for examples."
  1094. :group 'org-link
  1095. :type '(repeat
  1096. (cons
  1097. (string :tag "Protocol")
  1098. (choice
  1099. (string :tag "Format")
  1100. (function)))))
  1101. (defcustom org-descriptive-links t
  1102. "Non-nil means hide link part and only show description of bracket links.
  1103. Bracket links are like [[link][description]]. This variable sets the initial
  1104. state in new org-mode buffers. The setting can then be toggled on a
  1105. per-buffer basis from the Org->Hyperlinks menu."
  1106. :group 'org-link
  1107. :type 'boolean)
  1108. (defcustom org-link-file-path-type 'adaptive
  1109. "How the path name in file links should be stored.
  1110. Valid values are:
  1111. relative Relative to the current directory, i.e. the directory of the file
  1112. into which the link is being inserted.
  1113. absolute Absolute path, if possible with ~ for home directory.
  1114. noabbrev Absolute path, no abbreviation of home directory.
  1115. adaptive Use relative path for files in the current directory and sub-
  1116. directories of it. For other files, use an absolute path."
  1117. :group 'org-link
  1118. :type '(choice
  1119. (const relative)
  1120. (const absolute)
  1121. (const noabbrev)
  1122. (const adaptive)))
  1123. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1124. "Types of links that should be activated in Org-mode files.
  1125. This is a list of symbols, each leading to the activation of a certain link
  1126. type. In principle, it does not hurt to turn on most link types - there may
  1127. be a small gain when turning off unused link types. The types are:
  1128. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1129. angle Links in angular brackets that may contain whitespace like
  1130. <bbdb:Carsten Dominik>.
  1131. plain Plain links in normal text, no whitespace, like http://google.com.
  1132. radio Text that is matched by a radio target, see manual for details.
  1133. tag Tag settings in a headline (link to tag search).
  1134. date Time stamps (link to calendar).
  1135. footnote Footnote labels.
  1136. Changing this variable requires a restart of Emacs to become effective."
  1137. :group 'org-link
  1138. :type '(set :greedy t
  1139. (const :tag "Double bracket links" bracket)
  1140. (const :tag "Angular bracket links" angle)
  1141. (const :tag "Plain text links" plain)
  1142. (const :tag "Radio target matches" radio)
  1143. (const :tag "Tags" tag)
  1144. (const :tag "Timestamps" date)
  1145. (const :tag "Footnotes" footnote)))
  1146. (defcustom org-make-link-description-function nil
  1147. "Function to use to generate link descriptions from links.
  1148. If nil the link location will be used. This function must take
  1149. two parameters; the first is the link and the second the
  1150. description `org-insert-link' has generated, and should return the
  1151. description to use."
  1152. :group 'org-link
  1153. :type 'function)
  1154. (defgroup org-link-store nil
  1155. "Options concerning storing links in Org-mode."
  1156. :tag "Org Store Link"
  1157. :group 'org-link)
  1158. (defcustom org-email-link-description-format "Email %c: %.30s"
  1159. "Format of the description part of a link to an email or usenet message.
  1160. The following %-escapes will be replaced by corresponding information:
  1161. %F full \"From\" field
  1162. %f name, taken from \"From\" field, address if no name
  1163. %T full \"To\" field
  1164. %t first name in \"To\" field, address if no name
  1165. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1166. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1167. %s subject
  1168. %d date
  1169. %m message-id.
  1170. You may use normal field width specification between the % and the letter.
  1171. This is for example useful to limit the length of the subject.
  1172. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1173. :group 'org-link-store
  1174. :type 'string)
  1175. (defcustom org-from-is-user-regexp
  1176. (let (r1 r2)
  1177. (when (and user-mail-address (not (string= user-mail-address "")))
  1178. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1179. (when (and user-full-name (not (string= user-full-name "")))
  1180. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1181. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1182. "Regexp matched against the \"From:\" header of an email or usenet message.
  1183. It should match if the message is from the user him/herself."
  1184. :group 'org-link-store
  1185. :type 'regexp)
  1186. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1187. "Non-nil means storing a link to an Org file will use entry IDs.
  1188. Note that before this variable is even considered, org-id must be loaded,
  1189. so please customize `org-modules' and turn it on.
  1190. The variable can have the following values:
  1191. t Create an ID if needed to make a link to the current entry.
  1192. create-if-interactive
  1193. If `org-store-link' is called directly (interactively, as a user
  1194. command), do create an ID to support the link. But when doing the
  1195. job for remember, only use the ID if it already exists. The
  1196. purpose of this setting is to avoid proliferation of unwanted
  1197. IDs, just because you happen to be in an Org file when you
  1198. call `org-remember' that automatically and preemptively
  1199. creates a link. If you do want to get an ID link in a remember
  1200. template to an entry not having an ID, create it first by
  1201. explicitly creating a link to it, using `C-c C-l' first.
  1202. create-if-interactive-and-no-custom-id
  1203. Like create-if-interactive, but do not create an ID if there is
  1204. a CUSTOM_ID property defined in the entry. This is the default.
  1205. use-existing
  1206. Use existing ID, do not create one.
  1207. nil Never use an ID to make a link, instead link using a text search for
  1208. the headline text."
  1209. :group 'org-link-store
  1210. :type '(choice
  1211. (const :tag "Create ID to make link" t)
  1212. (const :tag "Create if storing link interactively"
  1213. create-if-interactive)
  1214. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1215. create-if-interactive-and-no-custom-id)
  1216. (const :tag "Only use existing" use-existing)
  1217. (const :tag "Do not use ID to create link" nil)))
  1218. (defcustom org-context-in-file-links t
  1219. "Non-nil means file links from `org-store-link' contain context.
  1220. A search string will be added to the file name with :: as separator and
  1221. used to find the context when the link is activated by the command
  1222. `org-open-at-point'. When this option is t, the entire active region
  1223. will be placed in the search string of the file link. If set to a
  1224. positive integer, only the first n lines of context will be stored.
  1225. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1226. negates this setting for the duration of the command."
  1227. :group 'org-link-store
  1228. :type '(choice boolean integer))
  1229. (defcustom org-keep-stored-link-after-insertion nil
  1230. "Non-nil means keep link in list for entire session.
  1231. The command `org-store-link' adds a link pointing to the current
  1232. location to an internal list. These links accumulate during a session.
  1233. The command `org-insert-link' can be used to insert links into any
  1234. Org-mode file (offering completion for all stored links). When this
  1235. option is nil, every link which has been inserted once using \\[org-insert-link]
  1236. will be removed from the list, to make completing the unused links
  1237. more efficient."
  1238. :group 'org-link-store
  1239. :type 'boolean)
  1240. (defgroup org-link-follow nil
  1241. "Options concerning following links in Org-mode."
  1242. :tag "Org Follow Link"
  1243. :group 'org-link)
  1244. (defcustom org-link-translation-function nil
  1245. "Function to translate links with different syntax to Org syntax.
  1246. This can be used to translate links created for example by the Planner
  1247. or emacs-wiki packages to Org syntax.
  1248. The function must accept two parameters, a TYPE containing the link
  1249. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1250. which is everything after the link protocol. It should return a cons
  1251. with possibly modified values of type and path.
  1252. Org contains a function for this, so if you set this variable to
  1253. `org-translate-link-from-planner', you should be able follow many
  1254. links created by planner."
  1255. :group 'org-link-follow
  1256. :type 'function)
  1257. (defcustom org-follow-link-hook nil
  1258. "Hook that is run after a link has been followed."
  1259. :group 'org-link-follow
  1260. :type 'hook)
  1261. (defcustom org-tab-follows-link nil
  1262. "Non-nil means on links TAB will follow the link.
  1263. Needs to be set before org.el is loaded.
  1264. This really should not be used, it does not make sense, and the
  1265. implementation is bad."
  1266. :group 'org-link-follow
  1267. :type 'boolean)
  1268. (defcustom org-return-follows-link nil
  1269. "Non-nil means on links RET will follow the link."
  1270. :group 'org-link-follow
  1271. :type 'boolean)
  1272. (defcustom org-mouse-1-follows-link
  1273. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1274. "Non-nil means mouse-1 on a link will follow the link.
  1275. A longer mouse click will still set point. Does not work on XEmacs.
  1276. Needs to be set before org.el is loaded."
  1277. :group 'org-link-follow
  1278. :type 'boolean)
  1279. (defcustom org-mark-ring-length 4
  1280. "Number of different positions to be recorded in the ring.
  1281. Changing this requires a restart of Emacs to work correctly."
  1282. :group 'org-link-follow
  1283. :type 'integer)
  1284. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1285. "Non-nil means internal links in Org files must exactly match a headline.
  1286. When nil, the link search tries to match a phrase with all words
  1287. in the search text."
  1288. :group 'org-link-follow
  1289. :type '(choice
  1290. (const :tag "Use fuzy text search" nil)
  1291. (const :tag "Match only exact headline" t)
  1292. (const :tag "Match extact headline or query to create it"
  1293. query-to-create)))
  1294. (defcustom org-link-frame-setup
  1295. '((vm . vm-visit-folder-other-frame)
  1296. (gnus . org-gnus-no-new-news)
  1297. (file . find-file-other-window)
  1298. (wl . wl-other-frame))
  1299. "Setup the frame configuration for following links.
  1300. When following a link with Emacs, it may often be useful to display
  1301. this link in another window or frame. This variable can be used to
  1302. set this up for the different types of links.
  1303. For VM, use any of
  1304. `vm-visit-folder'
  1305. `vm-visit-folder-other-window'
  1306. `vm-visit-folder-other-frame'
  1307. For Gnus, use any of
  1308. `gnus'
  1309. `gnus-other-frame'
  1310. `org-gnus-no-new-news'
  1311. For FILE, use any of
  1312. `find-file'
  1313. `find-file-other-window'
  1314. `find-file-other-frame'
  1315. For Wanderlust use any of
  1316. `wl'
  1317. `wl-other-frame'
  1318. For the calendar, use the variable `calendar-setup'.
  1319. For BBDB, it is currently only possible to display the matches in
  1320. another window."
  1321. :group 'org-link-follow
  1322. :type '(list
  1323. (cons (const vm)
  1324. (choice
  1325. (const vm-visit-folder)
  1326. (const vm-visit-folder-other-window)
  1327. (const vm-visit-folder-other-frame)))
  1328. (cons (const gnus)
  1329. (choice
  1330. (const gnus)
  1331. (const gnus-other-frame)
  1332. (const org-gnus-no-new-news)))
  1333. (cons (const file)
  1334. (choice
  1335. (const find-file)
  1336. (const find-file-other-window)
  1337. (const find-file-other-frame)))
  1338. (cons (const wl)
  1339. (choice
  1340. (const wl)
  1341. (const wl-other-frame)))))
  1342. (defcustom org-display-internal-link-with-indirect-buffer nil
  1343. "Non-nil means use indirect buffer to display infile links.
  1344. Activating internal links (from one location in a file to another location
  1345. in the same file) normally just jumps to the location. When the link is
  1346. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1347. is displayed in
  1348. another window. When this option is set, the other window actually displays
  1349. an indirect buffer clone of the current buffer, to avoid any visibility
  1350. changes to the current buffer."
  1351. :group 'org-link-follow
  1352. :type 'boolean)
  1353. (defcustom org-open-non-existing-files nil
  1354. "Non-nil means `org-open-file' will open non-existing files.
  1355. When nil, an error will be generated.
  1356. This variable applies only to external applications because they
  1357. might choke on non-existing files. If the link is to a file that
  1358. will be opened in Emacs, the variable is ignored."
  1359. :group 'org-link-follow
  1360. :type 'boolean)
  1361. (defcustom org-open-directory-means-index-dot-org nil
  1362. "Non-nil means a link to a directory really means to index.org.
  1363. When nil, following a directory link will run dired or open a finder/explorer
  1364. window on that directory."
  1365. :group 'org-link-follow
  1366. :type 'boolean)
  1367. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1368. "Function and arguments to call for following mailto links.
  1369. This is a list with the first element being a Lisp function, and the
  1370. remaining elements being arguments to the function. In string arguments,
  1371. %a will be replaced by the address, and %s will be replaced by the subject
  1372. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1373. :group 'org-link-follow
  1374. :type '(choice
  1375. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1376. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1377. (const :tag "message-mail" (message-mail "%a" "%s"))
  1378. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1379. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1380. "Non-nil means ask for confirmation before executing shell links.
  1381. Shell links can be dangerous: just think about a link
  1382. [[shell:rm -rf ~/*][Google Search]]
  1383. This link would show up in your Org-mode document as \"Google Search\",
  1384. but really it would remove your entire home directory.
  1385. Therefore we advise against setting this variable to nil.
  1386. Just change it to `y-or-n-p' if you want to confirm with a
  1387. single keystroke rather than having to type \"yes\"."
  1388. :group 'org-link-follow
  1389. :type '(choice
  1390. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1391. (const :tag "with y-or-n (faster)" y-or-n-p)
  1392. (const :tag "no confirmation (dangerous)" nil)))
  1393. (put 'org-confirm-shell-link-function
  1394. 'safe-local-variable
  1395. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1396. (defcustom org-confirm-shell-link-not-regexp ""
  1397. "A regexp to skip confirmation for shell links."
  1398. :group 'org-link-follow
  1399. :type 'regexp)
  1400. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1401. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1402. Elisp links can be dangerous: just think about a link
  1403. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1404. This link would show up in your Org-mode document as \"Google Search\",
  1405. but really it would remove your entire home directory.
  1406. Therefore we advise against setting this variable to nil.
  1407. Just change it to `y-or-n-p' if you want to confirm with a
  1408. single keystroke rather than having to type \"yes\"."
  1409. :group 'org-link-follow
  1410. :type '(choice
  1411. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1412. (const :tag "with y-or-n (faster)" y-or-n-p)
  1413. (const :tag "no confirmation (dangerous)" nil)))
  1414. (put 'org-confirm-shell-link-function
  1415. 'safe-local-variable
  1416. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1417. (defcustom org-confirm-elisp-link-not-regexp ""
  1418. "A regexp to skip confirmation for Elisp links."
  1419. :group 'org-link-follow
  1420. :type 'regexp)
  1421. (defconst org-file-apps-defaults-gnu
  1422. '((remote . emacs)
  1423. (system . mailcap)
  1424. (t . mailcap))
  1425. "Default file applications on a UNIX or GNU/Linux system.
  1426. See `org-file-apps'.")
  1427. (defconst org-file-apps-defaults-macosx
  1428. '((remote . emacs)
  1429. (t . "open %s")
  1430. (system . "open %s")
  1431. ("ps.gz" . "gv %s")
  1432. ("eps.gz" . "gv %s")
  1433. ("dvi" . "xdvi %s")
  1434. ("fig" . "xfig %s"))
  1435. "Default file applications on a MacOS X system.
  1436. The system \"open\" is known as a default, but we use X11 applications
  1437. for some files for which the OS does not have a good default.
  1438. See `org-file-apps'.")
  1439. (defconst org-file-apps-defaults-windowsnt
  1440. (list
  1441. '(remote . emacs)
  1442. (cons t
  1443. (list (if (featurep 'xemacs)
  1444. 'mswindows-shell-execute
  1445. 'w32-shell-execute)
  1446. "open" 'file))
  1447. (cons 'system
  1448. (list (if (featurep 'xemacs)
  1449. 'mswindows-shell-execute
  1450. 'w32-shell-execute)
  1451. "open" 'file)))
  1452. "Default file applications on a Windows NT system.
  1453. The system \"open\" is used for most files.
  1454. See `org-file-apps'.")
  1455. (defcustom org-file-apps
  1456. '(
  1457. (auto-mode . emacs)
  1458. ("\\.mm\\'" . default)
  1459. ("\\.x?html?\\'" . default)
  1460. ("\\.pdf\\'" . default)
  1461. )
  1462. "External applications for opening `file:path' items in a document.
  1463. Org-mode uses system defaults for different file types, but
  1464. you can use this variable to set the application for a given file
  1465. extension. The entries in this list are cons cells where the car identifies
  1466. files and the cdr the corresponding command. Possible values for the
  1467. file identifier are
  1468. \"string\" A string as a file identifier can be interpreted in different
  1469. ways, depending on its contents:
  1470. - Alphanumeric characters only:
  1471. Match links with this file extension.
  1472. Example: (\"pdf\" . \"evince %s\")
  1473. to open PDFs with evince.
  1474. - Regular expression: Match links where the
  1475. filename matches the regexp. If you want to
  1476. use groups here, use shy groups.
  1477. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1478. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1479. to open *.html and *.xhtml with firefox.
  1480. - Regular expression which contains (non-shy) groups:
  1481. Match links where the whole link, including \"::\", and
  1482. anything after that, matches the regexp.
  1483. In a custom command string, %1, %2, etc. are replaced with
  1484. the parts of the link that were matched by the groups.
  1485. For backwards compatibility, if a command string is given
  1486. that does not use any of the group matches, this case is
  1487. handled identically to the second one (i.e. match against
  1488. file name only).
  1489. In a custom lisp form, you can access the group matches with
  1490. (match-string n link).
  1491. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1492. to open [[file:document.pdf::5]] with evince at page 5.
  1493. `directory' Matches a directory
  1494. `remote' Matches a remote file, accessible through tramp or efs.
  1495. Remote files most likely should be visited through Emacs
  1496. because external applications cannot handle such paths.
  1497. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1498. so all files Emacs knows how to handle. Using this with
  1499. command `emacs' will open most files in Emacs. Beware that this
  1500. will also open html files inside Emacs, unless you add
  1501. (\"html\" . default) to the list as well.
  1502. t Default for files not matched by any of the other options.
  1503. `system' The system command to open files, like `open' on Windows
  1504. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1505. that will be selected if you call `C-c C-o' with a double
  1506. \\[universal-argument] \\[universal-argument] prefix.
  1507. Possible values for the command are:
  1508. `emacs' The file will be visited by the current Emacs process.
  1509. `default' Use the default application for this file type, which is the
  1510. association for t in the list, most likely in the system-specific
  1511. part.
  1512. This can be used to overrule an unwanted setting in the
  1513. system-specific variable.
  1514. `system' Use the system command for opening files, like \"open\".
  1515. This command is specified by the entry whose car is `system'.
  1516. Most likely, the system-specific version of this variable
  1517. does define this command, but you can overrule/replace it
  1518. here.
  1519. string A command to be executed by a shell; %s will be replaced
  1520. by the path to the file.
  1521. sexp A Lisp form which will be evaluated. The file path will
  1522. be available in the Lisp variable `file'.
  1523. For more examples, see the system specific constants
  1524. `org-file-apps-defaults-macosx'
  1525. `org-file-apps-defaults-windowsnt'
  1526. `org-file-apps-defaults-gnu'."
  1527. :group 'org-link-follow
  1528. :type '(repeat
  1529. (cons (choice :value ""
  1530. (string :tag "Extension")
  1531. (const :tag "System command to open files" system)
  1532. (const :tag "Default for unrecognized files" t)
  1533. (const :tag "Remote file" remote)
  1534. (const :tag "Links to a directory" directory)
  1535. (const :tag "Any files that have Emacs modes"
  1536. auto-mode))
  1537. (choice :value ""
  1538. (const :tag "Visit with Emacs" emacs)
  1539. (const :tag "Use default" default)
  1540. (const :tag "Use the system command" system)
  1541. (string :tag "Command")
  1542. (sexp :tag "Lisp form")))))
  1543. (defgroup org-refile nil
  1544. "Options concerning refiling entries in Org-mode."
  1545. :tag "Org Refile"
  1546. :group 'org)
  1547. (defcustom org-directory "~/org"
  1548. "Directory with org files.
  1549. This is just a default location to look for Org files. There is no need
  1550. at all to put your files into this directory. It is only used in the
  1551. following situations:
  1552. 1. When a remember template specifies a target file that is not an
  1553. absolute path. The path will then be interpreted relative to
  1554. `org-directory'
  1555. 2. When a remember note is filed away in an interactive way (when exiting the
  1556. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1557. with `org-directory' as the default path."
  1558. :group 'org-refile
  1559. :group 'org-remember
  1560. :type 'directory)
  1561. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1562. "Default target for storing notes.
  1563. Used as a fall back file for org-remember.el and org-capture.el, for
  1564. templates that do not specify a target file."
  1565. :group 'org-refile
  1566. :group 'org-remember
  1567. :type '(choice
  1568. (const :tag "Default from remember-data-file" nil)
  1569. file))
  1570. (defcustom org-goto-interface 'outline
  1571. "The default interface to be used for `org-goto'.
  1572. Allowed values are:
  1573. outline The interface shows an outline of the relevant file
  1574. and the correct heading is found by moving through
  1575. the outline or by searching with incremental search.
  1576. outline-path-completion Headlines in the current buffer are offered via
  1577. completion. This is the interface also used by
  1578. the refile command."
  1579. :group 'org-refile
  1580. :type '(choice
  1581. (const :tag "Outline" outline)
  1582. (const :tag "Outline-path-completion" outline-path-completion)))
  1583. (defcustom org-goto-max-level 5
  1584. "Maximum target level when running `org-goto' with refile interface."
  1585. :group 'org-refile
  1586. :type 'integer)
  1587. (defcustom org-reverse-note-order nil
  1588. "Non-nil means store new notes at the beginning of a file or entry.
  1589. When nil, new notes will be filed to the end of a file or entry.
  1590. This can also be a list with cons cells of regular expressions that
  1591. are matched against file names, and values."
  1592. :group 'org-remember
  1593. :group 'org-refile
  1594. :type '(choice
  1595. (const :tag "Reverse always" t)
  1596. (const :tag "Reverse never" nil)
  1597. (repeat :tag "By file name regexp"
  1598. (cons regexp boolean))))
  1599. (defcustom org-log-refile nil
  1600. "Information to record when a task is refiled.
  1601. Possible values are:
  1602. nil Don't add anything
  1603. time Add a time stamp to the task
  1604. note Prompt for a note and add it with template `org-log-note-headings'
  1605. This option can also be set with on a per-file-basis with
  1606. #+STARTUP: nologrefile
  1607. #+STARTUP: logrefile
  1608. #+STARTUP: lognoterefile
  1609. You can have local logging settings for a subtree by setting the LOGGING
  1610. property to one or more of these keywords.
  1611. When bulk-refiling from the agenda, the value `note' is forbidden and
  1612. will temporarily be changed to `time'."
  1613. :group 'org-refile
  1614. :group 'org-progress
  1615. :type '(choice
  1616. (const :tag "No logging" nil)
  1617. (const :tag "Record timestamp" time)
  1618. (const :tag "Record timestamp with note." note)))
  1619. (defcustom org-refile-targets nil
  1620. "Targets for refiling entries with \\[org-refile].
  1621. This is list of cons cells. Each cell contains:
  1622. - a specification of the files to be considered, either a list of files,
  1623. or a symbol whose function or variable value will be used to retrieve
  1624. a file name or a list of file names. If you use `org-agenda-files' for
  1625. that, all agenda files will be scanned for targets. Nil means consider
  1626. headings in the current buffer.
  1627. - A specification of how to find candidate refile targets. This may be
  1628. any of:
  1629. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1630. This tag has to be present in all target headlines, inheritance will
  1631. not be considered.
  1632. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1633. todo keyword.
  1634. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1635. headlines that are refiling targets.
  1636. - a cons cell (:level . N). Any headline of level N is considered a target.
  1637. Note that, when `org-odd-levels-only' is set, level corresponds to
  1638. order in hierarchy, not to the number of stars.
  1639. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1640. Note that, when `org-odd-levels-only' is set, level corresponds to
  1641. order in hierarchy, not to the number of stars.
  1642. You can set the variable `org-refile-target-verify-function' to a function
  1643. to verify each headline found by the simple criteria above.
  1644. When this variable is nil, all top-level headlines in the current buffer
  1645. are used, equivalent to the value `((nil . (:level . 1))'."
  1646. :group 'org-refile
  1647. :type '(repeat
  1648. (cons
  1649. (choice :value org-agenda-files
  1650. (const :tag "All agenda files" org-agenda-files)
  1651. (const :tag "Current buffer" nil)
  1652. (function) (variable) (file))
  1653. (choice :tag "Identify target headline by"
  1654. (cons :tag "Specific tag" (const :value :tag) (string))
  1655. (cons :tag "TODO keyword" (const :value :todo) (string))
  1656. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1657. (cons :tag "Level number" (const :value :level) (integer))
  1658. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1659. (defcustom org-refile-target-verify-function nil
  1660. "Function to verify if the headline at point should be a refile target.
  1661. The function will be called without arguments, with point at the
  1662. beginning of the headline. It should return t and leave point
  1663. where it is if the headline is a valid target for refiling.
  1664. If the target should not be selected, the function must return nil.
  1665. In addition to this, it may move point to a place from where the search
  1666. should be continued. For example, the function may decide that the entire
  1667. subtree of the current entry should be excluded and move point to the end
  1668. of the subtree."
  1669. :group 'org-refile
  1670. :type 'function)
  1671. (defcustom org-refile-use-cache nil
  1672. "Non-nil means cache refile targets to speed up the process.
  1673. The cache for a particular file will be updated automatically when
  1674. the buffer has been killed, or when any of the marker used for flagging
  1675. refile targets no longer points at a live buffer.
  1676. If you have added new entries to a buffer that might themselves be targets,
  1677. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1678. find that easier, `C-u C-u C-u C-c C-w'."
  1679. :group 'org-refile
  1680. :type 'boolean)
  1681. (defcustom org-refile-use-outline-path nil
  1682. "Non-nil means provide refile targets as paths.
  1683. So a level 3 headline will be available as level1/level2/level3.
  1684. When the value is `file', also include the file name (without directory)
  1685. into the path. In this case, you can also stop the completion after
  1686. the file name, to get entries inserted as top level in the file.
  1687. When `full-file-path', include the full file path."
  1688. :group 'org-refile
  1689. :type '(choice
  1690. (const :tag "Not" nil)
  1691. (const :tag "Yes" t)
  1692. (const :tag "Start with file name" file)
  1693. (const :tag "Start with full file path" full-file-path)))
  1694. (defcustom org-outline-path-complete-in-steps t
  1695. "Non-nil means complete the outline path in hierarchical steps.
  1696. When Org-mode uses the refile interface to select an outline path
  1697. \(see variable `org-refile-use-outline-path'), the completion of
  1698. the path can be done is a single go, or if can be done in steps down
  1699. the headline hierarchy. Going in steps is probably the best if you
  1700. do not use a special completion package like `ido' or `icicles'.
  1701. However, when using these packages, going in one step can be very
  1702. fast, while still showing the whole path to the entry."
  1703. :group 'org-refile
  1704. :type 'boolean)
  1705. (defcustom org-refile-allow-creating-parent-nodes nil
  1706. "Non-nil means allow to create new nodes as refile targets.
  1707. New nodes are then created by adding \"/new node name\" to the completion
  1708. of an existing node. When the value of this variable is `confirm',
  1709. new node creation must be confirmed by the user (recommended)
  1710. When nil, the completion must match an existing entry.
  1711. Note that, if the new heading is not seen by the criteria
  1712. listed in `org-refile-targets', multiple instances of the same
  1713. heading would be created by trying again to file under the new
  1714. heading."
  1715. :group 'org-refile
  1716. :type '(choice
  1717. (const :tag "Never" nil)
  1718. (const :tag "Always" t)
  1719. (const :tag "Prompt for confirmation" confirm)))
  1720. (defgroup org-todo nil
  1721. "Options concerning TODO items in Org-mode."
  1722. :tag "Org TODO"
  1723. :group 'org)
  1724. (defgroup org-progress nil
  1725. "Options concerning Progress logging in Org-mode."
  1726. :tag "Org Progress"
  1727. :group 'org-time)
  1728. (defvar org-todo-interpretation-widgets
  1729. '(
  1730. (:tag "Sequence (cycling hits every state)" sequence)
  1731. (:tag "Type (cycling directly to DONE)" type))
  1732. "The available interpretation symbols for customizing `org-todo-keywords'.
  1733. Interested libraries should add to this list.")
  1734. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1735. "List of TODO entry keyword sequences and their interpretation.
  1736. \\<org-mode-map>This is a list of sequences.
  1737. Each sequence starts with a symbol, either `sequence' or `type',
  1738. indicating if the keywords should be interpreted as a sequence of
  1739. action steps, or as different types of TODO items. The first
  1740. keywords are states requiring action - these states will select a headline
  1741. for inclusion into the global TODO list Org-mode produces. If one of
  1742. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1743. signify that no further action is necessary. If \"|\" is not found,
  1744. the last keyword is treated as the only DONE state of the sequence.
  1745. The command \\[org-todo] cycles an entry through these states, and one
  1746. additional state where no keyword is present. For details about this
  1747. cycling, see the manual.
  1748. TODO keywords and interpretation can also be set on a per-file basis with
  1749. the special #+SEQ_TODO and #+TYP_TODO lines.
  1750. Each keyword can optionally specify a character for fast state selection
  1751. \(in combination with the variable `org-use-fast-todo-selection')
  1752. and specifiers for state change logging, using the same syntax
  1753. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1754. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1755. indicates to record a time stamp each time this state is selected.
  1756. Each keyword may also specify if a timestamp or a note should be
  1757. recorded when entering or leaving the state, by adding additional
  1758. characters in the parenthesis after the keyword. This looks like this:
  1759. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1760. record only the time of the state change. With X and Y being either
  1761. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1762. Y when leaving the state if and only if the *target* state does not
  1763. define X. You may omit any of the fast-selection key or X or /Y,
  1764. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1765. For backward compatibility, this variable may also be just a list
  1766. of keywords - in this case the interpretation (sequence or type) will be
  1767. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1768. :group 'org-todo
  1769. :group 'org-keywords
  1770. :type '(choice
  1771. (repeat :tag "Old syntax, just keywords"
  1772. (string :tag "Keyword"))
  1773. (repeat :tag "New syntax"
  1774. (cons
  1775. (choice
  1776. :tag "Interpretation"
  1777. ;;Quick and dirty way to see
  1778. ;;`org-todo-interpretations'. This takes the
  1779. ;;place of item arguments
  1780. :convert-widget
  1781. (lambda (widget)
  1782. (widget-put widget
  1783. :args (mapcar
  1784. #'(lambda (x)
  1785. (widget-convert
  1786. (cons 'const x)))
  1787. org-todo-interpretation-widgets))
  1788. widget))
  1789. (repeat
  1790. (string :tag "Keyword"))))))
  1791. (defvar org-todo-keywords-1 nil
  1792. "All TODO and DONE keywords active in a buffer.")
  1793. (make-variable-buffer-local 'org-todo-keywords-1)
  1794. (defvar org-todo-keywords-for-agenda nil)
  1795. (defvar org-done-keywords-for-agenda nil)
  1796. (defvar org-drawers-for-agenda nil)
  1797. (defvar org-todo-keyword-alist-for-agenda nil)
  1798. (defvar org-tag-alist-for-agenda nil)
  1799. (defvar org-agenda-contributing-files nil)
  1800. (defvar org-not-done-keywords nil)
  1801. (make-variable-buffer-local 'org-not-done-keywords)
  1802. (defvar org-done-keywords nil)
  1803. (make-variable-buffer-local 'org-done-keywords)
  1804. (defvar org-todo-heads nil)
  1805. (make-variable-buffer-local 'org-todo-heads)
  1806. (defvar org-todo-sets nil)
  1807. (make-variable-buffer-local 'org-todo-sets)
  1808. (defvar org-todo-log-states nil)
  1809. (make-variable-buffer-local 'org-todo-log-states)
  1810. (defvar org-todo-kwd-alist nil)
  1811. (make-variable-buffer-local 'org-todo-kwd-alist)
  1812. (defvar org-todo-key-alist nil)
  1813. (make-variable-buffer-local 'org-todo-key-alist)
  1814. (defvar org-todo-key-trigger nil)
  1815. (make-variable-buffer-local 'org-todo-key-trigger)
  1816. (defcustom org-todo-interpretation 'sequence
  1817. "Controls how TODO keywords are interpreted.
  1818. This variable is in principle obsolete and is only used for
  1819. backward compatibility, if the interpretation of todo keywords is
  1820. not given already in `org-todo-keywords'. See that variable for
  1821. more information."
  1822. :group 'org-todo
  1823. :group 'org-keywords
  1824. :type '(choice (const sequence)
  1825. (const type)))
  1826. (defcustom org-use-fast-todo-selection t
  1827. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1828. This variable describes if and under what circumstances the cycling
  1829. mechanism for TODO keywords will be replaced by a single-key, direct
  1830. selection scheme.
  1831. When nil, fast selection is never used.
  1832. When the symbol `prefix', it will be used when `org-todo' is called with
  1833. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1834. in an agenda buffer.
  1835. When t, fast selection is used by default. In this case, the prefix
  1836. argument forces cycling instead.
  1837. In all cases, the special interface is only used if access keys have actually
  1838. been assigned by the user, i.e. if keywords in the configuration are followed
  1839. by a letter in parenthesis, like TODO(t)."
  1840. :group 'org-todo
  1841. :type '(choice
  1842. (const :tag "Never" nil)
  1843. (const :tag "By default" t)
  1844. (const :tag "Only with C-u C-c C-t" prefix)))
  1845. (defcustom org-provide-todo-statistics t
  1846. "Non-nil means update todo statistics after insert and toggle.
  1847. ALL-HEADLINES means update todo statistics by including headlines
  1848. with no TODO keyword as well, counting them as not done.
  1849. A list of TODO keywords means the same, but skip keywords that are
  1850. not in this list.
  1851. When this is set, todo statistics is updated in the parent of the
  1852. current entry each time a todo state is changed."
  1853. :group 'org-todo
  1854. :type '(choice
  1855. (const :tag "Yes, only for TODO entries" t)
  1856. (const :tag "Yes, including all entries" 'all-headlines)
  1857. (repeat :tag "Yes, for TODOs in this list"
  1858. (string :tag "TODO keyword"))
  1859. (other :tag "No TODO statistics" nil)))
  1860. (defcustom org-hierarchical-todo-statistics t
  1861. "Non-nil means TODO statistics covers just direct children.
  1862. When nil, all entries in the subtree are considered.
  1863. This has only an effect if `org-provide-todo-statistics' is set.
  1864. To set this to nil for only a single subtree, use a COOKIE_DATA
  1865. property and include the word \"recursive\" into the value."
  1866. :group 'org-todo
  1867. :type 'boolean)
  1868. (defcustom org-after-todo-state-change-hook nil
  1869. "Hook which is run after the state of a TODO item was changed.
  1870. The new state (a string with a TODO keyword, or nil) is available in the
  1871. Lisp variable `state'."
  1872. :group 'org-todo
  1873. :type 'hook)
  1874. (defvar org-blocker-hook nil
  1875. "Hook for functions that are allowed to block a state change.
  1876. Each function gets as its single argument a property list, see
  1877. `org-trigger-hook' for more information about this list.
  1878. If any of the functions in this hook returns nil, the state change
  1879. is blocked.")
  1880. (defvar org-trigger-hook nil
  1881. "Hook for functions that are triggered by a state change.
  1882. Each function gets as its single argument a property list with at least
  1883. the following elements:
  1884. (:type type-of-change :position pos-at-entry-start
  1885. :from old-state :to new-state)
  1886. Depending on the type, more properties may be present.
  1887. This mechanism is currently implemented for:
  1888. TODO state changes
  1889. ------------------
  1890. :type todo-state-change
  1891. :from previous state (keyword as a string), or nil, or a symbol
  1892. 'todo' or 'done', to indicate the general type of state.
  1893. :to new state, like in :from")
  1894. (defcustom org-enforce-todo-dependencies nil
  1895. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1896. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1897. be blocked if any prior sibling is not yet done.
  1898. Finally, if the parent is blocked because of ordered siblings of its own,
  1899. the child will also be blocked.
  1900. This variable needs to be set before org.el is loaded, and you need to
  1901. restart Emacs after a change to make the change effective. The only way
  1902. to change is while Emacs is running is through the customize interface."
  1903. :set (lambda (var val)
  1904. (set var val)
  1905. (if val
  1906. (add-hook 'org-blocker-hook
  1907. 'org-block-todo-from-children-or-siblings-or-parent)
  1908. (remove-hook 'org-blocker-hook
  1909. 'org-block-todo-from-children-or-siblings-or-parent)))
  1910. :group 'org-todo
  1911. :type 'boolean)
  1912. (defcustom org-enforce-todo-checkbox-dependencies nil
  1913. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1914. When this is nil, checkboxes have no influence on switching TODO states.
  1915. When non-nil, you first need to check off all check boxes before the TODO
  1916. entry can be switched to DONE.
  1917. This variable needs to be set before org.el is loaded, and you need to
  1918. restart Emacs after a change to make the change effective. The only way
  1919. to change is while Emacs is running is through the customize interface."
  1920. :set (lambda (var val)
  1921. (set var val)
  1922. (if val
  1923. (add-hook 'org-blocker-hook
  1924. 'org-block-todo-from-checkboxes)
  1925. (remove-hook 'org-blocker-hook
  1926. 'org-block-todo-from-checkboxes)))
  1927. :group 'org-todo
  1928. :type 'boolean)
  1929. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1930. "Non-nil means inserting a TODO heading is treated as state change.
  1931. So when the command \\[org-insert-todo-heading] is used, state change
  1932. logging will apply if appropriate. When nil, the new TODO item will
  1933. be inserted directly, and no logging will take place."
  1934. :group 'org-todo
  1935. :type 'boolean)
  1936. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1937. "Non-nil means switching TODO states with S-cursor counts as state change.
  1938. This is the default behavior. However, setting this to nil allows a
  1939. convenient way to select a TODO state and bypass any logging associated
  1940. with that."
  1941. :group 'org-todo
  1942. :type 'boolean)
  1943. (defcustom org-todo-state-tags-triggers nil
  1944. "Tag changes that should be triggered by TODO state changes.
  1945. This is a list. Each entry is
  1946. (state-change (tag . flag) .......)
  1947. State-change can be a string with a state, and empty string to indicate the
  1948. state that has no TODO keyword, or it can be one of the symbols `todo'
  1949. or `done', meaning any not-done or done state, respectively."
  1950. :group 'org-todo
  1951. :group 'org-tags
  1952. :type '(repeat
  1953. (cons (choice :tag "When changing to"
  1954. (const :tag "Not-done state" todo)
  1955. (const :tag "Done state" done)
  1956. (string :tag "State"))
  1957. (repeat
  1958. (cons :tag "Tag action"
  1959. (string :tag "Tag")
  1960. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1961. (defcustom org-log-done nil
  1962. "Information to record when a task moves to the DONE state.
  1963. Possible values are:
  1964. nil Don't add anything, just change the keyword
  1965. time Add a time stamp to the task
  1966. note Prompt for a note and add it with template `org-log-note-headings'
  1967. This option can also be set with on a per-file-basis with
  1968. #+STARTUP: nologdone
  1969. #+STARTUP: logdone
  1970. #+STARTUP: lognotedone
  1971. You can have local logging settings for a subtree by setting the LOGGING
  1972. property to one or more of these keywords."
  1973. :group 'org-todo
  1974. :group 'org-progress
  1975. :type '(choice
  1976. (const :tag "No logging" nil)
  1977. (const :tag "Record CLOSED timestamp" time)
  1978. (const :tag "Record CLOSED timestamp with note." note)))
  1979. ;; Normalize old uses of org-log-done.
  1980. (cond
  1981. ((eq org-log-done t) (setq org-log-done 'time))
  1982. ((and (listp org-log-done) (memq 'done org-log-done))
  1983. (setq org-log-done 'note)))
  1984. (defcustom org-log-reschedule nil
  1985. "Information to record when the scheduling date of a tasks is modified.
  1986. Possible values are:
  1987. nil Don't add anything, just change the date
  1988. time Add a time stamp to the task
  1989. note Prompt for a note and add it with template `org-log-note-headings'
  1990. This option can also be set with on a per-file-basis with
  1991. #+STARTUP: nologreschedule
  1992. #+STARTUP: logreschedule
  1993. #+STARTUP: lognotereschedule"
  1994. :group 'org-todo
  1995. :group 'org-progress
  1996. :type '(choice
  1997. (const :tag "No logging" nil)
  1998. (const :tag "Record timestamp" time)
  1999. (const :tag "Record timestamp with note." note)))
  2000. (defcustom org-log-redeadline nil
  2001. "Information to record when the deadline date of a tasks is modified.
  2002. Possible values are:
  2003. nil Don't add anything, just change the date
  2004. time Add a time stamp to the task
  2005. note Prompt for a note and add it with template `org-log-note-headings'
  2006. This option can also be set with on a per-file-basis with
  2007. #+STARTUP: nologredeadline
  2008. #+STARTUP: logredeadline
  2009. #+STARTUP: lognoteredeadline
  2010. You can have local logging settings for a subtree by setting the LOGGING
  2011. property to one or more of these keywords."
  2012. :group 'org-todo
  2013. :group 'org-progress
  2014. :type '(choice
  2015. (const :tag "No logging" nil)
  2016. (const :tag "Record timestamp" time)
  2017. (const :tag "Record timestamp with note." note)))
  2018. (defcustom org-log-note-clock-out nil
  2019. "Non-nil means record a note when clocking out of an item.
  2020. This can also be configured on a per-file basis by adding one of
  2021. the following lines anywhere in the buffer:
  2022. #+STARTUP: lognoteclock-out
  2023. #+STARTUP: nolognoteclock-out"
  2024. :group 'org-todo
  2025. :group 'org-progress
  2026. :type 'boolean)
  2027. (defcustom org-log-done-with-time t
  2028. "Non-nil means the CLOSED time stamp will contain date and time.
  2029. When nil, only the date will be recorded."
  2030. :group 'org-progress
  2031. :type 'boolean)
  2032. (defcustom org-log-note-headings
  2033. '((done . "CLOSING NOTE %t")
  2034. (state . "State %-12s from %-12S %t")
  2035. (note . "Note taken on %t")
  2036. (reschedule . "Rescheduled from %S on %t")
  2037. (delschedule . "Not scheduled, was %S on %t")
  2038. (redeadline . "New deadline from %S on %t")
  2039. (deldeadline . "Removed deadline, was %S on %t")
  2040. (refile . "Refiled on %t")
  2041. (clock-out . ""))
  2042. "Headings for notes added to entries.
  2043. The value is an alist, with the car being a symbol indicating the note
  2044. context, and the cdr is the heading to be used. The heading may also be the
  2045. empty string.
  2046. %t in the heading will be replaced by a time stamp.
  2047. %T will be an active time stamp instead the default inactive one
  2048. %s will be replaced by the new TODO state, in double quotes.
  2049. %S will be replaced by the old TODO state, in double quotes.
  2050. %u will be replaced by the user name.
  2051. %U will be replaced by the full user name.
  2052. In fact, it is not a good idea to change the `state' entry, because
  2053. agenda log mode depends on the format of these entries."
  2054. :group 'org-todo
  2055. :group 'org-progress
  2056. :type '(list :greedy t
  2057. (cons (const :tag "Heading when closing an item" done) string)
  2058. (cons (const :tag
  2059. "Heading when changing todo state (todo sequence only)"
  2060. state) string)
  2061. (cons (const :tag "Heading when just taking a note" note) string)
  2062. (cons (const :tag "Heading when clocking out" clock-out) string)
  2063. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2064. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2065. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2066. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2067. (cons (const :tag "Heading when refiling" refile) string)))
  2068. (unless (assq 'note org-log-note-headings)
  2069. (push '(note . "%t") org-log-note-headings))
  2070. (defcustom org-log-into-drawer nil
  2071. "Non-nil means insert state change notes and time stamps into a drawer.
  2072. When nil, state changes notes will be inserted after the headline and
  2073. any scheduling and clock lines, but not inside a drawer.
  2074. The value of this variable should be the name of the drawer to use.
  2075. LOGBOOK is proposed as the default drawer for this purpose, you can
  2076. also set this to a string to define the drawer of your choice.
  2077. A value of t is also allowed, representing \"LOGBOOK\".
  2078. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2079. will be ignored.
  2080. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2081. a subtree."
  2082. :group 'org-todo
  2083. :group 'org-progress
  2084. :type '(choice
  2085. (const :tag "Not into a drawer" nil)
  2086. (const :tag "LOGBOOK" t)
  2087. (string :tag "Other")))
  2088. (if (fboundp 'defvaralias)
  2089. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2090. (defun org-log-into-drawer ()
  2091. "Return the value of `org-log-into-drawer', but let properties overrule.
  2092. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2093. used instead of the default value."
  2094. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2095. (cond
  2096. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2097. ((equal p "t") "LOGBOOK")
  2098. (t p))))
  2099. (defcustom org-log-state-notes-insert-after-drawers nil
  2100. "Non-nil means insert state change notes after any drawers in entry.
  2101. Only the drawers that *immediately* follow the headline and the
  2102. deadline/scheduled line are skipped.
  2103. When nil, insert notes right after the heading and perhaps the line
  2104. with deadline/scheduling if present.
  2105. This variable will have no effect if `org-log-into-drawer' is
  2106. set."
  2107. :group 'org-todo
  2108. :group 'org-progress
  2109. :type 'boolean)
  2110. (defcustom org-log-states-order-reversed t
  2111. "Non-nil means the latest state note will be directly after heading.
  2112. When nil, the state change notes will be ordered according to time."
  2113. :group 'org-todo
  2114. :group 'org-progress
  2115. :type 'boolean)
  2116. (defcustom org-todo-repeat-to-state nil
  2117. "The TODO state to which a repeater should return the repeating task.
  2118. By default this is the first task in a TODO sequence, or the previous state
  2119. in a TODO_TYP set. But you can specify another task here.
  2120. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2121. :group 'org-todo
  2122. :type '(choice (const :tag "Head of sequence" nil)
  2123. (string :tag "Specific state")))
  2124. (defcustom org-log-repeat 'time
  2125. "Non-nil means record moving through the DONE state when triggering repeat.
  2126. An auto-repeating task is immediately switched back to TODO when
  2127. marked DONE. If you are not logging state changes (by adding \"@\"
  2128. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2129. record a closing note, there will be no record of the task moving
  2130. through DONE. This variable forces taking a note anyway.
  2131. nil Don't force a record
  2132. time Record a time stamp
  2133. note Record a note
  2134. This option can also be set with on a per-file-basis with
  2135. #+STARTUP: logrepeat
  2136. #+STARTUP: lognoterepeat
  2137. #+STARTUP: nologrepeat
  2138. You can have local logging settings for a subtree by setting the LOGGING
  2139. property to one or more of these keywords."
  2140. :group 'org-todo
  2141. :group 'org-progress
  2142. :type '(choice
  2143. (const :tag "Don't force a record" nil)
  2144. (const :tag "Force recording the DONE state" time)
  2145. (const :tag "Force recording a note with the DONE state" note)))
  2146. (defgroup org-priorities nil
  2147. "Priorities in Org-mode."
  2148. :tag "Org Priorities"
  2149. :group 'org-todo)
  2150. (defcustom org-enable-priority-commands t
  2151. "Non-nil means priority commands are active.
  2152. When nil, these commands will be disabled, so that you never accidentally
  2153. set a priority."
  2154. :group 'org-priorities
  2155. :type 'boolean)
  2156. (defcustom org-highest-priority ?A
  2157. "The highest priority of TODO items. A character like ?A, ?B etc.
  2158. Must have a smaller ASCII number than `org-lowest-priority'."
  2159. :group 'org-priorities
  2160. :type 'character)
  2161. (defcustom org-lowest-priority ?C
  2162. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2163. Must have a larger ASCII number than `org-highest-priority'."
  2164. :group 'org-priorities
  2165. :type 'character)
  2166. (defcustom org-default-priority ?B
  2167. "The default priority of TODO items.
  2168. This is the priority an item gets if no explicit priority is given.
  2169. When starting to cycle on an empty priority the first step in the cycle
  2170. depends on `org-priority-start-cycle-with-default'. The resulting first
  2171. step priority must not exceed the range from `org-highest-priority' to
  2172. `org-lowest-priority' which means that `org-default-priority' has to be
  2173. in this range exclusive or inclusive the range boundaries. Else the
  2174. first step refuses to set the default and the second will fall back
  2175. to (depending on the command used) the highest or lowest priority."
  2176. :group 'org-priorities
  2177. :type 'character)
  2178. (defcustom org-priority-start-cycle-with-default t
  2179. "Non-nil means start with default priority when starting to cycle.
  2180. When this is nil, the first step in the cycle will be (depending on the
  2181. command used) one higher or lower than the default priority.
  2182. See also `org-default-priority'."
  2183. :group 'org-priorities
  2184. :type 'boolean)
  2185. (defcustom org-get-priority-function nil
  2186. "Function to extract the priority from a string.
  2187. The string is normally the headline. If this is nil Org computes the
  2188. priority from the priority cookie like [#A] in the headline. It returns
  2189. an integer, increasing by 1000 for each priority level.
  2190. The user can set a different function here, which should take a string
  2191. as an argument and return the numeric priority."
  2192. :group 'org-priorities
  2193. :type 'function)
  2194. (defgroup org-time nil
  2195. "Options concerning time stamps and deadlines in Org-mode."
  2196. :tag "Org Time"
  2197. :group 'org)
  2198. (defcustom org-insert-labeled-timestamps-at-point nil
  2199. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2200. When nil, these labeled time stamps are forces into the second line of an
  2201. entry, just after the headline. When scheduling from the global TODO list,
  2202. the time stamp will always be forced into the second line."
  2203. :group 'org-time
  2204. :type 'boolean)
  2205. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2206. "Formats for `format-time-string' which are used for time stamps.
  2207. It is not recommended to change this constant.")
  2208. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2209. "Number of minutes to round time stamps to.
  2210. These are two values, the first applies when first creating a time stamp.
  2211. The second applies when changing it with the commands `S-up' and `S-down'.
  2212. When changing the time stamp, this means that it will change in steps
  2213. of N minutes, as given by the second value.
  2214. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2215. numbers should be factors of 60, so for example 5, 10, 15.
  2216. When this is larger than 1, you can still force an exact time stamp by using
  2217. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2218. and by using a prefix arg to `S-up/down' to specify the exact number
  2219. of minutes to shift."
  2220. :group 'org-time
  2221. :get #'(lambda (var) ; Make sure both elements are there
  2222. (if (integerp (default-value var))
  2223. (list (default-value var) 5)
  2224. (default-value var)))
  2225. :type '(list
  2226. (integer :tag "when inserting times")
  2227. (integer :tag "when modifying times")))
  2228. ;; Normalize old customizations of this variable.
  2229. (when (integerp org-time-stamp-rounding-minutes)
  2230. (setq org-time-stamp-rounding-minutes
  2231. (list org-time-stamp-rounding-minutes
  2232. org-time-stamp-rounding-minutes)))
  2233. (defcustom org-display-custom-times nil
  2234. "Non-nil means overlay custom formats over all time stamps.
  2235. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2236. To turn this on on a per-file basis, insert anywhere in the file:
  2237. #+STARTUP: customtime"
  2238. :group 'org-time
  2239. :set 'set-default
  2240. :type 'sexp)
  2241. (make-variable-buffer-local 'org-display-custom-times)
  2242. (defcustom org-time-stamp-custom-formats
  2243. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2244. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2245. These are overlayed over the default ISO format if the variable
  2246. `org-display-custom-times' is set. Time like %H:%M should be at the
  2247. end of the second format. The custom formats are also honored by export
  2248. commands, if custom time display is turned on at the time of export."
  2249. :group 'org-time
  2250. :type 'sexp)
  2251. (defun org-time-stamp-format (&optional long inactive)
  2252. "Get the right format for a time string."
  2253. (let ((f (if long (cdr org-time-stamp-formats)
  2254. (car org-time-stamp-formats))))
  2255. (if inactive
  2256. (concat "[" (substring f 1 -1) "]")
  2257. f)))
  2258. (defcustom org-time-clocksum-format "%d:%02d"
  2259. "The format string used when creating CLOCKSUM lines.
  2260. This is also used when org-mode generates a time duration."
  2261. :group 'org-time
  2262. :type 'string)
  2263. (defcustom org-time-clocksum-use-fractional nil
  2264. "If non-nil, \\[org-clock-display] uses fractional times.
  2265. org-mode generates a time duration."
  2266. :group 'org-time
  2267. :type 'boolean)
  2268. (defcustom org-time-clocksum-fractional-format "%.2f"
  2269. "The format string used when creating CLOCKSUM lines, or when
  2270. org-mode generates a time duration."
  2271. :group 'org-time
  2272. :type 'string)
  2273. (defcustom org-deadline-warning-days 14
  2274. "No. of days before expiration during which a deadline becomes active.
  2275. This variable governs the display in sparse trees and in the agenda.
  2276. When 0 or negative, it means use this number (the absolute value of it)
  2277. even if a deadline has a different individual lead time specified.
  2278. Custom commands can set this variable in the options section."
  2279. :group 'org-time
  2280. :group 'org-agenda-daily/weekly
  2281. :type 'integer)
  2282. (defcustom org-read-date-prefer-future t
  2283. "Non-nil means assume future for incomplete date input from user.
  2284. This affects the following situations:
  2285. 1. The user gives a month but not a year.
  2286. For example, if it is April and you enter \"feb 2\", this will be read
  2287. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2288. 2. The user gives a day, but no month.
  2289. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2290. read this as the third of *next* month. However, if you enter \"17\",
  2291. it will be considered as *this* month.
  2292. If you set this variable to the symbol `time', then also the following
  2293. will work:
  2294. 3. If the user gives a time, but no day. If the time is before now,
  2295. to will be interpreted as tomorrow.
  2296. Currently none of this works for ISO week specifications.
  2297. When this option is nil, the current day, month and year will always be
  2298. used as defaults.
  2299. See also `org-agenda-jump-prefer-future'."
  2300. :group 'org-time
  2301. :type '(choice
  2302. (const :tag "Never" nil)
  2303. (const :tag "Check month and day" t)
  2304. (const :tag "Check month, day, and time" time)))
  2305. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2306. "Should the agenda jump command prefer the future for incomplete dates?
  2307. The default is to do the same as configured in `org-read-date-prefer-future'.
  2308. But you can also set a deviating value here.
  2309. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2310. :group 'org-agenda
  2311. :group 'org-time
  2312. :type '(choice
  2313. (const :tag "Use org-read-date-prefer-future"
  2314. org-read-date-prefer-future)
  2315. (const :tag "Never" nil)
  2316. (const :tag "Always" t)))
  2317. (defcustom org-read-date-force-compatible-dates t
  2318. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2319. Depending on the system Emacs is running on, certain dates cannot
  2320. be represented with the type used internally to represent time.
  2321. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2322. correctly. Some systems allow for earlier dates, some for later,
  2323. some for both. One way to find out it to insert any date into an
  2324. Org buffer, putting the cursor on the year and hitting S-up and
  2325. S-down to test the range.
  2326. When this variable is set to t, the date/time prompt will not let
  2327. you specify dates outside the 1970-2037 range, so it is certain that
  2328. these dates will work in whatever version of Emacs you are
  2329. running, and also that you can move a file from one Emacs implementation
  2330. to another. WHenever Org is forcing the year for you, it will display
  2331. a message and beep.
  2332. When this variable is nil, Org will check if the date is
  2333. representable in the specific Emacs implementation you are using.
  2334. If not, it will force a year, usually the current year, and beep
  2335. to remind you. Currently this setting is not recommended because
  2336. the likelihood that you will open your Org files in an Emacs that
  2337. has limited date range is not negligible.
  2338. A workaround for this problem is to use diary sexp dates for time
  2339. stamps outside of this range."
  2340. :group 'org-time
  2341. :type 'boolean)
  2342. (defcustom org-read-date-display-live t
  2343. "Non-nil means display current interpretation of date prompt live.
  2344. This display will be in an overlay, in the minibuffer."
  2345. :group 'org-time
  2346. :type 'boolean)
  2347. (defcustom org-read-date-popup-calendar t
  2348. "Non-nil means pop up a calendar when prompting for a date.
  2349. In the calendar, the date can be selected with mouse-1. However, the
  2350. minibuffer will also be active, and you can simply enter the date as well.
  2351. When nil, only the minibuffer will be available."
  2352. :group 'org-time
  2353. :type 'boolean)
  2354. (if (fboundp 'defvaralias)
  2355. (defvaralias 'org-popup-calendar-for-date-prompt
  2356. 'org-read-date-popup-calendar))
  2357. (defcustom org-read-date-minibuffer-setup-hook nil
  2358. "Hook to be used to set up keys for the date/time interface.
  2359. Add key definitions to `minibuffer-local-map', which will be a temporary
  2360. copy."
  2361. :group 'org-time
  2362. :type 'hook)
  2363. (defcustom org-extend-today-until 0
  2364. "The hour when your day really ends. Must be an integer.
  2365. This has influence for the following applications:
  2366. - When switching the agenda to \"today\". It it is still earlier than
  2367. the time given here, the day recognized as TODAY is actually yesterday.
  2368. - When a date is read from the user and it is still before the time given
  2369. here, the current date and time will be assumed to be yesterday, 23:59.
  2370. Also, timestamps inserted in remember templates follow this rule.
  2371. IMPORTANT: This is a feature whose implementation is and likely will
  2372. remain incomplete. Really, it is only here because past midnight seems to
  2373. be the favorite working time of John Wiegley :-)"
  2374. :group 'org-time
  2375. :type 'integer)
  2376. (defcustom org-edit-timestamp-down-means-later nil
  2377. "Non-nil means S-down will increase the time in a time stamp.
  2378. When nil, S-up will increase."
  2379. :group 'org-time
  2380. :type 'boolean)
  2381. (defcustom org-calendar-follow-timestamp-change t
  2382. "Non-nil means make the calendar window follow timestamp changes.
  2383. When a timestamp is modified and the calendar window is visible, it will be
  2384. moved to the new date."
  2385. :group 'org-time
  2386. :type 'boolean)
  2387. (defgroup org-tags nil
  2388. "Options concerning tags in Org-mode."
  2389. :tag "Org Tags"
  2390. :group 'org)
  2391. (defcustom org-tag-alist nil
  2392. "List of tags allowed in Org-mode files.
  2393. When this list is nil, Org-mode will base TAG input on what is already in the
  2394. buffer.
  2395. The value of this variable is an alist, the car of each entry must be a
  2396. keyword as a string, the cdr may be a character that is used to select
  2397. that tag through the fast-tag-selection interface.
  2398. See the manual for details."
  2399. :group 'org-tags
  2400. :type '(repeat
  2401. (choice
  2402. (cons (string :tag "Tag name")
  2403. (character :tag "Access char"))
  2404. (list :tag "Start radio group"
  2405. (const :startgroup)
  2406. (option (string :tag "Group description")))
  2407. (list :tag "End radio group"
  2408. (const :endgroup)
  2409. (option (string :tag "Group description")))
  2410. (const :tag "New line" (:newline)))))
  2411. (defcustom org-tag-persistent-alist nil
  2412. "List of tags that will always appear in all Org-mode files.
  2413. This is in addition to any in buffer settings or customizations
  2414. of `org-tag-alist'.
  2415. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2416. The value of this variable is an alist, the car of each entry must be a
  2417. keyword as a string, the cdr may be a character that is used to select
  2418. that tag through the fast-tag-selection interface.
  2419. See the manual for details.
  2420. To disable these tags on a per-file basis, insert anywhere in the file:
  2421. #+STARTUP: noptag"
  2422. :group 'org-tags
  2423. :type '(repeat
  2424. (choice
  2425. (cons (string :tag "Tag name")
  2426. (character :tag "Access char"))
  2427. (const :tag "Start radio group" (:startgroup))
  2428. (const :tag "End radio group" (:endgroup))
  2429. (const :tag "New line" (:newline)))))
  2430. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2431. "If non-nil, always offer completion for all tags of all agenda files.
  2432. Instead of customizing this variable directly, you might want to
  2433. set it locally for capture buffers, because there no list of
  2434. tags in that file can be created dynamically (there are none).
  2435. (add-hook 'org-capture-mode-hook
  2436. (lambda ()
  2437. (set (make-local-variable
  2438. 'org-complete-tags-always-offer-all-agenda-tags)
  2439. t)))"
  2440. :group 'org-tags
  2441. :type 'boolean)
  2442. (defvar org-file-tags nil
  2443. "List of tags that can be inherited by all entries in the file.
  2444. The tags will be inherited if the variable `org-use-tag-inheritance'
  2445. says they should be.
  2446. This variable is populated from #+FILETAGS lines.")
  2447. (defcustom org-use-fast-tag-selection 'auto
  2448. "Non-nil means use fast tag selection scheme.
  2449. This is a special interface to select and deselect tags with single keys.
  2450. When nil, fast selection is never used.
  2451. When the symbol `auto', fast selection is used if and only if selection
  2452. characters for tags have been configured, either through the variable
  2453. `org-tag-alist' or through a #+TAGS line in the buffer.
  2454. When t, fast selection is always used and selection keys are assigned
  2455. automatically if necessary."
  2456. :group 'org-tags
  2457. :type '(choice
  2458. (const :tag "Always" t)
  2459. (const :tag "Never" nil)
  2460. (const :tag "When selection characters are configured" 'auto)))
  2461. (defcustom org-fast-tag-selection-single-key nil
  2462. "Non-nil means fast tag selection exits after first change.
  2463. When nil, you have to press RET to exit it.
  2464. During fast tag selection, you can toggle this flag with `C-c'.
  2465. This variable can also have the value `expert'. In this case, the window
  2466. displaying the tags menu is not even shown, until you press C-c again."
  2467. :group 'org-tags
  2468. :type '(choice
  2469. (const :tag "No" nil)
  2470. (const :tag "Yes" t)
  2471. (const :tag "Expert" expert)))
  2472. (defvar org-fast-tag-selection-include-todo nil
  2473. "Non-nil means fast tags selection interface will also offer TODO states.
  2474. This is an undocumented feature, you should not rely on it.")
  2475. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2476. "The column to which tags should be indented in a headline.
  2477. If this number is positive, it specifies the column. If it is negative,
  2478. it means that the tags should be flushright to that column. For example,
  2479. -80 works well for a normal 80 character screen."
  2480. :group 'org-tags
  2481. :type 'integer)
  2482. (defcustom org-auto-align-tags t
  2483. "Non-nil keeps tags aligned when modifying headlines.
  2484. Some operations (i.e. demoting) change the length of a headline and
  2485. therefore shift the tags around. With this option turned on, after
  2486. each such operation the tags are again aligned to `org-tags-column'."
  2487. :group 'org-tags
  2488. :type 'boolean)
  2489. (defcustom org-use-tag-inheritance t
  2490. "Non-nil means tags in levels apply also for sublevels.
  2491. When nil, only the tags directly given in a specific line apply there.
  2492. This may also be a list of tags that should be inherited, or a regexp that
  2493. matches tags that should be inherited. Additional control is possible
  2494. with the variable `org-tags-exclude-from-inheritance' which gives an
  2495. explicit list of tags to be excluded from inheritance., even if the value of
  2496. `org-use-tag-inheritance' would select it for inheritance.
  2497. If this option is t, a match early-on in a tree can lead to a large
  2498. number of matches in the subtree when constructing the agenda or creating
  2499. a sparse tree. If you only want to see the first match in a tree during
  2500. a search, check out the variable `org-tags-match-list-sublevels'."
  2501. :group 'org-tags
  2502. :type '(choice
  2503. (const :tag "Not" nil)
  2504. (const :tag "Always" t)
  2505. (repeat :tag "Specific tags" (string :tag "Tag"))
  2506. (regexp :tag "Tags matched by regexp")))
  2507. (defcustom org-tags-exclude-from-inheritance nil
  2508. "List of tags that should never be inherited.
  2509. This is a way to exclude a few tags from inheritance. For way to do
  2510. the opposite, to actively allow inheritance for selected tags,
  2511. see the variable `org-use-tag-inheritance'."
  2512. :group 'org-tags
  2513. :type '(repeat (string :tag "Tag")))
  2514. (defun org-tag-inherit-p (tag)
  2515. "Check if TAG is one that should be inherited."
  2516. (cond
  2517. ((member tag org-tags-exclude-from-inheritance) nil)
  2518. ((eq org-use-tag-inheritance t) t)
  2519. ((not org-use-tag-inheritance) nil)
  2520. ((stringp org-use-tag-inheritance)
  2521. (string-match org-use-tag-inheritance tag))
  2522. ((listp org-use-tag-inheritance)
  2523. (member tag org-use-tag-inheritance))
  2524. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2525. (defcustom org-tags-match-list-sublevels t
  2526. "Non-nil means list also sublevels of headlines matching a search.
  2527. This variable applies to tags/property searches, and also to stuck
  2528. projects because this search is based on a tags match as well.
  2529. When set to the symbol `indented', sublevels are indented with
  2530. leading dots.
  2531. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2532. the sublevels of a headline matching a tag search often also match
  2533. the same search. Listing all of them can create very long lists.
  2534. Setting this variable to nil causes subtrees of a match to be skipped.
  2535. This variable is semi-obsolete and probably should always be true. It
  2536. is better to limit inheritance to certain tags using the variables
  2537. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2538. :group 'org-tags
  2539. :type '(choice
  2540. (const :tag "No, don't list them" nil)
  2541. (const :tag "Yes, do list them" t)
  2542. (const :tag "List them, indented with leading dots" indented)))
  2543. (defcustom org-tags-sort-function nil
  2544. "When set, tags are sorted using this function as a comparator."
  2545. :group 'org-tags
  2546. :type '(choice
  2547. (const :tag "No sorting" nil)
  2548. (const :tag "Alphabetical" string<)
  2549. (const :tag "Reverse alphabetical" string>)
  2550. (function :tag "Custom function" nil)))
  2551. (defvar org-tags-history nil
  2552. "History of minibuffer reads for tags.")
  2553. (defvar org-last-tags-completion-table nil
  2554. "The last used completion table for tags.")
  2555. (defvar org-after-tags-change-hook nil
  2556. "Hook that is run after the tags in a line have changed.")
  2557. (defgroup org-properties nil
  2558. "Options concerning properties in Org-mode."
  2559. :tag "Org Properties"
  2560. :group 'org)
  2561. (defcustom org-property-format "%-10s %s"
  2562. "How property key/value pairs should be formatted by `indent-line'.
  2563. When `indent-line' hits a property definition, it will format the line
  2564. according to this format, mainly to make sure that the values are
  2565. lined-up with respect to each other."
  2566. :group 'org-properties
  2567. :type 'string)
  2568. (defcustom org-use-property-inheritance nil
  2569. "Non-nil means properties apply also for sublevels.
  2570. This setting is chiefly used during property searches. Turning it on can
  2571. cause significant overhead when doing a search, which is why it is not
  2572. on by default.
  2573. When nil, only the properties directly given in the current entry count.
  2574. When t, every property is inherited. The value may also be a list of
  2575. properties that should have inheritance, or a regular expression matching
  2576. properties that should be inherited.
  2577. However, note that some special properties use inheritance under special
  2578. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2579. and the properties ending in \"_ALL\" when they are used as descriptor
  2580. for valid values of a property.
  2581. Note for programmers:
  2582. When querying an entry with `org-entry-get', you can control if inheritance
  2583. should be used. By default, `org-entry-get' looks only at the local
  2584. properties. You can request inheritance by setting the inherit argument
  2585. to t (to force inheritance) or to `selective' (to respect the setting
  2586. in this variable)."
  2587. :group 'org-properties
  2588. :type '(choice
  2589. (const :tag "Not" nil)
  2590. (const :tag "Always" t)
  2591. (repeat :tag "Specific properties" (string :tag "Property"))
  2592. (regexp :tag "Properties matched by regexp")))
  2593. (defun org-property-inherit-p (property)
  2594. "Check if PROPERTY is one that should be inherited."
  2595. (cond
  2596. ((eq org-use-property-inheritance t) t)
  2597. ((not org-use-property-inheritance) nil)
  2598. ((stringp org-use-property-inheritance)
  2599. (string-match org-use-property-inheritance property))
  2600. ((listp org-use-property-inheritance)
  2601. (member property org-use-property-inheritance))
  2602. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2603. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2604. "The default column format, if no other format has been defined.
  2605. This variable can be set on the per-file basis by inserting a line
  2606. #+COLUMNS: %25ITEM ....."
  2607. :group 'org-properties
  2608. :type 'string)
  2609. (defcustom org-columns-ellipses ".."
  2610. "The ellipses to be used when a field in column view is truncated.
  2611. When this is the empty string, as many characters as possible are shown,
  2612. but then there will be no visual indication that the field has been truncated.
  2613. When this is a string of length N, the last N characters of a truncated
  2614. field are replaced by this string. If the column is narrower than the
  2615. ellipses string, only part of the ellipses string will be shown."
  2616. :group 'org-properties
  2617. :type 'string)
  2618. (defcustom org-columns-modify-value-for-display-function nil
  2619. "Function that modifies values for display in column view.
  2620. For example, it can be used to cut out a certain part from a time stamp.
  2621. The function must take 2 arguments:
  2622. column-title The title of the column (*not* the property name)
  2623. value The value that should be modified.
  2624. The function should return the value that should be displayed,
  2625. or nil if the normal value should be used."
  2626. :group 'org-properties
  2627. :type 'function)
  2628. (defcustom org-effort-property "Effort"
  2629. "The property that is being used to keep track of effort estimates.
  2630. Effort estimates given in this property need to have the format H:MM."
  2631. :group 'org-properties
  2632. :group 'org-progress
  2633. :type '(string :tag "Property"))
  2634. (defconst org-global-properties-fixed
  2635. '(("VISIBILITY_ALL" . "folded children content all")
  2636. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2637. "List of property/value pairs that can be inherited by any entry.
  2638. These are fixed values, for the preset properties. The user variable
  2639. that can be used to add to this list is `org-global-properties'.
  2640. The entries in this list are cons cells where the car is a property
  2641. name and cdr is a string with the value. If the value represents
  2642. multiple items like an \"_ALL\" property, separate the items by
  2643. spaces.")
  2644. (defcustom org-global-properties nil
  2645. "List of property/value pairs that can be inherited by any entry.
  2646. This list will be combined with the constant `org-global-properties-fixed'.
  2647. The entries in this list are cons cells where the car is a property
  2648. name and cdr is a string with the value.
  2649. You can set buffer-local values for the same purpose in the variable
  2650. `org-file-properties' this by adding lines like
  2651. #+PROPERTY: NAME VALUE"
  2652. :group 'org-properties
  2653. :type '(repeat
  2654. (cons (string :tag "Property")
  2655. (string :tag "Value"))))
  2656. (defvar org-file-properties nil
  2657. "List of property/value pairs that can be inherited by any entry.
  2658. Valid for the current buffer.
  2659. This variable is populated from #+PROPERTY lines.")
  2660. (make-variable-buffer-local 'org-file-properties)
  2661. (defgroup org-agenda nil
  2662. "Options concerning agenda views in Org-mode."
  2663. :tag "Org Agenda"
  2664. :group 'org)
  2665. (defvar org-category nil
  2666. "Variable used by org files to set a category for agenda display.
  2667. Such files should use a file variable to set it, for example
  2668. # -*- mode: org; org-category: \"ELisp\"
  2669. or contain a special line
  2670. #+CATEGORY: ELisp
  2671. If the file does not specify a category, then file's base name
  2672. is used instead.")
  2673. (make-variable-buffer-local 'org-category)
  2674. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2675. (defcustom org-agenda-files nil
  2676. "The files to be used for agenda display.
  2677. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2678. \\[org-remove-file]. You can also use customize to edit the list.
  2679. If an entry is a directory, all files in that directory that are matched by
  2680. `org-agenda-file-regexp' will be part of the file list.
  2681. If the value of the variable is not a list but a single file name, then
  2682. the list of agenda files is actually stored and maintained in that file, one
  2683. agenda file per line. In this file paths can be given relative to
  2684. `org-directory'. Tilde expansion and environment variable substitution
  2685. are also made."
  2686. :group 'org-agenda
  2687. :type '(choice
  2688. (repeat :tag "List of files and directories" file)
  2689. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2690. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2691. "Regular expression to match files for `org-agenda-files'.
  2692. If any element in the list in that variable contains a directory instead
  2693. of a normal file, all files in that directory that are matched by this
  2694. regular expression will be included."
  2695. :group 'org-agenda
  2696. :type 'regexp)
  2697. (defcustom org-agenda-text-search-extra-files nil
  2698. "List of extra files to be searched by text search commands.
  2699. These files will be search in addition to the agenda files by the
  2700. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2701. Note that these files will only be searched for text search commands,
  2702. not for the other agenda views like todo lists, tag searches or the weekly
  2703. agenda. This variable is intended to list notes and possibly archive files
  2704. that should also be searched by these two commands.
  2705. In fact, if the first element in the list is the symbol `agenda-archives',
  2706. than all archive files of all agenda files will be added to the search
  2707. scope."
  2708. :group 'org-agenda
  2709. :type '(set :greedy t
  2710. (const :tag "Agenda Archives" agenda-archives)
  2711. (repeat :inline t (file))))
  2712. (if (fboundp 'defvaralias)
  2713. (defvaralias 'org-agenda-multi-occur-extra-files
  2714. 'org-agenda-text-search-extra-files))
  2715. (defcustom org-agenda-skip-unavailable-files nil
  2716. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2717. A nil value means to remove them, after a query, from the list."
  2718. :group 'org-agenda
  2719. :type 'boolean)
  2720. (defcustom org-calendar-to-agenda-key [?c]
  2721. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2722. The command `org-calendar-goto-agenda' will be bound to this key. The
  2723. default is the character `c' because then `c' can be used to switch back and
  2724. forth between agenda and calendar."
  2725. :group 'org-agenda
  2726. :type 'sexp)
  2727. (defcustom org-calendar-agenda-action-key [?k]
  2728. "The key to be installed in `calendar-mode-map' for agenda-action.
  2729. The command `org-agenda-action' will be bound to this key. The
  2730. default is the character `k' because we use the same key in the agenda."
  2731. :group 'org-agenda
  2732. :type 'sexp)
  2733. (defcustom org-calendar-insert-diary-entry-key [?i]
  2734. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2735. This option is irrelevant until `org-agenda-diary-file' has been configured
  2736. to point to an Org-mode file. When that is the case, the command
  2737. `org-agenda-diary-entry' will be bound to the key given here, by default
  2738. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2739. if you want to continue doing this, you need to change this to a different
  2740. key."
  2741. :group 'org-agenda
  2742. :type 'sexp)
  2743. (defcustom org-agenda-diary-file 'diary-file
  2744. "File to which to add new entries with the `i' key in agenda and calendar.
  2745. When this is the symbol `diary-file', the functionality in the Emacs
  2746. calendar will be used to add entries to the `diary-file'. But when this
  2747. points to a file, `org-agenda-diary-entry' will be used instead."
  2748. :group 'org-agenda
  2749. :type '(choice
  2750. (const :tag "The standard Emacs diary file" diary-file)
  2751. (file :tag "Special Org file diary entries")))
  2752. (eval-after-load "calendar"
  2753. '(progn
  2754. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2755. 'org-calendar-goto-agenda)
  2756. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2757. 'org-agenda-action)
  2758. (add-hook 'calendar-mode-hook
  2759. (lambda ()
  2760. (unless (eq org-agenda-diary-file 'diary-file)
  2761. (define-key calendar-mode-map
  2762. org-calendar-insert-diary-entry-key
  2763. 'org-agenda-diary-entry))))))
  2764. (defgroup org-latex nil
  2765. "Options for embedding LaTeX code into Org-mode."
  2766. :tag "Org LaTeX"
  2767. :group 'org)
  2768. (defcustom org-format-latex-options
  2769. '(:foreground default :background default :scale 1.0
  2770. :html-foreground "Black" :html-background "Transparent"
  2771. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2772. "Options for creating images from LaTeX fragments.
  2773. This is a property list with the following properties:
  2774. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2775. `default' means use the foreground of the default face.
  2776. :background the background color, or \"Transparent\".
  2777. `default' means use the background of the default face.
  2778. :scale a scaling factor for the size of the images, to get more pixels
  2779. :html-foreground, :html-background, :html-scale
  2780. the same numbers for HTML export.
  2781. :matchers a list indicating which matchers should be used to
  2782. find LaTeX fragments. Valid members of this list are:
  2783. \"begin\" find environments
  2784. \"$1\" find single characters surrounded by $.$
  2785. \"$\" find math expressions surrounded by $...$
  2786. \"$$\" find math expressions surrounded by $$....$$
  2787. \"\\(\" find math expressions surrounded by \\(...\\)
  2788. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2789. :group 'org-latex
  2790. :type 'plist)
  2791. (defcustom org-format-latex-signal-error t
  2792. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2793. When nil, just push out a message."
  2794. :group 'org-latex
  2795. :type 'boolean)
  2796. (defcustom org-format-latex-header "\\documentclass{article}
  2797. \\usepackage[usenames]{color}
  2798. \\usepackage{amsmath}
  2799. \\usepackage[mathscr]{eucal}
  2800. \\pagestyle{empty} % do not remove
  2801. \[PACKAGES]
  2802. \[DEFAULT-PACKAGES]
  2803. % The settings below are copied from fullpage.sty
  2804. \\setlength{\\textwidth}{\\paperwidth}
  2805. \\addtolength{\\textwidth}{-3cm}
  2806. \\setlength{\\oddsidemargin}{1.5cm}
  2807. \\addtolength{\\oddsidemargin}{-2.54cm}
  2808. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2809. \\setlength{\\textheight}{\\paperheight}
  2810. \\addtolength{\\textheight}{-\\headheight}
  2811. \\addtolength{\\textheight}{-\\headsep}
  2812. \\addtolength{\\textheight}{-\\footskip}
  2813. \\addtolength{\\textheight}{-3cm}
  2814. \\setlength{\\topmargin}{1.5cm}
  2815. \\addtolength{\\topmargin}{-2.54cm}"
  2816. "The document header used for processing LaTeX fragments.
  2817. It is imperative that this header make sure that no page number
  2818. appears on the page. The package defined in the variables
  2819. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2820. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2821. will be appended."
  2822. :group 'org-latex
  2823. :type 'string)
  2824. (defvar org-format-latex-header-extra nil)
  2825. (defun org-set-packages-alist (var val)
  2826. "Set the packages alist and make sure it has 3 elements per entry."
  2827. (set var (mapcar (lambda (x)
  2828. (if (and (consp x) (= (length x) 2))
  2829. (list (car x) (nth 1 x) t)
  2830. x))
  2831. val)))
  2832. (defun org-get-packages-alist (var)
  2833. "Get the packages alist and make sure it has 3 elements per entry."
  2834. (mapcar (lambda (x)
  2835. (if (and (consp x) (= (length x) 2))
  2836. (list (car x) (nth 1 x) t)
  2837. x))
  2838. (default-value var)))
  2839. ;; The following variables are defined here because is it also used
  2840. ;; when formatting latex fragments. Originally it was part of the
  2841. ;; LaTeX exporter, which is why the name includes "export".
  2842. (defcustom org-export-latex-default-packages-alist
  2843. '(("AUTO" "inputenc" t)
  2844. ("T1" "fontenc" t)
  2845. ("" "fixltx2e" nil)
  2846. ("" "graphicx" t)
  2847. ("" "longtable" nil)
  2848. ("" "float" nil)
  2849. ("" "wrapfig" nil)
  2850. ("" "soul" t)
  2851. ("" "textcomp" t)
  2852. ("" "marvosym" t)
  2853. ("" "wasysym" t)
  2854. ("" "latexsym" t)
  2855. ("" "amssymb" t)
  2856. ("" "hyperref" nil)
  2857. "\\tolerance=1000"
  2858. )
  2859. "Alist of default packages to be inserted in the header.
  2860. Change this only if one of the packages here causes an incompatibility
  2861. with another package you are using.
  2862. The packages in this list are needed by one part or another of Org-mode
  2863. to function properly.
  2864. - inputenc, fontenc: for basic font and character selection
  2865. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2866. for interpreting the entities in `org-entities'. You can skip some of these
  2867. packages if you don't use any of the symbols in it.
  2868. - graphicx: for including images
  2869. - float, wrapfig: for figure placement
  2870. - longtable: for long tables
  2871. - hyperref: for cross references
  2872. Therefore you should not modify this variable unless you know what you
  2873. are doing. The one reason to change it anyway is that you might be loading
  2874. some other package that conflicts with one of the default packages.
  2875. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2876. If SNIPPET-FLAG is t, the package also needs to be included when
  2877. compiling LaTeX snippets into images for inclusion into HTML."
  2878. :group 'org-export-latex
  2879. :set 'org-set-packages-alist
  2880. :get 'org-get-packages-alist
  2881. :type '(repeat
  2882. (choice
  2883. (list :tag "options/package pair"
  2884. (string :tag "options")
  2885. (string :tag "package")
  2886. (boolean :tag "Snippet"))
  2887. (string :tag "A line of LaTeX"))))
  2888. (defcustom org-export-latex-packages-alist nil
  2889. "Alist of packages to be inserted in every LaTeX header.
  2890. These will be inserted after `org-export-latex-default-packages-alist'.
  2891. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2892. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2893. turning LaTeX snippets into images for inclusion into HTML.
  2894. Make sure that you only list packages here which:
  2895. - you want in every file
  2896. - do not conflict with the default packages in
  2897. `org-export-latex-default-packages-alist'
  2898. - do not conflict with the setup in `org-format-latex-header'."
  2899. :group 'org-export-latex
  2900. :set 'org-set-packages-alist
  2901. :get 'org-get-packages-alist
  2902. :type '(repeat
  2903. (choice
  2904. (list :tag "options/package pair"
  2905. (string :tag "options")
  2906. (string :tag "package")
  2907. (boolean :tag "Snippet"))
  2908. (string :tag "A line of LaTeX"))))
  2909. (defgroup org-appearance nil
  2910. "Settings for Org-mode appearance."
  2911. :tag "Org Appearance"
  2912. :group 'org)
  2913. (defcustom org-level-color-stars-only nil
  2914. "Non-nil means fontify only the stars in each headline.
  2915. When nil, the entire headline is fontified.
  2916. Changing it requires restart of `font-lock-mode' to become effective
  2917. also in regions already fontified."
  2918. :group 'org-appearance
  2919. :type 'boolean)
  2920. (defcustom org-hide-leading-stars nil
  2921. "Non-nil means hide the first N-1 stars in a headline.
  2922. This works by using the face `org-hide' for these stars. This
  2923. face is white for a light background, and black for a dark
  2924. background. You may have to customize the face `org-hide' to
  2925. make this work.
  2926. Changing it requires restart of `font-lock-mode' to become effective
  2927. also in regions already fontified.
  2928. You may also set this on a per-file basis by adding one of the following
  2929. lines to the buffer:
  2930. #+STARTUP: hidestars
  2931. #+STARTUP: showstars"
  2932. :group 'org-appearance
  2933. :type 'boolean)
  2934. (defcustom org-hidden-keywords nil
  2935. "List of symbols corresponding to keywords to be hidden the org buffer.
  2936. For example, a value '(title) for this list will make the document's title
  2937. appear in the buffer without the initial #+TITLE: keyword."
  2938. :group 'org-appearance
  2939. :type '(set (const :tag "#+AUTHOR" author)
  2940. (const :tag "#+DATE" date)
  2941. (const :tag "#+EMAIL" email)
  2942. (const :tag "#+TITLE" title)))
  2943. (defcustom org-fontify-done-headline nil
  2944. "Non-nil means change the face of a headline if it is marked DONE.
  2945. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2946. When this is non-nil, the headline after the keyword is set to the
  2947. `org-headline-done' as an additional indication."
  2948. :group 'org-appearance
  2949. :type 'boolean)
  2950. (defcustom org-fontify-emphasized-text t
  2951. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2952. Changing this variable requires a restart of Emacs to take effect."
  2953. :group 'org-appearance
  2954. :type 'boolean)
  2955. (defcustom org-fontify-whole-heading-line nil
  2956. "Non-nil means fontify the whole line for headings.
  2957. This is useful when setting a background color for the
  2958. org-level-* faces."
  2959. :group 'org-appearance
  2960. :type 'boolean)
  2961. (defcustom org-highlight-latex-fragments-and-specials nil
  2962. "Non-nil means fontify what is treated specially by the exporters."
  2963. :group 'org-appearance
  2964. :type 'boolean)
  2965. (defcustom org-hide-emphasis-markers nil
  2966. "Non-nil mean font-lock should hide the emphasis marker characters."
  2967. :group 'org-appearance
  2968. :type 'boolean)
  2969. (defcustom org-pretty-entities nil
  2970. "Non-nil means show entities as UTF8 characters.
  2971. When nil, the \\name form remains in the buffer."
  2972. :group 'org-appearance
  2973. :type 'boolean)
  2974. (defcustom org-pretty-entities-include-sub-superscripts t
  2975. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  2976. :group 'org-appearance
  2977. :type 'boolean)
  2978. (defvar org-emph-re nil
  2979. "Regular expression for matching emphasis.
  2980. After a match, the match groups contain these elements:
  2981. 0 The match of the full regular expression, including the characters
  2982. before and after the proper match
  2983. 1 The character before the proper match, or empty at beginning of line
  2984. 2 The proper match, including the leading and trailing markers
  2985. 3 The leading marker like * or /, indicating the type of highlighting
  2986. 4 The text between the emphasis markers, not including the markers
  2987. 5 The character after the match, empty at the end of a line")
  2988. (defvar org-verbatim-re nil
  2989. "Regular expression for matching verbatim text.")
  2990. (defvar org-emphasis-regexp-components) ; defined just below
  2991. (defvar org-emphasis-alist) ; defined just below
  2992. (defun org-set-emph-re (var val)
  2993. "Set variable and compute the emphasis regular expression."
  2994. (set var val)
  2995. (when (and (boundp 'org-emphasis-alist)
  2996. (boundp 'org-emphasis-regexp-components)
  2997. org-emphasis-alist org-emphasis-regexp-components)
  2998. (let* ((e org-emphasis-regexp-components)
  2999. (pre (car e))
  3000. (post (nth 1 e))
  3001. (border (nth 2 e))
  3002. (body (nth 3 e))
  3003. (nl (nth 4 e))
  3004. (body1 (concat body "*?"))
  3005. (markers (mapconcat 'car org-emphasis-alist ""))
  3006. (vmarkers (mapconcat
  3007. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3008. org-emphasis-alist "")))
  3009. ;; make sure special characters appear at the right position in the class
  3010. (if (string-match "\\^" markers)
  3011. (setq markers (concat (replace-match "" t t markers) "^")))
  3012. (if (string-match "-" markers)
  3013. (setq markers (concat (replace-match "" t t markers) "-")))
  3014. (if (string-match "\\^" vmarkers)
  3015. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3016. (if (string-match "-" vmarkers)
  3017. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3018. (if (> nl 0)
  3019. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3020. (int-to-string nl) "\\}")))
  3021. ;; Make the regexp
  3022. (setq org-emph-re
  3023. (concat "\\([" pre "]\\|^\\)"
  3024. "\\("
  3025. "\\([" markers "]\\)"
  3026. "\\("
  3027. "[^" border "]\\|"
  3028. "[^" border "]"
  3029. body1
  3030. "[^" border "]"
  3031. "\\)"
  3032. "\\3\\)"
  3033. "\\([" post "]\\|$\\)"))
  3034. (setq org-verbatim-re
  3035. (concat "\\([" pre "]\\|^\\)"
  3036. "\\("
  3037. "\\([" vmarkers "]\\)"
  3038. "\\("
  3039. "[^" border "]\\|"
  3040. "[^" border "]"
  3041. body1
  3042. "[^" border "]"
  3043. "\\)"
  3044. "\\3\\)"
  3045. "\\([" post "]\\|$\\)")))))
  3046. (defcustom org-emphasis-regexp-components
  3047. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3048. "Components used to build the regular expression for emphasis.
  3049. This is a list with five entries. Terminology: In an emphasis string
  3050. like \" *strong word* \", we call the initial space PREMATCH, the final
  3051. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3052. and \"trong wor\" is the body. The different components in this variable
  3053. specify what is allowed/forbidden in each part:
  3054. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3055. post Chars allowed as postmatch. End of line will be allowed too.
  3056. border The chars *forbidden* as border characters.
  3057. body-regexp A regexp like \".\" to match a body character. Don't use
  3058. non-shy groups here, and don't allow newline here.
  3059. newline The maximum number of newlines allowed in an emphasis exp.
  3060. Use customize to modify this, or restart Emacs after changing it."
  3061. :group 'org-appearance
  3062. :set 'org-set-emph-re
  3063. :type '(list
  3064. (sexp :tag "Allowed chars in pre ")
  3065. (sexp :tag "Allowed chars in post ")
  3066. (sexp :tag "Forbidden chars in border ")
  3067. (sexp :tag "Regexp for body ")
  3068. (integer :tag "number of newlines allowed")
  3069. (option (boolean :tag "Please ignore this button"))))
  3070. (defcustom org-emphasis-alist
  3071. `(("*" bold "<b>" "</b>")
  3072. ("/" italic "<i>" "</i>")
  3073. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3074. ("=" org-code "<code>" "</code>" verbatim)
  3075. ("~" org-verbatim "<code>" "</code>" verbatim)
  3076. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3077. "<del>" "</del>")
  3078. )
  3079. "Special syntax for emphasized text.
  3080. Text starting and ending with a special character will be emphasized, for
  3081. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3082. characters, the face to be used by font-lock for highlighting in Org-mode
  3083. Emacs buffers, and the HTML tags to be used for this.
  3084. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3085. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3086. Use customize to modify this, or restart Emacs after changing it."
  3087. :group 'org-appearance
  3088. :set 'org-set-emph-re
  3089. :type '(repeat
  3090. (list
  3091. (string :tag "Marker character")
  3092. (choice
  3093. (face :tag "Font-lock-face")
  3094. (plist :tag "Face property list"))
  3095. (string :tag "HTML start tag")
  3096. (string :tag "HTML end tag")
  3097. (option (const verbatim)))))
  3098. (defvar org-protecting-blocks
  3099. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3100. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3101. This is needed for font-lock setup.")
  3102. ;;; Miscellaneous options
  3103. (defgroup org-completion nil
  3104. "Completion in Org-mode."
  3105. :tag "Org Completion"
  3106. :group 'org)
  3107. (defcustom org-completion-use-ido nil
  3108. "Non-nil means use ido completion wherever possible.
  3109. Note that `ido-mode' must be active for this variable to be relevant.
  3110. If you decide to turn this variable on, you might well want to turn off
  3111. `org-outline-path-complete-in-steps'.
  3112. See also `org-completion-use-iswitchb'."
  3113. :group 'org-completion
  3114. :type 'boolean)
  3115. (defcustom org-completion-use-iswitchb nil
  3116. "Non-nil means use iswitchb completion wherever possible.
  3117. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3118. If you decide to turn this variable on, you might well want to turn off
  3119. `org-outline-path-complete-in-steps'.
  3120. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3121. :group 'org-completion
  3122. :type 'boolean)
  3123. (defcustom org-completion-fallback-command 'hippie-expand
  3124. "The expansion command called by \\[pcomplete] in normal context.
  3125. Normal means, no org-mode-specific context."
  3126. :group 'org-completion
  3127. :type 'function)
  3128. ;;; Functions and variables from their packages
  3129. ;; Declared here to avoid compiler warnings
  3130. ;; XEmacs only
  3131. (defvar outline-mode-menu-heading)
  3132. (defvar outline-mode-menu-show)
  3133. (defvar outline-mode-menu-hide)
  3134. (defvar zmacs-regions) ; XEmacs regions
  3135. ;; Emacs only
  3136. (defvar mark-active)
  3137. ;; Various packages
  3138. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3139. (declare-function calendar-forward-day "cal-move" (arg))
  3140. (declare-function calendar-goto-date "cal-move" (date))
  3141. (declare-function calendar-goto-today "cal-move" ())
  3142. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3143. (defvar calc-embedded-close-formula)
  3144. (defvar calc-embedded-open-formula)
  3145. (declare-function cdlatex-tab "ext:cdlatex" ())
  3146. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3147. (defvar font-lock-unfontify-region-function)
  3148. (declare-function iswitchb-read-buffer "iswitchb"
  3149. (prompt &optional default require-match start matches-set))
  3150. (defvar iswitchb-temp-buflist)
  3151. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3152. (defvar org-agenda-tags-todo-honor-ignore-options)
  3153. (declare-function org-agenda-skip "org-agenda" ())
  3154. (declare-function
  3155. org-format-agenda-item "org-agenda"
  3156. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3157. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3158. (declare-function org-agenda-change-all-lines "org-agenda"
  3159. (newhead hdmarker &optional fixface just-this))
  3160. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3161. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3162. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3163. (beg end))
  3164. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3165. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3166. "org-agenda" (&optional end))
  3167. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3168. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3169. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3170. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3171. (declare-function org-indent-mode "org-indent" (&optional arg))
  3172. (declare-function parse-time-string "parse-time" (string))
  3173. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3174. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3175. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3176. (defvar remember-data-file)
  3177. (defvar texmathp-why)
  3178. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3179. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3180. (defvar w3m-current-url)
  3181. (defvar w3m-current-title)
  3182. (defvar org-latex-regexps)
  3183. ;;; Autoload and prepare some org modules
  3184. ;; Some table stuff that needs to be defined here, because it is used
  3185. ;; by the functions setting up org-mode or checking for table context.
  3186. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3187. "Detect an org-type or table-type table.")
  3188. (defconst org-table-line-regexp "^[ \t]*|"
  3189. "Detect an org-type table line.")
  3190. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3191. "Detect an org-type table line.")
  3192. (defconst org-table-hline-regexp "^[ \t]*|-"
  3193. "Detect an org-type table hline.")
  3194. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3195. "Detect a table-type table hline.")
  3196. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3197. "Detect the first line outside a table when searching from within it.
  3198. This works for both table types.")
  3199. ;; Autoload the functions in org-table.el that are needed by functions here.
  3200. (eval-and-compile
  3201. (org-autoload "org-table"
  3202. '(org-table-align org-table-begin org-table-blank-field
  3203. org-table-convert org-table-convert-region org-table-copy-down
  3204. org-table-copy-region org-table-create
  3205. org-table-create-or-convert-from-region
  3206. org-table-create-with-table.el org-table-current-dline
  3207. org-table-cut-region org-table-delete-column org-table-edit-field
  3208. org-table-edit-formulas org-table-end org-table-eval-formula
  3209. org-table-export org-table-field-info
  3210. org-table-get-stored-formulas org-table-goto-column
  3211. org-table-hline-and-move org-table-import org-table-insert-column
  3212. org-table-insert-hline org-table-insert-row org-table-iterate
  3213. org-table-justify-field-maybe org-table-kill-row
  3214. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3215. org-table-move-column org-table-move-column-left
  3216. org-table-move-column-right org-table-move-row
  3217. org-table-move-row-down org-table-move-row-up
  3218. org-table-next-field org-table-next-row org-table-paste-rectangle
  3219. org-table-previous-field org-table-recalculate
  3220. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3221. org-table-toggle-coordinate-overlays
  3222. org-table-toggle-formula-debugger org-table-wrap-region
  3223. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3224. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3225. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3226. (defun org-at-table-p (&optional table-type)
  3227. "Return t if the cursor is inside an org-type table.
  3228. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3229. (if org-enable-table-editor
  3230. (save-excursion
  3231. (beginning-of-line 1)
  3232. (looking-at (if table-type org-table-any-line-regexp
  3233. org-table-line-regexp)))
  3234. nil))
  3235. (defsubst org-table-p () (org-at-table-p))
  3236. (defun org-at-table.el-p ()
  3237. "Return t if and only if we are at a table.el table."
  3238. (and (org-at-table-p 'any)
  3239. (save-excursion
  3240. (goto-char (org-table-begin 'any))
  3241. (looking-at org-table1-hline-regexp))))
  3242. (defun org-table-recognize-table.el ()
  3243. "If there is a table.el table nearby, recognize it and move into it."
  3244. (if org-table-tab-recognizes-table.el
  3245. (if (org-at-table.el-p)
  3246. (progn
  3247. (beginning-of-line 1)
  3248. (if (looking-at org-table-dataline-regexp)
  3249. nil
  3250. (if (looking-at org-table1-hline-regexp)
  3251. (progn
  3252. (beginning-of-line 2)
  3253. (if (looking-at org-table-any-border-regexp)
  3254. (beginning-of-line -1)))))
  3255. (if (re-search-forward "|" (org-table-end t) t)
  3256. (progn
  3257. (require 'table)
  3258. (if (table--at-cell-p (point))
  3259. t
  3260. (message "recognizing table.el table...")
  3261. (table-recognize-table)
  3262. (message "recognizing table.el table...done")))
  3263. (error "This should not happen"))
  3264. t)
  3265. nil)
  3266. nil))
  3267. (defun org-at-table-hline-p ()
  3268. "Return t if the cursor is inside a hline in a table."
  3269. (if org-enable-table-editor
  3270. (save-excursion
  3271. (beginning-of-line 1)
  3272. (looking-at org-table-hline-regexp))
  3273. nil))
  3274. (defvar org-table-clean-did-remove-column nil)
  3275. (defun org-table-map-tables (function &optional quietly)
  3276. "Apply FUNCTION to the start of all tables in the buffer."
  3277. (save-excursion
  3278. (save-restriction
  3279. (widen)
  3280. (goto-char (point-min))
  3281. (while (re-search-forward org-table-any-line-regexp nil t)
  3282. (unless quietly
  3283. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3284. (beginning-of-line 1)
  3285. (when (looking-at org-table-line-regexp)
  3286. (save-excursion (funcall function))
  3287. (or (looking-at org-table-line-regexp)
  3288. (forward-char 1)))
  3289. (re-search-forward org-table-any-border-regexp nil 1))))
  3290. (unless quietly (message "Mapping tables: done")))
  3291. ;; Declare and autoload functions from org-exp.el & Co
  3292. (declare-function org-default-export-plist "org-exp")
  3293. (declare-function org-infile-export-plist "org-exp")
  3294. (declare-function org-get-current-options "org-exp")
  3295. (eval-and-compile
  3296. (org-autoload "org-exp"
  3297. '(org-export org-export-visible
  3298. org-insert-export-options-template
  3299. org-table-clean-before-export))
  3300. (org-autoload "org-ascii"
  3301. '(org-export-as-ascii org-export-ascii-preprocess
  3302. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3303. org-export-region-as-ascii))
  3304. (org-autoload "org-latex"
  3305. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3306. org-replace-region-by-latex org-export-region-as-latex
  3307. org-export-as-latex org-export-as-pdf
  3308. org-export-as-pdf-and-open))
  3309. (org-autoload "org-html"
  3310. '(org-export-as-html-and-open
  3311. org-export-as-html-batch org-export-as-html-to-buffer
  3312. org-replace-region-by-html org-export-region-as-html
  3313. org-export-as-html))
  3314. (org-autoload "org-docbook"
  3315. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3316. org-replace-region-by-docbook org-export-region-as-docbook
  3317. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3318. org-export-as-docbook))
  3319. (org-autoload "org-icalendar"
  3320. '(org-export-icalendar-this-file
  3321. org-export-icalendar-all-agenda-files
  3322. org-export-icalendar-combine-agenda-files))
  3323. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3324. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3325. ;; Declare and autoload functions from org-agenda.el
  3326. (eval-and-compile
  3327. (org-autoload "org-agenda"
  3328. '(org-agenda org-agenda-list org-search-view
  3329. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3330. org-diary org-agenda-to-appt
  3331. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3332. ;; Autoload org-remember
  3333. (eval-and-compile
  3334. (org-autoload "org-remember"
  3335. '(org-remember-insinuate org-remember-annotation
  3336. org-remember-apply-template org-remember org-remember-handler)))
  3337. (eval-and-compile
  3338. (org-autoload "org-capture"
  3339. '(org-capture org-capture-insert-template-here
  3340. org-capture-import-remember-templates)))
  3341. ;; Autoload org-clock.el
  3342. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3343. (beg end))
  3344. (declare-function org-clock-update-mode-line "org-clock" ())
  3345. (declare-function org-resolve-clocks "org-clock"
  3346. (&optional also-non-dangling-p prompt last-valid))
  3347. (defvar org-clock-start-time)
  3348. (defvar org-clock-marker (make-marker)
  3349. "Marker recording the last clock-in.")
  3350. (defvar org-clock-hd-marker (make-marker)
  3351. "Marker recording the last clock-in, but the headline position.")
  3352. (defvar org-clock-heading ""
  3353. "The heading of the current clock entry.")
  3354. (defun org-clock-is-active ()
  3355. "Return non-nil if clock is currently running.
  3356. The return value is actually the clock marker."
  3357. (marker-buffer org-clock-marker))
  3358. (eval-and-compile
  3359. (org-autoload
  3360. "org-clock"
  3361. '(org-clock-in org-clock-out org-clock-cancel
  3362. org-clock-goto org-clock-sum org-clock-display
  3363. org-clock-remove-overlays org-clock-report
  3364. org-clocktable-shift org-dblock-write:clocktable
  3365. org-get-clocktable org-resolve-clocks)))
  3366. (defun org-clock-update-time-maybe ()
  3367. "If this is a CLOCK line, update it and return t.
  3368. Otherwise, return nil."
  3369. (interactive)
  3370. (save-excursion
  3371. (beginning-of-line 1)
  3372. (skip-chars-forward " \t")
  3373. (when (looking-at org-clock-string)
  3374. (let ((re (concat "[ \t]*" org-clock-string
  3375. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3376. "\\([ \t]*=>.*\\)?\\)?"))
  3377. ts te h m s neg)
  3378. (cond
  3379. ((not (looking-at re))
  3380. nil)
  3381. ((not (match-end 2))
  3382. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3383. (> org-clock-marker (point))
  3384. (<= org-clock-marker (point-at-eol)))
  3385. ;; The clock is running here
  3386. (setq org-clock-start-time
  3387. (apply 'encode-time
  3388. (org-parse-time-string (match-string 1))))
  3389. (org-clock-update-mode-line)))
  3390. (t
  3391. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3392. (end-of-line 1)
  3393. (setq ts (match-string 1)
  3394. te (match-string 3))
  3395. (setq s (- (org-float-time
  3396. (apply 'encode-time (org-parse-time-string te)))
  3397. (org-float-time
  3398. (apply 'encode-time (org-parse-time-string ts))))
  3399. neg (< s 0)
  3400. s (abs s)
  3401. h (floor (/ s 3600))
  3402. s (- s (* 3600 h))
  3403. m (floor (/ s 60))
  3404. s (- s (* 60 s)))
  3405. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3406. t))))))
  3407. (defun org-check-running-clock ()
  3408. "Check if the current buffer contains the running clock.
  3409. If yes, offer to stop it and to save the buffer with the changes."
  3410. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3411. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3412. (buffer-name))))
  3413. (org-clock-out)
  3414. (when (y-or-n-p "Save changed buffer?")
  3415. (save-buffer))))
  3416. (defun org-clocktable-try-shift (dir n)
  3417. "Check if this line starts a clock table, if yes, shift the time block."
  3418. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3419. (org-clocktable-shift dir n)))
  3420. ;; Autoload org-timer.el
  3421. (eval-and-compile
  3422. (org-autoload
  3423. "org-timer"
  3424. '(org-timer-start org-timer org-timer-item
  3425. org-timer-change-times-in-region
  3426. org-timer-set-timer
  3427. org-timer-reset-timers
  3428. org-timer-show-remaining-time)))
  3429. ;; Autoload org-feed.el
  3430. (eval-and-compile
  3431. (org-autoload
  3432. "org-feed"
  3433. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3434. ;; Autoload org-indent.el
  3435. ;; Define the variable already here, to make sure we have it.
  3436. (defvar org-indent-mode nil
  3437. "Non-nil if Org-Indent mode is enabled.
  3438. Use the command `org-indent-mode' to change this variable.")
  3439. (eval-and-compile
  3440. (org-autoload
  3441. "org-indent"
  3442. '(org-indent-mode)))
  3443. ;; Autoload org-mobile.el
  3444. (eval-and-compile
  3445. (org-autoload
  3446. "org-mobile"
  3447. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3448. ;; Autoload archiving code
  3449. ;; The stuff that is needed for cycling and tags has to be defined here.
  3450. (defgroup org-archive nil
  3451. "Options concerning archiving in Org-mode."
  3452. :tag "Org Archive"
  3453. :group 'org-structure)
  3454. (defcustom org-archive-location "%s_archive::"
  3455. "The location where subtrees should be archived.
  3456. The value of this variable is a string, consisting of two parts,
  3457. separated by a double-colon. The first part is a filename and
  3458. the second part is a headline.
  3459. When the filename is omitted, archiving happens in the same file.
  3460. %s in the filename will be replaced by the current file
  3461. name (without the directory part). Archiving to a different file
  3462. is useful to keep archived entries from contributing to the
  3463. Org-mode Agenda.
  3464. The archived entries will be filed as subtrees of the specified
  3465. headline. When the headline is omitted, the subtrees are simply
  3466. filed away at the end of the file, as top-level entries. Also in
  3467. the heading you can use %s to represent the file name, this can be
  3468. useful when using the same archive for a number of different files.
  3469. Here are a few examples:
  3470. \"%s_archive::\"
  3471. If the current file is Projects.org, archive in file
  3472. Projects.org_archive, as top-level trees. This is the default.
  3473. \"::* Archived Tasks\"
  3474. Archive in the current file, under the top-level headline
  3475. \"* Archived Tasks\".
  3476. \"~/org/archive.org::\"
  3477. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3478. \"~/org/archive.org::From %s\"
  3479. Archive in file ~/org/archive.org (absolute path), under headlines
  3480. \"From FILENAME\" where file name is the current file name.
  3481. \"basement::** Finished Tasks\"
  3482. Archive in file ./basement (relative path), as level 3 trees
  3483. below the level 2 heading \"** Finished Tasks\".
  3484. You may set this option on a per-file basis by adding to the buffer a
  3485. line like
  3486. #+ARCHIVE: basement::** Finished Tasks
  3487. You may also define it locally for a subtree by setting an ARCHIVE property
  3488. in the entry. If such a property is found in an entry, or anywhere up
  3489. the hierarchy, it will be used."
  3490. :group 'org-archive
  3491. :type 'string)
  3492. (defcustom org-archive-tag "ARCHIVE"
  3493. "The tag that marks a subtree as archived.
  3494. An archived subtree does not open during visibility cycling, and does
  3495. not contribute to the agenda listings.
  3496. After changing this, font-lock must be restarted in the relevant buffers to
  3497. get the proper fontification."
  3498. :group 'org-archive
  3499. :group 'org-keywords
  3500. :type 'string)
  3501. (defcustom org-agenda-skip-archived-trees t
  3502. "Non-nil means the agenda will skip any items located in archived trees.
  3503. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3504. variable is no longer recommended, you should leave it at the value t.
  3505. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3506. :group 'org-archive
  3507. :group 'org-agenda-skip
  3508. :type 'boolean)
  3509. (defcustom org-columns-skip-archived-trees t
  3510. "Non-nil means ignore archived trees when creating column view."
  3511. :group 'org-archive
  3512. :group 'org-properties
  3513. :type 'boolean)
  3514. (defcustom org-cycle-open-archived-trees nil
  3515. "Non-nil means `org-cycle' will open archived trees.
  3516. An archived tree is a tree marked with the tag ARCHIVE.
  3517. When nil, archived trees will stay folded. You can still open them with
  3518. normal outline commands like `show-all', but not with the cycling commands."
  3519. :group 'org-archive
  3520. :group 'org-cycle
  3521. :type 'boolean)
  3522. (defcustom org-sparse-tree-open-archived-trees nil
  3523. "Non-nil means sparse tree construction shows matches in archived trees.
  3524. When nil, matches in these trees are highlighted, but the trees are kept in
  3525. collapsed state."
  3526. :group 'org-archive
  3527. :group 'org-sparse-trees
  3528. :type 'boolean)
  3529. (defun org-cycle-hide-archived-subtrees (state)
  3530. "Re-hide all archived subtrees after a visibility state change."
  3531. (when (and (not org-cycle-open-archived-trees)
  3532. (not (memq state '(overview folded))))
  3533. (save-excursion
  3534. (let* ((globalp (memq state '(contents all)))
  3535. (beg (if globalp (point-min) (point)))
  3536. (end (if globalp (point-max) (org-end-of-subtree t))))
  3537. (org-hide-archived-subtrees beg end)
  3538. (goto-char beg)
  3539. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3540. (message "%s" (substitute-command-keys
  3541. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3542. (defun org-force-cycle-archived ()
  3543. "Cycle subtree even if it is archived."
  3544. (interactive)
  3545. (setq this-command 'org-cycle)
  3546. (let ((org-cycle-open-archived-trees t))
  3547. (call-interactively 'org-cycle)))
  3548. (defun org-hide-archived-subtrees (beg end)
  3549. "Re-hide all archived subtrees after a visibility state change."
  3550. (save-excursion
  3551. (let* ((re (concat ":" org-archive-tag ":")))
  3552. (goto-char beg)
  3553. (while (re-search-forward re end t)
  3554. (when (org-on-heading-p)
  3555. (org-flag-subtree t)
  3556. (org-end-of-subtree t))))))
  3557. (defun org-flag-subtree (flag)
  3558. (save-excursion
  3559. (org-back-to-heading t)
  3560. (outline-end-of-heading)
  3561. (outline-flag-region (point)
  3562. (progn (org-end-of-subtree t) (point))
  3563. flag)))
  3564. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3565. (eval-and-compile
  3566. (org-autoload "org-archive"
  3567. '(org-add-archive-files org-archive-subtree
  3568. org-archive-to-archive-sibling org-toggle-archive-tag
  3569. org-archive-subtree-default
  3570. org-archive-subtree-default-with-confirmation)))
  3571. ;; Autoload Column View Code
  3572. (declare-function org-columns-number-to-string "org-colview")
  3573. (declare-function org-columns-get-format-and-top-level "org-colview")
  3574. (declare-function org-columns-compute "org-colview")
  3575. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3576. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3577. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3578. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3579. ;; Autoload ID code
  3580. (declare-function org-id-store-link "org-id")
  3581. (declare-function org-id-locations-load "org-id")
  3582. (declare-function org-id-locations-save "org-id")
  3583. (defvar org-id-track-globally)
  3584. (org-autoload "org-id"
  3585. '(org-id-get-create org-id-new org-id-copy org-id-get
  3586. org-id-get-with-outline-path-completion
  3587. org-id-get-with-outline-drilling org-id-store-link
  3588. org-id-goto org-id-find org-id-store-link))
  3589. ;; Autoload Plotting Code
  3590. (org-autoload "org-plot"
  3591. '(org-plot/gnuplot))
  3592. ;;; Variables for pre-computed regular expressions, all buffer local
  3593. (defvar org-drawer-regexp nil
  3594. "Matches first line of a hidden block.")
  3595. (make-variable-buffer-local 'org-drawer-regexp)
  3596. (defvar org-todo-regexp nil
  3597. "Matches any of the TODO state keywords.")
  3598. (make-variable-buffer-local 'org-todo-regexp)
  3599. (defvar org-not-done-regexp nil
  3600. "Matches any of the TODO state keywords except the last one.")
  3601. (make-variable-buffer-local 'org-not-done-regexp)
  3602. (defvar org-not-done-heading-regexp nil
  3603. "Matches a TODO headline that is not done.")
  3604. (make-variable-buffer-local 'org-not-done-regexp)
  3605. (defvar org-todo-line-regexp nil
  3606. "Matches a headline and puts TODO state into group 2 if present.")
  3607. (make-variable-buffer-local 'org-todo-line-regexp)
  3608. (defvar org-complex-heading-regexp nil
  3609. "Matches a headline and puts everything into groups:
  3610. group 1: the stars
  3611. group 2: The todo keyword, maybe
  3612. group 3: Priority cookie
  3613. group 4: True headline
  3614. group 5: Tags")
  3615. (make-variable-buffer-local 'org-complex-heading-regexp)
  3616. (defvar org-complex-heading-regexp-format nil
  3617. "Printf format to make regexp to match an exact headline.
  3618. This regexp will match the headline of any node which hase the exact
  3619. headline text that is put into the format, but may have any TODO state,
  3620. priority and tags.")
  3621. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3622. (defvar org-todo-line-tags-regexp nil
  3623. "Matches a headline and puts TODO state into group 2 if present.
  3624. Also put tags into group 4 if tags are present.")
  3625. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3626. (defvar org-nl-done-regexp nil
  3627. "Matches newline followed by a headline with the DONE keyword.")
  3628. (make-variable-buffer-local 'org-nl-done-regexp)
  3629. (defvar org-looking-at-done-regexp nil
  3630. "Matches the DONE keyword a point.")
  3631. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3632. (defvar org-ds-keyword-length 12
  3633. "Maximum length of the Deadline and SCHEDULED keywords.")
  3634. (make-variable-buffer-local 'org-ds-keyword-length)
  3635. (defvar org-deadline-regexp nil
  3636. "Matches the DEADLINE keyword.")
  3637. (make-variable-buffer-local 'org-deadline-regexp)
  3638. (defvar org-deadline-time-regexp nil
  3639. "Matches the DEADLINE keyword together with a time stamp.")
  3640. (make-variable-buffer-local 'org-deadline-time-regexp)
  3641. (defvar org-deadline-line-regexp nil
  3642. "Matches the DEADLINE keyword and the rest of the line.")
  3643. (make-variable-buffer-local 'org-deadline-line-regexp)
  3644. (defvar org-scheduled-regexp nil
  3645. "Matches the SCHEDULED keyword.")
  3646. (make-variable-buffer-local 'org-scheduled-regexp)
  3647. (defvar org-scheduled-time-regexp nil
  3648. "Matches the SCHEDULED keyword together with a time stamp.")
  3649. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3650. (defvar org-closed-time-regexp nil
  3651. "Matches the CLOSED keyword together with a time stamp.")
  3652. (make-variable-buffer-local 'org-closed-time-regexp)
  3653. (defvar org-keyword-time-regexp nil
  3654. "Matches any of the 4 keywords, together with the time stamp.")
  3655. (make-variable-buffer-local 'org-keyword-time-regexp)
  3656. (defvar org-keyword-time-not-clock-regexp nil
  3657. "Matches any of the 3 keywords, together with the time stamp.")
  3658. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3659. (defvar org-maybe-keyword-time-regexp nil
  3660. "Matches a timestamp, possibly preceded by a keyword.")
  3661. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3662. (defvar org-planning-or-clock-line-re nil
  3663. "Matches a line with planning or clock info.")
  3664. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3665. (defvar org-all-time-keywords nil
  3666. "List of time keywords.")
  3667. (make-variable-buffer-local 'org-all-time-keywords)
  3668. (defconst org-plain-time-of-day-regexp
  3669. (concat
  3670. "\\(\\<[012]?[0-9]"
  3671. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3672. "\\(--?"
  3673. "\\(\\<[012]?[0-9]"
  3674. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3675. "\\)?")
  3676. "Regular expression to match a plain time or time range.
  3677. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3678. groups carry important information:
  3679. 0 the full match
  3680. 1 the first time, range or not
  3681. 8 the second time, if it is a range.")
  3682. (defconst org-plain-time-extension-regexp
  3683. (concat
  3684. "\\(\\<[012]?[0-9]"
  3685. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3686. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3687. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3688. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3689. groups carry important information:
  3690. 0 the full match
  3691. 7 hours of duration
  3692. 9 minutes of duration")
  3693. (defconst org-stamp-time-of-day-regexp
  3694. (concat
  3695. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3696. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3697. "\\(--?"
  3698. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3699. "Regular expression to match a timestamp time or time range.
  3700. After a match, the following groups carry important information:
  3701. 0 the full match
  3702. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3703. 2 the first time, range or not
  3704. 4 the second time, if it is a range.")
  3705. (defconst org-startup-options
  3706. '(("fold" org-startup-folded t)
  3707. ("overview" org-startup-folded t)
  3708. ("nofold" org-startup-folded nil)
  3709. ("showall" org-startup-folded nil)
  3710. ("showeverything" org-startup-folded showeverything)
  3711. ("content" org-startup-folded content)
  3712. ("indent" org-startup-indented t)
  3713. ("noindent" org-startup-indented nil)
  3714. ("hidestars" org-hide-leading-stars t)
  3715. ("showstars" org-hide-leading-stars nil)
  3716. ("odd" org-odd-levels-only t)
  3717. ("oddeven" org-odd-levels-only nil)
  3718. ("align" org-startup-align-all-tables t)
  3719. ("noalign" org-startup-align-all-tables nil)
  3720. ("inlineimages" org-startup-with-inline-images t)
  3721. ("noinlineimages" org-startup-with-inline-images nil)
  3722. ("customtime" org-display-custom-times t)
  3723. ("logdone" org-log-done time)
  3724. ("lognotedone" org-log-done note)
  3725. ("nologdone" org-log-done nil)
  3726. ("lognoteclock-out" org-log-note-clock-out t)
  3727. ("nolognoteclock-out" org-log-note-clock-out nil)
  3728. ("logrepeat" org-log-repeat state)
  3729. ("lognoterepeat" org-log-repeat note)
  3730. ("nologrepeat" org-log-repeat nil)
  3731. ("logreschedule" org-log-reschedule time)
  3732. ("lognotereschedule" org-log-reschedule note)
  3733. ("nologreschedule" org-log-reschedule nil)
  3734. ("logredeadline" org-log-redeadline time)
  3735. ("lognoteredeadline" org-log-redeadline note)
  3736. ("nologredeadline" org-log-redeadline nil)
  3737. ("logrefile" org-log-refile time)
  3738. ("lognoterefile" org-log-refile note)
  3739. ("nologrefile" org-log-refile nil)
  3740. ("fninline" org-footnote-define-inline t)
  3741. ("nofninline" org-footnote-define-inline nil)
  3742. ("fnlocal" org-footnote-section nil)
  3743. ("fnauto" org-footnote-auto-label t)
  3744. ("fnprompt" org-footnote-auto-label nil)
  3745. ("fnconfirm" org-footnote-auto-label confirm)
  3746. ("fnplain" org-footnote-auto-label plain)
  3747. ("fnadjust" org-footnote-auto-adjust t)
  3748. ("nofnadjust" org-footnote-auto-adjust nil)
  3749. ("constcgs" constants-unit-system cgs)
  3750. ("constSI" constants-unit-system SI)
  3751. ("noptag" org-tag-persistent-alist nil)
  3752. ("hideblocks" org-hide-block-startup t)
  3753. ("nohideblocks" org-hide-block-startup nil)
  3754. ("beamer" org-startup-with-beamer-mode t)
  3755. ("entitiespretty" org-pretty-entities t)
  3756. ("entitiesplain" org-pretty-entities nil))
  3757. "Variable associated with STARTUP options for org-mode.
  3758. Each element is a list of three items: The startup options as written
  3759. in the #+STARTUP line, the corresponding variable, and the value to
  3760. set this variable to if the option is found. An optional forth element PUSH
  3761. means to push this value onto the list in the variable.")
  3762. (defun org-set-regexps-and-options ()
  3763. "Precompute regular expressions for current buffer."
  3764. (when (org-mode-p)
  3765. (org-set-local 'org-todo-kwd-alist nil)
  3766. (org-set-local 'org-todo-key-alist nil)
  3767. (org-set-local 'org-todo-key-trigger nil)
  3768. (org-set-local 'org-todo-keywords-1 nil)
  3769. (org-set-local 'org-done-keywords nil)
  3770. (org-set-local 'org-todo-heads nil)
  3771. (org-set-local 'org-todo-sets nil)
  3772. (org-set-local 'org-todo-log-states nil)
  3773. (org-set-local 'org-file-properties nil)
  3774. (org-set-local 'org-file-tags nil)
  3775. (let ((re (org-make-options-regexp
  3776. '("CATEGORY" "TODO" "COLUMNS"
  3777. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3778. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3779. "OPTIONS")
  3780. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3781. (splitre "[ \t]+")
  3782. (scripts org-use-sub-superscripts)
  3783. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3784. tail sep kws1 prio props ftags drawers beamer-p
  3785. ext-setup-or-nil setup-contents (start 0))
  3786. (save-excursion
  3787. (save-restriction
  3788. (widen)
  3789. (goto-char (point-min))
  3790. (while (or (and ext-setup-or-nil
  3791. (string-match re ext-setup-or-nil start)
  3792. (setq start (match-end 0)))
  3793. (and (setq ext-setup-or-nil nil start 0)
  3794. (re-search-forward re nil t)))
  3795. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3796. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3797. (if (stringp value) (setq value (org-trim value)))
  3798. (cond
  3799. ((equal key "CATEGORY")
  3800. (setq cat value))
  3801. ((member key '("SEQ_TODO" "TODO"))
  3802. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3803. ((equal key "TYP_TODO")
  3804. (push (cons 'type (org-split-string value splitre)) kwds))
  3805. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3806. ;; general TODO-like setup
  3807. (push (cons (intern (downcase (match-string 1 key)))
  3808. (org-split-string value splitre)) kwds))
  3809. ((equal key "TAGS")
  3810. (setq tags (append tags (if tags '("\\n") nil)
  3811. (org-split-string value splitre))))
  3812. ((equal key "COLUMNS")
  3813. (org-set-local 'org-columns-default-format value))
  3814. ((equal key "LINK")
  3815. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3816. (push (cons (match-string 1 value)
  3817. (org-trim (match-string 2 value)))
  3818. links)))
  3819. ((equal key "PRIORITIES")
  3820. (setq prio (org-split-string value " +")))
  3821. ((equal key "PROPERTY")
  3822. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3823. (push (cons (match-string 1 value) (match-string 2 value))
  3824. props)))
  3825. ((equal key "FILETAGS")
  3826. (when (string-match "\\S-" value)
  3827. (setq ftags
  3828. (append
  3829. ftags
  3830. (apply 'append
  3831. (mapcar (lambda (x) (org-split-string x ":"))
  3832. (org-split-string value)))))))
  3833. ((equal key "DRAWERS")
  3834. (setq drawers (org-split-string value splitre)))
  3835. ((equal key "CONSTANTS")
  3836. (setq const (append const (org-split-string value splitre))))
  3837. ((equal key "STARTUP")
  3838. (let ((opts (org-split-string value splitre))
  3839. l var val)
  3840. (while (setq l (pop opts))
  3841. (when (setq l (assoc l org-startup-options))
  3842. (setq var (nth 1 l) val (nth 2 l))
  3843. (if (not (nth 3 l))
  3844. (set (make-local-variable var) val)
  3845. (if (not (listp (symbol-value var)))
  3846. (set (make-local-variable var) nil))
  3847. (set (make-local-variable var) (symbol-value var))
  3848. (add-to-list var val))))))
  3849. ((equal key "ARCHIVE")
  3850. (setq arch value)
  3851. (remove-text-properties 0 (length arch)
  3852. '(face t fontified t) arch))
  3853. ((equal key "LATEX_CLASS")
  3854. (setq beamer-p (equal value "beamer")))
  3855. ((equal key "OPTIONS")
  3856. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3857. (setq scripts (read (match-string 2 value)))))
  3858. ((equal key "SETUPFILE")
  3859. (setq setup-contents (org-file-contents
  3860. (expand-file-name
  3861. (org-remove-double-quotes value))
  3862. 'noerror))
  3863. (if (not ext-setup-or-nil)
  3864. (setq ext-setup-or-nil setup-contents start 0)
  3865. (setq ext-setup-or-nil
  3866. (concat (substring ext-setup-or-nil 0 start)
  3867. "\n" setup-contents "\n"
  3868. (substring ext-setup-or-nil start)))))
  3869. ))))
  3870. (org-set-local 'org-use-sub-superscripts scripts)
  3871. (when cat
  3872. (org-set-local 'org-category (intern cat))
  3873. (push (cons "CATEGORY" cat) props))
  3874. (when prio
  3875. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3876. (setq prio (mapcar 'string-to-char prio))
  3877. (org-set-local 'org-highest-priority (nth 0 prio))
  3878. (org-set-local 'org-lowest-priority (nth 1 prio))
  3879. (org-set-local 'org-default-priority (nth 2 prio)))
  3880. (and props (org-set-local 'org-file-properties (nreverse props)))
  3881. (and ftags (org-set-local 'org-file-tags
  3882. (mapcar 'org-add-prop-inherited ftags)))
  3883. (and drawers (org-set-local 'org-drawers drawers))
  3884. (and arch (org-set-local 'org-archive-location arch))
  3885. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3886. ;; Process the TODO keywords
  3887. (unless kwds
  3888. ;; Use the global values as if they had been given locally.
  3889. (setq kwds (default-value 'org-todo-keywords))
  3890. (if (stringp (car kwds))
  3891. (setq kwds (list (cons org-todo-interpretation
  3892. (default-value 'org-todo-keywords)))))
  3893. (setq kwds (reverse kwds)))
  3894. (setq kwds (nreverse kwds))
  3895. (let (inter kws kw)
  3896. (while (setq kws (pop kwds))
  3897. (let ((kws (or
  3898. (run-hook-with-args-until-success
  3899. 'org-todo-setup-filter-hook kws)
  3900. kws)))
  3901. (setq inter (pop kws) sep (member "|" kws)
  3902. kws0 (delete "|" (copy-sequence kws))
  3903. kwsa nil
  3904. kws1 (mapcar
  3905. (lambda (x)
  3906. ;; 1 2
  3907. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3908. (progn
  3909. (setq kw (match-string 1 x)
  3910. key (and (match-end 2) (match-string 2 x))
  3911. log (org-extract-log-state-settings x))
  3912. (push (cons kw (and key (string-to-char key))) kwsa)
  3913. (and log (push log org-todo-log-states))
  3914. kw)
  3915. (error "Invalid TODO keyword %s" x)))
  3916. kws0)
  3917. kwsa (if kwsa (append '((:startgroup))
  3918. (nreverse kwsa)
  3919. '((:endgroup))))
  3920. hw (car kws1)
  3921. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3922. tail (list inter hw (car dws) (org-last dws))))
  3923. (add-to-list 'org-todo-heads hw 'append)
  3924. (push kws1 org-todo-sets)
  3925. (setq org-done-keywords (append org-done-keywords dws nil))
  3926. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3927. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3928. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3929. (setq org-todo-sets (nreverse org-todo-sets)
  3930. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3931. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3932. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3933. ;; Process the constants
  3934. (when const
  3935. (let (e cst)
  3936. (while (setq e (pop const))
  3937. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3938. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3939. (setq org-table-formula-constants-local cst)))
  3940. ;; Process the tags.
  3941. (when tags
  3942. (let (e tgs)
  3943. (while (setq e (pop tags))
  3944. (cond
  3945. ((equal e "{") (push '(:startgroup) tgs))
  3946. ((equal e "}") (push '(:endgroup) tgs))
  3947. ((equal e "\\n") (push '(:newline) tgs))
  3948. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  3949. (push (cons (match-string 1 e)
  3950. (string-to-char (match-string 2 e)))
  3951. tgs))
  3952. (t (push (list e) tgs))))
  3953. (org-set-local 'org-tag-alist nil)
  3954. (while (setq e (pop tgs))
  3955. (or (and (stringp (car e))
  3956. (assoc (car e) org-tag-alist))
  3957. (push e org-tag-alist)))))
  3958. ;; Compute the regular expressions and other local variables
  3959. (if (not org-done-keywords)
  3960. (setq org-done-keywords (and org-todo-keywords-1
  3961. (list (org-last org-todo-keywords-1)))))
  3962. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3963. (length org-scheduled-string)
  3964. (length org-clock-string)
  3965. (length org-closed-string)))
  3966. org-drawer-regexp
  3967. (concat "^[ \t]*:\\("
  3968. (mapconcat 'regexp-quote org-drawers "\\|")
  3969. "\\):[ \t]*$")
  3970. org-not-done-keywords
  3971. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3972. org-todo-regexp
  3973. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3974. "\\|") "\\)\\>")
  3975. org-not-done-regexp
  3976. (concat "\\<\\("
  3977. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3978. "\\)\\>")
  3979. org-not-done-heading-regexp
  3980. (concat "^\\(\\*+\\)[ \t]+\\("
  3981. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3982. "\\)\\>")
  3983. org-todo-line-regexp
  3984. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3985. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3986. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3987. org-complex-heading-regexp
  3988. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3989. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3990. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3991. "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  3992. org-complex-heading-regexp-format
  3993. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3994. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3995. "\\)\\>\\)?"
  3996. "\\(?:[ \t]*\\(\\[#.\\]\\)\\)?"
  3997. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3998. "[ \t]*\\(%s\\)"
  3999. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  4000. "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?[ \t]*$")
  4001. org-nl-done-regexp
  4002. (concat "\n\\*+[ \t]+"
  4003. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  4004. "\\)" "\\>")
  4005. org-todo-line-tags-regexp
  4006. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  4007. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4008. (org-re
  4009. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@#%]+:[ \t]*\\)?$\\)"))
  4010. org-looking-at-done-regexp
  4011. (concat "^" "\\(?:"
  4012. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4013. "\\>")
  4014. org-deadline-regexp (concat "\\<" org-deadline-string)
  4015. org-deadline-time-regexp
  4016. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4017. org-deadline-line-regexp
  4018. (concat "\\<\\(" org-deadline-string "\\).*")
  4019. org-scheduled-regexp
  4020. (concat "\\<" org-scheduled-string)
  4021. org-scheduled-time-regexp
  4022. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4023. org-closed-time-regexp
  4024. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4025. org-keyword-time-regexp
  4026. (concat "\\<\\(" org-scheduled-string
  4027. "\\|" org-deadline-string
  4028. "\\|" org-closed-string
  4029. "\\|" org-clock-string "\\)"
  4030. " *[[<]\\([^]>]+\\)[]>]")
  4031. org-keyword-time-not-clock-regexp
  4032. (concat "\\<\\(" org-scheduled-string
  4033. "\\|" org-deadline-string
  4034. "\\|" org-closed-string
  4035. "\\)"
  4036. " *[[<]\\([^]>]+\\)[]>]")
  4037. org-maybe-keyword-time-regexp
  4038. (concat "\\(\\<\\(" org-scheduled-string
  4039. "\\|" org-deadline-string
  4040. "\\|" org-closed-string
  4041. "\\|" org-clock-string "\\)\\)?"
  4042. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4043. org-planning-or-clock-line-re
  4044. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4045. "\\|" org-deadline-string
  4046. "\\|" org-closed-string "\\|" org-clock-string
  4047. "\\)\\>\\)")
  4048. org-all-time-keywords
  4049. (mapcar (lambda (w) (substring w 0 -1))
  4050. (list org-scheduled-string org-deadline-string
  4051. org-clock-string org-closed-string))
  4052. )
  4053. (org-compute-latex-and-specials-regexp)
  4054. (org-set-font-lock-defaults))))
  4055. (defun org-file-contents (file &optional noerror)
  4056. "Return the contents of FILE, as a string."
  4057. (if (or (not file)
  4058. (not (file-readable-p file)))
  4059. (if noerror
  4060. (progn
  4061. (message "Cannot read file \"%s\"" file)
  4062. (ding) (sit-for 2)
  4063. "")
  4064. (error "Cannot read file \"%s\"" file))
  4065. (with-temp-buffer
  4066. (insert-file-contents file)
  4067. (buffer-string))))
  4068. (defun org-extract-log-state-settings (x)
  4069. "Extract the log state setting from a TODO keyword string.
  4070. This will extract info from a string like \"WAIT(w@/!)\"."
  4071. (let (kw key log1 log2)
  4072. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4073. (setq kw (match-string 1 x)
  4074. key (and (match-end 2) (match-string 2 x))
  4075. log1 (and (match-end 3) (match-string 3 x))
  4076. log2 (and (match-end 4) (match-string 4 x)))
  4077. (and (or log1 log2)
  4078. (list kw
  4079. (and log1 (if (equal log1 "!") 'time 'note))
  4080. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4081. (defun org-remove-keyword-keys (list)
  4082. "Remove a pair of parenthesis at the end of each string in LIST."
  4083. (mapcar (lambda (x)
  4084. (if (string-match "(.*)$" x)
  4085. (substring x 0 (match-beginning 0))
  4086. x))
  4087. list))
  4088. (defun org-assign-fast-keys (alist)
  4089. "Assign fast keys to a keyword-key alist.
  4090. Respect keys that are already there."
  4091. (let (new e (alt ?0))
  4092. (while (setq e (pop alist))
  4093. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4094. (cdr e)) ;; Key already assigned.
  4095. (push e new)
  4096. (let ((clist (string-to-list (downcase (car e))))
  4097. (used (append new alist)))
  4098. (when (= (car clist) ?@)
  4099. (pop clist))
  4100. (while (and clist (rassoc (car clist) used))
  4101. (pop clist))
  4102. (unless clist
  4103. (while (rassoc alt used)
  4104. (incf alt)))
  4105. (push (cons (car e) (or (car clist) alt)) new))))
  4106. (nreverse new)))
  4107. ;;; Some variables used in various places
  4108. (defvar org-window-configuration nil
  4109. "Used in various places to store a window configuration.")
  4110. (defvar org-selected-window nil
  4111. "Used in various places to store a window configuration.")
  4112. (defvar org-finish-function nil
  4113. "Function to be called when `C-c C-c' is used.
  4114. This is for getting out of special buffers like remember.")
  4115. ;; FIXME: Occasionally check by commenting these, to make sure
  4116. ;; no other functions uses these, forgetting to let-bind them.
  4117. (defvar entry)
  4118. (defvar last-state)
  4119. (defvar date)
  4120. ;; Defined somewhere in this file, but used before definition.
  4121. (defvar org-entities) ;; defined in org-entities.el
  4122. (defvar org-struct-menu)
  4123. (defvar org-org-menu)
  4124. (defvar org-tbl-menu)
  4125. ;;;; Define the Org-mode
  4126. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4127. (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"))
  4128. ;; We use a before-change function to check if a table might need
  4129. ;; an update.
  4130. (defvar org-table-may-need-update t
  4131. "Indicates that a table might need an update.
  4132. This variable is set by `org-before-change-function'.
  4133. `org-table-align' sets it back to nil.")
  4134. (defun org-before-change-function (beg end)
  4135. "Every change indicates that a table might need an update."
  4136. (setq org-table-may-need-update t))
  4137. (defvar org-mode-map)
  4138. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4139. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4140. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4141. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4142. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4143. (defvar org-table-buffer-is-an nil)
  4144. (defconst org-outline-regexp "\\*+ ")
  4145. ;;;###autoload
  4146. (define-derived-mode org-mode outline-mode "Org"
  4147. "Outline-based notes management and organizer, alias
  4148. \"Carsten's outline-mode for keeping track of everything.\"
  4149. Org-mode develops organizational tasks around a NOTES file which
  4150. contains information about projects as plain text. Org-mode is
  4151. implemented on top of outline-mode, which is ideal to keep the content
  4152. of large files well structured. It supports ToDo items, deadlines and
  4153. time stamps, which magically appear in the diary listing of the Emacs
  4154. calendar. Tables are easily created with a built-in table editor.
  4155. Plain text URL-like links connect to websites, emails (VM), Usenet
  4156. messages (Gnus), BBDB entries, and any files related to the project.
  4157. For printing and sharing of notes, an Org-mode file (or a part of it)
  4158. can be exported as a structured ASCII or HTML file.
  4159. The following commands are available:
  4160. \\{org-mode-map}"
  4161. ;; Get rid of Outline menus, they are not needed
  4162. ;; Need to do this here because define-derived-mode sets up
  4163. ;; the keymap so late. Still, it is a waste to call this each time
  4164. ;; we switch another buffer into org-mode.
  4165. (if (featurep 'xemacs)
  4166. (when (boundp 'outline-mode-menu-heading)
  4167. ;; Assume this is Greg's port, it uses easymenu
  4168. (easy-menu-remove outline-mode-menu-heading)
  4169. (easy-menu-remove outline-mode-menu-show)
  4170. (easy-menu-remove outline-mode-menu-hide))
  4171. (define-key org-mode-map [menu-bar headings] 'undefined)
  4172. (define-key org-mode-map [menu-bar hide] 'undefined)
  4173. (define-key org-mode-map [menu-bar show] 'undefined))
  4174. (org-load-modules-maybe)
  4175. (easy-menu-add org-org-menu)
  4176. (easy-menu-add org-tbl-menu)
  4177. (org-install-agenda-files-menu)
  4178. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4179. (add-to-invisibility-spec '(org-cwidth))
  4180. (add-to-invisibility-spec '(org-hide-block . t))
  4181. (when (featurep 'xemacs)
  4182. (org-set-local 'line-move-ignore-invisible t))
  4183. (org-set-local 'outline-regexp org-outline-regexp)
  4184. (org-set-local 'outline-level 'org-outline-level)
  4185. (when (and org-ellipsis
  4186. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4187. (fboundp 'make-glyph-code))
  4188. (unless org-display-table
  4189. (setq org-display-table (make-display-table)))
  4190. (set-display-table-slot
  4191. org-display-table 4
  4192. (vconcat (mapcar
  4193. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4194. org-ellipsis)))
  4195. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4196. (setq buffer-display-table org-display-table))
  4197. (org-set-regexps-and-options)
  4198. (when (and org-tag-faces (not org-tags-special-faces-re))
  4199. ;; tag faces set outside customize.... force initialization.
  4200. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4201. ;; Calc embedded
  4202. (org-set-local 'calc-embedded-open-mode "# ")
  4203. (modify-syntax-entry ?@ "w")
  4204. (if org-startup-truncated (setq truncate-lines t))
  4205. (org-set-local 'font-lock-unfontify-region-function
  4206. 'org-unfontify-region)
  4207. ;; Activate before-change-function
  4208. (org-set-local 'org-table-may-need-update t)
  4209. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4210. 'local)
  4211. ;; Check for running clock before killing a buffer
  4212. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4213. ;; Paragraphs and auto-filling
  4214. (org-set-autofill-regexps)
  4215. (setq indent-line-function 'org-indent-line-function)
  4216. (org-update-radio-target-regexp)
  4217. ;; Beginning/end of defun
  4218. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4219. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4220. ;; Next error for sparse trees
  4221. (org-set-local 'next-error-function 'org-occur-next-match)
  4222. ;; Make sure dependence stuff works reliably, even for users who set it
  4223. ;; too late :-(
  4224. (if org-enforce-todo-dependencies
  4225. (add-hook 'org-blocker-hook
  4226. 'org-block-todo-from-children-or-siblings-or-parent)
  4227. (remove-hook 'org-blocker-hook
  4228. 'org-block-todo-from-children-or-siblings-or-parent))
  4229. (if org-enforce-todo-checkbox-dependencies
  4230. (add-hook 'org-blocker-hook
  4231. 'org-block-todo-from-checkboxes)
  4232. (remove-hook 'org-blocker-hook
  4233. 'org-block-todo-from-checkboxes))
  4234. ;; Comment characters
  4235. (org-set-local 'comment-start "#")
  4236. (org-set-local 'comment-padding " ")
  4237. ;; Align options lines
  4238. (org-set-local
  4239. 'align-mode-rules-list
  4240. '((org-in-buffer-settings
  4241. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4242. (modes . '(org-mode)))))
  4243. ;; Imenu
  4244. (org-set-local 'imenu-create-index-function
  4245. 'org-imenu-get-tree)
  4246. ;; Make isearch reveal context
  4247. (if (or (featurep 'xemacs)
  4248. (not (boundp 'outline-isearch-open-invisible-function)))
  4249. ;; Emacs 21 and XEmacs make use of the hook
  4250. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4251. ;; Emacs 22 deals with this through a special variable
  4252. (org-set-local 'outline-isearch-open-invisible-function
  4253. (lambda (&rest ignore) (org-show-context 'isearch))))
  4254. ;; Turn on org-beamer-mode?
  4255. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4256. ;; Setup the pcomplete hooks
  4257. (set (make-local-variable 'pcomplete-command-completion-function)
  4258. 'org-pcomplete-initial)
  4259. (set (make-local-variable 'pcomplete-command-name-function)
  4260. 'org-command-at-point)
  4261. (set (make-local-variable 'pcomplete-default-completion-function)
  4262. 'ignore)
  4263. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4264. 'org-parse-arguments)
  4265. (set (make-local-variable 'pcomplete-termination-string) "")
  4266. ;; If empty file that did not turn on org-mode automatically, make it to.
  4267. (if (and org-insert-mode-line-in-empty-file
  4268. (org-called-interactively-p 'any)
  4269. (= (point-min) (point-max)))
  4270. (insert "# -*- mode: org -*-\n\n"))
  4271. (unless org-inhibit-startup
  4272. (when org-startup-align-all-tables
  4273. (let ((bmp (buffer-modified-p)))
  4274. (org-table-map-tables 'org-table-align 'quietly)
  4275. (set-buffer-modified-p bmp)))
  4276. (when org-startup-with-inline-images
  4277. (org-display-inline-images))
  4278. (when org-startup-indented
  4279. (require 'org-indent)
  4280. (org-indent-mode 1))
  4281. (unless org-inhibit-startup-visibility-stuff
  4282. (org-set-startup-visibility))))
  4283. (when (fboundp 'abbrev-table-put)
  4284. (abbrev-table-put org-mode-abbrev-table
  4285. :parents (list text-mode-abbrev-table)))
  4286. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4287. (defun org-current-time ()
  4288. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4289. (if (> (car org-time-stamp-rounding-minutes) 1)
  4290. (let ((r (car org-time-stamp-rounding-minutes))
  4291. (time (decode-time)))
  4292. (apply 'encode-time
  4293. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4294. (nthcdr 2 time))))
  4295. (current-time)))
  4296. (defun org-today ()
  4297. "Return today date, considering `org-extend-today-until'."
  4298. (time-to-days
  4299. (time-subtract (current-time)
  4300. (list 0 (* 3600 org-extend-today-until) 0))))
  4301. ;;;; Font-Lock stuff, including the activators
  4302. (defvar org-mouse-map (make-sparse-keymap))
  4303. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4304. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4305. (when org-mouse-1-follows-link
  4306. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4307. (when org-tab-follows-link
  4308. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4309. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4310. (require 'font-lock)
  4311. (defconst org-non-link-chars "]\t\n\r<>")
  4312. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4313. "shell" "elisp" "doi" "message"))
  4314. (defvar org-link-types-re nil
  4315. "Matches a link that has a url-like prefix like \"http:\"")
  4316. (defvar org-link-re-with-space nil
  4317. "Matches a link with spaces, optional angular brackets around it.")
  4318. (defvar org-link-re-with-space2 nil
  4319. "Matches a link with spaces, optional angular brackets around it.")
  4320. (defvar org-link-re-with-space3 nil
  4321. "Matches a link with spaces, only for internal part in bracket links.")
  4322. (defvar org-angle-link-re nil
  4323. "Matches link with angular brackets, spaces are allowed.")
  4324. (defvar org-plain-link-re nil
  4325. "Matches plain link, without spaces.")
  4326. (defvar org-bracket-link-regexp nil
  4327. "Matches a link in double brackets.")
  4328. (defvar org-bracket-link-analytic-regexp nil
  4329. "Regular expression used to analyze links.
  4330. Here is what the match groups contain after a match:
  4331. 1: http:
  4332. 2: http
  4333. 3: path
  4334. 4: [desc]
  4335. 5: desc")
  4336. (defvar org-bracket-link-analytic-regexp++ nil
  4337. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4338. (defvar org-any-link-re nil
  4339. "Regular expression matching any link.")
  4340. (defcustom org-match-sexp-depth 3
  4341. "Number of stacked braces for sub/superscript matching.
  4342. This has to be set before loading org.el to be effective."
  4343. :group 'org-export-translation ; ??????????????????????????/
  4344. :type 'integer)
  4345. (defun org-create-multibrace-regexp (left right n)
  4346. "Create a regular expression which will match a balanced sexp.
  4347. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4348. as single character strings.
  4349. The regexp returned will match the entire expression including the
  4350. delimiters. It will also define a single group which contains the
  4351. match except for the outermost delimiters. The maximum depth of
  4352. stacked delimiters is N. Escaping delimiters is not possible."
  4353. (let* ((nothing (concat "[^" left right "]*?"))
  4354. (or "\\|")
  4355. (re nothing)
  4356. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4357. (while (> n 1)
  4358. (setq n (1- n)
  4359. re (concat re or next)
  4360. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4361. (concat left "\\(" re "\\)" right)))
  4362. (defvar org-match-substring-regexp
  4363. (concat
  4364. "\\([^\\]\\)\\([_^]\\)\\("
  4365. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4366. "\\|"
  4367. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4368. "\\|"
  4369. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4370. "The regular expression matching a sub- or superscript.")
  4371. (defvar org-match-substring-with-braces-regexp
  4372. (concat
  4373. "\\([^\\]\\)\\([_^]\\)\\("
  4374. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4375. "\\)")
  4376. "The regular expression matching a sub- or superscript, forcing braces.")
  4377. (defun org-make-link-regexps ()
  4378. "Update the link regular expressions.
  4379. This should be called after the variable `org-link-types' has changed."
  4380. (setq org-link-types-re
  4381. (concat
  4382. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4383. org-link-re-with-space
  4384. (concat
  4385. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4386. "\\([^" org-non-link-chars " ]"
  4387. "[^" org-non-link-chars "]*"
  4388. "[^" org-non-link-chars " ]\\)>?")
  4389. org-link-re-with-space2
  4390. (concat
  4391. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4392. "\\([^" org-non-link-chars " ]"
  4393. "[^\t\n\r]*"
  4394. "[^" org-non-link-chars " ]\\)>?")
  4395. org-link-re-with-space3
  4396. (concat
  4397. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4398. "\\([^" org-non-link-chars " ]"
  4399. "[^\t\n\r]*\\)")
  4400. org-angle-link-re
  4401. (concat
  4402. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4403. "\\([^" org-non-link-chars " ]"
  4404. "[^" org-non-link-chars "]*"
  4405. "\\)>")
  4406. org-plain-link-re
  4407. (concat
  4408. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4409. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4410. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4411. org-bracket-link-regexp
  4412. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4413. org-bracket-link-analytic-regexp
  4414. (concat
  4415. "\\[\\["
  4416. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4417. "\\([^]]+\\)"
  4418. "\\]"
  4419. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4420. "\\]")
  4421. org-bracket-link-analytic-regexp++
  4422. (concat
  4423. "\\[\\["
  4424. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4425. "\\([^]]+\\)"
  4426. "\\]"
  4427. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4428. "\\]")
  4429. org-any-link-re
  4430. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4431. org-angle-link-re "\\)\\|\\("
  4432. org-plain-link-re "\\)")))
  4433. (org-make-link-regexps)
  4434. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4435. "Regular expression for fast time stamp matching.")
  4436. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?\\)[]>]"
  4437. "Regular expression for fast time stamp matching.")
  4438. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4439. "Regular expression matching time strings for analysis.
  4440. This one does not require the space after the date, so it can be used
  4441. on a string that terminates immediately after the date.")
  4442. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4443. "Regular expression matching time strings for analysis.")
  4444. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4445. "Regular expression matching time stamps, with groups.")
  4446. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4447. "Regular expression matching time stamps (also [..]), with groups.")
  4448. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4449. "Regular expression matching a time stamp range.")
  4450. (defconst org-tr-regexp-both
  4451. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4452. "Regular expression matching a time stamp range.")
  4453. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4454. org-ts-regexp "\\)?")
  4455. "Regular expression matching a time stamp or time stamp range.")
  4456. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4457. org-ts-regexp-both "\\)?")
  4458. "Regular expression matching a time stamp or time stamp range.
  4459. The time stamps may be either active or inactive.")
  4460. (defvar org-emph-face nil)
  4461. (defun org-do-emphasis-faces (limit)
  4462. "Run through the buffer and add overlays to links."
  4463. (let (rtn a)
  4464. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4465. (if (not (= (char-after (match-beginning 3))
  4466. (char-after (match-beginning 4))))
  4467. (progn
  4468. (setq rtn t)
  4469. (setq a (assoc (match-string 3) org-emphasis-alist))
  4470. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4471. 'face
  4472. (nth 1 a))
  4473. (and (nth 4 a)
  4474. (org-remove-flyspell-overlays-in
  4475. (match-beginning 0) (match-end 0)))
  4476. (add-text-properties (match-beginning 2) (match-end 2)
  4477. '(font-lock-multiline t org-emphasis t))
  4478. (when org-hide-emphasis-markers
  4479. (add-text-properties (match-end 4) (match-beginning 5)
  4480. '(invisible org-link))
  4481. (add-text-properties (match-beginning 3) (match-end 3)
  4482. '(invisible org-link)))))
  4483. (backward-char 1))
  4484. rtn))
  4485. (defun org-emphasize (&optional char)
  4486. "Insert or change an emphasis, i.e. a font like bold or italic.
  4487. If there is an active region, change that region to a new emphasis.
  4488. If there is no region, just insert the marker characters and position
  4489. the cursor between them.
  4490. CHAR should be either the marker character, or the first character of the
  4491. HTML tag associated with that emphasis. If CHAR is a space, the means
  4492. to remove the emphasis of the selected region.
  4493. If char is not given (for example in an interactive call) it
  4494. will be prompted for."
  4495. (interactive)
  4496. (let ((eal org-emphasis-alist) e det
  4497. (erc org-emphasis-regexp-components)
  4498. (prompt "")
  4499. (string "") beg end move tag c s)
  4500. (if (org-region-active-p)
  4501. (setq beg (region-beginning) end (region-end)
  4502. string (buffer-substring beg end))
  4503. (setq move t))
  4504. (while (setq e (pop eal))
  4505. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4506. c (aref tag 0))
  4507. (push (cons c (string-to-char (car e))) det)
  4508. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4509. (substring tag 1)))))
  4510. (setq det (nreverse det))
  4511. (unless char
  4512. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4513. (setq char (read-char-exclusive)))
  4514. (setq char (or (cdr (assoc char det)) char))
  4515. (if (equal char ?\ )
  4516. (setq s "" move nil)
  4517. (unless (assoc (char-to-string char) org-emphasis-alist)
  4518. (error "No such emphasis marker: \"%c\"" char))
  4519. (setq s (char-to-string char)))
  4520. (while (and (> (length string) 1)
  4521. (equal (substring string 0 1) (substring string -1))
  4522. (assoc (substring string 0 1) org-emphasis-alist))
  4523. (setq string (substring string 1 -1)))
  4524. (setq string (concat s string s))
  4525. (if beg (delete-region beg end))
  4526. (unless (or (bolp)
  4527. (string-match (concat "[" (nth 0 erc) "\n]")
  4528. (char-to-string (char-before (point)))))
  4529. (insert " "))
  4530. (unless (or (eobp)
  4531. (string-match (concat "[" (nth 1 erc) "\n]")
  4532. (char-to-string (char-after (point)))))
  4533. (insert " ") (backward-char 1))
  4534. (insert string)
  4535. (and move (backward-char 1))))
  4536. (defconst org-nonsticky-props
  4537. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4538. (defsubst org-rear-nonsticky-at (pos)
  4539. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4540. (defun org-activate-plain-links (limit)
  4541. "Run through the buffer and add overlays to links."
  4542. (catch 'exit
  4543. (let (f)
  4544. (if (re-search-forward org-plain-link-re limit t)
  4545. (progn
  4546. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4547. (setq f (get-text-property (match-beginning 0) 'face))
  4548. (if (or (eq f 'org-tag)
  4549. (and (listp f) (memq 'org-tag f)))
  4550. nil
  4551. (add-text-properties (match-beginning 0) (match-end 0)
  4552. (list 'mouse-face 'highlight
  4553. 'face 'org-link
  4554. 'keymap org-mouse-map))
  4555. (org-rear-nonsticky-at (match-end 0)))
  4556. t)))))
  4557. (defun org-activate-code (limit)
  4558. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4559. (progn
  4560. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4561. (remove-text-properties (match-beginning 0) (match-end 0)
  4562. '(display t invisible t intangible t))
  4563. t)))
  4564. (defcustom org-src-fontify-natively nil
  4565. "When non-nil, fontify code in code blocks."
  4566. :type 'boolean
  4567. :group 'org-appearance
  4568. :group 'org-babel)
  4569. (defun org-fontify-meta-lines-and-blocks (limit)
  4570. (condition-case nil
  4571. (org-fontify-meta-lines-and-blocks-1 limit)
  4572. (error (message "org-mode fontification error"))))
  4573. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4574. "Fontify #+ lines and blocks, in the correct ways."
  4575. (let ((case-fold-search t))
  4576. (if (re-search-forward
  4577. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4578. limit t)
  4579. (let ((beg (match-beginning 0))
  4580. (block-start (match-end 0))
  4581. (block-end nil)
  4582. (lang (match-string 7))
  4583. (beg1 (line-beginning-position 2))
  4584. (dc1 (downcase (match-string 2)))
  4585. (dc3 (downcase (match-string 3)))
  4586. end end1 quoting block-type ovl)
  4587. (cond
  4588. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4589. ;; a single line of backend-specific content
  4590. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4591. (remove-text-properties (match-beginning 0) (match-end 0)
  4592. '(display t invisible t intangible t))
  4593. (add-text-properties (match-beginning 1) (match-end 3)
  4594. '(font-lock-fontified t face org-meta-line))
  4595. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4596. '(font-lock-fontified t face org-block))
  4597. ; for backend-specific code
  4598. t)
  4599. ((and (match-end 4) (equal dc3 "begin"))
  4600. ;; Truly a block
  4601. (setq block-type (downcase (match-string 5))
  4602. quoting (member block-type org-protecting-blocks))
  4603. (when (re-search-forward
  4604. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4605. nil t) ;; on purpose, we look further than LIMIT
  4606. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4607. (setq block-end (match-beginning 0))
  4608. (when quoting
  4609. (remove-text-properties beg end
  4610. '(display t invisible t intangible t)))
  4611. (add-text-properties
  4612. beg end
  4613. '(font-lock-fontified t font-lock-multiline t))
  4614. (add-text-properties beg beg1 '(face org-meta-line))
  4615. (add-text-properties end1 (+ end 1) '(face org-meta-line))
  4616. ; for end_src
  4617. (cond
  4618. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4619. (org-src-font-lock-fontify-block lang block-start block-end)
  4620. ;; remove old background overlays
  4621. (mapc (lambda (ov)
  4622. (if (eq (overlay-get ov 'face) 'org-block-background)
  4623. (delete-overlay ov)))
  4624. (overlays-at (/ (+ beg1 block-end) 2)))
  4625. ;; add a background overlay
  4626. (setq ovl (make-overlay beg1 block-end))
  4627. (overlay-put ovl 'face 'org-block-background)
  4628. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4629. (quoting
  4630. (add-text-properties beg1 (+ end1 1) '(face org-block)))
  4631. ; end of source block
  4632. ((not org-fontify-quote-and-verse-blocks))
  4633. ((string= block-type "quote")
  4634. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4635. ((string= block-type "verse")
  4636. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4637. (add-text-properties beg beg1 '(face org-block-begin-line))
  4638. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4639. t))
  4640. ((member dc1 '("title:" "author:" "email:" "date:"))
  4641. (add-text-properties
  4642. beg (match-end 3)
  4643. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4644. '(font-lock-fontified t invisible t)
  4645. '(font-lock-fontified t face org-document-info-keyword)))
  4646. (add-text-properties
  4647. (match-beginning 6) (match-end 6)
  4648. (if (string-equal dc1 "title:")
  4649. '(font-lock-fontified t face org-document-title)
  4650. '(font-lock-fontified t face org-document-info))))
  4651. ((not (member (char-after beg) '(?\ ?\t)))
  4652. ;; just any other in-buffer setting, but not indented
  4653. (add-text-properties
  4654. beg (1+ (match-end 0))
  4655. '(font-lock-fontified t face org-meta-line))
  4656. t)
  4657. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4658. "orgtbl:" "tblfm:" "tblname:" "result:"
  4659. "results:" "source:" "srcname:" "call:"
  4660. "data:" "header:" "headers:"))
  4661. (and (match-end 4) (equal dc3 "attr")))
  4662. (add-text-properties
  4663. beg (match-end 0)
  4664. '(font-lock-fontified t face org-meta-line))
  4665. t)
  4666. ((member dc3 '(" " ""))
  4667. (add-text-properties
  4668. beg (match-end 0)
  4669. '(font-lock-fontified t face font-lock-comment-face)))
  4670. (t nil))))))
  4671. (defun org-activate-angle-links (limit)
  4672. "Run through the buffer and add overlays to links."
  4673. (if (re-search-forward org-angle-link-re limit t)
  4674. (progn
  4675. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4676. (add-text-properties (match-beginning 0) (match-end 0)
  4677. (list 'mouse-face 'highlight
  4678. 'keymap org-mouse-map))
  4679. (org-rear-nonsticky-at (match-end 0))
  4680. t)))
  4681. (defun org-activate-footnote-links (limit)
  4682. "Run through the buffer and add overlays to footnotes."
  4683. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4684. (when fn
  4685. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4686. (org-remove-flyspell-overlays-in beg end)
  4687. (add-text-properties beg end
  4688. (list 'mouse-face 'highlight
  4689. 'keymap org-mouse-map
  4690. 'help-echo
  4691. (if (= (point-at-bol) beg)
  4692. "Footnote definition"
  4693. "Footnote reference")
  4694. 'font-lock-fontified t
  4695. 'font-lock-multiline t
  4696. 'face 'org-footnote))))))
  4697. (defun org-activate-bracket-links (limit)
  4698. "Run through the buffer and add overlays to bracketed links."
  4699. (if (re-search-forward org-bracket-link-regexp limit t)
  4700. (let* ((help (concat "LINK: "
  4701. (org-match-string-no-properties 1)))
  4702. ;; FIXME: above we should remove the escapes.
  4703. ;; but that requires another match, protecting match data,
  4704. ;; a lot of overhead for font-lock.
  4705. (ip (org-maybe-intangible
  4706. (list 'invisible 'org-link
  4707. 'keymap org-mouse-map 'mouse-face 'highlight
  4708. 'font-lock-multiline t 'help-echo help)))
  4709. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4710. 'font-lock-multiline t 'help-echo help)))
  4711. ;; We need to remove the invisible property here. Table narrowing
  4712. ;; may have made some of this invisible.
  4713. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4714. (remove-text-properties (match-beginning 0) (match-end 0)
  4715. '(invisible nil))
  4716. (if (match-end 3)
  4717. (progn
  4718. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4719. (org-rear-nonsticky-at (match-beginning 3))
  4720. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4721. (org-rear-nonsticky-at (match-end 3))
  4722. (add-text-properties (match-end 3) (match-end 0) ip)
  4723. (org-rear-nonsticky-at (match-end 0)))
  4724. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4725. (org-rear-nonsticky-at (match-beginning 1))
  4726. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4727. (org-rear-nonsticky-at (match-end 1))
  4728. (add-text-properties (match-end 1) (match-end 0) ip)
  4729. (org-rear-nonsticky-at (match-end 0)))
  4730. t)))
  4731. (defun org-activate-dates (limit)
  4732. "Run through the buffer and add overlays to dates."
  4733. (if (re-search-forward org-tsr-regexp-both limit t)
  4734. (progn
  4735. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4736. (add-text-properties (match-beginning 0) (match-end 0)
  4737. (list 'mouse-face 'highlight
  4738. 'keymap org-mouse-map))
  4739. (org-rear-nonsticky-at (match-end 0))
  4740. (when org-display-custom-times
  4741. (if (match-end 3)
  4742. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4743. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4744. t)))
  4745. (defvar org-target-link-regexp nil
  4746. "Regular expression matching radio targets in plain text.")
  4747. (make-variable-buffer-local 'org-target-link-regexp)
  4748. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4749. "Regular expression matching a link target.")
  4750. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4751. "Regular expression matching a radio target.")
  4752. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4753. "Regular expression matching any target.")
  4754. (defun org-activate-target-links (limit)
  4755. "Run through the buffer and add overlays to target matches."
  4756. (when org-target-link-regexp
  4757. (let ((case-fold-search t))
  4758. (if (re-search-forward org-target-link-regexp limit t)
  4759. (progn
  4760. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4761. (add-text-properties (match-beginning 0) (match-end 0)
  4762. (list 'mouse-face 'highlight
  4763. 'keymap org-mouse-map
  4764. 'help-echo "Radio target link"
  4765. 'org-linked-text t))
  4766. (org-rear-nonsticky-at (match-end 0))
  4767. t)))))
  4768. (defun org-update-radio-target-regexp ()
  4769. "Find all radio targets in this file and update the regular expression."
  4770. (interactive)
  4771. (when (memq 'radio org-activate-links)
  4772. (setq org-target-link-regexp
  4773. (org-make-target-link-regexp (org-all-targets 'radio)))
  4774. (org-restart-font-lock)))
  4775. (defun org-hide-wide-columns (limit)
  4776. (let (s e)
  4777. (setq s (text-property-any (point) (or limit (point-max))
  4778. 'org-cwidth t))
  4779. (when s
  4780. (setq e (next-single-property-change s 'org-cwidth))
  4781. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4782. (goto-char e)
  4783. t)))
  4784. (defvar org-latex-and-specials-regexp nil
  4785. "Regular expression for highlighting export special stuff.")
  4786. (defvar org-match-substring-regexp)
  4787. (defvar org-match-substring-with-braces-regexp)
  4788. ;; This should be with the exporter code, but we also use if for font-locking
  4789. (defconst org-export-html-special-string-regexps
  4790. '(("\\\\-" . "&shy;")
  4791. ("---\\([^-]\\)" . "&mdash;\\1")
  4792. ("--\\([^-]\\)" . "&ndash;\\1")
  4793. ("\\.\\.\\." . "&hellip;"))
  4794. "Regular expressions for special string conversion.")
  4795. (defun org-compute-latex-and-specials-regexp ()
  4796. "Compute regular expression for stuff treated specially by exporters."
  4797. (if (not org-highlight-latex-fragments-and-specials)
  4798. (org-set-local 'org-latex-and-specials-regexp nil)
  4799. (require 'org-exp)
  4800. (let*
  4801. ((matchers (plist-get org-format-latex-options :matchers))
  4802. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4803. org-latex-regexps)))
  4804. (org-export-allow-BIND nil)
  4805. (options (org-combine-plists (org-default-export-plist)
  4806. (org-infile-export-plist)))
  4807. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4808. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4809. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4810. (org-export-html-expand (plist-get options :expand-quoted-html))
  4811. (org-export-with-special-strings (plist-get options :special-strings))
  4812. (re-sub
  4813. (cond
  4814. ((equal org-export-with-sub-superscripts '{})
  4815. (list org-match-substring-with-braces-regexp))
  4816. (org-export-with-sub-superscripts
  4817. (list org-match-substring-regexp))
  4818. (t nil)))
  4819. (re-latex
  4820. (if org-export-with-LaTeX-fragments
  4821. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4822. (re-macros
  4823. (if org-export-with-TeX-macros
  4824. (list (concat "\\\\"
  4825. (regexp-opt
  4826. (append
  4827. (delq nil
  4828. (mapcar 'car-safe
  4829. (append org-entities-user
  4830. org-entities)))
  4831. (if (boundp 'org-latex-entities)
  4832. (mapcar (lambda (x)
  4833. (or (car-safe x) x))
  4834. org-latex-entities)
  4835. nil))
  4836. 'words))) ; FIXME
  4837. ))
  4838. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4839. (re-special (if org-export-with-special-strings
  4840. (mapcar (lambda (x) (car x))
  4841. org-export-html-special-string-regexps)))
  4842. (re-rest
  4843. (delq nil
  4844. (list
  4845. (if org-export-html-expand "@<[^>\n]+>")
  4846. ))))
  4847. (org-set-local
  4848. 'org-latex-and-specials-regexp
  4849. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4850. re-rest) "\\|")))))
  4851. (defun org-do-latex-and-special-faces (limit)
  4852. "Run through the buffer and add overlays to links."
  4853. (when org-latex-and-specials-regexp
  4854. (let (rtn d)
  4855. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4856. limit t))
  4857. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4858. 'face))
  4859. '(org-code org-verbatim underline)))
  4860. (progn
  4861. (setq rtn t
  4862. d (cond ((member (char-after (1+ (match-beginning 0)))
  4863. '(?_ ?^)) 1)
  4864. (t 0)))
  4865. (font-lock-prepend-text-property
  4866. (+ d (match-beginning 0)) (match-end 0)
  4867. 'face 'org-latex-and-export-specials)
  4868. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4869. '(font-lock-multiline t)))))
  4870. rtn)))
  4871. (defun org-restart-font-lock ()
  4872. "Restart `font-lock-mode', to force refontification."
  4873. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4874. (font-lock-mode -1)
  4875. (font-lock-mode 1)))
  4876. (defun org-all-targets (&optional radio)
  4877. "Return a list of all targets in this file.
  4878. With optional argument RADIO, only find radio targets."
  4879. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4880. rtn)
  4881. (save-excursion
  4882. (goto-char (point-min))
  4883. (while (re-search-forward re nil t)
  4884. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4885. rtn)))
  4886. (defun org-make-target-link-regexp (targets)
  4887. "Make regular expression matching all strings in TARGETS.
  4888. The regular expression finds the targets also if there is a line break
  4889. between words."
  4890. (and targets
  4891. (concat
  4892. "\\<\\("
  4893. (mapconcat
  4894. (lambda (x)
  4895. (setq x (regexp-quote x))
  4896. (while (string-match " +" x)
  4897. (setq x (replace-match "\\s-+" t t x)))
  4898. x)
  4899. targets
  4900. "\\|")
  4901. "\\)\\>")))
  4902. (defun org-activate-tags (limit)
  4903. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  4904. (progn
  4905. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4906. (add-text-properties (match-beginning 1) (match-end 1)
  4907. (list 'mouse-face 'highlight
  4908. 'keymap org-mouse-map))
  4909. (org-rear-nonsticky-at (match-end 1))
  4910. t)))
  4911. (defun org-outline-level ()
  4912. "Compute the outline level of the heading at point.
  4913. This function assumes that the cursor is at the beginning of a line matched
  4914. by `outline-regexp'. Otherwise it returns garbage.
  4915. If this is called at a normal headline, the level is the number of stars.
  4916. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  4917. (save-excursion
  4918. (looking-at outline-regexp)
  4919. (1- (- (match-end 0) (match-beginning 0)))))
  4920. (defvar org-font-lock-keywords nil)
  4921. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4922. "Regular expression matching a property line.")
  4923. (defvar org-font-lock-hook nil
  4924. "Functions to be called for special font lock stuff.")
  4925. (defvar org-font-lock-set-keywords-hook nil
  4926. "Functions that can manipulate `org-font-lock-extra-keywords'.
  4927. This is calles after `org-font-lock-extra-keywords' is defined, but before
  4928. it is installed to be used by font lock. This can be useful if something
  4929. needs to be inserted at a specific position in the font-lock sequence.")
  4930. (defun org-font-lock-hook (limit)
  4931. (run-hook-with-args 'org-font-lock-hook limit))
  4932. (defun org-set-font-lock-defaults ()
  4933. (let* ((em org-fontify-emphasized-text)
  4934. (lk org-activate-links)
  4935. (org-font-lock-extra-keywords
  4936. (list
  4937. ;; Call the hook
  4938. '(org-font-lock-hook)
  4939. ;; Headlines
  4940. `(,(if org-fontify-whole-heading-line
  4941. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4942. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4943. (1 (org-get-level-face 1))
  4944. (2 (org-get-level-face 2))
  4945. (3 (org-get-level-face 3)))
  4946. ;; Table lines
  4947. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4948. (1 'org-table t))
  4949. ;; Table internals
  4950. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4951. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4952. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4953. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  4954. ;; Drawers
  4955. (list org-drawer-regexp '(0 'org-special-keyword t))
  4956. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4957. ;; Properties
  4958. (list org-property-re
  4959. '(1 'org-special-keyword t)
  4960. '(3 'org-property-value t))
  4961. ;; Links
  4962. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4963. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4964. (if (memq 'plain lk) '(org-activate-plain-links))
  4965. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4966. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4967. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4968. (if (memq 'footnote lk) '(org-activate-footnote-links))
  4969. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4970. '(org-hide-wide-columns (0 nil append))
  4971. ;; TODO lines
  4972. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4973. '(1 (org-get-todo-face 1) t))
  4974. ;; DONE
  4975. (if org-fontify-done-headline
  4976. (list (concat "^[*]+ +\\<\\("
  4977. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4978. "\\)\\(.*\\)")
  4979. '(2 'org-headline-done t))
  4980. nil)
  4981. ;; Priorities
  4982. '(org-font-lock-add-priority-faces)
  4983. ;; Tags
  4984. '(org-font-lock-add-tag-faces)
  4985. ;; Special keywords
  4986. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4987. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4988. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4989. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4990. ;; Emphasis
  4991. (if em
  4992. (if (featurep 'xemacs)
  4993. '(org-do-emphasis-faces (0 nil append))
  4994. '(org-do-emphasis-faces)))
  4995. ;; Checkboxes
  4996. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  4997. 1 'org-checkbox prepend)
  4998. (if (cdr (assq 'checkbox org-list-automatic-rules))
  4999. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5000. (0 (org-get-checkbox-statistics-face) t)))
  5001. ;; Description list items
  5002. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5003. 1 'bold prepend)
  5004. ;; ARCHIVEd headings
  5005. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  5006. '(1 'org-archived prepend))
  5007. ;; Specials
  5008. '(org-do-latex-and-special-faces)
  5009. '(org-fontify-entities)
  5010. '(org-raise-scripts)
  5011. ;; Code
  5012. '(org-activate-code (1 'org-code t))
  5013. ;; COMMENT
  5014. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  5015. "\\|" org-quote-string "\\)\\>")
  5016. '(1 'org-special-keyword t))
  5017. '("^#.*" (0 'font-lock-comment-face t))
  5018. ;; Blocks and meta lines
  5019. '(org-fontify-meta-lines-and-blocks)
  5020. )))
  5021. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5022. (run-hooks 'org-font-lock-set-keywords-hook)
  5023. ;; Now set the full font-lock-keywords
  5024. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5025. (org-set-local 'font-lock-defaults
  5026. '(org-font-lock-keywords t nil nil backward-paragraph))
  5027. (kill-local-variable 'font-lock-keywords) nil))
  5028. (defun org-toggle-pretty-entities ()
  5029. "Toggle the composition display of entities as UTF8 characters."
  5030. (interactive)
  5031. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5032. (org-restart-font-lock)
  5033. (if org-pretty-entities
  5034. (message "Entities are displayed as UTF8 characers")
  5035. (save-restriction
  5036. (widen)
  5037. (org-decompose-region (point-min) (point-max))
  5038. (message "Entities are displayed plain"))))
  5039. (defun org-fontify-entities (limit)
  5040. "Find an entity to fontify."
  5041. (let (ee)
  5042. (when org-pretty-entities
  5043. (catch 'match
  5044. (while (re-search-forward
  5045. "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
  5046. limit t)
  5047. (if (and (not (org-in-indented-comment-line))
  5048. (setq ee (org-entity-get (match-string 1)))
  5049. (= (length (nth 6 ee)) 1))
  5050. (progn
  5051. (add-text-properties
  5052. (match-beginning 0) (match-end 1)
  5053. (list 'font-lock-fontified t))
  5054. (compose-region (match-beginning 0) (match-end 1)
  5055. (nth 6 ee) nil)
  5056. (backward-char 1)
  5057. (throw 'match t))))
  5058. nil))))
  5059. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5060. "Fontify string S like in Org-mode."
  5061. (with-temp-buffer
  5062. (insert s)
  5063. (let ((org-odd-levels-only odd-levels))
  5064. (org-mode)
  5065. (font-lock-fontify-buffer)
  5066. (buffer-string))))
  5067. (defvar org-m nil)
  5068. (defvar org-l nil)
  5069. (defvar org-f nil)
  5070. (defun org-get-level-face (n)
  5071. "Get the right face for match N in font-lock matching of headlines."
  5072. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5073. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5074. (if org-cycle-level-faces
  5075. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5076. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5077. (cond
  5078. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5079. ((eq n 2) org-f)
  5080. (t (if org-level-color-stars-only nil org-f))))
  5081. (defun org-get-todo-face (kwd)
  5082. "Get the right face for a TODO keyword KWD.
  5083. If KWD is a number, get the corresponding match group."
  5084. (if (numberp kwd) (setq kwd (match-string kwd)))
  5085. (or (org-face-from-face-or-color
  5086. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5087. (and (member kwd org-done-keywords) 'org-done)
  5088. 'org-todo))
  5089. (defun org-face-from-face-or-color (context inherit face-or-color)
  5090. "Create a face list that inherits INHERIT, but sets the foreground color.
  5091. When FACE-OR-COLOR is not a string, just return it."
  5092. (if (stringp face-or-color)
  5093. (list :inherit inherit
  5094. (cdr (assoc context org-faces-easy-properties))
  5095. face-or-color)
  5096. face-or-color))
  5097. (defun org-font-lock-add-tag-faces (limit)
  5098. "Add the special tag faces."
  5099. (when (and org-tag-faces org-tags-special-faces-re)
  5100. (while (re-search-forward org-tags-special-faces-re limit t)
  5101. (add-text-properties (match-beginning 1) (match-end 1)
  5102. (list 'face (org-get-tag-face 1)
  5103. 'font-lock-fontified t))
  5104. (backward-char 1))))
  5105. (defun org-font-lock-add-priority-faces (limit)
  5106. "Add the special priority faces."
  5107. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5108. (add-text-properties
  5109. (match-beginning 0) (match-end 0)
  5110. (list 'face (or (org-face-from-face-or-color
  5111. 'priority 'org-special-keyword
  5112. (cdr (assoc (char-after (match-beginning 1))
  5113. org-priority-faces)))
  5114. 'org-special-keyword)
  5115. 'font-lock-fontified t))))
  5116. (defun org-get-tag-face (kwd)
  5117. "Get the right face for a TODO keyword KWD.
  5118. If KWD is a number, get the corresponding match group."
  5119. (if (numberp kwd) (setq kwd (match-string kwd)))
  5120. (or (org-face-from-face-or-color
  5121. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5122. 'org-tag))
  5123. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5124. "Remove fontification and activation overlays from links."
  5125. (font-lock-default-unfontify-region beg end)
  5126. (let* ((buffer-undo-list t)
  5127. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5128. (inhibit-modification-hooks t)
  5129. deactivate-mark buffer-file-name buffer-file-truename)
  5130. (org-decompose-region beg end)
  5131. (remove-text-properties
  5132. beg end
  5133. (if org-indent-mode
  5134. ;; also remove line-prefix and wrap-prefix properties
  5135. '(mouse-face t keymap t org-linked-text t
  5136. invisible t intangible t
  5137. line-prefix t wrap-prefix t
  5138. org-no-flyspell t org-emphasis t)
  5139. '(mouse-face t keymap t org-linked-text t
  5140. invisible t intangible t
  5141. org-no-flyspell t org-emphasis t)))
  5142. (org-remove-font-lock-display-properties beg end)))
  5143. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5144. ((raise 0.3) (height 0.7))
  5145. ((raise -0.5))
  5146. ((raise 0.5)))
  5147. "Display properties for showing superscripts and subscripts.")
  5148. (defun org-remove-font-lock-display-properties (beg end)
  5149. "Remove specific display properties that have been added by font lock.
  5150. The will remove the raise properties that are used to show superscripts
  5151. and subscripts."
  5152. (let (next prop)
  5153. (while (< beg end)
  5154. (setq next (next-single-property-change beg 'display nil end)
  5155. prop (get-text-property beg 'display))
  5156. (if (member prop org-script-display)
  5157. (put-text-property beg next 'display nil))
  5158. (setq beg next))))
  5159. (defun org-raise-scripts (limit)
  5160. "Add raise properties to sub/superscripts."
  5161. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5162. (if (re-search-forward
  5163. (if (eq org-use-sub-superscripts t)
  5164. org-match-substring-regexp
  5165. org-match-substring-with-braces-regexp)
  5166. limit t)
  5167. (let* ((pos (point)) table-p comment-p
  5168. (mpos (match-beginning 3))
  5169. (emph-p (get-text-property mpos 'org-emphasis))
  5170. (link-p (get-text-property mpos 'mouse-face))
  5171. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5172. (goto-char (point-at-bol))
  5173. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5174. comment-p (org-looking-at-p "[ \t]*#"))
  5175. (goto-char pos)
  5176. ;; FIXME: Should we go back one character here, for a_b^c
  5177. ;; (goto-char (1- pos)) ;????????????????????
  5178. (if (or comment-p emph-p link-p keyw-p)
  5179. t
  5180. (put-text-property (match-beginning 3) (match-end 0)
  5181. 'display
  5182. (if (equal (char-after (match-beginning 2)) ?^)
  5183. (nth (if table-p 3 1) org-script-display)
  5184. (nth (if table-p 2 0) org-script-display)))
  5185. (add-text-properties (match-beginning 2) (match-end 2)
  5186. (list 'invisible t
  5187. 'org-dwidth t 'org-dwidth-n 1))
  5188. (if (and (eq (char-after (match-beginning 3)) ?{)
  5189. (eq (char-before (match-end 3)) ?}))
  5190. (progn
  5191. (add-text-properties
  5192. (match-beginning 3) (1+ (match-beginning 3))
  5193. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5194. (add-text-properties
  5195. (1- (match-end 3)) (match-end 3)
  5196. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5197. t)))))
  5198. ;;;; Visibility cycling, including org-goto and indirect buffer
  5199. ;;; Cycling
  5200. (defvar org-cycle-global-status nil)
  5201. (make-variable-buffer-local 'org-cycle-global-status)
  5202. (defvar org-cycle-subtree-status nil)
  5203. (make-variable-buffer-local 'org-cycle-subtree-status)
  5204. ;;;###autoload
  5205. (defvar org-inlinetask-min-level)
  5206. (defun org-cycle (&optional arg)
  5207. "TAB-action and visibility cycling for Org-mode.
  5208. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5209. is outline visibility cycling, but it also invokes other actions
  5210. in special contexts.
  5211. - When this function is called with a prefix argument, rotate the entire
  5212. buffer through 3 states (global cycling)
  5213. 1. OVERVIEW: Show only top-level headlines.
  5214. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5215. 3. SHOW ALL: Show everything.
  5216. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5217. determined by the variable `org-startup-folded', and by any VISIBILITY
  5218. properties in the buffer.
  5219. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5220. including any drawers.
  5221. - When inside a table, re-align the table and move to the next field.
  5222. - When point is at the beginning of a headline, rotate the subtree started
  5223. by this line through 3 different states (local cycling)
  5224. 1. FOLDED: Only the main headline is shown.
  5225. 2. CHILDREN: The main headline and the direct children are shown.
  5226. From this state, you can move to one of the children
  5227. and zoom in further.
  5228. 3. SUBTREE: Show the entire subtree, including body text.
  5229. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5230. - When point is at the beginning of an empty headline and the variable
  5231. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5232. of the headline by demoting and promoting it to likely levels. This
  5233. speeds up creation document structure by pressing TAB once or several
  5234. times right after creating a new headline.
  5235. - When there is a numeric prefix, go up to a heading with level ARG, do
  5236. a `show-subtree' and return to the previous cursor position. If ARG
  5237. is negative, go up that many levels.
  5238. - When point is not at the beginning of a headline, execute the global
  5239. binding for TAB, which is re-indenting the line. See the option
  5240. `org-cycle-emulate-tab' for details.
  5241. - Special case: if point is at the beginning of the buffer and there is
  5242. no headline in line 1, this function will act as if called with prefix arg
  5243. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5244. But only if also the variable `org-cycle-global-at-bob' is t."
  5245. (interactive "P")
  5246. (org-load-modules-maybe)
  5247. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5248. (and org-cycle-level-after-item/entry-creation
  5249. (or (org-cycle-level)
  5250. (org-cycle-item-indentation))))
  5251. (let* ((limit-level
  5252. (or org-cycle-max-level
  5253. (and (boundp 'org-inlinetask-min-level)
  5254. org-inlinetask-min-level
  5255. (1- org-inlinetask-min-level))))
  5256. (nstars (and limit-level
  5257. (if org-odd-levels-only
  5258. (and limit-level (1- (* limit-level 2)))
  5259. limit-level)))
  5260. (outline-regexp
  5261. (if (not (org-mode-p))
  5262. outline-regexp
  5263. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5264. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5265. (not (looking-at outline-regexp))))
  5266. (org-cycle-hook
  5267. (if bob-special
  5268. (delq 'org-optimize-window-after-visibility-change
  5269. (copy-sequence org-cycle-hook))
  5270. org-cycle-hook))
  5271. (pos (point)))
  5272. (if (or bob-special (equal arg '(4)))
  5273. ;; special case: use global cycling
  5274. (setq arg t))
  5275. (cond
  5276. ((equal arg '(16))
  5277. (setq last-command 'dummy)
  5278. (org-set-startup-visibility)
  5279. (message "Startup visibility, plus VISIBILITY properties"))
  5280. ((equal arg '(64))
  5281. (show-all)
  5282. (message "Entire buffer visible, including drawers"))
  5283. ;; Table: enter it or move to the next field.
  5284. ((org-at-table-p 'any)
  5285. (if (org-at-table.el-p)
  5286. (message "Use C-c ' to edit table.el tables")
  5287. (if arg (org-table-edit-field t)
  5288. (org-table-justify-field-maybe)
  5289. (call-interactively 'org-table-next-field))))
  5290. ((run-hook-with-args-until-success
  5291. 'org-tab-after-check-for-table-hook))
  5292. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5293. ((eq arg t) (org-cycle-internal-global))
  5294. ;; Drawers: delegate to `org-flag-drawer'.
  5295. ((and org-drawers org-drawer-regexp
  5296. (save-excursion
  5297. (beginning-of-line 1)
  5298. (looking-at org-drawer-regexp)))
  5299. (org-flag-drawer ; toggle block visibility
  5300. (not (get-char-property (match-end 0) 'invisible))))
  5301. ;; Show-subtree, ARG levels up from here.
  5302. ((integerp arg)
  5303. (save-excursion
  5304. (org-back-to-heading)
  5305. (outline-up-heading (if (< arg 0) (- arg)
  5306. (- (funcall outline-level) arg)))
  5307. (org-show-subtree)))
  5308. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5309. ((and (featurep 'org-inlinetask)
  5310. (org-inlinetask-at-task-p)
  5311. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5312. (org-inlinetask-toggle-visibility))
  5313. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5314. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5315. (save-excursion (beginning-of-line 1)
  5316. (looking-at outline-regexp)))
  5317. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5318. (org-cycle-internal-local))
  5319. ;; From there: TAB emulation and template completion.
  5320. (buffer-read-only (org-back-to-heading))
  5321. ((run-hook-with-args-until-success
  5322. 'org-tab-after-check-for-cycling-hook))
  5323. ((org-try-structure-completion))
  5324. ((org-try-cdlatex-tab))
  5325. ((run-hook-with-args-until-success
  5326. 'org-tab-before-tab-emulation-hook))
  5327. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5328. (or (not (bolp))
  5329. (not (looking-at outline-regexp))))
  5330. (call-interactively (global-key-binding "\t")))
  5331. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5332. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5333. (or (and (eq org-cycle-emulate-tab 'white)
  5334. (= (match-end 0) (point-at-eol)))
  5335. (and (eq org-cycle-emulate-tab 'whitestart)
  5336. (>= (match-end 0) pos))))
  5337. t
  5338. (eq org-cycle-emulate-tab t))
  5339. (call-interactively (global-key-binding "\t")))
  5340. (t (save-excursion
  5341. (org-back-to-heading)
  5342. (org-cycle)))))))
  5343. (defun org-cycle-internal-global ()
  5344. "Do the global cycling action."
  5345. (cond
  5346. ((and (eq last-command this-command)
  5347. (eq org-cycle-global-status 'overview))
  5348. ;; We just created the overview - now do table of contents
  5349. ;; This can be slow in very large buffers, so indicate action
  5350. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5351. (message "CONTENTS...")
  5352. (org-content)
  5353. (message "CONTENTS...done")
  5354. (setq org-cycle-global-status 'contents)
  5355. (run-hook-with-args 'org-cycle-hook 'contents))
  5356. ((and (eq last-command this-command)
  5357. (eq org-cycle-global-status 'contents))
  5358. ;; We just showed the table of contents - now show everything
  5359. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5360. (show-all)
  5361. (message "SHOW ALL")
  5362. (setq org-cycle-global-status 'all)
  5363. (run-hook-with-args 'org-cycle-hook 'all))
  5364. (t
  5365. ;; Default action: go to overview
  5366. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5367. (org-overview)
  5368. (message "OVERVIEW")
  5369. (setq org-cycle-global-status 'overview)
  5370. (run-hook-with-args 'org-cycle-hook 'overview))))
  5371. (defun org-cycle-internal-local ()
  5372. "Do the local cycling action."
  5373. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5374. ;; First, determine end of headline (EOH), end of subtree or item
  5375. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5376. (save-excursion
  5377. (if (org-at-item-p)
  5378. (progn
  5379. (beginning-of-line)
  5380. (setq struct (org-list-struct))
  5381. (setq eoh (point-at-eol))
  5382. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5383. (setq has-children (org-list-has-child-p (point) struct)))
  5384. (org-back-to-heading)
  5385. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5386. (setq eos (save-excursion
  5387. (org-end-of-subtree t)
  5388. (unless (eobp)
  5389. (skip-chars-forward " \t\n"))
  5390. (if (eobp) (point) (1- (point)))))
  5391. (setq has-children
  5392. (or (save-excursion
  5393. (let ((level (funcall outline-level)))
  5394. (outline-next-heading)
  5395. (and (org-at-heading-p t)
  5396. (> (funcall outline-level) level))))
  5397. (save-excursion
  5398. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5399. ;; Determine end invisible part of buffer (EOL)
  5400. (beginning-of-line 2)
  5401. ;; XEmacs doesn't have `next-single-char-property-change'
  5402. (if (featurep 'xemacs)
  5403. (while (and (not (eobp)) ;; this is like `next-line'
  5404. (get-char-property (1- (point)) 'invisible))
  5405. (beginning-of-line 2))
  5406. (while (and (not (eobp)) ;; this is like `next-line'
  5407. (get-char-property (1- (point)) 'invisible))
  5408. (goto-char (next-single-char-property-change (point) 'invisible))
  5409. (and (eolp) (beginning-of-line 2))))
  5410. (setq eol (point)))
  5411. ;; Find out what to do next and set `this-command'
  5412. (cond
  5413. ((= eos eoh)
  5414. ;; Nothing is hidden behind this heading
  5415. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5416. (message "EMPTY ENTRY")
  5417. (setq org-cycle-subtree-status nil)
  5418. (save-excursion
  5419. (goto-char eos)
  5420. (outline-next-heading)
  5421. (if (outline-invisible-p) (org-flag-heading nil))))
  5422. ((and (or (>= eol eos)
  5423. (not (string-match "\\S-" (buffer-substring eol eos))))
  5424. (or has-children
  5425. (not (setq children-skipped
  5426. org-cycle-skip-children-state-if-no-children))))
  5427. ;; Entire subtree is hidden in one line: children view
  5428. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5429. (if (org-at-item-p)
  5430. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5431. (org-show-entry)
  5432. (show-children)
  5433. ;; Fold every list in subtree to top-level items.
  5434. (when (eq org-cycle-include-plain-lists 'integrate)
  5435. (save-excursion
  5436. (org-back-to-heading)
  5437. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5438. (beginning-of-line 1)
  5439. (let* ((struct (org-list-struct))
  5440. (prevs (org-list-prevs-alist struct))
  5441. (end (org-list-get-bottom-point struct)))
  5442. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5443. (org-list-get-all-items (point) struct prevs))
  5444. (goto-char end))))))
  5445. (message "CHILDREN")
  5446. (save-excursion
  5447. (goto-char eos)
  5448. (outline-next-heading)
  5449. (if (outline-invisible-p) (org-flag-heading nil)))
  5450. (setq org-cycle-subtree-status 'children)
  5451. (run-hook-with-args 'org-cycle-hook 'children))
  5452. ((or children-skipped
  5453. (and (eq last-command this-command)
  5454. (eq org-cycle-subtree-status 'children)))
  5455. ;; We just showed the children, or no children are there,
  5456. ;; now show everything.
  5457. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5458. (outline-flag-region eoh eos nil)
  5459. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5460. (setq org-cycle-subtree-status 'subtree)
  5461. (run-hook-with-args 'org-cycle-hook 'subtree))
  5462. (t
  5463. ;; Default action: hide the subtree.
  5464. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5465. (outline-flag-region eoh eos t)
  5466. (message "FOLDED")
  5467. (setq org-cycle-subtree-status 'folded)
  5468. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5469. ;;;###autoload
  5470. (defun org-global-cycle (&optional arg)
  5471. "Cycle the global visibility. For details see `org-cycle'.
  5472. With \\[universal-argument] prefix arg, switch to startup visibility.
  5473. With a numeric prefix, show all headlines up to that level."
  5474. (interactive "P")
  5475. (let ((org-cycle-include-plain-lists
  5476. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5477. (cond
  5478. ((integerp arg)
  5479. (show-all)
  5480. (hide-sublevels arg)
  5481. (setq org-cycle-global-status 'contents))
  5482. ((equal arg '(4))
  5483. (org-set-startup-visibility)
  5484. (message "Startup visibility, plus VISIBILITY properties."))
  5485. (t
  5486. (org-cycle '(4))))))
  5487. (defun org-set-startup-visibility ()
  5488. "Set the visibility required by startup options and properties."
  5489. (cond
  5490. ((eq org-startup-folded t)
  5491. (org-cycle '(4)))
  5492. ((eq org-startup-folded 'content)
  5493. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5494. (org-cycle '(4)) (org-cycle '(4)))))
  5495. (unless (eq org-startup-folded 'showeverything)
  5496. (if org-hide-block-startup (org-hide-block-all))
  5497. (org-set-visibility-according-to-property 'no-cleanup)
  5498. (org-cycle-hide-archived-subtrees 'all)
  5499. (org-cycle-hide-drawers 'all)
  5500. (org-cycle-show-empty-lines t)))
  5501. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5502. "Switch subtree visibilities according to :VISIBILITY: property."
  5503. (interactive)
  5504. (let (org-show-entry-below state)
  5505. (save-excursion
  5506. (goto-char (point-min))
  5507. (while (re-search-forward
  5508. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5509. nil t)
  5510. (setq state (match-string 1))
  5511. (save-excursion
  5512. (org-back-to-heading t)
  5513. (hide-subtree)
  5514. (org-reveal)
  5515. (cond
  5516. ((equal state '("fold" "folded"))
  5517. (hide-subtree))
  5518. ((equal state "children")
  5519. (org-show-hidden-entry)
  5520. (show-children))
  5521. ((equal state "content")
  5522. (save-excursion
  5523. (save-restriction
  5524. (org-narrow-to-subtree)
  5525. (org-content))))
  5526. ((member state '("all" "showall"))
  5527. (show-subtree)))))
  5528. (unless no-cleanup
  5529. (org-cycle-hide-archived-subtrees 'all)
  5530. (org-cycle-hide-drawers 'all)
  5531. (org-cycle-show-empty-lines 'all)))))
  5532. (defun org-overview ()
  5533. "Switch to overview mode, showing only top-level headlines.
  5534. Really, this shows all headlines with level equal or greater than the level
  5535. of the first headline in the buffer. This is important, because if the
  5536. first headline is not level one, then (hide-sublevels 1) gives confusing
  5537. results."
  5538. (interactive)
  5539. (let ((level (save-excursion
  5540. (goto-char (point-min))
  5541. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5542. (progn
  5543. (goto-char (match-beginning 0))
  5544. (funcall outline-level))))))
  5545. (and level (hide-sublevels level))))
  5546. (defun org-content (&optional arg)
  5547. "Show all headlines in the buffer, like a table of contents.
  5548. With numerical argument N, show content up to level N."
  5549. (interactive "P")
  5550. (save-excursion
  5551. ;; Visit all headings and show their offspring
  5552. (and (integerp arg) (org-overview))
  5553. (goto-char (point-max))
  5554. (catch 'exit
  5555. (while (and (progn (condition-case nil
  5556. (outline-previous-visible-heading 1)
  5557. (error (goto-char (point-min))))
  5558. t)
  5559. (looking-at outline-regexp))
  5560. (if (integerp arg)
  5561. (show-children (1- arg))
  5562. (show-branches))
  5563. (if (bobp) (throw 'exit nil))))))
  5564. (defun org-optimize-window-after-visibility-change (state)
  5565. "Adjust the window after a change in outline visibility.
  5566. This function is the default value of the hook `org-cycle-hook'."
  5567. (when (get-buffer-window (current-buffer))
  5568. (cond
  5569. ((eq state 'content) nil)
  5570. ((eq state 'all) nil)
  5571. ((eq state 'folded) nil)
  5572. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5573. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5574. (defun org-remove-empty-overlays-at (pos)
  5575. "Remove outline overlays that do not contain non-white stuff."
  5576. (mapc
  5577. (lambda (o)
  5578. (and (eq 'outline (overlay-get o 'invisible))
  5579. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5580. (overlay-end o))))
  5581. (delete-overlay o)))
  5582. (overlays-at pos)))
  5583. (defun org-clean-visibility-after-subtree-move ()
  5584. "Fix visibility issues after moving a subtree."
  5585. ;; First, find a reasonable region to look at:
  5586. ;; Start two siblings above, end three below
  5587. (let* ((beg (save-excursion
  5588. (and (org-get-last-sibling)
  5589. (org-get-last-sibling))
  5590. (point)))
  5591. (end (save-excursion
  5592. (and (org-get-next-sibling)
  5593. (org-get-next-sibling)
  5594. (org-get-next-sibling))
  5595. (if (org-at-heading-p)
  5596. (point-at-eol)
  5597. (point))))
  5598. (level (looking-at "\\*+"))
  5599. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5600. (save-excursion
  5601. (save-restriction
  5602. (narrow-to-region beg end)
  5603. (when re
  5604. ;; Properly fold already folded siblings
  5605. (goto-char (point-min))
  5606. (while (re-search-forward re nil t)
  5607. (if (and (not (outline-invisible-p))
  5608. (save-excursion
  5609. (goto-char (point-at-eol)) (outline-invisible-p)))
  5610. (hide-entry))))
  5611. (org-cycle-show-empty-lines 'overview)
  5612. (org-cycle-hide-drawers 'overview)))))
  5613. (defun org-cycle-show-empty-lines (state)
  5614. "Show empty lines above all visible headlines.
  5615. The region to be covered depends on STATE when called through
  5616. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5617. entire buffer covered. Note that an empty line is only shown if there
  5618. are at least `org-cycle-separator-lines' empty lines before the headline."
  5619. (when (not (= org-cycle-separator-lines 0))
  5620. (save-excursion
  5621. (let* ((n (abs org-cycle-separator-lines))
  5622. (re (cond
  5623. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5624. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5625. (t (let ((ns (number-to-string (- n 2))))
  5626. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5627. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5628. beg end b e)
  5629. (cond
  5630. ((memq state '(overview contents t))
  5631. (setq beg (point-min) end (point-max)))
  5632. ((memq state '(children folded))
  5633. (setq beg (point) end (progn (org-end-of-subtree t t)
  5634. (beginning-of-line 2)
  5635. (point)))))
  5636. (when beg
  5637. (goto-char beg)
  5638. (while (re-search-forward re end t)
  5639. (unless (get-char-property (match-end 1) 'invisible)
  5640. (setq e (match-end 1))
  5641. (if (< org-cycle-separator-lines 0)
  5642. (setq b (save-excursion
  5643. (goto-char (match-beginning 0))
  5644. (org-back-over-empty-lines)
  5645. (if (save-excursion
  5646. (goto-char (max (point-min) (1- (point))))
  5647. (org-on-heading-p))
  5648. (1- (point))
  5649. (point))))
  5650. (setq b (match-beginning 1)))
  5651. (outline-flag-region b e nil)))))))
  5652. ;; Never hide empty lines at the end of the file.
  5653. (save-excursion
  5654. (goto-char (point-max))
  5655. (outline-previous-heading)
  5656. (outline-end-of-heading)
  5657. (if (and (looking-at "[ \t\n]+")
  5658. (= (match-end 0) (point-max)))
  5659. (outline-flag-region (point) (match-end 0) nil))))
  5660. (defun org-show-empty-lines-in-parent ()
  5661. "Move to the parent and re-show empty lines before visible headlines."
  5662. (save-excursion
  5663. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5664. (org-cycle-show-empty-lines context))))
  5665. (defun org-files-list ()
  5666. "Return `org-agenda-files' list, plus all open org-mode files.
  5667. This is useful for operations that need to scan all of a user's
  5668. open and agenda-wise Org files."
  5669. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5670. (dolist (buf (buffer-list))
  5671. (with-current-buffer buf
  5672. (if (and (org-mode-p) (buffer-file-name))
  5673. (let ((file (expand-file-name (buffer-file-name))))
  5674. (unless (member file files)
  5675. (push file files))))))
  5676. files))
  5677. (defsubst org-entry-beginning-position ()
  5678. "Return the beginning position of the current entry."
  5679. (save-excursion (outline-back-to-heading t) (point)))
  5680. (defsubst org-entry-end-position ()
  5681. "Return the end position of the current entry."
  5682. (save-excursion (outline-next-heading) (point)))
  5683. (defun org-cycle-hide-drawers (state)
  5684. "Re-hide all drawers after a visibility state change."
  5685. (when (and (org-mode-p)
  5686. (not (memq state '(overview folded contents))))
  5687. (save-excursion
  5688. (let* ((globalp (memq state '(contents all)))
  5689. (beg (if globalp (point-min) (point)))
  5690. (end (if globalp (point-max)
  5691. (if (eq state 'children)
  5692. (save-excursion (outline-next-heading) (point))
  5693. (org-end-of-subtree t)))))
  5694. (goto-char beg)
  5695. (while (re-search-forward org-drawer-regexp end t)
  5696. (org-flag-drawer t))))))
  5697. (defun org-flag-drawer (flag)
  5698. (save-excursion
  5699. (beginning-of-line 1)
  5700. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5701. (let ((b (match-end 0))
  5702. (outline-regexp org-outline-regexp))
  5703. (if (re-search-forward
  5704. "^[ \t]*:END:"
  5705. (save-excursion (outline-next-heading) (point)) t)
  5706. (outline-flag-region b (point-at-eol) flag)
  5707. (error ":END: line missing at position %s" b))))))
  5708. (defun org-subtree-end-visible-p ()
  5709. "Is the end of the current subtree visible?"
  5710. (pos-visible-in-window-p
  5711. (save-excursion (org-end-of-subtree t) (point))))
  5712. (defun org-first-headline-recenter (&optional N)
  5713. "Move cursor to the first headline and recenter the headline.
  5714. Optional argument N means put the headline into the Nth line of the window."
  5715. (goto-char (point-min))
  5716. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5717. (beginning-of-line)
  5718. (recenter (prefix-numeric-value N))))
  5719. ;;; Saving and restoring visibility
  5720. (defun org-outline-overlay-data (&optional use-markers)
  5721. "Return a list of the locations of all outline overlays.
  5722. These are overlays with the `invisible' property value `outline'.
  5723. The return value is a list of cons cells, with start and stop
  5724. positions for each overlay.
  5725. If USE-MARKERS is set, return the positions as markers."
  5726. (let (beg end)
  5727. (save-excursion
  5728. (save-restriction
  5729. (widen)
  5730. (delq nil
  5731. (mapcar (lambda (o)
  5732. (when (eq (overlay-get o 'invisible) 'outline)
  5733. (setq beg (overlay-start o)
  5734. end (overlay-end o))
  5735. (and beg end (> end beg)
  5736. (if use-markers
  5737. (cons (move-marker (make-marker) beg)
  5738. (move-marker (make-marker) end))
  5739. (cons beg end)))))
  5740. (overlays-in (point-min) (point-max))))))))
  5741. (defun org-set-outline-overlay-data (data)
  5742. "Create visibility overlays for all positions in DATA.
  5743. DATA should have been made by `org-outline-overlay-data'."
  5744. (let (o)
  5745. (save-excursion
  5746. (save-restriction
  5747. (widen)
  5748. (show-all)
  5749. (mapc (lambda (c)
  5750. (setq o (make-overlay (car c) (cdr c)))
  5751. (overlay-put o 'invisible 'outline))
  5752. data)))))
  5753. ;;; Folding of blocks
  5754. (defconst org-block-regexp
  5755. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  5756. "Regular expression for hiding blocks.")
  5757. (defvar org-hide-block-overlays nil
  5758. "Overlays hiding blocks.")
  5759. (make-variable-buffer-local 'org-hide-block-overlays)
  5760. (defun org-block-map (function &optional start end)
  5761. "Call FUNCTION at the head of all source blocks in the current buffer.
  5762. Optional arguments START and END can be used to limit the range."
  5763. (let ((start (or start (point-min)))
  5764. (end (or end (point-max))))
  5765. (save-excursion
  5766. (goto-char start)
  5767. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5768. (save-excursion
  5769. (save-match-data
  5770. (goto-char (match-beginning 0))
  5771. (funcall function)))))))
  5772. (defun org-hide-block-toggle-all ()
  5773. "Toggle the visibility of all blocks in the current buffer."
  5774. (org-block-map #'org-hide-block-toggle))
  5775. (defun org-hide-block-all ()
  5776. "Fold all blocks in the current buffer."
  5777. (interactive)
  5778. (org-show-block-all)
  5779. (org-block-map #'org-hide-block-toggle-maybe))
  5780. (defun org-show-block-all ()
  5781. "Unfold all blocks in the current buffer."
  5782. (interactive)
  5783. (mapc 'delete-overlay org-hide-block-overlays)
  5784. (setq org-hide-block-overlays nil))
  5785. (defun org-hide-block-toggle-maybe ()
  5786. "Toggle visibility of block at point."
  5787. (interactive)
  5788. (let ((case-fold-search t))
  5789. (if (save-excursion
  5790. (beginning-of-line 1)
  5791. (looking-at org-block-regexp))
  5792. (progn (org-hide-block-toggle)
  5793. t) ;; to signal that we took action
  5794. nil))) ;; to signal that we did not
  5795. (defun org-hide-block-toggle (&optional force)
  5796. "Toggle the visibility of the current block."
  5797. (interactive)
  5798. (save-excursion
  5799. (beginning-of-line)
  5800. (if (re-search-forward org-block-regexp nil t)
  5801. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5802. (end (match-end 0)) ;; end of entire body
  5803. ov)
  5804. (if (memq t (mapcar (lambda (overlay)
  5805. (eq (overlay-get overlay 'invisible)
  5806. 'org-hide-block))
  5807. (overlays-at start)))
  5808. (if (or (not force) (eq force 'off))
  5809. (mapc (lambda (ov)
  5810. (when (member ov org-hide-block-overlays)
  5811. (setq org-hide-block-overlays
  5812. (delq ov org-hide-block-overlays)))
  5813. (when (eq (overlay-get ov 'invisible)
  5814. 'org-hide-block)
  5815. (delete-overlay ov)))
  5816. (overlays-at start)))
  5817. (setq ov (make-overlay start end))
  5818. (overlay-put ov 'invisible 'org-hide-block)
  5819. ;; make the block accessible to isearch
  5820. (overlay-put
  5821. ov 'isearch-open-invisible
  5822. (lambda (ov)
  5823. (when (member ov org-hide-block-overlays)
  5824. (setq org-hide-block-overlays
  5825. (delq ov org-hide-block-overlays)))
  5826. (when (eq (overlay-get ov 'invisible)
  5827. 'org-hide-block)
  5828. (delete-overlay ov))))
  5829. (push ov org-hide-block-overlays)))
  5830. (error "Not looking at a source block"))))
  5831. ;; org-tab-after-check-for-cycling-hook
  5832. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5833. ;; Remove overlays when changing major mode
  5834. (add-hook 'org-mode-hook
  5835. (lambda () (org-add-hook 'change-major-mode-hook
  5836. 'org-show-block-all 'append 'local)))
  5837. ;;; Org-goto
  5838. (defvar org-goto-window-configuration nil)
  5839. (defvar org-goto-marker nil)
  5840. (defvar org-goto-map
  5841. (let ((map (make-sparse-keymap)))
  5842. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5843. (while (setq cmd (pop cmds))
  5844. (substitute-key-definition cmd cmd map global-map)))
  5845. (suppress-keymap map)
  5846. (org-defkey map "\C-m" 'org-goto-ret)
  5847. (org-defkey map [(return)] 'org-goto-ret)
  5848. (org-defkey map [(left)] 'org-goto-left)
  5849. (org-defkey map [(right)] 'org-goto-right)
  5850. (org-defkey map [(control ?g)] 'org-goto-quit)
  5851. (org-defkey map "\C-i" 'org-cycle)
  5852. (org-defkey map [(tab)] 'org-cycle)
  5853. (org-defkey map [(down)] 'outline-next-visible-heading)
  5854. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5855. (if org-goto-auto-isearch
  5856. (if (fboundp 'define-key-after)
  5857. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5858. nil)
  5859. (org-defkey map "q" 'org-goto-quit)
  5860. (org-defkey map "n" 'outline-next-visible-heading)
  5861. (org-defkey map "p" 'outline-previous-visible-heading)
  5862. (org-defkey map "f" 'outline-forward-same-level)
  5863. (org-defkey map "b" 'outline-backward-same-level)
  5864. (org-defkey map "u" 'outline-up-heading))
  5865. (org-defkey map "/" 'org-occur)
  5866. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5867. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5868. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5869. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5870. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5871. map))
  5872. (defconst org-goto-help
  5873. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5874. RET=jump to location [Q]uit and return to previous location
  5875. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5876. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5877. ;; FIXME: Docstring does not mention both interfaces
  5878. (defun org-goto (&optional alternative-interface)
  5879. "Look up a different location in the current file, keeping current visibility.
  5880. When you want look-up or go to a different location in a document, the
  5881. fastest way is often to fold the entire buffer and then dive into the tree.
  5882. This method has the disadvantage, that the previous location will be folded,
  5883. which may not be what you want.
  5884. This command works around this by showing a copy of the current buffer
  5885. in an indirect buffer, in overview mode. You can dive into the tree in
  5886. that copy, use org-occur and incremental search to find a location.
  5887. When pressing RET or `Q', the command returns to the original buffer in
  5888. which the visibility is still unchanged. After RET is will also jump to
  5889. the location selected in the indirect buffer and expose the headline
  5890. hierarchy above."
  5891. (interactive "P")
  5892. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5893. (org-refile-use-outline-path t)
  5894. (org-refile-target-verify-function nil)
  5895. (interface
  5896. (if (not alternative-interface)
  5897. org-goto-interface
  5898. (if (eq org-goto-interface 'outline)
  5899. 'outline-path-completion
  5900. 'outline)))
  5901. (org-goto-start-pos (point))
  5902. (selected-point
  5903. (if (eq interface 'outline)
  5904. (car (org-get-location (current-buffer) org-goto-help))
  5905. (let ((pa (org-refile-get-location "Goto")))
  5906. (org-refile-check-position pa)
  5907. (nth 3 pa)))))
  5908. (if selected-point
  5909. (progn
  5910. (org-mark-ring-push org-goto-start-pos)
  5911. (goto-char selected-point)
  5912. (if (or (outline-invisible-p) (org-invisible-p2))
  5913. (org-show-context 'org-goto)))
  5914. (message "Quit"))))
  5915. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5916. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5917. (defvar org-goto-local-auto-isearch-map) ; defined below
  5918. (defun org-get-location (buf help)
  5919. "Let the user select a location in the Org-mode buffer BUF.
  5920. This function uses a recursive edit. It returns the selected position
  5921. or nil."
  5922. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5923. (isearch-hide-immediately nil)
  5924. (isearch-search-fun-function
  5925. (lambda () 'org-goto-local-search-headings))
  5926. (org-goto-selected-point org-goto-exit-command)
  5927. (pop-up-frames nil)
  5928. (special-display-buffer-names nil)
  5929. (special-display-regexps nil)
  5930. (special-display-function nil))
  5931. (save-excursion
  5932. (save-window-excursion
  5933. (delete-other-windows)
  5934. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5935. (switch-to-buffer
  5936. (condition-case nil
  5937. (make-indirect-buffer (current-buffer) "*org-goto*")
  5938. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5939. (with-output-to-temp-buffer "*Help*"
  5940. (princ help))
  5941. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5942. (setq buffer-read-only nil)
  5943. (let ((org-startup-truncated t)
  5944. (org-startup-folded nil)
  5945. (org-startup-align-all-tables nil))
  5946. (org-mode)
  5947. (org-overview))
  5948. (setq buffer-read-only t)
  5949. (if (and (boundp 'org-goto-start-pos)
  5950. (integer-or-marker-p org-goto-start-pos))
  5951. (let ((org-show-hierarchy-above t)
  5952. (org-show-siblings t)
  5953. (org-show-following-heading t))
  5954. (goto-char org-goto-start-pos)
  5955. (and (outline-invisible-p) (org-show-context)))
  5956. (goto-char (point-min)))
  5957. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5958. (message "Select location and press RET")
  5959. (use-local-map org-goto-map)
  5960. (recursive-edit)
  5961. ))
  5962. (kill-buffer "*org-goto*")
  5963. (cons org-goto-selected-point org-goto-exit-command)))
  5964. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5965. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5966. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5967. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5968. (defun org-goto-local-search-headings (string bound noerror)
  5969. "Search and make sure that any matches are in headlines."
  5970. (catch 'return
  5971. (while (if isearch-forward
  5972. (search-forward string bound noerror)
  5973. (search-backward string bound noerror))
  5974. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5975. (and (member :headline context)
  5976. (not (member :tags context))))
  5977. (throw 'return (point))))))
  5978. (defun org-goto-local-auto-isearch ()
  5979. "Start isearch."
  5980. (interactive)
  5981. (goto-char (point-min))
  5982. (let ((keys (this-command-keys)))
  5983. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5984. (isearch-mode t)
  5985. (isearch-process-search-char (string-to-char keys)))))
  5986. (defun org-goto-ret (&optional arg)
  5987. "Finish `org-goto' by going to the new location."
  5988. (interactive "P")
  5989. (setq org-goto-selected-point (point)
  5990. org-goto-exit-command 'return)
  5991. (throw 'exit nil))
  5992. (defun org-goto-left ()
  5993. "Finish `org-goto' by going to the new location."
  5994. (interactive)
  5995. (if (org-on-heading-p)
  5996. (progn
  5997. (beginning-of-line 1)
  5998. (setq org-goto-selected-point (point)
  5999. org-goto-exit-command 'left)
  6000. (throw 'exit nil))
  6001. (error "Not on a heading")))
  6002. (defun org-goto-right ()
  6003. "Finish `org-goto' by going to the new location."
  6004. (interactive)
  6005. (if (org-on-heading-p)
  6006. (progn
  6007. (setq org-goto-selected-point (point)
  6008. org-goto-exit-command 'right)
  6009. (throw 'exit nil))
  6010. (error "Not on a heading")))
  6011. (defun org-goto-quit ()
  6012. "Finish `org-goto' without cursor motion."
  6013. (interactive)
  6014. (setq org-goto-selected-point nil)
  6015. (setq org-goto-exit-command 'quit)
  6016. (throw 'exit nil))
  6017. ;;; Indirect buffer display of subtrees
  6018. (defvar org-indirect-dedicated-frame nil
  6019. "This is the frame being used for indirect tree display.")
  6020. (defvar org-last-indirect-buffer nil)
  6021. (defun org-tree-to-indirect-buffer (&optional arg)
  6022. "Create indirect buffer and narrow it to current subtree.
  6023. With numerical prefix ARG, go up to this level and then take that tree.
  6024. If ARG is negative, go up that many levels.
  6025. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6026. indirect buffer previously made with this command, to avoid proliferation of
  6027. indirect buffers. However, when you call the command with a \
  6028. \\[universal-argument] prefix, or
  6029. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6030. is kept so that you can work with several indirect buffers at the same time.
  6031. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6032. \\[universal-argument] prefix also
  6033. requests that a new frame be made for the new buffer, so that the dedicated
  6034. frame is not changed."
  6035. (interactive "P")
  6036. (let ((cbuf (current-buffer))
  6037. (cwin (selected-window))
  6038. (pos (point))
  6039. beg end level heading ibuf)
  6040. (save-excursion
  6041. (org-back-to-heading t)
  6042. (when (numberp arg)
  6043. (setq level (org-outline-level))
  6044. (if (< arg 0) (setq arg (+ level arg)))
  6045. (while (> (setq level (org-outline-level)) arg)
  6046. (outline-up-heading 1 t)))
  6047. (setq beg (point)
  6048. heading (org-get-heading))
  6049. (org-end-of-subtree t t)
  6050. (if (org-on-heading-p) (backward-char 1))
  6051. (setq end (point)))
  6052. (if (and (buffer-live-p org-last-indirect-buffer)
  6053. (not (eq org-indirect-buffer-display 'new-frame))
  6054. (not arg))
  6055. (kill-buffer org-last-indirect-buffer))
  6056. (setq ibuf (org-get-indirect-buffer cbuf)
  6057. org-last-indirect-buffer ibuf)
  6058. (cond
  6059. ((or (eq org-indirect-buffer-display 'new-frame)
  6060. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6061. (select-frame (make-frame))
  6062. (delete-other-windows)
  6063. (switch-to-buffer ibuf)
  6064. (org-set-frame-title heading))
  6065. ((eq org-indirect-buffer-display 'dedicated-frame)
  6066. (raise-frame
  6067. (select-frame (or (and org-indirect-dedicated-frame
  6068. (frame-live-p org-indirect-dedicated-frame)
  6069. org-indirect-dedicated-frame)
  6070. (setq org-indirect-dedicated-frame (make-frame)))))
  6071. (delete-other-windows)
  6072. (switch-to-buffer ibuf)
  6073. (org-set-frame-title (concat "Indirect: " heading)))
  6074. ((eq org-indirect-buffer-display 'current-window)
  6075. (switch-to-buffer ibuf))
  6076. ((eq org-indirect-buffer-display 'other-window)
  6077. (pop-to-buffer ibuf))
  6078. (t (error "Invalid value")))
  6079. (if (featurep 'xemacs)
  6080. (save-excursion (org-mode) (turn-on-font-lock)))
  6081. (narrow-to-region beg end)
  6082. (show-all)
  6083. (goto-char pos)
  6084. (and (window-live-p cwin) (select-window cwin))))
  6085. (defun org-get-indirect-buffer (&optional buffer)
  6086. (setq buffer (or buffer (current-buffer)))
  6087. (let ((n 1) (base (buffer-name buffer)) bname)
  6088. (while (buffer-live-p
  6089. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6090. (setq n (1+ n)))
  6091. (condition-case nil
  6092. (make-indirect-buffer buffer bname 'clone)
  6093. (error (make-indirect-buffer buffer bname)))))
  6094. (defun org-set-frame-title (title)
  6095. "Set the title of the current frame to the string TITLE."
  6096. ;; FIXME: how to name a single frame in XEmacs???
  6097. (unless (featurep 'xemacs)
  6098. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6099. ;;;; Structure editing
  6100. ;;; Inserting headlines
  6101. (defun org-previous-line-empty-p ()
  6102. (save-excursion
  6103. (and (not (bobp))
  6104. (or (beginning-of-line 0) t)
  6105. (save-match-data
  6106. (looking-at "[ \t]*$")))))
  6107. (defun org-insert-heading (&optional force-heading invisible-ok)
  6108. "Insert a new heading or item with same depth at point.
  6109. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6110. If point is at the beginning of a headline, insert a sibling before the
  6111. current headline. If point is not at the beginning, split the line,
  6112. create the new headline with the text in the current line after point
  6113. \(but see also the variable `org-M-RET-may-split-line').
  6114. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6115. This is important for non-interactive uses of the command."
  6116. (interactive "P")
  6117. (if (or (= (buffer-size) 0)
  6118. (and (not (save-excursion
  6119. (and (ignore-errors (org-back-to-heading invisible-ok))
  6120. (org-on-heading-p))))
  6121. (not (org-in-item-p))))
  6122. (progn
  6123. (insert "\n* ")
  6124. (run-hooks 'org-insert-heading-hook))
  6125. (when (or force-heading (not (org-insert-item)))
  6126. (let* ((empty-line-p nil)
  6127. (level nil)
  6128. (on-heading (org-on-heading-p))
  6129. (head (save-excursion
  6130. (condition-case nil
  6131. (progn
  6132. (org-back-to-heading invisible-ok)
  6133. (when (and (not on-heading)
  6134. (featurep 'org-inlinetask)
  6135. (integerp org-inlinetask-min-level)
  6136. (>= (length (match-string 0))
  6137. org-inlinetask-min-level))
  6138. ;; Find a heading level before the inline task
  6139. (while (and (setq level (org-up-heading-safe))
  6140. (>= level org-inlinetask-min-level)))
  6141. (if (org-on-heading-p)
  6142. (org-back-to-heading invisible-ok)
  6143. (error "This should not happen")))
  6144. (setq empty-line-p (org-previous-line-empty-p))
  6145. (match-string 0))
  6146. (error "*"))))
  6147. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6148. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6149. pos hide-previous previous-pos)
  6150. (cond
  6151. ((and (org-on-heading-p) (bolp)
  6152. (or (bobp)
  6153. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6154. ;; insert before the current line
  6155. (open-line (if blank 2 1)))
  6156. ((and (bolp)
  6157. (not org-insert-heading-respect-content)
  6158. (or (bobp)
  6159. (save-excursion
  6160. (backward-char 1) (not (outline-invisible-p)))))
  6161. ;; insert right here
  6162. nil)
  6163. (t
  6164. ;; somewhere in the line
  6165. (save-excursion
  6166. (setq previous-pos (point-at-bol))
  6167. (end-of-line)
  6168. (setq hide-previous (outline-invisible-p)))
  6169. (and org-insert-heading-respect-content (org-show-subtree))
  6170. (let ((split
  6171. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6172. (save-excursion
  6173. (let ((p (point)))
  6174. (goto-char (point-at-bol))
  6175. (and (looking-at org-complex-heading-regexp)
  6176. (> p (match-beginning 4)))))))
  6177. tags pos)
  6178. (cond
  6179. (org-insert-heading-respect-content
  6180. (org-end-of-subtree nil t)
  6181. (when (featurep 'org-inlinetask)
  6182. (while (and (not (eobp))
  6183. (looking-at "\\(\\*+\\)[ \t]+")
  6184. (>= (length (match-string 1))
  6185. org-inlinetask-min-level))
  6186. (org-end-of-subtree nil t)))
  6187. (or (bolp) (newline))
  6188. (or (org-previous-line-empty-p)
  6189. (and blank (newline)))
  6190. (open-line 1))
  6191. ((org-on-heading-p)
  6192. (when hide-previous
  6193. (show-children)
  6194. (org-show-entry))
  6195. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6196. (setq tags (and (match-end 2) (match-string 2)))
  6197. (and (match-end 1)
  6198. (delete-region (match-beginning 1) (match-end 1)))
  6199. (setq pos (point-at-bol))
  6200. (or split (end-of-line 1))
  6201. (delete-horizontal-space)
  6202. (if (string-match "\\`\\*+\\'"
  6203. (buffer-substring (point-at-bol) (point)))
  6204. (insert " "))
  6205. (newline (if blank 2 1))
  6206. (when tags
  6207. (save-excursion
  6208. (goto-char pos)
  6209. (end-of-line 1)
  6210. (insert " " tags)
  6211. (org-set-tags nil 'align))))
  6212. (t
  6213. (or split (end-of-line 1))
  6214. (newline (if blank 2 1)))))))
  6215. (insert head) (just-one-space)
  6216. (setq pos (point))
  6217. (end-of-line 1)
  6218. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6219. (when (and org-insert-heading-respect-content hide-previous)
  6220. (save-excursion
  6221. (goto-char previous-pos)
  6222. (hide-subtree)))
  6223. (run-hooks 'org-insert-heading-hook)))))
  6224. (defun org-get-heading (&optional no-tags)
  6225. "Return the heading of the current entry, without the stars."
  6226. (save-excursion
  6227. (org-back-to-heading t)
  6228. (if (looking-at
  6229. (if no-tags
  6230. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@#%]+:[ \t]*\\)?$")
  6231. "\\*+[ \t]+\\([^\r\n]*\\)"))
  6232. (match-string 1) "")))
  6233. (defun org-heading-components ()
  6234. "Return the components of the current heading.
  6235. This is a list with the following elements:
  6236. - the level as an integer
  6237. - the reduced level, different if `org-odd-levels-only' is set.
  6238. - the TODO keyword, or nil
  6239. - the priority character, like ?A, or nil if no priority is given
  6240. - the headline text itself, or the tags string if no headline text
  6241. - the tags string, or nil."
  6242. (save-excursion
  6243. (org-back-to-heading t)
  6244. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6245. (list (length (match-string 1))
  6246. (org-reduced-level (length (match-string 1)))
  6247. (org-match-string-no-properties 2)
  6248. (and (match-end 3) (aref (match-string 3) 2))
  6249. (org-match-string-no-properties 4)
  6250. (org-match-string-no-properties 5)))))
  6251. (defun org-get-entry ()
  6252. "Get the entry text, after heading, entire subtree."
  6253. (save-excursion
  6254. (org-back-to-heading t)
  6255. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6256. (defun org-insert-heading-after-current ()
  6257. "Insert a new heading with same level as current, after current subtree."
  6258. (interactive)
  6259. (org-back-to-heading)
  6260. (org-insert-heading)
  6261. (org-move-subtree-down)
  6262. (end-of-line 1))
  6263. (defun org-insert-heading-respect-content ()
  6264. (interactive)
  6265. (let ((org-insert-heading-respect-content t))
  6266. (org-insert-heading t)))
  6267. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6268. (interactive "P")
  6269. (let ((org-insert-heading-respect-content t))
  6270. (org-insert-todo-heading force-state t)))
  6271. (defun org-insert-todo-heading (arg &optional force-heading)
  6272. "Insert a new heading with the same level and TODO state as current heading.
  6273. If the heading has no TODO state, or if the state is DONE, use the first
  6274. state (TODO by default). Also with prefix arg, force first state."
  6275. (interactive "P")
  6276. (when (or force-heading (not (org-insert-item 'checkbox)))
  6277. (org-insert-heading force-heading)
  6278. (save-excursion
  6279. (org-back-to-heading)
  6280. (outline-previous-heading)
  6281. (looking-at org-todo-line-regexp))
  6282. (let*
  6283. ((new-mark-x
  6284. (if (or arg
  6285. (not (match-beginning 2))
  6286. (member (match-string 2) org-done-keywords))
  6287. (car org-todo-keywords-1)
  6288. (match-string 2)))
  6289. (new-mark
  6290. (or
  6291. (run-hook-with-args-until-success
  6292. 'org-todo-get-default-hook new-mark-x nil)
  6293. new-mark-x)))
  6294. (beginning-of-line 1)
  6295. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  6296. (if org-treat-insert-todo-heading-as-state-change
  6297. (org-todo new-mark)
  6298. (insert new-mark " "))))
  6299. (when org-provide-todo-statistics
  6300. (org-update-parent-todo-statistics))))
  6301. (defun org-insert-subheading (arg)
  6302. "Insert a new subheading and demote it.
  6303. Works for outline headings and for plain lists alike."
  6304. (interactive "P")
  6305. (org-insert-heading arg)
  6306. (cond
  6307. ((org-on-heading-p) (org-do-demote))
  6308. ((org-at-item-p) (org-indent-item))))
  6309. (defun org-insert-todo-subheading (arg)
  6310. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6311. Works for outline headings and for plain lists alike."
  6312. (interactive "P")
  6313. (org-insert-todo-heading arg)
  6314. (cond
  6315. ((org-on-heading-p) (org-do-demote))
  6316. ((org-at-item-p) (org-indent-item))))
  6317. ;;; Promotion and Demotion
  6318. (defvar org-after-demote-entry-hook nil
  6319. "Hook run after an entry has been demoted.
  6320. The cursor will be at the beginning of the entry.
  6321. When a subtree is being demoted, the hook will be called for each node.")
  6322. (defvar org-after-promote-entry-hook nil
  6323. "Hook run after an entry has been promoted.
  6324. The cursor will be at the beginning of the entry.
  6325. When a subtree is being promoted, the hook will be called for each node.")
  6326. (defun org-promote-subtree ()
  6327. "Promote the entire subtree.
  6328. See also `org-promote'."
  6329. (interactive)
  6330. (save-excursion
  6331. (org-with-limited-levels (org-map-tree 'org-promote)))
  6332. (org-fix-position-after-promote))
  6333. (defun org-demote-subtree ()
  6334. "Demote the entire subtree. See `org-demote'.
  6335. See also `org-promote'."
  6336. (interactive)
  6337. (save-excursion
  6338. (org-with-limited-levels (org-map-tree 'org-demote)))
  6339. (org-fix-position-after-promote))
  6340. (defun org-do-promote ()
  6341. "Promote the current heading higher up the tree.
  6342. If the region is active in `transient-mark-mode', promote all headings
  6343. in the region."
  6344. (interactive)
  6345. (save-excursion
  6346. (if (org-region-active-p)
  6347. (org-map-region 'org-promote (region-beginning) (region-end))
  6348. (org-promote)))
  6349. (org-fix-position-after-promote))
  6350. (defun org-do-demote ()
  6351. "Demote the current heading lower down the tree.
  6352. If the region is active in `transient-mark-mode', demote all headings
  6353. in the region."
  6354. (interactive)
  6355. (save-excursion
  6356. (if (org-region-active-p)
  6357. (org-map-region 'org-demote (region-beginning) (region-end))
  6358. (org-demote)))
  6359. (org-fix-position-after-promote))
  6360. (defun org-fix-position-after-promote ()
  6361. "Make sure that after pro/demotion cursor position is right."
  6362. (let ((pos (point)))
  6363. (when (save-excursion
  6364. (beginning-of-line 1)
  6365. (looking-at org-todo-line-regexp)
  6366. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6367. (cond ((eobp) (insert " "))
  6368. ((eolp) (insert " "))
  6369. ((equal (char-after) ?\ ) (forward-char 1))))))
  6370. (defun org-current-level ()
  6371. "Return the level of the current entry, or nil if before the first headline.
  6372. The level is the number of stars at the beginning of the headline."
  6373. (save-excursion
  6374. (org-with-limited-levels
  6375. (ignore-errors
  6376. (org-back-to-heading t)
  6377. (funcall outline-level)))))
  6378. (defun org-get-previous-line-level ()
  6379. "Return the outline depth of the last headline before the current line.
  6380. Returns 0 for the first headline in the buffer, and nil if before the
  6381. first headline."
  6382. (let ((current-level (org-current-level))
  6383. (prev-level (when (> (line-number-at-pos) 1)
  6384. (save-excursion
  6385. (beginning-of-line 0)
  6386. (org-current-level)))))
  6387. (cond ((null current-level) nil) ; Before first headline
  6388. ((null prev-level) 0) ; At first headline
  6389. (prev-level))))
  6390. (defun org-reduced-level (l)
  6391. "Compute the effective level of a heading.
  6392. This takes into account the setting of `org-odd-levels-only'."
  6393. (cond
  6394. ((zerop l) 0)
  6395. (org-odd-levels-only (1+ (floor (/ l 2))))
  6396. (t l)))
  6397. (defun org-level-increment ()
  6398. "Return the number of stars that will be added or removed at a
  6399. time to headlines when structure editing, based on the value of
  6400. `org-odd-levels-only'."
  6401. (if org-odd-levels-only 2 1))
  6402. (defun org-get-valid-level (level &optional change)
  6403. "Rectify a level change under the influence of `org-odd-levels-only'
  6404. LEVEL is a current level, CHANGE is by how much the level should be
  6405. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6406. even level numbers will become the next higher odd number."
  6407. (if org-odd-levels-only
  6408. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6409. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6410. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6411. (max 1 (+ level (or change 0)))))
  6412. (if (boundp 'define-obsolete-function-alias)
  6413. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6414. (define-obsolete-function-alias 'org-get-legal-level
  6415. 'org-get-valid-level)
  6416. (define-obsolete-function-alias 'org-get-legal-level
  6417. 'org-get-valid-level "23.1")))
  6418. (defun org-promote ()
  6419. "Promote the current heading higher up the tree.
  6420. If the region is active in `transient-mark-mode', promote all headings
  6421. in the region."
  6422. (org-back-to-heading t)
  6423. (let* ((level (save-match-data (funcall outline-level)))
  6424. (after-change-functions (remove 'flyspell-after-change-function
  6425. after-change-functions))
  6426. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6427. (diff (abs (- level (length up-head) -1))))
  6428. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6429. (replace-match up-head nil t)
  6430. ;; Fixup tag positioning
  6431. (and org-auto-align-tags (org-set-tags nil t))
  6432. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6433. (run-hooks 'org-after-promote-entry-hook)))
  6434. (defun org-demote ()
  6435. "Demote the current heading lower down the tree.
  6436. If the region is active in `transient-mark-mode', demote all headings
  6437. in the region."
  6438. (org-back-to-heading t)
  6439. (let* ((level (save-match-data (funcall outline-level)))
  6440. (after-change-functions (remove 'flyspell-after-change-function
  6441. after-change-functions))
  6442. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6443. (diff (abs (- level (length down-head) -1))))
  6444. (replace-match down-head nil t)
  6445. ;; Fixup tag positioning
  6446. (and org-auto-align-tags (org-set-tags nil t))
  6447. (if org-adapt-indentation (org-fixup-indentation diff))
  6448. (run-hooks 'org-after-demote-entry-hook)))
  6449. (defun org-cycle-level ()
  6450. "Cycle the level of an empty headline through possible states.
  6451. This goes first to child, then to parent, level, then up the hierarchy.
  6452. After top level, it switches back to sibling level."
  6453. (interactive)
  6454. (let ((org-adapt-indentation nil))
  6455. (when (org-point-at-end-of-empty-headline)
  6456. (setq this-command 'org-cycle-level) ; Only needed for caching
  6457. (let ((cur-level (org-current-level))
  6458. (prev-level (org-get-previous-line-level)))
  6459. (cond
  6460. ;; If first headline in file, promote to top-level.
  6461. ((= prev-level 0)
  6462. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6463. do (org-do-promote)))
  6464. ;; If same level as prev, demote one.
  6465. ((= prev-level cur-level)
  6466. (org-do-demote))
  6467. ;; If parent is top-level, promote to top level if not already.
  6468. ((= prev-level 1)
  6469. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6470. do (org-do-promote)))
  6471. ;; If top-level, return to prev-level.
  6472. ((= cur-level 1)
  6473. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6474. do (org-do-demote)))
  6475. ;; If less than prev-level, promote one.
  6476. ((< cur-level prev-level)
  6477. (org-do-promote))
  6478. ;; If deeper than prev-level, promote until higher than
  6479. ;; prev-level.
  6480. ((> cur-level prev-level)
  6481. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6482. do (org-do-promote))))
  6483. t))))
  6484. (defun org-map-tree (fun)
  6485. "Call FUN for every heading underneath the current one."
  6486. (org-back-to-heading)
  6487. (let ((level (funcall outline-level)))
  6488. (save-excursion
  6489. (funcall fun)
  6490. (while (and (progn
  6491. (outline-next-heading)
  6492. (> (funcall outline-level) level))
  6493. (not (eobp)))
  6494. (funcall fun)))))
  6495. (defun org-map-region (fun beg end)
  6496. "Call FUN for every heading between BEG and END."
  6497. (let ((org-ignore-region t))
  6498. (save-excursion
  6499. (setq end (copy-marker end))
  6500. (goto-char beg)
  6501. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  6502. (< (point) end))
  6503. (funcall fun))
  6504. (while (and (progn
  6505. (outline-next-heading)
  6506. (< (point) end))
  6507. (not (eobp)))
  6508. (funcall fun)))))
  6509. (defun org-fixup-indentation (diff)
  6510. "Change the indentation in the current entry by DIFF.
  6511. However, if any line in the current entry has no indentation, or if it
  6512. would end up with no indentation after the change, nothing at all is done."
  6513. (save-excursion
  6514. (let ((end (save-excursion (outline-next-heading)
  6515. (point-marker)))
  6516. (prohibit (if (> diff 0)
  6517. "^\\S-"
  6518. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6519. col)
  6520. (unless (save-excursion (end-of-line 1)
  6521. (re-search-forward prohibit end t))
  6522. (while (and (< (point) end)
  6523. (re-search-forward "^[ \t]+" end t))
  6524. (goto-char (match-end 0))
  6525. (setq col (current-column))
  6526. (if (< diff 0) (replace-match ""))
  6527. (org-indent-to-column (+ diff col))))
  6528. (move-marker end nil))))
  6529. (defun org-convert-to-odd-levels ()
  6530. "Convert an org-mode file with all levels allowed to one with odd levels.
  6531. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6532. level 5 etc."
  6533. (interactive)
  6534. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6535. (let ((outline-regexp org-outline-regexp)
  6536. (outline-level 'org-outline-level)
  6537. (org-odd-levels-only nil) n)
  6538. (save-excursion
  6539. (goto-char (point-min))
  6540. (while (re-search-forward "^\\*\\*+ " nil t)
  6541. (setq n (- (length (match-string 0)) 2))
  6542. (while (>= (setq n (1- n)) 0)
  6543. (org-demote))
  6544. (end-of-line 1))))))
  6545. (defun org-convert-to-oddeven-levels ()
  6546. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6547. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6548. file contains a section with an even level, conversion would
  6549. destroy the structure of the file. An error is signaled in this
  6550. case."
  6551. (interactive)
  6552. (goto-char (point-min))
  6553. ;; First check if there are no even levels
  6554. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6555. (org-show-context t)
  6556. (error "Not all levels are odd in this file. Conversion not possible"))
  6557. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6558. (let ((outline-regexp org-outline-regexp)
  6559. (outline-level 'org-outline-level)
  6560. (org-odd-levels-only nil) n)
  6561. (save-excursion
  6562. (goto-char (point-min))
  6563. (while (re-search-forward "^\\*\\*+ " nil t)
  6564. (setq n (/ (1- (length (match-string 0))) 2))
  6565. (while (>= (setq n (1- n)) 0)
  6566. (org-promote))
  6567. (end-of-line 1))))))
  6568. (defun org-tr-level (n)
  6569. "Make N odd if required."
  6570. (if org-odd-levels-only (1+ (/ n 2)) n))
  6571. ;;; Vertical tree motion, cutting and pasting of subtrees
  6572. (defun org-move-subtree-up (&optional arg)
  6573. "Move the current subtree up past ARG headlines of the same level."
  6574. (interactive "p")
  6575. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6576. (defun org-move-subtree-down (&optional arg)
  6577. "Move the current subtree down past ARG headlines of the same level."
  6578. (interactive "p")
  6579. (setq arg (prefix-numeric-value arg))
  6580. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6581. 'org-get-last-sibling))
  6582. (ins-point (make-marker))
  6583. (cnt (abs arg))
  6584. (col (current-column))
  6585. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6586. ;; Select the tree
  6587. (org-back-to-heading)
  6588. (setq beg0 (point))
  6589. (save-excursion
  6590. (setq ne-beg (org-back-over-empty-lines))
  6591. (setq beg (point)))
  6592. (save-match-data
  6593. (save-excursion (outline-end-of-heading)
  6594. (setq folded (outline-invisible-p)))
  6595. (outline-end-of-subtree))
  6596. (outline-next-heading)
  6597. (setq ne-end (org-back-over-empty-lines))
  6598. (setq end (point))
  6599. (goto-char beg0)
  6600. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6601. ;; include less whitespace
  6602. (save-excursion
  6603. (goto-char beg)
  6604. (forward-line (- ne-beg ne-end))
  6605. (setq beg (point))))
  6606. ;; Find insertion point, with error handling
  6607. (while (> cnt 0)
  6608. (or (and (funcall movfunc) (looking-at outline-regexp))
  6609. (progn (goto-char beg0)
  6610. (error "Cannot move past superior level or buffer limit")))
  6611. (setq cnt (1- cnt)))
  6612. (if (> arg 0)
  6613. ;; Moving forward - still need to move over subtree
  6614. (progn (org-end-of-subtree t t)
  6615. (save-excursion
  6616. (org-back-over-empty-lines)
  6617. (or (bolp) (newline)))))
  6618. (setq ne-ins (org-back-over-empty-lines))
  6619. (move-marker ins-point (point))
  6620. (setq txt (buffer-substring beg end))
  6621. (org-save-markers-in-region beg end)
  6622. (delete-region beg end)
  6623. (org-remove-empty-overlays-at beg)
  6624. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6625. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6626. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6627. (let ((bbb (point)))
  6628. (insert-before-markers txt)
  6629. (org-reinstall-markers-in-region bbb)
  6630. (move-marker ins-point bbb))
  6631. (or (bolp) (insert "\n"))
  6632. (setq ins-end (point))
  6633. (goto-char ins-point)
  6634. (org-skip-whitespace)
  6635. (when (and (< arg 0)
  6636. (org-first-sibling-p)
  6637. (> ne-ins ne-beg))
  6638. ;; Move whitespace back to beginning
  6639. (save-excursion
  6640. (goto-char ins-end)
  6641. (let ((kill-whole-line t))
  6642. (kill-line (- ne-ins ne-beg)) (point)))
  6643. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6644. (move-marker ins-point nil)
  6645. (if folded
  6646. (hide-subtree)
  6647. (org-show-entry)
  6648. (show-children)
  6649. (org-cycle-hide-drawers 'children))
  6650. (org-clean-visibility-after-subtree-move)
  6651. ;; move back to the initial column we were at
  6652. (move-to-column col)))
  6653. (defvar org-subtree-clip ""
  6654. "Clipboard for cut and paste of subtrees.
  6655. This is actually only a copy of the kill, because we use the normal kill
  6656. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6657. (defvar org-subtree-clip-folded nil
  6658. "Was the last copied subtree folded?
  6659. This is used to fold the tree back after pasting.")
  6660. (defun org-cut-subtree (&optional n)
  6661. "Cut the current subtree into the clipboard.
  6662. With prefix arg N, cut this many sequential subtrees.
  6663. This is a short-hand for marking the subtree and then cutting it."
  6664. (interactive "p")
  6665. (org-copy-subtree n 'cut))
  6666. (defun org-copy-subtree (&optional n cut force-store-markers)
  6667. "Cut the current subtree into the clipboard.
  6668. With prefix arg N, cut this many sequential subtrees.
  6669. This is a short-hand for marking the subtree and then copying it.
  6670. If CUT is non-nil, actually cut the subtree.
  6671. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6672. of some markers in the region, even if CUT is non-nil. This is
  6673. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6674. (interactive "p")
  6675. (let (beg end folded (beg0 (point)))
  6676. (if (org-called-interactively-p 'any)
  6677. (org-back-to-heading nil) ; take what looks like a subtree
  6678. (org-back-to-heading t)) ; take what is really there
  6679. (org-back-over-empty-lines)
  6680. (setq beg (point))
  6681. (skip-chars-forward " \t\r\n")
  6682. (save-match-data
  6683. (save-excursion (outline-end-of-heading)
  6684. (setq folded (outline-invisible-p)))
  6685. (condition-case nil
  6686. (org-forward-same-level (1- n) t)
  6687. (error nil))
  6688. (org-end-of-subtree t t))
  6689. (org-back-over-empty-lines)
  6690. (setq end (point))
  6691. (goto-char beg0)
  6692. (when (> end beg)
  6693. (setq org-subtree-clip-folded folded)
  6694. (when (or cut force-store-markers)
  6695. (org-save-markers-in-region beg end))
  6696. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6697. (setq org-subtree-clip (current-kill 0))
  6698. (message "%s: Subtree(s) with %d characters"
  6699. (if cut "Cut" "Copied")
  6700. (length org-subtree-clip)))))
  6701. (defun org-paste-subtree (&optional level tree for-yank)
  6702. "Paste the clipboard as a subtree, with modification of headline level.
  6703. The entire subtree is promoted or demoted in order to match a new headline
  6704. level.
  6705. If the cursor is at the beginning of a headline, the same level as
  6706. that headline is used to paste the tree
  6707. If not, the new level is derived from the *visible* headings
  6708. before and after the insertion point, and taken to be the inferior headline
  6709. level of the two. So if the previous visible heading is level 3 and the
  6710. next is level 4 (or vice versa), level 4 will be used for insertion.
  6711. This makes sure that the subtree remains an independent subtree and does
  6712. not swallow low level entries.
  6713. You can also force a different level, either by using a numeric prefix
  6714. argument, or by inserting the heading marker by hand. For example, if the
  6715. cursor is after \"*****\", then the tree will be shifted to level 5.
  6716. If optional TREE is given, use this text instead of the kill ring.
  6717. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6718. move back over whitespace before inserting, and move point to the end of
  6719. the inserted text when done."
  6720. (interactive "P")
  6721. (setq tree (or tree (and kill-ring (current-kill 0))))
  6722. (unless (org-kill-is-subtree-p tree)
  6723. (error "%s"
  6724. (substitute-command-keys
  6725. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6726. (let* ((visp (not (outline-invisible-p)))
  6727. (txt tree)
  6728. (^re (concat "^\\(" outline-regexp "\\)"))
  6729. (re (concat "\\(" outline-regexp "\\)"))
  6730. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6731. (old-level (if (string-match ^re txt)
  6732. (- (match-end 0) (match-beginning 0) 1)
  6733. -1))
  6734. (force-level (cond (level (prefix-numeric-value level))
  6735. ((and (looking-at "[ \t]*$")
  6736. (string-match
  6737. ^re_ (buffer-substring
  6738. (point-at-bol) (point))))
  6739. (- (match-end 1) (match-beginning 1)))
  6740. ((and (bolp)
  6741. (looking-at org-outline-regexp))
  6742. (- (match-end 0) (point) 1))
  6743. (t nil)))
  6744. (previous-level (save-excursion
  6745. (condition-case nil
  6746. (progn
  6747. (outline-previous-visible-heading 1)
  6748. (if (looking-at re)
  6749. (- (match-end 0) (match-beginning 0) 1)
  6750. 1))
  6751. (error 1))))
  6752. (next-level (save-excursion
  6753. (condition-case nil
  6754. (progn
  6755. (or (looking-at outline-regexp)
  6756. (outline-next-visible-heading 1))
  6757. (if (looking-at re)
  6758. (- (match-end 0) (match-beginning 0) 1)
  6759. 1))
  6760. (error 1))))
  6761. (new-level (or force-level (max previous-level next-level)))
  6762. (shift (if (or (= old-level -1)
  6763. (= new-level -1)
  6764. (= old-level new-level))
  6765. 0
  6766. (- new-level old-level)))
  6767. (delta (if (> shift 0) -1 1))
  6768. (func (if (> shift 0) 'org-demote 'org-promote))
  6769. (org-odd-levels-only nil)
  6770. beg end newend)
  6771. ;; Remove the forced level indicator
  6772. (if force-level
  6773. (delete-region (point-at-bol) (point)))
  6774. ;; Paste
  6775. (beginning-of-line 1)
  6776. (unless for-yank (org-back-over-empty-lines))
  6777. (setq beg (point))
  6778. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6779. (insert-before-markers txt)
  6780. (unless (string-match "\n\\'" txt) (insert "\n"))
  6781. (setq newend (point))
  6782. (org-reinstall-markers-in-region beg)
  6783. (setq end (point))
  6784. (goto-char beg)
  6785. (skip-chars-forward " \t\n\r")
  6786. (setq beg (point))
  6787. (if (and (outline-invisible-p) visp)
  6788. (save-excursion (outline-show-heading)))
  6789. ;; Shift if necessary
  6790. (unless (= shift 0)
  6791. (save-restriction
  6792. (narrow-to-region beg end)
  6793. (while (not (= shift 0))
  6794. (org-map-region func (point-min) (point-max))
  6795. (setq shift (+ delta shift)))
  6796. (goto-char (point-min))
  6797. (setq newend (point-max))))
  6798. (when (or (org-called-interactively-p 'interactive) for-yank)
  6799. (message "Clipboard pasted as level %d subtree" new-level))
  6800. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6801. kill-ring
  6802. (eq org-subtree-clip (current-kill 0))
  6803. org-subtree-clip-folded)
  6804. ;; The tree was folded before it was killed/copied
  6805. (hide-subtree))
  6806. (and for-yank (goto-char newend))))
  6807. (defun org-kill-is-subtree-p (&optional txt)
  6808. "Check if the current kill is an outline subtree, or a set of trees.
  6809. Returns nil if kill does not start with a headline, or if the first
  6810. headline level is not the largest headline level in the tree.
  6811. So this will actually accept several entries of equal levels as well,
  6812. which is OK for `org-paste-subtree'.
  6813. If optional TXT is given, check this string instead of the current kill."
  6814. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6815. (start-level (and kill
  6816. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6817. org-outline-regexp "\\)")
  6818. kill)
  6819. (- (match-end 2) (match-beginning 2) 1)))
  6820. (re (concat "^" org-outline-regexp))
  6821. (start (1+ (or (match-beginning 2) -1))))
  6822. (if (not start-level)
  6823. (progn
  6824. nil) ;; does not even start with a heading
  6825. (catch 'exit
  6826. (while (setq start (string-match re kill (1+ start)))
  6827. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6828. (throw 'exit nil)))
  6829. t))))
  6830. (defvar org-markers-to-move nil
  6831. "Markers that should be moved with a cut-and-paste operation.
  6832. Those markers are stored together with their positions relative to
  6833. the start of the region.")
  6834. (defun org-save-markers-in-region (beg end)
  6835. "Check markers in region.
  6836. If these markers are between BEG and END, record their position relative
  6837. to BEG, so that after moving the block of text, we can put the markers back
  6838. into place.
  6839. This function gets called just before an entry or tree gets cut from the
  6840. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6841. called immediately, to move the markers with the entries."
  6842. (setq org-markers-to-move nil)
  6843. (when (featurep 'org-clock)
  6844. (org-clock-save-markers-for-cut-and-paste beg end))
  6845. (when (featurep 'org-agenda)
  6846. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6847. (defun org-check-and-save-marker (marker beg end)
  6848. "Check if MARKER is between BEG and END.
  6849. If yes, remember the marker and the distance to BEG."
  6850. (when (and (marker-buffer marker)
  6851. (equal (marker-buffer marker) (current-buffer)))
  6852. (if (and (>= marker beg) (< marker end))
  6853. (push (cons marker (- marker beg)) org-markers-to-move))))
  6854. (defun org-reinstall-markers-in-region (beg)
  6855. "Move all remembered markers to their position relative to BEG."
  6856. (mapc (lambda (x)
  6857. (move-marker (car x) (+ beg (cdr x))))
  6858. org-markers-to-move)
  6859. (setq org-markers-to-move nil))
  6860. (defun org-narrow-to-subtree ()
  6861. "Narrow buffer to the current subtree."
  6862. (interactive)
  6863. (save-excursion
  6864. (save-match-data
  6865. (org-with-limited-levels
  6866. (narrow-to-region
  6867. (progn (org-back-to-heading t) (point))
  6868. (progn (org-end-of-subtree t t)
  6869. (if (and (org-on-heading-p) (not (eobp))) (backward-char 1))
  6870. (point)))))))
  6871. (defun org-narrow-to-block ()
  6872. "Narrow buffer to the current block."
  6873. (interactive)
  6874. (let ((bstart "^[ \t]*#\\+begin")
  6875. (bend "[ \t]*#\\+end")
  6876. (case-fold-search t) ;; allow #+BEGIN
  6877. b_start b_end)
  6878. (if (org-in-regexps-block-p bstart bend)
  6879. (progn
  6880. (save-excursion (re-search-backward bstart nil t)
  6881. (setq b_start (match-beginning 0)))
  6882. (save-excursion (re-search-forward bend nil t)
  6883. (setq b_end (match-end 0)))
  6884. (narrow-to-region b_start b_end))
  6885. (error "Not in a block"))))
  6886. (eval-when-compile
  6887. (defvar org-property-drawer-re))
  6888. (defvar org-property-start-re) ;; defined below
  6889. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6890. "Clone the task (subtree) at point N times.
  6891. The clones will be inserted as siblings.
  6892. In interactive use, the user will be prompted for the number of
  6893. clones to be produced, and for a time SHIFT, which may be a
  6894. repeater as used in time stamps, for example `+3d'.
  6895. When a valid repeater is given and the entry contains any time
  6896. stamps, the clones will become a sequence in time, with time
  6897. stamps in the subtree shifted for each clone produced. If SHIFT
  6898. is nil or the empty string, time stamps will be left alone. The
  6899. ID property of the original subtree is removed.
  6900. If the original subtree did contain time stamps with a repeater,
  6901. the following will happen:
  6902. - the repeater will be removed in each clone
  6903. - an additional clone will be produced, with the current, unshifted
  6904. date(s) in the entry.
  6905. - the original entry will be placed *after* all the clones, with
  6906. repeater intact.
  6907. - the start days in the repeater in the original entry will be shifted
  6908. to past the last clone.
  6909. I this way you can spell out a number of instances of a repeating task,
  6910. and still retain the repeater to cover future instances of the task."
  6911. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6912. (let (beg end template task idprop
  6913. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6914. (if (not (and (integerp n) (> n 0)))
  6915. (error "Invalid number of replications %s" n))
  6916. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6917. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6918. shift)))
  6919. (error "Invalid shift specification %s" shift))
  6920. (when doshift
  6921. (setq shift-n (string-to-number (match-string 1 shift))
  6922. shift-what (cdr (assoc (match-string 2 shift)
  6923. '(("d" . day) ("w" . week)
  6924. ("m" . month) ("y" . year))))))
  6925. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6926. (setq nmin 1 nmax n)
  6927. (org-back-to-heading t)
  6928. (setq beg (point))
  6929. (setq idprop (org-entry-get nil "ID"))
  6930. (org-end-of-subtree t t)
  6931. (or (bolp) (insert "\n"))
  6932. (setq end (point))
  6933. (setq template (buffer-substring beg end))
  6934. (when (and doshift
  6935. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6936. (delete-region beg end)
  6937. (setq end beg)
  6938. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6939. (goto-char end)
  6940. (loop for n from nmin to nmax do
  6941. ;; prepare clone
  6942. (with-temp-buffer
  6943. (insert template)
  6944. (org-mode)
  6945. (goto-char (point-min))
  6946. (and idprop (if org-clone-delete-id
  6947. (org-entry-delete nil "ID")
  6948. (org-id-get-create t)))
  6949. (while (re-search-forward org-property-start-re nil t)
  6950. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  6951. (goto-char (point-min))
  6952. (when doshift
  6953. (while (re-search-forward org-ts-regexp-both nil t)
  6954. (org-timestamp-change (* n shift-n) shift-what))
  6955. (unless (= n n-no-remove)
  6956. (goto-char (point-min))
  6957. (while (re-search-forward org-ts-regexp nil t)
  6958. (save-excursion
  6959. (goto-char (match-beginning 0))
  6960. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6961. (delete-region (match-beginning 1) (match-end 1)))))))
  6962. (setq task (buffer-string)))
  6963. (insert task))
  6964. (goto-char beg)))
  6965. ;;; Outline Sorting
  6966. (defun org-sort (with-case)
  6967. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  6968. Optional argument WITH-CASE means sort case-sensitively.
  6969. With a double prefix argument, also remove duplicate entries."
  6970. (interactive "P")
  6971. (cond
  6972. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  6973. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  6974. (t
  6975. (org-call-with-arg 'org-sort-entries with-case))))
  6976. (defun org-sort-remove-invisible (s)
  6977. (remove-text-properties 0 (length s) org-rm-props s)
  6978. (while (string-match org-bracket-link-regexp s)
  6979. (setq s (replace-match (if (match-end 2)
  6980. (match-string 3 s)
  6981. (match-string 1 s)) t t s)))
  6982. s)
  6983. (defvar org-priority-regexp) ; defined later in the file
  6984. (defvar org-after-sorting-entries-or-items-hook nil
  6985. "Hook that is run after a bunch of entries or items have been sorted.
  6986. When children are sorted, the cursor is in the parent line when this
  6987. hook gets called. When a region or a plain list is sorted, the cursor
  6988. will be in the first entry of the sorted region/list.")
  6989. (defun org-sort-entries
  6990. (&optional with-case sorting-type getkey-func compare-func property)
  6991. "Sort entries on a certain level of an outline tree.
  6992. If there is an active region, the entries in the region are sorted.
  6993. Else, if the cursor is before the first entry, sort the top-level items.
  6994. Else, the children of the entry at point are sorted.
  6995. Sorting can be alphabetically, numerically, by date/time as given by
  6996. a time stamp, by a property or by priority.
  6997. The command prompts for the sorting type unless it has been given to the
  6998. function through the SORTING-TYPE argument, which needs to be a character,
  6999. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7000. precise meaning of each character:
  7001. n Numerically, by converting the beginning of the entry/item to a number.
  7002. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7003. t By date/time, either the first active time stamp in the entry, or, if
  7004. none exist, by the first inactive one.
  7005. s By the scheduled date/time.
  7006. d By deadline date/time.
  7007. c By creation time, which is assumed to be the first inactive time stamp
  7008. at the beginning of a line.
  7009. p By priority according to the cookie.
  7010. r By the value of a property.
  7011. Capital letters will reverse the sort order.
  7012. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7013. called with point at the beginning of the record. It must return either
  7014. a string or a number that should serve as the sorting key for that record.
  7015. Comparing entries ignores case by default. However, with an optional argument
  7016. WITH-CASE, the sorting considers case as well."
  7017. (interactive "P")
  7018. (let ((case-func (if with-case 'identity 'downcase))
  7019. start beg end stars re re2
  7020. txt what tmp)
  7021. ;; Find beginning and end of region to sort
  7022. (cond
  7023. ((org-region-active-p)
  7024. ;; we will sort the region
  7025. (setq end (region-end)
  7026. what "region")
  7027. (goto-char (region-beginning))
  7028. (if (not (org-on-heading-p)) (outline-next-heading))
  7029. (setq start (point)))
  7030. ((or (org-on-heading-p)
  7031. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7032. ;; we will sort the children of the current headline
  7033. (org-back-to-heading)
  7034. (setq start (point)
  7035. end (progn (org-end-of-subtree t t)
  7036. (or (bolp) (insert "\n"))
  7037. (org-back-over-empty-lines)
  7038. (point))
  7039. what "children")
  7040. (goto-char start)
  7041. (show-subtree)
  7042. (outline-next-heading))
  7043. (t
  7044. ;; we will sort the top-level entries in this file
  7045. (goto-char (point-min))
  7046. (or (org-on-heading-p) (outline-next-heading))
  7047. (setq start (point))
  7048. (goto-char (point-max))
  7049. (beginning-of-line 1)
  7050. (when (looking-at ".*?\\S-")
  7051. ;; File ends in a non-white line
  7052. (end-of-line 1)
  7053. (insert "\n"))
  7054. (setq end (point-max))
  7055. (setq what "top-level")
  7056. (goto-char start)
  7057. (show-all)))
  7058. (setq beg (point))
  7059. (if (>= beg end) (error "Nothing to sort"))
  7060. (looking-at "\\(\\*+\\)")
  7061. (setq stars (match-string 1)
  7062. re (concat "^" (regexp-quote stars) " +")
  7063. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7064. txt (buffer-substring beg end))
  7065. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7066. (if (and (not (equal stars "*")) (string-match re2 txt))
  7067. (error "Region to sort contains a level above the first entry"))
  7068. (unless sorting-type
  7069. (message
  7070. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7071. [t]ime [s]cheduled [d]eadline [c]reated
  7072. A/N/T/S/D/C/P/O/F means reversed:"
  7073. what)
  7074. (setq sorting-type (read-char-exclusive))
  7075. (and (= (downcase sorting-type) ?f)
  7076. (setq getkey-func
  7077. (org-icompleting-read "Sort using function: "
  7078. obarray 'fboundp t nil nil))
  7079. (setq getkey-func (intern getkey-func)))
  7080. (and (= (downcase sorting-type) ?r)
  7081. (setq property
  7082. (org-icompleting-read "Property: "
  7083. (mapcar 'list (org-buffer-property-keys t))
  7084. nil t))))
  7085. (message "Sorting entries...")
  7086. (save-restriction
  7087. (narrow-to-region start end)
  7088. (let ((dcst (downcase sorting-type))
  7089. (case-fold-search nil)
  7090. (now (current-time)))
  7091. (sort-subr
  7092. (/= dcst sorting-type)
  7093. ;; This function moves to the beginning character of the "record" to
  7094. ;; be sorted.
  7095. (lambda nil
  7096. (if (re-search-forward re nil t)
  7097. (goto-char (match-beginning 0))
  7098. (goto-char (point-max))))
  7099. ;; This function moves to the last character of the "record" being
  7100. ;; sorted.
  7101. (lambda nil
  7102. (save-match-data
  7103. (condition-case nil
  7104. (outline-forward-same-level 1)
  7105. (error
  7106. (goto-char (point-max))))))
  7107. ;; This function returns the value that gets sorted against.
  7108. (lambda nil
  7109. (cond
  7110. ((= dcst ?n)
  7111. (if (looking-at org-complex-heading-regexp)
  7112. (string-to-number (match-string 4))
  7113. nil))
  7114. ((= dcst ?a)
  7115. (if (looking-at org-complex-heading-regexp)
  7116. (funcall case-func (match-string 4))
  7117. nil))
  7118. ((= dcst ?t)
  7119. (let ((end (save-excursion (outline-next-heading) (point))))
  7120. (if (or (re-search-forward org-ts-regexp end t)
  7121. (re-search-forward org-ts-regexp-both end t))
  7122. (org-time-string-to-seconds (match-string 0))
  7123. (org-float-time now))))
  7124. ((= dcst ?c)
  7125. (let ((end (save-excursion (outline-next-heading) (point))))
  7126. (if (re-search-forward
  7127. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7128. end t)
  7129. (org-time-string-to-seconds (match-string 0))
  7130. (org-float-time now))))
  7131. ((= dcst ?s)
  7132. (let ((end (save-excursion (outline-next-heading) (point))))
  7133. (if (re-search-forward org-scheduled-time-regexp end t)
  7134. (org-time-string-to-seconds (match-string 1))
  7135. (org-float-time now))))
  7136. ((= dcst ?d)
  7137. (let ((end (save-excursion (outline-next-heading) (point))))
  7138. (if (re-search-forward org-deadline-time-regexp end t)
  7139. (org-time-string-to-seconds (match-string 1))
  7140. (org-float-time now))))
  7141. ((= dcst ?p)
  7142. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7143. (string-to-char (match-string 2))
  7144. org-default-priority))
  7145. ((= dcst ?r)
  7146. (or (org-entry-get nil property) ""))
  7147. ((= dcst ?o)
  7148. (if (looking-at org-complex-heading-regexp)
  7149. (- 9999 (length (member (match-string 2)
  7150. org-todo-keywords-1)))))
  7151. ((= dcst ?f)
  7152. (if getkey-func
  7153. (progn
  7154. (setq tmp (funcall getkey-func))
  7155. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7156. tmp)
  7157. (error "Invalid key function `%s'" getkey-func)))
  7158. (t (error "Invalid sorting type `%c'" sorting-type))))
  7159. nil
  7160. (cond
  7161. ((= dcst ?a) 'string<)
  7162. ((= dcst ?f) compare-func)
  7163. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7164. (t nil)))))
  7165. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7166. (message "Sorting entries...done")))
  7167. (defun org-do-sort (table what &optional with-case sorting-type)
  7168. "Sort TABLE of WHAT according to SORTING-TYPE.
  7169. The user will be prompted for the SORTING-TYPE if the call to this
  7170. function does not specify it. WHAT is only for the prompt, to indicate
  7171. what is being sorted. The sorting key will be extracted from
  7172. the car of the elements of the table.
  7173. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7174. (unless sorting-type
  7175. (message
  7176. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7177. what)
  7178. (setq sorting-type (read-char-exclusive)))
  7179. (let ((dcst (downcase sorting-type))
  7180. extractfun comparefun)
  7181. ;; Define the appropriate functions
  7182. (cond
  7183. ((= dcst ?n)
  7184. (setq extractfun 'string-to-number
  7185. comparefun (if (= dcst sorting-type) '< '>)))
  7186. ((= dcst ?a)
  7187. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7188. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7189. comparefun (if (= dcst sorting-type)
  7190. 'string<
  7191. (lambda (a b) (and (not (string< a b))
  7192. (not (string= a b)))))))
  7193. ((= dcst ?t)
  7194. (setq extractfun
  7195. (lambda (x)
  7196. (if (or (string-match org-ts-regexp x)
  7197. (string-match org-ts-regexp-both x))
  7198. (org-float-time
  7199. (org-time-string-to-time (match-string 0 x)))
  7200. 0))
  7201. comparefun (if (= dcst sorting-type) '< '>)))
  7202. (t (error "Invalid sorting type `%c'" sorting-type)))
  7203. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7204. table)
  7205. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7206. ;;; The orgstruct minor mode
  7207. ;; Define a minor mode which can be used in other modes in order to
  7208. ;; integrate the org-mode structure editing commands.
  7209. ;; This is really a hack, because the org-mode structure commands use
  7210. ;; keys which normally belong to the major mode. Here is how it
  7211. ;; works: The minor mode defines all the keys necessary to operate the
  7212. ;; structure commands, but wraps the commands into a function which
  7213. ;; tests if the cursor is currently at a headline or a plain list
  7214. ;; item. If that is the case, the structure command is used,
  7215. ;; temporarily setting many Org-mode variables like regular
  7216. ;; expressions for filling etc. However, when any of those keys is
  7217. ;; used at a different location, function uses `key-binding' to look
  7218. ;; up if the key has an associated command in another currently active
  7219. ;; keymap (minor modes, major mode, global), and executes that
  7220. ;; command. There might be problems if any of the keys is otherwise
  7221. ;; used as a prefix key.
  7222. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7223. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7224. ;; addresses this by checking explicitly for both bindings.
  7225. (defvar orgstruct-mode-map (make-sparse-keymap)
  7226. "Keymap for the minor `orgstruct-mode'.")
  7227. (defvar org-local-vars nil
  7228. "List of local variables, for use by `orgstruct-mode'.")
  7229. ;;;###autoload
  7230. (define-minor-mode orgstruct-mode
  7231. "Toggle the minor mode `orgstruct-mode'.
  7232. This mode is for using Org-mode structure commands in other
  7233. modes. The following keys behave as if Org-mode were active, if
  7234. the cursor is on a headline, or on a plain list item (both as
  7235. defined by Org-mode).
  7236. M-up Move entry/item up
  7237. M-down Move entry/item down
  7238. M-left Promote
  7239. M-right Demote
  7240. M-S-up Move entry/item up
  7241. M-S-down Move entry/item down
  7242. M-S-left Promote subtree
  7243. M-S-right Demote subtree
  7244. M-q Fill paragraph and items like in Org-mode
  7245. C-c ^ Sort entries
  7246. C-c - Cycle list bullet
  7247. TAB Cycle item visibility
  7248. M-RET Insert new heading/item
  7249. S-M-RET Insert new TODO heading / Checkbox item
  7250. C-c C-c Set tags / toggle checkbox"
  7251. nil " OrgStruct" nil
  7252. (org-load-modules-maybe)
  7253. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7254. ;;;###autoload
  7255. (defun turn-on-orgstruct ()
  7256. "Unconditionally turn on `orgstruct-mode'."
  7257. (orgstruct-mode 1))
  7258. (defun orgstruct++-mode (&optional arg)
  7259. "Toggle `orgstruct-mode', the enhanced version of it.
  7260. In addition to setting orgstruct-mode, this also exports all indentation
  7261. and autofilling variables from org-mode into the buffer. It will also
  7262. recognize item context in multiline items.
  7263. Note that turning off orgstruct-mode will *not* remove the
  7264. indentation/paragraph settings. This can only be done by refreshing the
  7265. major mode, for example with \\[normal-mode]."
  7266. (interactive "P")
  7267. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7268. (if (< arg 1)
  7269. (orgstruct-mode -1)
  7270. (orgstruct-mode 1)
  7271. (let (var val)
  7272. (mapc
  7273. (lambda (x)
  7274. (when (string-match
  7275. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7276. (symbol-name (car x)))
  7277. (setq var (car x) val (nth 1 x))
  7278. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7279. org-local-vars)
  7280. (org-set-local 'orgstruct-is-++ t))))
  7281. (defvar orgstruct-is-++ nil
  7282. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7283. (make-variable-buffer-local 'orgstruct-is-++)
  7284. ;;;###autoload
  7285. (defun turn-on-orgstruct++ ()
  7286. "Unconditionally turn on `orgstruct++-mode'."
  7287. (orgstruct++-mode 1))
  7288. (defun orgstruct-error ()
  7289. "Error when there is no default binding for a structure key."
  7290. (interactive)
  7291. (error "This key has no function outside structure elements"))
  7292. (defun orgstruct-setup ()
  7293. "Setup orgstruct keymaps."
  7294. (let ((nfunc 0)
  7295. (bindings
  7296. (list
  7297. '([(meta up)] org-metaup)
  7298. '([(meta down)] org-metadown)
  7299. '([(meta left)] org-metaleft)
  7300. '([(meta right)] org-metaright)
  7301. '([(meta shift up)] org-shiftmetaup)
  7302. '([(meta shift down)] org-shiftmetadown)
  7303. '([(meta shift left)] org-shiftmetaleft)
  7304. '([(meta shift right)] org-shiftmetaright)
  7305. '([?\e (up)] org-metaup)
  7306. '([?\e (down)] org-metadown)
  7307. '([?\e (left)] org-metaleft)
  7308. '([?\e (right)] org-metaright)
  7309. '([?\e (shift up)] org-shiftmetaup)
  7310. '([?\e (shift down)] org-shiftmetadown)
  7311. '([?\e (shift left)] org-shiftmetaleft)
  7312. '([?\e (shift right)] org-shiftmetaright)
  7313. '([(shift up)] org-shiftup)
  7314. '([(shift down)] org-shiftdown)
  7315. '([(shift left)] org-shiftleft)
  7316. '([(shift right)] org-shiftright)
  7317. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7318. '("\M-q" fill-paragraph)
  7319. '("\C-c^" org-sort)
  7320. '("\C-c-" org-cycle-list-bullet)))
  7321. elt key fun cmd)
  7322. (while (setq elt (pop bindings))
  7323. (setq nfunc (1+ nfunc))
  7324. (setq key (org-key (car elt))
  7325. fun (nth 1 elt)
  7326. cmd (orgstruct-make-binding fun nfunc key))
  7327. (org-defkey orgstruct-mode-map key cmd))
  7328. ;; Special treatment needed for TAB and RET
  7329. (org-defkey orgstruct-mode-map [(tab)]
  7330. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7331. (org-defkey orgstruct-mode-map "\C-i"
  7332. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7333. (org-defkey orgstruct-mode-map "\M-\C-m"
  7334. (orgstruct-make-binding 'org-insert-heading 105
  7335. "\M-\C-m" [(meta return)]))
  7336. (org-defkey orgstruct-mode-map [(meta return)]
  7337. (orgstruct-make-binding 'org-insert-heading 106
  7338. [(meta return)] "\M-\C-m"))
  7339. (org-defkey orgstruct-mode-map [(shift meta return)]
  7340. (orgstruct-make-binding 'org-insert-todo-heading 107
  7341. [(meta return)] "\M-\C-m"))
  7342. (org-defkey orgstruct-mode-map "\e\C-m"
  7343. (orgstruct-make-binding 'org-insert-heading 108
  7344. "\e\C-m" [?\e (return)]))
  7345. (org-defkey orgstruct-mode-map [?\e (return)]
  7346. (orgstruct-make-binding 'org-insert-heading 109
  7347. [?\e (return)] "\e\C-m"))
  7348. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7349. (orgstruct-make-binding 'org-insert-todo-heading 110
  7350. [?\e (return)] "\e\C-m"))
  7351. (unless org-local-vars
  7352. (setq org-local-vars (org-get-local-variables)))
  7353. t))
  7354. (defun orgstruct-make-binding (fun n &rest keys)
  7355. "Create a function for binding in the structure minor mode.
  7356. FUN is the command to call inside a table. N is used to create a unique
  7357. command name. KEYS are keys that should be checked in for a command
  7358. to execute outside of tables."
  7359. (eval
  7360. (list 'defun
  7361. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7362. '(arg)
  7363. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7364. "Outside of structure, run the binding of `"
  7365. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7366. "'.")
  7367. '(interactive "p")
  7368. (list 'if
  7369. `(org-context-p 'headline 'item
  7370. (and orgstruct-is-++
  7371. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7372. 'item-body))
  7373. (list 'org-run-like-in-org-mode (list 'quote fun))
  7374. (list 'let '(orgstruct-mode)
  7375. (list 'call-interactively
  7376. (append '(or)
  7377. (mapcar (lambda (k)
  7378. (list 'key-binding k))
  7379. keys)
  7380. '('orgstruct-error))))))))
  7381. (defun org-context-p (&rest contexts)
  7382. "Check if local context is any of CONTEXTS.
  7383. Possible values in the list of contexts are `table', `headline', and `item'."
  7384. (let ((pos (point)))
  7385. (goto-char (point-at-bol))
  7386. (prog1 (or (and (memq 'table contexts)
  7387. (looking-at "[ \t]*|"))
  7388. (and (memq 'headline contexts)
  7389. ;;????????? (looking-at "\\*+"))
  7390. (looking-at outline-regexp))
  7391. (and (memq 'item contexts)
  7392. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7393. (and (memq 'item-body contexts)
  7394. (org-in-item-p)))
  7395. (goto-char pos))))
  7396. (defun org-get-local-variables ()
  7397. "Return a list of all local variables in an org-mode buffer."
  7398. (let (varlist)
  7399. (with-current-buffer (get-buffer-create "*Org tmp*")
  7400. (erase-buffer)
  7401. (org-mode)
  7402. (setq varlist (buffer-local-variables)))
  7403. (kill-buffer "*Org tmp*")
  7404. (delq nil
  7405. (mapcar
  7406. (lambda (x)
  7407. (setq x
  7408. (if (symbolp x)
  7409. (list x)
  7410. (list (car x) (list 'quote (cdr x)))))
  7411. (if (string-match
  7412. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7413. (symbol-name (car x)))
  7414. x nil))
  7415. varlist))))
  7416. (defun org-clone-local-variables (from-buffer &optional regexp)
  7417. "Clone local variables from FROM-BUFFER.
  7418. Optional argument REGEXP selects variables to clone."
  7419. (mapc
  7420. (lambda (pair)
  7421. (and (symbolp (car pair))
  7422. (or (null regexp)
  7423. (string-match regexp (symbol-name (car pair))))
  7424. (set (make-local-variable (car pair))
  7425. (cdr pair))))
  7426. (buffer-local-variables from-buffer)))
  7427. ;;;###autoload
  7428. (defun org-run-like-in-org-mode (cmd)
  7429. "Run a command, pretending that the current buffer is in Org-mode.
  7430. This will temporarily bind local variables that are typically bound in
  7431. Org-mode to the values they have in Org-mode, and then interactively
  7432. call CMD."
  7433. (org-load-modules-maybe)
  7434. (unless org-local-vars
  7435. (setq org-local-vars (org-get-local-variables)))
  7436. (eval (list 'let org-local-vars
  7437. (list 'call-interactively (list 'quote cmd)))))
  7438. ;;;; Archiving
  7439. (defun org-get-category (&optional pos force-refresh)
  7440. "Get the category applying to position POS."
  7441. (if force-refresh (org-refresh-category-properties))
  7442. (let ((pos (or pos (point))))
  7443. (or (get-text-property pos 'org-category)
  7444. (progn (org-refresh-category-properties)
  7445. (get-text-property pos 'org-category)))))
  7446. (defun org-refresh-category-properties ()
  7447. "Refresh category text properties in the buffer."
  7448. (let ((def-cat (cond
  7449. ((null org-category)
  7450. (if buffer-file-name
  7451. (file-name-sans-extension
  7452. (file-name-nondirectory buffer-file-name))
  7453. "???"))
  7454. ((symbolp org-category) (symbol-name org-category))
  7455. (t org-category)))
  7456. beg end cat pos optionp)
  7457. (org-unmodified
  7458. (save-excursion
  7459. (save-restriction
  7460. (widen)
  7461. (goto-char (point-min))
  7462. (put-text-property (point) (point-max) 'org-category def-cat)
  7463. (while (re-search-forward
  7464. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7465. (setq pos (match-end 0)
  7466. optionp (equal (char-after (match-beginning 0)) ?#)
  7467. cat (org-trim (match-string 2)))
  7468. (if optionp
  7469. (setq beg (point-at-bol) end (point-max))
  7470. (org-back-to-heading t)
  7471. (setq beg (point) end (org-end-of-subtree t t)))
  7472. (put-text-property beg end 'org-category cat)
  7473. (goto-char pos)))))))
  7474. ;;;; Link Stuff
  7475. ;;; Link abbreviations
  7476. (defun org-link-expand-abbrev (link)
  7477. "Apply replacements as defined in `org-link-abbrev-alist."
  7478. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7479. (let* ((key (match-string 1 link))
  7480. (as (or (assoc key org-link-abbrev-alist-local)
  7481. (assoc key org-link-abbrev-alist)))
  7482. (tag (and (match-end 2) (match-string 3 link)))
  7483. rpl)
  7484. (if (not as)
  7485. link
  7486. (setq rpl (cdr as))
  7487. (cond
  7488. ((symbolp rpl) (funcall rpl tag))
  7489. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7490. ((string-match "%h" rpl)
  7491. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7492. (t (concat rpl tag)))))
  7493. link))
  7494. ;;; Storing and inserting links
  7495. (defvar org-insert-link-history nil
  7496. "Minibuffer history for links inserted with `org-insert-link'.")
  7497. (defvar org-stored-links nil
  7498. "Contains the links stored with `org-store-link'.")
  7499. (defvar org-store-link-plist nil
  7500. "Plist with info about the most recently link created with `org-store-link'.")
  7501. (defvar org-link-protocols nil
  7502. "Link protocols added to Org-mode using `org-add-link-type'.")
  7503. (defvar org-store-link-functions nil
  7504. "List of functions that are called to create and store a link.
  7505. Each function will be called in turn until one returns a non-nil
  7506. value. Each function should check if it is responsible for creating
  7507. this link (for example by looking at the major mode).
  7508. If not, it must exit and return nil.
  7509. If yes, it should return a non-nil value after a calling
  7510. `org-store-link-props' with a list of properties and values.
  7511. Special properties are:
  7512. :type The link prefix, like \"http\". This must be given.
  7513. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7514. This is obligatory as well.
  7515. :description Optional default description for the second pair
  7516. of brackets in an Org-mode link. The user can still change
  7517. this when inserting this link into an Org-mode buffer.
  7518. In addition to these, any additional properties can be specified
  7519. and then used in remember templates.")
  7520. (defun org-add-link-type (type &optional follow export)
  7521. "Add TYPE to the list of `org-link-types'.
  7522. Re-compute all regular expressions depending on `org-link-types'
  7523. FOLLOW and EXPORT are two functions.
  7524. FOLLOW should take the link path as the single argument and do whatever
  7525. is necessary to follow the link, for example find a file or display
  7526. a mail message.
  7527. EXPORT should format the link path for export to one of the export formats.
  7528. It should be a function accepting three arguments:
  7529. path the path of the link, the text after the prefix (like \"http:\")
  7530. desc the description of the link, if any, or a description added by
  7531. org-export-normalize-links if there is none
  7532. format the export format, a symbol like `html' or `latex' or `ascii'..
  7533. The function may use the FORMAT information to return different values
  7534. depending on the format. The return value will be put literally into
  7535. the exported file. If the return value is nil, this means Org should
  7536. do what it normally does with links which do not have EXPORT defined.
  7537. Org-mode has a built-in default for exporting links. If you are happy with
  7538. this default, there is no need to define an export function for the link
  7539. type. For a simple example of an export function, see `org-bbdb.el'."
  7540. (add-to-list 'org-link-types type t)
  7541. (org-make-link-regexps)
  7542. (if (assoc type org-link-protocols)
  7543. (setcdr (assoc type org-link-protocols) (list follow export))
  7544. (push (list type follow export) org-link-protocols)))
  7545. (defvar org-agenda-buffer-name)
  7546. ;;;###autoload
  7547. (defun org-store-link (arg)
  7548. "\\<org-mode-map>Store an org-link to the current location.
  7549. This link is added to `org-stored-links' and can later be inserted
  7550. into an org-buffer with \\[org-insert-link].
  7551. For some link types, a prefix arg is interpreted:
  7552. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7553. For file links, arg negates `org-context-in-file-links'."
  7554. (interactive "P")
  7555. (org-load-modules-maybe)
  7556. (setq org-store-link-plist nil) ; reset
  7557. (org-with-limited-levels
  7558. (let (link cpltxt desc description search txt custom-id agenda-link)
  7559. (cond
  7560. ((run-hook-with-args-until-success 'org-store-link-functions)
  7561. (setq link (plist-get org-store-link-plist :link)
  7562. desc (or (plist-get org-store-link-plist :description) link)))
  7563. ((equal (buffer-name) "*Org Edit Src Example*")
  7564. (let (label gc)
  7565. (while (or (not label)
  7566. (save-excursion
  7567. (save-restriction
  7568. (widen)
  7569. (goto-char (point-min))
  7570. (re-search-forward
  7571. (regexp-quote (format org-coderef-label-format label))
  7572. nil t))))
  7573. (when label (message "Label exists already") (sit-for 2))
  7574. (setq label (read-string "Code line label: " label)))
  7575. (end-of-line 1)
  7576. (setq link (format org-coderef-label-format label))
  7577. (setq gc (- 79 (length link)))
  7578. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7579. (insert link)
  7580. (setq link (concat "(" label ")") desc nil)))
  7581. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7582. ;; We are in the agenda, link to referenced location
  7583. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7584. (get-text-property (point) 'org-marker))))
  7585. (when m
  7586. (org-with-point-at m
  7587. (setq agenda-link
  7588. (if (org-called-interactively-p 'any)
  7589. (call-interactively 'org-store-link)
  7590. (org-store-link nil)))))))
  7591. ((eq major-mode 'calendar-mode)
  7592. (let ((cd (calendar-cursor-to-date)))
  7593. (setq link
  7594. (format-time-string
  7595. (car org-time-stamp-formats)
  7596. (apply 'encode-time
  7597. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7598. nil nil nil))))
  7599. (org-store-link-props :type "calendar" :date cd)))
  7600. ((eq major-mode 'w3-mode)
  7601. (setq cpltxt (if (and (buffer-name)
  7602. (not (string-match "Untitled" (buffer-name))))
  7603. (buffer-name)
  7604. (url-view-url t))
  7605. link (org-make-link (url-view-url t)))
  7606. (org-store-link-props :type "w3" :url (url-view-url t)))
  7607. ((eq major-mode 'w3m-mode)
  7608. (setq cpltxt (or w3m-current-title w3m-current-url)
  7609. link (org-make-link w3m-current-url))
  7610. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7611. ((setq search (run-hook-with-args-until-success
  7612. 'org-create-file-search-functions))
  7613. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7614. "::" search))
  7615. (setq cpltxt (or description link)))
  7616. ((eq major-mode 'image-mode)
  7617. (setq cpltxt (concat "file:"
  7618. (abbreviate-file-name buffer-file-name))
  7619. link (org-make-link cpltxt))
  7620. (org-store-link-props :type "image" :file buffer-file-name))
  7621. ((eq major-mode 'dired-mode)
  7622. ;; link to the file in the current line
  7623. (let ((file (dired-get-filename nil t)))
  7624. (setq file (if file
  7625. (abbreviate-file-name
  7626. (expand-file-name (dired-get-filename nil t)))
  7627. ;; otherwise, no file so use current directory.
  7628. default-directory))
  7629. (setq cpltxt (concat "file:" file)
  7630. link (org-make-link cpltxt))))
  7631. ((and (buffer-file-name (buffer-base-buffer)) (org-mode-p))
  7632. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7633. (cond
  7634. ((org-in-regexp "<<\\(.*?\\)>>")
  7635. (setq cpltxt
  7636. (concat "file:"
  7637. (abbreviate-file-name
  7638. (buffer-file-name (buffer-base-buffer)))
  7639. "::" (match-string 1))
  7640. link (org-make-link cpltxt)))
  7641. ((and (featurep 'org-id)
  7642. (or (eq org-link-to-org-use-id t)
  7643. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7644. (org-called-interactively-p 'any))
  7645. (and (eq org-link-to-org-use-id
  7646. 'create-if-interactive-and-no-custom-id)
  7647. (org-called-interactively-p 'any)
  7648. (not custom-id))
  7649. (and org-link-to-org-use-id
  7650. (org-entry-get nil "ID"))))
  7651. ;; We can make a link using the ID.
  7652. (setq link (condition-case nil
  7653. (prog1 (org-id-store-link)
  7654. (setq desc (plist-get org-store-link-plist
  7655. :description)))
  7656. (error
  7657. ;; probably before first headline, link to file only
  7658. (concat "file:"
  7659. (abbreviate-file-name
  7660. (buffer-file-name (buffer-base-buffer))))))))
  7661. (t
  7662. ;; Just link to current headline
  7663. (setq cpltxt (concat "file:"
  7664. (abbreviate-file-name
  7665. (buffer-file-name (buffer-base-buffer)))))
  7666. ;; Add a context search string
  7667. (when (org-xor org-context-in-file-links arg)
  7668. (setq txt (cond
  7669. ((org-on-heading-p) nil)
  7670. ((org-region-active-p)
  7671. (buffer-substring (region-beginning) (region-end)))
  7672. (t nil)))
  7673. (when (or (null txt) (string-match "\\S-" txt))
  7674. (setq cpltxt
  7675. (concat cpltxt "::"
  7676. (condition-case nil
  7677. (org-make-org-heading-search-string txt)
  7678. (error "")))
  7679. desc (or (nth 4 (ignore-errors
  7680. (org-heading-components))) "NONE"))))
  7681. (if (string-match "::\\'" cpltxt)
  7682. (setq cpltxt (substring cpltxt 0 -2)))
  7683. (setq link (org-make-link cpltxt)))))
  7684. ((buffer-file-name (buffer-base-buffer))
  7685. ;; Just link to this file here.
  7686. (setq cpltxt (concat "file:"
  7687. (abbreviate-file-name
  7688. (buffer-file-name (buffer-base-buffer)))))
  7689. ;; Add a context string
  7690. (when (org-xor org-context-in-file-links arg)
  7691. (setq txt (if (org-region-active-p)
  7692. (buffer-substring (region-beginning) (region-end))
  7693. (buffer-substring (point-at-bol) (point-at-eol))))
  7694. ;; Only use search option if there is some text.
  7695. (when (string-match "\\S-" txt)
  7696. (setq cpltxt
  7697. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7698. desc "NONE")))
  7699. (setq link (org-make-link cpltxt)))
  7700. ((org-called-interactively-p 'interactive)
  7701. (error "Cannot link to a buffer which is not visiting a file"))
  7702. (t (setq link nil)))
  7703. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7704. (setq link (or link cpltxt)
  7705. desc (or desc cpltxt))
  7706. (if (equal desc "NONE") (setq desc nil))
  7707. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  7708. (progn
  7709. (setq org-stored-links
  7710. (cons (list link desc) org-stored-links))
  7711. (message "Stored: %s" (or desc link))
  7712. (when custom-id
  7713. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7714. "::#" custom-id))
  7715. (setq org-stored-links
  7716. (cons (list link desc) org-stored-links))))
  7717. (or agenda-link (and link (org-make-link-string link desc)))))))
  7718. (defun org-store-link-props (&rest plist)
  7719. "Store link properties, extract names and addresses."
  7720. (let (x adr)
  7721. (when (setq x (plist-get plist :from))
  7722. (setq adr (mail-extract-address-components x))
  7723. (setq plist (plist-put plist :fromname (car adr)))
  7724. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7725. (when (setq x (plist-get plist :to))
  7726. (setq adr (mail-extract-address-components x))
  7727. (setq plist (plist-put plist :toname (car adr)))
  7728. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7729. (let ((from (plist-get plist :from))
  7730. (to (plist-get plist :to)))
  7731. (when (and from to org-from-is-user-regexp)
  7732. (setq plist
  7733. (plist-put plist :fromto
  7734. (if (string-match org-from-is-user-regexp from)
  7735. (concat "to %t")
  7736. (concat "from %f"))))))
  7737. (setq org-store-link-plist plist))
  7738. (defun org-add-link-props (&rest plist)
  7739. "Add these properties to the link property list."
  7740. (let (key value)
  7741. (while plist
  7742. (setq key (pop plist) value (pop plist))
  7743. (setq org-store-link-plist
  7744. (plist-put org-store-link-plist key value)))))
  7745. (defun org-email-link-description (&optional fmt)
  7746. "Return the description part of an email link.
  7747. This takes information from `org-store-link-plist' and formats it
  7748. according to FMT (default from `org-email-link-description-format')."
  7749. (setq fmt (or fmt org-email-link-description-format))
  7750. (let* ((p org-store-link-plist)
  7751. (to (plist-get p :toaddress))
  7752. (from (plist-get p :fromaddress))
  7753. (table
  7754. (list
  7755. (cons "%c" (plist-get p :fromto))
  7756. (cons "%F" (plist-get p :from))
  7757. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7758. (cons "%T" (plist-get p :to))
  7759. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7760. (cons "%s" (plist-get p :subject))
  7761. (cons "%d" (plist-get p :date))
  7762. (cons "%m" (plist-get p :message-id)))))
  7763. (when (string-match "%c" fmt)
  7764. ;; Check if the user wrote this message
  7765. (if (and org-from-is-user-regexp from to
  7766. (save-match-data (string-match org-from-is-user-regexp from)))
  7767. (setq fmt (replace-match "to %t" t t fmt))
  7768. (setq fmt (replace-match "from %f" t t fmt))))
  7769. (org-replace-escapes fmt table)))
  7770. (defun org-make-org-heading-search-string (&optional string heading)
  7771. "Make search string for STRING or current headline."
  7772. (interactive)
  7773. (let ((s (or string (org-get-heading)))
  7774. (lines org-context-in-file-links))
  7775. (unless (and string (not heading))
  7776. ;; We are using a headline, clean up garbage in there.
  7777. (if (string-match org-todo-regexp s)
  7778. (setq s (replace-match "" t t s)))
  7779. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7780. (setq s (replace-match "" t t s)))
  7781. (setq s (org-trim s))
  7782. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7783. org-comment-string "\\)") s)
  7784. (setq s (replace-match "" t t s)))
  7785. (while (string-match org-ts-regexp s)
  7786. (setq s (replace-match "" t t s))))
  7787. (or string (setq s (concat "*" s))) ; Add * for headlines
  7788. (when (and string (integerp lines) (> lines 0))
  7789. (let ((slines (org-split-string s "\n")))
  7790. (when (< lines (length slines))
  7791. (setq s (mapconcat
  7792. 'identity
  7793. (reverse (nthcdr (- (length slines) lines)
  7794. (reverse slines))) "\n")))))
  7795. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7796. (defun org-make-link (&rest strings)
  7797. "Concatenate STRINGS."
  7798. (apply 'concat strings))
  7799. (defun org-make-link-string (link &optional description)
  7800. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7801. (unless (string-match "\\S-" link)
  7802. (error "Empty link"))
  7803. (when (and description
  7804. (stringp description)
  7805. (not (string-match "\\S-" description)))
  7806. (setq description nil))
  7807. (when (stringp description)
  7808. ;; Remove brackets from the description, they are fatal.
  7809. (while (string-match "\\[" description)
  7810. (setq description (replace-match "{" t t description)))
  7811. (while (string-match "\\]" description)
  7812. (setq description (replace-match "}" t t description))))
  7813. (when (equal link description)
  7814. ;; No description needed, it is identical
  7815. (setq description nil))
  7816. (when (and (not description)
  7817. (not (string-match (org-image-file-name-regexp) link))
  7818. (not (equal link (org-link-escape link))))
  7819. (setq description (org-extract-attributes link)))
  7820. (setq link
  7821. (cond ((string-match (org-image-file-name-regexp) link) link)
  7822. ((string-match org-link-types-re link)
  7823. (concat (match-string 1 link)
  7824. (org-link-escape (substring link (match-end 1)))))
  7825. (t (org-link-escape link))))
  7826. (concat "[[" link "]"
  7827. (if description (concat "[" description "]") "")
  7828. "]"))
  7829. (defconst org-link-escape-chars
  7830. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  7831. "List of characters that should be escaped in link.
  7832. This is the list that is used for internal purposes.")
  7833. (defvar org-url-encoding-use-url-hexify nil)
  7834. (defconst org-link-escape-chars-browser
  7835. '(?\ )
  7836. "List of escapes for characters that are problematic in links.
  7837. This is the list that is used before handing over to the browser.")
  7838. (defun org-link-escape (text &optional table merge)
  7839. "Return percent escaped representation of TEXT.
  7840. TEXT is a string with the text to escape.
  7841. Optional argument TABLE is a list with characters that should be
  7842. escaped. When nil, `org-link-escape-chars' is used.
  7843. If optional argument MERGE is set, merge TABLE into
  7844. `org-link-escape-chars'."
  7845. (if (and org-url-encoding-use-url-hexify (not table))
  7846. (url-hexify-string text)
  7847. (cond
  7848. ((and table merge)
  7849. (mapc (lambda (defchr)
  7850. (unless (member defchr table)
  7851. (setq table (cons defchr table)))) org-link-escape-chars))
  7852. ((null table)
  7853. (setq table org-link-escape-chars)))
  7854. (mapconcat
  7855. (lambda (char)
  7856. (if (or (member char table)
  7857. (< char 32) (= char 37) (> char 126))
  7858. (mapconcat (lambda (sequence-element)
  7859. (format "%%%.2X" sequence-element))
  7860. (or (encode-coding-char char 'utf-8)
  7861. (error "Unable to percent escape character: %s"
  7862. (char-to-string char))) "")
  7863. (char-to-string char))) text "")))
  7864. (defun org-link-unescape (str)
  7865. "Unhex hexified unicode strings as returned from the JavaScript function
  7866. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  7867. (unless (and (null str) (string= "" str))
  7868. (let ((pos 0) (case-fold-search t) unhexed)
  7869. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  7870. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  7871. (setq str (replace-match unhexed t t str))
  7872. (setq pos (+ pos (length unhexed))))))
  7873. str)
  7874. (defun org-link-unescape-compound (hex)
  7875. "Unhexify unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  7876. Note: this function also decodes single byte encodings like
  7877. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  7878. (save-match-data
  7879. (let* ((bytes (cdr (split-string hex "%")))
  7880. (ret "")
  7881. (eat 0)
  7882. (sum 0))
  7883. (while bytes
  7884. (let* ((val (string-to-number (pop bytes) 16))
  7885. (shift-xor
  7886. (if (= 0 eat)
  7887. (cond
  7888. ((>= val 252) (cons 6 252))
  7889. ((>= val 248) (cons 5 248))
  7890. ((>= val 240) (cons 4 240))
  7891. ((>= val 224) (cons 3 224))
  7892. ((>= val 192) (cons 2 192))
  7893. (t (cons 0 0)))
  7894. (cons 6 128))))
  7895. (if (>= val 192) (setq eat (car shift-xor)))
  7896. (setq val (logxor val (cdr shift-xor)))
  7897. (setq sum (+ (lsh sum (car shift-xor)) val))
  7898. (if (> eat 0) (setq eat (- eat 1)))
  7899. (cond
  7900. ((= 0 eat) ;multi byte
  7901. (setq ret (concat ret (org-char-to-string sum)))
  7902. (setq sum 0))
  7903. ((not bytes) ; single byte(s)
  7904. (setq ret (org-link-unescape-single-byte-sequence hex))))
  7905. )) ;; end (while bytes
  7906. ret )))
  7907. (defun org-link-unescape-single-byte-sequence (hex)
  7908. "Unhexify hex-encoded single byte character sequences."
  7909. (mapconcat (lambda (byte)
  7910. (char-to-string (string-to-number byte 16)))
  7911. (cdr (split-string hex "%")) ""))
  7912. (defun org-xor (a b)
  7913. "Exclusive or."
  7914. (if a (not b) b))
  7915. (defun org-fixup-message-id-for-http (s)
  7916. "Replace special characters in a message id, so it can be used in an http query."
  7917. (when (string-match "%" s)
  7918. (setq s (mapconcat (lambda (c)
  7919. (if (eq c ?%)
  7920. "%25"
  7921. (char-to-string c)))
  7922. s "")))
  7923. (while (string-match "<" s)
  7924. (setq s (replace-match "%3C" t t s)))
  7925. (while (string-match ">" s)
  7926. (setq s (replace-match "%3E" t t s)))
  7927. (while (string-match "@" s)
  7928. (setq s (replace-match "%40" t t s)))
  7929. s)
  7930. ;;;###autoload
  7931. (defun org-insert-link-global ()
  7932. "Insert a link like Org-mode does.
  7933. This command can be called in any mode to insert a link in Org-mode syntax."
  7934. (interactive)
  7935. (org-load-modules-maybe)
  7936. (org-run-like-in-org-mode 'org-insert-link))
  7937. (defun org-insert-link (&optional complete-file link-location)
  7938. "Insert a link. At the prompt, enter the link.
  7939. Completion can be used to insert any of the link protocol prefixes like
  7940. http or ftp in use.
  7941. The history can be used to select a link previously stored with
  7942. `org-store-link'. When the empty string is entered (i.e. if you just
  7943. press RET at the prompt), the link defaults to the most recently
  7944. stored link. As SPC triggers completion in the minibuffer, you need to
  7945. use M-SPC or C-q SPC to force the insertion of a space character.
  7946. You will also be prompted for a description, and if one is given, it will
  7947. be displayed in the buffer instead of the link.
  7948. If there is already a link at point, this command will allow you to edit link
  7949. and description parts.
  7950. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7951. be selected using completion. The path to the file will be relative to the
  7952. current directory if the file is in the current directory or a subdirectory.
  7953. Otherwise, the link will be the absolute path as completed in the minibuffer
  7954. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7955. option `org-link-file-path-type'.
  7956. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7957. the current directory or below.
  7958. With three \\[universal-argument] prefixes, negate the meaning of
  7959. `org-keep-stored-link-after-insertion'.
  7960. If `org-make-link-description-function' is non-nil, this function will be
  7961. called with the link target, and the result will be the default
  7962. link description.
  7963. If the LINK-LOCATION parameter is non-nil, this value will be
  7964. used as the link location instead of reading one interactively."
  7965. (interactive "P")
  7966. (let* ((wcf (current-window-configuration))
  7967. (region (if (org-region-active-p)
  7968. (buffer-substring (region-beginning) (region-end))))
  7969. (remove (and region (list (region-beginning) (region-end))))
  7970. (desc region)
  7971. tmphist ; byte-compile incorrectly complains about this
  7972. (link link-location)
  7973. entry file all-prefixes)
  7974. (cond
  7975. (link-location) ; specified by arg, just use it.
  7976. ((org-in-regexp org-bracket-link-regexp 1)
  7977. ;; We do have a link at point, and we are going to edit it.
  7978. (setq remove (list (match-beginning 0) (match-end 0)))
  7979. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7980. (setq link (read-string "Link: "
  7981. (org-link-unescape
  7982. (org-match-string-no-properties 1)))))
  7983. ((or (org-in-regexp org-angle-link-re)
  7984. (org-in-regexp org-plain-link-re))
  7985. ;; Convert to bracket link
  7986. (setq remove (list (match-beginning 0) (match-end 0))
  7987. link (read-string "Link: "
  7988. (org-remove-angle-brackets (match-string 0)))))
  7989. ((member complete-file '((4) (16)))
  7990. ;; Completing read for file names.
  7991. (setq link (org-file-complete-link complete-file)))
  7992. (t
  7993. ;; Read link, with completion for stored links.
  7994. (with-output-to-temp-buffer "*Org Links*"
  7995. (princ "Insert a link.
  7996. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7997. (when org-stored-links
  7998. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7999. (princ (mapconcat
  8000. (lambda (x)
  8001. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  8002. (reverse org-stored-links) "\n"))))
  8003. (let ((cw (selected-window)))
  8004. (select-window (get-buffer-window "*Org Links*" 'visible))
  8005. (with-current-buffer "*Org Links*" (setq truncate-lines) t)
  8006. (unless (pos-visible-in-window-p (point-max))
  8007. (org-fit-window-to-buffer))
  8008. (and (window-live-p cw) (select-window cw)))
  8009. ;; Fake a link history, containing the stored links.
  8010. (setq tmphist (append (mapcar 'car org-stored-links)
  8011. org-insert-link-history))
  8012. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8013. (mapcar 'car org-link-abbrev-alist)
  8014. org-link-types))
  8015. (unwind-protect
  8016. (progn
  8017. (setq link
  8018. (let ((org-completion-use-ido nil)
  8019. (org-completion-use-iswitchb nil))
  8020. (org-completing-read
  8021. "Link: "
  8022. (append
  8023. (mapcar (lambda (x) (list (concat x ":")))
  8024. all-prefixes)
  8025. (mapcar 'car org-stored-links))
  8026. nil nil nil
  8027. 'tmphist
  8028. (car (car org-stored-links)))))
  8029. (if (not (string-match "\\S-" link))
  8030. (error "No link selected"))
  8031. (if (or (member link all-prefixes)
  8032. (and (equal ":" (substring link -1))
  8033. (member (substring link 0 -1) all-prefixes)
  8034. (setq link (substring link 0 -1))))
  8035. (setq link (org-link-try-special-completion link))))
  8036. (set-window-configuration wcf)
  8037. (kill-buffer "*Org Links*"))
  8038. (setq entry (assoc link org-stored-links))
  8039. (or entry (push link org-insert-link-history))
  8040. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8041. (not org-keep-stored-link-after-insertion))
  8042. (setq org-stored-links (delq (assoc link org-stored-links)
  8043. org-stored-links)))
  8044. (setq desc (or desc (nth 1 entry)))))
  8045. (if (string-match org-plain-link-re link)
  8046. ;; URL-like link, normalize the use of angular brackets.
  8047. (setq link (org-make-link (org-remove-angle-brackets link))))
  8048. ;; Check if we are linking to the current file with a search option
  8049. ;; If yes, simplify the link by using only the search option.
  8050. (when (and buffer-file-name
  8051. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8052. (let* ((path (match-string 1 link))
  8053. (case-fold-search nil)
  8054. (search (match-string 2 link)))
  8055. (save-match-data
  8056. (if (equal (file-truename buffer-file-name) (file-truename path))
  8057. ;; We are linking to this same file, with a search option
  8058. (setq link search)))))
  8059. ;; Check if we can/should use a relative path. If yes, simplify the link
  8060. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8061. (let* ((type (match-string 1 link))
  8062. (path (match-string 2 link))
  8063. (origpath path)
  8064. (case-fold-search nil))
  8065. (cond
  8066. ((or (eq org-link-file-path-type 'absolute)
  8067. (equal complete-file '(16)))
  8068. (setq path (abbreviate-file-name (expand-file-name path))))
  8069. ((eq org-link-file-path-type 'noabbrev)
  8070. (setq path (expand-file-name path)))
  8071. ((eq org-link-file-path-type 'relative)
  8072. (setq path (file-relative-name path)))
  8073. (t
  8074. (save-match-data
  8075. (if (string-match (concat "^" (regexp-quote
  8076. (expand-file-name
  8077. (file-name-as-directory
  8078. default-directory))))
  8079. (expand-file-name path))
  8080. ;; We are linking a file with relative path name.
  8081. (setq path (substring (expand-file-name path)
  8082. (match-end 0)))
  8083. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8084. (setq link (concat type path))
  8085. (if (equal desc origpath)
  8086. (setq desc path))))
  8087. (if org-make-link-description-function
  8088. (setq desc (funcall org-make-link-description-function link desc)))
  8089. (setq desc (read-string "Description: " desc))
  8090. (unless (string-match "\\S-" desc) (setq desc nil))
  8091. (if remove (apply 'delete-region remove))
  8092. (insert (org-make-link-string link desc))))
  8093. (defun org-link-try-special-completion (type)
  8094. "If there is completion support for link type TYPE, offer it."
  8095. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8096. (if (functionp fun)
  8097. (funcall fun)
  8098. (read-string "Link (no completion support): " (concat type ":")))))
  8099. (defun org-file-complete-link (&optional arg)
  8100. "Create a file link using completion."
  8101. (let (file link)
  8102. (setq file (read-file-name "File: "))
  8103. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8104. (pwd1 (file-name-as-directory (abbreviate-file-name
  8105. (expand-file-name ".")))))
  8106. (cond
  8107. ((equal arg '(16))
  8108. (setq link (org-make-link
  8109. "file:"
  8110. (abbreviate-file-name (expand-file-name file)))))
  8111. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8112. (setq link (org-make-link "file:" (match-string 1 file))))
  8113. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8114. (expand-file-name file))
  8115. (setq link (org-make-link
  8116. "file:" (match-string 1 (expand-file-name file)))))
  8117. (t (setq link (org-make-link "file:" file)))))
  8118. link))
  8119. (defun org-completing-read (&rest args)
  8120. "Completing-read with SPACE being a normal character."
  8121. (let ((minibuffer-local-completion-map
  8122. (copy-keymap minibuffer-local-completion-map)))
  8123. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8124. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8125. (apply 'org-icompleting-read args)))
  8126. (defun org-completing-read-no-i (&rest args)
  8127. (let (org-completion-use-ido org-completion-use-iswitchb)
  8128. (apply 'org-completing-read args)))
  8129. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8130. "Use iswitch as a completing-read replacement to choose from choices.
  8131. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8132. from."
  8133. (let* ((iswitchb-use-virtual-buffers nil)
  8134. (iswitchb-make-buflist-hook
  8135. (lambda ()
  8136. (setq iswitchb-temp-buflist choices))))
  8137. (iswitchb-read-buffer prompt)))
  8138. (defun org-icompleting-read (&rest args)
  8139. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8140. (org-without-partial-completion
  8141. (if (and org-completion-use-ido
  8142. (fboundp 'ido-completing-read)
  8143. (boundp 'ido-mode) ido-mode
  8144. (listp (second args)))
  8145. (let ((ido-enter-matching-directory nil))
  8146. (apply 'ido-completing-read (concat (car args))
  8147. (if (consp (car (nth 1 args)))
  8148. (mapcar 'car (nth 1 args))
  8149. (nth 1 args))
  8150. (cddr args)))
  8151. (if (and org-completion-use-iswitchb
  8152. (boundp 'iswitchb-mode) iswitchb-mode
  8153. (listp (second args)))
  8154. (apply 'org-iswitchb-completing-read (concat (car args))
  8155. (if (consp (car (nth 1 args)))
  8156. (mapcar 'car (nth 1 args))
  8157. (nth 1 args))
  8158. (cddr args))
  8159. (apply 'completing-read args)))))
  8160. (defun org-extract-attributes (s)
  8161. "Extract the attributes cookie from a string and set as text property."
  8162. (let (a attr (start 0) key value)
  8163. (save-match-data
  8164. (when (string-match "{{\\([^}]+\\)}}$" s)
  8165. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8166. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8167. (setq key (match-string 1 a) value (match-string 2 a)
  8168. start (match-end 0)
  8169. attr (plist-put attr (intern key) value))))
  8170. (org-add-props s nil 'org-attr attr))
  8171. s))
  8172. (defun org-extract-attributes-from-string (tag)
  8173. (let (key value attr)
  8174. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8175. (setq key (match-string 1 tag) value (match-string 2 tag)
  8176. tag (replace-match "" t t tag)
  8177. attr (plist-put attr (intern key) value)))
  8178. (cons tag attr)))
  8179. (defun org-attributes-to-string (plist)
  8180. "Format a property list into an HTML attribute list."
  8181. (let ((s "") key value)
  8182. (while plist
  8183. (setq key (pop plist) value (pop plist))
  8184. (and value
  8185. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8186. s))
  8187. ;;; Opening/following a link
  8188. (defvar org-link-search-failed nil)
  8189. (defvar org-open-link-functions nil
  8190. "Hook for functions finding a plain text link.
  8191. These functions must take a single argument, the link content.
  8192. They will be called for links that look like [[link text][description]]
  8193. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8194. like a filename (e.g. \"./blue.png\").
  8195. These functions will be called *before* Org attempts to resolve the
  8196. link by doing text searches in the current buffer - so if you want a
  8197. link \"[[target]]\" to still find \"<<target>>\", your function should
  8198. handle this as a special case.
  8199. When the function does handle the link, it must return a non-nil value.
  8200. If it decides that it is not responsible for this link, it must return
  8201. nil to indicate that that Org-mode can continue with other options
  8202. like exact and fuzzy text search.")
  8203. (defun org-next-link ()
  8204. "Move forward to the next link.
  8205. If the link is in hidden text, expose it."
  8206. (interactive)
  8207. (when (and org-link-search-failed (eq this-command last-command))
  8208. (goto-char (point-min))
  8209. (message "Link search wrapped back to beginning of buffer"))
  8210. (setq org-link-search-failed nil)
  8211. (let* ((pos (point))
  8212. (ct (org-context))
  8213. (a (assoc :link ct)))
  8214. (if a (goto-char (nth 2 a)))
  8215. (if (re-search-forward org-any-link-re nil t)
  8216. (progn
  8217. (goto-char (match-beginning 0))
  8218. (if (outline-invisible-p) (org-show-context)))
  8219. (goto-char pos)
  8220. (setq org-link-search-failed t)
  8221. (error "No further link found"))))
  8222. (defun org-previous-link ()
  8223. "Move backward to the previous link.
  8224. If the link is in hidden text, expose it."
  8225. (interactive)
  8226. (when (and org-link-search-failed (eq this-command last-command))
  8227. (goto-char (point-max))
  8228. (message "Link search wrapped back to end of buffer"))
  8229. (setq org-link-search-failed nil)
  8230. (let* ((pos (point))
  8231. (ct (org-context))
  8232. (a (assoc :link ct)))
  8233. (if a (goto-char (nth 1 a)))
  8234. (if (re-search-backward org-any-link-re nil t)
  8235. (progn
  8236. (goto-char (match-beginning 0))
  8237. (if (outline-invisible-p) (org-show-context)))
  8238. (goto-char pos)
  8239. (setq org-link-search-failed t)
  8240. (error "No further link found"))))
  8241. (defun org-translate-link (s)
  8242. "Translate a link string if a translation function has been defined."
  8243. (if (and org-link-translation-function
  8244. (fboundp org-link-translation-function)
  8245. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8246. (progn
  8247. (setq s (funcall org-link-translation-function
  8248. (match-string 1) (match-string 2)))
  8249. (concat (car s) ":" (cdr s)))
  8250. s))
  8251. (defun org-translate-link-from-planner (type path)
  8252. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8253. This is still an experimental function, your mileage may vary."
  8254. (cond
  8255. ((member type '("http" "https" "news" "ftp"))
  8256. ;; standard Internet links are the same.
  8257. nil)
  8258. ((and (equal type "irc") (string-match "^//" path))
  8259. ;; Planner has two / at the beginning of an irc link, we have 1.
  8260. ;; We should have zero, actually....
  8261. (setq path (substring path 1)))
  8262. ((and (equal type "lisp") (string-match "^/" path))
  8263. ;; Planner has a slash, we do not.
  8264. (setq type "elisp" path (substring path 1)))
  8265. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8266. ;; A typical message link. Planner has the id after the final slash,
  8267. ;; we separate it with a hash mark
  8268. (setq path (concat (match-string 1 path) "#"
  8269. (org-remove-angle-brackets (match-string 2 path)))))
  8270. )
  8271. (cons type path))
  8272. (defun org-find-file-at-mouse (ev)
  8273. "Open file link or URL at mouse."
  8274. (interactive "e")
  8275. (mouse-set-point ev)
  8276. (org-open-at-point 'in-emacs))
  8277. (defun org-open-at-mouse (ev)
  8278. "Open file link or URL at mouse."
  8279. (interactive "e")
  8280. (mouse-set-point ev)
  8281. (if (eq major-mode 'org-agenda-mode)
  8282. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8283. (org-open-at-point))
  8284. (defvar org-window-config-before-follow-link nil
  8285. "The window configuration before following a link.
  8286. This is saved in case the need arises to restore it.")
  8287. (defvar org-open-link-marker (make-marker)
  8288. "Marker pointing to the location where `org-open-at-point; was called.")
  8289. ;;;###autoload
  8290. (defun org-open-at-point-global ()
  8291. "Follow a link like Org-mode does.
  8292. This command can be called in any mode to follow a link that has
  8293. Org-mode syntax."
  8294. (interactive)
  8295. (org-run-like-in-org-mode 'org-open-at-point))
  8296. ;;;###autoload
  8297. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8298. "Open a link in the string S, as if it was in Org-mode."
  8299. (interactive "sLink: \nP")
  8300. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8301. (with-temp-buffer
  8302. (let ((org-inhibit-startup t))
  8303. (org-mode)
  8304. (insert s)
  8305. (goto-char (point-min))
  8306. (when reference-buffer
  8307. (setq org-link-abbrev-alist-local
  8308. (with-current-buffer reference-buffer
  8309. org-link-abbrev-alist-local)))
  8310. (org-open-at-point arg reference-buffer)))))
  8311. (defvar org-open-at-point-functions nil
  8312. "Hook that is run when following a link at point.
  8313. Functions in this hook must return t if they identify and follow
  8314. a link at point. If they don't find anything interesting at point,
  8315. they must return nil.")
  8316. (defun org-open-at-point (&optional arg reference-buffer)
  8317. "Open link at or after point.
  8318. If there is no link at point, this function will search forward up to
  8319. the end of the current line.
  8320. Normally, files will be opened by an appropriate application. If the
  8321. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8322. With a double prefix argument, try to open outside of Emacs, in the
  8323. application the system uses for this file type."
  8324. (interactive "P")
  8325. ;; if in a code block, then open the block's results
  8326. (unless (call-interactively #'org-babel-open-src-block-result)
  8327. (org-load-modules-maybe)
  8328. (move-marker org-open-link-marker (point))
  8329. (setq org-window-config-before-follow-link (current-window-configuration))
  8330. (org-remove-occur-highlights nil nil t)
  8331. (cond
  8332. ((and (org-on-heading-p)
  8333. (not (org-in-regexp
  8334. (concat org-plain-link-re "\\|"
  8335. org-bracket-link-regexp "\\|"
  8336. org-angle-link-re "\\|"
  8337. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8338. (not (get-text-property (point) 'org-linked-text)))
  8339. (or (org-offer-links-in-entry arg)
  8340. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8341. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8342. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8343. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8344. (not (org-in-regexp org-bracket-link-regexp)))
  8345. (org-footnote-action))
  8346. (t
  8347. (let (type path link line search (pos (point)))
  8348. (catch 'match
  8349. (save-excursion
  8350. (skip-chars-forward "^]\n\r")
  8351. (when (org-in-regexp org-bracket-link-regexp 1)
  8352. (setq link (org-extract-attributes
  8353. (org-link-unescape (org-match-string-no-properties 1))))
  8354. (while (string-match " *\n *" link)
  8355. (setq link (replace-match " " t t link)))
  8356. (setq link (org-link-expand-abbrev link))
  8357. (cond
  8358. ((or (file-name-absolute-p link)
  8359. (string-match "^\\.\\.?/" link))
  8360. (setq type "file" path link))
  8361. ((string-match org-link-re-with-space3 link)
  8362. (setq type (match-string 1 link) path (match-string 2 link)))
  8363. (t (setq type "thisfile" path link)))
  8364. (throw 'match t)))
  8365. (when (get-text-property (point) 'org-linked-text)
  8366. (setq type "thisfile"
  8367. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8368. (1+ (point)) (point))
  8369. path (buffer-substring
  8370. (or (previous-single-property-change pos 'org-linked-text)
  8371. (point-min))
  8372. (or (next-single-property-change pos 'org-linked-text)
  8373. (point-max))))
  8374. (throw 'match t))
  8375. (save-excursion
  8376. (when (or (org-in-regexp org-angle-link-re)
  8377. (org-in-regexp org-plain-link-re))
  8378. (setq type (match-string 1) path (match-string 2))
  8379. (throw 'match t)))
  8380. (save-excursion
  8381. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8382. (setq type "tags"
  8383. path (match-string 1))
  8384. (while (string-match ":" path)
  8385. (setq path (replace-match "+" t t path)))
  8386. (throw 'match t)))
  8387. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8388. (setq type "tree-match"
  8389. path (match-string 1))
  8390. (throw 'match t)))
  8391. (unless path
  8392. (error "No link found"))
  8393. ;; switch back to reference buffer
  8394. ;; needed when if called in a temporary buffer through
  8395. ;; org-open-link-from-string
  8396. (with-current-buffer (or reference-buffer (current-buffer))
  8397. ;; Remove any trailing spaces in path
  8398. (if (string-match " +\\'" path)
  8399. (setq path (replace-match "" t t path)))
  8400. (if (and org-link-translation-function
  8401. (fboundp org-link-translation-function))
  8402. ;; Check if we need to translate the link
  8403. (let ((tmp (funcall org-link-translation-function type path)))
  8404. (setq type (car tmp) path (cdr tmp))))
  8405. (cond
  8406. ((assoc type org-link-protocols)
  8407. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8408. ((equal type "mailto")
  8409. (let ((cmd (car org-link-mailto-program))
  8410. (args (cdr org-link-mailto-program)) args1
  8411. (address path) (subject "") a)
  8412. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8413. (setq address (match-string 1 path)
  8414. subject (org-link-escape (match-string 2 path))))
  8415. (while args
  8416. (cond
  8417. ((not (stringp (car args))) (push (pop args) args1))
  8418. (t (setq a (pop args))
  8419. (if (string-match "%a" a)
  8420. (setq a (replace-match address t t a)))
  8421. (if (string-match "%s" a)
  8422. (setq a (replace-match subject t t a)))
  8423. (push a args1))))
  8424. (apply cmd (nreverse args1))))
  8425. ((member type '("http" "https" "ftp" "news"))
  8426. (browse-url (concat type ":" (org-link-escape
  8427. path org-link-escape-chars-browser))))
  8428. ((string= type "doi")
  8429. (browse-url (concat "http://dx.doi.org/"
  8430. (org-link-escape
  8431. path org-link-escape-chars-browser))))
  8432. ((member type '("message"))
  8433. (browse-url (concat type ":" path)))
  8434. ((string= type "tags")
  8435. (org-tags-view arg path))
  8436. ((string= type "tree-match")
  8437. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8438. ((string= type "file")
  8439. (if (string-match "::\\([0-9]+\\)\\'" path)
  8440. (setq line (string-to-number (match-string 1 path))
  8441. path (substring path 0 (match-beginning 0)))
  8442. (if (string-match "::\\(.+\\)\\'" path)
  8443. (setq search (match-string 1 path)
  8444. path (substring path 0 (match-beginning 0)))))
  8445. (if (string-match "[*?{]" (file-name-nondirectory path))
  8446. (dired path)
  8447. (org-open-file path arg line search)))
  8448. ((string= type "shell")
  8449. (let ((cmd path))
  8450. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8451. (string-match org-confirm-shell-link-not-regexp cmd))
  8452. (not org-confirm-shell-link-function)
  8453. (funcall org-confirm-shell-link-function
  8454. (format "Execute \"%s\" in shell? "
  8455. (org-add-props cmd nil
  8456. 'face 'org-warning))))
  8457. (progn
  8458. (message "Executing %s" cmd)
  8459. (shell-command cmd))
  8460. (error "Abort"))))
  8461. ((string= type "elisp")
  8462. (let ((cmd path))
  8463. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8464. (string-match org-confirm-elisp-link-not-regexp cmd))
  8465. (not org-confirm-elisp-link-function)
  8466. (funcall org-confirm-elisp-link-function
  8467. (format "Execute \"%s\" as elisp? "
  8468. (org-add-props cmd nil
  8469. 'face 'org-warning))))
  8470. (message "%s => %s" cmd
  8471. (if (equal (string-to-char cmd) ?\()
  8472. (eval (read cmd))
  8473. (call-interactively (read cmd))))
  8474. (error "Abort"))))
  8475. ((and (string= type "thisfile")
  8476. (run-hook-with-args-until-success
  8477. 'org-open-link-functions path)))
  8478. ((string= type "thisfile")
  8479. (if arg
  8480. (switch-to-buffer-other-window
  8481. (org-get-buffer-for-internal-link (current-buffer)))
  8482. (org-mark-ring-push))
  8483. (let ((cmd `(org-link-search
  8484. ,path
  8485. ,(cond ((equal arg '(4)) ''occur)
  8486. ((equal arg '(16)) ''org-occur)
  8487. (t nil))
  8488. ,pos)))
  8489. (condition-case nil (eval cmd)
  8490. (error (progn (widen) (eval cmd))))))
  8491. (t
  8492. (browse-url-at-point)))))))
  8493. (move-marker org-open-link-marker nil)
  8494. (run-hook-with-args 'org-follow-link-hook)))
  8495. (defun org-offer-links-in-entry (&optional nth zero)
  8496. "Offer links in the current entry and follow the selected link.
  8497. If there is only one link, follow it immediately as well.
  8498. If NTH is an integer, immediately pick the NTH link found.
  8499. If ZERO is a string, check also this string for a link, and if
  8500. there is one, offer it as link number zero."
  8501. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8502. "\\(" org-angle-link-re "\\)\\|"
  8503. "\\(" org-plain-link-re "\\)"))
  8504. (cnt ?0)
  8505. (in-emacs (if (integerp nth) nil nth))
  8506. have-zero end links link c)
  8507. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8508. (push (match-string 0 zero) links)
  8509. (setq cnt (1- cnt) have-zero t))
  8510. (save-excursion
  8511. (org-back-to-heading t)
  8512. (setq end (save-excursion (outline-next-heading) (point)))
  8513. (while (re-search-forward re end t)
  8514. (push (match-string 0) links))
  8515. (setq links (org-uniquify (reverse links))))
  8516. (cond
  8517. ((null links)
  8518. (message "No links"))
  8519. ((equal (length links) 1)
  8520. (setq link (list (car links))))
  8521. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8522. (setq link (nth (if have-zero nth (1- nth)) links)))
  8523. (t ; we have to select a link
  8524. (save-excursion
  8525. (save-window-excursion
  8526. (delete-other-windows)
  8527. (with-output-to-temp-buffer "*Select Link*"
  8528. (mapc (lambda (l)
  8529. (if (not (string-match org-bracket-link-regexp l))
  8530. (princ (format "[%c] %s\n" (incf cnt)
  8531. (org-remove-angle-brackets l)))
  8532. (if (match-end 3)
  8533. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8534. (match-string 3 l) (match-string 1 l)))
  8535. (princ (format "[%c] %s\n" (incf cnt)
  8536. (match-string 1 l))))))
  8537. links))
  8538. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8539. (message "Select link to open, RET to open all:")
  8540. (setq c (read-char-exclusive))
  8541. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8542. (when (equal c ?q) (error "Abort"))
  8543. (if (equal c ?\C-m)
  8544. (setq link links)
  8545. (setq nth (- c ?0))
  8546. (if have-zero (setq nth (1+ nth)))
  8547. (unless (and (integerp nth) (>= (length links) nth))
  8548. (error "Invalid link selection"))
  8549. (setq link (list (nth (1- nth) links))))))
  8550. (if link
  8551. (let ((buf (current-buffer)))
  8552. (dolist (l link)
  8553. (org-open-link-from-string l in-emacs buf))
  8554. t)
  8555. nil)))
  8556. ;; Add special file links that specify the way of opening
  8557. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8558. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8559. (defun org-open-file-with-system (path)
  8560. "Open file at PATH using the system way of opening it."
  8561. (org-open-file path 'system))
  8562. (defun org-open-file-with-emacs (path)
  8563. "Open file at PATH in Emacs."
  8564. (org-open-file path 'emacs))
  8565. (defun org-remove-file-link-modifiers ()
  8566. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8567. (goto-char (point-min))
  8568. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8569. (org-if-unprotected
  8570. (replace-match "file:" t t))))
  8571. (eval-after-load "org-exp"
  8572. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8573. 'org-remove-file-link-modifiers))
  8574. ;;;; Time estimates
  8575. (defun org-get-effort (&optional pom)
  8576. "Get the effort estimate for the current entry."
  8577. (org-entry-get pom org-effort-property))
  8578. ;;; File search
  8579. (defvar org-create-file-search-functions nil
  8580. "List of functions to construct the right search string for a file link.
  8581. These functions are called in turn with point at the location to
  8582. which the link should point.
  8583. A function in the hook should first test if it would like to
  8584. handle this file type, for example by checking the `major-mode'
  8585. or the file extension. If it decides not to handle this file, it
  8586. should just return nil to give other functions a chance. If it
  8587. does handle the file, it must return the search string to be used
  8588. when following the link. The search string will be part of the
  8589. file link, given after a double colon, and `org-open-at-point'
  8590. will automatically search for it. If special measures must be
  8591. taken to make the search successful, another function should be
  8592. added to the companion hook `org-execute-file-search-functions',
  8593. which see.
  8594. A function in this hook may also use `setq' to set the variable
  8595. `description' to provide a suggestion for the descriptive text to
  8596. be used for this link when it gets inserted into an Org-mode
  8597. buffer with \\[org-insert-link].")
  8598. (defvar org-execute-file-search-functions nil
  8599. "List of functions to execute a file search triggered by a link.
  8600. Functions added to this hook must accept a single argument, the
  8601. search string that was part of the file link, the part after the
  8602. double colon. The function must first check if it would like to
  8603. handle this search, for example by checking the `major-mode' or
  8604. the file extension. If it decides not to handle this search, it
  8605. should just return nil to give other functions a chance. If it
  8606. does handle the search, it must return a non-nil value to keep
  8607. other functions from trying.
  8608. Each function can access the current prefix argument through the
  8609. variable `current-prefix-argument'. Note that a single prefix is
  8610. used to force opening a link in Emacs, so it may be good to only
  8611. use a numeric or double prefix to guide the search function.
  8612. In case this is needed, a function in this hook can also restore
  8613. the window configuration before `org-open-at-point' was called using:
  8614. (set-window-configuration org-window-config-before-follow-link)")
  8615. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8616. (defun org-link-search (s &optional type avoid-pos)
  8617. "Search for a link search option.
  8618. If S is surrounded by forward slashes, it is interpreted as a
  8619. regular expression. In org-mode files, this will create an `org-occur'
  8620. sparse tree. In ordinary files, `occur' will be used to list matches.
  8621. If the current buffer is in `dired-mode', grep will be used to search
  8622. in all files. If AVOID-POS is given, ignore matches near that position."
  8623. (let ((case-fold-search t)
  8624. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8625. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8626. (append '(("") (" ") ("\t") ("\n"))
  8627. org-emphasis-alist)
  8628. "\\|") "\\)"))
  8629. (pos (point))
  8630. (pre nil) (post nil)
  8631. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8632. (cond
  8633. ;; First check if there are any special search functions
  8634. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8635. ;; Now try the builtin stuff
  8636. ((and (equal (string-to-char s0) ?#)
  8637. (> (length s0) 1)
  8638. (save-excursion
  8639. (goto-char (point-min))
  8640. (and
  8641. (re-search-forward
  8642. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8643. (setq type 'dedicated
  8644. pos (match-beginning 0))))
  8645. ;; There is an exact target for this
  8646. (goto-char pos)
  8647. (org-back-to-heading t)))
  8648. ((save-excursion
  8649. (goto-char (point-min))
  8650. (and
  8651. (re-search-forward
  8652. (concat "<<" (regexp-quote s0) ">>") nil t)
  8653. (setq type 'dedicated
  8654. pos (match-beginning 0))))
  8655. ;; There is an exact target for this
  8656. (goto-char pos))
  8657. ((and (string-match "^(\\(.*\\))$" s0)
  8658. (save-excursion
  8659. (goto-char (point-min))
  8660. (and
  8661. (re-search-forward
  8662. (concat "[^[]" (regexp-quote
  8663. (format org-coderef-label-format
  8664. (match-string 1 s0))))
  8665. nil t)
  8666. (setq type 'dedicated
  8667. pos (1+ (match-beginning 0))))))
  8668. ;; There is a coderef target for this
  8669. (goto-char pos))
  8670. ((string-match "^/\\(.*\\)/$" s)
  8671. ;; A regular expression
  8672. (cond
  8673. ((org-mode-p)
  8674. (org-occur (match-string 1 s)))
  8675. ;;((eq major-mode 'dired-mode)
  8676. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8677. (t (org-do-occur (match-string 1 s)))))
  8678. ((and (org-mode-p) org-link-search-must-match-exact-headline)
  8679. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8680. (goto-char (point-min))
  8681. (cond
  8682. ((let (case-fold-search)
  8683. (re-search-forward (format org-complex-heading-regexp-format
  8684. (regexp-quote s))
  8685. nil t))
  8686. ;; OK, found a match
  8687. (setq type 'dedicated)
  8688. (goto-char (match-beginning 0)))
  8689. ((and (not org-link-search-inhibit-query)
  8690. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8691. (y-or-n-p "No match - create this as a new heading? "))
  8692. (goto-char (point-max))
  8693. (or (bolp) (newline))
  8694. (insert "* " s "\n")
  8695. (beginning-of-line 0))
  8696. (t
  8697. (goto-char pos)
  8698. (error "No match"))))
  8699. (t
  8700. ;; A normal search string
  8701. (when (equal (string-to-char s) ?*)
  8702. ;; Anchor on headlines, post may include tags.
  8703. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8704. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8705. s (substring s 1)))
  8706. (remove-text-properties
  8707. 0 (length s)
  8708. '(face nil mouse-face nil keymap nil fontified nil) s)
  8709. ;; Make a series of regular expressions to find a match
  8710. (setq words (org-split-string s "[ \n\r\t]+")
  8711. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8712. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8713. "\\)" markers)
  8714. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8715. re2a (concat "[ \t\r\n]" re2a_)
  8716. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8717. re4 (concat "[^a-zA-Z_]" re4_)
  8718. re1 (concat pre re2 post)
  8719. re3 (concat pre (if pre re4_ re4) post)
  8720. re5 (concat pre ".*" re4)
  8721. re2 (concat pre re2)
  8722. re2a (concat pre (if pre re2a_ re2a))
  8723. re4 (concat pre (if pre re4_ re4))
  8724. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8725. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8726. re5 "\\)"
  8727. ))
  8728. (cond
  8729. ((eq type 'org-occur) (org-occur reall))
  8730. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8731. (t (goto-char (point-min))
  8732. (setq type 'fuzzy)
  8733. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8734. (org-search-not-self 1 re1 nil t)
  8735. (org-search-not-self 1 re2 nil t)
  8736. (org-search-not-self 1 re2a nil t)
  8737. (org-search-not-self 1 re3 nil t)
  8738. (org-search-not-self 1 re4 nil t)
  8739. (org-search-not-self 1 re5 nil t)
  8740. )
  8741. (goto-char (match-beginning 1))
  8742. (goto-char pos)
  8743. (error "No match"))))))
  8744. (and (org-mode-p) (org-show-context 'link-search))
  8745. type))
  8746. (defun org-search-not-self (group &rest args)
  8747. "Execute `re-search-forward', but only accept matches that do not
  8748. enclose the position of `org-open-link-marker'."
  8749. (let ((m org-open-link-marker))
  8750. (catch 'exit
  8751. (while (apply 're-search-forward args)
  8752. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8753. (goto-char (match-end group))
  8754. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8755. (> (match-beginning 0) (marker-position m))
  8756. (< (match-end 0) (marker-position m)))
  8757. (save-match-data
  8758. (or (not (org-in-regexp
  8759. org-bracket-link-analytic-regexp 1))
  8760. (not (match-end 4)) ; no description
  8761. (and (<= (match-beginning 4) (point))
  8762. (>= (match-end 4) (point))))))
  8763. (throw 'exit (point))))))))
  8764. (defun org-get-buffer-for-internal-link (buffer)
  8765. "Return a buffer to be used for displaying the link target of internal links."
  8766. (cond
  8767. ((not org-display-internal-link-with-indirect-buffer)
  8768. buffer)
  8769. ((string-match "(Clone)$" (buffer-name buffer))
  8770. (message "Buffer is already a clone, not making another one")
  8771. ;; we also do not modify visibility in this case
  8772. buffer)
  8773. (t ; make a new indirect buffer for displaying the link
  8774. (let* ((bn (buffer-name buffer))
  8775. (ibn (concat bn "(Clone)"))
  8776. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8777. (with-current-buffer ib (org-overview))
  8778. ib))))
  8779. (defun org-do-occur (regexp &optional cleanup)
  8780. "Call the Emacs command `occur'.
  8781. If CLEANUP is non-nil, remove the printout of the regular expression
  8782. in the *Occur* buffer. This is useful if the regex is long and not useful
  8783. to read."
  8784. (occur regexp)
  8785. (when cleanup
  8786. (let ((cwin (selected-window)) win beg end)
  8787. (when (setq win (get-buffer-window "*Occur*"))
  8788. (select-window win))
  8789. (goto-char (point-min))
  8790. (when (re-search-forward "match[a-z]+" nil t)
  8791. (setq beg (match-end 0))
  8792. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8793. (setq end (1- (match-beginning 0)))))
  8794. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8795. (goto-char (point-min))
  8796. (select-window cwin))))
  8797. ;;; The mark ring for links jumps
  8798. (defvar org-mark-ring nil
  8799. "Mark ring for positions before jumps in Org-mode.")
  8800. (defvar org-mark-ring-last-goto nil
  8801. "Last position in the mark ring used to go back.")
  8802. ;; Fill and close the ring
  8803. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8804. (loop for i from 1 to org-mark-ring-length do
  8805. (push (make-marker) org-mark-ring))
  8806. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8807. org-mark-ring)
  8808. (defun org-mark-ring-push (&optional pos buffer)
  8809. "Put the current position or POS into the mark ring and rotate it."
  8810. (interactive)
  8811. (setq pos (or pos (point)))
  8812. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8813. (move-marker (car org-mark-ring)
  8814. (or pos (point))
  8815. (or buffer (current-buffer)))
  8816. (message "%s"
  8817. (substitute-command-keys
  8818. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8819. (defun org-mark-ring-goto (&optional n)
  8820. "Jump to the previous position in the mark ring.
  8821. With prefix arg N, jump back that many stored positions. When
  8822. called several times in succession, walk through the entire ring.
  8823. Org-mode commands jumping to a different position in the current file,
  8824. or to another Org-mode file, automatically push the old position
  8825. onto the ring."
  8826. (interactive "p")
  8827. (let (p m)
  8828. (if (eq last-command this-command)
  8829. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8830. (setq p org-mark-ring))
  8831. (setq org-mark-ring-last-goto p)
  8832. (setq m (car p))
  8833. (switch-to-buffer (marker-buffer m))
  8834. (goto-char m)
  8835. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8836. (defun org-remove-angle-brackets (s)
  8837. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8838. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8839. s)
  8840. (defun org-add-angle-brackets (s)
  8841. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8842. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8843. s)
  8844. (defun org-remove-double-quotes (s)
  8845. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8846. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8847. s)
  8848. ;;; Following specific links
  8849. (defun org-follow-timestamp-link ()
  8850. (cond
  8851. ((org-at-date-range-p t)
  8852. (let ((org-agenda-start-on-weekday)
  8853. (t1 (match-string 1))
  8854. (t2 (match-string 2)))
  8855. (setq t1 (time-to-days (org-time-string-to-time t1))
  8856. t2 (time-to-days (org-time-string-to-time t2)))
  8857. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8858. ((org-at-timestamp-p t)
  8859. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8860. (substring (match-string 1) 0 10)))
  8861. 1))
  8862. (t (error "This should not happen"))))
  8863. ;;; Following file links
  8864. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  8865. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  8866. (declare-function mailcap-mime-info
  8867. "mailcap" (string &optional request no-decode))
  8868. (defvar org-wait nil)
  8869. (defun org-open-file (path &optional in-emacs line search)
  8870. "Open the file at PATH.
  8871. First, this expands any special file name abbreviations. Then the
  8872. configuration variable `org-file-apps' is checked if it contains an
  8873. entry for this file type, and if yes, the corresponding command is launched.
  8874. If no application is found, Emacs simply visits the file.
  8875. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8876. With a double \\[universal-argument] \\[universal-argument] \
  8877. prefix arg, Org tries to avoid opening in Emacs
  8878. and to use an external application to visit the file.
  8879. Optional LINE specifies a line to go to, optional SEARCH a string
  8880. to search for. If LINE or SEARCH is given, the file will be
  8881. opened in Emacs, unless an entry from org-file-apps that makes
  8882. use of groups in a regexp matches.
  8883. If the file does not exist, an error is thrown."
  8884. (let* ((file (if (equal path "")
  8885. buffer-file-name
  8886. (substitute-in-file-name (expand-file-name path))))
  8887. (file-apps (append org-file-apps (org-default-apps)))
  8888. (apps (org-remove-if
  8889. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8890. (apps-dlink (org-remove-if-not
  8891. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8892. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8893. (dirp (if remp nil (file-directory-p file)))
  8894. (file (if (and dirp org-open-directory-means-index-dot-org)
  8895. (concat (file-name-as-directory file) "index.org")
  8896. file))
  8897. (a-m-a-p (assq 'auto-mode apps))
  8898. (dfile (downcase file))
  8899. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8900. (link (cond ((and (eq line nil)
  8901. (eq search nil))
  8902. file)
  8903. (line
  8904. (concat file "::" (number-to-string line)))
  8905. (search
  8906. (concat file "::" search))))
  8907. (dlink (downcase link))
  8908. (old-buffer (current-buffer))
  8909. (old-pos (point))
  8910. (old-mode major-mode)
  8911. ext cmd link-match-data)
  8912. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8913. (setq ext (match-string 1 dfile))
  8914. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8915. (setq ext (match-string 1 dfile))))
  8916. (cond
  8917. ((member in-emacs '((16) system))
  8918. (setq cmd (cdr (assoc 'system apps))))
  8919. (in-emacs (setq cmd 'emacs))
  8920. (t
  8921. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8922. (and dirp (cdr (assoc 'directory apps)))
  8923. ; first, try matching against apps-dlink
  8924. ; if we get a match here, store the match data for later
  8925. (let ((match (assoc-default dlink apps-dlink
  8926. 'string-match)))
  8927. (if match
  8928. (progn (setq link-match-data (match-data))
  8929. match)
  8930. (progn (setq in-emacs (or in-emacs line search))
  8931. nil))) ; if we have no match in apps-dlink,
  8932. ; always open the file in emacs if line or search
  8933. ; is given (for backwards compatibility)
  8934. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8935. 'string-match)
  8936. (cdr (assoc ext apps))
  8937. (cdr (assoc t apps))))))
  8938. (when (eq cmd 'system)
  8939. (setq cmd (cdr (assoc 'system apps))))
  8940. (when (eq cmd 'default)
  8941. (setq cmd (cdr (assoc t apps))))
  8942. (when (eq cmd 'mailcap)
  8943. (require 'mailcap)
  8944. (mailcap-parse-mailcaps)
  8945. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8946. (command (mailcap-mime-info mime-type)))
  8947. (if (stringp command)
  8948. (setq cmd command)
  8949. (setq cmd 'emacs))))
  8950. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8951. (not (file-exists-p file))
  8952. (not org-open-non-existing-files))
  8953. (error "No such file: %s" file))
  8954. (cond
  8955. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8956. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8957. (while (string-match "['\"]%s['\"]" cmd)
  8958. (setq cmd (replace-match "%s" t t cmd)))
  8959. (while (string-match "%s" cmd)
  8960. (setq cmd (replace-match
  8961. (save-match-data
  8962. (shell-quote-argument
  8963. (convert-standard-filename file)))
  8964. t t cmd)))
  8965. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8966. (save-match-data
  8967. (let ((match-index 1)
  8968. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8969. (set-match-data link-match-data)
  8970. (while (<= match-index number-of-groups)
  8971. (let ((regex (concat "%" (number-to-string match-index)))
  8972. (replace-with (match-string match-index dlink)))
  8973. (while (string-match regex cmd)
  8974. (setq cmd (replace-match replace-with t t cmd))))
  8975. (setq match-index (+ match-index 1)))))
  8976. (save-window-excursion
  8977. (start-process-shell-command cmd nil cmd)
  8978. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8979. ))
  8980. ((or (stringp cmd)
  8981. (eq cmd 'emacs))
  8982. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8983. (widen)
  8984. (if line (org-goto-line line)
  8985. (if search (org-link-search search))))
  8986. ((consp cmd)
  8987. (let ((file (convert-standard-filename file)))
  8988. (save-match-data
  8989. (set-match-data link-match-data)
  8990. (eval cmd))))
  8991. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8992. (and (org-mode-p) (eq old-mode 'org-mode)
  8993. (or (not (equal old-buffer (current-buffer)))
  8994. (not (equal old-pos (point))))
  8995. (org-mark-ring-push old-pos old-buffer))))
  8996. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8997. "This function returns non-nil if `entry' uses a regular
  8998. expression which should be matched against the whole link by
  8999. org-open-file.
  9000. It assumes that is the case when the entry uses a regular
  9001. expression which has at least one grouping construct and the
  9002. action is either a lisp form or a command string containing
  9003. '%1', i.e. using at least one subexpression match as a
  9004. parameter."
  9005. (let ((selector (car entry))
  9006. (action (cdr entry)))
  9007. (if (stringp selector)
  9008. (and (> (regexp-opt-depth selector) 0)
  9009. (or (and (stringp action)
  9010. (string-match "%[0-9]" action))
  9011. (consp action)))
  9012. nil)))
  9013. (defun org-default-apps ()
  9014. "Return the default applications for this operating system."
  9015. (cond
  9016. ((eq system-type 'darwin)
  9017. org-file-apps-defaults-macosx)
  9018. ((eq system-type 'windows-nt)
  9019. org-file-apps-defaults-windowsnt)
  9020. (t org-file-apps-defaults-gnu)))
  9021. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9022. "Convert extensions to regular expressions in the cars of LIST.
  9023. Also, weed out any non-string entries, because the return value is used
  9024. only for regexp matching.
  9025. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9026. point to the symbol `emacs', indicating that the file should
  9027. be opened in Emacs."
  9028. (append
  9029. (delq nil
  9030. (mapcar (lambda (x)
  9031. (if (not (stringp (car x)))
  9032. nil
  9033. (if (string-match "\\W" (car x))
  9034. x
  9035. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9036. list))
  9037. (if add-auto-mode
  9038. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9039. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9040. (defun org-file-remote-p (file)
  9041. "Test whether FILE specifies a location on a remote system.
  9042. Return non-nil if the location is indeed remote.
  9043. For example, the filename \"/user@host:/foo\" specifies a location
  9044. on the system \"/user@host:\"."
  9045. (cond ((fboundp 'file-remote-p)
  9046. (file-remote-p file))
  9047. ((fboundp 'tramp-handle-file-remote-p)
  9048. (tramp-handle-file-remote-p file))
  9049. ((and (boundp 'ange-ftp-name-format)
  9050. (string-match (car ange-ftp-name-format) file))
  9051. t)
  9052. (t nil)))
  9053. ;;;; Refiling
  9054. (defun org-get-org-file ()
  9055. "Read a filename, with default directory `org-directory'."
  9056. (let ((default (or org-default-notes-file remember-data-file)))
  9057. (read-file-name (format "File name [%s]: " default)
  9058. (file-name-as-directory org-directory)
  9059. default)))
  9060. (defun org-notes-order-reversed-p ()
  9061. "Check if the current file should receive notes in reversed order."
  9062. (cond
  9063. ((not org-reverse-note-order) nil)
  9064. ((eq t org-reverse-note-order) t)
  9065. ((not (listp org-reverse-note-order)) nil)
  9066. (t (catch 'exit
  9067. (let ((all org-reverse-note-order)
  9068. entry)
  9069. (while (setq entry (pop all))
  9070. (if (string-match (car entry) buffer-file-name)
  9071. (throw 'exit (cdr entry))))
  9072. nil)))))
  9073. (defvar org-refile-target-table nil
  9074. "The list of refile targets, created by `org-refile'.")
  9075. (defvar org-agenda-new-buffers nil
  9076. "Buffers created to visit agenda files.")
  9077. (defvar org-refile-cache nil
  9078. "Cache for refile targets.")
  9079. (defvar org-refile-markers nil
  9080. "All the markers used for caching refile locations.")
  9081. (defun org-refile-marker (pos)
  9082. "Get a new refile marker, but only if caching is in use."
  9083. (if (not org-refile-use-cache)
  9084. pos
  9085. (let ((m (make-marker)))
  9086. (move-marker m pos)
  9087. (push m org-refile-markers)
  9088. m)))
  9089. (defun org-refile-cache-clear ()
  9090. "Clear the refile cache and disable all the markers."
  9091. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9092. (setq org-refile-markers nil)
  9093. (setq org-refile-cache nil)
  9094. (message "Refile cache has been cleared"))
  9095. (defun org-refile-cache-check-set (set)
  9096. "Check if all the markers in the cache still have live buffers."
  9097. (let (marker)
  9098. (catch 'exit
  9099. (while (and set (setq marker (nth 3 (pop set))))
  9100. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9101. (when (and marker (null (marker-buffer marker)))
  9102. (message "not found") (sit-for 3)
  9103. (throw 'exit nil)))
  9104. t)))
  9105. (defun org-refile-cache-put (set &rest identifiers)
  9106. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9107. (let* ((key (sha1 (prin1-to-string identifiers)))
  9108. (entry (assoc key org-refile-cache)))
  9109. (if entry
  9110. (setcdr entry set)
  9111. (push (cons key set) org-refile-cache))))
  9112. (defun org-refile-cache-get (&rest identifiers)
  9113. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9114. (cond
  9115. ((not org-refile-cache) nil)
  9116. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9117. (t
  9118. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9119. org-refile-cache))))
  9120. (and set (org-refile-cache-check-set set) set)))))
  9121. (defun org-refile-get-targets (&optional default-buffer)
  9122. "Produce a table with refile targets."
  9123. (let ((case-fold-search nil)
  9124. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9125. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9126. targets tgs txt re files f desc descre fast-path-p level pos0)
  9127. (message "Getting targets...")
  9128. (with-current-buffer (or default-buffer (current-buffer))
  9129. (while (setq entry (pop entries))
  9130. (setq files (car entry) desc (cdr entry))
  9131. (setq fast-path-p nil)
  9132. (cond
  9133. ((null files) (setq files (list (current-buffer))))
  9134. ((eq files 'org-agenda-files)
  9135. (setq files (org-agenda-files 'unrestricted)))
  9136. ((and (symbolp files) (fboundp files))
  9137. (setq files (funcall files)))
  9138. ((and (symbolp files) (boundp files))
  9139. (setq files (symbol-value files))))
  9140. (if (stringp files) (setq files (list files)))
  9141. (cond
  9142. ((eq (car desc) :tag)
  9143. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9144. ((eq (car desc) :todo)
  9145. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9146. ((eq (car desc) :regexp)
  9147. (setq descre (cdr desc)))
  9148. ((eq (car desc) :level)
  9149. (setq descre (concat "^\\*\\{" (number-to-string
  9150. (if org-odd-levels-only
  9151. (1- (* 2 (cdr desc)))
  9152. (cdr desc)))
  9153. "\\}[ \t]")))
  9154. ((eq (car desc) :maxlevel)
  9155. (setq fast-path-p t)
  9156. (setq descre (concat "^\\*\\{1," (number-to-string
  9157. (if org-odd-levels-only
  9158. (1- (* 2 (cdr desc)))
  9159. (cdr desc)))
  9160. "\\}[ \t]")))
  9161. (t (error "Bad refiling target description %s" desc)))
  9162. (while (setq f (pop files))
  9163. (with-current-buffer
  9164. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9165. (or
  9166. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9167. (progn
  9168. (if (bufferp f) (setq f (buffer-file-name
  9169. (buffer-base-buffer f))))
  9170. (setq f (and f (expand-file-name f)))
  9171. (if (eq org-refile-use-outline-path 'file)
  9172. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9173. (save-excursion
  9174. (save-restriction
  9175. (widen)
  9176. (goto-char (point-min))
  9177. (while (re-search-forward descre nil t)
  9178. (goto-char (setq pos0 (point-at-bol)))
  9179. (catch 'next
  9180. (when org-refile-target-verify-function
  9181. (save-match-data
  9182. (or (funcall org-refile-target-verify-function)
  9183. (throw 'next t))))
  9184. (when (looking-at org-complex-heading-regexp)
  9185. (setq level (org-reduced-level
  9186. (- (match-end 1) (match-beginning 1)))
  9187. txt (org-link-display-format (match-string 4))
  9188. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9189. re (format org-complex-heading-regexp-format
  9190. (regexp-quote (match-string 4))))
  9191. (when org-refile-use-outline-path
  9192. (setq txt (mapconcat
  9193. 'org-protect-slash
  9194. (append
  9195. (if (eq org-refile-use-outline-path
  9196. 'file)
  9197. (list (file-name-nondirectory
  9198. (buffer-file-name
  9199. (buffer-base-buffer))))
  9200. (if (eq org-refile-use-outline-path
  9201. 'full-file-path)
  9202. (list (buffer-file-name
  9203. (buffer-base-buffer)))))
  9204. (org-get-outline-path fast-path-p
  9205. level txt)
  9206. (list txt))
  9207. "/")))
  9208. (push (list txt f re (org-refile-marker (point)))
  9209. tgs)))
  9210. (when (= (point) pos0)
  9211. ;; verification function has not moved point
  9212. (goto-char (point-at-eol))))))))
  9213. (when org-refile-use-cache
  9214. (org-refile-cache-put tgs (buffer-file-name) descre))
  9215. (setq targets (append tgs targets))
  9216. ))))
  9217. (message "Getting targets...done")
  9218. (nreverse targets)))
  9219. (defun org-protect-slash (s)
  9220. (while (string-match "/" s)
  9221. (setq s (replace-match "\\" t t s)))
  9222. s)
  9223. (defvar org-olpa (make-vector 20 nil))
  9224. (defun org-get-outline-path (&optional fastp level heading)
  9225. "Return the outline path to the current entry, as a list.
  9226. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9227. routine which makes outline path derivations for an entire file,
  9228. avoiding backtracing. Refile target collection makes use of that."
  9229. (if fastp
  9230. (progn
  9231. (if (> level 19)
  9232. (error "Outline path failure, more than 19 levels"))
  9233. (loop for i from level upto 19 do
  9234. (aset org-olpa i nil))
  9235. (prog1
  9236. (delq nil (append org-olpa nil))
  9237. (aset org-olpa level heading)))
  9238. (let (rtn case-fold-search)
  9239. (save-excursion
  9240. (save-restriction
  9241. (widen)
  9242. (while (org-up-heading-safe)
  9243. (when (looking-at org-complex-heading-regexp)
  9244. (push (org-match-string-no-properties 4) rtn)))
  9245. rtn)))))
  9246. (defun org-format-outline-path (path &optional width prefix)
  9247. "Format the outline path PATH for display.
  9248. Width is the maximum number of characters that is available.
  9249. Prefix is a prefix to be included in the returned string,
  9250. such as the file name."
  9251. (setq width (or width 79))
  9252. (if prefix (setq width (- width (length prefix))))
  9253. (if (not path)
  9254. (or prefix "")
  9255. (let* ((nsteps (length path))
  9256. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9257. (maxwidth (if (<= total-width width)
  9258. 10000 ;; everything fits
  9259. ;; we need to shorten the level headings
  9260. (/ (- width nsteps) nsteps)))
  9261. (org-odd-levels-only nil)
  9262. (n 0)
  9263. (total (1+ (length prefix))))
  9264. (setq maxwidth (max maxwidth 10))
  9265. (concat prefix
  9266. (mapconcat
  9267. (lambda (h)
  9268. (setq n (1+ n))
  9269. (if (and (= n nsteps) (< maxwidth 10000))
  9270. (setq maxwidth (- total-width total)))
  9271. (if (< (length h) maxwidth)
  9272. (progn (setq total (+ total (length h) 1)) h)
  9273. (setq h (substring h 0 (- maxwidth 2))
  9274. total (+ total maxwidth 1))
  9275. (if (string-match "[ \t]+\\'" h)
  9276. (setq h (substring h 0 (match-beginning 0))))
  9277. (setq h (concat h "..")))
  9278. (org-add-props h nil 'face
  9279. (nth (% (1- n) org-n-level-faces)
  9280. org-level-faces))
  9281. h)
  9282. path "/")))))
  9283. (defun org-display-outline-path (&optional file current)
  9284. "Display the current outline path in the echo area."
  9285. (interactive "P")
  9286. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9287. (case-fold-search nil)
  9288. (path (and (org-mode-p) (org-get-outline-path))))
  9289. (if current (setq path (append path
  9290. (save-excursion
  9291. (org-back-to-heading t)
  9292. (if (looking-at org-complex-heading-regexp)
  9293. (list (match-string 4)))))))
  9294. (message "%s"
  9295. (org-format-outline-path
  9296. path
  9297. (1- (frame-width))
  9298. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9299. (defvar org-refile-history nil
  9300. "History for refiling operations.")
  9301. (defvar org-after-refile-insert-hook nil
  9302. "Hook run after `org-refile' has inserted its stuff at the new location.
  9303. Note that this is still *before* the stuff will be removed from
  9304. the *old* location.")
  9305. (defvar org-capture-last-stored-marker)
  9306. (defun org-refile (&optional goto default-buffer rfloc)
  9307. "Move the entry at point to another heading.
  9308. The list of target headings is compiled using the information in
  9309. `org-refile-targets', which see. This list is created before each use
  9310. and will therefore always be up-to-date.
  9311. At the target location, the entry is filed as a subitem of the target heading.
  9312. Depending on `org-reverse-note-order', the new subitem will either be the
  9313. first or the last subitem.
  9314. If there is an active region, all entries in that region will be moved.
  9315. However, the region must fulfill the requirement that the first heading
  9316. is the first one sets the top-level of the moved text - at most siblings
  9317. below it are allowed.
  9318. With prefix arg GOTO, the command will only visit the target location,
  9319. not actually move anything.
  9320. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9321. go to the location where the last refiling
  9322. operation has put the subtree.
  9323. With a prefix argument of `2', refile to the running clock.
  9324. RFLOC can be a refile location obtained in a different way.
  9325. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9326. If you are using target caching (see `org-refile-use-cache'),
  9327. You have to clear the target cache in order to find new targets.
  9328. This can be done with a 0 prefix: `C-0 C-c C-w'"
  9329. (interactive "P")
  9330. (if (member goto '(0 (64)))
  9331. (org-refile-cache-clear)
  9332. (let* ((cbuf (current-buffer))
  9333. (regionp (org-region-active-p))
  9334. (region-start (and regionp (region-beginning)))
  9335. (region-end (and regionp (region-end)))
  9336. (region-length (and regionp (- region-end region-start)))
  9337. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9338. pos it nbuf file re level reversed)
  9339. (setq last-command nil)
  9340. (when regionp
  9341. (goto-char region-start)
  9342. (or (bolp) (goto-char (point-at-bol)))
  9343. (setq region-start (point))
  9344. (unless (org-kill-is-subtree-p
  9345. (buffer-substring region-start region-end))
  9346. (error "The region is not a (sequence of) subtree(s)")))
  9347. (if (equal goto '(16))
  9348. (org-refile-goto-last-stored)
  9349. (when (or
  9350. (and (equal goto 2)
  9351. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9352. (prog1
  9353. (setq it (list (or org-clock-heading "running clock")
  9354. (buffer-file-name
  9355. (marker-buffer org-clock-hd-marker))
  9356. ""
  9357. (marker-position org-clock-hd-marker)))
  9358. (setq goto nil)))
  9359. (setq it (or rfloc
  9360. (save-excursion
  9361. (org-refile-get-location
  9362. (if goto "Goto" "Refile to") default-buffer
  9363. org-refile-allow-creating-parent-nodes)))))
  9364. (setq file (nth 1 it)
  9365. re (nth 2 it)
  9366. pos (nth 3 it))
  9367. (if (and (not goto)
  9368. pos
  9369. (equal (buffer-file-name) file)
  9370. (if regionp
  9371. (and (>= pos region-start)
  9372. (<= pos region-end))
  9373. (and (>= pos (point))
  9374. (< pos (save-excursion
  9375. (org-end-of-subtree t t))))))
  9376. (error "Cannot refile to position inside the tree or region"))
  9377. (setq nbuf (or (find-buffer-visiting file)
  9378. (find-file-noselect file)))
  9379. (if goto
  9380. (progn
  9381. (switch-to-buffer nbuf)
  9382. (goto-char pos)
  9383. (org-show-context 'org-goto))
  9384. (if regionp
  9385. (progn
  9386. (org-kill-new (buffer-substring region-start region-end))
  9387. (org-save-markers-in-region region-start region-end))
  9388. (org-copy-subtree 1 nil t))
  9389. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9390. (find-file-noselect file)))
  9391. (setq reversed (org-notes-order-reversed-p))
  9392. (save-excursion
  9393. (save-restriction
  9394. (widen)
  9395. (if pos
  9396. (progn
  9397. (goto-char pos)
  9398. (looking-at outline-regexp)
  9399. (setq level (org-get-valid-level (funcall outline-level) 1))
  9400. (goto-char
  9401. (if reversed
  9402. (or (outline-next-heading) (point-max))
  9403. (or (save-excursion (org-get-next-sibling))
  9404. (org-end-of-subtree t t)
  9405. (point-max)))))
  9406. (setq level 1)
  9407. (if (not reversed)
  9408. (goto-char (point-max))
  9409. (goto-char (point-min))
  9410. (or (outline-next-heading) (goto-char (point-max)))))
  9411. (if (not (bolp)) (newline))
  9412. (org-paste-subtree level)
  9413. (when org-log-refile
  9414. (org-add-log-setup 'refile nil nil 'findpos
  9415. org-log-refile)
  9416. (unless (eq org-log-refile 'note)
  9417. (save-excursion (org-add-log-note))))
  9418. (and org-auto-align-tags (org-set-tags nil t))
  9419. (bookmark-set "org-refile-last-stored")
  9420. ;; If we are refiling for capture, make sure that the
  9421. ;; last-capture pointers point here
  9422. (when (org-bound-and-true-p org-refile-for-capture)
  9423. (bookmark-set "org-capture-last-stored-marker")
  9424. (move-marker org-capture-last-stored-marker (point)))
  9425. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9426. (run-hooks 'org-after-refile-insert-hook))))
  9427. (if regionp
  9428. (delete-region (point) (+ (point) region-length))
  9429. (org-cut-subtree))
  9430. (when (featurep 'org-inlinetask)
  9431. (org-inlinetask-remove-END-maybe))
  9432. (setq org-markers-to-move nil)
  9433. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9434. (defun org-refile-goto-last-stored ()
  9435. "Go to the location where the last refile was stored."
  9436. (interactive)
  9437. (bookmark-jump "org-refile-last-stored")
  9438. (message "This is the location of the last refile"))
  9439. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  9440. "Prompt the user for a refile location, using PROMPT.
  9441. PROMPT should not be suffixed with a colon and a space, because
  9442. this function appends the default value from
  9443. `org-refile-history' automatically, if that is not empty."
  9444. (let ((org-refile-targets org-refile-targets)
  9445. (org-refile-use-outline-path org-refile-use-outline-path))
  9446. (setq org-refile-target-table (org-refile-get-targets default-buffer)))
  9447. (unless org-refile-target-table
  9448. (error "No refile targets"))
  9449. (let* ((prompt (concat prompt
  9450. (and (car org-refile-history)
  9451. (concat " (default " (car org-refile-history) ")"))
  9452. ": "))
  9453. (cbuf (current-buffer))
  9454. (partial-completion-mode nil)
  9455. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9456. (cfunc (if (and org-refile-use-outline-path
  9457. org-outline-path-complete-in-steps)
  9458. 'org-olpath-completing-read
  9459. 'org-icompleting-read))
  9460. (extra (if org-refile-use-outline-path "/" ""))
  9461. (filename (and cfn (expand-file-name cfn)))
  9462. (tbl (mapcar
  9463. (lambda (x)
  9464. (if (and (not (member org-refile-use-outline-path
  9465. '(file full-file-path)))
  9466. (not (equal filename (nth 1 x))))
  9467. (cons (concat (car x) extra " ("
  9468. (file-name-nondirectory (nth 1 x)) ")")
  9469. (cdr x))
  9470. (cons (concat (car x) extra) (cdr x))))
  9471. org-refile-target-table))
  9472. (completion-ignore-case t)
  9473. pa answ parent-target child parent old-hist)
  9474. (setq old-hist org-refile-history)
  9475. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9476. nil 'org-refile-history (car org-refile-history)))
  9477. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9478. (org-refile-check-position pa)
  9479. (if pa
  9480. (progn
  9481. (when (or (not org-refile-history)
  9482. (not (eq old-hist org-refile-history))
  9483. (not (equal (car pa) (car org-refile-history))))
  9484. (setq org-refile-history
  9485. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9486. org-refile-history
  9487. (cdr org-refile-history))))
  9488. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9489. (pop org-refile-history)))
  9490. pa)
  9491. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9492. (progn
  9493. (setq parent (match-string 1 answ)
  9494. child (match-string 2 answ))
  9495. (setq parent-target (or (assoc parent tbl)
  9496. (assoc (concat parent "/") tbl)))
  9497. (when (and parent-target
  9498. (or (eq new-nodes t)
  9499. (and (eq new-nodes 'confirm)
  9500. (y-or-n-p (format "Create new node \"%s\"? "
  9501. child)))))
  9502. (org-refile-new-child parent-target child)))
  9503. (error "Invalid target location")))))
  9504. (defun org-refile-check-position (refile-pointer)
  9505. "Check if the refile pointer matches the readline to which it points."
  9506. (let* ((file (nth 1 refile-pointer))
  9507. (re (nth 2 refile-pointer))
  9508. (pos (nth 3 refile-pointer))
  9509. buffer)
  9510. (when (org-string-nw-p re)
  9511. (setq buffer (if (markerp pos)
  9512. (marker-buffer pos)
  9513. (or (find-buffer-visiting file)
  9514. (find-file-noselect file))))
  9515. (with-current-buffer buffer
  9516. (save-excursion
  9517. (save-restriction
  9518. (widen)
  9519. (goto-char pos)
  9520. (beginning-of-line 1)
  9521. (unless (org-looking-at-p re)
  9522. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9523. (defun org-refile-new-child (parent-target child)
  9524. "Use refile target PARENT-TARGET to add new CHILD below it."
  9525. (unless parent-target
  9526. (error "Cannot find parent for new node"))
  9527. (let ((file (nth 1 parent-target))
  9528. (pos (nth 3 parent-target))
  9529. level)
  9530. (with-current-buffer (or (find-buffer-visiting file)
  9531. (find-file-noselect file))
  9532. (save-excursion
  9533. (save-restriction
  9534. (widen)
  9535. (if pos
  9536. (goto-char pos)
  9537. (goto-char (point-max))
  9538. (if (not (bolp)) (newline)))
  9539. (when (looking-at outline-regexp)
  9540. (setq level (funcall outline-level))
  9541. (org-end-of-subtree t t))
  9542. (org-back-over-empty-lines)
  9543. (insert "\n" (make-string
  9544. (if pos (org-get-valid-level level 1) 1) ?*)
  9545. " " child "\n")
  9546. (beginning-of-line 0)
  9547. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9548. (defun org-olpath-completing-read (prompt collection &rest args)
  9549. "Read an outline path like a file name."
  9550. (let ((thetable collection)
  9551. (org-completion-use-ido nil) ; does not work with ido.
  9552. (org-completion-use-iswitchb nil)) ; or iswitchb
  9553. (apply
  9554. 'org-icompleting-read prompt
  9555. (lambda (string predicate &optional flag)
  9556. (let (rtn r f (l (length string)))
  9557. (cond
  9558. ((eq flag nil)
  9559. ;; try completion
  9560. (try-completion string thetable))
  9561. ((eq flag t)
  9562. ;; all-completions
  9563. (setq rtn (all-completions string thetable predicate))
  9564. (mapcar
  9565. (lambda (x)
  9566. (setq r (substring x l))
  9567. (if (string-match " ([^)]*)$" x)
  9568. (setq f (match-string 0 x))
  9569. (setq f ""))
  9570. (if (string-match "/" r)
  9571. (concat string (substring r 0 (match-end 0)) f)
  9572. x))
  9573. rtn))
  9574. ((eq flag 'lambda)
  9575. ;; exact match?
  9576. (assoc string thetable)))
  9577. ))
  9578. args)))
  9579. ;;;; Dynamic blocks
  9580. (defun org-find-dblock (name)
  9581. "Find the first dynamic block with name NAME in the buffer.
  9582. If not found, stay at current position and return nil."
  9583. (let (pos)
  9584. (save-excursion
  9585. (goto-char (point-min))
  9586. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9587. nil t)
  9588. (match-beginning 0))))
  9589. (if pos (goto-char pos))
  9590. pos))
  9591. (defconst org-dblock-start-re
  9592. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9593. "Matches the start line of a dynamic block, with parameters.")
  9594. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9595. "Matches the end of a dynamic block.")
  9596. (defun org-create-dblock (plist)
  9597. "Create a dynamic block section, with parameters taken from PLIST.
  9598. PLIST must contain a :name entry which is used as name of the block."
  9599. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9600. (end-of-line 1)
  9601. (newline))
  9602. (let ((col (current-column))
  9603. (name (plist-get plist :name)))
  9604. (insert "#+BEGIN: " name)
  9605. (while plist
  9606. (if (eq (car plist) :name)
  9607. (setq plist (cddr plist))
  9608. (insert " " (prin1-to-string (pop plist)))))
  9609. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9610. (beginning-of-line -2)))
  9611. (defun org-prepare-dblock ()
  9612. "Prepare dynamic block for refresh.
  9613. This empties the block, puts the cursor at the insert position and returns
  9614. the property list including an extra property :name with the block name."
  9615. (unless (looking-at org-dblock-start-re)
  9616. (error "Not at a dynamic block"))
  9617. (let* ((begdel (1+ (match-end 0)))
  9618. (name (org-no-properties (match-string 1)))
  9619. (params (append (list :name name)
  9620. (read (concat "(" (match-string 3) ")")))))
  9621. (save-excursion
  9622. (beginning-of-line 1)
  9623. (skip-chars-forward " \t")
  9624. (setq params (plist-put params :indentation-column (current-column))))
  9625. (unless (re-search-forward org-dblock-end-re nil t)
  9626. (error "Dynamic block not terminated"))
  9627. (setq params
  9628. (append params
  9629. (list :content (buffer-substring
  9630. begdel (match-beginning 0)))))
  9631. (delete-region begdel (match-beginning 0))
  9632. (goto-char begdel)
  9633. (open-line 1)
  9634. params))
  9635. (defun org-map-dblocks (&optional command)
  9636. "Apply COMMAND to all dynamic blocks in the current buffer.
  9637. If COMMAND is not given, use `org-update-dblock'."
  9638. (let ((cmd (or command 'org-update-dblock)))
  9639. (save-excursion
  9640. (goto-char (point-min))
  9641. (while (re-search-forward org-dblock-start-re nil t)
  9642. (goto-char (match-beginning 0))
  9643. (save-excursion
  9644. (condition-case nil
  9645. (funcall cmd)
  9646. (error (message "Error during update of dynamic block"))))
  9647. (unless (re-search-forward org-dblock-end-re nil t)
  9648. (error "Dynamic block not terminated"))))))
  9649. (defun org-dblock-update (&optional arg)
  9650. "User command for updating dynamic blocks.
  9651. Update the dynamic block at point. With prefix ARG, update all dynamic
  9652. blocks in the buffer."
  9653. (interactive "P")
  9654. (if arg
  9655. (org-update-all-dblocks)
  9656. (or (looking-at org-dblock-start-re)
  9657. (org-beginning-of-dblock))
  9658. (org-update-dblock)))
  9659. (defun org-update-dblock ()
  9660. "Update the dynamic block at point.
  9661. This means to empty the block, parse for parameters and then call
  9662. the correct writing function."
  9663. (interactive)
  9664. (save-window-excursion
  9665. (let* ((pos (point))
  9666. (line (org-current-line))
  9667. (params (org-prepare-dblock))
  9668. (name (plist-get params :name))
  9669. (indent (plist-get params :indentation-column))
  9670. (cmd (intern (concat "org-dblock-write:" name))))
  9671. (message "Updating dynamic block `%s' at line %d..." name line)
  9672. (funcall cmd params)
  9673. (message "Updating dynamic block `%s' at line %d...done" name line)
  9674. (goto-char pos)
  9675. (when (and indent (> indent 0))
  9676. (setq indent (make-string indent ?\ ))
  9677. (save-excursion
  9678. (org-beginning-of-dblock)
  9679. (forward-line 1)
  9680. (while (not (looking-at org-dblock-end-re))
  9681. (insert indent)
  9682. (beginning-of-line 2))
  9683. (when (looking-at org-dblock-end-re)
  9684. (and (looking-at "[ \t]+")
  9685. (replace-match ""))
  9686. (insert indent)))))))
  9687. (defun org-beginning-of-dblock ()
  9688. "Find the beginning of the dynamic block at point.
  9689. Error if there is no such block at point."
  9690. (let ((pos (point))
  9691. beg)
  9692. (end-of-line 1)
  9693. (if (and (re-search-backward org-dblock-start-re nil t)
  9694. (setq beg (match-beginning 0))
  9695. (re-search-forward org-dblock-end-re nil t)
  9696. (> (match-end 0) pos))
  9697. (goto-char beg)
  9698. (goto-char pos)
  9699. (error "Not in a dynamic block"))))
  9700. (defun org-update-all-dblocks ()
  9701. "Update all dynamic blocks in the buffer.
  9702. This function can be used in a hook."
  9703. (interactive)
  9704. (when (org-mode-p)
  9705. (org-map-dblocks 'org-update-dblock)))
  9706. ;;;; Completion
  9707. (defconst org-additional-option-like-keywords
  9708. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9709. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9710. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9711. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9712. "BEGIN:" "END:"
  9713. "ORGTBL" "TBLFM:" "TBLNAME:"
  9714. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9715. "BEGIN_QUOTE" "END_QUOTE"
  9716. "BEGIN_VERSE" "END_VERSE"
  9717. "BEGIN_CENTER" "END_CENTER"
  9718. "BEGIN_SRC" "END_SRC"
  9719. "BEGIN_RESULT" "END_RESULT"
  9720. "SOURCE:" "SRCNAME:" "FUNCTION:"
  9721. "RESULTS:" "DATA:"
  9722. "HEADER:" "HEADERS:"
  9723. "BABEL:"
  9724. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9725. "CAPTION:" "LABEL:"
  9726. "SETUPFILE:"
  9727. "INCLUDE:"
  9728. "BIND:"
  9729. "MACRO:"))
  9730. (defcustom org-structure-template-alist
  9731. '(
  9732. ("s" "#+begin_src ?\n\n#+end_src"
  9733. "<src lang=\"?\">\n\n</src>")
  9734. ("e" "#+begin_example\n?\n#+end_example"
  9735. "<example>\n?\n</example>")
  9736. ("q" "#+begin_quote\n?\n#+end_quote"
  9737. "<quote>\n?\n</quote>")
  9738. ("v" "#+begin_verse\n?\n#+end_verse"
  9739. "<verse>\n?\n/verse>")
  9740. ("c" "#+begin_center\n?\n#+end_center"
  9741. "<center>\n?\n/center>")
  9742. ("l" "#+begin_latex\n?\n#+end_latex"
  9743. "<literal style=\"latex\">\n?\n</literal>")
  9744. ("L" "#+latex: "
  9745. "<literal style=\"latex\">?</literal>")
  9746. ("h" "#+begin_html\n?\n#+end_html"
  9747. "<literal style=\"html\">\n?\n</literal>")
  9748. ("H" "#+html: "
  9749. "<literal style=\"html\">?</literal>")
  9750. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9751. ("A" "#+ascii: ")
  9752. ("i" "#+index: ?"
  9753. "#+index: ?")
  9754. ("I" "#+include %file ?"
  9755. "<include file=%file markup=\"?\">")
  9756. )
  9757. "Structure completion elements.
  9758. This is a list of abbreviation keys and values. The value gets inserted
  9759. if you type `<' followed by the key and then press the completion key,
  9760. usually `M-TAB'. %file will be replaced by a file name after prompting
  9761. for the file using completion. The cursor will be placed at the position
  9762. of the `?` in the template.
  9763. There are two templates for each key, the first uses the original Org syntax,
  9764. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9765. the default when the /org-mtags.el/ module has been loaded. See also the
  9766. variable `org-mtags-prefer-muse-templates'.
  9767. This is an experimental feature, it is undecided if it is going to stay in."
  9768. :group 'org-completion
  9769. :type '(repeat
  9770. (string :tag "Key")
  9771. (string :tag "Template")
  9772. (string :tag "Muse Template")))
  9773. (defun org-try-structure-completion ()
  9774. "Try to complete a structure template before point.
  9775. This looks for strings like \"<e\" on an otherwise empty line and
  9776. expands them."
  9777. (let ((l (buffer-substring (point-at-bol) (point)))
  9778. a)
  9779. (when (and (looking-at "[ \t]*$")
  9780. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  9781. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9782. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9783. (match-beginning 1)) a)
  9784. t)))
  9785. (defun org-complete-expand-structure-template (start cell)
  9786. "Expand a structure template."
  9787. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9788. (rpl (nth (if musep 2 1) cell))
  9789. (ind ""))
  9790. (delete-region start (point))
  9791. (when (string-match "\\`#\\+" rpl)
  9792. (cond
  9793. ((bolp))
  9794. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9795. (setq ind (buffer-substring (point-at-bol) (point))))
  9796. (t (newline))))
  9797. (setq start (point))
  9798. (if (string-match "%file" rpl)
  9799. (setq rpl (replace-match
  9800. (concat
  9801. "\""
  9802. (save-match-data
  9803. (abbreviate-file-name (read-file-name "Include file: ")))
  9804. "\"")
  9805. t t rpl)))
  9806. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9807. (concat "\n" ind)))
  9808. (insert rpl)
  9809. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9810. ;;;; TODO, DEADLINE, Comments
  9811. (defun org-toggle-comment ()
  9812. "Change the COMMENT state of an entry."
  9813. (interactive)
  9814. (save-excursion
  9815. (org-back-to-heading)
  9816. (let (case-fold-search)
  9817. (if (looking-at (concat outline-regexp
  9818. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9819. (replace-match "" t t nil 1)
  9820. (if (looking-at outline-regexp)
  9821. (progn
  9822. (goto-char (match-end 0))
  9823. (insert org-comment-string " ")))))))
  9824. (defvar org-last-todo-state-is-todo nil
  9825. "This is non-nil when the last TODO state change led to a TODO state.
  9826. If the last change removed the TODO tag or switched to DONE, then
  9827. this is nil.")
  9828. (defvar org-setting-tags nil) ; dynamically skipped
  9829. (defvar org-todo-setup-filter-hook nil
  9830. "Hook for functions that pre-filter todo specs.
  9831. Each function takes a todo spec and returns either nil or the spec
  9832. transformed into canonical form." )
  9833. (defvar org-todo-get-default-hook nil
  9834. "Hook for functions that get a default item for todo.
  9835. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9836. nil or a string to be used for the todo mark." )
  9837. (defvar org-agenda-headline-snapshot-before-repeat)
  9838. (defun org-todo (&optional arg)
  9839. "Change the TODO state of an item.
  9840. The state of an item is given by a keyword at the start of the heading,
  9841. like
  9842. *** TODO Write paper
  9843. *** DONE Call mom
  9844. The different keywords are specified in the variable `org-todo-keywords'.
  9845. By default the available states are \"TODO\" and \"DONE\".
  9846. So for this example: when the item starts with TODO, it is changed to DONE.
  9847. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9848. DONE are present, add TODO at the beginning of the heading.
  9849. With \\[universal-argument] prefix arg, use completion to determine the new \
  9850. state.
  9851. With numeric prefix arg, switch to that state.
  9852. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  9853. keywords (nextset).
  9854. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  9855. For calling through lisp, arg is also interpreted in the following way:
  9856. 'none -> empty state
  9857. \"\"(empty string) -> switch to empty state
  9858. 'done -> switch to DONE
  9859. 'nextset -> switch to the next set of keywords
  9860. 'previousset -> switch to the previous set of keywords
  9861. \"WAITING\" -> switch to the specified keyword, but only if it
  9862. really is a member of `org-todo-keywords'."
  9863. (interactive "P")
  9864. (if (equal arg '(16)) (setq arg 'nextset))
  9865. (let ((org-blocker-hook org-blocker-hook)
  9866. (case-fold-search nil))
  9867. (when (equal arg '(64))
  9868. (setq arg nil org-blocker-hook nil))
  9869. (when (and org-blocker-hook
  9870. (or org-inhibit-blocking
  9871. (org-entry-get nil "NOBLOCKING")))
  9872. (setq org-blocker-hook nil))
  9873. (save-excursion
  9874. (catch 'exit
  9875. (org-back-to-heading t)
  9876. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9877. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9878. (looking-at " *"))
  9879. (let* ((match-data (match-data))
  9880. (startpos (point-at-bol))
  9881. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  9882. (org-log-done org-log-done)
  9883. (org-log-repeat org-log-repeat)
  9884. (org-todo-log-states org-todo-log-states)
  9885. (this (match-string 1))
  9886. (hl-pos (match-beginning 0))
  9887. (head (org-get-todo-sequence-head this))
  9888. (ass (assoc head org-todo-kwd-alist))
  9889. (interpret (nth 1 ass))
  9890. (done-word (nth 3 ass))
  9891. (final-done-word (nth 4 ass))
  9892. (last-state (or this ""))
  9893. (completion-ignore-case t)
  9894. (member (member this org-todo-keywords-1))
  9895. (tail (cdr member))
  9896. (state (cond
  9897. ((and org-todo-key-trigger
  9898. (or (and (equal arg '(4))
  9899. (eq org-use-fast-todo-selection 'prefix))
  9900. (and (not arg) org-use-fast-todo-selection
  9901. (not (eq org-use-fast-todo-selection
  9902. 'prefix)))))
  9903. ;; Use fast selection
  9904. (org-fast-todo-selection))
  9905. ((and (equal arg '(4))
  9906. (or (not org-use-fast-todo-selection)
  9907. (not org-todo-key-trigger)))
  9908. ;; Read a state with completion
  9909. (org-icompleting-read
  9910. "State: " (mapcar (lambda(x) (list x))
  9911. org-todo-keywords-1)
  9912. nil t))
  9913. ((eq arg 'right)
  9914. (if this
  9915. (if tail (car tail) nil)
  9916. (car org-todo-keywords-1)))
  9917. ((eq arg 'left)
  9918. (if (equal member org-todo-keywords-1)
  9919. nil
  9920. (if this
  9921. (nth (- (length org-todo-keywords-1)
  9922. (length tail) 2)
  9923. org-todo-keywords-1)
  9924. (org-last org-todo-keywords-1))))
  9925. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9926. (setq arg nil))) ; hack to fall back to cycling
  9927. (arg
  9928. ;; user or caller requests a specific state
  9929. (cond
  9930. ((equal arg "") nil)
  9931. ((eq arg 'none) nil)
  9932. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9933. ((eq arg 'nextset)
  9934. (or (car (cdr (member head org-todo-heads)))
  9935. (car org-todo-heads)))
  9936. ((eq arg 'previousset)
  9937. (let ((org-todo-heads (reverse org-todo-heads)))
  9938. (or (car (cdr (member head org-todo-heads)))
  9939. (car org-todo-heads))))
  9940. ((car (member arg org-todo-keywords-1)))
  9941. ((stringp arg)
  9942. (error "State `%s' not valid in this file" arg))
  9943. ((nth (1- (prefix-numeric-value arg))
  9944. org-todo-keywords-1))))
  9945. ((null member) (or head (car org-todo-keywords-1)))
  9946. ((equal this final-done-word) nil) ;; -> make empty
  9947. ((null tail) nil) ;; -> first entry
  9948. ((memq interpret '(type priority))
  9949. (if (eq this-command last-command)
  9950. (car tail)
  9951. (if (> (length tail) 0)
  9952. (or done-word (car org-done-keywords))
  9953. nil)))
  9954. (t
  9955. (car tail))))
  9956. (state (or
  9957. (run-hook-with-args-until-success
  9958. 'org-todo-get-default-hook state last-state)
  9959. state))
  9960. (next (if state (concat " " state " ") " "))
  9961. (change-plist (list :type 'todo-state-change :from this :to state
  9962. :position startpos))
  9963. dolog now-done-p)
  9964. (when org-blocker-hook
  9965. (setq org-last-todo-state-is-todo
  9966. (not (member this org-done-keywords)))
  9967. (unless (save-excursion
  9968. (save-match-data
  9969. (org-with-wide-buffer
  9970. (run-hook-with-args-until-failure
  9971. 'org-blocker-hook change-plist))))
  9972. (if (org-called-interactively-p 'interactive)
  9973. (error "TODO state change from %s to %s blocked" this state)
  9974. ;; fail silently
  9975. (message "TODO state change from %s to %s blocked" this state)
  9976. (throw 'exit nil))))
  9977. (store-match-data match-data)
  9978. (replace-match next t t)
  9979. (unless (pos-visible-in-window-p hl-pos)
  9980. (message "TODO state changed to %s" (org-trim next)))
  9981. (unless head
  9982. (setq head (org-get-todo-sequence-head state)
  9983. ass (assoc head org-todo-kwd-alist)
  9984. interpret (nth 1 ass)
  9985. done-word (nth 3 ass)
  9986. final-done-word (nth 4 ass)))
  9987. (when (memq arg '(nextset previousset))
  9988. (message "Keyword-Set %d/%d: %s"
  9989. (- (length org-todo-sets) -1
  9990. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9991. (length org-todo-sets)
  9992. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9993. (setq org-last-todo-state-is-todo
  9994. (not (member state org-done-keywords)))
  9995. (setq now-done-p (and (member state org-done-keywords)
  9996. (not (member this org-done-keywords))))
  9997. (and logging (org-local-logging logging))
  9998. (when (and (or org-todo-log-states org-log-done)
  9999. (not (eq org-inhibit-logging t))
  10000. (not (memq arg '(nextset previousset))))
  10001. ;; we need to look at recording a time and note
  10002. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  10003. (nth 2 (assoc this org-todo-log-states))))
  10004. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10005. (setq dolog 'time))
  10006. (when (and state
  10007. (member state org-not-done-keywords)
  10008. (not (member this org-not-done-keywords)))
  10009. ;; This is now a todo state and was not one before
  10010. ;; If there was a CLOSED time stamp, get rid of it.
  10011. (org-add-planning-info nil nil 'closed))
  10012. (when (and now-done-p org-log-done)
  10013. ;; It is now done, and it was not done before
  10014. (org-add-planning-info 'closed (org-current-time))
  10015. (if (and (not dolog) (eq 'note org-log-done))
  10016. (org-add-log-setup 'done state this 'findpos 'note)))
  10017. (when (and state dolog)
  10018. ;; This is a non-nil state, and we need to log it
  10019. (org-add-log-setup 'state state this 'findpos dolog)))
  10020. ;; Fixup tag positioning
  10021. (org-todo-trigger-tag-changes state)
  10022. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10023. (when org-provide-todo-statistics
  10024. (org-update-parent-todo-statistics))
  10025. (run-hooks 'org-after-todo-state-change-hook)
  10026. (if (and arg (not (member state org-done-keywords)))
  10027. (setq head (org-get-todo-sequence-head state)))
  10028. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10029. ;; Do we need to trigger a repeat?
  10030. (when now-done-p
  10031. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10032. ;; This is for the agenda, take a snapshot of the headline.
  10033. (save-match-data
  10034. (setq org-agenda-headline-snapshot-before-repeat
  10035. (org-get-heading))))
  10036. (org-auto-repeat-maybe state))
  10037. ;; Fixup cursor location if close to the keyword
  10038. (if (and (outline-on-heading-p)
  10039. (not (bolp))
  10040. (save-excursion (beginning-of-line 1)
  10041. (looking-at org-todo-line-regexp))
  10042. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10043. (progn
  10044. (goto-char (or (match-end 2) (match-end 1)))
  10045. (and (looking-at " ") (just-one-space))))
  10046. (when org-trigger-hook
  10047. (save-excursion
  10048. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  10049. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10050. "Block turning an entry into a TODO, using the hierarchy.
  10051. This checks whether the current task should be blocked from state
  10052. changes. Such blocking occurs when:
  10053. 1. The task has children which are not all in a completed state.
  10054. 2. A task has a parent with the property :ORDERED:, and there
  10055. are siblings prior to the current task with incomplete
  10056. status.
  10057. 3. The parent of the task is blocked because it has siblings that should
  10058. be done first, or is child of a block grandparent TODO entry."
  10059. (if (not org-enforce-todo-dependencies)
  10060. t ; if locally turned off don't block
  10061. (catch 'dont-block
  10062. ;; If this is not a todo state change, or if this entry is already DONE,
  10063. ;; do not block
  10064. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10065. (member (plist-get change-plist :from)
  10066. (cons 'done org-done-keywords))
  10067. (member (plist-get change-plist :to)
  10068. (cons 'todo org-not-done-keywords))
  10069. (not (plist-get change-plist :to)))
  10070. (throw 'dont-block t))
  10071. ;; If this task has children, and any are undone, it's blocked
  10072. (save-excursion
  10073. (org-back-to-heading t)
  10074. (let ((this-level (funcall outline-level)))
  10075. (outline-next-heading)
  10076. (let ((child-level (funcall outline-level)))
  10077. (while (and (not (eobp))
  10078. (> child-level this-level))
  10079. ;; this todo has children, check whether they are all
  10080. ;; completed
  10081. (if (and (not (org-entry-is-done-p))
  10082. (org-entry-is-todo-p))
  10083. (throw 'dont-block nil))
  10084. (outline-next-heading)
  10085. (setq child-level (funcall outline-level))))))
  10086. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10087. ;; any previous siblings are undone, it's blocked
  10088. (save-excursion
  10089. (org-back-to-heading t)
  10090. (let* ((pos (point))
  10091. (parent-pos (and (org-up-heading-safe) (point))))
  10092. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10093. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10094. (forward-line 1)
  10095. (re-search-forward org-not-done-heading-regexp pos t))
  10096. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10097. ;; Search further up the hierarchy, to see if an anchestor is blocked
  10098. (while t
  10099. (goto-char parent-pos)
  10100. (if (not (looking-at org-not-done-heading-regexp))
  10101. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10102. (setq pos (point))
  10103. (setq parent-pos (and (org-up-heading-safe) (point)))
  10104. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10105. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10106. (forward-line 1)
  10107. (re-search-forward org-not-done-heading-regexp pos t))
  10108. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10109. (defcustom org-track-ordered-property-with-tag nil
  10110. "Should the ORDERED property also be shown as a tag?
  10111. The ORDERED property decides if an entry should require subtasks to be
  10112. completed in sequence. Since a property is not very visible, setting
  10113. this option means that toggling the ORDERED property with the command
  10114. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10115. not relevant for the behavior, but it makes things more visible.
  10116. Note that toggling the tag with tags commands will not change the property
  10117. and therefore not influence behavior!
  10118. This can be t, meaning the tag ORDERED should be used, It can also be a
  10119. string to select a different tag for this task."
  10120. :group 'org-todo
  10121. :type '(choice
  10122. (const :tag "No tracking" nil)
  10123. (const :tag "Track with ORDERED tag" t)
  10124. (string :tag "Use other tag")))
  10125. (defun org-toggle-ordered-property ()
  10126. "Toggle the ORDERED property of the current entry.
  10127. For better visibility, you can track the value of this property with a tag.
  10128. See variable `org-track-ordered-property-with-tag'."
  10129. (interactive)
  10130. (let* ((t1 org-track-ordered-property-with-tag)
  10131. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10132. (save-excursion
  10133. (org-back-to-heading)
  10134. (if (org-entry-get nil "ORDERED")
  10135. (progn
  10136. (org-delete-property "ORDERED")
  10137. (and tag (org-toggle-tag tag 'off))
  10138. (message "Subtasks can be completed in arbitrary order"))
  10139. (org-entry-put nil "ORDERED" "t")
  10140. (and tag (org-toggle-tag tag 'on))
  10141. (message "Subtasks must be completed in sequence")))))
  10142. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10143. (defun org-block-todo-from-checkboxes (change-plist)
  10144. "Block turning an entry into a TODO, using checkboxes.
  10145. This checks whether the current task should be blocked from state
  10146. changes because there are unchecked boxes in this entry."
  10147. (if (not org-enforce-todo-checkbox-dependencies)
  10148. t ; if locally turned off don't block
  10149. (catch 'dont-block
  10150. ;; If this is not a todo state change, or if this entry is already DONE,
  10151. ;; do not block
  10152. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10153. (member (plist-get change-plist :from)
  10154. (cons 'done org-done-keywords))
  10155. (member (plist-get change-plist :to)
  10156. (cons 'todo org-not-done-keywords))
  10157. (not (plist-get change-plist :to)))
  10158. (throw 'dont-block t))
  10159. ;; If this task has checkboxes that are not checked, it's blocked
  10160. (save-excursion
  10161. (org-back-to-heading t)
  10162. (let ((beg (point)) end)
  10163. (outline-next-heading)
  10164. (setq end (point))
  10165. (goto-char beg)
  10166. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  10167. end t)
  10168. (progn
  10169. (if (boundp 'org-blocked-by-checkboxes)
  10170. (setq org-blocked-by-checkboxes t))
  10171. (throw 'dont-block nil)))))
  10172. t))) ; do not block
  10173. (defun org-entry-blocked-p ()
  10174. "Is the current entry blocked?"
  10175. (if (org-entry-get nil "NOBLOCKING")
  10176. nil ;; Never block this entry
  10177. (not
  10178. (run-hook-with-args-until-failure
  10179. 'org-blocker-hook
  10180. (list :type 'todo-state-change
  10181. :position (point)
  10182. :from 'todo
  10183. :to 'done)))))
  10184. (defun org-update-statistics-cookies (all)
  10185. "Update the statistics cookie, either from TODO or from checkboxes.
  10186. This should be called with the cursor in a line with a statistics cookie."
  10187. (interactive "P")
  10188. (if all
  10189. (progn
  10190. (org-update-checkbox-count 'all)
  10191. (org-map-entries 'org-update-parent-todo-statistics))
  10192. (if (not (org-on-heading-p))
  10193. (org-update-checkbox-count)
  10194. (let ((pos (move-marker (make-marker) (point)))
  10195. end l1 l2)
  10196. (ignore-errors (org-back-to-heading t))
  10197. (if (not (org-on-heading-p))
  10198. (org-update-checkbox-count)
  10199. (setq l1 (org-outline-level))
  10200. (setq end (save-excursion
  10201. (outline-next-heading)
  10202. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10203. (point)))
  10204. (if (and (save-excursion
  10205. (re-search-forward
  10206. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10207. (not (save-excursion (re-search-forward
  10208. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10209. (org-update-checkbox-count)
  10210. (if (and l2 (> l2 l1))
  10211. (progn
  10212. (goto-char end)
  10213. (org-update-parent-todo-statistics))
  10214. (goto-char pos)
  10215. (beginning-of-line 1)
  10216. (while (re-search-forward
  10217. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10218. (point-at-eol) t)
  10219. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10220. (goto-char pos)
  10221. (move-marker pos nil)))))
  10222. (defvar org-entry-property-inherited-from) ;; defined below
  10223. (defun org-update-parent-todo-statistics ()
  10224. "Update any statistics cookie in the parent of the current headline.
  10225. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10226. the entire subtree and this will travel up the hierarchy and update
  10227. statistics everywhere."
  10228. (interactive)
  10229. (let* ((lim 0) prop
  10230. (recursive (or (not org-hierarchical-todo-statistics)
  10231. (string-match
  10232. "\\<recursive\\>"
  10233. (or (setq prop (org-entry-get
  10234. nil "COOKIE_DATA" 'inherit)) ""))))
  10235. (lim (or (and prop (marker-position
  10236. org-entry-property-inherited-from))
  10237. lim))
  10238. (first t)
  10239. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10240. level ltoggle l1 new ndel
  10241. (cnt-all 0) (cnt-done 0) is-percent kwd
  10242. checkbox-beg ov ovs ove cookie-present)
  10243. (catch 'exit
  10244. (save-excursion
  10245. (beginning-of-line 1)
  10246. (if (org-at-heading-p)
  10247. (setq ltoggle (funcall outline-level))
  10248. (error "This should not happen"))
  10249. (while (and (setq level (org-up-heading-safe))
  10250. (or recursive first)
  10251. (>= (point) lim))
  10252. (setq first nil cookie-present nil)
  10253. (unless (and level
  10254. (not (string-match
  10255. "\\<checkbox\\>"
  10256. (downcase
  10257. (or (org-entry-get
  10258. nil "COOKIE_DATA")
  10259. "")))))
  10260. (throw 'exit nil))
  10261. (while (re-search-forward box-re (point-at-eol) t)
  10262. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10263. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10264. (save-match-data
  10265. (unless (outline-next-heading) (throw 'exit nil))
  10266. (while (and (looking-at org-complex-heading-regexp)
  10267. (> (setq l1 (length (match-string 1))) level))
  10268. (setq kwd (and (or recursive (= l1 ltoggle))
  10269. (match-string 2)))
  10270. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10271. (and (listp org-provide-todo-statistics)
  10272. (or (member kwd org-provide-todo-statistics)
  10273. (member kwd org-done-keywords))))
  10274. (setq cnt-all (1+ cnt-all))
  10275. (if (eq org-provide-todo-statistics t)
  10276. (and kwd (setq cnt-all (1+ cnt-all)))))
  10277. (and (member kwd org-done-keywords)
  10278. (setq cnt-done (1+ cnt-done)))
  10279. (outline-next-heading)))
  10280. (setq new
  10281. (if is-percent
  10282. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10283. (format "[%d/%d]" cnt-done cnt-all))
  10284. ndel (- (match-end 0) checkbox-beg))
  10285. ;; handle overlays when updating cookie from column view
  10286. (when (setq ov (car (overlays-at checkbox-beg)))
  10287. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10288. (delete-overlay ov))
  10289. (goto-char checkbox-beg)
  10290. (insert new)
  10291. (delete-region (point) (+ (point) ndel))
  10292. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10293. (when ov (move-overlay ov ovs ove)))
  10294. (when cookie-present
  10295. (run-hook-with-args 'org-after-todo-statistics-hook
  10296. cnt-done (- cnt-all cnt-done))))))
  10297. (run-hooks 'org-todo-statistics-hook)))
  10298. (defvar org-after-todo-statistics-hook nil
  10299. "Hook that is called after a TODO statistics cookie has been updated.
  10300. Each function is called with two arguments: the number of not-done entries
  10301. and the number of done entries.
  10302. For example, the following function, when added to this hook, will switch
  10303. an entry to DONE when all children are done, and back to TODO when new
  10304. entries are set to a TODO status. Note that this hook is only called
  10305. when there is a statistics cookie in the headline!
  10306. (defun org-summary-todo (n-done n-not-done)
  10307. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10308. (let (org-log-done org-log-states) ; turn off logging
  10309. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10310. ")
  10311. (defvar org-todo-statistics-hook nil
  10312. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10313. This hook runs even if there is no statistics cookie present, in which case
  10314. `org-after-todo-statistics-hook' would not run.")
  10315. (defun org-todo-trigger-tag-changes (state)
  10316. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10317. (let ((l org-todo-state-tags-triggers)
  10318. changes)
  10319. (when (or (not state) (equal state ""))
  10320. (setq changes (append changes (cdr (assoc "" l)))))
  10321. (when (and (stringp state) (> (length state) 0))
  10322. (setq changes (append changes (cdr (assoc state l)))))
  10323. (when (member state org-not-done-keywords)
  10324. (setq changes (append changes (cdr (assoc 'todo l)))))
  10325. (when (member state org-done-keywords)
  10326. (setq changes (append changes (cdr (assoc 'done l)))))
  10327. (dolist (c changes)
  10328. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10329. (defun org-local-logging (value)
  10330. "Get logging settings from a property VALUE."
  10331. (let* (words w a)
  10332. ;; directly set the variables, they are already local.
  10333. (setq org-log-done nil
  10334. org-log-repeat nil
  10335. org-todo-log-states nil)
  10336. (setq words (org-split-string value))
  10337. (while (setq w (pop words))
  10338. (cond
  10339. ((setq a (assoc w org-startup-options))
  10340. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10341. (set (nth 1 a) (nth 2 a))))
  10342. ((setq a (org-extract-log-state-settings w))
  10343. (and (member (car a) org-todo-keywords-1)
  10344. (push a org-todo-log-states)))))))
  10345. (defun org-get-todo-sequence-head (kwd)
  10346. "Return the head of the TODO sequence to which KWD belongs.
  10347. If KWD is not set, check if there is a text property remembering the
  10348. right sequence."
  10349. (let (p)
  10350. (cond
  10351. ((not kwd)
  10352. (or (get-text-property (point-at-bol) 'org-todo-head)
  10353. (progn
  10354. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10355. nil (point-at-eol)))
  10356. (get-text-property p 'org-todo-head))))
  10357. ((not (member kwd org-todo-keywords-1))
  10358. (car org-todo-keywords-1))
  10359. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10360. (defun org-fast-todo-selection ()
  10361. "Fast TODO keyword selection with single keys.
  10362. Returns the new TODO keyword, or nil if no state change should occur."
  10363. (let* ((fulltable org-todo-key-alist)
  10364. (done-keywords org-done-keywords) ;; needed for the faces.
  10365. (maxlen (apply 'max (mapcar
  10366. (lambda (x)
  10367. (if (stringp (car x)) (string-width (car x)) 0))
  10368. fulltable)))
  10369. (expert nil)
  10370. (fwidth (+ maxlen 3 1 3))
  10371. (ncol (/ (- (window-width) 4) fwidth))
  10372. tg cnt e c tbl
  10373. groups ingroup)
  10374. (save-excursion
  10375. (save-window-excursion
  10376. (if expert
  10377. (set-buffer (get-buffer-create " *Org todo*"))
  10378. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10379. (erase-buffer)
  10380. (org-set-local 'org-done-keywords done-keywords)
  10381. (setq tbl fulltable cnt 0)
  10382. (while (setq e (pop tbl))
  10383. (cond
  10384. ((equal e '(:startgroup))
  10385. (push '() groups) (setq ingroup t)
  10386. (when (not (= cnt 0))
  10387. (setq cnt 0)
  10388. (insert "\n"))
  10389. (insert "{ "))
  10390. ((equal e '(:endgroup))
  10391. (setq ingroup nil cnt 0)
  10392. (insert "}\n"))
  10393. ((equal e '(:newline))
  10394. (when (not (= cnt 0))
  10395. (setq cnt 0)
  10396. (insert "\n")
  10397. (setq e (car tbl))
  10398. (while (equal (car tbl) '(:newline))
  10399. (insert "\n")
  10400. (setq tbl (cdr tbl)))))
  10401. (t
  10402. (setq tg (car e) c (cdr e))
  10403. (if ingroup (push tg (car groups)))
  10404. (setq tg (org-add-props tg nil 'face
  10405. (org-get-todo-face tg)))
  10406. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10407. (insert "[" c "] " tg (make-string
  10408. (- fwidth 4 (length tg)) ?\ ))
  10409. (when (= (setq cnt (1+ cnt)) ncol)
  10410. (insert "\n")
  10411. (if ingroup (insert " "))
  10412. (setq cnt 0)))))
  10413. (insert "\n")
  10414. (goto-char (point-min))
  10415. (if (not expert) (org-fit-window-to-buffer))
  10416. (message "[a-z..]:Set [SPC]:clear")
  10417. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10418. (cond
  10419. ((or (= c ?\C-g)
  10420. (and (= c ?q) (not (rassoc c fulltable))))
  10421. (setq quit-flag t))
  10422. ((= c ?\ ) nil)
  10423. ((setq e (rassoc c fulltable) tg (car e))
  10424. tg)
  10425. (t (setq quit-flag t)))))))
  10426. (defun org-entry-is-todo-p ()
  10427. (member (org-get-todo-state) org-not-done-keywords))
  10428. (defun org-entry-is-done-p ()
  10429. (member (org-get-todo-state) org-done-keywords))
  10430. (defun org-get-todo-state ()
  10431. (save-excursion
  10432. (org-back-to-heading t)
  10433. (and (looking-at org-todo-line-regexp)
  10434. (match-end 2)
  10435. (match-string 2))))
  10436. (defun org-at-date-range-p (&optional inactive-ok)
  10437. "Is the cursor inside a date range?"
  10438. (interactive)
  10439. (save-excursion
  10440. (catch 'exit
  10441. (let ((pos (point)))
  10442. (skip-chars-backward "^[<\r\n")
  10443. (skip-chars-backward "<[")
  10444. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10445. (>= (match-end 0) pos)
  10446. (throw 'exit t))
  10447. (skip-chars-backward "^<[\r\n")
  10448. (skip-chars-backward "<[")
  10449. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10450. (>= (match-end 0) pos)
  10451. (throw 'exit t)))
  10452. nil)))
  10453. (defun org-get-repeat (&optional tagline)
  10454. "Check if there is a deadline/schedule with repeater in this entry."
  10455. (save-match-data
  10456. (save-excursion
  10457. (org-back-to-heading t)
  10458. (and (re-search-forward (if tagline
  10459. (concat tagline "\\s-*" org-repeat-re)
  10460. org-repeat-re)
  10461. (org-entry-end-position) t)
  10462. (match-string-no-properties 1)))))
  10463. (defvar org-last-changed-timestamp)
  10464. (defvar org-last-inserted-timestamp)
  10465. (defvar org-log-post-message)
  10466. (defvar org-log-note-purpose)
  10467. (defvar org-log-note-how)
  10468. (defvar org-log-note-extra)
  10469. (defun org-auto-repeat-maybe (done-word)
  10470. "Check if the current headline contains a repeated deadline/schedule.
  10471. If yes, set TODO state back to what it was and change the base date
  10472. of repeating deadline/scheduled time stamps to new date.
  10473. This function is run automatically after each state change to a DONE state."
  10474. ;; last-state is dynamically scoped into this function
  10475. (let* ((repeat (org-get-repeat))
  10476. (aa (assoc last-state org-todo-kwd-alist))
  10477. (interpret (nth 1 aa))
  10478. (head (nth 2 aa))
  10479. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10480. (msg "Entry repeats: ")
  10481. (org-log-done nil)
  10482. (org-todo-log-states nil)
  10483. re type n what ts time to-state)
  10484. (when repeat
  10485. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10486. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10487. org-todo-repeat-to-state))
  10488. (unless (and to-state (member to-state org-todo-keywords-1))
  10489. (setq to-state (if (eq interpret 'type) last-state head)))
  10490. (org-todo to-state)
  10491. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10492. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10493. (org-time-stamp-format t t))))
  10494. (when org-log-repeat
  10495. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10496. (memq 'org-add-log-note post-command-hook))
  10497. ;; OK, we are already setup for some record
  10498. (if (eq org-log-repeat 'note)
  10499. ;; make sure we take a note, not only a time stamp
  10500. (setq org-log-note-how 'note))
  10501. ;; Set up for taking a record
  10502. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10503. last-state
  10504. 'findpos org-log-repeat)))
  10505. (org-back-to-heading t)
  10506. (org-add-planning-info nil nil 'closed)
  10507. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10508. org-deadline-time-regexp "\\)\\|\\("
  10509. org-ts-regexp "\\)"))
  10510. (while (re-search-forward
  10511. re (save-excursion (outline-next-heading) (point)) t)
  10512. (setq type (if (match-end 1) org-scheduled-string
  10513. (if (match-end 3) org-deadline-string "Plain:"))
  10514. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10515. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10516. (setq n (string-to-number (match-string 2 ts))
  10517. what (match-string 3 ts))
  10518. (if (equal what "w") (setq n (* n 7) what "d"))
  10519. ;; Preparation, see if we need to modify the start date for the change
  10520. (when (match-end 1)
  10521. (setq time (save-match-data (org-time-string-to-time ts)))
  10522. (cond
  10523. ((equal (match-string 1 ts) ".")
  10524. ;; Shift starting date to today
  10525. (org-timestamp-change
  10526. (- (time-to-days (current-time)) (time-to-days time))
  10527. 'day))
  10528. ((equal (match-string 1 ts) "+")
  10529. (let ((nshiftmax 10) (nshift 0))
  10530. (while (or (= nshift 0)
  10531. (<= (time-to-days time)
  10532. (time-to-days (current-time))))
  10533. (when (= (incf nshift) nshiftmax)
  10534. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10535. (error "Abort")))
  10536. (org-timestamp-change n (cdr (assoc what whata)))
  10537. (org-at-timestamp-p t)
  10538. (setq ts (match-string 1))
  10539. (setq time (save-match-data (org-time-string-to-time ts)))))
  10540. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10541. ;; rematch, so that we have everything in place for the real shift
  10542. (org-at-timestamp-p t)
  10543. (setq ts (match-string 1))
  10544. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10545. (org-timestamp-change n (cdr (assoc what whata)))
  10546. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10547. (setq org-log-post-message msg)
  10548. (message "%s" msg))))
  10549. (defun org-show-todo-tree (arg)
  10550. "Make a compact tree which shows all headlines marked with TODO.
  10551. The tree will show the lines where the regexp matches, and all higher
  10552. headlines above the match.
  10553. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10554. With a numeric prefix N, construct a sparse tree for the Nth element
  10555. of `org-todo-keywords-1'."
  10556. (interactive "P")
  10557. (let ((case-fold-search nil)
  10558. (kwd-re
  10559. (cond ((null arg) org-not-done-regexp)
  10560. ((equal arg '(4))
  10561. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10562. (mapcar 'list org-todo-keywords-1))))
  10563. (concat "\\("
  10564. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10565. "\\)\\>")))
  10566. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10567. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10568. org-todo-keywords-1)))
  10569. (t (error "Invalid prefix argument: %s" arg)))))
  10570. (message "%d TODO entries found"
  10571. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  10572. (defun org-deadline (&optional remove time)
  10573. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10574. With argument REMOVE, remove any deadline from the item.
  10575. When TIME is set, it should be an internal time specification, and the
  10576. scheduling will use the corresponding date."
  10577. (interactive "P")
  10578. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10579. (repeater (and old-date
  10580. (string-match
  10581. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10582. old-date)
  10583. (match-string 1 old-date))))
  10584. (if remove
  10585. (progn
  10586. (when (and old-date org-log-redeadline)
  10587. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10588. org-log-redeadline))
  10589. (org-remove-timestamp-with-keyword org-deadline-string)
  10590. (message "Item no longer has a deadline."))
  10591. (org-add-planning-info 'deadline time 'closed)
  10592. (when (and old-date org-log-redeadline
  10593. (not (equal old-date
  10594. (substring org-last-inserted-timestamp 1 -1))))
  10595. (org-add-log-setup 'redeadline nil old-date 'findpos
  10596. org-log-redeadline))
  10597. (when repeater
  10598. (save-excursion
  10599. (org-back-to-heading t)
  10600. (when (re-search-forward (concat org-deadline-string " "
  10601. org-last-inserted-timestamp)
  10602. (save-excursion
  10603. (outline-next-heading) (point)) t)
  10604. (goto-char (1- (match-end 0)))
  10605. (insert " " repeater)
  10606. (setq org-last-inserted-timestamp
  10607. (concat (substring org-last-inserted-timestamp 0 -1)
  10608. " " repeater
  10609. (substring org-last-inserted-timestamp -1))))))
  10610. (message "Deadline on %s" org-last-inserted-timestamp))))
  10611. (defun org-schedule (&optional remove time)
  10612. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10613. With argument REMOVE, remove any scheduling date from the item.
  10614. When TIME is set, it should be an internal time specification, and the
  10615. scheduling will use the corresponding date."
  10616. (interactive "P")
  10617. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10618. (repeater (and old-date
  10619. (string-match
  10620. "\\([.+-]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10621. old-date)
  10622. (match-string 1 old-date))))
  10623. (if remove
  10624. (progn
  10625. (when (and old-date org-log-reschedule)
  10626. (org-add-log-setup 'delschedule nil old-date 'findpos
  10627. org-log-reschedule))
  10628. (org-remove-timestamp-with-keyword org-scheduled-string)
  10629. (message "Item is no longer scheduled."))
  10630. (org-add-planning-info 'scheduled time 'closed)
  10631. (when (and old-date org-log-reschedule
  10632. (not (equal old-date
  10633. (substring org-last-inserted-timestamp 1 -1))))
  10634. (org-add-log-setup 'reschedule nil old-date 'findpos
  10635. org-log-reschedule))
  10636. (when repeater
  10637. (save-excursion
  10638. (org-back-to-heading t)
  10639. (when (re-search-forward (concat org-scheduled-string " "
  10640. org-last-inserted-timestamp)
  10641. (save-excursion
  10642. (outline-next-heading) (point)) t)
  10643. (goto-char (1- (match-end 0)))
  10644. (insert " " repeater)
  10645. (setq org-last-inserted-timestamp
  10646. (concat (substring org-last-inserted-timestamp 0 -1)
  10647. " " repeater
  10648. (substring org-last-inserted-timestamp -1))))))
  10649. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10650. (defun org-get-scheduled-time (pom &optional inherit)
  10651. "Get the scheduled time as a time tuple, of a format suitable
  10652. for calling org-schedule with, or if there is no scheduling,
  10653. returns nil."
  10654. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10655. (when time
  10656. (apply 'encode-time (org-parse-time-string time)))))
  10657. (defun org-get-deadline-time (pom &optional inherit)
  10658. "Get the deadline as a time tuple, of a format suitable for
  10659. calling org-deadline with, or if there is no scheduling, returns
  10660. nil."
  10661. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10662. (when time
  10663. (apply 'encode-time (org-parse-time-string time)))))
  10664. (defun org-remove-timestamp-with-keyword (keyword)
  10665. "Remove all time stamps with KEYWORD in the current entry."
  10666. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10667. beg)
  10668. (save-excursion
  10669. (org-back-to-heading t)
  10670. (setq beg (point))
  10671. (outline-next-heading)
  10672. (while (re-search-backward re beg t)
  10673. (replace-match "")
  10674. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10675. (equal (char-before) ?\ ))
  10676. (backward-delete-char 1)
  10677. (if (string-match "^[ \t]*$" (buffer-substring
  10678. (point-at-bol) (point-at-eol)))
  10679. (delete-region (point-at-bol)
  10680. (min (point-max) (1+ (point-at-eol))))))))))
  10681. (defun org-add-planning-info (what &optional time &rest remove)
  10682. "Insert new timestamp with keyword in the line directly after the headline.
  10683. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10684. If non is given, the user is prompted for a date.
  10685. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10686. be removed."
  10687. (interactive)
  10688. (let (org-time-was-given org-end-time-was-given ts
  10689. end default-time default-input)
  10690. (catch 'exit
  10691. (when (and (not time) (memq what '(scheduled deadline)))
  10692. ;; Try to get a default date/time from existing timestamp
  10693. (save-excursion
  10694. (org-back-to-heading t)
  10695. (setq end (save-excursion (outline-next-heading) (point)))
  10696. (when (re-search-forward (if (eq what 'scheduled)
  10697. org-scheduled-time-regexp
  10698. org-deadline-time-regexp)
  10699. end t)
  10700. (setq ts (match-string 1)
  10701. default-time
  10702. (apply 'encode-time (org-parse-time-string ts))
  10703. default-input (and ts (org-get-compact-tod ts))))))
  10704. (when what
  10705. ;; If necessary, get the time from the user
  10706. (setq time (or time (org-read-date nil 'to-time nil nil
  10707. default-time default-input))))
  10708. (when (and org-insert-labeled-timestamps-at-point
  10709. (member what '(scheduled deadline)))
  10710. (insert
  10711. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10712. (org-insert-time-stamp time org-time-was-given
  10713. nil nil nil (list org-end-time-was-given))
  10714. (setq what nil))
  10715. (save-excursion
  10716. (save-restriction
  10717. (let (col list elt ts buffer-invisibility-spec)
  10718. (org-back-to-heading t)
  10719. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10720. (goto-char (match-end 1))
  10721. (setq col (current-column))
  10722. (goto-char (match-end 0))
  10723. (if (eobp) (insert "\n") (forward-char 1))
  10724. (when (and (not what)
  10725. (not (looking-at
  10726. (concat "[ \t]*"
  10727. org-keyword-time-not-clock-regexp))))
  10728. ;; Nothing to add, nothing to remove...... :-)
  10729. (throw 'exit nil))
  10730. (if (and (not (looking-at outline-regexp))
  10731. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10732. "[^\r\n]*"))
  10733. (not (equal (match-string 1) org-clock-string)))
  10734. (narrow-to-region (match-beginning 0) (match-end 0))
  10735. (insert-before-markers "\n")
  10736. (backward-char 1)
  10737. (narrow-to-region (point) (point))
  10738. (and org-adapt-indentation (org-indent-to-column col)))
  10739. ;; Check if we have to remove something.
  10740. (setq list (cons what remove))
  10741. (while list
  10742. (setq elt (pop list))
  10743. (goto-char (point-min))
  10744. (when (or (and (eq elt 'scheduled)
  10745. (re-search-forward org-scheduled-time-regexp nil t))
  10746. (and (eq elt 'deadline)
  10747. (re-search-forward org-deadline-time-regexp nil t))
  10748. (and (eq elt 'closed)
  10749. (re-search-forward org-closed-time-regexp nil t)))
  10750. (replace-match "")
  10751. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10752. (skip-chars-backward " ")
  10753. (if (looking-at " +") (replace-match ""))))
  10754. (goto-char (point-max))
  10755. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10756. (when what
  10757. (insert
  10758. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10759. (cond ((eq what 'scheduled) org-scheduled-string)
  10760. ((eq what 'deadline) org-deadline-string)
  10761. ((eq what 'closed) org-closed-string))
  10762. " ")
  10763. (setq ts (org-insert-time-stamp
  10764. time
  10765. (or org-time-was-given
  10766. (and (eq what 'closed) org-log-done-with-time))
  10767. (eq what 'closed)
  10768. nil nil (list org-end-time-was-given)))
  10769. (end-of-line 1))
  10770. (goto-char (point-min))
  10771. (widen)
  10772. (if (and (looking-at "[ \t]*\n")
  10773. (equal (char-before) ?\n))
  10774. (delete-region (1- (point)) (point-at-eol)))
  10775. ts))))))
  10776. (defvar org-log-note-marker (make-marker))
  10777. (defvar org-log-note-purpose nil)
  10778. (defvar org-log-note-state nil)
  10779. (defvar org-log-note-previous-state nil)
  10780. (defvar org-log-note-how nil)
  10781. (defvar org-log-note-extra nil)
  10782. (defvar org-log-note-window-configuration nil)
  10783. (defvar org-log-note-return-to (make-marker))
  10784. (defvar org-log-post-message nil
  10785. "Message to be displayed after a log note has been stored.
  10786. The auto-repeater uses this.")
  10787. (defun org-add-note ()
  10788. "Add a note to the current entry.
  10789. This is done in the same way as adding a state change note."
  10790. (interactive)
  10791. (org-add-log-setup 'note nil nil 'findpos nil))
  10792. (defvar org-property-end-re)
  10793. (defun org-add-log-setup (&optional purpose state prev-state
  10794. findpos how extra)
  10795. "Set up the post command hook to take a note.
  10796. If this is about to TODO state change, the new state is expected in STATE.
  10797. When FINDPOS is non-nil, find the correct position for the note in
  10798. the current entry. If not, assume that it can be inserted at point.
  10799. HOW is an indicator what kind of note should be created.
  10800. EXTRA is additional text that will be inserted into the notes buffer."
  10801. (let* ((org-log-into-drawer (org-log-into-drawer))
  10802. (drawer (cond ((stringp org-log-into-drawer)
  10803. org-log-into-drawer)
  10804. (org-log-into-drawer "LOGBOOK")
  10805. (t nil))))
  10806. (save-restriction
  10807. (save-excursion
  10808. (when findpos
  10809. (org-back-to-heading t)
  10810. (narrow-to-region (point) (save-excursion
  10811. (outline-next-heading) (point)))
  10812. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10813. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10814. "[^\r\n]*\\)?"))
  10815. (goto-char (match-end 0))
  10816. (cond
  10817. (drawer
  10818. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10819. nil t)
  10820. (progn
  10821. (goto-char (match-end 0))
  10822. (or org-log-states-order-reversed
  10823. (and (re-search-forward org-property-end-re nil t)
  10824. (goto-char (1- (match-beginning 0))))))
  10825. (insert "\n:" drawer ":\n:END:")
  10826. (beginning-of-line 0)
  10827. (org-indent-line-function)
  10828. (beginning-of-line 2)
  10829. (org-indent-line-function)
  10830. (end-of-line 0)))
  10831. ((and org-log-state-notes-insert-after-drawers
  10832. (save-excursion
  10833. (forward-line) (looking-at org-drawer-regexp)))
  10834. (forward-line)
  10835. (while (looking-at org-drawer-regexp)
  10836. (goto-char (match-end 0))
  10837. (re-search-forward org-property-end-re (point-max) t)
  10838. (forward-line))
  10839. (forward-line -1)))
  10840. (unless org-log-states-order-reversed
  10841. (and (= (char-after) ?\n) (forward-char 1))
  10842. (org-skip-over-state-notes)
  10843. (skip-chars-backward " \t\n\r")))
  10844. (move-marker org-log-note-marker (point))
  10845. (setq org-log-note-purpose purpose
  10846. org-log-note-state state
  10847. org-log-note-previous-state prev-state
  10848. org-log-note-how how
  10849. org-log-note-extra extra)
  10850. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10851. (defun org-skip-over-state-notes ()
  10852. "Skip past the list of State notes in an entry."
  10853. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10854. (when (ignore-errors (goto-char (org-in-item-p)))
  10855. (let* ((struct (org-list-struct))
  10856. (prevs (org-list-prevs-alist struct)))
  10857. (while (looking-at "[ \t]*- State")
  10858. (goto-char (or (org-list-get-next-item (point) struct prevs)
  10859. (org-list-get-item-end (point) struct)))))))
  10860. (defun org-add-log-note (&optional purpose)
  10861. "Pop up a window for taking a note, and add this note later at point."
  10862. (remove-hook 'post-command-hook 'org-add-log-note)
  10863. (setq org-log-note-window-configuration (current-window-configuration))
  10864. (delete-other-windows)
  10865. (move-marker org-log-note-return-to (point))
  10866. (switch-to-buffer (marker-buffer org-log-note-marker))
  10867. (goto-char org-log-note-marker)
  10868. (org-switch-to-buffer-other-window "*Org Note*")
  10869. (erase-buffer)
  10870. (if (memq org-log-note-how '(time state))
  10871. (let (current-prefix-arg) (org-store-log-note))
  10872. (let ((org-inhibit-startup t)) (org-mode))
  10873. (insert (format "# Insert note for %s.
  10874. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10875. (cond
  10876. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10877. ((eq org-log-note-purpose 'done) "closed todo item")
  10878. ((eq org-log-note-purpose 'state)
  10879. (format "state change from \"%s\" to \"%s\""
  10880. (or org-log-note-previous-state "")
  10881. (or org-log-note-state "")))
  10882. ((eq org-log-note-purpose 'reschedule)
  10883. "rescheduling")
  10884. ((eq org-log-note-purpose 'delschedule)
  10885. "no longer scheduled")
  10886. ((eq org-log-note-purpose 'redeadline)
  10887. "changing deadline")
  10888. ((eq org-log-note-purpose 'deldeadline)
  10889. "removing deadline")
  10890. ((eq org-log-note-purpose 'refile)
  10891. "refiling")
  10892. ((eq org-log-note-purpose 'note)
  10893. "this entry")
  10894. (t (error "This should not happen")))))
  10895. (if org-log-note-extra (insert org-log-note-extra))
  10896. (org-set-local 'org-finish-function 'org-store-log-note)))
  10897. (defvar org-note-abort nil) ; dynamically scoped
  10898. (defun org-store-log-note ()
  10899. "Finish taking a log note, and insert it to where it belongs."
  10900. (let ((txt (buffer-string))
  10901. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10902. lines ind bul)
  10903. (kill-buffer (current-buffer))
  10904. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10905. (setq txt (replace-match "" t t txt)))
  10906. (if (string-match "\\s-+\\'" txt)
  10907. (setq txt (replace-match "" t t txt)))
  10908. (setq lines (org-split-string txt "\n"))
  10909. (when (and note (string-match "\\S-" note))
  10910. (setq note
  10911. (org-replace-escapes
  10912. note
  10913. (list (cons "%u" (user-login-name))
  10914. (cons "%U" user-full-name)
  10915. (cons "%t" (format-time-string
  10916. (org-time-stamp-format 'long 'inactive)
  10917. (current-time)))
  10918. (cons "%T" (format-time-string
  10919. (org-time-stamp-format 'long nil)
  10920. (current-time)))
  10921. (cons "%s" (if org-log-note-state
  10922. (concat "\"" org-log-note-state "\"")
  10923. ""))
  10924. (cons "%S" (if org-log-note-previous-state
  10925. (concat "\"" org-log-note-previous-state "\"")
  10926. "\"\"")))))
  10927. (if lines (setq note (concat note " \\\\")))
  10928. (push note lines))
  10929. (when (or current-prefix-arg org-note-abort)
  10930. (when org-log-into-drawer
  10931. (org-remove-empty-drawer-at
  10932. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10933. org-log-note-marker))
  10934. (setq lines nil))
  10935. (when lines
  10936. (with-current-buffer (marker-buffer org-log-note-marker)
  10937. (save-excursion
  10938. (goto-char org-log-note-marker)
  10939. (move-marker org-log-note-marker nil)
  10940. (end-of-line 1)
  10941. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10942. (setq ind (save-excursion
  10943. (if (ignore-errors (goto-char (org-in-item-p)))
  10944. (let ((struct (org-list-struct)))
  10945. (org-list-get-ind
  10946. (org-list-get-top-point struct) struct))
  10947. (skip-chars-backward " \r\t\n")
  10948. (cond
  10949. ((and (org-at-heading-p)
  10950. org-adapt-indentation)
  10951. (1+ (org-current-level)))
  10952. ((org-at-heading-p) 0)
  10953. (t (org-get-indentation))))))
  10954. (setq bul (org-list-bullet-string "-"))
  10955. (org-indent-line-to ind)
  10956. (insert bul (pop lines))
  10957. (let ((ind-body (+ (length bul) ind)))
  10958. (while lines
  10959. (insert "\n")
  10960. (org-indent-line-to ind-body)
  10961. (insert (pop lines))))
  10962. (message "Note stored")
  10963. (org-back-to-heading t)
  10964. (org-cycle-hide-drawers 'children)))))
  10965. (set-window-configuration org-log-note-window-configuration)
  10966. (with-current-buffer (marker-buffer org-log-note-return-to)
  10967. (goto-char org-log-note-return-to))
  10968. (move-marker org-log-note-return-to nil)
  10969. (and org-log-post-message (message "%s" org-log-post-message)))
  10970. (defun org-remove-empty-drawer-at (drawer pos)
  10971. "Remove an empty drawer DRAWER at position POS.
  10972. POS may also be a marker."
  10973. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10974. (save-excursion
  10975. (save-restriction
  10976. (widen)
  10977. (goto-char pos)
  10978. (if (org-in-regexp
  10979. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10980. (replace-match ""))))))
  10981. (defun org-sparse-tree (&optional arg)
  10982. "Create a sparse tree, prompt for the details.
  10983. This command can create sparse trees. You first need to select the type
  10984. of match used to create the tree:
  10985. t Show all TODO entries.
  10986. T Show entries with a specific TODO keyword.
  10987. m Show entries selected by a tags/property match.
  10988. p Enter a property name and its value (both with completion on existing
  10989. names/values) and show entries with that property.
  10990. r Show entries matching a regular expression (`/' can be used as well)
  10991. d Show deadlines due within `org-deadline-warning-days'.
  10992. b Show deadlines and scheduled items before a date.
  10993. a Show deadlines and scheduled items after a date."
  10994. (interactive "P")
  10995. (let (ans kwd value)
  10996. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date")
  10997. (setq ans (read-char-exclusive))
  10998. (cond
  10999. ((equal ans ?d)
  11000. (call-interactively 'org-check-deadlines))
  11001. ((equal ans ?b)
  11002. (call-interactively 'org-check-before-date))
  11003. ((equal ans ?a)
  11004. (call-interactively 'org-check-after-date))
  11005. ((equal ans ?t)
  11006. (org-show-todo-tree nil))
  11007. ((equal ans ?T)
  11008. (org-show-todo-tree '(4)))
  11009. ((member ans '(?T ?m))
  11010. (call-interactively 'org-match-sparse-tree))
  11011. ((member ans '(?p ?P))
  11012. (setq kwd (org-icompleting-read "Property: "
  11013. (mapcar 'list (org-buffer-property-keys))))
  11014. (setq value (org-icompleting-read "Value: "
  11015. (mapcar 'list (org-property-values kwd))))
  11016. (unless (string-match "\\`{.*}\\'" value)
  11017. (setq value (concat "\"" value "\"")))
  11018. (org-match-sparse-tree arg (concat kwd "=" value)))
  11019. ((member ans '(?r ?R ?/))
  11020. (call-interactively 'org-occur))
  11021. (t (error "No such sparse tree command \"%c\"" ans)))))
  11022. (defvar org-occur-highlights nil
  11023. "List of overlays used for occur matches.")
  11024. (make-variable-buffer-local 'org-occur-highlights)
  11025. (defvar org-occur-parameters nil
  11026. "Parameters of the active org-occur calls.
  11027. This is a list, each call to org-occur pushes as cons cell,
  11028. containing the regular expression and the callback, onto the list.
  11029. The list can contain several entries if `org-occur' has been called
  11030. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11031. will only contain one set of parameters. When the highlights are
  11032. removed (for example with `C-c C-c', or with the next edit (depending
  11033. on `org-remove-highlights-with-change'), this variable is emptied
  11034. as well.")
  11035. (make-variable-buffer-local 'org-occur-parameters)
  11036. (defun org-occur (regexp &optional keep-previous callback)
  11037. "Make a compact tree which shows all matches of REGEXP.
  11038. The tree will show the lines where the regexp matches, and all higher
  11039. headlines above the match. It will also show the heading after the match,
  11040. to make sure editing the matching entry is easy.
  11041. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11042. call to `org-occur' will be kept, to allow stacking of calls to this
  11043. command.
  11044. If CALLBACK is non-nil, it is a function which is called to confirm
  11045. that the match should indeed be shown."
  11046. (interactive "sRegexp: \nP")
  11047. (when (equal regexp "")
  11048. (error "Regexp cannot be empty"))
  11049. (unless keep-previous
  11050. (org-remove-occur-highlights nil nil t))
  11051. (push (cons regexp callback) org-occur-parameters)
  11052. (let ((cnt 0))
  11053. (save-excursion
  11054. (goto-char (point-min))
  11055. (if (or (not keep-previous) ; do not want to keep
  11056. (not org-occur-highlights)) ; no previous matches
  11057. ;; hide everything
  11058. (org-overview))
  11059. (while (re-search-forward regexp nil t)
  11060. (when (or (not callback)
  11061. (save-match-data (funcall callback)))
  11062. (setq cnt (1+ cnt))
  11063. (when org-highlight-sparse-tree-matches
  11064. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11065. (org-show-context 'occur-tree))))
  11066. (when org-remove-highlights-with-change
  11067. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11068. nil 'local))
  11069. (unless org-sparse-tree-open-archived-trees
  11070. (org-hide-archived-subtrees (point-min) (point-max)))
  11071. (run-hooks 'org-occur-hook)
  11072. (if (org-called-interactively-p 'interactive)
  11073. (message "%d match(es) for regexp %s" cnt regexp))
  11074. cnt))
  11075. (defun org-occur-next-match (&optional n reset)
  11076. "Function for `next-error-function' to find sparse tree matches.
  11077. N is the number of matches to move, when negative move backwards.
  11078. RESET is entirely ignored - this function always goes back to the
  11079. starting point when no match is found."
  11080. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11081. (search-func (if (< n 0)
  11082. 'previous-single-char-property-change
  11083. 'next-single-char-property-change))
  11084. (n (abs n))
  11085. (pos (point))
  11086. p1)
  11087. (catch 'exit
  11088. (while (setq p1 (funcall search-func (point) 'org-type))
  11089. (when (equal p1 limit)
  11090. (goto-char pos)
  11091. (error "No more matches"))
  11092. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11093. (setq n (1- n))
  11094. (when (= n 0)
  11095. (goto-char p1)
  11096. (throw 'exit (point))))
  11097. (goto-char p1))
  11098. (goto-char p1)
  11099. (error "No more matches"))))
  11100. (defun org-show-context (&optional key)
  11101. "Make sure point and context are visible.
  11102. How much context is shown depends upon the variables
  11103. `org-show-hierarchy-above', `org-show-following-heading'. and
  11104. `org-show-siblings'."
  11105. (let ((heading-p (org-on-heading-p t))
  11106. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11107. (following-p (org-get-alist-option org-show-following-heading key))
  11108. (entry-p (org-get-alist-option org-show-entry-below key))
  11109. (siblings-p (org-get-alist-option org-show-siblings key)))
  11110. (catch 'exit
  11111. ;; Show heading or entry text
  11112. (if (and heading-p (not entry-p))
  11113. (org-flag-heading nil) ; only show the heading
  11114. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11115. (org-show-hidden-entry))) ; show entire entry
  11116. (when following-p
  11117. ;; Show next sibling, or heading below text
  11118. (save-excursion
  11119. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11120. (org-flag-heading nil))))
  11121. (when siblings-p (org-show-siblings))
  11122. (when hierarchy-p
  11123. ;; show all higher headings, possibly with siblings
  11124. (save-excursion
  11125. (while (and (condition-case nil
  11126. (progn (org-up-heading-all 1) t)
  11127. (error nil))
  11128. (not (bobp)))
  11129. (org-flag-heading nil)
  11130. (when siblings-p (org-show-siblings))))))))
  11131. (defvar org-reveal-start-hook nil
  11132. "Hook run before revealing a location.")
  11133. (defun org-reveal (&optional siblings)
  11134. "Show current entry, hierarchy above it, and the following headline.
  11135. This can be used to show a consistent set of context around locations
  11136. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11137. not t for the search context.
  11138. With optional argument SIBLINGS, on each level of the hierarchy all
  11139. siblings are shown. This repairs the tree structure to what it would
  11140. look like when opened with hierarchical calls to `org-cycle'.
  11141. With double optional argument \\[universal-argument] \\[universal-argument], \
  11142. go to the parent and show the
  11143. entire tree."
  11144. (interactive "P")
  11145. (run-hooks 'org-reveal-start-hook)
  11146. (let ((org-show-hierarchy-above t)
  11147. (org-show-following-heading t)
  11148. (org-show-siblings (if siblings t org-show-siblings)))
  11149. (org-show-context nil))
  11150. (when (equal siblings '(16))
  11151. (save-excursion
  11152. (when (org-up-heading-safe)
  11153. (org-show-subtree)
  11154. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11155. (defun org-highlight-new-match (beg end)
  11156. "Highlight from BEG to END and mark the highlight is an occur headline."
  11157. (let ((ov (make-overlay beg end)))
  11158. (overlay-put ov 'face 'secondary-selection)
  11159. (overlay-put ov 'org-type 'org-occur)
  11160. (push ov org-occur-highlights)))
  11161. (defun org-remove-occur-highlights (&optional beg end noremove)
  11162. "Remove the occur highlights from the buffer.
  11163. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11164. from the `before-change-functions' in the current buffer."
  11165. (interactive)
  11166. (unless org-inhibit-highlight-removal
  11167. (mapc 'delete-overlay org-occur-highlights)
  11168. (setq org-occur-highlights nil)
  11169. (setq org-occur-parameters nil)
  11170. (unless noremove
  11171. (remove-hook 'before-change-functions
  11172. 'org-remove-occur-highlights 'local))))
  11173. ;;;; Priorities
  11174. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11175. "Regular expression matching the priority indicator.")
  11176. (defvar org-remove-priority-next-time nil)
  11177. (defun org-priority-up ()
  11178. "Increase the priority of the current item."
  11179. (interactive)
  11180. (org-priority 'up))
  11181. (defun org-priority-down ()
  11182. "Decrease the priority of the current item."
  11183. (interactive)
  11184. (org-priority 'down))
  11185. (defun org-priority (&optional action)
  11186. "Change the priority of an item by ARG.
  11187. ACTION can be `set', `up', `down', or a character."
  11188. (interactive)
  11189. (unless org-enable-priority-commands
  11190. (error "Priority commands are disabled"))
  11191. (setq action (or action 'set))
  11192. (let (current new news have remove)
  11193. (save-excursion
  11194. (org-back-to-heading t)
  11195. (if (looking-at org-priority-regexp)
  11196. (setq current (string-to-char (match-string 2))
  11197. have t))
  11198. (cond
  11199. ((eq action 'remove)
  11200. (setq remove t new ?\ ))
  11201. ((or (eq action 'set)
  11202. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11203. (if (not (eq action 'set))
  11204. (setq new action)
  11205. (message "Priority %c-%c, SPC to remove: "
  11206. org-highest-priority org-lowest-priority)
  11207. (save-match-data
  11208. (setq new (read-char-exclusive))))
  11209. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11210. (= (upcase org-lowest-priority) org-lowest-priority))
  11211. (setq new (upcase new)))
  11212. (cond ((equal new ?\ ) (setq remove t))
  11213. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11214. (error "Priority must be between `%c' and `%c'"
  11215. org-highest-priority org-lowest-priority))))
  11216. ((eq action 'up)
  11217. (setq new (if have
  11218. (1- current) ; normal cycling
  11219. ;; last priority was empty
  11220. (if (eq last-command this-command)
  11221. org-lowest-priority ; wrap around empty to lowest
  11222. ;; default
  11223. (if org-priority-start-cycle-with-default
  11224. org-default-priority
  11225. (1- org-default-priority))))))
  11226. ((eq action 'down)
  11227. (setq new (if have
  11228. (1+ current) ; normal cycling
  11229. ;; last priority was empty
  11230. (if (eq last-command this-command)
  11231. org-highest-priority ; wrap around empty to highest
  11232. ;; default
  11233. (if org-priority-start-cycle-with-default
  11234. org-default-priority
  11235. (1+ org-default-priority))))))
  11236. (t (error "Invalid action")))
  11237. (if (or (< (upcase new) org-highest-priority)
  11238. (> (upcase new) org-lowest-priority))
  11239. (if (and (memq action '(up down))
  11240. (not have) (not (eq last-command this-command)))
  11241. ;; `new' is from default priority
  11242. (error
  11243. "The default can not be set, see `org-default-priority' why")
  11244. ;; normal cycling: `new' is beyond highest/lowest priority
  11245. ;; and is wrapped around to the empty priority
  11246. (setq remove t)))
  11247. (setq news (format "%c" new))
  11248. (if have
  11249. (if remove
  11250. (replace-match "" t t nil 1)
  11251. (replace-match news t t nil 2))
  11252. (if remove
  11253. (error "No priority cookie found in line")
  11254. (let ((case-fold-search nil))
  11255. (looking-at org-todo-line-regexp))
  11256. (if (match-end 2)
  11257. (progn
  11258. (goto-char (match-end 2))
  11259. (insert " [#" news "]"))
  11260. (goto-char (match-beginning 3))
  11261. (insert "[#" news "] "))))
  11262. (org-preserve-lc (org-set-tags nil 'align)))
  11263. (if remove
  11264. (message "Priority removed")
  11265. (message "Priority of current item set to %s" news))))
  11266. (defun org-get-priority (s)
  11267. "Find priority cookie and return priority."
  11268. (if (functionp org-get-priority-function)
  11269. (funcall org-get-priority-function)
  11270. (save-match-data
  11271. (if (not (string-match org-priority-regexp s))
  11272. (* 1000 (- org-lowest-priority org-default-priority))
  11273. (* 1000 (- org-lowest-priority
  11274. (string-to-char (match-string 2 s))))))))
  11275. ;;;; Tags
  11276. (defvar org-agenda-archives-mode)
  11277. (defvar org-map-continue-from nil
  11278. "Position from where mapping should continue.
  11279. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11280. (defvar org-scanner-tags nil
  11281. "The current tag list while the tags scanner is running.")
  11282. (defvar org-trust-scanner-tags nil
  11283. "Should `org-get-tags-at' use the tags for the scanner.
  11284. This is for internal dynamical scoping only.
  11285. When this is non-nil, the function `org-get-tags-at' will return the value
  11286. of `org-scanner-tags' instead of building the list by itself. This
  11287. can lead to large speed-ups when the tags scanner is used in a file with
  11288. many entries, and when the list of tags is retrieved, for example to
  11289. obtain a list of properties. Building the tags list for each entry in such
  11290. a file becomes an N^2 operation - but with this variable set, it scales
  11291. as N.")
  11292. (defun org-scan-tags (action matcher &optional todo-only)
  11293. "Scan headline tags with inheritance and produce output ACTION.
  11294. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11295. or `agenda' to produce an entry list for an agenda view. It can also be
  11296. a Lisp form or a function that should be called at each matched headline, in
  11297. this case the return value is a list of all return values from these calls.
  11298. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11299. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11300. only lines with a TODO keyword are included in the output."
  11301. (require 'org-agenda)
  11302. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  11303. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11304. (org-re
  11305. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11306. (props (list 'face 'default
  11307. 'done-face 'org-agenda-done
  11308. 'undone-face 'default
  11309. 'mouse-face 'highlight
  11310. 'org-not-done-regexp org-not-done-regexp
  11311. 'org-todo-regexp org-todo-regexp
  11312. 'help-echo
  11313. (format "mouse-2 or RET jump to org file %s"
  11314. (abbreviate-file-name
  11315. (or (buffer-file-name (buffer-base-buffer))
  11316. (buffer-name (buffer-base-buffer)))))))
  11317. (case-fold-search nil)
  11318. (org-map-continue-from nil)
  11319. lspos tags tags-list
  11320. (tags-alist (list (cons 0 org-file-tags)))
  11321. (llast 0) rtn rtn1 level category i txt
  11322. todo marker entry priority)
  11323. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11324. (setq action (list 'lambda nil action)))
  11325. (save-excursion
  11326. (goto-char (point-min))
  11327. (when (eq action 'sparse-tree)
  11328. (org-overview)
  11329. (org-remove-occur-highlights))
  11330. (while (re-search-forward re nil t)
  11331. (catch :skip
  11332. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11333. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11334. (goto-char (setq lspos (match-beginning 0)))
  11335. (setq level (org-reduced-level (funcall outline-level))
  11336. category (org-get-category))
  11337. (setq i llast llast level)
  11338. ;; remove tag lists from same and sublevels
  11339. (while (>= i level)
  11340. (when (setq entry (assoc i tags-alist))
  11341. (setq tags-alist (delete entry tags-alist)))
  11342. (setq i (1- i)))
  11343. ;; add the next tags
  11344. (when tags
  11345. (setq tags (org-split-string tags ":")
  11346. tags-alist
  11347. (cons (cons level tags) tags-alist)))
  11348. ;; compile tags for current headline
  11349. (setq tags-list
  11350. (if org-use-tag-inheritance
  11351. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11352. tags)
  11353. org-scanner-tags tags-list)
  11354. (when org-use-tag-inheritance
  11355. (setcdr (car tags-alist)
  11356. (mapcar (lambda (x)
  11357. (setq x (copy-sequence x))
  11358. (org-add-prop-inherited x))
  11359. (cdar tags-alist))))
  11360. (when (and tags org-use-tag-inheritance
  11361. (or (not (eq t org-use-tag-inheritance))
  11362. org-tags-exclude-from-inheritance))
  11363. ;; selective inheritance, remove uninherited ones
  11364. (setcdr (car tags-alist)
  11365. (org-remove-uninherited-tags (cdar tags-alist))))
  11366. (when (and
  11367. ;; eval matcher only when the todo condition is OK
  11368. (and (or (not todo-only) (member todo org-not-done-keywords))
  11369. (let ((case-fold-search t)) (eval matcher)))
  11370. ;; Call the skipper, but return t if it does not skip,
  11371. ;; so that the `and' form continues evaluating
  11372. (progn
  11373. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11374. t)
  11375. ;; Check if timestamps are deselecting this entry
  11376. (or (not todo-only)
  11377. (and (member todo org-not-done-keywords)
  11378. (or (not org-agenda-tags-todo-honor-ignore-options)
  11379. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11380. ;; Extra check for the archive tag
  11381. ;; FIXME: Does the skipper already do this????
  11382. (or
  11383. (not (member org-archive-tag tags-list))
  11384. ;; we have an archive tag, should we use this anyway?
  11385. (or (not org-agenda-skip-archived-trees)
  11386. (and (eq action 'agenda) org-agenda-archives-mode))))
  11387. ;; select this headline
  11388. (cond
  11389. ((eq action 'sparse-tree)
  11390. (and org-highlight-sparse-tree-matches
  11391. (org-get-heading) (match-end 0)
  11392. (org-highlight-new-match
  11393. (match-beginning 0) (match-beginning 1)))
  11394. (org-show-context 'tags-tree))
  11395. ((eq action 'agenda)
  11396. (setq txt (org-format-agenda-item
  11397. ""
  11398. (concat
  11399. (if (eq org-tags-match-list-sublevels 'indented)
  11400. (make-string (1- level) ?.) "")
  11401. (org-get-heading))
  11402. category
  11403. tags-list
  11404. )
  11405. priority (org-get-priority txt))
  11406. (goto-char lspos)
  11407. (setq marker (org-agenda-new-marker))
  11408. (org-add-props txt props
  11409. 'org-marker marker 'org-hd-marker marker 'org-category category
  11410. 'todo-state todo
  11411. 'priority priority 'type "tagsmatch")
  11412. (push txt rtn))
  11413. ((functionp action)
  11414. (setq org-map-continue-from nil)
  11415. (save-excursion
  11416. (setq rtn1 (funcall action))
  11417. (push rtn1 rtn)))
  11418. (t (error "Invalid action")))
  11419. ;; if we are to skip sublevels, jump to end of subtree
  11420. (unless org-tags-match-list-sublevels
  11421. (org-end-of-subtree t)
  11422. (backward-char 1))))
  11423. ;; Get the correct position from where to continue
  11424. (if org-map-continue-from
  11425. (goto-char org-map-continue-from)
  11426. (and (= (point) lspos) (end-of-line 1)))))
  11427. (when (and (eq action 'sparse-tree)
  11428. (not org-sparse-tree-open-archived-trees))
  11429. (org-hide-archived-subtrees (point-min) (point-max)))
  11430. (nreverse rtn)))
  11431. (defun org-remove-uninherited-tags (tags)
  11432. "Remove all tags that are not inherited from the list TAGS."
  11433. (cond
  11434. ((eq org-use-tag-inheritance t)
  11435. (if org-tags-exclude-from-inheritance
  11436. (org-delete-all org-tags-exclude-from-inheritance tags)
  11437. tags))
  11438. ((not org-use-tag-inheritance) nil)
  11439. ((stringp org-use-tag-inheritance)
  11440. (delq nil (mapcar
  11441. (lambda (x)
  11442. (if (and (string-match org-use-tag-inheritance x)
  11443. (not (member x org-tags-exclude-from-inheritance)))
  11444. x nil))
  11445. tags)))
  11446. ((listp org-use-tag-inheritance)
  11447. (delq nil (mapcar
  11448. (lambda (x)
  11449. (if (member x org-use-tag-inheritance) x nil))
  11450. tags)))))
  11451. (defvar todo-only) ;; dynamically scoped
  11452. (defun org-match-sparse-tree (&optional todo-only match)
  11453. "Create a sparse tree according to tags string MATCH.
  11454. MATCH can contain positive and negative selection of tags, like
  11455. \"+WORK+URGENT-WITHBOSS\".
  11456. If optional argument TODO-ONLY is non-nil, only select lines that are
  11457. also TODO lines."
  11458. (interactive "P")
  11459. (org-prepare-agenda-buffers (list (current-buffer)))
  11460. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11461. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11462. (defvar org-cached-props nil)
  11463. (defun org-cached-entry-get (pom property)
  11464. (if (or (eq t org-use-property-inheritance)
  11465. (and (stringp org-use-property-inheritance)
  11466. (string-match org-use-property-inheritance property))
  11467. (and (listp org-use-property-inheritance)
  11468. (member property org-use-property-inheritance)))
  11469. ;; Caching is not possible, check it directly
  11470. (org-entry-get pom property 'inherit)
  11471. ;; Get all properties, so that we can do complicated checks easily
  11472. (cdr (assoc property (or org-cached-props
  11473. (setq org-cached-props
  11474. (org-entry-properties pom)))))))
  11475. (defun org-global-tags-completion-table (&optional files)
  11476. "Return the list of all tags in all agenda buffer/files.
  11477. Optional FILES argument is a list of files to which can be used
  11478. instead of the agenda files."
  11479. (save-excursion
  11480. (org-uniquify
  11481. (delq nil
  11482. (apply 'append
  11483. (mapcar
  11484. (lambda (file)
  11485. (set-buffer (find-file-noselect file))
  11486. (append (org-get-buffer-tags)
  11487. (mapcar (lambda (x) (if (stringp (car-safe x))
  11488. (list (car-safe x)) nil))
  11489. org-tag-alist)))
  11490. (if (and files (car files))
  11491. files
  11492. (org-agenda-files))))))))
  11493. (defun org-make-tags-matcher (match)
  11494. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11495. ;; todo-only is scoped dynamically into this function, and the function
  11496. ;; may change it if the matcher asks for it.
  11497. (unless match
  11498. ;; Get a new match request, with completion
  11499. (let ((org-last-tags-completion-table
  11500. (org-global-tags-completion-table)))
  11501. (setq match (org-completing-read-no-i
  11502. "Match: " 'org-tags-completion-function nil nil nil
  11503. 'org-tags-history))))
  11504. ;; Parse the string and create a lisp form
  11505. (let ((match0 match)
  11506. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11507. minus tag mm
  11508. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11509. orterms term orlist re-p str-p level-p level-op time-p
  11510. prop-p pn pv po gv rest)
  11511. (if (string-match "/+" match)
  11512. ;; match contains also a todo-matching request
  11513. (progn
  11514. (setq tagsmatch (substring match 0 (match-beginning 0))
  11515. todomatch (substring match (match-end 0)))
  11516. (if (string-match "^!" todomatch)
  11517. (setq todo-only t todomatch (substring todomatch 1)))
  11518. (if (string-match "^\\s-*$" todomatch)
  11519. (setq todomatch nil)))
  11520. ;; only matching tags
  11521. (setq tagsmatch match todomatch nil))
  11522. ;; Make the tags matcher
  11523. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11524. (setq tagsmatcher t)
  11525. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11526. (while (setq term (pop orterms))
  11527. (while (and (equal (substring term -1) "\\") orterms)
  11528. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11529. (while (string-match re term)
  11530. (setq rest (substring term (match-end 0))
  11531. minus (and (match-end 1)
  11532. (equal (match-string 1 term) "-"))
  11533. tag (save-match-data (replace-regexp-in-string
  11534. "\\\\-" "-"
  11535. (match-string 2 term)))
  11536. re-p (equal (string-to-char tag) ?{)
  11537. level-p (match-end 4)
  11538. prop-p (match-end 5)
  11539. mm (cond
  11540. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11541. (level-p
  11542. (setq level-op (org-op-to-function (match-string 3 term)))
  11543. `(,level-op level ,(string-to-number
  11544. (match-string 4 term))))
  11545. (prop-p
  11546. (setq pn (match-string 5 term)
  11547. po (match-string 6 term)
  11548. pv (match-string 7 term)
  11549. re-p (equal (string-to-char pv) ?{)
  11550. str-p (equal (string-to-char pv) ?\")
  11551. time-p (save-match-data
  11552. (string-match "^\"[[<].*[]>]\"$" pv))
  11553. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11554. (if time-p (setq pv (org-matcher-time pv)))
  11555. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11556. (cond
  11557. ((equal pn "CATEGORY")
  11558. (setq gv '(get-text-property (point) 'org-category)))
  11559. ((equal pn "TODO")
  11560. (setq gv 'todo))
  11561. (t
  11562. (setq gv `(org-cached-entry-get nil ,pn))))
  11563. (if re-p
  11564. (if (eq po 'org<>)
  11565. `(not (string-match ,pv (or ,gv "")))
  11566. `(string-match ,pv (or ,gv "")))
  11567. (if str-p
  11568. `(,po (or ,gv "") ,pv)
  11569. `(,po (string-to-number (or ,gv ""))
  11570. ,(string-to-number pv) ))))
  11571. (t `(member ,tag tags-list)))
  11572. mm (if minus (list 'not mm) mm)
  11573. term rest)
  11574. (push mm tagsmatcher))
  11575. (push (if (> (length tagsmatcher) 1)
  11576. (cons 'and tagsmatcher)
  11577. (car tagsmatcher))
  11578. orlist)
  11579. (setq tagsmatcher nil))
  11580. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11581. (setq tagsmatcher
  11582. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11583. ;; Make the todo matcher
  11584. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11585. (setq todomatcher t)
  11586. (setq orterms (org-split-string todomatch "|") orlist nil)
  11587. (while (setq term (pop orterms))
  11588. (while (string-match re term)
  11589. (setq minus (and (match-end 1)
  11590. (equal (match-string 1 term) "-"))
  11591. kwd (match-string 2 term)
  11592. re-p (equal (string-to-char kwd) ?{)
  11593. term (substring term (match-end 0))
  11594. mm (if re-p
  11595. `(string-match ,(substring kwd 1 -1) todo)
  11596. (list 'equal 'todo kwd))
  11597. mm (if minus (list 'not mm) mm))
  11598. (push mm todomatcher))
  11599. (push (if (> (length todomatcher) 1)
  11600. (cons 'and todomatcher)
  11601. (car todomatcher))
  11602. orlist)
  11603. (setq todomatcher nil))
  11604. (setq todomatcher (if (> (length orlist) 1)
  11605. (cons 'or orlist) (car orlist))))
  11606. ;; Return the string and lisp forms of the matcher
  11607. (setq matcher (if todomatcher
  11608. (list 'and tagsmatcher todomatcher)
  11609. tagsmatcher))
  11610. (cons match0 matcher)))
  11611. (defun org-op-to-function (op &optional stringp)
  11612. "Turn an operator into the appropriate function."
  11613. (setq op
  11614. (cond
  11615. ((equal op "<" ) '(< string< org-time<))
  11616. ((equal op ">" ) '(> org-string> org-time>))
  11617. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11618. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11619. ((member op '("=" "==")) '(= string= org-time=))
  11620. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11621. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11622. (defun org<> (a b) (not (= a b)))
  11623. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11624. (defun org-string>= (a b) (not (string< a b)))
  11625. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11626. (defun org-string<> (a b) (not (string= a b)))
  11627. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11628. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11629. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11630. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11631. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11632. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11633. (defun org-2ft (s)
  11634. "Convert S to a floating point time.
  11635. If S is already a number, just return it. If it is a string, parse
  11636. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11637. (cond
  11638. ((numberp s) s)
  11639. ((stringp s)
  11640. (condition-case nil
  11641. (float-time (apply 'encode-time (org-parse-time-string s)))
  11642. (error 0.)))
  11643. (t 0.)))
  11644. (defun org-time-today ()
  11645. "Time in seconds today at 0:00.
  11646. Returns the float number of seconds since the beginning of the
  11647. epoch to the beginning of today (00:00)."
  11648. (float-time (apply 'encode-time
  11649. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11650. (defun org-matcher-time (s)
  11651. "Interpret a time comparison value."
  11652. (save-match-data
  11653. (cond
  11654. ((string= s "<now>") (float-time))
  11655. ((string= s "<today>") (org-time-today))
  11656. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11657. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11658. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11659. (+ (org-time-today)
  11660. (* (string-to-number (match-string 1 s))
  11661. (cdr (assoc (match-string 2 s)
  11662. '(("d" . 86400.0) ("w" . 604800.0)
  11663. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11664. (t (org-2ft s)))))
  11665. (defun org-match-any-p (re list)
  11666. "Does re match any element of list?"
  11667. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11668. (delq nil list))
  11669. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11670. (defvar org-tags-overlay (make-overlay 1 1))
  11671. (org-detach-overlay org-tags-overlay)
  11672. (defun org-get-local-tags-at (&optional pos)
  11673. "Get a list of tags defined in the current headline."
  11674. (org-get-tags-at pos 'local))
  11675. (defun org-get-local-tags ()
  11676. "Get a list of tags defined in the current headline."
  11677. (org-get-tags-at nil 'local))
  11678. (defun org-get-tags-at (&optional pos local)
  11679. "Get a list of all headline tags applicable at POS.
  11680. POS defaults to point. If tags are inherited, the list contains
  11681. the targets in the same sequence as the headlines appear, i.e.
  11682. the tags of the current headline come last.
  11683. When LOCAL is non-nil, only return tags from the current headline,
  11684. ignore inherited ones."
  11685. (interactive)
  11686. (if (and org-trust-scanner-tags
  11687. (or (not pos) (equal pos (point)))
  11688. (not local))
  11689. org-scanner-tags
  11690. (let (tags ltags lastpos parent)
  11691. (save-excursion
  11692. (save-restriction
  11693. (widen)
  11694. (goto-char (or pos (point)))
  11695. (save-match-data
  11696. (catch 'done
  11697. (condition-case nil
  11698. (progn
  11699. (org-back-to-heading t)
  11700. (while (not (equal lastpos (point)))
  11701. (setq lastpos (point))
  11702. (when (looking-at
  11703. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  11704. (setq ltags (org-split-string
  11705. (org-match-string-no-properties 1) ":"))
  11706. (when parent
  11707. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11708. (setq tags (append
  11709. (if parent
  11710. (org-remove-uninherited-tags ltags)
  11711. ltags)
  11712. tags)))
  11713. (or org-use-tag-inheritance (throw 'done t))
  11714. (if local (throw 'done t))
  11715. (or (org-up-heading-safe) (error nil))
  11716. (setq parent t)))
  11717. (error nil)))))
  11718. (if local
  11719. tags
  11720. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  11721. (defun org-add-prop-inherited (s)
  11722. (add-text-properties 0 (length s) '(inherited t) s)
  11723. s)
  11724. (defun org-toggle-tag (tag &optional onoff)
  11725. "Toggle the tag TAG for the current line.
  11726. If ONOFF is `on' or `off', don't toggle but set to this state."
  11727. (let (res current)
  11728. (save-excursion
  11729. (org-back-to-heading t)
  11730. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  11731. (point-at-eol) t)
  11732. (progn
  11733. (setq current (match-string 1))
  11734. (replace-match ""))
  11735. (setq current ""))
  11736. (setq current (nreverse (org-split-string current ":")))
  11737. (cond
  11738. ((eq onoff 'on)
  11739. (setq res t)
  11740. (or (member tag current) (push tag current)))
  11741. ((eq onoff 'off)
  11742. (or (not (member tag current)) (setq current (delete tag current))))
  11743. (t (if (member tag current)
  11744. (setq current (delete tag current))
  11745. (setq res t)
  11746. (push tag current))))
  11747. (end-of-line 1)
  11748. (if current
  11749. (progn
  11750. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11751. (org-set-tags nil t))
  11752. (delete-horizontal-space))
  11753. (run-hooks 'org-after-tags-change-hook))
  11754. res))
  11755. (defun org-align-tags-here (to-col)
  11756. ;; Assumes that this is a headline
  11757. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11758. (beginning-of-line 1)
  11759. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11760. (< pos (match-beginning 2)))
  11761. (progn
  11762. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11763. (goto-char (match-beginning 1))
  11764. (insert " ")
  11765. (delete-region (point) (1+ (match-beginning 2)))
  11766. (setq ncol (max (1+ (current-column))
  11767. (1+ col)
  11768. (if (> to-col 0)
  11769. to-col
  11770. (- (abs to-col) tags-l))))
  11771. (setq p (point))
  11772. (insert (make-string (- ncol (current-column)) ?\ ))
  11773. (setq ncol (current-column))
  11774. (when indent-tabs-mode (tabify p (point-at-eol)))
  11775. (org-move-to-column (min ncol col) t))
  11776. (goto-char pos))))
  11777. (defun org-set-tags-command (&optional arg just-align)
  11778. "Call the set-tags command for the current entry."
  11779. (interactive "P")
  11780. (if (org-on-heading-p)
  11781. (org-set-tags arg just-align)
  11782. (save-excursion
  11783. (org-back-to-heading t)
  11784. (org-set-tags arg just-align))))
  11785. (defun org-set-tags-to (data)
  11786. "Set the tags of the current entry to DATA, replacing the current tags.
  11787. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11788. If DATA is nil or the empty string, any tags will be removed."
  11789. (interactive "sTags: ")
  11790. (setq data
  11791. (cond
  11792. ((eq data nil) "")
  11793. ((equal data "") "")
  11794. ((stringp data)
  11795. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11796. ":"))
  11797. ((listp data)
  11798. (concat ":" (mapconcat 'identity data ":") ":"))
  11799. (t nil)))
  11800. (when data
  11801. (save-excursion
  11802. (org-back-to-heading t)
  11803. (when (looking-at org-complex-heading-regexp)
  11804. (if (match-end 5)
  11805. (progn
  11806. (goto-char (match-beginning 5))
  11807. (insert data)
  11808. (delete-region (point) (point-at-eol))
  11809. (org-set-tags nil 'align))
  11810. (goto-char (point-at-eol))
  11811. (insert " " data)
  11812. (org-set-tags nil 'align)))
  11813. (beginning-of-line 1)
  11814. (if (looking-at ".*?\\([ \t]+\\)$")
  11815. (delete-region (match-beginning 1) (match-end 1))))))
  11816. (defun org-align-all-tags ()
  11817. "Align the tags i all headings."
  11818. (interactive)
  11819. (save-excursion
  11820. (or (ignore-errors (org-back-to-heading t))
  11821. (outline-next-heading))
  11822. (if (org-on-heading-p)
  11823. (org-set-tags t)
  11824. (message "No headings"))))
  11825. (defvar org-indent-indentation-per-level)
  11826. (defun org-set-tags (&optional arg just-align)
  11827. "Set the tags for the current headline.
  11828. With prefix ARG, realign all tags in headings in the current buffer."
  11829. (interactive "P")
  11830. (let* ((re (concat "^" outline-regexp))
  11831. (current (org-get-tags-string))
  11832. (col (current-column))
  11833. (org-setting-tags t)
  11834. table current-tags inherited-tags ; computed below when needed
  11835. tags p0 c0 c1 rpl di tc level)
  11836. (if arg
  11837. (save-excursion
  11838. (goto-char (point-min))
  11839. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11840. (while (re-search-forward re nil t)
  11841. (org-set-tags nil t)
  11842. (end-of-line 1)))
  11843. (message "All tags realigned to column %d" org-tags-column))
  11844. (if just-align
  11845. (setq tags current)
  11846. ;; Get a new set of tags from the user
  11847. (save-excursion
  11848. (setq table (append org-tag-persistent-alist
  11849. (or org-tag-alist (org-get-buffer-tags))
  11850. (and
  11851. org-complete-tags-always-offer-all-agenda-tags
  11852. (org-global-tags-completion-table
  11853. (org-agenda-files))))
  11854. org-last-tags-completion-table table
  11855. current-tags (org-split-string current ":")
  11856. inherited-tags (nreverse
  11857. (nthcdr (length current-tags)
  11858. (nreverse (org-get-tags-at))))
  11859. tags
  11860. (if (or (eq t org-use-fast-tag-selection)
  11861. (and org-use-fast-tag-selection
  11862. (delq nil (mapcar 'cdr table))))
  11863. (org-fast-tag-selection
  11864. current-tags inherited-tags table
  11865. (if org-fast-tag-selection-include-todo
  11866. org-todo-key-alist))
  11867. (let ((org-add-colon-after-tag-completion t))
  11868. (org-trim
  11869. (org-icompleting-read "Tags: "
  11870. 'org-tags-completion-function
  11871. nil nil current 'org-tags-history))))))
  11872. (while (string-match "[-+&]+" tags)
  11873. ;; No boolean logic, just a list
  11874. (setq tags (replace-match ":" t t tags))))
  11875. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  11876. (if org-tags-sort-function
  11877. (setq tags (mapconcat 'identity
  11878. (sort (org-split-string
  11879. tags (org-re "[^[:alnum:]_@#%]+"))
  11880. org-tags-sort-function) ":")))
  11881. (if (string-match "\\`[\t ]*\\'" tags)
  11882. (setq tags "")
  11883. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11884. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11885. ;; Insert new tags at the correct column
  11886. (beginning-of-line 1)
  11887. (setq level (or (and (looking-at org-outline-regexp)
  11888. (- (match-end 0) (point) 1))
  11889. 1))
  11890. (cond
  11891. ((and (equal current "") (equal tags "")))
  11892. ((re-search-forward
  11893. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11894. (point-at-eol) t)
  11895. (if (equal tags "")
  11896. (setq rpl "")
  11897. (goto-char (match-beginning 0))
  11898. (setq c0 (current-column)
  11899. ;; compute offset for the case of org-indent-mode active
  11900. di (if org-indent-mode
  11901. (* (1- org-indent-indentation-per-level) (1- level))
  11902. 0)
  11903. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  11904. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  11905. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  11906. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11907. (replace-match rpl t t)
  11908. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11909. tags)
  11910. (t (error "Tags alignment failed")))
  11911. (org-move-to-column col)
  11912. (unless just-align
  11913. (run-hooks 'org-after-tags-change-hook)))))
  11914. (defun org-change-tag-in-region (beg end tag off)
  11915. "Add or remove TAG for each entry in the region.
  11916. This works in the agenda, and also in an org-mode buffer."
  11917. (interactive
  11918. (list (region-beginning) (region-end)
  11919. (let ((org-last-tags-completion-table
  11920. (if (org-mode-p)
  11921. (org-get-buffer-tags)
  11922. (org-global-tags-completion-table))))
  11923. (org-icompleting-read
  11924. "Tag: " 'org-tags-completion-function nil nil nil
  11925. 'org-tags-history))
  11926. (progn
  11927. (message "[s]et or [r]emove? ")
  11928. (equal (read-char-exclusive) ?r))))
  11929. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11930. (let ((agendap (equal major-mode 'org-agenda-mode))
  11931. l1 l2 m buf pos newhead (cnt 0))
  11932. (goto-char end)
  11933. (setq l2 (1- (org-current-line)))
  11934. (goto-char beg)
  11935. (setq l1 (org-current-line))
  11936. (loop for l from l1 to l2 do
  11937. (org-goto-line l)
  11938. (setq m (get-text-property (point) 'org-hd-marker))
  11939. (when (or (and (org-mode-p) (org-on-heading-p))
  11940. (and agendap m))
  11941. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11942. pos (if agendap m (point)))
  11943. (with-current-buffer buf
  11944. (save-excursion
  11945. (save-restriction
  11946. (goto-char pos)
  11947. (setq cnt (1+ cnt))
  11948. (org-toggle-tag tag (if off 'off 'on))
  11949. (setq newhead (org-get-heading)))))
  11950. (and agendap (org-agenda-change-all-lines newhead m))))
  11951. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11952. (defun org-tags-completion-function (string predicate &optional flag)
  11953. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11954. (confirm (lambda (x) (stringp (car x)))))
  11955. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  11956. (setq s1 (match-string 1 string)
  11957. s2 (match-string 2 string))
  11958. (setq s1 "" s2 string))
  11959. (cond
  11960. ((eq flag nil)
  11961. ;; try completion
  11962. (setq rtn (try-completion s2 ctable confirm))
  11963. (if (stringp rtn)
  11964. (setq rtn
  11965. (concat s1 s2 (substring rtn (length s2))
  11966. (if (and org-add-colon-after-tag-completion
  11967. (assoc rtn ctable))
  11968. ":" ""))))
  11969. rtn)
  11970. ((eq flag t)
  11971. ;; all-completions
  11972. (all-completions s2 ctable confirm)
  11973. )
  11974. ((eq flag 'lambda)
  11975. ;; exact match?
  11976. (assoc s2 ctable)))
  11977. ))
  11978. (defun org-fast-tag-insert (kwd tags face &optional end)
  11979. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11980. (insert (format "%-12s" (concat kwd ":"))
  11981. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11982. (or end "")))
  11983. (defun org-fast-tag-show-exit (flag)
  11984. (save-excursion
  11985. (org-goto-line 3)
  11986. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11987. (replace-match ""))
  11988. (when flag
  11989. (end-of-line 1)
  11990. (org-move-to-column (- (window-width) 19) t)
  11991. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11992. (defun org-set-current-tags-overlay (current prefix)
  11993. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11994. (if (featurep 'xemacs)
  11995. (org-overlay-display org-tags-overlay (concat prefix s)
  11996. 'secondary-selection)
  11997. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11998. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11999. (defvar org-last-tag-selection-key nil)
  12000. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12001. "Fast tag selection with single keys.
  12002. CURRENT is the current list of tags in the headline, INHERITED is the
  12003. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12004. possibly with grouping information. TODO-TABLE is a similar table with
  12005. TODO keywords, should these have keys assigned to them.
  12006. If the keys are nil, a-z are automatically assigned.
  12007. Returns the new tags string, or nil to not change the current settings."
  12008. (let* ((fulltable (append table todo-table))
  12009. (maxlen (apply 'max (mapcar
  12010. (lambda (x)
  12011. (if (stringp (car x)) (string-width (car x)) 0))
  12012. fulltable)))
  12013. (buf (current-buffer))
  12014. (expert (eq org-fast-tag-selection-single-key 'expert))
  12015. (buffer-tags nil)
  12016. (fwidth (+ maxlen 3 1 3))
  12017. (ncol (/ (- (window-width) 4) fwidth))
  12018. (i-face 'org-done)
  12019. (c-face 'org-todo)
  12020. tg cnt e c char c1 c2 ntable tbl rtn
  12021. ov-start ov-end ov-prefix
  12022. (exit-after-next org-fast-tag-selection-single-key)
  12023. (done-keywords org-done-keywords)
  12024. groups ingroup)
  12025. (save-excursion
  12026. (beginning-of-line 1)
  12027. (if (looking-at
  12028. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12029. (setq ov-start (match-beginning 1)
  12030. ov-end (match-end 1)
  12031. ov-prefix "")
  12032. (setq ov-start (1- (point-at-eol))
  12033. ov-end (1+ ov-start))
  12034. (skip-chars-forward "^\n\r")
  12035. (setq ov-prefix
  12036. (concat
  12037. (buffer-substring (1- (point)) (point))
  12038. (if (> (current-column) org-tags-column)
  12039. " "
  12040. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12041. (move-overlay org-tags-overlay ov-start ov-end)
  12042. (save-window-excursion
  12043. (if expert
  12044. (set-buffer (get-buffer-create " *Org tags*"))
  12045. (delete-other-windows)
  12046. (split-window-vertically)
  12047. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12048. (erase-buffer)
  12049. (org-set-local 'org-done-keywords done-keywords)
  12050. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12051. (org-fast-tag-insert "Current" current c-face "\n\n")
  12052. (org-fast-tag-show-exit exit-after-next)
  12053. (org-set-current-tags-overlay current ov-prefix)
  12054. (setq tbl fulltable char ?a cnt 0)
  12055. (while (setq e (pop tbl))
  12056. (cond
  12057. ((equal (car e) :startgroup)
  12058. (push '() groups) (setq ingroup t)
  12059. (when (not (= cnt 0))
  12060. (setq cnt 0)
  12061. (insert "\n"))
  12062. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12063. ((equal (car e) :endgroup)
  12064. (setq ingroup nil cnt 0)
  12065. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12066. ((equal e '(:newline))
  12067. (when (not (= cnt 0))
  12068. (setq cnt 0)
  12069. (insert "\n")
  12070. (setq e (car tbl))
  12071. (while (equal (car tbl) '(:newline))
  12072. (insert "\n")
  12073. (setq tbl (cdr tbl)))))
  12074. (t
  12075. (setq tg (copy-sequence (car e)) c2 nil)
  12076. (if (cdr e)
  12077. (setq c (cdr e))
  12078. ;; automatically assign a character.
  12079. (setq c1 (string-to-char
  12080. (downcase (substring
  12081. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12082. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12083. (while (or (rassoc char ntable) (rassoc char table))
  12084. (setq char (1+ char)))
  12085. (setq c2 c1))
  12086. (setq c (or c2 char)))
  12087. (if ingroup (push tg (car groups)))
  12088. (setq tg (org-add-props tg nil 'face
  12089. (cond
  12090. ((not (assoc tg table))
  12091. (org-get-todo-face tg))
  12092. ((member tg current) c-face)
  12093. ((member tg inherited) i-face)
  12094. (t nil))))
  12095. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12096. (insert "[" c "] " tg (make-string
  12097. (- fwidth 4 (length tg)) ?\ ))
  12098. (push (cons tg c) ntable)
  12099. (when (= (setq cnt (1+ cnt)) ncol)
  12100. (insert "\n")
  12101. (if ingroup (insert " "))
  12102. (setq cnt 0)))))
  12103. (setq ntable (nreverse ntable))
  12104. (insert "\n")
  12105. (goto-char (point-min))
  12106. (if (not expert) (org-fit-window-to-buffer))
  12107. (setq rtn
  12108. (catch 'exit
  12109. (while t
  12110. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12111. (if (not groups) "no " "")
  12112. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12113. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12114. (setq org-last-tag-selection-key c)
  12115. (cond
  12116. ((= c ?\r) (throw 'exit t))
  12117. ((= c ?!)
  12118. (setq groups (not groups))
  12119. (goto-char (point-min))
  12120. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12121. ((= c ?\C-c)
  12122. (if (not expert)
  12123. (org-fast-tag-show-exit
  12124. (setq exit-after-next (not exit-after-next)))
  12125. (setq expert nil)
  12126. (delete-other-windows)
  12127. (set-window-buffer (split-window-vertically) " *Org tags*")
  12128. (org-switch-to-buffer-other-window " *Org tags*")
  12129. (org-fit-window-to-buffer)))
  12130. ((or (= c ?\C-g)
  12131. (and (= c ?q) (not (rassoc c ntable))))
  12132. (org-detach-overlay org-tags-overlay)
  12133. (setq quit-flag t))
  12134. ((= c ?\ )
  12135. (setq current nil)
  12136. (if exit-after-next (setq exit-after-next 'now)))
  12137. ((= c ?\t)
  12138. (condition-case nil
  12139. (setq tg (org-icompleting-read
  12140. "Tag: "
  12141. (or buffer-tags
  12142. (with-current-buffer buf
  12143. (org-get-buffer-tags)))))
  12144. (quit (setq tg "")))
  12145. (when (string-match "\\S-" tg)
  12146. (add-to-list 'buffer-tags (list tg))
  12147. (if (member tg current)
  12148. (setq current (delete tg current))
  12149. (push tg current)))
  12150. (if exit-after-next (setq exit-after-next 'now)))
  12151. ((setq e (rassoc c todo-table) tg (car e))
  12152. (with-current-buffer buf
  12153. (save-excursion (org-todo tg)))
  12154. (if exit-after-next (setq exit-after-next 'now)))
  12155. ((setq e (rassoc c ntable) tg (car e))
  12156. (if (member tg current)
  12157. (setq current (delete tg current))
  12158. (loop for g in groups do
  12159. (if (member tg g)
  12160. (mapc (lambda (x)
  12161. (setq current (delete x current)))
  12162. g)))
  12163. (push tg current))
  12164. (if exit-after-next (setq exit-after-next 'now))))
  12165. ;; Create a sorted list
  12166. (setq current
  12167. (sort current
  12168. (lambda (a b)
  12169. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12170. (if (eq exit-after-next 'now) (throw 'exit t))
  12171. (goto-char (point-min))
  12172. (beginning-of-line 2)
  12173. (delete-region (point) (point-at-eol))
  12174. (org-fast-tag-insert "Current" current c-face)
  12175. (org-set-current-tags-overlay current ov-prefix)
  12176. (while (re-search-forward
  12177. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12178. (setq tg (match-string 1))
  12179. (add-text-properties
  12180. (match-beginning 1) (match-end 1)
  12181. (list 'face
  12182. (cond
  12183. ((member tg current) c-face)
  12184. ((member tg inherited) i-face)
  12185. (t (get-text-property (match-beginning 1) 'face))))))
  12186. (goto-char (point-min)))))
  12187. (org-detach-overlay org-tags-overlay)
  12188. (if rtn
  12189. (mapconcat 'identity current ":")
  12190. nil))))
  12191. (defun org-get-tags-string ()
  12192. "Get the TAGS string in the current headline."
  12193. (unless (org-on-heading-p t)
  12194. (error "Not on a heading"))
  12195. (save-excursion
  12196. (beginning-of-line 1)
  12197. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12198. (org-match-string-no-properties 1)
  12199. "")))
  12200. (defun org-get-tags ()
  12201. "Get the list of tags specified in the current headline."
  12202. (org-split-string (org-get-tags-string) ":"))
  12203. (defun org-get-buffer-tags ()
  12204. "Get a table of all tags used in the buffer, for completion."
  12205. (let (tags)
  12206. (save-excursion
  12207. (goto-char (point-min))
  12208. (while (re-search-forward
  12209. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12210. (when (equal (char-after (point-at-bol 0)) ?*)
  12211. (mapc (lambda (x) (add-to-list 'tags x))
  12212. (org-split-string (org-match-string-no-properties 1) ":")))))
  12213. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12214. (mapcar 'list tags)))
  12215. ;;;; The mapping API
  12216. ;;;###autoload
  12217. (defun org-map-entries (func &optional match scope &rest skip)
  12218. "Call FUNC at each headline selected by MATCH in SCOPE.
  12219. FUNC is a function or a lisp form. The function will be called without
  12220. arguments, with the cursor positioned at the beginning of the headline.
  12221. The return values of all calls to the function will be collected and
  12222. returned as a list.
  12223. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12224. does not need to preserve point. After evaluation, the cursor will be
  12225. moved to the end of the line (presumably of the headline of the
  12226. processed entry) and search continues from there. Under some
  12227. circumstances, this may not produce the wanted results. For example,
  12228. if you have removed (e.g. archived) the current (sub)tree it could
  12229. mean that the next entry will be skipped entirely. In such cases, you
  12230. can specify the position from where search should continue by making
  12231. FUNC set the variable `org-map-continue-from' to the desired buffer
  12232. position.
  12233. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12234. Only headlines that are matched by this query will be considered during
  12235. the iteration. When MATCH is nil or t, all headlines will be
  12236. visited by the iteration.
  12237. SCOPE determines the scope of this command. It can be any of:
  12238. nil The current buffer, respecting the restriction if any
  12239. tree The subtree started with the entry at point
  12240. file The current buffer, without restriction
  12241. file-with-archives
  12242. The current buffer, and any archives associated with it
  12243. agenda All agenda files
  12244. agenda-with-archives
  12245. All agenda files with any archive files associated with them
  12246. \(file1 file2 ...)
  12247. If this is a list, all files in the list will be scanned
  12248. The remaining args are treated as settings for the skipping facilities of
  12249. the scanner. The following items can be given here:
  12250. archive skip trees with the archive tag.
  12251. comment skip trees with the COMMENT keyword
  12252. function or Emacs Lisp form:
  12253. will be used as value for `org-agenda-skip-function', so whenever
  12254. the function returns t, FUNC will not be called for that
  12255. entry and search will continue from the point where the
  12256. function leaves it.
  12257. If your function needs to retrieve the tags including inherited tags
  12258. at the *current* entry, you can use the value of the variable
  12259. `org-scanner-tags' which will be much faster than getting the value
  12260. with `org-get-tags-at'. If your function gets properties with
  12261. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12262. to t around the call to `org-entry-properties' to get the same speedup.
  12263. Note that if your function moves around to retrieve tags and properties at
  12264. a *different* entry, you cannot use these techniques."
  12265. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12266. (org-agenda-skip-archived-trees (memq 'archive skip))
  12267. (org-agenda-skip-comment-trees (memq 'comment skip))
  12268. (org-agenda-skip-function
  12269. (car (org-delete-all '(comment archive) skip)))
  12270. (org-tags-match-list-sublevels t)
  12271. matcher file res
  12272. org-todo-keywords-for-agenda
  12273. org-done-keywords-for-agenda
  12274. org-todo-keyword-alist-for-agenda
  12275. org-drawers-for-agenda
  12276. org-tag-alist-for-agenda)
  12277. (cond
  12278. ((eq match t) (setq matcher t))
  12279. ((eq match nil) (setq matcher t))
  12280. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12281. (save-excursion
  12282. (save-restriction
  12283. (when (eq scope 'tree)
  12284. (org-back-to-heading t)
  12285. (org-narrow-to-subtree)
  12286. (setq scope nil))
  12287. (if (not scope)
  12288. (progn
  12289. (org-prepare-agenda-buffers
  12290. (list (buffer-file-name (current-buffer))))
  12291. (setq res (org-scan-tags func matcher)))
  12292. ;; Get the right scope
  12293. (cond
  12294. ((and scope (listp scope) (symbolp (car scope)))
  12295. (setq scope (eval scope)))
  12296. ((eq scope 'agenda)
  12297. (setq scope (org-agenda-files t)))
  12298. ((eq scope 'agenda-with-archives)
  12299. (setq scope (org-agenda-files t))
  12300. (setq scope (org-add-archive-files scope)))
  12301. ((eq scope 'file)
  12302. (setq scope (list (buffer-file-name))))
  12303. ((eq scope 'file-with-archives)
  12304. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12305. (org-prepare-agenda-buffers scope)
  12306. (while (setq file (pop scope))
  12307. (with-current-buffer (org-find-base-buffer-visiting file)
  12308. (save-excursion
  12309. (save-restriction
  12310. (widen)
  12311. (goto-char (point-min))
  12312. (setq res (append res (org-scan-tags func matcher))))))))))
  12313. res))
  12314. ;;;; Properties
  12315. ;;; Setting and retrieving properties
  12316. (defconst org-special-properties
  12317. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12318. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12319. "The special properties valid in Org-mode.
  12320. These are properties that are not defined in the property drawer,
  12321. but in some other way.")
  12322. (defconst org-default-properties
  12323. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12324. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12325. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12326. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12327. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12328. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12329. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12330. "Some properties that are used by Org-mode for various purposes.
  12331. Being in this list makes sure that they are offered for completion.")
  12332. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12333. "Regular expression matching the first line of a property drawer.")
  12334. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12335. "Regular expression matching the last line of a property drawer.")
  12336. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12337. "Regular expression matching the first line of a property drawer.")
  12338. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12339. "Regular expression matching the first line of a property drawer.")
  12340. (defconst org-property-drawer-re
  12341. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12342. org-property-end-re "\\)\n?")
  12343. "Matches an entire property drawer.")
  12344. (defconst org-clock-drawer-re
  12345. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12346. org-property-end-re "\\)\n?")
  12347. "Matches an entire clock drawer.")
  12348. (defsubst org-re-property (property)
  12349. "Return a regexp matching PROPERTY.
  12350. Match group 1 will be set to the value "
  12351. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12352. (defun org-property-action ()
  12353. "Do an action on properties."
  12354. (interactive)
  12355. (let (c)
  12356. (org-at-property-p)
  12357. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12358. (setq c (read-char-exclusive))
  12359. (cond
  12360. ((equal c ?s)
  12361. (call-interactively 'org-set-property))
  12362. ((equal c ?d)
  12363. (call-interactively 'org-delete-property))
  12364. ((equal c ?D)
  12365. (call-interactively 'org-delete-property-globally))
  12366. ((equal c ?c)
  12367. (call-interactively 'org-compute-property-at-point))
  12368. (t (error "No such property action %c" c)))))
  12369. (defun org-set-effort (&optional value)
  12370. "Set the effort property of the current entry.
  12371. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12372. allowed value."
  12373. (interactive "P")
  12374. (if (equal value 0) (setq value 10))
  12375. (let* ((completion-ignore-case t)
  12376. (prop org-effort-property)
  12377. (cur (org-entry-get nil prop))
  12378. (allowed (org-property-get-allowed-values nil prop 'table))
  12379. (existing (mapcar 'list (org-property-values prop)))
  12380. rpl
  12381. (val (cond
  12382. ((stringp value) value)
  12383. ((and allowed (integerp value))
  12384. (or (car (nth (1- value) allowed))
  12385. (car (org-last allowed))))
  12386. (allowed
  12387. (message "Select 1-9,0, [RET%s]: %s"
  12388. (if cur (concat "=" cur) "")
  12389. (mapconcat 'car allowed " "))
  12390. (setq rpl (read-char-exclusive))
  12391. (if (equal rpl ?\r)
  12392. cur
  12393. (setq rpl (- rpl ?0))
  12394. (if (equal rpl 0) (setq rpl 10))
  12395. (if (and (> rpl 0) (<= rpl (length allowed)))
  12396. (car (nth (1- rpl) allowed))
  12397. (org-completing-read "Effort: " allowed nil))))
  12398. (t
  12399. (let (org-completion-use-ido org-completion-use-iswitchb)
  12400. (org-completing-read
  12401. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12402. (concat "[" cur "]") "")
  12403. ": ")
  12404. existing nil nil "" nil cur))))))
  12405. (unless (equal (org-entry-get nil prop) val)
  12406. (org-entry-put nil prop val))
  12407. (message "%s is now %s" prop val)))
  12408. (defun org-at-property-p ()
  12409. "Is cursor inside a property drawer?"
  12410. (save-excursion
  12411. (beginning-of-line 1)
  12412. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12413. (save-match-data ;; Used by calling procedures
  12414. (let ((p (point))
  12415. (range (unless (org-before-first-heading-p)
  12416. (org-get-property-block))))
  12417. (and range (<= (car range) p) (< p (cdr range))))))))
  12418. (defun org-get-property-block (&optional beg end force)
  12419. "Return the (beg . end) range of the body of the property drawer.
  12420. BEG and END can be beginning and end of subtree, if not given
  12421. they will be found.
  12422. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12423. (catch 'exit
  12424. (save-excursion
  12425. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12426. (end (or end (progn (outline-next-heading) (point)))))
  12427. (goto-char beg)
  12428. (if (re-search-forward org-property-start-re end t)
  12429. (setq beg (1+ (match-end 0)))
  12430. (if force
  12431. (save-excursion
  12432. (org-insert-property-drawer)
  12433. (setq end (progn (outline-next-heading) (point))))
  12434. (throw 'exit nil))
  12435. (goto-char beg)
  12436. (if (re-search-forward org-property-start-re end t)
  12437. (setq beg (1+ (match-end 0)))))
  12438. (if (re-search-forward org-property-end-re end t)
  12439. (setq end (match-beginning 0))
  12440. (or force (throw 'exit nil))
  12441. (goto-char beg)
  12442. (setq end beg)
  12443. (org-indent-line-function)
  12444. (insert ":END:\n"))
  12445. (cons beg end)))))
  12446. (defun org-entry-properties (&optional pom which specific)
  12447. "Get all properties of the entry at point-or-marker POM.
  12448. This includes the TODO keyword, the tags, time strings for deadline,
  12449. scheduled, and clocking, and any additional properties defined in the
  12450. entry. The return value is an alist, keys may occur multiple times
  12451. if the property key was used several times.
  12452. POM may also be nil, in which case the current entry is used.
  12453. If WHICH is nil or `all', get all properties. If WHICH is
  12454. `special' or `standard', only get that subclass. If WHICH
  12455. is a string only get exactly this property. SPECIFIC can be a string, the
  12456. specific property we are interested in. Specifying it can speed
  12457. things up because then unnecessary parsing is avoided."
  12458. (setq which (or which 'all))
  12459. (org-with-point-at pom
  12460. (let ((clockstr (substring org-clock-string 0 -1))
  12461. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12462. (case-fold-search nil)
  12463. beg end range props sum-props key key1 value string clocksum)
  12464. (save-excursion
  12465. (when (condition-case nil
  12466. (and (org-mode-p) (org-back-to-heading t))
  12467. (error nil))
  12468. (setq beg (point))
  12469. (setq sum-props (get-text-property (point) 'org-summaries))
  12470. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12471. (outline-next-heading)
  12472. (setq end (point))
  12473. (when (memq which '(all special))
  12474. ;; Get the special properties, like TODO and tags
  12475. (goto-char beg)
  12476. (when (and (or (not specific) (string= specific "TODO"))
  12477. (looking-at org-todo-line-regexp) (match-end 2))
  12478. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12479. (when (and (or (not specific) (string= specific "PRIORITY"))
  12480. (looking-at org-priority-regexp))
  12481. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12482. (when (or (not specific) (string= specific "FILE"))
  12483. (push (cons "FILE" buffer-file-name) props))
  12484. (when (and (or (not specific) (string= specific "TAGS"))
  12485. (setq value (org-get-tags-string))
  12486. (string-match "\\S-" value))
  12487. (push (cons "TAGS" value) props))
  12488. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12489. (setq value (org-get-tags-at)))
  12490. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12491. ":"))
  12492. props))
  12493. (when (or (not specific) (string= specific "BLOCKED"))
  12494. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12495. (when (or (not specific)
  12496. (member specific
  12497. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12498. "TIMESTAMP" "TIMESTAMP_IA")))
  12499. (catch 'match
  12500. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12501. (setq key (if (match-end 1)
  12502. (substring (org-match-string-no-properties 1)
  12503. 0 -1))
  12504. string (if (equal key clockstr)
  12505. (org-no-properties
  12506. (org-trim
  12507. (buffer-substring
  12508. (match-beginning 3) (goto-char
  12509. (point-at-eol)))))
  12510. (substring (org-match-string-no-properties 3)
  12511. 1 -1)))
  12512. ;; Get the correct property name from the key. This is
  12513. ;; necessary if the user has configured time keywords.
  12514. (setq key1 (concat key ":"))
  12515. (cond
  12516. ((not key)
  12517. (setq key
  12518. (if (= (char-after (match-beginning 3)) ?\[)
  12519. "TIMESTAMP_IA" "TIMESTAMP")))
  12520. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12521. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12522. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12523. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12524. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12525. (progn
  12526. (push (cons key string) props)
  12527. ;; no need to search further if match is found
  12528. (throw 'match t))
  12529. (when (or (equal key "CLOCK") (not (assoc key props)))
  12530. (push (cons key string) props))))))
  12531. )
  12532. (when (memq which '(all standard))
  12533. ;; Get the standard properties, like :PROP: ...
  12534. (setq range (org-get-property-block beg end))
  12535. (when range
  12536. (goto-char (car range))
  12537. (while (re-search-forward
  12538. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12539. (cdr range) t)
  12540. (setq key (org-match-string-no-properties 1)
  12541. value (org-trim (or (org-match-string-no-properties 2) "")))
  12542. (unless (member key excluded)
  12543. (push (cons key (or value "")) props)))))
  12544. (if clocksum
  12545. (push (cons "CLOCKSUM"
  12546. (org-columns-number-to-string (/ (float clocksum) 60.)
  12547. 'add_times))
  12548. props))
  12549. (unless (assoc "CATEGORY" props)
  12550. (push (cons "CATEGORY" (org-get-category)) props))
  12551. (append sum-props (nreverse props)))))))
  12552. (defun org-entry-get (pom property &optional inherit literal-nil)
  12553. "Get value of PROPERTY for entry at point-or-marker POM.
  12554. If INHERIT is non-nil and the entry does not have the property,
  12555. then also check higher levels of the hierarchy.
  12556. If INHERIT is the symbol `selective', use inheritance only if the setting
  12557. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12558. If the property is present but empty, the return value is the empty string.
  12559. If the property is not present at all, nil is returned.
  12560. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12561. do not interpret it as the list atom nil. This is used for inheritance
  12562. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12563. (org-with-point-at pom
  12564. (if (and inherit (if (eq inherit 'selective)
  12565. (org-property-inherit-p property)
  12566. t))
  12567. (org-entry-get-with-inheritance property literal-nil)
  12568. (if (member property org-special-properties)
  12569. ;; We need a special property. Use `org-entry-properties' to
  12570. ;; retrieve it, but specify the wanted property
  12571. (cdr (assoc property (org-entry-properties nil 'special property)))
  12572. (let ((range (unless (org-before-first-heading-p)
  12573. (org-get-property-block))))
  12574. (if (and range
  12575. (goto-char (car range))
  12576. (re-search-forward
  12577. (org-re-property property)
  12578. (cdr range) t))
  12579. ;; Found the property, return it.
  12580. (if (match-end 1)
  12581. (if literal-nil
  12582. (org-match-string-no-properties 1)
  12583. (org-not-nil (org-match-string-no-properties 1)))
  12584. "")))))))
  12585. (defun org-property-or-variable-value (var &optional inherit)
  12586. "Check if there is a property fixing the value of VAR.
  12587. If yes, return this value. If not, return the current value of the variable."
  12588. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12589. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12590. (read prop)
  12591. (symbol-value var))))
  12592. (defun org-entry-delete (pom property)
  12593. "Delete the property PROPERTY from entry at point-or-marker POM."
  12594. (org-with-point-at pom
  12595. (if (member property org-special-properties)
  12596. nil ; cannot delete these properties.
  12597. (let ((range (org-get-property-block)))
  12598. (if (and range
  12599. (goto-char (car range))
  12600. (re-search-forward
  12601. (org-re-property property)
  12602. (cdr range) t))
  12603. (progn
  12604. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12605. t)
  12606. nil)))))
  12607. ;; Multi-values properties are properties that contain multiple values
  12608. ;; These values are assumed to be single words, separated by whitespace.
  12609. (defun org-entry-add-to-multivalued-property (pom property value)
  12610. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12611. (let* ((old (org-entry-get pom property))
  12612. (values (and old (org-split-string old "[ \t]"))))
  12613. (setq value (org-entry-protect-space value))
  12614. (unless (member value values)
  12615. (setq values (cons value values))
  12616. (org-entry-put pom property
  12617. (mapconcat 'identity values " ")))))
  12618. (defun org-entry-remove-from-multivalued-property (pom property value)
  12619. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12620. (let* ((old (org-entry-get pom property))
  12621. (values (and old (org-split-string old "[ \t]"))))
  12622. (setq value (org-entry-protect-space value))
  12623. (when (member value values)
  12624. (setq values (delete value values))
  12625. (org-entry-put pom property
  12626. (mapconcat 'identity values " ")))))
  12627. (defun org-entry-member-in-multivalued-property (pom property value)
  12628. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12629. (let* ((old (org-entry-get pom property))
  12630. (values (and old (org-split-string old "[ \t]"))))
  12631. (setq value (org-entry-protect-space value))
  12632. (member value values)))
  12633. (defun org-entry-get-multivalued-property (pom property)
  12634. "Return a list of values in a multivalued property."
  12635. (let* ((value (org-entry-get pom property))
  12636. (values (and value (org-split-string value "[ \t]"))))
  12637. (mapcar 'org-entry-restore-space values)))
  12638. (defun org-entry-put-multivalued-property (pom property &rest values)
  12639. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12640. VALUES should be a list of strings. Spaces will be protected."
  12641. (org-entry-put pom property
  12642. (mapconcat 'org-entry-protect-space values " "))
  12643. (let* ((value (org-entry-get pom property))
  12644. (values (and value (org-split-string value "[ \t]"))))
  12645. (mapcar 'org-entry-restore-space values)))
  12646. (defun org-entry-protect-space (s)
  12647. "Protect spaces and newline in string S."
  12648. (while (string-match " " s)
  12649. (setq s (replace-match "%20" t t s)))
  12650. (while (string-match "\n" s)
  12651. (setq s (replace-match "%0A" t t s)))
  12652. s)
  12653. (defun org-entry-restore-space (s)
  12654. "Restore spaces and newline in string S."
  12655. (while (string-match "%20" s)
  12656. (setq s (replace-match " " t t s)))
  12657. (while (string-match "%0A" s)
  12658. (setq s (replace-match "\n" t t s)))
  12659. s)
  12660. (defvar org-entry-property-inherited-from (make-marker)
  12661. "Marker pointing to the entry from where a property was inherited.
  12662. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12663. location of the entry where the inheritance search matched. If there was
  12664. no match, the marker will point nowhere.
  12665. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12666. is set.")
  12667. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12668. "Get entry property, and search higher levels if not present.
  12669. The search will stop at the first ancestor which has the property defined.
  12670. If the value found is \"nil\", return nil to show that the property
  12671. should be considered as undefined (this is the meaning of nil here).
  12672. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12673. (move-marker org-entry-property-inherited-from nil)
  12674. (let (tmp)
  12675. (unless (org-before-first-heading-p)
  12676. (save-excursion
  12677. (save-restriction
  12678. (widen)
  12679. (catch 'ex
  12680. (while t
  12681. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12682. (org-back-to-heading t)
  12683. (move-marker org-entry-property-inherited-from (point))
  12684. (throw 'ex tmp))
  12685. (or (org-up-heading-safe) (throw 'ex nil)))))))
  12686. (setq tmp (or tmp
  12687. (cdr (assoc property org-file-properties))
  12688. (cdr (assoc property org-global-properties))
  12689. (cdr (assoc property org-global-properties-fixed))))
  12690. (if literal-nil tmp (org-not-nil tmp))))
  12691. (defvar org-property-changed-functions nil
  12692. "Hook called when the value of a property has changed.
  12693. Each hook function should accept two arguments, the name of the property
  12694. and the new value.")
  12695. (defun org-entry-put (pom property value)
  12696. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12697. (org-with-point-at pom
  12698. (org-back-to-heading t)
  12699. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12700. range)
  12701. (cond
  12702. ((equal property "TODO")
  12703. (when (and (stringp value) (string-match "\\S-" value)
  12704. (not (member value org-todo-keywords-1)))
  12705. (error "\"%s\" is not a valid TODO state" value))
  12706. (if (or (not value)
  12707. (not (string-match "\\S-" value)))
  12708. (setq value 'none))
  12709. (org-todo value)
  12710. (org-set-tags nil 'align))
  12711. ((equal property "PRIORITY")
  12712. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12713. (string-to-char value) ?\ ))
  12714. (org-set-tags nil 'align))
  12715. ((equal property "SCHEDULED")
  12716. (if (re-search-forward org-scheduled-time-regexp end t)
  12717. (cond
  12718. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12719. ((eq value 'later) (org-timestamp-change 1 'day))
  12720. (t (call-interactively 'org-schedule)))
  12721. (call-interactively 'org-schedule)))
  12722. ((equal property "DEADLINE")
  12723. (if (re-search-forward org-deadline-time-regexp end t)
  12724. (cond
  12725. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12726. ((eq value 'later) (org-timestamp-change 1 'day))
  12727. (t (call-interactively 'org-deadline)))
  12728. (call-interactively 'org-deadline)))
  12729. ((member property org-special-properties)
  12730. (error "The %s property can not yet be set with `org-entry-put'"
  12731. property))
  12732. (t ; a non-special property
  12733. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12734. (setq range (org-get-property-block beg end 'force))
  12735. (goto-char (car range))
  12736. (if (re-search-forward
  12737. (org-re-property property) (cdr range) t)
  12738. (progn
  12739. (delete-region (match-beginning 0) (match-end 0))
  12740. (goto-char (match-beginning 0)))
  12741. (goto-char (cdr range))
  12742. (insert "\n")
  12743. (backward-char 1)
  12744. (org-indent-line-function))
  12745. (insert ":" property ":")
  12746. (and value (insert " " value))
  12747. (org-indent-line-function)))))
  12748. (run-hook-with-args 'org-property-changed-functions property value)))
  12749. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12750. "Get all property keys in the current buffer.
  12751. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12752. like tags and TODO state.
  12753. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12754. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  12755. and others.
  12756. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12757. formats in the current buffer."
  12758. (let (rtn range cfmt s p)
  12759. (save-excursion
  12760. (save-restriction
  12761. (widen)
  12762. (goto-char (point-min))
  12763. (while (re-search-forward org-property-start-re nil t)
  12764. (setq range (org-get-property-block))
  12765. (goto-char (car range))
  12766. (while (re-search-forward
  12767. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12768. (cdr range) t)
  12769. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12770. (outline-next-heading))))
  12771. (when include-specials
  12772. (setq rtn (append org-special-properties rtn)))
  12773. (when include-defaults
  12774. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12775. (add-to-list 'rtn org-effort-property))
  12776. (when include-columns
  12777. (save-excursion
  12778. (save-restriction
  12779. (widen)
  12780. (goto-char (point-min))
  12781. (while (re-search-forward
  12782. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12783. nil t)
  12784. (setq cfmt (match-string 2) s 0)
  12785. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12786. cfmt s)
  12787. (setq s (match-end 0)
  12788. p (match-string 1 cfmt))
  12789. (unless (or (equal p "ITEM")
  12790. (member p org-special-properties))
  12791. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12792. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12793. (defun org-property-values (key)
  12794. "Return a list of all values of property KEY in the current buffer."
  12795. (save-excursion
  12796. (save-restriction
  12797. (widen)
  12798. (goto-char (point-min))
  12799. (let ((re (org-re-property key))
  12800. values)
  12801. (while (re-search-forward re nil t)
  12802. (add-to-list 'values (org-trim (match-string 1))))
  12803. (delete "" values)))))
  12804. (defun org-insert-property-drawer ()
  12805. "Insert a property drawer into the current entry."
  12806. (interactive)
  12807. (org-back-to-heading t)
  12808. (looking-at outline-regexp)
  12809. (let ((indent (if org-adapt-indentation
  12810. (- (match-end 0)(match-beginning 0))
  12811. 0))
  12812. (beg (point))
  12813. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12814. end hiddenp)
  12815. (outline-next-heading)
  12816. (setq end (point))
  12817. (goto-char beg)
  12818. (while (re-search-forward re end t))
  12819. (setq hiddenp (outline-invisible-p))
  12820. (end-of-line 1)
  12821. (and (equal (char-after) ?\n) (forward-char 1))
  12822. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12823. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12824. ;; just skip this line
  12825. (beginning-of-line 2)
  12826. ;; Drawer start, find the end
  12827. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12828. (beginning-of-line 1)))
  12829. (org-skip-over-state-notes)
  12830. (skip-chars-backward " \t\n\r")
  12831. (if (eq (char-before) ?*) (forward-char 1))
  12832. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12833. (beginning-of-line 0)
  12834. (org-indent-to-column indent)
  12835. (beginning-of-line 2)
  12836. (org-indent-to-column indent)
  12837. (beginning-of-line 0)
  12838. (if hiddenp
  12839. (save-excursion
  12840. (org-back-to-heading t)
  12841. (hide-entry))
  12842. (org-flag-drawer t))))
  12843. (defvar org-property-set-functions-alist nil
  12844. "Property set function alist.
  12845. Each entry should have the following format:
  12846. (PROPERTY . READ-FUNCTION)
  12847. The read function will be called with the same argument as
  12848. `org-completing-read'.")
  12849. (defun org-set-property-function (property)
  12850. "Get the function that should be used to set PROPERTY.
  12851. This is computed according to `org-property-set-functions-alist'."
  12852. (or (cdr (assoc property org-property-set-functions-alist))
  12853. 'org-completing-read))
  12854. (defun org-read-property-value (property)
  12855. "Read PROPERTY value from user."
  12856. (let* ((completion-ignore-case t)
  12857. (allowed (org-property-get-allowed-values nil property 'table))
  12858. (cur (org-entry-get nil property))
  12859. (prompt (concat property " value"
  12860. (if (and cur (string-match "\\S-" cur))
  12861. (concat " [" cur "]") "") ": "))
  12862. (set-function (org-set-property-function property))
  12863. (val (if allowed
  12864. (funcall set-function prompt allowed nil
  12865. (not (get-text-property 0 'org-unrestricted
  12866. (caar allowed))))
  12867. (let (org-completion-use-ido org-completion-use-iswitchb)
  12868. (funcall set-function prompt
  12869. (mapcar 'list (org-property-values property))
  12870. nil nil "" nil cur)))))
  12871. (if (equal val "")
  12872. cur
  12873. val)))
  12874. (defun org-read-property-name ()
  12875. "Read a property name."
  12876. (let* ((completion-ignore-case t)
  12877. (keys (org-buffer-property-keys nil t t))
  12878. (default-prop (save-excursion
  12879. (save-match-data
  12880. (beginning-of-line)
  12881. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  12882. (null (string= (match-string 1) "END"))
  12883. (match-string 1)))))
  12884. (property (org-icompleting-read
  12885. (concat "Property"
  12886. (if default-prop (concat " [" default-prop "]") "")
  12887. ": ")
  12888. (mapcar 'list keys)
  12889. nil nil nil nil
  12890. default-prop
  12891. )))
  12892. (if (member property keys)
  12893. property
  12894. (or (cdr (assoc (downcase property)
  12895. (mapcar (lambda (x) (cons (downcase x) x))
  12896. keys)))
  12897. property))))
  12898. (defun org-set-property (property value)
  12899. "In the current entry, set PROPERTY to VALUE.
  12900. When called interactively, this will prompt for a property name, offering
  12901. completion on existing and default properties. And then it will prompt
  12902. for a value, offering completion either on allowed values (via an inherited
  12903. xxx_ALL property) or on existing values in other instances of this property
  12904. in the current file."
  12905. (interactive (list nil nil))
  12906. (let* ((property (or property (org-read-property-name)))
  12907. (value (or value (org-read-property-value property))))
  12908. (unless (equal (org-entry-get nil property) value)
  12909. (org-entry-put nil property value))))
  12910. (defun org-delete-property (property)
  12911. "In the current entry, delete PROPERTY."
  12912. (interactive
  12913. (let* ((completion-ignore-case t)
  12914. (prop (org-icompleting-read "Property: "
  12915. (org-entry-properties nil 'standard))))
  12916. (list prop)))
  12917. (message "Property %s %s" property
  12918. (if (org-entry-delete nil property)
  12919. "deleted"
  12920. "was not present in the entry")))
  12921. (defun org-delete-property-globally (property)
  12922. "Remove PROPERTY globally, from all entries."
  12923. (interactive
  12924. (let* ((completion-ignore-case t)
  12925. (prop (org-icompleting-read
  12926. "Globally remove property: "
  12927. (mapcar 'list (org-buffer-property-keys)))))
  12928. (list prop)))
  12929. (save-excursion
  12930. (save-restriction
  12931. (widen)
  12932. (goto-char (point-min))
  12933. (let ((cnt 0))
  12934. (while (re-search-forward
  12935. (org-re-property property)
  12936. nil t)
  12937. (setq cnt (1+ cnt))
  12938. (replace-match ""))
  12939. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12940. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12941. (defun org-compute-property-at-point ()
  12942. "Compute the property at point.
  12943. This looks for an enclosing column format, extracts the operator and
  12944. then applies it to the property in the column format's scope."
  12945. (interactive)
  12946. (unless (org-at-property-p)
  12947. (error "Not at a property"))
  12948. (let ((prop (org-match-string-no-properties 2)))
  12949. (org-columns-get-format-and-top-level)
  12950. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12951. (error "No operator defined for property %s" prop))
  12952. (org-columns-compute prop)))
  12953. (defvar org-property-allowed-value-functions nil
  12954. "Hook for functions supplying allowed values for a specific property.
  12955. The functions must take a single argument, the name of the property, and
  12956. return a flat list of allowed values. If \":ETC\" is one of
  12957. the values, this means that these values are intended as defaults for
  12958. completion, but that other values should be allowed too.
  12959. The functions must return nil if they are not responsible for this
  12960. property.")
  12961. (defun org-property-get-allowed-values (pom property &optional table)
  12962. "Get allowed values for the property PROPERTY.
  12963. When TABLE is non-nil, return an alist that can directly be used for
  12964. completion."
  12965. (let (vals)
  12966. (cond
  12967. ((equal property "TODO")
  12968. (setq vals (org-with-point-at pom
  12969. (append org-todo-keywords-1 '("")))))
  12970. ((equal property "PRIORITY")
  12971. (let ((n org-lowest-priority))
  12972. (while (>= n org-highest-priority)
  12973. (push (char-to-string n) vals)
  12974. (setq n (1- n)))))
  12975. ((member property org-special-properties))
  12976. ((setq vals (run-hook-with-args-until-success
  12977. 'org-property-allowed-value-functions property)))
  12978. (t
  12979. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12980. (when (and vals (string-match "\\S-" vals))
  12981. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12982. (setq vals (mapcar (lambda (x)
  12983. (cond ((stringp x) x)
  12984. ((numberp x) (number-to-string x))
  12985. ((symbolp x) (symbol-name x))
  12986. (t "???")))
  12987. vals)))))
  12988. (when (member ":ETC" vals)
  12989. (setq vals (remove ":ETC" vals))
  12990. (org-add-props (car vals) '(org-unrestricted t)))
  12991. (if table (mapcar 'list vals) vals)))
  12992. (defun org-property-previous-allowed-value (&optional previous)
  12993. "Switch to the next allowed value for this property."
  12994. (interactive)
  12995. (org-property-next-allowed-value t))
  12996. (defun org-property-next-allowed-value (&optional previous)
  12997. "Switch to the next allowed value for this property."
  12998. (interactive)
  12999. (unless (org-at-property-p)
  13000. (error "Not at a property"))
  13001. (let* ((key (match-string 2))
  13002. (value (match-string 3))
  13003. (allowed (or (org-property-get-allowed-values (point) key)
  13004. (and (member value '("[ ]" "[-]" "[X]"))
  13005. '("[ ]" "[X]"))))
  13006. nval)
  13007. (unless allowed
  13008. (error "Allowed values for this property have not been defined"))
  13009. (if previous (setq allowed (reverse allowed)))
  13010. (if (member value allowed)
  13011. (setq nval (car (cdr (member value allowed)))))
  13012. (setq nval (or nval (car allowed)))
  13013. (if (equal nval value)
  13014. (error "Only one allowed value for this property"))
  13015. (org-at-property-p)
  13016. (replace-match (concat " :" key ": " nval) t t)
  13017. (org-indent-line-function)
  13018. (beginning-of-line 1)
  13019. (skip-chars-forward " \t")
  13020. (run-hook-with-args 'org-property-changed-functions key nval)))
  13021. (defun org-find-olp (path &optional this-buffer)
  13022. "Return a marker pointing to the entry at outline path OLP.
  13023. If anything goes wrong, throw an error.
  13024. You can wrap this call to catch the error like this:
  13025. (condition-case msg
  13026. (org-mobile-locate-entry (match-string 4))
  13027. (error (nth 1 msg)))
  13028. The return value will then be either a string with the error message,
  13029. or a marker if everything is OK.
  13030. If THIS-BUFFER is set, the outline path does not contain a file,
  13031. only headings."
  13032. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13033. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13034. (level 1)
  13035. (lmin 1)
  13036. (lmax 1)
  13037. limit re end found pos heading cnt flevel)
  13038. (unless buffer (error "File not found :%s" file))
  13039. (with-current-buffer buffer
  13040. (save-excursion
  13041. (save-restriction
  13042. (widen)
  13043. (setq limit (point-max))
  13044. (goto-char (point-min))
  13045. (while (setq heading (pop path))
  13046. (setq re (format org-complex-heading-regexp-format
  13047. (regexp-quote heading)))
  13048. (setq cnt 0 pos (point))
  13049. (while (re-search-forward re end t)
  13050. (setq level (- (match-end 1) (match-beginning 1)))
  13051. (if (and (>= level lmin) (<= level lmax))
  13052. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13053. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13054. lmax heading))
  13055. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13056. lmax heading))
  13057. (goto-char found)
  13058. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13059. (setq end (save-excursion (org-end-of-subtree t t))))
  13060. (when (org-on-heading-p)
  13061. (move-marker (make-marker) (point))))))))
  13062. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13063. "Find node HEADING in BUFFER.
  13064. Return a marker to the heading if it was found, or nil if not.
  13065. If POS-ONLY is set, return just the position instead of a marker.
  13066. The heading text must match exact, but it may have a TODO keyword,
  13067. a priority cookie and tags in the standard locations."
  13068. (with-current-buffer (or buffer (current-buffer))
  13069. (save-excursion
  13070. (save-restriction
  13071. (widen)
  13072. (goto-char (point-min))
  13073. (let (case-fold-search)
  13074. (if (re-search-forward
  13075. (format org-complex-heading-regexp-format
  13076. (regexp-quote heading)) nil t)
  13077. (if pos-only
  13078. (match-beginning 0)
  13079. (move-marker (make-marker) (match-beginning 0)))))))))
  13080. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13081. "Find Org node headline HEADING in all .org files in directory DIR.
  13082. When the target headline is found, return a marker to this location."
  13083. (let ((files (directory-files (or dir default-directory)
  13084. nil "\\`[^.#].*\\.org\\'"))
  13085. file visiting m buffer)
  13086. (catch 'found
  13087. (while (setq file (pop files))
  13088. (message "trying %s" file)
  13089. (setq visiting (org-find-base-buffer-visiting file))
  13090. (setq buffer (or visiting (find-file-noselect file)))
  13091. (setq m (org-find-exact-headline-in-buffer
  13092. heading buffer))
  13093. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13094. (and m (throw 'found m))))))
  13095. (defun org-find-entry-with-id (ident)
  13096. "Locate the entry that contains the ID property with exact value IDENT.
  13097. IDENT can be a string, a symbol or a number, this function will search for
  13098. the string representation of it.
  13099. Return the position where this entry starts, or nil if there is no such entry."
  13100. (interactive "sID: ")
  13101. (let ((id (cond
  13102. ((stringp ident) ident)
  13103. ((symbol-name ident) (symbol-name ident))
  13104. ((numberp ident) (number-to-string ident))
  13105. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13106. (case-fold-search nil))
  13107. (save-excursion
  13108. (save-restriction
  13109. (widen)
  13110. (goto-char (point-min))
  13111. (when (re-search-forward
  13112. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13113. nil t)
  13114. (org-back-to-heading t)
  13115. (point))))))
  13116. ;;;; Timestamps
  13117. (defvar org-last-changed-timestamp nil)
  13118. (defvar org-last-inserted-timestamp nil
  13119. "The last time stamp inserted with `org-insert-time-stamp'.")
  13120. (defvar org-time-was-given) ; dynamically scoped parameter
  13121. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13122. (defvar org-ts-what) ; dynamically scoped parameter
  13123. (defun org-time-stamp (arg &optional inactive)
  13124. "Prompt for a date/time and insert a time stamp.
  13125. If the user specifies a time like HH:MM, or if this command is called
  13126. with a prefix argument, the time stamp will contain date and time.
  13127. Otherwise, only the date will be included. All parts of a date not
  13128. specified by the user will be filled in from the current date/time.
  13129. So if you press just return without typing anything, the time stamp
  13130. will represent the current date/time. If there is already a timestamp
  13131. at the cursor, it will be modified."
  13132. (interactive "P")
  13133. (let* ((ts nil)
  13134. (default-time
  13135. ;; Default time is either today, or, when entering a range,
  13136. ;; the range start.
  13137. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13138. (save-excursion
  13139. (re-search-backward
  13140. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13141. (- (point) 20) t)))
  13142. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13143. (current-time)))
  13144. (default-input (and ts (org-get-compact-tod ts)))
  13145. (repeater (save-excursion
  13146. (save-match-data
  13147. (beginning-of-line)
  13148. (when (re-search-forward
  13149. "\\([.+-]+[0-9]+[dwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  13150. (save-excursion (progn (end-of-line) (point))) t)
  13151. (match-string 0)))))
  13152. org-time-was-given org-end-time-was-given time)
  13153. (cond
  13154. ((and (org-at-timestamp-p t)
  13155. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13156. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13157. (insert "--")
  13158. (setq time (let ((this-command this-command))
  13159. (org-read-date arg 'totime nil nil
  13160. default-time default-input)))
  13161. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13162. ((org-at-timestamp-p t)
  13163. (setq time (let ((this-command this-command))
  13164. (org-read-date arg 'totime nil nil default-time default-input)))
  13165. (when (org-at-timestamp-p t) ; just to get the match data
  13166. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13167. (replace-match "")
  13168. (setq org-last-changed-timestamp
  13169. (org-insert-time-stamp
  13170. time (or org-time-was-given arg)
  13171. inactive nil nil (list org-end-time-was-given)))
  13172. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13173. (setq org-last-changed-timestamp
  13174. (concat (substring org-last-inserted-timestamp 0 -1)
  13175. " " repeater ">"))))
  13176. (message "Timestamp updated"))
  13177. (t
  13178. (setq time (let ((this-command this-command))
  13179. (org-read-date arg 'totime nil nil default-time default-input)))
  13180. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13181. nil nil (list org-end-time-was-given))))))
  13182. ;; FIXME: can we use this for something else, like computing time differences?
  13183. (defun org-get-compact-tod (s)
  13184. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13185. (let* ((t1 (match-string 1 s))
  13186. (h1 (string-to-number (match-string 2 s)))
  13187. (m1 (string-to-number (match-string 3 s)))
  13188. (t2 (and (match-end 4) (match-string 5 s)))
  13189. (h2 (and t2 (string-to-number (match-string 6 s))))
  13190. (m2 (and t2 (string-to-number (match-string 7 s))))
  13191. dh dm)
  13192. (if (not t2)
  13193. t1
  13194. (setq dh (- h2 h1) dm (- m2 m1))
  13195. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13196. (concat t1 "+" (number-to-string dh)
  13197. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13198. (defun org-time-stamp-inactive (&optional arg)
  13199. "Insert an inactive time stamp.
  13200. An inactive time stamp is enclosed in square brackets instead of angle
  13201. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13202. does not link to the calendar and cannot be changed with the S-cursor keys.
  13203. So these are more for recording a certain time/date."
  13204. (interactive "P")
  13205. (org-time-stamp arg 'inactive))
  13206. (defvar org-date-ovl (make-overlay 1 1))
  13207. (overlay-put org-date-ovl 'face 'org-warning)
  13208. (org-detach-overlay org-date-ovl)
  13209. (defvar org-ans1) ; dynamically scoped parameter
  13210. (defvar org-ans2) ; dynamically scoped parameter
  13211. (defvar org-plain-time-of-day-regexp) ; defined below
  13212. (defvar org-overriding-default-time nil) ; dynamically scoped
  13213. (defvar org-read-date-overlay nil)
  13214. (defvar org-dcst nil) ; dynamically scoped
  13215. (defvar org-read-date-history nil)
  13216. (defvar org-read-date-final-answer nil)
  13217. (defvar org-read-date-analyze-futurep nil)
  13218. (defvar org-read-date-analyze-forced-year nil)
  13219. (defun org-read-date (&optional with-time to-time from-string prompt
  13220. default-time default-input)
  13221. "Read a date, possibly a time, and make things smooth for the user.
  13222. The prompt will suggest to enter an ISO date, but you can also enter anything
  13223. which will at least partially be understood by `parse-time-string'.
  13224. Unrecognized parts of the date will default to the current day, month, year,
  13225. hour and minute. If this command is called to replace a timestamp at point,
  13226. of to enter the second timestamp of a range, the default time is taken
  13227. from the existing stamp. Furthermore, the command prefers the future,
  13228. so if you are giving a date where the year is not given, and the day-month
  13229. combination is already past in the current year, it will assume you
  13230. mean next year. For details, see the manual. A few examples:
  13231. 3-2-5 --> 2003-02-05
  13232. feb 15 --> currentyear-02-15
  13233. 2/15 --> currentyear-02-15
  13234. sep 12 9 --> 2009-09-12
  13235. 12:45 --> today 12:45
  13236. 22 sept 0:34 --> currentyear-09-22 0:34
  13237. 12 --> currentyear-currentmonth-12
  13238. Fri --> nearest Friday (today or later)
  13239. etc.
  13240. Furthermore you can specify a relative date by giving, as the *first* thing
  13241. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13242. change in days weeks, months, years.
  13243. With a single plus or minus, the date is relative to today. With a double
  13244. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13245. +4d --> four days from today
  13246. +4 --> same as above
  13247. +2w --> two weeks from today
  13248. ++5 --> five days from default date
  13249. The function understands only English month and weekday abbreviations,
  13250. but this can be configured with the variables `parse-time-months' and
  13251. `parse-time-weekdays'.
  13252. While prompting, a calendar is popped up - you can also select the
  13253. date with the mouse (button 1). The calendar shows a period of three
  13254. months. To scroll it to other months, use the keys `>' and `<'.
  13255. If you don't like the calendar, turn it off with
  13256. \(setq org-read-date-popup-calendar nil)
  13257. With optional argument TO-TIME, the date will immediately be converted
  13258. to an internal time.
  13259. With an optional argument WITH-TIME, the prompt will suggest to also
  13260. insert a time. Note that when WITH-TIME is not set, you can still
  13261. enter a time, and this function will inform the calling routine about
  13262. this change. The calling routine may then choose to change the format
  13263. used to insert the time stamp into the buffer to include the time.
  13264. With optional argument FROM-STRING, read from this string instead from
  13265. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13266. the time/date that is used for everything that is not specified by the
  13267. user."
  13268. (require 'parse-time)
  13269. (let* ((org-time-stamp-rounding-minutes
  13270. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13271. (org-dcst org-display-custom-times)
  13272. (ct (org-current-time))
  13273. (def (or org-overriding-default-time default-time ct))
  13274. (defdecode (decode-time def))
  13275. (dummy (progn
  13276. (when (< (nth 2 defdecode) org-extend-today-until)
  13277. (setcar (nthcdr 2 defdecode) -1)
  13278. (setcar (nthcdr 1 defdecode) 59)
  13279. (setq def (apply 'encode-time defdecode)
  13280. defdecode (decode-time def)))))
  13281. (calendar-frame-setup nil)
  13282. (calendar-setup nil)
  13283. (calendar-move-hook nil)
  13284. (calendar-view-diary-initially-flag nil)
  13285. (calendar-view-holidays-initially-flag nil)
  13286. (timestr (format-time-string
  13287. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13288. (prompt (concat (if prompt (concat prompt " ") "")
  13289. (format "Date+time [%s]: " timestr)))
  13290. ans (org-ans0 "") org-ans1 org-ans2 final)
  13291. (cond
  13292. (from-string (setq ans from-string))
  13293. (org-read-date-popup-calendar
  13294. (save-excursion
  13295. (save-window-excursion
  13296. (calendar)
  13297. (unwind-protect
  13298. (progn
  13299. (calendar-forward-day (- (time-to-days def)
  13300. (calendar-absolute-from-gregorian
  13301. (calendar-current-date))))
  13302. (org-eval-in-calendar nil t)
  13303. (let* ((old-map (current-local-map))
  13304. (map (copy-keymap calendar-mode-map))
  13305. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13306. (org-defkey map (kbd "RET") 'org-calendar-select)
  13307. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13308. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13309. (org-defkey minibuffer-local-map [(meta shift left)]
  13310. (lambda () (interactive)
  13311. (org-eval-in-calendar '(calendar-backward-month 1))))
  13312. (org-defkey minibuffer-local-map [(meta shift right)]
  13313. (lambda () (interactive)
  13314. (org-eval-in-calendar '(calendar-forward-month 1))))
  13315. (org-defkey minibuffer-local-map [(meta shift up)]
  13316. (lambda () (interactive)
  13317. (org-eval-in-calendar '(calendar-backward-year 1))))
  13318. (org-defkey minibuffer-local-map [(meta shift down)]
  13319. (lambda () (interactive)
  13320. (org-eval-in-calendar '(calendar-forward-year 1))))
  13321. (org-defkey minibuffer-local-map [?\e (shift left)]
  13322. (lambda () (interactive)
  13323. (org-eval-in-calendar '(calendar-backward-month 1))))
  13324. (org-defkey minibuffer-local-map [?\e (shift right)]
  13325. (lambda () (interactive)
  13326. (org-eval-in-calendar '(calendar-forward-month 1))))
  13327. (org-defkey minibuffer-local-map [?\e (shift up)]
  13328. (lambda () (interactive)
  13329. (org-eval-in-calendar '(calendar-backward-year 1))))
  13330. (org-defkey minibuffer-local-map [?\e (shift down)]
  13331. (lambda () (interactive)
  13332. (org-eval-in-calendar '(calendar-forward-year 1))))
  13333. (org-defkey minibuffer-local-map [(shift up)]
  13334. (lambda () (interactive)
  13335. (org-eval-in-calendar '(calendar-backward-week 1))))
  13336. (org-defkey minibuffer-local-map [(shift down)]
  13337. (lambda () (interactive)
  13338. (org-eval-in-calendar '(calendar-forward-week 1))))
  13339. (org-defkey minibuffer-local-map [(shift left)]
  13340. (lambda () (interactive)
  13341. (org-eval-in-calendar '(calendar-backward-day 1))))
  13342. (org-defkey minibuffer-local-map [(shift right)]
  13343. (lambda () (interactive)
  13344. (org-eval-in-calendar '(calendar-forward-day 1))))
  13345. (org-defkey minibuffer-local-map ">"
  13346. (lambda () (interactive)
  13347. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13348. (org-defkey minibuffer-local-map "<"
  13349. (lambda () (interactive)
  13350. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13351. (org-defkey minibuffer-local-map "\C-v"
  13352. (lambda () (interactive)
  13353. (org-eval-in-calendar
  13354. '(calendar-scroll-left-three-months 1))))
  13355. (org-defkey minibuffer-local-map "\M-v"
  13356. (lambda () (interactive)
  13357. (org-eval-in-calendar
  13358. '(calendar-scroll-right-three-months 1))))
  13359. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13360. (unwind-protect
  13361. (progn
  13362. (use-local-map map)
  13363. (add-hook 'post-command-hook 'org-read-date-display)
  13364. (setq org-ans0 (read-string prompt default-input
  13365. 'org-read-date-history nil))
  13366. ;; org-ans0: from prompt
  13367. ;; org-ans1: from mouse click
  13368. ;; org-ans2: from calendar motion
  13369. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13370. (remove-hook 'post-command-hook 'org-read-date-display)
  13371. (use-local-map old-map)
  13372. (when org-read-date-overlay
  13373. (delete-overlay org-read-date-overlay)
  13374. (setq org-read-date-overlay nil)))))
  13375. (bury-buffer "*Calendar*")))))
  13376. (t ; Naked prompt only
  13377. (unwind-protect
  13378. (setq ans (read-string prompt default-input
  13379. 'org-read-date-history timestr))
  13380. (when org-read-date-overlay
  13381. (delete-overlay org-read-date-overlay)
  13382. (setq org-read-date-overlay nil)))))
  13383. (setq final (org-read-date-analyze ans def defdecode))
  13384. (when org-read-date-analyze-forced-year
  13385. (message "Year was forced into %s"
  13386. (if org-read-date-force-compatible-dates
  13387. "compatible range (1970-2037)"
  13388. "range representable on this machine"))
  13389. (ding))
  13390. ;; One round trip to get rid of 34th of August and stuff like that....
  13391. (setq final (decode-time (apply 'encode-time final)))
  13392. (setq org-read-date-final-answer ans)
  13393. (if to-time
  13394. (apply 'encode-time final)
  13395. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13396. (format "%04d-%02d-%02d %02d:%02d"
  13397. (nth 5 final) (nth 4 final) (nth 3 final)
  13398. (nth 2 final) (nth 1 final))
  13399. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13400. (defvar def)
  13401. (defvar defdecode)
  13402. (defvar with-time)
  13403. (defun org-read-date-display ()
  13404. "Display the current date prompt interpretation in the minibuffer."
  13405. (when org-read-date-display-live
  13406. (when org-read-date-overlay
  13407. (delete-overlay org-read-date-overlay))
  13408. (let ((p (point)))
  13409. (end-of-line 1)
  13410. (while (not (equal (buffer-substring
  13411. (max (point-min) (- (point) 4)) (point))
  13412. " "))
  13413. (insert " "))
  13414. (goto-char p))
  13415. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13416. " " (or org-ans1 org-ans2)))
  13417. (org-end-time-was-given nil)
  13418. (f (org-read-date-analyze ans def defdecode))
  13419. (fmts (if org-dcst
  13420. org-time-stamp-custom-formats
  13421. org-time-stamp-formats))
  13422. (fmt (if (or with-time
  13423. (and (boundp 'org-time-was-given) org-time-was-given))
  13424. (cdr fmts)
  13425. (car fmts)))
  13426. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13427. (when (and org-end-time-was-given
  13428. (string-match org-plain-time-of-day-regexp txt))
  13429. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13430. org-end-time-was-given
  13431. (substring txt (match-end 0)))))
  13432. (when org-read-date-analyze-futurep
  13433. (setq txt (concat txt " (=>F)")))
  13434. (setq org-read-date-overlay
  13435. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13436. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13437. (defun org-read-date-analyze (ans def defdecode)
  13438. "Analyze the combined answer of the date prompt."
  13439. ;; FIXME: cleanup and comment
  13440. (let ((nowdecode (decode-time (current-time)))
  13441. delta deltan deltaw deltadef year month day
  13442. hour minute second wday pm h2 m2 tl wday1
  13443. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13444. (setq org-read-date-analyze-futurep nil
  13445. org-read-date-analyze-forced-year nil)
  13446. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13447. (setq ans "+0"))
  13448. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13449. (setq ans (replace-match "" t t ans)
  13450. deltan (car delta)
  13451. deltaw (nth 1 delta)
  13452. deltadef (nth 2 delta)))
  13453. ;; Check if there is an iso week date in there
  13454. ;; If yes, store the info and postpone interpreting it until the rest
  13455. ;; of the parsing is done
  13456. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13457. (setq iso-year (if (match-end 1)
  13458. (org-small-year-to-year
  13459. (string-to-number (match-string 1 ans))))
  13460. iso-weekday (if (match-end 3)
  13461. (string-to-number (match-string 3 ans)))
  13462. iso-week (string-to-number (match-string 2 ans)))
  13463. (setq ans (replace-match "" t t ans)))
  13464. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13465. (when (string-match
  13466. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13467. (setq year (if (match-end 2)
  13468. (string-to-number (match-string 2 ans))
  13469. (progn (setq kill-year t)
  13470. (string-to-number (format-time-string "%Y"))))
  13471. month (string-to-number (match-string 3 ans))
  13472. day (string-to-number (match-string 4 ans)))
  13473. (if (< year 100) (setq year (+ 2000 year)))
  13474. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13475. t nil ans)))
  13476. ;; Help matching dottet european dates
  13477. (when (string-match
  13478. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13479. (setq year (if (match-end 3)
  13480. (string-to-number (match-string 3 ans))
  13481. (progn (setq kill-year t)
  13482. (string-to-number (format-time-string "%Y"))))
  13483. day (string-to-number (match-string 1 ans))
  13484. month (string-to-number (match-string 2 ans))
  13485. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13486. t nil ans)))
  13487. ;; Help matching american dates, like 5/30 or 5/30/7
  13488. (when (string-match
  13489. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13490. (setq year (if (match-end 4)
  13491. (string-to-number (match-string 4 ans))
  13492. (progn (setq kill-year t)
  13493. (string-to-number (format-time-string "%Y"))))
  13494. month (string-to-number (match-string 1 ans))
  13495. day (string-to-number (match-string 2 ans)))
  13496. (if (< year 100) (setq year (+ 2000 year)))
  13497. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13498. t nil ans)))
  13499. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13500. ;; If there is a time with am/pm, and *no* time without it, we convert
  13501. ;; so that matching will be successful.
  13502. (loop for i from 1 to 2 do ; twice, for end time as well
  13503. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13504. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13505. (setq hour (string-to-number (match-string 1 ans))
  13506. minute (if (match-end 3)
  13507. (string-to-number (match-string 3 ans))
  13508. 0)
  13509. pm (equal ?p
  13510. (string-to-char (downcase (match-string 4 ans)))))
  13511. (if (and (= hour 12) (not pm))
  13512. (setq hour 0)
  13513. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13514. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13515. t t ans))))
  13516. ;; Check if a time range is given as a duration
  13517. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13518. (setq hour (string-to-number (match-string 1 ans))
  13519. h2 (+ hour (string-to-number (match-string 3 ans)))
  13520. minute (string-to-number (match-string 2 ans))
  13521. m2 (+ minute (if (match-end 5) (string-to-number
  13522. (match-string 5 ans))0)))
  13523. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13524. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13525. t t ans)))
  13526. ;; Check if there is a time range
  13527. (when (boundp 'org-end-time-was-given)
  13528. (setq org-time-was-given nil)
  13529. (when (and (string-match org-plain-time-of-day-regexp ans)
  13530. (match-end 8))
  13531. (setq org-end-time-was-given (match-string 8 ans))
  13532. (setq ans (concat (substring ans 0 (match-beginning 7))
  13533. (substring ans (match-end 7))))))
  13534. (setq tl (parse-time-string ans)
  13535. day (or (nth 3 tl) (nth 3 defdecode))
  13536. month (or (nth 4 tl)
  13537. (if (and org-read-date-prefer-future
  13538. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13539. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13540. (nth 4 defdecode)))
  13541. year (or (and (not kill-year) (nth 5 tl))
  13542. (if (and org-read-date-prefer-future
  13543. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13544. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13545. (nth 5 defdecode)))
  13546. hour (or (nth 2 tl) (nth 2 defdecode))
  13547. minute (or (nth 1 tl) (nth 1 defdecode))
  13548. second (or (nth 0 tl) 0)
  13549. wday (nth 6 tl))
  13550. (when (and (eq org-read-date-prefer-future 'time)
  13551. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13552. (equal day (nth 3 nowdecode))
  13553. (equal month (nth 4 nowdecode))
  13554. (equal year (nth 5 nowdecode))
  13555. (nth 2 tl)
  13556. (or (< (nth 2 tl) (nth 2 nowdecode))
  13557. (and (= (nth 2 tl) (nth 2 nowdecode))
  13558. (nth 1 tl)
  13559. (< (nth 1 tl) (nth 1 nowdecode)))))
  13560. (setq day (1+ day)
  13561. futurep t))
  13562. ;; Special date definitions below
  13563. (cond
  13564. (iso-week
  13565. ;; There was an iso week
  13566. (require 'cal-iso)
  13567. (setq futurep nil)
  13568. (setq year (or iso-year year)
  13569. day (or iso-weekday wday 1)
  13570. wday nil ; to make sure that the trigger below does not match
  13571. iso-date (calendar-gregorian-from-absolute
  13572. (calendar-absolute-from-iso
  13573. (list iso-week day year))))
  13574. ; FIXME: Should we also push ISO weeks into the future?
  13575. ; (when (and org-read-date-prefer-future
  13576. ; (not iso-year)
  13577. ; (< (calendar-absolute-from-gregorian iso-date)
  13578. ; (time-to-days (current-time))))
  13579. ; (setq year (1+ year)
  13580. ; iso-date (calendar-gregorian-from-absolute
  13581. ; (calendar-absolute-from-iso
  13582. ; (list iso-week day year)))))
  13583. (setq month (car iso-date)
  13584. year (nth 2 iso-date)
  13585. day (nth 1 iso-date)))
  13586. (deltan
  13587. (setq futurep nil)
  13588. (unless deltadef
  13589. (let ((now (decode-time (current-time))))
  13590. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13591. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13592. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13593. ((equal deltaw "m") (setq month (+ month deltan)))
  13594. ((equal deltaw "y") (setq year (+ year deltan)))))
  13595. ((and wday (not (nth 3 tl)))
  13596. (setq futurep nil)
  13597. ;; Weekday was given, but no day, so pick that day in the week
  13598. ;; on or after the derived date.
  13599. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13600. (unless (equal wday wday1)
  13601. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13602. (if (and (boundp 'org-time-was-given)
  13603. (nth 2 tl))
  13604. (setq org-time-was-given t))
  13605. (if (< year 100) (setq year (+ 2000 year)))
  13606. ;; Check of the date is representable
  13607. (if org-read-date-force-compatible-dates
  13608. (progn
  13609. (if (< year 1970)
  13610. (setq year 1970 org-read-date-analyze-forced-year t))
  13611. (if (> year 2037)
  13612. (setq year 2037 org-read-date-analyze-forced-year t)))
  13613. (condition-case nil
  13614. (ignore (encode-time second minute hour day month year))
  13615. (error
  13616. (setq year (nth 5 defdecode))
  13617. (setq org-read-date-analyze-forced-year t))))
  13618. (setq org-read-date-analyze-futurep futurep)
  13619. (list second minute hour day month year)))
  13620. (defvar parse-time-weekdays)
  13621. (defun org-read-date-get-relative (s today default)
  13622. "Check string S for special relative date string.
  13623. TODAY and DEFAULT are internal times, for today and for a default.
  13624. Return shift list (N what def-flag)
  13625. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13626. N is the number of WHATs to shift.
  13627. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13628. the DEFAULT date rather than TODAY."
  13629. (when (and
  13630. (string-match
  13631. (concat
  13632. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13633. "\\([0-9]+\\)?"
  13634. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13635. "\\([ \t]\\|$\\)") s)
  13636. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13637. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13638. (string-to-char (substring (match-string 1 s) -1))
  13639. ?+))
  13640. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13641. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13642. (what (if (match-end 3) (match-string 3 s) "d"))
  13643. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13644. (date (if rel default today))
  13645. (wday (nth 6 (decode-time date)))
  13646. delta)
  13647. (if wday1
  13648. (progn
  13649. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13650. (if (= dir ?-) (setq delta (- delta 7)))
  13651. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13652. (list delta "d" rel))
  13653. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13654. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13655. "Turn a user-specified date into the internal representation.
  13656. The internal representation needed by the calendar is (month day year).
  13657. This is a wrapper to handle the brain-dead convention in calendar that
  13658. user function argument order change dependent on argument order."
  13659. (if (boundp 'calendar-date-style)
  13660. (cond
  13661. ((eq calendar-date-style 'american)
  13662. (list arg1 arg2 arg3))
  13663. ((eq calendar-date-style 'european)
  13664. (list arg2 arg1 arg3))
  13665. ((eq calendar-date-style 'iso)
  13666. (list arg2 arg3 arg1)))
  13667. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  13668. (if (org-bound-and-true-p european-calendar-style)
  13669. (list arg2 arg1 arg3)
  13670. (list arg1 arg2 arg3)))))
  13671. (defun org-eval-in-calendar (form &optional keepdate)
  13672. "Eval FORM in the calendar window and return to current window.
  13673. Also, store the cursor date in variable org-ans2."
  13674. (let ((sf (selected-frame))
  13675. (sw (selected-window)))
  13676. (select-window (get-buffer-window "*Calendar*" t))
  13677. (eval form)
  13678. (when (and (not keepdate) (calendar-cursor-to-date))
  13679. (let* ((date (calendar-cursor-to-date))
  13680. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13681. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13682. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13683. (select-window sw)
  13684. (org-select-frame-set-input-focus sf)))
  13685. (defun org-calendar-select ()
  13686. "Return to `org-read-date' with the date currently selected.
  13687. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13688. (interactive)
  13689. (when (calendar-cursor-to-date)
  13690. (let* ((date (calendar-cursor-to-date))
  13691. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13692. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13693. (if (active-minibuffer-window) (exit-minibuffer))))
  13694. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13695. "Insert a date stamp for the date given by the internal TIME.
  13696. WITH-HM means use the stamp format that includes the time of the day.
  13697. INACTIVE means use square brackets instead of angular ones, so that the
  13698. stamp will not contribute to the agenda.
  13699. PRE and POST are optional strings to be inserted before and after the
  13700. stamp.
  13701. The command returns the inserted time stamp."
  13702. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13703. stamp)
  13704. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13705. (insert-before-markers (or pre ""))
  13706. (when (listp extra)
  13707. (setq extra (car extra))
  13708. (if (and (stringp extra)
  13709. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13710. (setq extra (format "-%02d:%02d"
  13711. (string-to-number (match-string 1 extra))
  13712. (string-to-number (match-string 2 extra))))
  13713. (setq extra nil)))
  13714. (when extra
  13715. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  13716. (insert-before-markers (setq stamp (format-time-string fmt time)))
  13717. (insert-before-markers (or post ""))
  13718. (setq org-last-inserted-timestamp stamp)))
  13719. (defun org-toggle-time-stamp-overlays ()
  13720. "Toggle the use of custom time stamp formats."
  13721. (interactive)
  13722. (setq org-display-custom-times (not org-display-custom-times))
  13723. (unless org-display-custom-times
  13724. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13725. (while (setq p (next-single-property-change p 'display))
  13726. (if (and (get-text-property p 'display)
  13727. (eq (get-text-property p 'face) 'org-date))
  13728. (remove-text-properties
  13729. p (setq p (next-single-property-change p 'display))
  13730. '(display t))))
  13731. (set-buffer-modified-p bmp)))
  13732. (if (featurep 'xemacs)
  13733. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13734. (org-restart-font-lock)
  13735. (setq org-table-may-need-update t)
  13736. (if org-display-custom-times
  13737. (message "Time stamps are overlayed with custom format")
  13738. (message "Time stamp overlays removed")))
  13739. (defun org-display-custom-time (beg end)
  13740. "Overlay modified time stamp format over timestamp between BEG and END."
  13741. (let* ((ts (buffer-substring beg end))
  13742. t1 w1 with-hm tf time str w2 (off 0))
  13743. (save-match-data
  13744. (setq t1 (org-parse-time-string ts t))
  13745. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13746. (setq off (- (match-end 0) (match-beginning 0)))))
  13747. (setq end (- end off))
  13748. (setq w1 (- end beg)
  13749. with-hm (and (nth 1 t1) (nth 2 t1))
  13750. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13751. time (org-fix-decoded-time t1)
  13752. str (org-add-props
  13753. (format-time-string
  13754. (substring tf 1 -1) (apply 'encode-time time))
  13755. nil 'mouse-face 'highlight)
  13756. w2 (length str))
  13757. (if (not (= w2 w1))
  13758. (add-text-properties (1+ beg) (+ 2 beg)
  13759. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13760. (if (featurep 'xemacs)
  13761. (progn
  13762. (put-text-property beg end 'invisible t)
  13763. (put-text-property beg end 'end-glyph (make-glyph str)))
  13764. (put-text-property beg end 'display str))))
  13765. (defun org-translate-time (string)
  13766. "Translate all timestamps in STRING to custom format.
  13767. But do this only if the variable `org-display-custom-times' is set."
  13768. (when org-display-custom-times
  13769. (save-match-data
  13770. (let* ((start 0)
  13771. (re org-ts-regexp-both)
  13772. t1 with-hm inactive tf time str beg end)
  13773. (while (setq start (string-match re string start))
  13774. (setq beg (match-beginning 0)
  13775. end (match-end 0)
  13776. t1 (save-match-data
  13777. (org-parse-time-string (substring string beg end) t))
  13778. with-hm (and (nth 1 t1) (nth 2 t1))
  13779. inactive (equal (substring string beg (1+ beg)) "[")
  13780. tf (funcall (if with-hm 'cdr 'car)
  13781. org-time-stamp-custom-formats)
  13782. time (org-fix-decoded-time t1)
  13783. str (format-time-string
  13784. (concat
  13785. (if inactive "[" "<") (substring tf 1 -1)
  13786. (if inactive "]" ">"))
  13787. (apply 'encode-time time))
  13788. string (replace-match str t t string)
  13789. start (+ start (length str)))))))
  13790. string)
  13791. (defun org-fix-decoded-time (time)
  13792. "Set 0 instead of nil for the first 6 elements of time.
  13793. Don't touch the rest."
  13794. (let ((n 0))
  13795. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13796. (defun org-days-to-time (timestamp-string)
  13797. "Difference between TIMESTAMP-STRING and now in days."
  13798. (- (time-to-days (org-time-string-to-time timestamp-string))
  13799. (time-to-days (current-time))))
  13800. (defun org-deadline-close (timestamp-string &optional ndays)
  13801. "Is the time in TIMESTAMP-STRING close to the current date?"
  13802. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13803. (and (< (org-days-to-time timestamp-string) ndays)
  13804. (not (org-entry-is-done-p))))
  13805. (defun org-get-wdays (ts)
  13806. "Get the deadline lead time appropriate for timestring TS."
  13807. (cond
  13808. ((<= org-deadline-warning-days 0)
  13809. ;; 0 or negative, enforce this value no matter what
  13810. (- org-deadline-warning-days))
  13811. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13812. ;; lead time is specified.
  13813. (floor (* (string-to-number (match-string 1 ts))
  13814. (cdr (assoc (match-string 2 ts)
  13815. '(("d" . 1) ("w" . 7)
  13816. ("m" . 30.4) ("y" . 365.25)))))))
  13817. ;; go for the default.
  13818. (t org-deadline-warning-days)))
  13819. (defun org-calendar-select-mouse (ev)
  13820. "Return to `org-read-date' with the date currently selected.
  13821. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13822. (interactive "e")
  13823. (mouse-set-point ev)
  13824. (when (calendar-cursor-to-date)
  13825. (let* ((date (calendar-cursor-to-date))
  13826. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13827. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13828. (if (active-minibuffer-window) (exit-minibuffer))))
  13829. (defun org-check-deadlines (ndays)
  13830. "Check if there are any deadlines due or past due.
  13831. A deadline is considered due if it happens within `org-deadline-warning-days'
  13832. days from today's date. If the deadline appears in an entry marked DONE,
  13833. it is not shown. The prefix arg NDAYS can be used to test that many
  13834. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13835. (interactive "P")
  13836. (let* ((org-warn-days
  13837. (cond
  13838. ((equal ndays '(4)) 100000)
  13839. (ndays (prefix-numeric-value ndays))
  13840. (t (abs org-deadline-warning-days))))
  13841. (case-fold-search nil)
  13842. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13843. (callback
  13844. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13845. (message "%d deadlines past-due or due within %d days"
  13846. (org-occur regexp nil callback)
  13847. org-warn-days)))
  13848. (defun org-check-before-date (date)
  13849. "Check if there are deadlines or scheduled entries before DATE."
  13850. (interactive (list (org-read-date)))
  13851. (let ((case-fold-search nil)
  13852. (regexp (concat "\\<\\(" org-deadline-string
  13853. "\\|" org-scheduled-string
  13854. "\\) *<\\([^>]+\\)>"))
  13855. (callback
  13856. (lambda () (time-less-p
  13857. (org-time-string-to-time (match-string 2))
  13858. (org-time-string-to-time date)))))
  13859. (message "%d entries before %s"
  13860. (org-occur regexp nil callback) date)))
  13861. (defun org-check-after-date (date)
  13862. "Check if there are deadlines or scheduled entries after DATE."
  13863. (interactive (list (org-read-date)))
  13864. (let ((case-fold-search nil)
  13865. (regexp (concat "\\<\\(" org-deadline-string
  13866. "\\|" org-scheduled-string
  13867. "\\) *<\\([^>]+\\)>"))
  13868. (callback
  13869. (lambda () (not
  13870. (time-less-p
  13871. (org-time-string-to-time (match-string 2))
  13872. (org-time-string-to-time date))))))
  13873. (message "%d entries after %s"
  13874. (org-occur regexp nil callback) date)))
  13875. (defun org-evaluate-time-range (&optional to-buffer)
  13876. "Evaluate a time range by computing the difference between start and end.
  13877. Normally the result is just printed in the echo area, but with prefix arg
  13878. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13879. If the time range is actually in a table, the result is inserted into the
  13880. next column.
  13881. For time difference computation, a year is assumed to be exactly 365
  13882. days in order to avoid rounding problems."
  13883. (interactive "P")
  13884. (or
  13885. (org-clock-update-time-maybe)
  13886. (save-excursion
  13887. (unless (org-at-date-range-p t)
  13888. (goto-char (point-at-bol))
  13889. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13890. (if (not (org-at-date-range-p t))
  13891. (error "Not at a time-stamp range, and none found in current line")))
  13892. (let* ((ts1 (match-string 1))
  13893. (ts2 (match-string 2))
  13894. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13895. (match-end (match-end 0))
  13896. (time1 (org-time-string-to-time ts1))
  13897. (time2 (org-time-string-to-time ts2))
  13898. (t1 (org-float-time time1))
  13899. (t2 (org-float-time time2))
  13900. (diff (abs (- t2 t1)))
  13901. (negative (< (- t2 t1) 0))
  13902. ;; (ys (floor (* 365 24 60 60)))
  13903. (ds (* 24 60 60))
  13904. (hs (* 60 60))
  13905. (fy "%dy %dd %02d:%02d")
  13906. (fy1 "%dy %dd")
  13907. (fd "%dd %02d:%02d")
  13908. (fd1 "%dd")
  13909. (fh "%02d:%02d")
  13910. y d h m align)
  13911. (if havetime
  13912. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13913. y 0
  13914. d (floor (/ diff ds)) diff (mod diff ds)
  13915. h (floor (/ diff hs)) diff (mod diff hs)
  13916. m (floor (/ diff 60)))
  13917. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13918. y 0
  13919. d (floor (+ (/ diff ds) 0.5))
  13920. h 0 m 0))
  13921. (if (not to-buffer)
  13922. (message "%s" (org-make-tdiff-string y d h m))
  13923. (if (org-at-table-p)
  13924. (progn
  13925. (goto-char match-end)
  13926. (setq align t)
  13927. (and (looking-at " *|") (goto-char (match-end 0))))
  13928. (goto-char match-end))
  13929. (if (looking-at
  13930. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13931. (replace-match ""))
  13932. (if negative (insert " -"))
  13933. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13934. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13935. (insert " " (format fh h m))))
  13936. (if align (org-table-align))
  13937. (message "Time difference inserted")))))
  13938. (defun org-make-tdiff-string (y d h m)
  13939. (let ((fmt "")
  13940. (l nil))
  13941. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13942. l (push y l)))
  13943. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13944. l (push d l)))
  13945. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13946. l (push h l)))
  13947. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13948. l (push m l)))
  13949. (apply 'format fmt (nreverse l))))
  13950. (defun org-time-string-to-time (s)
  13951. (apply 'encode-time (org-parse-time-string s)))
  13952. (defun org-time-string-to-seconds (s)
  13953. (org-float-time (org-time-string-to-time s)))
  13954. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13955. "Convert a time stamp to an absolute day number.
  13956. If there is a specifier for a cyclic time stamp, get the closest date to
  13957. DAYNR.
  13958. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13959. The variable date is bound by the calendar when this is called."
  13960. (cond
  13961. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13962. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13963. daynr
  13964. (+ daynr 1000)))
  13965. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13966. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13967. (time-to-days (current-time))) (match-string 0 s)
  13968. prefer show-all))
  13969. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13970. (defun org-days-to-iso-week (days)
  13971. "Return the iso week number."
  13972. (require 'cal-iso)
  13973. (car (calendar-iso-from-absolute days)))
  13974. (defun org-small-year-to-year (year)
  13975. "Convert 2-digit years into 4-digit years.
  13976. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13977. The year 2000 cannot be abbreviated. Any year larger than 99
  13978. is returned unchanged."
  13979. (if (< year 38)
  13980. (setq year (+ 2000 year))
  13981. (if (< year 100)
  13982. (setq year (+ 1900 year))))
  13983. year)
  13984. (defun org-time-from-absolute (d)
  13985. "Return the time corresponding to date D.
  13986. D may be an absolute day number, or a calendar-type list (month day year)."
  13987. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13988. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13989. (defun org-calendar-holiday ()
  13990. "List of holidays, for Diary display in Org-mode."
  13991. (require 'holidays)
  13992. (let ((hl (funcall
  13993. (if (fboundp 'calendar-check-holidays)
  13994. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13995. (if hl (mapconcat 'identity hl "; "))))
  13996. (defun org-diary-sexp-entry (sexp entry date)
  13997. "Process a SEXP diary ENTRY for DATE."
  13998. (require 'diary-lib)
  13999. (let ((result (if calendar-debug-sexp
  14000. (let ((stack-trace-on-error t))
  14001. (eval (car (read-from-string sexp))))
  14002. (condition-case nil
  14003. (eval (car (read-from-string sexp)))
  14004. (error
  14005. (beep)
  14006. (message "Bad sexp at line %d in %s: %s"
  14007. (org-current-line)
  14008. (buffer-file-name) sexp)
  14009. (sleep-for 2))))))
  14010. (cond ((stringp result) (split-string result "; "))
  14011. ((and (consp result)
  14012. (not (consp (cdr result)))
  14013. (stringp (cdr result))) (cdr result))
  14014. ((and (consp result)
  14015. (stringp (car result))) result)
  14016. (result entry)
  14017. (t nil))))
  14018. (defun org-diary-to-ical-string (frombuf)
  14019. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14020. This uses the icalendar.el library."
  14021. (let* ((tmpdir (if (featurep 'xemacs)
  14022. (temp-directory)
  14023. temporary-file-directory))
  14024. (tmpfile (make-temp-name
  14025. (expand-file-name "orgics" tmpdir)))
  14026. buf rtn b e)
  14027. (with-current-buffer frombuf
  14028. (icalendar-export-region (point-min) (point-max) tmpfile)
  14029. (setq buf (find-buffer-visiting tmpfile))
  14030. (set-buffer buf)
  14031. (goto-char (point-min))
  14032. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14033. (setq b (match-beginning 0)))
  14034. (goto-char (point-max))
  14035. (if (re-search-backward "^END:VEVENT" nil t)
  14036. (setq e (match-end 0)))
  14037. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14038. (kill-buffer buf)
  14039. (delete-file tmpfile)
  14040. rtn))
  14041. (defun org-closest-date (start current change prefer show-all)
  14042. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14043. When PREFER is `past', return a date that is either CURRENT or past.
  14044. When PREFER is `future', return a date that is either CURRENT or future.
  14045. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14046. ;; Make the proper lists from the dates
  14047. (catch 'exit
  14048. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14049. dn dw sday cday n1 n2 n0
  14050. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14051. (setq start (org-date-to-gregorian start)
  14052. current (org-date-to-gregorian
  14053. (if show-all
  14054. current
  14055. (time-to-days (current-time))))
  14056. sday (calendar-absolute-from-gregorian start)
  14057. cday (calendar-absolute-from-gregorian current))
  14058. (if (<= cday sday) (throw 'exit sday))
  14059. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  14060. (setq dn (string-to-number (match-string 1 change))
  14061. dw (cdr (assoc (match-string 2 change) a1)))
  14062. (error "Invalid change specifier: %s" change))
  14063. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14064. (cond
  14065. ((eq dw 'day)
  14066. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14067. n2 (+ n1 dn)))
  14068. ((eq dw 'year)
  14069. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14070. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14071. (setq date1 (list m d y1)
  14072. n1 (calendar-absolute-from-gregorian date1)
  14073. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14074. n2 (calendar-absolute-from-gregorian date2)))
  14075. ((eq dw 'month)
  14076. ;; approx number of month between the two dates
  14077. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14078. ;; How often does dn fit in there?
  14079. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14080. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14081. m (+ m nm)
  14082. ny (floor (/ m 12))
  14083. y (+ y ny)
  14084. m (- m (* ny 12)))
  14085. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14086. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14087. (setq m2 (+ m dn) y2 y)
  14088. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14089. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14090. (while (<= n2 cday)
  14091. (setq n1 n2 m m2 y y2)
  14092. (setq m2 (+ m dn) y2 y)
  14093. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14094. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14095. ;; Make sure n1 is the earlier date
  14096. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14097. (if show-all
  14098. (cond
  14099. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14100. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14101. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14102. (cond
  14103. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14104. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14105. (t (if (= cday n1) n1 n2)))))))
  14106. (defun org-date-to-gregorian (date)
  14107. "Turn any specification of DATE into a Gregorian date for the calendar."
  14108. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14109. ((and (listp date) (= (length date) 3)) date)
  14110. ((stringp date)
  14111. (setq date (org-parse-time-string date))
  14112. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14113. ((listp date)
  14114. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14115. (defun org-parse-time-string (s &optional nodefault)
  14116. "Parse the standard Org-mode time string.
  14117. This should be a lot faster than the normal `parse-time-string'.
  14118. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14119. hour and minute fields will be nil if not given."
  14120. (if (string-match org-ts-regexp0 s)
  14121. (list 0
  14122. (if (or (match-beginning 8) (not nodefault))
  14123. (string-to-number (or (match-string 8 s) "0")))
  14124. (if (or (match-beginning 7) (not nodefault))
  14125. (string-to-number (or (match-string 7 s) "0")))
  14126. (string-to-number (match-string 4 s))
  14127. (string-to-number (match-string 3 s))
  14128. (string-to-number (match-string 2 s))
  14129. nil nil nil)
  14130. (error "Not a standard Org-mode time string: %s" s)))
  14131. (defun org-timestamp-up (&optional arg)
  14132. "Increase the date item at the cursor by one.
  14133. If the cursor is on the year, change the year. If it is on the month or
  14134. the day, change that.
  14135. With prefix ARG, change by that many units."
  14136. (interactive "p")
  14137. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14138. (defun org-timestamp-down (&optional arg)
  14139. "Decrease the date item at the cursor by one.
  14140. If the cursor is on the year, change the year. If it is on the month or
  14141. the day, change that.
  14142. With prefix ARG, change by that many units."
  14143. (interactive "p")
  14144. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14145. (defun org-timestamp-up-day (&optional arg)
  14146. "Increase the date in the time stamp by one day.
  14147. With prefix ARG, change that many days."
  14148. (interactive "p")
  14149. (if (and (not (org-at-timestamp-p t))
  14150. (org-on-heading-p))
  14151. (org-todo 'up)
  14152. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14153. (defun org-timestamp-down-day (&optional arg)
  14154. "Decrease the date in the time stamp by one day.
  14155. With prefix ARG, change that many days."
  14156. (interactive "p")
  14157. (if (and (not (org-at-timestamp-p t))
  14158. (org-on-heading-p))
  14159. (org-todo 'down)
  14160. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14161. (defun org-at-timestamp-p (&optional inactive-ok)
  14162. "Determine if the cursor is in or at a timestamp."
  14163. (interactive)
  14164. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14165. (pos (point))
  14166. (ans (or (looking-at tsr)
  14167. (save-excursion
  14168. (skip-chars-backward "^[<\n\r\t")
  14169. (if (> (point) (point-min)) (backward-char 1))
  14170. (and (looking-at tsr)
  14171. (> (- (match-end 0) pos) -1))))))
  14172. (and ans
  14173. (boundp 'org-ts-what)
  14174. (setq org-ts-what
  14175. (cond
  14176. ((= pos (match-beginning 0)) 'bracket)
  14177. ((= pos (1- (match-end 0))) 'bracket)
  14178. ((org-pos-in-match-range pos 2) 'year)
  14179. ((org-pos-in-match-range pos 3) 'month)
  14180. ((org-pos-in-match-range pos 7) 'hour)
  14181. ((org-pos-in-match-range pos 8) 'minute)
  14182. ((or (org-pos-in-match-range pos 4)
  14183. (org-pos-in-match-range pos 5)) 'day)
  14184. ((and (> pos (or (match-end 8) (match-end 5)))
  14185. (< pos (match-end 0)))
  14186. (- pos (or (match-end 8) (match-end 5))))
  14187. (t 'day))))
  14188. ans))
  14189. (defun org-toggle-timestamp-type ()
  14190. "Toggle the type (<active> or [inactive]) of a time stamp."
  14191. (interactive)
  14192. (when (org-at-timestamp-p t)
  14193. (let ((beg (match-beginning 0)) (end (match-end 0))
  14194. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14195. (save-excursion
  14196. (goto-char beg)
  14197. (while (re-search-forward "[][<>]" end t)
  14198. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14199. t t)))
  14200. (message "Timestamp is now %sactive"
  14201. (if (equal (char-after beg) ?<) "" "in")))))
  14202. (defun org-timestamp-change (n &optional what updown)
  14203. "Change the date in the time stamp at point.
  14204. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14205. `year', `minute', `second'. If WHAT is not given, the cursor position
  14206. in the timestamp determines what will be changed."
  14207. (let ((pos (point))
  14208. with-hm inactive
  14209. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14210. org-ts-what
  14211. extra rem
  14212. ts time time0)
  14213. (if (not (org-at-timestamp-p t))
  14214. (error "Not at a timestamp"))
  14215. (if (and (not what) (eq org-ts-what 'bracket))
  14216. (org-toggle-timestamp-type)
  14217. (if (and (not what) (not (eq org-ts-what 'day))
  14218. org-display-custom-times
  14219. (get-text-property (point) 'display)
  14220. (not (get-text-property (1- (point)) 'display)))
  14221. (setq org-ts-what 'day))
  14222. (setq org-ts-what (or what org-ts-what)
  14223. inactive (= (char-after (match-beginning 0)) ?\[)
  14224. ts (match-string 0))
  14225. (replace-match "")
  14226. (if (string-match
  14227. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14228. ts)
  14229. (setq extra (match-string 1 ts)))
  14230. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14231. (setq with-hm t))
  14232. (setq time0 (org-parse-time-string ts))
  14233. (when (and updown
  14234. (eq org-ts-what 'minute)
  14235. (not current-prefix-arg))
  14236. ;; This looks like s-up and s-down. Change by one rounding step.
  14237. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14238. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14239. (setcar (cdr time0) (+ (nth 1 time0)
  14240. (if (> n 0) (- rem) (- dm rem))))))
  14241. (setq time
  14242. (encode-time (or (car time0) 0)
  14243. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14244. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14245. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14246. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14247. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14248. (nthcdr 6 time0)))
  14249. (when (and (member org-ts-what '(hour minute))
  14250. extra
  14251. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14252. (setq extra (org-modify-ts-extra
  14253. extra
  14254. (if (eq org-ts-what 'hour) 2 5)
  14255. n dm)))
  14256. (when (integerp org-ts-what)
  14257. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14258. (if (eq what 'calendar)
  14259. (let ((cal-date (org-get-date-from-calendar)))
  14260. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14261. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14262. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14263. (setcar time0 (or (car time0) 0))
  14264. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14265. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14266. (setq time (apply 'encode-time time0))))
  14267. (setq org-last-changed-timestamp
  14268. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14269. (org-clock-update-time-maybe)
  14270. (goto-char pos)
  14271. ;; Try to recenter the calendar window, if any
  14272. (if (and org-calendar-follow-timestamp-change
  14273. (get-buffer-window "*Calendar*" t)
  14274. (memq org-ts-what '(day month year)))
  14275. (org-recenter-calendar (time-to-days time))))))
  14276. (defun org-modify-ts-extra (s pos n dm)
  14277. "Change the different parts of the lead-time and repeat fields in timestamp."
  14278. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14279. ng h m new rem)
  14280. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14281. (cond
  14282. ((or (org-pos-in-match-range pos 2)
  14283. (org-pos-in-match-range pos 3))
  14284. (setq m (string-to-number (match-string 3 s))
  14285. h (string-to-number (match-string 2 s)))
  14286. (if (org-pos-in-match-range pos 2)
  14287. (setq h (+ h n))
  14288. (setq n (* dm (org-no-warnings (signum n))))
  14289. (when (not (= 0 (setq rem (% m dm))))
  14290. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14291. (setq m (+ m n)))
  14292. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14293. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14294. (setq h (min 24 (max 0 h)))
  14295. (setq ng 1 new (format "-%02d:%02d" h m)))
  14296. ((org-pos-in-match-range pos 6)
  14297. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14298. ((org-pos-in-match-range pos 5)
  14299. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14300. ((org-pos-in-match-range pos 9)
  14301. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14302. ((org-pos-in-match-range pos 8)
  14303. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14304. (when ng
  14305. (setq s (concat
  14306. (substring s 0 (match-beginning ng))
  14307. new
  14308. (substring s (match-end ng))))))
  14309. s))
  14310. (defun org-recenter-calendar (date)
  14311. "If the calendar is visible, recenter it to DATE."
  14312. (let* ((win (selected-window))
  14313. (cwin (get-buffer-window "*Calendar*" t))
  14314. (calendar-move-hook nil))
  14315. (when cwin
  14316. (select-window cwin)
  14317. (calendar-goto-date (if (listp date) date
  14318. (calendar-gregorian-from-absolute date)))
  14319. (select-window win))))
  14320. (defun org-goto-calendar (&optional arg)
  14321. "Go to the Emacs calendar at the current date.
  14322. If there is a time stamp in the current line, go to that date.
  14323. A prefix ARG can be used to force the current date."
  14324. (interactive "P")
  14325. (let ((tsr org-ts-regexp) diff
  14326. (calendar-move-hook nil)
  14327. (calendar-view-holidays-initially-flag nil)
  14328. (calendar-view-diary-initially-flag nil))
  14329. (if (or (org-at-timestamp-p)
  14330. (save-excursion
  14331. (beginning-of-line 1)
  14332. (looking-at (concat ".*" tsr))))
  14333. (let ((d1 (time-to-days (current-time)))
  14334. (d2 (time-to-days
  14335. (org-time-string-to-time (match-string 1)))))
  14336. (setq diff (- d2 d1))))
  14337. (calendar)
  14338. (calendar-goto-today)
  14339. (if (and diff (not arg)) (calendar-forward-day diff))))
  14340. (defun org-get-date-from-calendar ()
  14341. "Return a list (month day year) of date at point in calendar."
  14342. (with-current-buffer "*Calendar*"
  14343. (save-match-data
  14344. (calendar-cursor-to-date))))
  14345. (defun org-date-from-calendar ()
  14346. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14347. If there is already a time stamp at the cursor position, update it."
  14348. (interactive)
  14349. (if (org-at-timestamp-p t)
  14350. (org-timestamp-change 0 'calendar)
  14351. (let ((cal-date (org-get-date-from-calendar)))
  14352. (org-insert-time-stamp
  14353. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14354. (defun org-minutes-to-hh:mm-string (m)
  14355. "Compute H:MM from a number of minutes."
  14356. (let ((h (/ m 60)))
  14357. (setq m (- m (* 60 h)))
  14358. (format org-time-clocksum-format h m)))
  14359. (defun org-hh:mm-string-to-minutes (s)
  14360. "Convert a string H:MM to a number of minutes.
  14361. If the string is just a number, interpret it as minutes.
  14362. In fact, the first hh:mm or number in the string will be taken,
  14363. there can be extra stuff in the string.
  14364. If no number is found, the return value is 0."
  14365. (cond
  14366. ((integerp s) s)
  14367. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14368. (+ (* (string-to-number (match-string 1 s)) 60)
  14369. (string-to-number (match-string 2 s))))
  14370. ((string-match "\\([0-9]+\\)" s)
  14371. (string-to-number (match-string 1 s)))
  14372. (t 0)))
  14373. (defcustom org-effort-durations
  14374. `(("h" . 60)
  14375. ("d" . ,(* 60 8))
  14376. ("w" . ,(* 60 8 5))
  14377. ("m" . ,(* 60 8 5 4))
  14378. ("y" . ,(* 60 8 5 40)))
  14379. "Conversion factor to minutes for an effort modifier.
  14380. Each entry has the form (MODIFIER . MINUTES).
  14381. In an effort string, a number followed by MODIFIER is multiplied
  14382. by the specified number of MINUTES to obtain an effort in
  14383. minutes.
  14384. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14385. effort string \"2hours\" is equivalent to 120 minutes."
  14386. :group 'org-agenda
  14387. :type '(alist :key-type (string :tag "Modifier")
  14388. :value-type (number :tag "Minutes")))
  14389. (defun org-duration-string-to-minutes (s)
  14390. "Convert a duration string S to minutes.
  14391. A bare number is interpreted as minutes, modifiers can be set by
  14392. customizing `org-effort-durations' (which see).
  14393. Entries containing a colon are interpreted as H:MM by
  14394. `org-hh:mm-string-to-minutes'."
  14395. (let ((result 0)
  14396. (re (concat "\\([0-9]+\\) *\\("
  14397. (regexp-opt (mapcar 'car org-effort-durations))
  14398. "\\)")))
  14399. (while (string-match re s)
  14400. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14401. (string-to-number (match-string 1 s))))
  14402. (setq s (replace-match "" nil t s)))
  14403. (incf result (org-hh:mm-string-to-minutes s))
  14404. result))
  14405. ;;;; Files
  14406. (defun org-save-all-org-buffers ()
  14407. "Save all Org-mode buffers without user confirmation."
  14408. (interactive)
  14409. (message "Saving all Org-mode buffers...")
  14410. (save-some-buffers t 'org-mode-p)
  14411. (when (featurep 'org-id) (org-id-locations-save))
  14412. (message "Saving all Org-mode buffers... done"))
  14413. (defun org-revert-all-org-buffers ()
  14414. "Revert all Org-mode buffers.
  14415. Prompt for confirmation when there are unsaved changes.
  14416. Be sure you know what you are doing before letting this function
  14417. overwrite your changes.
  14418. This function is useful in a setup where one tracks org files
  14419. with a version control system, to revert on one machine after pulling
  14420. changes from another. I believe the procedure must be like this:
  14421. 1. M-x org-save-all-org-buffers
  14422. 2. Pull changes from the other machine, resolve conflicts
  14423. 3. M-x org-revert-all-org-buffers"
  14424. (interactive)
  14425. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14426. (error "Abort"))
  14427. (save-excursion
  14428. (save-window-excursion
  14429. (mapc
  14430. (lambda (b)
  14431. (when (and (with-current-buffer b (org-mode-p))
  14432. (with-current-buffer b buffer-file-name))
  14433. (switch-to-buffer b)
  14434. (revert-buffer t 'no-confirm)))
  14435. (buffer-list))
  14436. (when (and (featurep 'org-id) org-id-track-globally)
  14437. (org-id-locations-load)))))
  14438. ;;;; Agenda files
  14439. ;;;###autoload
  14440. (defun org-switchb (&optional arg)
  14441. "Switch between Org buffers.
  14442. With a prefix argument, restrict available to files.
  14443. With two prefix arguments, restrict available buffers to agenda files.
  14444. Defaults to `iswitchb' for buffer name completion.
  14445. Set `org-completion-use-ido' to make it use ido instead."
  14446. (interactive "P")
  14447. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14448. ((equal arg '(16)) (org-buffer-list 'agenda))
  14449. (t (org-buffer-list))))
  14450. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14451. (org-completion-use-ido org-completion-use-ido))
  14452. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14453. (setq org-completion-use-iswitchb t))
  14454. (switch-to-buffer
  14455. (org-icompleting-read "Org buffer: "
  14456. (mapcar 'list (mapcar 'buffer-name blist))
  14457. nil t))))
  14458. ;;; Define some older names previously used for this functionality
  14459. ;;;###autoload
  14460. (defalias 'org-ido-switchb 'org-switchb)
  14461. ;;;###autoload
  14462. (defalias 'org-iswitchb 'org-switchb)
  14463. (defun org-buffer-list (&optional predicate exclude-tmp)
  14464. "Return a list of Org buffers.
  14465. PREDICATE can be `export', `files' or `agenda'.
  14466. export restrict the list to Export buffers.
  14467. files restrict the list to buffers visiting Org files.
  14468. agenda restrict the list to buffers visiting agenda files.
  14469. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14470. (let* ((bfn nil)
  14471. (agenda-files (and (eq predicate 'agenda)
  14472. (mapcar 'file-truename (org-agenda-files t))))
  14473. (filter
  14474. (cond
  14475. ((eq predicate 'files)
  14476. (lambda (b) (with-current-buffer b (org-mode-p))))
  14477. ((eq predicate 'export)
  14478. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14479. ((eq predicate 'agenda)
  14480. (lambda (b)
  14481. (with-current-buffer b
  14482. (and (org-mode-p)
  14483. (setq bfn (buffer-file-name b))
  14484. (member (file-truename bfn) agenda-files)))))
  14485. (t (lambda (b) (with-current-buffer b
  14486. (or (org-mode-p)
  14487. (string-match "\*Org .*Export"
  14488. (buffer-name b)))))))))
  14489. (delq nil
  14490. (mapcar
  14491. (lambda(b)
  14492. (if (and (funcall filter b)
  14493. (or (not exclude-tmp)
  14494. (not (string-match "tmp" (buffer-name b)))))
  14495. b
  14496. nil))
  14497. (buffer-list)))))
  14498. (defun org-agenda-files (&optional unrestricted archives)
  14499. "Get the list of agenda files.
  14500. Optional UNRESTRICTED means return the full list even if a restriction
  14501. is currently in place.
  14502. When ARCHIVES is t, include all archive files that are really being
  14503. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14504. `org-agenda-archives-mode' is t."
  14505. (let ((files
  14506. (cond
  14507. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14508. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14509. ((listp org-agenda-files) org-agenda-files)
  14510. (t (error "Invalid value of `org-agenda-files'")))))
  14511. (setq files (apply 'append
  14512. (mapcar (lambda (f)
  14513. (if (file-directory-p f)
  14514. (directory-files
  14515. f t org-agenda-file-regexp)
  14516. (list f)))
  14517. files)))
  14518. (when org-agenda-skip-unavailable-files
  14519. (setq files (delq nil
  14520. (mapcar (function
  14521. (lambda (file)
  14522. (and (file-readable-p file) file)))
  14523. files))))
  14524. (when (or (eq archives t)
  14525. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14526. (setq files (org-add-archive-files files)))
  14527. files))
  14528. (defun org-agenda-file-p (&optional file)
  14529. "Return non-nil, if FILE is an agenda file.
  14530. If FILE is omitted, use the file associated with the current
  14531. buffer."
  14532. (member (or file (buffer-file-name))
  14533. (org-agenda-files t)))
  14534. (defun org-edit-agenda-file-list ()
  14535. "Edit the list of agenda files.
  14536. Depending on setup, this either uses customize to edit the variable
  14537. `org-agenda-files', or it visits the file that is holding the list. In the
  14538. latter case, the buffer is set up in a way that saving it automatically kills
  14539. the buffer and restores the previous window configuration."
  14540. (interactive)
  14541. (if (stringp org-agenda-files)
  14542. (let ((cw (current-window-configuration)))
  14543. (find-file org-agenda-files)
  14544. (org-set-local 'org-window-configuration cw)
  14545. (org-add-hook 'after-save-hook
  14546. (lambda ()
  14547. (set-window-configuration
  14548. (prog1 org-window-configuration
  14549. (kill-buffer (current-buffer))))
  14550. (org-install-agenda-files-menu)
  14551. (message "New agenda file list installed"))
  14552. nil 'local)
  14553. (message "%s" (substitute-command-keys
  14554. "Edit list and finish with \\[save-buffer]")))
  14555. (customize-variable 'org-agenda-files)))
  14556. (defun org-store-new-agenda-file-list (list)
  14557. "Set new value for the agenda file list and save it correctly."
  14558. (if (stringp org-agenda-files)
  14559. (let ((fe (org-read-agenda-file-list t)) b u)
  14560. (while (setq b (find-buffer-visiting org-agenda-files))
  14561. (kill-buffer b))
  14562. (with-temp-file org-agenda-files
  14563. (insert
  14564. (mapconcat
  14565. (lambda (f) ;; Keep un-expanded entries.
  14566. (if (setq u (assoc f fe))
  14567. (cdr u)
  14568. f))
  14569. list "\n")
  14570. "\n")))
  14571. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14572. (org-insert-mode-line-in-empty-file nil))
  14573. (setq org-agenda-files list)
  14574. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14575. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14576. "Read the list of agenda files from a file.
  14577. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14578. filenames, used by `org-store-new-agenda-file-list' to write back
  14579. un-expanded file names."
  14580. (when (file-directory-p org-agenda-files)
  14581. (error "`org-agenda-files' cannot be a single directory"))
  14582. (when (stringp org-agenda-files)
  14583. (with-temp-buffer
  14584. (insert-file-contents org-agenda-files)
  14585. (mapcar
  14586. (lambda (f)
  14587. (let ((e (expand-file-name (substitute-in-file-name f)
  14588. org-directory)))
  14589. (if pair-with-expansion
  14590. (cons e f)
  14591. e)))
  14592. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14593. ;;;###autoload
  14594. (defun org-cycle-agenda-files ()
  14595. "Cycle through the files in `org-agenda-files'.
  14596. If the current buffer visits an agenda file, find the next one in the list.
  14597. If the current buffer does not, find the first agenda file."
  14598. (interactive)
  14599. (let* ((fs (org-agenda-files t))
  14600. (files (append fs (list (car fs))))
  14601. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14602. file)
  14603. (unless files (error "No agenda files"))
  14604. (catch 'exit
  14605. (while (setq file (pop files))
  14606. (if (equal (file-truename file) tcf)
  14607. (when (car files)
  14608. (find-file (car files))
  14609. (throw 'exit t))))
  14610. (find-file (car fs)))
  14611. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  14612. (defun org-agenda-file-to-front (&optional to-end)
  14613. "Move/add the current file to the top of the agenda file list.
  14614. If the file is not present in the list, it is added to the front. If it is
  14615. present, it is moved there. With optional argument TO-END, add/move to the
  14616. end of the list."
  14617. (interactive "P")
  14618. (let ((org-agenda-skip-unavailable-files nil)
  14619. (file-alist (mapcar (lambda (x)
  14620. (cons (file-truename x) x))
  14621. (org-agenda-files t)))
  14622. (ctf (file-truename buffer-file-name))
  14623. x had)
  14624. (setq x (assoc ctf file-alist) had x)
  14625. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14626. (if to-end
  14627. (setq file-alist (append (delq x file-alist) (list x)))
  14628. (setq file-alist (cons x (delq x file-alist))))
  14629. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14630. (org-install-agenda-files-menu)
  14631. (message "File %s to %s of agenda file list"
  14632. (if had "moved" "added") (if to-end "end" "front"))))
  14633. (defun org-remove-file (&optional file)
  14634. "Remove current file from the list of files in variable `org-agenda-files'.
  14635. These are the files which are being checked for agenda entries.
  14636. Optional argument FILE means use this file instead of the current."
  14637. (interactive)
  14638. (let* ((org-agenda-skip-unavailable-files nil)
  14639. (file (or file buffer-file-name))
  14640. (true-file (file-truename file))
  14641. (afile (abbreviate-file-name file))
  14642. (files (delq nil (mapcar
  14643. (lambda (x)
  14644. (if (equal true-file
  14645. (file-truename x))
  14646. nil x))
  14647. (org-agenda-files t)))))
  14648. (if (not (= (length files) (length (org-agenda-files t))))
  14649. (progn
  14650. (org-store-new-agenda-file-list files)
  14651. (org-install-agenda-files-menu)
  14652. (message "Removed file: %s" afile))
  14653. (message "File was not in list: %s (not removed)" afile))))
  14654. (defun org-file-menu-entry (file)
  14655. (vector file (list 'find-file file) t))
  14656. (defun org-check-agenda-file (file)
  14657. "Make sure FILE exists. If not, ask user what to do."
  14658. (when (not (file-exists-p file))
  14659. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14660. (abbreviate-file-name file))
  14661. (let ((r (downcase (read-char-exclusive))))
  14662. (cond
  14663. ((equal r ?r)
  14664. (org-remove-file file)
  14665. (throw 'nextfile t))
  14666. (t (error "Abort"))))))
  14667. (defun org-get-agenda-file-buffer (file)
  14668. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14669. it to the list of buffers which might be released later."
  14670. (let ((buf (org-find-base-buffer-visiting file)))
  14671. (if buf
  14672. buf ; just return it
  14673. ;; Make a new buffer and remember it
  14674. (setq buf (find-file-noselect file))
  14675. (if buf (push buf org-agenda-new-buffers))
  14676. buf)))
  14677. (defun org-release-buffers (blist)
  14678. "Release all buffers in list, asking the user for confirmation when needed.
  14679. When a buffer is unmodified, it is just killed. When modified, it is saved
  14680. \(if the user agrees) and then killed."
  14681. (let (buf file)
  14682. (while (setq buf (pop blist))
  14683. (setq file (buffer-file-name buf))
  14684. (when (and (buffer-modified-p buf)
  14685. file
  14686. (y-or-n-p (format "Save file %s? " file)))
  14687. (with-current-buffer buf (save-buffer)))
  14688. (kill-buffer buf))))
  14689. (defun org-prepare-agenda-buffers (files)
  14690. "Create buffers for all agenda files, protect archived trees and comments."
  14691. (interactive)
  14692. (let ((pa '(:org-archived t))
  14693. (pc '(:org-comment t))
  14694. (pall '(:org-archived t :org-comment t))
  14695. (inhibit-read-only t)
  14696. (rea (concat ":" org-archive-tag ":"))
  14697. bmp file re)
  14698. (save-excursion
  14699. (save-restriction
  14700. (while (setq file (pop files))
  14701. (catch 'nextfile
  14702. (if (bufferp file)
  14703. (set-buffer file)
  14704. (org-check-agenda-file file)
  14705. (set-buffer (org-get-agenda-file-buffer file)))
  14706. (widen)
  14707. (setq bmp (buffer-modified-p))
  14708. (org-refresh-category-properties)
  14709. (setq org-todo-keywords-for-agenda
  14710. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14711. (setq org-done-keywords-for-agenda
  14712. (append org-done-keywords-for-agenda org-done-keywords))
  14713. (setq org-todo-keyword-alist-for-agenda
  14714. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  14715. (setq org-drawers-for-agenda
  14716. (append org-drawers-for-agenda org-drawers))
  14717. (setq org-tag-alist-for-agenda
  14718. (append org-tag-alist-for-agenda org-tag-alist))
  14719. (save-excursion
  14720. (remove-text-properties (point-min) (point-max) pall)
  14721. (when org-agenda-skip-archived-trees
  14722. (goto-char (point-min))
  14723. (while (re-search-forward rea nil t)
  14724. (if (org-on-heading-p t)
  14725. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14726. (goto-char (point-min))
  14727. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  14728. (while (re-search-forward re nil t)
  14729. (add-text-properties
  14730. (match-beginning 0) (org-end-of-subtree t) pc)))
  14731. (set-buffer-modified-p bmp)))))
  14732. (setq org-todo-keywords-for-agenda
  14733. (org-uniquify org-todo-keywords-for-agenda))
  14734. (setq org-todo-keyword-alist-for-agenda
  14735. (org-uniquify org-todo-keyword-alist-for-agenda)
  14736. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  14737. ;;;; Embedded LaTeX
  14738. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14739. "Keymap for the minor `org-cdlatex-mode'.")
  14740. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14741. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14742. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14743. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14744. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14745. (defvar org-cdlatex-texmathp-advice-is-done nil
  14746. "Flag remembering if we have applied the advice to texmathp already.")
  14747. (define-minor-mode org-cdlatex-mode
  14748. "Toggle the minor `org-cdlatex-mode'.
  14749. This mode supports entering LaTeX environment and math in LaTeX fragments
  14750. in Org-mode.
  14751. \\{org-cdlatex-mode-map}"
  14752. nil " OCDL" nil
  14753. (when org-cdlatex-mode (require 'cdlatex))
  14754. (unless org-cdlatex-texmathp-advice-is-done
  14755. (setq org-cdlatex-texmathp-advice-is-done t)
  14756. (defadvice texmathp (around org-math-always-on activate)
  14757. "Always return t in org-mode buffers.
  14758. This is because we want to insert math symbols without dollars even outside
  14759. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14760. an embedded LaTeX fragment, let texmathp do its job.
  14761. \\[org-cdlatex-mode-map]"
  14762. (interactive)
  14763. (let (p)
  14764. (cond
  14765. ((not (org-mode-p)) ad-do-it)
  14766. ((eq this-command 'cdlatex-math-symbol)
  14767. (setq ad-return-value t
  14768. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14769. (t
  14770. (let ((p (org-inside-LaTeX-fragment-p)))
  14771. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14772. (setq ad-return-value t
  14773. texmathp-why '("Org-mode embedded math" . 0))
  14774. (if p ad-do-it)))))))))
  14775. (defun turn-on-org-cdlatex ()
  14776. "Unconditionally turn on `org-cdlatex-mode'."
  14777. (org-cdlatex-mode 1))
  14778. (defun org-inside-LaTeX-fragment-p ()
  14779. "Test if point is inside a LaTeX fragment.
  14780. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14781. sequence appearing also before point.
  14782. Even though the matchers for math are configurable, this function assumes
  14783. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14784. delimiters are skipped when they have been removed by customization.
  14785. The return value is nil, or a cons cell with the delimiter and the
  14786. position of this delimiter.
  14787. This function does a reasonably good job, but can locally be fooled by
  14788. for example currency specifications. For example it will assume being in
  14789. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14790. fragments that are properly closed, but during editing, we have to live
  14791. with the uncertainty caused by missing closing delimiters. This function
  14792. looks only before point, not after."
  14793. (catch 'exit
  14794. (let ((pos (point))
  14795. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14796. (lim (progn
  14797. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14798. (point)))
  14799. dd-on str (start 0) m re)
  14800. (goto-char pos)
  14801. (when dodollar
  14802. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14803. re (nth 1 (assoc "$" org-latex-regexps)))
  14804. (while (string-match re str start)
  14805. (cond
  14806. ((= (match-end 0) (length str))
  14807. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14808. ((= (match-end 0) (- (length str) 5))
  14809. (throw 'exit nil))
  14810. (t (setq start (match-end 0))))))
  14811. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14812. (goto-char pos)
  14813. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14814. (and (match-beginning 2) (throw 'exit nil))
  14815. ;; count $$
  14816. (while (re-search-backward "\\$\\$" lim t)
  14817. (setq dd-on (not dd-on)))
  14818. (goto-char pos)
  14819. (if dd-on (cons "$$" m))))))
  14820. (defun org-inside-latex-macro-p ()
  14821. "Is point inside a LaTeX macro or its arguments?"
  14822. (save-match-data
  14823. (org-in-regexp
  14824. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14825. (defun org-try-cdlatex-tab ()
  14826. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14827. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14828. - inside a LaTeX fragment, or
  14829. - after the first word in a line, where an abbreviation expansion could
  14830. insert a LaTeX environment."
  14831. (when org-cdlatex-mode
  14832. (cond
  14833. ((save-excursion
  14834. (skip-chars-backward "a-zA-Z0-9*")
  14835. (skip-chars-backward " \t")
  14836. (bolp))
  14837. (cdlatex-tab) t)
  14838. ((org-inside-LaTeX-fragment-p)
  14839. (cdlatex-tab) t)
  14840. (t nil))))
  14841. (defun org-cdlatex-underscore-caret (&optional arg)
  14842. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14843. Revert to the normal definition outside of these fragments."
  14844. (interactive "P")
  14845. (if (org-inside-LaTeX-fragment-p)
  14846. (call-interactively 'cdlatex-sub-superscript)
  14847. (let (org-cdlatex-mode)
  14848. (call-interactively (key-binding (vector last-input-event))))))
  14849. (defun org-cdlatex-math-modify (&optional arg)
  14850. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14851. Revert to the normal definition outside of these fragments."
  14852. (interactive "P")
  14853. (if (org-inside-LaTeX-fragment-p)
  14854. (call-interactively 'cdlatex-math-modify)
  14855. (let (org-cdlatex-mode)
  14856. (call-interactively (key-binding (vector last-input-event))))))
  14857. (defvar org-latex-fragment-image-overlays nil
  14858. "List of overlays carrying the images of latex fragments.")
  14859. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14860. (defun org-remove-latex-fragment-image-overlays ()
  14861. "Remove all overlays with LaTeX fragment images in current buffer."
  14862. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14863. (setq org-latex-fragment-image-overlays nil))
  14864. (defun org-preview-latex-fragment (&optional subtree)
  14865. "Preview the LaTeX fragment at point, or all locally or globally.
  14866. If the cursor is in a LaTeX fragment, create the image and overlay
  14867. it over the source code. If there is no fragment at point, display
  14868. all fragments in the current text, from one headline to the next. With
  14869. prefix SUBTREE, display all fragments in the current subtree. With a
  14870. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  14871. the cursor is before the first headline,
  14872. display all fragments in the buffer.
  14873. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14874. (interactive "P")
  14875. (org-remove-latex-fragment-image-overlays)
  14876. (save-excursion
  14877. (save-restriction
  14878. (let (beg end at msg)
  14879. (cond
  14880. ((or (equal subtree '(16))
  14881. (not (save-excursion
  14882. (re-search-backward (concat "^" outline-regexp) nil t))))
  14883. (setq beg (point-min) end (point-max)
  14884. msg "Creating images for buffer...%s"))
  14885. ((equal subtree '(4))
  14886. (org-back-to-heading)
  14887. (setq beg (point) end (org-end-of-subtree t)
  14888. msg "Creating images for subtree...%s"))
  14889. (t
  14890. (if (setq at (org-inside-LaTeX-fragment-p))
  14891. (goto-char (max (point-min) (- (cdr at) 2)))
  14892. (org-back-to-heading))
  14893. (setq beg (point) end (progn (outline-next-heading) (point))
  14894. msg (if at "Creating image...%s"
  14895. "Creating images for entry...%s"))))
  14896. (message msg "")
  14897. (narrow-to-region beg end)
  14898. (goto-char beg)
  14899. (org-format-latex
  14900. (concat "ltxpng/" (file-name-sans-extension
  14901. (file-name-nondirectory
  14902. buffer-file-name)))
  14903. default-directory 'overlays msg at 'forbuffer 'dvipng)
  14904. (message msg "done. Use `C-c C-c' to remove images.")))))
  14905. (defvar org-latex-regexps
  14906. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14907. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14908. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14909. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14910. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14911. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14912. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14913. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14914. "Regular expressions for matching embedded LaTeX.")
  14915. (defvar org-export-have-math nil) ;; dynamic scoping
  14916. (defun org-format-latex (prefix &optional dir overlays msg at
  14917. forbuffer processing-type)
  14918. "Replace LaTeX fragments with links to an image, and produce images.
  14919. Some of the options can be changed using the variable
  14920. `org-format-latex-options'."
  14921. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14922. (let* ((prefixnodir (file-name-nondirectory prefix))
  14923. (absprefix (expand-file-name prefix dir))
  14924. (todir (file-name-directory absprefix))
  14925. (opt org-format-latex-options)
  14926. (matchers (plist-get opt :matchers))
  14927. (re-list org-latex-regexps)
  14928. (org-format-latex-header-extra
  14929. (plist-get (org-infile-export-plist) :latex-header-extra))
  14930. (cnt 0) txt hash link beg end re e checkdir
  14931. executables-checked string
  14932. m n block linkfile movefile ov)
  14933. ;; Check the different regular expressions
  14934. (while (setq e (pop re-list))
  14935. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14936. block (if (nth 3 e) "\n\n" ""))
  14937. (when (member m matchers)
  14938. (goto-char (point-min))
  14939. (while (re-search-forward re nil t)
  14940. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14941. (not (get-text-property (match-beginning n)
  14942. 'org-protected))
  14943. (or (not overlays)
  14944. (not (eq (get-char-property (match-beginning n)
  14945. 'org-overlay-type)
  14946. 'org-latex-overlay))))
  14947. (setq org-export-have-math t)
  14948. (cond
  14949. ((eq processing-type 'verbatim)
  14950. ;; Leave the text verbatim, just protect it
  14951. (add-text-properties (match-beginning n) (match-end n)
  14952. '(org-protected t)))
  14953. ((eq processing-type 'mathjax)
  14954. ;; Prepare for MathJax processing
  14955. (setq string (match-string n))
  14956. (if (member m '("$" "$1"))
  14957. (save-excursion
  14958. (delete-region (match-beginning n) (match-end n))
  14959. (goto-char (match-beginning n))
  14960. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  14961. "\\)")
  14962. '(org-protected t))))
  14963. (add-text-properties (match-beginning n) (match-end n)
  14964. '(org-protected t))))
  14965. ((or (eq processing-type 'dvipng) t)
  14966. ;; Process to an image
  14967. (setq txt (match-string n)
  14968. beg (match-beginning n) end (match-end n)
  14969. cnt (1+ cnt))
  14970. (let (print-length print-level) ; make sure full list is printed
  14971. (setq hash (sha1 (prin1-to-string
  14972. (list org-format-latex-header
  14973. org-format-latex-header-extra
  14974. org-export-latex-default-packages-alist
  14975. org-export-latex-packages-alist
  14976. org-format-latex-options
  14977. forbuffer txt)))
  14978. linkfile (format "%s_%s.png" prefix hash)
  14979. movefile (format "%s_%s.png" absprefix hash)))
  14980. (setq link (concat block "[[file:" linkfile "]]" block))
  14981. (if msg (message msg cnt))
  14982. (goto-char beg)
  14983. (unless checkdir ; make sure the directory exists
  14984. (setq checkdir t)
  14985. (or (file-directory-p todir) (make-directory todir t)))
  14986. (unless executables-checked
  14987. (org-check-external-command
  14988. "latex" "needed to convert LaTeX fragments to images")
  14989. (org-check-external-command
  14990. "dvipng" "needed to convert LaTeX fragments to images")
  14991. (setq executables-checked t))
  14992. (unless (file-exists-p movefile)
  14993. (org-create-formula-image
  14994. txt movefile opt forbuffer))
  14995. (if overlays
  14996. (progn
  14997. (mapc (lambda (o)
  14998. (if (eq (overlay-get o 'org-overlay-type)
  14999. 'org-latex-overlay)
  15000. (delete-overlay o)))
  15001. (overlays-in beg end))
  15002. (setq ov (make-overlay beg end))
  15003. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15004. (if (featurep 'xemacs)
  15005. (progn
  15006. (overlay-put ov 'invisible t)
  15007. (overlay-put
  15008. ov 'end-glyph
  15009. (make-glyph (vector 'png :file movefile))))
  15010. (overlay-put
  15011. ov 'display
  15012. (list 'image :type 'png :file movefile :ascent 'center)))
  15013. (push ov org-latex-fragment-image-overlays)
  15014. (goto-char end))
  15015. (delete-region beg end)
  15016. (insert (org-add-props link
  15017. (list 'org-latex-src
  15018. (replace-regexp-in-string
  15019. "\"" "" txt)))))))))))))
  15020. ;; This function borrows from Ganesh Swami's latex2png.el
  15021. (defun org-create-formula-image (string tofile options buffer)
  15022. "This calls dvipng."
  15023. (require 'org-latex)
  15024. (let* ((tmpdir (if (featurep 'xemacs)
  15025. (temp-directory)
  15026. temporary-file-directory))
  15027. (texfilebase (make-temp-name
  15028. (expand-file-name "orgtex" tmpdir)))
  15029. (texfile (concat texfilebase ".tex"))
  15030. (dvifile (concat texfilebase ".dvi"))
  15031. (pngfile (concat texfilebase ".png"))
  15032. (fnh (if (featurep 'xemacs)
  15033. (font-height (get-face-font 'default))
  15034. (face-attribute 'default :height nil)))
  15035. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15036. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15037. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15038. "Black"))
  15039. (bg (or (plist-get options (if buffer :background :html-background))
  15040. "Transparent")))
  15041. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15042. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15043. (with-temp-file texfile
  15044. (insert (org-splice-latex-header
  15045. org-format-latex-header
  15046. org-export-latex-default-packages-alist
  15047. org-export-latex-packages-alist t
  15048. org-format-latex-header-extra))
  15049. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15050. (require 'org-latex)
  15051. (org-export-latex-fix-inputenc))
  15052. (let ((dir default-directory))
  15053. (condition-case nil
  15054. (progn
  15055. (cd tmpdir)
  15056. (call-process "latex" nil nil nil texfile))
  15057. (error nil))
  15058. (cd dir))
  15059. (if (not (file-exists-p dvifile))
  15060. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15061. (condition-case nil
  15062. (call-process "dvipng" nil nil nil
  15063. "-fg" fg "-bg" bg
  15064. "-D" dpi
  15065. ;;"-x" scale "-y" scale
  15066. "-T" "tight"
  15067. "-o" pngfile
  15068. dvifile)
  15069. (error nil))
  15070. (if (not (file-exists-p pngfile))
  15071. (if org-format-latex-signal-error
  15072. (error "Failed to create png file from %s" texfile)
  15073. (message "Failed to create png file from %s" texfile)
  15074. nil)
  15075. ;; Use the requested file name and clean up
  15076. (copy-file pngfile tofile 'replace)
  15077. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  15078. (delete-file (concat texfilebase e)))
  15079. pngfile))))
  15080. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  15081. "Fill a LaTeX header template TPL.
  15082. In the template, the following place holders will be recognized:
  15083. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  15084. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  15085. [PACKAGES] \\usepackage statements for PKG
  15086. [NO-PACKAGES] do not include PKG
  15087. [EXTRA] the string EXTRA
  15088. [NO-EXTRA] do not include EXTRA
  15089. For backward compatibility, if both the positive and the negative place
  15090. holder is missing, the positive one (without the \"NO-\") will be
  15091. assumed to be present at the end of the template.
  15092. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15093. EXTRA is a string.
  15094. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15095. (let (rpl (end ""))
  15096. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15097. (setq rpl (if (or (match-end 1) (not def-pkg))
  15098. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15099. tpl (replace-match rpl t t tpl))
  15100. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15101. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15102. (setq rpl (if (or (match-end 1) (not pkg))
  15103. "" (org-latex-packages-to-string pkg snippets-p t))
  15104. tpl (replace-match rpl t t tpl))
  15105. (if pkg (setq end
  15106. (concat end "\n"
  15107. (org-latex-packages-to-string pkg snippets-p)))))
  15108. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15109. (setq rpl (if (or (match-end 1) (not extra))
  15110. "" (concat extra "\n"))
  15111. tpl (replace-match rpl t t tpl))
  15112. (if (and extra (string-match "\\S-" extra))
  15113. (setq end (concat end "\n" extra))))
  15114. (if (string-match "\\S-" end)
  15115. (concat tpl "\n" end)
  15116. tpl)))
  15117. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15118. "Turn an alist of packages into a string with the \\usepackage macros."
  15119. (setq pkg (mapconcat (lambda(p)
  15120. (cond
  15121. ((stringp p) p)
  15122. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15123. (format "%% Package %s omitted" (cadr p)))
  15124. ((equal "" (car p))
  15125. (format "\\usepackage{%s}" (cadr p)))
  15126. (t
  15127. (format "\\usepackage[%s]{%s}"
  15128. (car p) (cadr p)))))
  15129. pkg
  15130. "\n"))
  15131. (if newline (concat pkg "\n") pkg))
  15132. (defun org-dvipng-color (attr)
  15133. "Return an rgb color specification for dvipng."
  15134. (apply 'format "rgb %s %s %s"
  15135. (mapcar 'org-normalize-color
  15136. (color-values (face-attribute 'default attr nil)))))
  15137. (defun org-normalize-color (value)
  15138. "Return string to be used as color value for an RGB component."
  15139. (format "%g" (/ value 65535.0)))
  15140. ;; Image display
  15141. (defvar org-inline-image-overlays nil)
  15142. (make-variable-buffer-local 'org-inline-image-overlays)
  15143. (defun org-toggle-inline-images (&optional include-linked)
  15144. "Toggle the display of inline images.
  15145. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15146. (interactive "P")
  15147. (if org-inline-image-overlays
  15148. (progn
  15149. (org-remove-inline-images)
  15150. (message "Inline image display turned off"))
  15151. (org-display-inline-images include-linked)
  15152. (if org-inline-image-overlays
  15153. (message "%d images displayed inline"
  15154. (length org-inline-image-overlays))
  15155. (message "No images to display inline"))))
  15156. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15157. "Display inline images.
  15158. Normally only links without a description part are inlined, because this
  15159. is how it will work for export. When INCLUDE-LINKED is set, also links
  15160. with a description part will be inlined. This can be nice for a quick
  15161. look at those images, but it does not reflect what exported files will look
  15162. like.
  15163. When REFRESH is set, refresh existing images between BEG and END.
  15164. This will create new image displays only if necessary.
  15165. BEG and END default to the buffer boundaries."
  15166. (interactive "P")
  15167. (unless refresh
  15168. (org-remove-inline-images)
  15169. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15170. (save-excursion
  15171. (save-restriction
  15172. (widen)
  15173. (setq beg (or beg (point-min)) end (or end (point-max)))
  15174. (goto-char (point-min))
  15175. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15176. (substring (org-image-file-name-regexp) 0 -2)
  15177. "\\)\\]" (if include-linked "" "\\]")))
  15178. old file ov img)
  15179. (while (re-search-forward re end t)
  15180. (setq old (get-char-property-and-overlay (match-beginning 1)
  15181. 'org-image-overlay))
  15182. (setq file (expand-file-name
  15183. (concat (or (match-string 3) "") (match-string 4))))
  15184. (when (file-exists-p file)
  15185. (if (and (car-safe old) refresh)
  15186. (image-refresh (overlay-get (cdr old) 'display))
  15187. (setq img (save-match-data (create-image file)))
  15188. (when img
  15189. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15190. (overlay-put ov 'display img)
  15191. (overlay-put ov 'face 'default)
  15192. (overlay-put ov 'org-image-overlay t)
  15193. (overlay-put ov 'modification-hooks
  15194. (list 'org-display-inline-modification-hook))
  15195. (push ov org-inline-image-overlays)))))))))
  15196. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15197. "Remove inline-display overlay if a corresponding region is modified."
  15198. (let ((inhibit-modification-hooks t))
  15199. (when (and ov after)
  15200. (delete ov org-inline-image-overlays)
  15201. (delete-overlay ov))))
  15202. (defun org-remove-inline-images ()
  15203. "Remove inline display of images."
  15204. (interactive)
  15205. (mapc 'delete-overlay org-inline-image-overlays)
  15206. (setq org-inline-image-overlays nil))
  15207. ;;;; Key bindings
  15208. ;; Make `C-c C-x' a prefix key
  15209. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15210. ;; TAB key with modifiers
  15211. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15212. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15213. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15214. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15215. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15216. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15217. ;; The following line is necessary under Suse GNU/Linux
  15218. (unless (featurep 'xemacs)
  15219. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15220. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15221. (define-key org-mode-map [backtab] 'org-shifttab)
  15222. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15223. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15224. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15225. ;; Cursor keys with modifiers
  15226. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15227. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15228. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15229. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15230. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15231. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15232. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15233. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15234. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15235. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15236. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15237. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15238. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15239. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15240. ;; Babel keys
  15241. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15242. (mapc (lambda (pair)
  15243. (define-key org-babel-map (car pair) (cdr pair)))
  15244. org-babel-key-bindings)
  15245. ;;; Extra keys for tty access.
  15246. ;; We only set them when really needed because otherwise the
  15247. ;; menus don't show the simple keys
  15248. (when (or org-use-extra-keys
  15249. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15250. (not window-system))
  15251. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15252. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15253. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15254. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15255. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15256. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15257. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15258. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15259. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15260. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15261. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15262. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15263. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15264. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15265. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15266. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15267. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15268. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15269. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15270. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15271. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15272. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15273. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15274. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15275. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15276. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15277. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15278. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15279. ;; All the other keys
  15280. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15281. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15282. (if (boundp 'narrow-map)
  15283. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15284. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15285. (if (boundp 'narrow-map)
  15286. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15287. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15288. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15289. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15290. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15291. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15292. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15293. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15294. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15295. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15296. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15297. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15298. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15299. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15300. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15301. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15302. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15303. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15304. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15305. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15306. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15307. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15308. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15309. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15310. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15311. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15312. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15313. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15314. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15315. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15316. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15317. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15318. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15319. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15320. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15321. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15322. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15323. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15324. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15325. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15326. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15327. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15328. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15329. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15330. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15331. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15332. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15333. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15334. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15335. (org-defkey org-mode-map "\C-m" 'org-return)
  15336. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15337. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15338. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15339. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15340. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15341. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15342. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15343. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15344. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15345. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15346. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15347. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15348. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15349. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15350. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15351. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15352. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15353. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15354. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15355. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  15356. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15357. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15358. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15359. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15360. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15361. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15362. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15363. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15364. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15365. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15366. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15367. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15368. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15369. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15370. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15371. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15372. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15373. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15374. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15375. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15376. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15377. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15378. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15379. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15380. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15381. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15382. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15383. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  15384. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15385. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15386. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15387. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15388. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15389. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15390. (when (featurep 'xemacs)
  15391. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15392. (defconst org-speed-commands-default
  15393. '(
  15394. ("Outline Navigation")
  15395. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15396. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15397. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15398. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15399. ("u" . (org-speed-move-safe 'outline-up-heading))
  15400. ("j" . org-goto)
  15401. ("g" . (org-refile t))
  15402. ("Outline Visibility")
  15403. ("c" . org-cycle)
  15404. ("C" . org-shifttab)
  15405. (" " . org-display-outline-path)
  15406. ("Outline Structure Editing")
  15407. ("U" . org-shiftmetaup)
  15408. ("D" . org-shiftmetadown)
  15409. ("r" . org-metaright)
  15410. ("l" . org-metaleft)
  15411. ("R" . org-shiftmetaright)
  15412. ("L" . org-shiftmetaleft)
  15413. ("i" . (progn (forward-char 1) (call-interactively
  15414. 'org-insert-heading-respect-content)))
  15415. ("^" . org-sort)
  15416. ("w" . org-refile)
  15417. ("a" . org-archive-subtree-default-with-confirmation)
  15418. ("." . org-mark-subtree)
  15419. ("Clock Commands")
  15420. ("I" . org-clock-in)
  15421. ("O" . org-clock-out)
  15422. ("Meta Data Editing")
  15423. ("t" . org-todo)
  15424. ("0" . (org-priority ?\ ))
  15425. ("1" . (org-priority ?A))
  15426. ("2" . (org-priority ?B))
  15427. ("3" . (org-priority ?C))
  15428. (";" . org-set-tags-command)
  15429. ("e" . org-set-effort)
  15430. ("Agenda Views etc")
  15431. ("v" . org-agenda)
  15432. ("/" . org-sparse-tree)
  15433. ("Misc")
  15434. ("o" . org-open-at-point)
  15435. ("?" . org-speed-command-help)
  15436. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15437. (">" . (org-agenda-remove-restriction-lock))
  15438. )
  15439. "The default speed commands.")
  15440. (defun org-print-speed-command (e)
  15441. (if (> (length (car e)) 1)
  15442. (progn
  15443. (princ "\n")
  15444. (princ (car e))
  15445. (princ "\n")
  15446. (princ (make-string (length (car e)) ?-))
  15447. (princ "\n"))
  15448. (princ (car e))
  15449. (princ " ")
  15450. (if (symbolp (cdr e))
  15451. (princ (symbol-name (cdr e)))
  15452. (prin1 (cdr e)))
  15453. (princ "\n")))
  15454. (defun org-speed-command-help ()
  15455. "Show the available speed commands."
  15456. (interactive)
  15457. (if (not org-use-speed-commands)
  15458. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15459. (with-output-to-temp-buffer "*Help*"
  15460. (princ "User-defined Speed commands\n===========================\n")
  15461. (mapc 'org-print-speed-command org-speed-commands-user)
  15462. (princ "\n")
  15463. (princ "Built-in Speed commands\n=======================\n")
  15464. (mapc 'org-print-speed-command org-speed-commands-default))
  15465. (with-current-buffer "*Help*"
  15466. (setq truncate-lines t))))
  15467. (defun org-speed-move-safe (cmd)
  15468. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15469. If not, return to the original position and throw an error."
  15470. (interactive)
  15471. (let ((pos (point)))
  15472. (call-interactively cmd)
  15473. (unless (and (bolp) (org-on-heading-p))
  15474. (goto-char pos)
  15475. (error "Boundary reached while executing %s" cmd))))
  15476. (defvar org-self-insert-command-undo-counter 0)
  15477. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15478. (defvar org-speed-command nil)
  15479. (defun org-speed-command-default-hook (keys)
  15480. "Hook for activating single-letter speed commands.
  15481. `org-speed-commands-default' specifies a minimal command set. Use
  15482. `org-speed-commands-user' for further customization."
  15483. (when (or (and (bolp) (looking-at outline-regexp))
  15484. (and (functionp org-use-speed-commands)
  15485. (funcall org-use-speed-commands)))
  15486. (cdr (assoc keys (append org-speed-commands-user
  15487. org-speed-commands-default)))))
  15488. (defun org-babel-speed-command-hook (keys)
  15489. "Hook for activating single-letter code block commands."
  15490. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  15491. (cdr (assoc keys org-babel-key-bindings))))
  15492. (defcustom org-speed-command-hook
  15493. '(org-speed-command-default-hook org-babel-speed-command-hook)
  15494. "Hook for activating speed commands at strategic locations.
  15495. Hook functions are called in sequence until a valid handler is
  15496. found.
  15497. Each hook takes a single argument, a user-pressed command key
  15498. which is also a `self-insert-command' from the global map.
  15499. Within the hook, examine the cursor position and the command key
  15500. and return nil or a valid handler as appropriate. Handler could
  15501. be one of an interactive command, a function, or a form.
  15502. Set `org-use-speed-commands' to non-nil value to enable this
  15503. hook. The default setting is `org-speed-command-default-hook'."
  15504. :group 'org-structure
  15505. :type 'hook)
  15506. (defun org-self-insert-command (N)
  15507. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15508. If the cursor is in a table looking at whitespace, the whitespace is
  15509. overwritten, and the table is not marked as requiring realignment."
  15510. (interactive "p")
  15511. (cond
  15512. ((and org-use-speed-commands
  15513. (setq org-speed-command
  15514. (run-hook-with-args-until-success
  15515. 'org-speed-command-hook (this-command-keys))))
  15516. (cond
  15517. ((commandp org-speed-command)
  15518. (setq this-command org-speed-command)
  15519. (call-interactively org-speed-command))
  15520. ((functionp org-speed-command)
  15521. (funcall org-speed-command))
  15522. ((and org-speed-command (listp org-speed-command))
  15523. (eval org-speed-command))
  15524. (t (let (org-use-speed-commands)
  15525. (call-interactively 'org-self-insert-command)))))
  15526. ((and
  15527. (org-table-p)
  15528. (progn
  15529. ;; check if we blank the field, and if that triggers align
  15530. (and (featurep 'org-table) org-table-auto-blank-field
  15531. (member last-command
  15532. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  15533. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15534. ;; got extra space, this field does not determine column width
  15535. (let (org-table-may-need-update) (org-table-blank-field))
  15536. ;; no extra space, this field may determine column width
  15537. (org-table-blank-field)))
  15538. t)
  15539. (eq N 1)
  15540. (looking-at "[^|\n]* |"))
  15541. (let (org-table-may-need-update)
  15542. (goto-char (1- (match-end 0)))
  15543. (delete-char -1)
  15544. (goto-char (match-beginning 0))
  15545. (self-insert-command N)))
  15546. (t
  15547. (setq org-table-may-need-update t)
  15548. (self-insert-command N)
  15549. (org-fix-tags-on-the-fly)
  15550. (if org-self-insert-cluster-for-undo
  15551. (if (not (eq last-command 'org-self-insert-command))
  15552. (setq org-self-insert-command-undo-counter 1)
  15553. (if (>= org-self-insert-command-undo-counter 20)
  15554. (setq org-self-insert-command-undo-counter 1)
  15555. (and (> org-self-insert-command-undo-counter 0)
  15556. buffer-undo-list (listp buffer-undo-list)
  15557. (not (cadr buffer-undo-list)) ; remove nil entry
  15558. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  15559. (setq org-self-insert-command-undo-counter
  15560. (1+ org-self-insert-command-undo-counter))))))))
  15561. (defun org-fix-tags-on-the-fly ()
  15562. (when (and (equal (char-after (point-at-bol)) ?*)
  15563. (org-on-heading-p))
  15564. (org-align-tags-here org-tags-column)))
  15565. (defun org-delete-backward-char (N)
  15566. "Like `delete-backward-char', insert whitespace at field end in tables.
  15567. When deleting backwards, in tables this function will insert whitespace in
  15568. front of the next \"|\" separator, to keep the table aligned. The table will
  15569. still be marked for re-alignment if the field did fill the entire column,
  15570. because, in this case the deletion might narrow the column."
  15571. (interactive "p")
  15572. (if (and (org-table-p)
  15573. (eq N 1)
  15574. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15575. (looking-at ".*?|"))
  15576. (let ((pos (point))
  15577. (noalign (looking-at "[^|\n\r]* |"))
  15578. (c org-table-may-need-update))
  15579. (backward-delete-char N)
  15580. (if (not overwrite-mode)
  15581. (progn
  15582. (skip-chars-forward "^|")
  15583. (insert " ")
  15584. (goto-char (1- pos))))
  15585. ;; noalign: if there were two spaces at the end, this field
  15586. ;; does not determine the width of the column.
  15587. (if noalign (setq org-table-may-need-update c)))
  15588. (backward-delete-char N)
  15589. (org-fix-tags-on-the-fly)))
  15590. (defun org-delete-char (N)
  15591. "Like `delete-char', but insert whitespace at field end in tables.
  15592. When deleting characters, in tables this function will insert whitespace in
  15593. front of the next \"|\" separator, to keep the table aligned. The table will
  15594. still be marked for re-alignment if the field did fill the entire column,
  15595. because, in this case the deletion might narrow the column."
  15596. (interactive "p")
  15597. (if (and (org-table-p)
  15598. (not (bolp))
  15599. (not (= (char-after) ?|))
  15600. (eq N 1))
  15601. (if (looking-at ".*?|")
  15602. (let ((pos (point))
  15603. (noalign (looking-at "[^|\n\r]* |"))
  15604. (c org-table-may-need-update))
  15605. (replace-match (concat
  15606. (substring (match-string 0) 1 -1)
  15607. " |"))
  15608. (goto-char pos)
  15609. ;; noalign: if there were two spaces at the end, this field
  15610. ;; does not determine the width of the column.
  15611. (if noalign (setq org-table-may-need-update c)))
  15612. (delete-char N))
  15613. (delete-char N)
  15614. (org-fix-tags-on-the-fly)))
  15615. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15616. (put 'org-self-insert-command 'delete-selection t)
  15617. (put 'orgtbl-self-insert-command 'delete-selection t)
  15618. (put 'org-delete-char 'delete-selection 'supersede)
  15619. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15620. (put 'org-yank 'delete-selection 'yank)
  15621. ;; Make `flyspell-mode' delay after some commands
  15622. (put 'org-self-insert-command 'flyspell-delayed t)
  15623. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  15624. (put 'org-delete-char 'flyspell-delayed t)
  15625. (put 'org-delete-backward-char 'flyspell-delayed t)
  15626. ;; Make pabbrev-mode expand after org-mode commands
  15627. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  15628. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  15629. ;; How to do this: Measure non-white length of current string
  15630. ;; If equal to column width, we should realign.
  15631. (defun org-remap (map &rest commands)
  15632. "In MAP, remap the functions given in COMMANDS.
  15633. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15634. (let (new old)
  15635. (while commands
  15636. (setq old (pop commands) new (pop commands))
  15637. (if (fboundp 'command-remapping)
  15638. (org-defkey map (vector 'remap old) new)
  15639. (substitute-key-definition old new map global-map)))))
  15640. (when (eq org-enable-table-editor 'optimized)
  15641. ;; If the user wants maximum table support, we need to hijack
  15642. ;; some standard editing functions
  15643. (org-remap org-mode-map
  15644. 'self-insert-command 'org-self-insert-command
  15645. 'delete-char 'org-delete-char
  15646. 'delete-backward-char 'org-delete-backward-char)
  15647. (org-defkey org-mode-map "|" 'org-force-self-insert))
  15648. (defvar org-ctrl-c-ctrl-c-hook nil
  15649. "Hook for functions attaching themselves to `C-c C-c'.
  15650. This can be used to add additional functionality to the C-c C-c key which
  15651. executes context-dependent commands.
  15652. Each function will be called with no arguments. The function must check
  15653. if the context is appropriate for it to act. If yes, it should do its
  15654. thing and then return a non-nil value. If the context is wrong,
  15655. just do nothing and return nil.")
  15656. (defvar org-tab-first-hook nil
  15657. "Hook for functions to attach themselves to TAB.
  15658. See `org-ctrl-c-ctrl-c-hook' for more information.
  15659. This hook runs as the first action when TAB is pressed, even before
  15660. `org-cycle' messes around with the `outline-regexp' to cater for
  15661. inline tasks and plain list item folding.
  15662. If any function in this hook returns t, any other actions that
  15663. would have been caused by TAB (such as table field motion or visibility
  15664. cycling) will not occur.")
  15665. (defvar org-tab-after-check-for-table-hook nil
  15666. "Hook for functions to attach themselves to TAB.
  15667. See `org-ctrl-c-ctrl-c-hook' for more information.
  15668. This hook runs after it has been established that the cursor is not in a
  15669. table, but before checking if the cursor is in a headline or if global cycling
  15670. should be done.
  15671. If any function in this hook returns t, not other actions like visibility
  15672. cycling will be done.")
  15673. (defvar org-tab-after-check-for-cycling-hook nil
  15674. "Hook for functions to attach themselves to TAB.
  15675. See `org-ctrl-c-ctrl-c-hook' for more information.
  15676. This hook runs after it has been established that not table field motion and
  15677. not visibility should be done because of current context. This is probably
  15678. the place where a package like yasnippets can hook in.")
  15679. (defvar org-tab-before-tab-emulation-hook nil
  15680. "Hook for functions to attach themselves to TAB.
  15681. See `org-ctrl-c-ctrl-c-hook' for more information.
  15682. This hook runs after every other options for TAB have been exhausted, but
  15683. before indentation and \t insertion takes place.")
  15684. (defvar org-metaleft-hook nil
  15685. "Hook for functions attaching themselves to `M-left'.
  15686. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15687. (defvar org-metaright-hook nil
  15688. "Hook for functions attaching themselves to `M-right'.
  15689. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15690. (defvar org-metaup-hook nil
  15691. "Hook for functions attaching themselves to `M-up'.
  15692. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15693. (defvar org-metadown-hook nil
  15694. "Hook for functions attaching themselves to `M-down'.
  15695. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15696. (defvar org-shiftmetaleft-hook nil
  15697. "Hook for functions attaching themselves to `M-S-left'.
  15698. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15699. (defvar org-shiftmetaright-hook nil
  15700. "Hook for functions attaching themselves to `M-S-right'.
  15701. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15702. (defvar org-shiftmetaup-hook nil
  15703. "Hook for functions attaching themselves to `M-S-up'.
  15704. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15705. (defvar org-shiftmetadown-hook nil
  15706. "Hook for functions attaching themselves to `M-S-down'.
  15707. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15708. (defvar org-metareturn-hook nil
  15709. "Hook for functions attaching themselves to `M-RET'.
  15710. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15711. (defvar org-shiftup-hook nil
  15712. "Hook for functions attaching themselves to `S-up'.
  15713. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15714. (defvar org-shiftup-final-hook nil
  15715. "Hook for functions attaching themselves to `S-up'.
  15716. This one runs after all other options except shift-select have been excluded.
  15717. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15718. (defvar org-shiftdown-hook nil
  15719. "Hook for functions attaching themselves to `S-down'.
  15720. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15721. (defvar org-shiftdown-final-hook nil
  15722. "Hook for functions attaching themselves to `S-down'.
  15723. This one runs after all other options except shift-select have been excluded.
  15724. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15725. (defvar org-shiftleft-hook nil
  15726. "Hook for functions attaching themselves to `S-left'.
  15727. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15728. (defvar org-shiftleft-final-hook nil
  15729. "Hook for functions attaching themselves to `S-left'.
  15730. This one runs after all other options except shift-select have been excluded.
  15731. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15732. (defvar org-shiftright-hook nil
  15733. "Hook for functions attaching themselves to `S-right'.
  15734. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15735. (defvar org-shiftright-final-hook nil
  15736. "Hook for functions attaching themselves to `S-right'.
  15737. This one runs after all other options except shift-select have been excluded.
  15738. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15739. (defun org-modifier-cursor-error ()
  15740. "Throw an error, a modified cursor command was applied in wrong context."
  15741. (error "This command is active in special context like tables, headlines or items"))
  15742. (defun org-shiftselect-error ()
  15743. "Throw an error because Shift-Cursor command was applied in wrong context."
  15744. (if (and (boundp 'shift-select-mode) shift-select-mode)
  15745. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  15746. (error "This command works only in special context like headlines or timestamps")))
  15747. (defun org-call-for-shift-select (cmd)
  15748. (let ((this-command-keys-shift-translated t))
  15749. (call-interactively cmd)))
  15750. (defun org-shifttab (&optional arg)
  15751. "Global visibility cycling or move to previous table field.
  15752. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15753. on context.
  15754. See the individual commands for more information."
  15755. (interactive "P")
  15756. (cond
  15757. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15758. ((integerp arg)
  15759. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  15760. (message "Content view to level: %d" arg)
  15761. (org-content (prefix-numeric-value arg2))
  15762. (setq org-cycle-global-status 'overview)))
  15763. (t (call-interactively 'org-global-cycle))))
  15764. (defun org-shiftmetaleft ()
  15765. "Promote subtree or delete table column.
  15766. Calls `org-promote-subtree', `org-outdent-item',
  15767. or `org-table-delete-column', depending on context.
  15768. See the individual commands for more information."
  15769. (interactive)
  15770. (cond
  15771. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  15772. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15773. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15774. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  15775. (t (org-modifier-cursor-error))))
  15776. (defun org-shiftmetaright ()
  15777. "Demote subtree or insert table column.
  15778. Calls `org-demote-subtree', `org-indent-item',
  15779. or `org-table-insert-column', depending on context.
  15780. See the individual commands for more information."
  15781. (interactive)
  15782. (cond
  15783. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  15784. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15785. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15786. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  15787. (t (org-modifier-cursor-error))))
  15788. (defun org-shiftmetaup (&optional arg)
  15789. "Move subtree up or kill table row.
  15790. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15791. `org-move-item-up' depending on context. See the individual commands
  15792. for more information."
  15793. (interactive "P")
  15794. (cond
  15795. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  15796. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15797. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15798. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15799. (t (org-modifier-cursor-error))))
  15800. (defun org-shiftmetadown (&optional arg)
  15801. "Move subtree down or insert table row.
  15802. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15803. `org-move-item-down', depending on context. See the individual
  15804. commands for more information."
  15805. (interactive "P")
  15806. (cond
  15807. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  15808. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15809. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15810. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15811. (t (org-modifier-cursor-error))))
  15812. (defsubst org-hidden-tree-error ()
  15813. (error
  15814. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  15815. (defun org-metaleft (&optional arg)
  15816. "Promote heading or move table column to left.
  15817. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15818. With no specific context, calls the Emacs default `backward-word'.
  15819. See the individual commands for more information."
  15820. (interactive "P")
  15821. (cond
  15822. ((run-hook-with-args-until-success 'org-metaleft-hook))
  15823. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15824. ((org-with-limited-levels
  15825. (or (org-on-heading-p)
  15826. (and (org-region-active-p)
  15827. (save-excursion
  15828. (goto-char (region-beginning))
  15829. (org-on-heading-p)))))
  15830. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15831. (call-interactively 'org-do-promote))
  15832. ;; At an inline task.
  15833. ((org-on-heading-p)
  15834. (call-interactively 'org-inlinetask-promote))
  15835. ((or (org-at-item-p)
  15836. (and (org-region-active-p)
  15837. (save-excursion
  15838. (goto-char (region-beginning))
  15839. (org-at-item-p))))
  15840. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15841. (call-interactively 'org-outdent-item))
  15842. (t (call-interactively 'backward-word))))
  15843. (defun org-metaright (&optional arg)
  15844. "Demote subtree or move table column to right.
  15845. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15846. With no specific context, calls the Emacs default `forward-word'.
  15847. See the individual commands for more information."
  15848. (interactive "P")
  15849. (cond
  15850. ((run-hook-with-args-until-success 'org-metaright-hook))
  15851. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15852. ((org-with-limited-levels
  15853. (or (org-on-heading-p)
  15854. (and (org-region-active-p)
  15855. (save-excursion
  15856. (goto-char (region-beginning))
  15857. (org-on-heading-p)))))
  15858. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15859. (call-interactively 'org-do-demote))
  15860. ;; At an inline task.
  15861. ((org-on-heading-p)
  15862. (call-interactively 'org-inlinetask-demote))
  15863. ((or (org-at-item-p)
  15864. (and (org-region-active-p)
  15865. (save-excursion
  15866. (goto-char (region-beginning))
  15867. (org-at-item-p))))
  15868. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15869. (call-interactively 'org-indent-item))
  15870. (t (call-interactively 'forward-word))))
  15871. (defun org-check-for-hidden (what)
  15872. "Check if there are hidden headlines/items in the current visual line.
  15873. WHAT can be either `headlines' or `items'. If the current line is
  15874. an outline or item heading and it has a folded subtree below it,
  15875. this function returns t, nil otherwise."
  15876. (let ((re (cond
  15877. ((eq what 'headlines) (concat "^" org-outline-regexp))
  15878. ((eq what 'items) (org-item-beginning-re))
  15879. (t (error "This should not happen"))))
  15880. beg end)
  15881. (save-excursion
  15882. (catch 'exit
  15883. (unless (org-region-active-p)
  15884. (setq beg (point-at-bol))
  15885. (beginning-of-line 2)
  15886. (while (and (not (eobp)) ;; this is like `next-line'
  15887. (get-char-property (1- (point)) 'invisible))
  15888. (beginning-of-line 2))
  15889. (setq end (point))
  15890. (goto-char beg)
  15891. (goto-char (point-at-eol))
  15892. (setq end (max end (point)))
  15893. (while (re-search-forward re end t)
  15894. (if (get-char-property (match-beginning 0) 'invisible)
  15895. (throw 'exit t))))
  15896. nil))))
  15897. (defun org-metaup (&optional arg)
  15898. "Move subtree up or move table row up.
  15899. Calls `org-move-subtree-up' or `org-table-move-row' or
  15900. `org-move-item-up', depending on context. See the individual commands
  15901. for more information."
  15902. (interactive "P")
  15903. (cond
  15904. ((run-hook-with-args-until-success 'org-metaup-hook))
  15905. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15906. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15907. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15908. (t (transpose-lines 1) (beginning-of-line -1))))
  15909. (defun org-metadown (&optional arg)
  15910. "Move subtree down or move table row down.
  15911. Calls `org-move-subtree-down' or `org-table-move-row' or
  15912. `org-move-item-down', depending on context. See the individual
  15913. commands for more information."
  15914. (interactive "P")
  15915. (cond
  15916. ((run-hook-with-args-until-success 'org-metadown-hook))
  15917. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15918. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15919. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15920. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15921. (defun org-shiftup (&optional arg)
  15922. "Increase item in timestamp or increase priority of current headline.
  15923. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15924. depending on context. See the individual commands for more information."
  15925. (interactive "P")
  15926. (cond
  15927. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15928. ((and org-support-shift-select (org-region-active-p))
  15929. (org-call-for-shift-select 'previous-line))
  15930. ((org-at-timestamp-p t)
  15931. (call-interactively (if org-edit-timestamp-down-means-later
  15932. 'org-timestamp-down 'org-timestamp-up)))
  15933. ((and (not (eq org-support-shift-select 'always))
  15934. org-enable-priority-commands
  15935. (org-on-heading-p))
  15936. (call-interactively 'org-priority-up))
  15937. ((and (not org-support-shift-select) (org-at-item-p))
  15938. (call-interactively 'org-previous-item))
  15939. ((org-clocktable-try-shift 'up arg))
  15940. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15941. (org-support-shift-select
  15942. (org-call-for-shift-select 'previous-line))
  15943. (t (org-shiftselect-error))))
  15944. (defun org-shiftdown (&optional arg)
  15945. "Decrease item in timestamp or decrease priority of current headline.
  15946. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15947. depending on context. See the individual commands for more information."
  15948. (interactive "P")
  15949. (cond
  15950. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15951. ((and org-support-shift-select (org-region-active-p))
  15952. (org-call-for-shift-select 'next-line))
  15953. ((org-at-timestamp-p t)
  15954. (call-interactively (if org-edit-timestamp-down-means-later
  15955. 'org-timestamp-up 'org-timestamp-down)))
  15956. ((and (not (eq org-support-shift-select 'always))
  15957. org-enable-priority-commands
  15958. (org-on-heading-p))
  15959. (call-interactively 'org-priority-down))
  15960. ((and (not org-support-shift-select) (org-at-item-p))
  15961. (call-interactively 'org-next-item))
  15962. ((org-clocktable-try-shift 'down arg))
  15963. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15964. (org-support-shift-select
  15965. (org-call-for-shift-select 'next-line))
  15966. (t (org-shiftselect-error))))
  15967. (defun org-shiftright (&optional arg)
  15968. "Cycle the thing at point or in the current line, depending on context.
  15969. Depending on context, this does one of the following:
  15970. - switch a timestamp at point one day into the future
  15971. - on a headline, switch to the next TODO keyword.
  15972. - on an item, switch entire list to the next bullet type
  15973. - on a property line, switch to the next allowed value
  15974. - on a clocktable definition line, move time block into the future"
  15975. (interactive "P")
  15976. (cond
  15977. ((run-hook-with-args-until-success 'org-shiftright-hook))
  15978. ((and org-support-shift-select (org-region-active-p))
  15979. (org-call-for-shift-select 'forward-char))
  15980. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15981. ((and (not (eq org-support-shift-select 'always))
  15982. (org-on-heading-p))
  15983. (let ((org-inhibit-logging
  15984. (not org-treat-S-cursor-todo-selection-as-state-change))
  15985. (org-inhibit-blocking
  15986. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15987. (org-call-with-arg 'org-todo 'right)))
  15988. ((or (and org-support-shift-select
  15989. (not (eq org-support-shift-select 'always))
  15990. (org-at-item-bullet-p))
  15991. (and (not org-support-shift-select) (org-at-item-p)))
  15992. (org-call-with-arg 'org-cycle-list-bullet nil))
  15993. ((and (not (eq org-support-shift-select 'always))
  15994. (org-at-property-p))
  15995. (call-interactively 'org-property-next-allowed-value))
  15996. ((org-clocktable-try-shift 'right arg))
  15997. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  15998. (org-support-shift-select
  15999. (org-call-for-shift-select 'forward-char))
  16000. (t (org-shiftselect-error))))
  16001. (defun org-shiftleft (&optional arg)
  16002. "Cycle the thing at point or in the current line, depending on context.
  16003. Depending on context, this does one of the following:
  16004. - switch a timestamp at point one day into the past
  16005. - on a headline, switch to the previous TODO keyword.
  16006. - on an item, switch entire list to the previous bullet type
  16007. - on a property line, switch to the previous allowed value
  16008. - on a clocktable definition line, move time block into the past"
  16009. (interactive "P")
  16010. (cond
  16011. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  16012. ((and org-support-shift-select (org-region-active-p))
  16013. (org-call-for-shift-select 'backward-char))
  16014. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  16015. ((and (not (eq org-support-shift-select 'always))
  16016. (org-on-heading-p))
  16017. (let ((org-inhibit-logging
  16018. (not org-treat-S-cursor-todo-selection-as-state-change))
  16019. (org-inhibit-blocking
  16020. (not org-treat-S-cursor-todo-selection-as-state-change)))
  16021. (org-call-with-arg 'org-todo 'left)))
  16022. ((or (and org-support-shift-select
  16023. (not (eq org-support-shift-select 'always))
  16024. (org-at-item-bullet-p))
  16025. (and (not org-support-shift-select) (org-at-item-p)))
  16026. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  16027. ((and (not (eq org-support-shift-select 'always))
  16028. (org-at-property-p))
  16029. (call-interactively 'org-property-previous-allowed-value))
  16030. ((org-clocktable-try-shift 'left arg))
  16031. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  16032. (org-support-shift-select
  16033. (org-call-for-shift-select 'backward-char))
  16034. (t (org-shiftselect-error))))
  16035. (defun org-shiftcontrolright ()
  16036. "Switch to next TODO set."
  16037. (interactive)
  16038. (cond
  16039. ((and org-support-shift-select (org-region-active-p))
  16040. (org-call-for-shift-select 'forward-word))
  16041. ((and (not (eq org-support-shift-select 'always))
  16042. (org-on-heading-p))
  16043. (org-call-with-arg 'org-todo 'nextset))
  16044. (org-support-shift-select
  16045. (org-call-for-shift-select 'forward-word))
  16046. (t (org-shiftselect-error))))
  16047. (defun org-shiftcontrolleft ()
  16048. "Switch to previous TODO set."
  16049. (interactive)
  16050. (cond
  16051. ((and org-support-shift-select (org-region-active-p))
  16052. (org-call-for-shift-select 'backward-word))
  16053. ((and (not (eq org-support-shift-select 'always))
  16054. (org-on-heading-p))
  16055. (org-call-with-arg 'org-todo 'previousset))
  16056. (org-support-shift-select
  16057. (org-call-for-shift-select 'backward-word))
  16058. (t (org-shiftselect-error))))
  16059. (defun org-ctrl-c-ret ()
  16060. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  16061. (interactive)
  16062. (cond
  16063. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  16064. (t (call-interactively 'org-insert-heading))))
  16065. (defun org-copy-special ()
  16066. "Copy region in table or copy current subtree.
  16067. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  16068. See the individual commands for more information."
  16069. (interactive)
  16070. (call-interactively
  16071. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  16072. (defun org-cut-special ()
  16073. "Cut region in table or cut current subtree.
  16074. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  16075. See the individual commands for more information."
  16076. (interactive)
  16077. (call-interactively
  16078. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  16079. (defun org-paste-special (arg)
  16080. "Paste rectangular region into table, or past subtree relative to level.
  16081. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  16082. See the individual commands for more information."
  16083. (interactive "P")
  16084. (if (org-at-table-p)
  16085. (org-table-paste-rectangle)
  16086. (org-paste-subtree arg)))
  16087. (defun org-edit-special (&optional arg)
  16088. "Call a special editor for the stuff at point.
  16089. When at a table, call the formula editor with `org-table-edit-formulas'.
  16090. When at the first line of an src example, call `org-edit-src-code'.
  16091. When in an #+include line, visit the include file. Otherwise call
  16092. `ffap' to visit the file at point."
  16093. (interactive)
  16094. ;; possibly prep session before editing source
  16095. (when arg
  16096. (let* ((info (org-babel-get-src-block-info))
  16097. (lang (nth 0 info))
  16098. (params (nth 2 info))
  16099. (session (cdr (assoc :session params))))
  16100. (when (and info session) ;; we are in a source-code block with a session
  16101. (funcall
  16102. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16103. (cond ;; proceed with `org-edit-special'
  16104. ((save-excursion
  16105. (beginning-of-line 1)
  16106. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16107. (find-file (org-trim (match-string 1))))
  16108. ((org-edit-src-code))
  16109. ((org-edit-fixed-width-region))
  16110. ((org-at-table.el-p)
  16111. (org-edit-src-code))
  16112. ((or (org-at-table-p)
  16113. (save-excursion
  16114. (beginning-of-line 1)
  16115. (looking-at "[ \t]*#\\+TBLFM:")))
  16116. (call-interactively 'org-table-edit-formulas))
  16117. (t (call-interactively 'ffap))))
  16118. (defun org-ctrl-c-ctrl-c (&optional arg)
  16119. "Set tags in headline, or update according to changed information at point.
  16120. This command does many different things, depending on context:
  16121. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16122. this is what we do.
  16123. - If the cursor is on a statistics cookie, update it.
  16124. - If the cursor is in a headline, prompt for tags and insert them
  16125. into the current line, aligned to `org-tags-column'. When called
  16126. with prefix arg, realign all tags in the current buffer.
  16127. - If the cursor is in one of the special #+KEYWORD lines, this
  16128. triggers scanning the buffer for these lines and updating the
  16129. information.
  16130. - If the cursor is inside a table, realign the table. This command
  16131. works even if the automatic table editor has been turned off.
  16132. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16133. the entire table.
  16134. - If the cursor is at a footnote reference or definition, jump to
  16135. the corresponding definition or references, respectively.
  16136. - If the cursor is a the beginning of a dynamic block, update it.
  16137. - If the current buffer is a capture buffer, close note and file it.
  16138. - If the cursor is on a <<<target>>>, update radio targets and
  16139. corresponding links in this buffer.
  16140. - If the cursor is on a numbered item in a plain list, renumber the
  16141. ordered list.
  16142. - If the cursor is on a checkbox, toggle it.
  16143. - If the cursor is on a code block, evaluate it. The variable
  16144. `org-confirm-babel-evaluate' can be used to control prompting
  16145. before code block evaluation, by default every code block
  16146. evaluation requires confirmation. Code block evaluation can be
  16147. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16148. (interactive "P")
  16149. (let ((org-enable-table-editor t))
  16150. (cond
  16151. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16152. org-occur-highlights
  16153. org-latex-fragment-image-overlays)
  16154. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16155. (org-remove-occur-highlights)
  16156. (org-remove-latex-fragment-image-overlays)
  16157. (message "Temporary highlights/overlays removed from current buffer"))
  16158. ((and (local-variable-p 'org-finish-function (current-buffer))
  16159. (fboundp org-finish-function))
  16160. (funcall org-finish-function))
  16161. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16162. ((or (looking-at org-property-start-re)
  16163. (org-at-property-p))
  16164. (call-interactively 'org-property-action))
  16165. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  16166. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16167. (or (org-on-heading-p) (org-at-item-p)))
  16168. (call-interactively 'org-update-statistics-cookies))
  16169. ((org-on-heading-p) (call-interactively 'org-set-tags))
  16170. ((org-at-table.el-p)
  16171. (message "Use C-c ' to edit table.el tables"))
  16172. ((org-at-table-p)
  16173. (org-table-maybe-eval-formula)
  16174. (if arg
  16175. (call-interactively 'org-table-recalculate)
  16176. (org-table-maybe-recalculate-line))
  16177. (call-interactively 'org-table-align)
  16178. (orgtbl-send-table 'maybe))
  16179. ((or (org-footnote-at-reference-p)
  16180. (org-footnote-at-definition-p))
  16181. (call-interactively 'org-footnote-action))
  16182. ((org-at-item-checkbox-p)
  16183. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16184. ;; list only if at top item.
  16185. (let* ((cbox (match-string 1))
  16186. (struct (org-list-struct))
  16187. (old-struct (copy-tree struct))
  16188. (parents (org-list-parents-alist struct))
  16189. (prevs (org-list-prevs-alist struct))
  16190. (orderedp (org-entry-get nil "ORDERED"))
  16191. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16192. block-item)
  16193. ;; Use a light version of `org-toggle-checkbox' to avoid
  16194. ;; computing list structure twice.
  16195. (org-list-set-checkbox (point-at-bol) struct
  16196. (cond
  16197. ((equal arg '(16)) "[-]")
  16198. ((equal arg '(4)) nil)
  16199. ((equal "[X]" cbox) "[ ]")
  16200. (t "[X]")))
  16201. (org-list-struct-fix-ind struct parents)
  16202. (org-list-struct-fix-bul struct prevs)
  16203. (setq block-item
  16204. (org-list-struct-fix-box struct parents prevs orderedp))
  16205. (when block-item
  16206. (message
  16207. "Checkboxes were removed due to unchecked box at line %d"
  16208. (org-current-line block-item)))
  16209. (org-list-struct-apply-struct struct old-struct)
  16210. (org-update-checkbox-count-maybe)
  16211. (when firstp (org-list-send-list 'maybe))))
  16212. ((org-at-item-p)
  16213. ;; Cursor at an item: repair list. Do checkbox related actions
  16214. ;; only if function was called with an argument. Send list only
  16215. ;; if at top item.
  16216. (let* ((struct (org-list-struct))
  16217. (old-struct (copy-tree struct))
  16218. (parents (org-list-parents-alist struct))
  16219. (prevs (org-list-prevs-alist struct))
  16220. (firstp (= (org-list-get-top-point struct) (point-at-bol))))
  16221. (org-list-struct-fix-ind struct parents)
  16222. (org-list-struct-fix-bul struct prevs)
  16223. (when arg
  16224. (org-list-set-checkbox (point-at-bol) struct "[ ]")
  16225. (org-list-struct-fix-box struct parents prevs))
  16226. (org-list-struct-apply-struct struct old-struct)
  16227. (when arg (org-update-checkbox-count-maybe))
  16228. (when firstp (org-list-send-list 'maybe))))
  16229. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  16230. ;; Dynamic block
  16231. (beginning-of-line 1)
  16232. (save-excursion (org-update-dblock)))
  16233. ((save-excursion
  16234. (beginning-of-line 1)
  16235. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  16236. (cond
  16237. ((equal (match-string 1) "TBLFM")
  16238. ;; Recalculate the table before this line
  16239. (save-excursion
  16240. (beginning-of-line 1)
  16241. (skip-chars-backward " \r\n\t")
  16242. (if (org-at-table-p)
  16243. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  16244. (t
  16245. (let ((org-inhibit-startup-visibility-stuff t)
  16246. (org-startup-align-all-tables nil))
  16247. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  16248. (message "Local setup has been refreshed"))))
  16249. ((org-clock-update-time-maybe))
  16250. (t (error "C-c C-c can do nothing useful at this location")))))
  16251. (defun org-mode-restart ()
  16252. "Restart Org-mode, to scan again for special lines.
  16253. Also updates the keyword regular expressions."
  16254. (interactive)
  16255. (org-mode)
  16256. (message "Org-mode restarted"))
  16257. (defun org-kill-note-or-show-branches ()
  16258. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  16259. (interactive)
  16260. (if (not org-finish-function)
  16261. (progn
  16262. (hide-subtree)
  16263. (call-interactively 'show-branches))
  16264. (let ((org-note-abort t))
  16265. (funcall org-finish-function))))
  16266. (defun org-return (&optional indent)
  16267. "Goto next table row or insert a newline.
  16268. Calls `org-table-next-row' or `newline', depending on context.
  16269. See the individual commands for more information."
  16270. (interactive)
  16271. (cond
  16272. ((bobp) (if indent (newline-and-indent) (newline)))
  16273. ((org-at-table-p)
  16274. (org-table-justify-field-maybe)
  16275. (call-interactively 'org-table-next-row))
  16276. ;; when `newline-and-indent' is called within a list, make sure
  16277. ;; text moved stays inside the item.
  16278. ((and (org-in-item-p) indent)
  16279. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  16280. (progn
  16281. (newline)
  16282. (org-indent-line-to (length (match-string 0))))
  16283. (let ((ind (org-get-indentation)))
  16284. (newline)
  16285. (if (org-looking-back org-list-end-re)
  16286. (org-indent-line-function)
  16287. (org-indent-line-to ind)))))
  16288. ((and org-return-follows-link
  16289. (eq (get-text-property (point) 'face) 'org-link))
  16290. (call-interactively 'org-open-at-point))
  16291. ((and (org-at-heading-p)
  16292. (looking-at
  16293. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  16294. (org-show-entry)
  16295. (end-of-line 1)
  16296. (newline))
  16297. (t (if indent (newline-and-indent) (newline)))))
  16298. (defun org-return-indent ()
  16299. "Goto next table row or insert a newline and indent.
  16300. Calls `org-table-next-row' or `newline-and-indent', depending on
  16301. context. See the individual commands for more information."
  16302. (interactive)
  16303. (org-return t))
  16304. (defun org-ctrl-c-star ()
  16305. "Compute table, or change heading status of lines.
  16306. Calls `org-table-recalculate' or `org-toggle-heading',
  16307. depending on context."
  16308. (interactive)
  16309. (cond
  16310. ((org-at-table-p)
  16311. (call-interactively 'org-table-recalculate))
  16312. (t
  16313. ;; Convert all lines in region to list items
  16314. (call-interactively 'org-toggle-heading))))
  16315. (defun org-ctrl-c-minus ()
  16316. "Insert separator line in table or modify bullet status of line.
  16317. Also turns a plain line or a region of lines into list items.
  16318. Calls `org-table-insert-hline', `org-toggle-item', or
  16319. `org-cycle-list-bullet', depending on context."
  16320. (interactive)
  16321. (cond
  16322. ((org-at-table-p)
  16323. (call-interactively 'org-table-insert-hline))
  16324. ((org-region-active-p)
  16325. (call-interactively 'org-toggle-item))
  16326. ((org-in-item-p)
  16327. (call-interactively 'org-cycle-list-bullet))
  16328. (t
  16329. (call-interactively 'org-toggle-item))))
  16330. (defun org-toggle-item (arg)
  16331. "Convert headings or normal lines to items, items to normal lines.
  16332. If there is no active region, only the current line is considered.
  16333. If the first non blank line in the region is an headline, convert
  16334. all headlines to items, shifting text accordingly.
  16335. If it is an item, convert all items to normal lines.
  16336. If it is normal text, change region into an item. With a prefix
  16337. argument ARG, change each line in region into an item."
  16338. (interactive "P")
  16339. (let ((shift-text
  16340. (function
  16341. ;; Shift text in current section to IND, from point to END.
  16342. ;; The function leaves point to END line.
  16343. (lambda (ind end)
  16344. (let ((min-i 1000) (end (copy-marker end)))
  16345. ;; First determine the minimum indentation (MIN-I) of
  16346. ;; the text.
  16347. (save-excursion
  16348. (catch 'exit
  16349. (while (< (point) end)
  16350. (let ((i (org-get-indentation)))
  16351. (cond
  16352. ;; Skip blank lines and inline tasks.
  16353. ((looking-at "^[ \t]*$"))
  16354. ((looking-at "^\\*+ "))
  16355. ;; We can't find less than 0 indentation.
  16356. ((zerop i) (throw 'exit (setq min-i 0)))
  16357. ((< i min-i) (setq min-i i))))
  16358. (forward-line))))
  16359. ;; Then indent each line so that a line indented to
  16360. ;; MIN-I becomes indented to IND. Ignore blank lines
  16361. ;; and inline tasks in the process.
  16362. (let ((delta (- ind min-i)))
  16363. (while (< (point) end)
  16364. (unless (or (looking-at "^[ \t]*$")
  16365. (looking-at "^\\*+ "))
  16366. (org-indent-line-to (+ (org-get-indentation) delta)))
  16367. (forward-line)))))))
  16368. (skip-blanks
  16369. (function
  16370. ;; Return beginning of first non-blank line, starting from
  16371. ;; line at POS.
  16372. (lambda (pos)
  16373. (save-excursion
  16374. (goto-char pos)
  16375. (skip-chars-forward " \r\t\n")
  16376. (point-at-bol)))))
  16377. beg end)
  16378. ;; Determine boundaries of changes.
  16379. (if (org-region-active-p)
  16380. (setq beg (funcall skip-blanks (region-beginning))
  16381. end (copy-marker (region-end)))
  16382. (setq beg (funcall skip-blanks (point-at-bol))
  16383. end (copy-marker (point-at-eol))))
  16384. ;; Depending on the starting line, choose an action on the text
  16385. ;; between BEG and END.
  16386. (org-with-limited-levels
  16387. (save-excursion
  16388. (goto-char beg)
  16389. (cond
  16390. ;; Case 1. Start at an item: de-itemize. Note that it only
  16391. ;; happens when a region is active: `org-ctrl-c-minus'
  16392. ;; would call `org-cycle-list-bullet' otherwise.
  16393. ((org-at-item-p)
  16394. (while (< (point) end)
  16395. (when (org-at-item-p)
  16396. (skip-chars-forward " \t")
  16397. (delete-region (point) (match-end 0)))
  16398. (forward-line)))
  16399. ;; Case 2. Start at an heading: convert to items.
  16400. ((org-on-heading-p)
  16401. (let* ((bul (org-list-bullet-string "-"))
  16402. (bul-len (length bul))
  16403. ;; Indentation of the first heading. It should be
  16404. ;; relative to the indentation of its parent, if any.
  16405. (start-ind (save-excursion
  16406. (cond
  16407. ((not org-adapt-indentation) 0)
  16408. ((not (outline-previous-heading)) 0)
  16409. (t (length (match-string 0))))))
  16410. ;; Level of first heading. Further headings will be
  16411. ;; compared to it to determine hierarchy in the list.
  16412. (ref-level (org-reduced-level (org-outline-level))))
  16413. (while (< (point) end)
  16414. (let* ((level (org-reduced-level (org-outline-level)))
  16415. (delta (max 0 (- level ref-level))))
  16416. ;; If current headline is less indented than the first
  16417. ;; one, set it as reference, in order to preserve
  16418. ;; subtrees.
  16419. (when (< level ref-level) (setq ref-level level))
  16420. (replace-match bul t t)
  16421. (org-indent-line-to (+ start-ind (* delta bul-len)))
  16422. ;; Ensure all text down to END (or SECTION-END) belongs
  16423. ;; to the newly created item.
  16424. (let ((section-end (save-excursion
  16425. (or (outline-next-heading) (point)))))
  16426. (forward-line)
  16427. (funcall shift-text
  16428. (+ start-ind (* (1+ delta) bul-len))
  16429. (min end section-end)))))))
  16430. ;; Case 3. Normal line with ARG: turn each non-item line into
  16431. ;; an item.
  16432. (arg
  16433. (while (< (point) end)
  16434. (unless (or (org-on-heading-p) (org-at-item-p))
  16435. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  16436. (replace-match
  16437. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  16438. (forward-line)))
  16439. ;; Case 4. Normal line without ARG: make the first line of
  16440. ;; region an item, and shift indentation of others
  16441. ;; lines to set them as item's body.
  16442. (t (let* ((bul (org-list-bullet-string "-"))
  16443. (bul-len (length bul))
  16444. (ref-ind (org-get-indentation)))
  16445. (skip-chars-forward " \t")
  16446. (insert bul)
  16447. (forward-line)
  16448. (while (< (point) end)
  16449. ;; Ensure that lines less indented than first one
  16450. ;; still get included in item body.
  16451. (funcall shift-text
  16452. (+ ref-ind bul-len)
  16453. (min end (save-excursion (or (outline-next-heading)
  16454. (point)))))
  16455. (forward-line)))))))))
  16456. (defun org-toggle-heading (&optional nstars)
  16457. "Convert headings to normal text, or items or text to headings.
  16458. If there is no active region, only the current line is considered.
  16459. If the first non blank line is an headline, remove the stars from
  16460. all headlines in the region.
  16461. If it is a plain list item, turn all plain list items into headings.
  16462. If it is a normal line, turn each and every normal line (i.e. not
  16463. an heading or an item) in the region into a heading.
  16464. When converting a line into a heading, the number of stars is chosen
  16465. such that the lines become children of the current entry. However,
  16466. when a prefix argument is given, its value determines the number of
  16467. stars to add."
  16468. (interactive "P")
  16469. (let ((skip-blanks
  16470. (function
  16471. ;; Return beginning of first non-blank line, starting from
  16472. ;; line at POS.
  16473. (lambda (pos)
  16474. (save-excursion
  16475. (goto-char pos)
  16476. (skip-chars-forward " \r\t\n")
  16477. (point-at-bol)))))
  16478. beg end)
  16479. ;; Determine boundaries of changes. If region ends at a bol, do
  16480. ;; not consider the last line to be in the region.
  16481. (if (org-region-active-p)
  16482. (setq beg (funcall skip-blanks (region-beginning))
  16483. end (copy-marker (save-excursion
  16484. (goto-char (region-end))
  16485. (if (bolp) (point) (point-at-eol)))))
  16486. (setq beg (funcall skip-blanks (point-at-bol))
  16487. end (copy-marker (point-at-eol))))
  16488. ;; Ensure inline tasks don't count as headings.
  16489. (org-with-limited-levels
  16490. (save-excursion
  16491. (goto-char beg)
  16492. (cond
  16493. ;; Case 1. Started at an heading: de-star headings.
  16494. ((org-on-heading-p)
  16495. (while (< (point) end)
  16496. (when (org-on-heading-p t)
  16497. (looking-at outline-regexp) (replace-match ""))
  16498. (forward-line)))
  16499. ;; Case 2. Started at an item: change items into headlines.
  16500. ;; One star will be added by `org-list-to-subtree'.
  16501. ((org-at-item-p)
  16502. (let* ((stars (make-string
  16503. (if nstars
  16504. ;; subtract the star that will be added again by
  16505. ;; `org-list-to-subtree'
  16506. (1- (prefix-numeric-value current-prefix-arg))
  16507. (or (org-current-level) 0))
  16508. ?*))
  16509. (add-stars
  16510. (cond (nstars "") ; stars from prefix only
  16511. ((equal stars "") "") ; before first heading
  16512. (org-odd-levels-only "*") ; inside heading, odd
  16513. (t "")))) ; inside heading, oddeven
  16514. (while (< (point) end)
  16515. (when (org-at-item-p)
  16516. ;; Pay attention to cases when region ends before list.
  16517. (let* ((struct (org-list-struct))
  16518. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  16519. (save-restriction
  16520. (narrow-to-region (point) list-end)
  16521. (insert
  16522. (org-list-to-subtree
  16523. (org-list-parse-list t)
  16524. '(:istart (concat stars add-stars (funcall get-stars depth))
  16525. :icount (concat stars add-stars (funcall get-stars depth))))))))
  16526. (forward-line))))
  16527. ;; Case 3. Started at normal text: make every line an heading,
  16528. ;; skipping headlines and items.
  16529. (t (let* ((stars (make-string
  16530. (if nstars
  16531. (prefix-numeric-value current-prefix-arg)
  16532. (or (org-current-level) 0))
  16533. ?*))
  16534. (add-stars
  16535. (cond (nstars "") ; stars from prefix only
  16536. ((equal stars "") "*") ; before first heading
  16537. (org-odd-levels-only "**") ; inside heading, odd
  16538. (t "*"))) ; inside heading, oddeven
  16539. (rpl (concat stars add-stars " ")))
  16540. (while (< (point) end)
  16541. (when (and (not (org-on-heading-p)) (not (org-at-item-p))
  16542. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  16543. (replace-match (concat rpl (match-string 2))))
  16544. (forward-line)))))))))
  16545. (defun org-meta-return (&optional arg)
  16546. "Insert a new heading or wrap a region in a table.
  16547. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  16548. See the individual commands for more information."
  16549. (interactive "P")
  16550. (cond
  16551. ((run-hook-with-args-until-success 'org-metareturn-hook))
  16552. ((org-at-table-p)
  16553. (call-interactively 'org-table-wrap-region))
  16554. (t (call-interactively 'org-insert-heading))))
  16555. ;;; Menu entries
  16556. ;; Define the Org-mode menus
  16557. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  16558. '("Tbl"
  16559. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  16560. ["Next Field" org-cycle (org-at-table-p)]
  16561. ["Previous Field" org-shifttab (org-at-table-p)]
  16562. ["Next Row" org-return (org-at-table-p)]
  16563. "--"
  16564. ["Blank Field" org-table-blank-field (org-at-table-p)]
  16565. ["Edit Field" org-table-edit-field (org-at-table-p)]
  16566. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  16567. "--"
  16568. ("Column"
  16569. ["Move Column Left" org-metaleft (org-at-table-p)]
  16570. ["Move Column Right" org-metaright (org-at-table-p)]
  16571. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  16572. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  16573. ("Row"
  16574. ["Move Row Up" org-metaup (org-at-table-p)]
  16575. ["Move Row Down" org-metadown (org-at-table-p)]
  16576. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  16577. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  16578. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  16579. "--"
  16580. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  16581. ("Rectangle"
  16582. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  16583. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  16584. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  16585. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  16586. "--"
  16587. ("Calculate"
  16588. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  16589. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  16590. ["Edit Formulas" org-edit-special (org-at-table-p)]
  16591. "--"
  16592. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  16593. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  16594. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  16595. "--"
  16596. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  16597. "--"
  16598. ["Sum Column/Rectangle" org-table-sum
  16599. (or (org-at-table-p) (org-region-active-p))]
  16600. ["Which Column?" org-table-current-column (org-at-table-p)])
  16601. ["Debug Formulas"
  16602. org-table-toggle-formula-debugger
  16603. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  16604. ["Show Col/Row Numbers"
  16605. org-table-toggle-coordinate-overlays
  16606. :style toggle
  16607. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  16608. "--"
  16609. ["Create" org-table-create (and (not (org-at-table-p))
  16610. org-enable-table-editor)]
  16611. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  16612. ["Import from File" org-table-import (not (org-at-table-p))]
  16613. ["Export to File" org-table-export (org-at-table-p)]
  16614. "--"
  16615. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  16616. (easy-menu-define org-org-menu org-mode-map "Org menu"
  16617. '("Org"
  16618. ("Show/Hide"
  16619. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  16620. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  16621. ["Sparse Tree..." org-sparse-tree t]
  16622. ["Reveal Context" org-reveal t]
  16623. ["Show All" show-all t]
  16624. "--"
  16625. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  16626. "--"
  16627. ["New Heading" org-insert-heading t]
  16628. ("Navigate Headings"
  16629. ["Up" outline-up-heading t]
  16630. ["Next" outline-next-visible-heading t]
  16631. ["Previous" outline-previous-visible-heading t]
  16632. ["Next Same Level" outline-forward-same-level t]
  16633. ["Previous Same Level" outline-backward-same-level t]
  16634. "--"
  16635. ["Jump" org-goto t])
  16636. ("Edit Structure"
  16637. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  16638. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  16639. "--"
  16640. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  16641. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  16642. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  16643. "--"
  16644. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  16645. "--"
  16646. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  16647. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  16648. ["Demote Heading" org-metaright (not (org-at-table-p))]
  16649. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  16650. "--"
  16651. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  16652. "--"
  16653. ["Convert to odd levels" org-convert-to-odd-levels t]
  16654. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  16655. ("Editing"
  16656. ["Emphasis..." org-emphasize t]
  16657. ["Edit Source Example" org-edit-special t]
  16658. "--"
  16659. ["Footnote new/jump" org-footnote-action t]
  16660. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  16661. ("Archive"
  16662. ["Archive (default method)" org-archive-subtree-default t]
  16663. "--"
  16664. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  16665. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  16666. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  16667. )
  16668. "--"
  16669. ("Hyperlinks"
  16670. ["Store Link (Global)" org-store-link t]
  16671. ["Find existing link to here" org-occur-link-in-agenda-files t]
  16672. ["Insert Link" org-insert-link t]
  16673. ["Follow Link" org-open-at-point t]
  16674. "--"
  16675. ["Next link" org-next-link t]
  16676. ["Previous link" org-previous-link t]
  16677. "--"
  16678. ["Descriptive Links"
  16679. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  16680. :style radio
  16681. :selected (member '(org-link) buffer-invisibility-spec)]
  16682. ["Literal Links"
  16683. (progn
  16684. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  16685. :style radio
  16686. :selected (not (member '(org-link) buffer-invisibility-spec))])
  16687. "--"
  16688. ("TODO Lists"
  16689. ["TODO/DONE/-" org-todo t]
  16690. ("Select keyword"
  16691. ["Next keyword" org-shiftright (org-on-heading-p)]
  16692. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  16693. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  16694. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  16695. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  16696. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  16697. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  16698. "--"
  16699. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  16700. :selected org-enforce-todo-dependencies :style toggle :active t]
  16701. "Settings for tree at point"
  16702. ["Do Children sequentially" org-toggle-ordered-property :style radio
  16703. :selected (org-entry-get nil "ORDERED")
  16704. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16705. ["Do Children parallel" org-toggle-ordered-property :style radio
  16706. :selected (not (org-entry-get nil "ORDERED"))
  16707. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16708. "--"
  16709. ["Set Priority" org-priority t]
  16710. ["Priority Up" org-shiftup t]
  16711. ["Priority Down" org-shiftdown t]
  16712. "--"
  16713. ["Get news from all feeds" org-feed-update-all t]
  16714. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  16715. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  16716. ("TAGS and Properties"
  16717. ["Set Tags" org-set-tags-command t]
  16718. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  16719. "--"
  16720. ["Set property" org-set-property t]
  16721. ["Column view of properties" org-columns t]
  16722. ["Insert Column View DBlock" org-insert-columns-dblock t])
  16723. ("Dates and Scheduling"
  16724. ["Timestamp" org-time-stamp t]
  16725. ["Timestamp (inactive)" org-time-stamp-inactive t]
  16726. ("Change Date"
  16727. ["1 Day Later" org-shiftright t]
  16728. ["1 Day Earlier" org-shiftleft t]
  16729. ["1 ... Later" org-shiftup t]
  16730. ["1 ... Earlier" org-shiftdown t])
  16731. ["Compute Time Range" org-evaluate-time-range t]
  16732. ["Schedule Item" org-schedule t]
  16733. ["Deadline" org-deadline t]
  16734. "--"
  16735. ["Custom time format" org-toggle-time-stamp-overlays
  16736. :style radio :selected org-display-custom-times]
  16737. "--"
  16738. ["Goto Calendar" org-goto-calendar t]
  16739. ["Date from Calendar" org-date-from-calendar t]
  16740. "--"
  16741. ["Start/Restart Timer" org-timer-start t]
  16742. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  16743. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  16744. ["Insert Timer String" org-timer t]
  16745. ["Insert Timer Item" org-timer-item t])
  16746. ("Logging work"
  16747. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  16748. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  16749. ["Clock out" org-clock-out t]
  16750. ["Clock cancel" org-clock-cancel t]
  16751. "--"
  16752. ["Mark as default task" org-clock-mark-default-task t]
  16753. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  16754. ["Goto running clock" org-clock-goto t]
  16755. "--"
  16756. ["Display times" org-clock-display t]
  16757. ["Create clock table" org-clock-report t]
  16758. "--"
  16759. ["Record DONE time"
  16760. (progn (setq org-log-done (not org-log-done))
  16761. (message "Switching to %s will %s record a timestamp"
  16762. (car org-done-keywords)
  16763. (if org-log-done "automatically" "not")))
  16764. :style toggle :selected org-log-done])
  16765. "--"
  16766. ["Agenda Command..." org-agenda t]
  16767. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  16768. ("File List for Agenda")
  16769. ("Special views current file"
  16770. ["TODO Tree" org-show-todo-tree t]
  16771. ["Check Deadlines" org-check-deadlines t]
  16772. ["Timeline" org-timeline t]
  16773. ["Tags/Property tree" org-match-sparse-tree t])
  16774. "--"
  16775. ["Export/Publish..." org-export t]
  16776. ("LaTeX"
  16777. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  16778. :selected org-cdlatex-mode]
  16779. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  16780. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  16781. ["Modify math symbol" org-cdlatex-math-modify
  16782. (org-inside-LaTeX-fragment-p)]
  16783. ["Insert citation" org-reftex-citation t]
  16784. "--"
  16785. ["Template for BEAMER" org-insert-beamer-options-template t])
  16786. "--"
  16787. ("MobileOrg"
  16788. ["Push Files and Views" org-mobile-push t]
  16789. ["Get Captured and Flagged" org-mobile-pull t]
  16790. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  16791. "--"
  16792. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  16793. "--"
  16794. ("Documentation"
  16795. ["Show Version" org-version t]
  16796. ["Info Documentation" org-info t])
  16797. ("Customize"
  16798. ["Browse Org Group" org-customize t]
  16799. "--"
  16800. ["Expand This Menu" org-create-customize-menu
  16801. (fboundp 'customize-menu-create)])
  16802. ["Send bug report" org-submit-bug-report t]
  16803. "--"
  16804. ("Refresh/Reload"
  16805. ["Refresh setup current buffer" org-mode-restart t]
  16806. ["Reload Org (after update)" org-reload t]
  16807. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  16808. ))
  16809. (defun org-info (&optional node)
  16810. "Read documentation for Org-mode in the info system.
  16811. With optional NODE, go directly to that node."
  16812. (interactive)
  16813. (info (format "(org)%s" (or node ""))))
  16814. ;;;###autoload
  16815. (defun org-submit-bug-report ()
  16816. "Submit a bug report on Org-mode via mail.
  16817. Don't hesitate to report any problems or inaccurate documentation.
  16818. If you don't have setup sending mail from (X)Emacs, please copy the
  16819. output buffer into your mail program, as it gives us important
  16820. information about your Org-mode version and configuration."
  16821. (interactive)
  16822. (require 'reporter)
  16823. (org-load-modules-maybe)
  16824. (org-require-autoloaded-modules)
  16825. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  16826. (reporter-submit-bug-report
  16827. "emacs-orgmode@gnu.org"
  16828. (org-version)
  16829. (let (list)
  16830. (save-window-excursion
  16831. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  16832. (delete-other-windows)
  16833. (erase-buffer)
  16834. (insert "You are about to submit a bug report to the Org-mode mailing list.
  16835. We would like to add your full Org-mode and Outline configuration to the
  16836. bug report. This greatly simplifies the work of the maintainer and
  16837. other experts on the mailing list.
  16838. HOWEVER, some variables you have customized may contain private
  16839. information. The names of customers, colleagues, or friends, might
  16840. appear in the form of file names, tags, todo states, or search strings.
  16841. If you answer yes to the prompt, you might want to check and remove
  16842. such private information before sending the email.")
  16843. (add-text-properties (point-min) (point-max) '(face org-warning))
  16844. (when (yes-or-no-p "Include your Org-mode configuration ")
  16845. (mapatoms
  16846. (lambda (v)
  16847. (and (boundp v)
  16848. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  16849. (or (and (symbol-value v)
  16850. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  16851. (and
  16852. (get v 'custom-type) (get v 'standard-value)
  16853. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  16854. (push v list)))))
  16855. (kill-buffer (get-buffer "*Warn about privacy*"))
  16856. list))
  16857. nil nil
  16858. "Remember to cover the basics, that is, what you expected to happen and
  16859. what in fact did happen. You don't know how to make a good report? See
  16860. http://orgmode.org/manual/Feedback.html#Feedback
  16861. Your bug report will be posted to the Org-mode mailing list.
  16862. ------------------------------------------------------------------------")
  16863. (save-excursion
  16864. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  16865. (replace-match "\\1Bug: \\3 [\\2]")))))
  16866. (defun org-install-agenda-files-menu ()
  16867. (let ((bl (buffer-list)))
  16868. (save-excursion
  16869. (while bl
  16870. (set-buffer (pop bl))
  16871. (if (org-mode-p) (setq bl nil)))
  16872. (when (org-mode-p)
  16873. (easy-menu-change
  16874. '("Org") "File List for Agenda"
  16875. (append
  16876. (list
  16877. ["Edit File List" (org-edit-agenda-file-list) t]
  16878. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  16879. ["Remove Current File from List" org-remove-file t]
  16880. ["Cycle through agenda files" org-cycle-agenda-files t]
  16881. ["Occur in all agenda files" org-occur-in-agenda-files t]
  16882. "--")
  16883. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  16884. ;;;; Documentation
  16885. ;;;###autoload
  16886. (defun org-require-autoloaded-modules ()
  16887. (interactive)
  16888. (mapc 'require
  16889. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  16890. org-docbook org-exp org-html org-icalendar
  16891. org-id org-latex
  16892. org-publish org-remember org-table
  16893. org-timer org-xoxo)))
  16894. ;;;###autoload
  16895. (defun org-reload (&optional uncompiled)
  16896. "Reload all org lisp files.
  16897. With prefix arg UNCOMPILED, load the uncompiled versions."
  16898. (interactive "P")
  16899. (require 'find-func)
  16900. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  16901. (dir-org (file-name-directory (org-find-library-name "org")))
  16902. (dir-org-contrib (ignore-errors
  16903. (file-name-directory
  16904. (org-find-library-name "org-contribdir"))))
  16905. (babel-files
  16906. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  16907. (append (list nil "comint" "eval" "exp" "keys"
  16908. "lob" "ref" "table" "tangle")
  16909. (delq nil
  16910. (mapcar
  16911. (lambda (lang)
  16912. (when (cdr lang) (symbol-name (car lang))))
  16913. org-babel-load-languages)))))
  16914. (files
  16915. (append (directory-files dir-org t file-re)
  16916. babel-files
  16917. (and dir-org-contrib
  16918. (directory-files dir-org-contrib t file-re))))
  16919. (remove-re (concat (if (featurep 'xemacs)
  16920. "org-colview" "org-colview-xemacs")
  16921. "\\'")))
  16922. (setq files (mapcar 'file-name-sans-extension files))
  16923. (setq files (mapcar
  16924. (lambda (x) (if (string-match remove-re x) nil x))
  16925. files))
  16926. (setq files (delq nil files))
  16927. (mapc
  16928. (lambda (f)
  16929. (when (featurep (intern (file-name-nondirectory f)))
  16930. (if (and (not uncompiled)
  16931. (file-exists-p (concat f ".elc")))
  16932. (load (concat f ".elc") nil nil t)
  16933. (load (concat f ".el") nil nil t))))
  16934. files))
  16935. (org-version))
  16936. ;;;###autoload
  16937. (defun org-customize ()
  16938. "Call the customize function with org as argument."
  16939. (interactive)
  16940. (org-load-modules-maybe)
  16941. (org-require-autoloaded-modules)
  16942. (customize-browse 'org))
  16943. (defun org-create-customize-menu ()
  16944. "Create a full customization menu for Org-mode, insert it into the menu."
  16945. (interactive)
  16946. (org-load-modules-maybe)
  16947. (org-require-autoloaded-modules)
  16948. (if (fboundp 'customize-menu-create)
  16949. (progn
  16950. (easy-menu-change
  16951. '("Org") "Customize"
  16952. `(["Browse Org group" org-customize t]
  16953. "--"
  16954. ,(customize-menu-create 'org)
  16955. ["Set" Custom-set t]
  16956. ["Save" Custom-save t]
  16957. ["Reset to Current" Custom-reset-current t]
  16958. ["Reset to Saved" Custom-reset-saved t]
  16959. ["Reset to Standard Settings" Custom-reset-standard t]))
  16960. (message "\"Org\"-menu now contains full customization menu"))
  16961. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  16962. ;;;; Miscellaneous stuff
  16963. ;;; Generally useful functions
  16964. (defun org-get-at-bol (property)
  16965. "Get text property PROPERTY at beginning of line."
  16966. (get-text-property (point-at-bol) property))
  16967. (defun org-find-text-property-in-string (prop s)
  16968. "Return the first non-nil value of property PROP in string S."
  16969. (or (get-text-property 0 prop s)
  16970. (get-text-property (or (next-single-property-change 0 prop s) 0)
  16971. prop s)))
  16972. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  16973. "Display the given MESSAGE as a warning."
  16974. (if (fboundp 'display-warning)
  16975. (display-warning 'org message
  16976. (if (featurep 'xemacs) 'warning :warning))
  16977. (let ((buf (get-buffer-create "*Org warnings*")))
  16978. (with-current-buffer buf
  16979. (goto-char (point-max))
  16980. (insert "Warning (Org): " message)
  16981. (unless (bolp)
  16982. (newline)))
  16983. (display-buffer buf)
  16984. (sit-for 0))))
  16985. (defun org-eval (form)
  16986. "Eval FORM and return result."
  16987. (condition-case error
  16988. (eval form)
  16989. (error (format "%%![Error: %s]" error))))
  16990. (defun org-in-commented-line ()
  16991. "Is point in a line starting with `#'?"
  16992. (equal (char-after (point-at-bol)) ?#))
  16993. (defun org-in-indented-comment-line ()
  16994. "Is point in a line starting with `#' after some white space?"
  16995. (save-excursion
  16996. (save-match-data
  16997. (goto-char (point-at-bol))
  16998. (looking-at "[ \t]*#"))))
  16999. (defun org-in-verbatim-emphasis ()
  17000. (save-match-data
  17001. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  17002. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  17003. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  17004. (if (and marker (marker-buffer marker)
  17005. (buffer-live-p (marker-buffer marker)))
  17006. (progn
  17007. (switch-to-buffer (marker-buffer marker))
  17008. (if (or (> marker (point-max)) (< marker (point-min)))
  17009. (widen))
  17010. (goto-char marker)
  17011. (org-show-context 'org-goto))
  17012. (if bookmark
  17013. (bookmark-jump bookmark)
  17014. (error "Cannot find location"))))
  17015. (defun org-quote-csv-field (s)
  17016. "Quote field for inclusion in CSV material."
  17017. (if (string-match "[\",]" s)
  17018. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  17019. s))
  17020. (defun org-force-self-insert (N)
  17021. "Needed to enforce self-insert under remapping."
  17022. (interactive "p")
  17023. (self-insert-command N))
  17024. (defun org-string-width (s)
  17025. "Compute width of string, ignoring invisible characters.
  17026. This ignores character with invisibility property `org-link', and also
  17027. characters with property `org-cwidth', because these will become invisible
  17028. upon the next fontification round."
  17029. (let (b l)
  17030. (when (or (eq t buffer-invisibility-spec)
  17031. (assq 'org-link buffer-invisibility-spec))
  17032. (while (setq b (text-property-any 0 (length s)
  17033. 'invisible 'org-link s))
  17034. (setq s (concat (substring s 0 b)
  17035. (substring s (or (next-single-property-change
  17036. b 'invisible s) (length s)))))))
  17037. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  17038. (setq s (concat (substring s 0 b)
  17039. (substring s (or (next-single-property-change
  17040. b 'org-cwidth s) (length s))))))
  17041. (setq l (string-width s) b -1)
  17042. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  17043. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  17044. l))
  17045. (defun org-shorten-string (s maxlength)
  17046. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  17047. If the string is shorter or has length MAXLENGTH, just return the
  17048. original string. If it is longer, the functions finds a space in the
  17049. string, breaks this string off at that locations and adds three dots
  17050. as ellipsis. Including the ellipsis, the string will not be longer
  17051. than MAXLENGTH. If finding a good breaking point in the string does
  17052. not work, the string is just chopped off in the middle of a word
  17053. if necessary."
  17054. (if (<= (length s) maxlength)
  17055. s
  17056. (let* ((n (max (- maxlength 4) 1))
  17057. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  17058. (if (string-match re s)
  17059. (concat (match-string 1 s) "...")
  17060. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  17061. (defun org-get-indentation (&optional line)
  17062. "Get the indentation of the current line, interpreting tabs.
  17063. When LINE is given, assume it represents a line and compute its indentation."
  17064. (if line
  17065. (if (string-match "^ *" (org-remove-tabs line))
  17066. (match-end 0))
  17067. (save-excursion
  17068. (beginning-of-line 1)
  17069. (skip-chars-forward " \t")
  17070. (current-column))))
  17071. (defun org-get-string-indentation (s)
  17072. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  17073. (let ((n -1) (i 0) (w tab-width) c)
  17074. (catch 'exit
  17075. (while (< (setq n (1+ n)) (length s))
  17076. (setq c (aref s n))
  17077. (cond ((= c ?\ ) (setq i (1+ i)))
  17078. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  17079. (t (throw 'exit t)))))
  17080. i))
  17081. (defun org-remove-tabs (s &optional width)
  17082. "Replace tabulators in S with spaces.
  17083. Assumes that s is a single line, starting in column 0."
  17084. (setq width (or width tab-width))
  17085. (while (string-match "\t" s)
  17086. (setq s (replace-match
  17087. (make-string
  17088. (- (* width (/ (+ (match-beginning 0) width) width))
  17089. (match-beginning 0)) ?\ )
  17090. t t s)))
  17091. s)
  17092. (defun org-fix-indentation (line ind)
  17093. "Fix indentation in LINE.
  17094. IND is a cons cell with target and minimum indentation.
  17095. If the current indentation in LINE is smaller than the minimum,
  17096. leave it alone. If it is larger than ind, set it to the target."
  17097. (let* ((l (org-remove-tabs line))
  17098. (i (org-get-indentation l))
  17099. (i1 (car ind)) (i2 (cdr ind)))
  17100. (if (>= i i2) (setq l (substring line i2)))
  17101. (if (> i1 0)
  17102. (concat (make-string i1 ?\ ) l)
  17103. l)))
  17104. (defun org-remove-indentation (code &optional n)
  17105. "Remove the maximum common indentation from the lines in CODE.
  17106. N may optionally be the number of spaces to remove."
  17107. (with-temp-buffer
  17108. (insert code)
  17109. (org-do-remove-indentation n)
  17110. (buffer-string)))
  17111. (defun org-do-remove-indentation (&optional n)
  17112. "Remove the maximum common indentation from the buffer."
  17113. (untabify (point-min) (point-max))
  17114. (let ((min 10000) re)
  17115. (if n
  17116. (setq min n)
  17117. (goto-char (point-min))
  17118. (while (re-search-forward "^ *[^ \n]" nil t)
  17119. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  17120. (unless (or (= min 0) (= min 10000))
  17121. (setq re (format "^ \\{%d\\}" min))
  17122. (goto-char (point-min))
  17123. (while (re-search-forward re nil t)
  17124. (replace-match "")
  17125. (end-of-line 1))
  17126. min)))
  17127. (defun org-fill-template (template alist)
  17128. "Find each %key of ALIST in TEMPLATE and replace it."
  17129. (let ((case-fold-search nil)
  17130. entry key value)
  17131. (setq alist (sort (copy-sequence alist)
  17132. (lambda (a b) (< (length (car a)) (length (car b))))))
  17133. (while (setq entry (pop alist))
  17134. (setq template
  17135. (replace-regexp-in-string
  17136. (concat "%" (regexp-quote (car entry)))
  17137. (cdr entry) template t t)))
  17138. template))
  17139. (defun org-base-buffer (buffer)
  17140. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  17141. (if (not buffer)
  17142. buffer
  17143. (or (buffer-base-buffer buffer)
  17144. buffer)))
  17145. (defun org-trim (s)
  17146. "Remove whitespace at beginning and end of string."
  17147. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  17148. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  17149. s)
  17150. (defun org-wrap (string &optional width lines)
  17151. "Wrap string to either a number of lines, or a width in characters.
  17152. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  17153. that costs. If there is a word longer than WIDTH, the text is actually
  17154. wrapped to the length of that word.
  17155. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  17156. many lines, whatever width that takes.
  17157. The return value is a list of lines, without newlines at the end."
  17158. (let* ((words (org-split-string string "[ \t\n]+"))
  17159. (maxword (apply 'max (mapcar 'org-string-width words)))
  17160. w ll)
  17161. (cond (width
  17162. (org-do-wrap words (max maxword width)))
  17163. (lines
  17164. (setq w maxword)
  17165. (setq ll (org-do-wrap words maxword))
  17166. (if (<= (length ll) lines)
  17167. ll
  17168. (setq ll words)
  17169. (while (> (length ll) lines)
  17170. (setq w (1+ w))
  17171. (setq ll (org-do-wrap words w)))
  17172. ll))
  17173. (t (error "Cannot wrap this")))))
  17174. (defun org-do-wrap (words width)
  17175. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  17176. (let (lines line)
  17177. (while words
  17178. (setq line (pop words))
  17179. (while (and words (< (+ (length line) (length (car words))) width))
  17180. (setq line (concat line " " (pop words))))
  17181. (setq lines (push line lines)))
  17182. (nreverse lines)))
  17183. (defun org-split-string (string &optional separators)
  17184. "Splits STRING into substrings at SEPARATORS.
  17185. No empty strings are returned if there are matches at the beginning
  17186. and end of string."
  17187. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  17188. (start 0)
  17189. notfirst
  17190. (list nil))
  17191. (while (and (string-match rexp string
  17192. (if (and notfirst
  17193. (= start (match-beginning 0))
  17194. (< start (length string)))
  17195. (1+ start) start))
  17196. (< (match-beginning 0) (length string)))
  17197. (setq notfirst t)
  17198. (or (eq (match-beginning 0) 0)
  17199. (and (eq (match-beginning 0) (match-end 0))
  17200. (eq (match-beginning 0) start))
  17201. (setq list
  17202. (cons (substring string start (match-beginning 0))
  17203. list)))
  17204. (setq start (match-end 0)))
  17205. (or (eq start (length string))
  17206. (setq list
  17207. (cons (substring string start)
  17208. list)))
  17209. (nreverse list)))
  17210. (defun org-quote-vert (s)
  17211. "Replace \"|\" with \"\\vert\"."
  17212. (while (string-match "|" s)
  17213. (setq s (replace-match "\\vert" t t s)))
  17214. s)
  17215. (defun org-uuidgen-p (s)
  17216. "Is S an ID created by UUIDGEN?"
  17217. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  17218. (defun org-context ()
  17219. "Return a list of contexts of the current cursor position.
  17220. If several contexts apply, all are returned.
  17221. Each context entry is a list with a symbol naming the context, and
  17222. two positions indicating start and end of the context. Possible
  17223. contexts are:
  17224. :headline anywhere in a headline
  17225. :headline-stars on the leading stars in a headline
  17226. :todo-keyword on a TODO keyword (including DONE) in a headline
  17227. :tags on the TAGS in a headline
  17228. :priority on the priority cookie in a headline
  17229. :item on the first line of a plain list item
  17230. :item-bullet on the bullet/number of a plain list item
  17231. :checkbox on the checkbox in a plain list item
  17232. :table in an org-mode table
  17233. :table-special on a special filed in a table
  17234. :table-table in a table.el table
  17235. :link on a hyperlink
  17236. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  17237. :target on a <<target>>
  17238. :radio-target on a <<<radio-target>>>
  17239. :latex-fragment on a LaTeX fragment
  17240. :latex-preview on a LaTeX fragment with overlayed preview image
  17241. This function expects the position to be visible because it uses font-lock
  17242. faces as a help to recognize the following contexts: :table-special, :link,
  17243. and :keyword."
  17244. (let* ((f (get-text-property (point) 'face))
  17245. (faces (if (listp f) f (list f)))
  17246. (p (point)) clist o)
  17247. ;; First the large context
  17248. (cond
  17249. ((org-on-heading-p t)
  17250. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  17251. (when (progn
  17252. (beginning-of-line 1)
  17253. (looking-at org-todo-line-tags-regexp))
  17254. (push (org-point-in-group p 1 :headline-stars) clist)
  17255. (push (org-point-in-group p 2 :todo-keyword) clist)
  17256. (push (org-point-in-group p 4 :tags) clist))
  17257. (goto-char p)
  17258. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  17259. (if (looking-at "\\[#[A-Z0-9]\\]")
  17260. (push (org-point-in-group p 0 :priority) clist)))
  17261. ((org-at-item-p)
  17262. (push (org-point-in-group p 2 :item-bullet) clist)
  17263. (push (list :item (point-at-bol)
  17264. (save-excursion (org-end-of-item) (point)))
  17265. clist)
  17266. (and (org-at-item-checkbox-p)
  17267. (push (org-point-in-group p 0 :checkbox) clist)))
  17268. ((org-at-table-p)
  17269. (push (list :table (org-table-begin) (org-table-end)) clist)
  17270. (if (memq 'org-formula faces)
  17271. (push (list :table-special
  17272. (previous-single-property-change p 'face)
  17273. (next-single-property-change p 'face)) clist)))
  17274. ((org-at-table-p 'any)
  17275. (push (list :table-table) clist)))
  17276. (goto-char p)
  17277. ;; Now the small context
  17278. (cond
  17279. ((org-at-timestamp-p)
  17280. (push (org-point-in-group p 0 :timestamp) clist))
  17281. ((memq 'org-link faces)
  17282. (push (list :link
  17283. (previous-single-property-change p 'face)
  17284. (next-single-property-change p 'face)) clist))
  17285. ((memq 'org-special-keyword faces)
  17286. (push (list :keyword
  17287. (previous-single-property-change p 'face)
  17288. (next-single-property-change p 'face)) clist))
  17289. ((org-on-target-p)
  17290. (push (org-point-in-group p 0 :target) clist)
  17291. (goto-char (1- (match-beginning 0)))
  17292. (if (looking-at org-radio-target-regexp)
  17293. (push (org-point-in-group p 0 :radio-target) clist))
  17294. (goto-char p))
  17295. ((setq o (car (delq nil
  17296. (mapcar
  17297. (lambda (x)
  17298. (if (memq x org-latex-fragment-image-overlays) x))
  17299. (overlays-at (point))))))
  17300. (push (list :latex-fragment
  17301. (overlay-start o) (overlay-end o)) clist)
  17302. (push (list :latex-preview
  17303. (overlay-start o) (overlay-end o)) clist))
  17304. ((org-inside-LaTeX-fragment-p)
  17305. ;; FIXME: positions wrong.
  17306. (push (list :latex-fragment (point) (point)) clist)))
  17307. (setq clist (nreverse (delq nil clist)))
  17308. clist))
  17309. ;; FIXME: Compare with at-regexp-p Do we need both?
  17310. (defun org-in-regexp (re &optional nlines visually)
  17311. "Check if point is inside a match of regexp.
  17312. Normally only the current line is checked, but you can include NLINES extra
  17313. lines both before and after point into the search.
  17314. If VISUALLY is set, require that the cursor is not after the match but
  17315. really on, so that the block visually is on the match."
  17316. (catch 'exit
  17317. (let ((pos (point))
  17318. (eol (point-at-eol (+ 1 (or nlines 0))))
  17319. (inc (if visually 1 0)))
  17320. (save-excursion
  17321. (beginning-of-line (- 1 (or nlines 0)))
  17322. (while (re-search-forward re eol t)
  17323. (if (and (<= (match-beginning 0) pos)
  17324. (>= (+ inc (match-end 0)) pos))
  17325. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  17326. (defun org-at-regexp-p (regexp)
  17327. "Is point inside a match of REGEXP in the current line?"
  17328. (catch 'exit
  17329. (save-excursion
  17330. (let ((pos (point)) (end (point-at-eol)))
  17331. (beginning-of-line 1)
  17332. (while (re-search-forward regexp end t)
  17333. (if (and (<= (match-beginning 0) pos)
  17334. (>= (match-end 0) pos))
  17335. (throw 'exit t)))
  17336. nil))))
  17337. (defun org-in-regexps-block-p (start-re end-re &optional bound)
  17338. "Return t if the current point is between matches of START-RE and END-RE.
  17339. This will also return t if point is on one of the two matches or
  17340. in an unfinished block. END-RE can be a string or a form
  17341. returning a string.
  17342. An optional third argument bounds the search for START-RE. It
  17343. defaults to previous heading or `point-min'."
  17344. (let ((pos (point))
  17345. (limit (or bound (save-excursion (outline-previous-heading)))))
  17346. (save-excursion
  17347. ;; we're on a block when point is on start-re...
  17348. (or (org-at-regexp-p start-re)
  17349. ;; ... or start-re can be found above...
  17350. (and (re-search-backward start-re limit t)
  17351. ;; ... but no end-re between start-re and point.
  17352. (not (re-search-forward (eval end-re) pos t)))))))
  17353. (defun org-occur-in-agenda-files (regexp &optional nlines)
  17354. "Call `multi-occur' with buffers for all agenda files."
  17355. (interactive "sOrg-files matching: \np")
  17356. (let* ((files (org-agenda-files))
  17357. (tnames (mapcar 'file-truename files))
  17358. (extra org-agenda-text-search-extra-files)
  17359. f)
  17360. (when (eq (car extra) 'agenda-archives)
  17361. (setq extra (cdr extra))
  17362. (setq files (org-add-archive-files files)))
  17363. (while (setq f (pop extra))
  17364. (unless (member (file-truename f) tnames)
  17365. (add-to-list 'files f 'append)
  17366. (add-to-list 'tnames (file-truename f) 'append)))
  17367. (multi-occur
  17368. (mapcar (lambda (x)
  17369. (with-current-buffer
  17370. (or (get-file-buffer x) (find-file-noselect x))
  17371. (widen)
  17372. (current-buffer)))
  17373. files)
  17374. regexp)))
  17375. (if (boundp 'occur-mode-find-occurrence-hook)
  17376. ;; Emacs 23
  17377. (add-hook 'occur-mode-find-occurrence-hook
  17378. (lambda ()
  17379. (when (org-mode-p)
  17380. (org-reveal))))
  17381. ;; Emacs 22
  17382. (defadvice occur-mode-goto-occurrence
  17383. (after org-occur-reveal activate)
  17384. (and (org-mode-p) (org-reveal)))
  17385. (defadvice occur-mode-goto-occurrence-other-window
  17386. (after org-occur-reveal activate)
  17387. (and (org-mode-p) (org-reveal)))
  17388. (defadvice occur-mode-display-occurrence
  17389. (after org-occur-reveal activate)
  17390. (when (org-mode-p)
  17391. (let ((pos (occur-mode-find-occurrence)))
  17392. (with-current-buffer (marker-buffer pos)
  17393. (save-excursion
  17394. (goto-char pos)
  17395. (org-reveal)))))))
  17396. (defun org-occur-link-in-agenda-files ()
  17397. "Create a link and search for it in the agendas.
  17398. The link is not stored in `org-stored-links', it is just created
  17399. for the search purpose."
  17400. (interactive)
  17401. (let ((link (condition-case nil
  17402. (org-store-link nil)
  17403. (error "Unable to create a link to here"))))
  17404. (org-occur-in-agenda-files (regexp-quote link))))
  17405. (defun org-uniquify (list)
  17406. "Remove duplicate elements from LIST."
  17407. (let (res)
  17408. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  17409. res))
  17410. (defun org-delete-all (elts list)
  17411. "Remove all elements in ELTS from LIST."
  17412. (while elts
  17413. (setq list (delete (pop elts) list)))
  17414. list)
  17415. (defun org-count (cl-item cl-seq)
  17416. "Count the number of occurrences of ITEM in SEQ.
  17417. Taken from `count' in cl-seq.el with all keyword arguments removed."
  17418. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  17419. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  17420. (while (< cl-start cl-end)
  17421. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  17422. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  17423. (setq cl-start (1+ cl-start)))
  17424. cl-count))
  17425. (defun org-remove-if (predicate seq)
  17426. "Remove everything from SEQ that fulfills PREDICATE."
  17427. (let (res e)
  17428. (while seq
  17429. (setq e (pop seq))
  17430. (if (not (funcall predicate e)) (push e res)))
  17431. (nreverse res)))
  17432. (defun org-remove-if-not (predicate seq)
  17433. "Remove everything from SEQ that does not fulfill PREDICATE."
  17434. (let (res e)
  17435. (while seq
  17436. (setq e (pop seq))
  17437. (if (funcall predicate e) (push e res)))
  17438. (nreverse res)))
  17439. (defun org-back-over-empty-lines ()
  17440. "Move backwards over whitespace, to the beginning of the first empty line.
  17441. Returns the number of empty lines passed."
  17442. (let ((pos (point)))
  17443. (if (cdr (assoc 'heading org-blank-before-new-entry))
  17444. (skip-chars-backward " \t\n\r")
  17445. (forward-line -1))
  17446. (beginning-of-line 2)
  17447. (goto-char (min (point) pos))
  17448. (count-lines (point) pos)))
  17449. (defun org-skip-whitespace ()
  17450. (skip-chars-forward " \t\n\r"))
  17451. (defun org-point-in-group (point group &optional context)
  17452. "Check if POINT is in match-group GROUP.
  17453. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  17454. match. If the match group does not exist or point is not inside it,
  17455. return nil."
  17456. (and (match-beginning group)
  17457. (>= point (match-beginning group))
  17458. (<= point (match-end group))
  17459. (if context
  17460. (list context (match-beginning group) (match-end group))
  17461. t)))
  17462. (defun org-switch-to-buffer-other-window (&rest args)
  17463. "Switch to buffer in a second window on the current frame.
  17464. In particular, do not allow pop-up frames.
  17465. Returns the newly created buffer."
  17466. (let (pop-up-frames special-display-buffer-names special-display-regexps
  17467. special-display-function)
  17468. (apply 'switch-to-buffer-other-window args)))
  17469. (defun org-combine-plists (&rest plists)
  17470. "Create a single property list from all plists in PLISTS.
  17471. The process starts by copying the first list, and then setting properties
  17472. from the other lists. Settings in the last list are the most significant
  17473. ones and overrule settings in the other lists."
  17474. (let ((rtn (copy-sequence (pop plists)))
  17475. p v ls)
  17476. (while plists
  17477. (setq ls (pop plists))
  17478. (while ls
  17479. (setq p (pop ls) v (pop ls))
  17480. (setq rtn (plist-put rtn p v))))
  17481. rtn))
  17482. (defun org-move-line-down (arg)
  17483. "Move the current line down. With prefix argument, move it past ARG lines."
  17484. (interactive "p")
  17485. (let ((col (current-column))
  17486. beg end pos)
  17487. (beginning-of-line 1) (setq beg (point))
  17488. (beginning-of-line 2) (setq end (point))
  17489. (beginning-of-line (+ 1 arg))
  17490. (setq pos (move-marker (make-marker) (point)))
  17491. (insert (delete-and-extract-region beg end))
  17492. (goto-char pos)
  17493. (org-move-to-column col)))
  17494. (defun org-move-line-up (arg)
  17495. "Move the current line up. With prefix argument, move it past ARG lines."
  17496. (interactive "p")
  17497. (let ((col (current-column))
  17498. beg end pos)
  17499. (beginning-of-line 1) (setq beg (point))
  17500. (beginning-of-line 2) (setq end (point))
  17501. (beginning-of-line (- arg))
  17502. (setq pos (move-marker (make-marker) (point)))
  17503. (insert (delete-and-extract-region beg end))
  17504. (goto-char pos)
  17505. (org-move-to-column col)))
  17506. (defun org-replace-escapes (string table)
  17507. "Replace %-escapes in STRING with values in TABLE.
  17508. TABLE is an association list with keys like \"%a\" and string values.
  17509. The sequences in STRING may contain normal field width and padding information,
  17510. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  17511. so values can contain further %-escapes if they are define later in TABLE."
  17512. (let ((tbl (copy-alist table))
  17513. (case-fold-search nil)
  17514. (pchg 0)
  17515. e re rpl)
  17516. (while (setq e (pop tbl))
  17517. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  17518. (when (and (cdr e) (string-match re (cdr e)))
  17519. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  17520. (safe "SREF"))
  17521. (add-text-properties 0 3 (list 'sref sref) safe)
  17522. (setcdr e (replace-match safe t t (cdr e)))))
  17523. (while (string-match re string)
  17524. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  17525. (cdr e)))
  17526. (setq string (replace-match rpl t t string))))
  17527. (while (setq pchg (next-property-change pchg string))
  17528. (let ((sref (get-text-property pchg 'sref string)))
  17529. (when (and sref (string-match "SREF" string pchg))
  17530. (setq string (replace-match sref t t string)))))
  17531. string))
  17532. (defun org-sublist (list start end)
  17533. "Return a section of LIST, from START to END.
  17534. Counting starts at 1."
  17535. (let (rtn (c start))
  17536. (setq list (nthcdr (1- start) list))
  17537. (while (and list (<= c end))
  17538. (push (pop list) rtn)
  17539. (setq c (1+ c)))
  17540. (nreverse rtn)))
  17541. (defun org-find-base-buffer-visiting (file)
  17542. "Like `find-buffer-visiting' but always return the base buffer and
  17543. not an indirect buffer."
  17544. (let ((buf (or (get-file-buffer file)
  17545. (find-buffer-visiting file))))
  17546. (if buf
  17547. (or (buffer-base-buffer buf) buf)
  17548. nil)))
  17549. (defun org-image-file-name-regexp (&optional extensions)
  17550. "Return regexp matching the file names of images.
  17551. If EXTENSIONS is given, only match these."
  17552. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  17553. (image-file-name-regexp)
  17554. (let ((image-file-name-extensions
  17555. (or extensions
  17556. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  17557. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  17558. (concat "\\."
  17559. (regexp-opt (nconc (mapcar 'upcase
  17560. image-file-name-extensions)
  17561. image-file-name-extensions)
  17562. t)
  17563. "\\'"))))
  17564. (defun org-file-image-p (file &optional extensions)
  17565. "Return non-nil if FILE is an image."
  17566. (save-match-data
  17567. (string-match (org-image-file-name-regexp extensions) file)))
  17568. (defun org-get-cursor-date ()
  17569. "Return the date at cursor in as a time.
  17570. This works in the calendar and in the agenda, anywhere else it just
  17571. returns the current time."
  17572. (let (date day defd)
  17573. (cond
  17574. ((eq major-mode 'calendar-mode)
  17575. (setq date (calendar-cursor-to-date)
  17576. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  17577. ((eq major-mode 'org-agenda-mode)
  17578. (setq day (get-text-property (point) 'day))
  17579. (if day
  17580. (setq date (calendar-gregorian-from-absolute day)
  17581. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  17582. (nth 2 date))))))
  17583. (or defd (current-time))))
  17584. (defvar org-agenda-action-marker (make-marker)
  17585. "Marker pointing to the entry for the next agenda action.")
  17586. (defun org-mark-entry-for-agenda-action ()
  17587. "Mark the current entry as target of an agenda action.
  17588. Agenda actions are actions executed from the agenda with the key `k',
  17589. which make use of the date at the cursor."
  17590. (interactive)
  17591. (move-marker org-agenda-action-marker
  17592. (save-excursion (org-back-to-heading t) (point))
  17593. (current-buffer))
  17594. (message
  17595. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  17596. (defun org-mark-subtree ()
  17597. "Mark the current subtree.
  17598. This puts point at the start of the current subtree, and mark at the end.
  17599. If point is in an inline task, mark that task instead."
  17600. (interactive)
  17601. (let ((inline-task-p
  17602. (and (featurep 'org-inlinetask)
  17603. (org-inlinetask-in-task-p)))
  17604. (beg))
  17605. ;; Get beginning of subtree
  17606. (cond
  17607. (inline-task-p (org-inlinetask-goto-beginning))
  17608. ((org-at-heading-p) (beginning-of-line))
  17609. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  17610. (setq beg (point))
  17611. ;; Get end of it
  17612. (if inline-task-p
  17613. (org-inlinetask-goto-end)
  17614. (org-end-of-subtree))
  17615. ;; Mark zone
  17616. (push-mark (point) nil t)
  17617. (goto-char beg)))
  17618. ;;; Paragraph filling stuff.
  17619. ;; We want this to be just right, so use the full arsenal.
  17620. (defun org-indent-line-function ()
  17621. "Indent line depending on context."
  17622. (interactive)
  17623. (let* ((pos (point))
  17624. (itemp (org-at-item-p))
  17625. (case-fold-search t)
  17626. (org-drawer-regexp (or org-drawer-regexp "\000"))
  17627. (inline-task-p (and (featurep 'org-inlinetask)
  17628. (org-inlinetask-in-task-p)))
  17629. (inline-re (and inline-task-p
  17630. (org-inlinetask-outline-regexp)))
  17631. column)
  17632. (beginning-of-line 1)
  17633. (cond
  17634. ;; Comments
  17635. ((looking-at "# ") (setq column 0))
  17636. ;; Headings
  17637. ((looking-at "\\*+ ") (setq column 0))
  17638. ;; Included files
  17639. ((looking-at "#\\+include:") (setq column 0))
  17640. ;; Footnote definition
  17641. ((looking-at org-footnote-definition-re) (setq column 0))
  17642. ;; Literal examples
  17643. ((looking-at "[ \t]*:[ \t]")
  17644. (setq column (org-get-indentation))) ; do nothing
  17645. ;; Lists
  17646. ((ignore-errors (goto-char (org-in-item-p)))
  17647. (setq column (if itemp
  17648. (org-get-indentation)
  17649. (org-list-item-body-column (point))))
  17650. (goto-char pos))
  17651. ;; Drawers
  17652. ((and (looking-at "[ \t]*:END:")
  17653. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  17654. (save-excursion
  17655. (goto-char (1- (match-beginning 1)))
  17656. (setq column (current-column))))
  17657. ;; Special blocks
  17658. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  17659. (save-excursion
  17660. (re-search-backward
  17661. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  17662. (setq column (org-get-indentation (match-string 0))))
  17663. ((and (not (looking-at "[ \t]*#\\+begin_"))
  17664. (org-in-regexps-block-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  17665. (save-excursion
  17666. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  17667. (setq column
  17668. (if (equal (downcase (match-string 1)) "src")
  17669. ;; src blocks: let `org-edit-src-exit' handle them
  17670. (org-get-indentation)
  17671. (org-get-indentation (match-string 0)))))
  17672. ;; This line has nothing special, look at the previous relevant
  17673. ;; line to compute indentation
  17674. (t
  17675. (beginning-of-line 0)
  17676. (while (and (not (bobp))
  17677. (not (looking-at org-drawer-regexp))
  17678. ;; When point started in an inline task, do not move
  17679. ;; above task starting line.
  17680. (not (and inline-task-p (looking-at inline-re)))
  17681. ;; Skip drawers, blocks, empty lines, verbatim,
  17682. ;; comments, tables, footnotes definitions, lists,
  17683. ;; inline tasks.
  17684. (or (and (looking-at "[ \t]*:END:")
  17685. (re-search-backward org-drawer-regexp nil t))
  17686. (and (looking-at "[ \t]*#\\+end_")
  17687. (re-search-backward "[ \t]*#\\+begin_"nil t))
  17688. (looking-at "[ \t]*[\n:#|]")
  17689. (looking-at org-footnote-definition-re)
  17690. (and (ignore-errors (goto-char (org-in-item-p)))
  17691. (goto-char
  17692. (org-list-get-top-point (org-list-struct))))
  17693. (and (not inline-task-p)
  17694. (featurep 'org-inlinetask)
  17695. (org-inlinetask-in-task-p)
  17696. (or (org-inlinetask-goto-beginning) t))))
  17697. (beginning-of-line 0))
  17698. (cond
  17699. ;; There was an heading above.
  17700. ((looking-at "\\*+[ \t]+")
  17701. (if (not org-adapt-indentation)
  17702. (setq column 0)
  17703. (goto-char (match-end 0))
  17704. (setq column (current-column))))
  17705. ;; A drawer had started and is unfinished
  17706. ((looking-at org-drawer-regexp)
  17707. (goto-char (1- (match-beginning 1)))
  17708. (setq column (current-column)))
  17709. ;; Else, nothing noticeable found: get indentation and go on.
  17710. (t (setq column (org-get-indentation))))))
  17711. ;; Now apply indentation and move cursor accordingly
  17712. (goto-char pos)
  17713. (if (<= (current-column) (current-indentation))
  17714. (org-indent-line-to column)
  17715. (save-excursion (org-indent-line-to column)))
  17716. ;; Special polishing for properties, see `org-property-format'
  17717. (setq column (current-column))
  17718. (beginning-of-line 1)
  17719. (if (looking-at
  17720. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  17721. (replace-match (concat (match-string 1)
  17722. (format org-property-format
  17723. (match-string 2) (match-string 3)))
  17724. t t))
  17725. (org-move-to-column column)))
  17726. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  17727. "Variable to store copy of `adaptive-fill-regexp'.
  17728. Since `adaptive-fill-regexp' is set to never match, we need to
  17729. store a backup of its value before entering `org-mode' so that
  17730. the functionality can be provided as a fall-back.")
  17731. (defun org-set-autofill-regexps ()
  17732. (interactive)
  17733. ;; In the paragraph separator we include headlines, because filling
  17734. ;; text in a line directly attached to a headline would otherwise
  17735. ;; fill the headline as well.
  17736. (org-set-local 'comment-start-skip "^#+[ \t]*")
  17737. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  17738. ;; The paragraph starter includes hand-formatted lists.
  17739. (org-set-local
  17740. 'paragraph-start
  17741. (concat
  17742. "\f" "\\|"
  17743. "[ ]*$" "\\|"
  17744. "\\*+ " "\\|"
  17745. "[ \t]*#" "\\|"
  17746. (org-item-re) "\\|"
  17747. "[ \t]*[:|]" "\\|"
  17748. "\\$\\$" "\\|"
  17749. "\\\\\\(begin\\|end\\|[][]\\)"))
  17750. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  17751. ;; But only if the user has not turned off tables or fixed-width regions
  17752. (org-set-local
  17753. 'auto-fill-inhibit-regexp
  17754. (concat "\\*+ \\|#\\+"
  17755. "\\|[ \t]*" org-keyword-time-regexp
  17756. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  17757. (concat
  17758. "\\|[ \t]*["
  17759. (if org-enable-table-editor "|" "")
  17760. (if org-enable-fixed-width-editor ":" "")
  17761. "]"))))
  17762. ;; We use our own fill-paragraph function, to make sure that tables
  17763. ;; and fixed-width regions are not wrapped. That function will pass
  17764. ;; through to `fill-paragraph' when appropriate.
  17765. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  17766. ;; Prevent auto-fill from inserting unwanted new items.
  17767. (org-set-local 'fill-nobreak-predicate
  17768. (if (memq 'org-fill-item-nobreak-p fill-nobreak-predicate)
  17769. fill-nobreak-predicate
  17770. (cons 'org-fill-item-nobreak-p fill-nobreak-predicate)))
  17771. ;; Adaptive filling: To get full control, first make sure that
  17772. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  17773. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  17774. (org-set-local 'org-adaptive-fill-regexp-backup
  17775. adaptive-fill-regexp))
  17776. (org-set-local 'adaptive-fill-regexp "\000")
  17777. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  17778. (org-set-local 'adaptive-fill-function
  17779. 'org-adaptive-fill-function)
  17780. (org-set-local
  17781. 'align-mode-rules-list
  17782. '((org-in-buffer-settings
  17783. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  17784. (modes . '(org-mode))))))
  17785. (defun org-fill-item-nobreak-p ()
  17786. "Non-nil when a line break at point would insert a new item."
  17787. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  17788. (defun org-fill-paragraph (&optional justify)
  17789. "Re-align a table, pass through to fill-paragraph if no table."
  17790. (let ((table-p (org-at-table-p))
  17791. (table.el-p (org-at-table.el-p))
  17792. (itemp (org-in-item-p)))
  17793. (cond ((and (equal (char-after (point-at-bol)) ?*)
  17794. (save-excursion (goto-char (point-at-bol))
  17795. (looking-at outline-regexp)))
  17796. t) ; skip headlines
  17797. (table.el-p t) ; skip table.el tables
  17798. (table-p (org-table-align) t) ; align Org tables
  17799. (itemp ; align text in items
  17800. (let* ((struct (save-excursion (goto-char itemp)
  17801. (org-list-struct)))
  17802. (parents (org-list-parents-alist struct))
  17803. (children (org-list-get-children itemp struct parents))
  17804. beg end prev next prefix)
  17805. ;; Determine in which part of item point is: before
  17806. ;; first child, after last child, between two
  17807. ;; sub-lists, or simply in item if there's no child.
  17808. (cond
  17809. ((not children)
  17810. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  17811. beg itemp
  17812. end (org-list-get-item-end itemp struct)))
  17813. ((< (point) (setq next (car children)))
  17814. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  17815. beg itemp
  17816. end next))
  17817. ((> (point) (setq prev (car (last children))))
  17818. (setq beg (org-list-get-item-end prev struct)
  17819. end (org-list-get-item-end itemp struct)
  17820. prefix (save-excursion
  17821. (goto-char beg)
  17822. (skip-chars-forward " \t")
  17823. (make-string (current-column) ?\ ))))
  17824. (t (catch 'exit
  17825. (while (setq next (pop children))
  17826. (if (> (point) next)
  17827. (setq prev next)
  17828. (setq beg (org-list-get-item-end prev struct)
  17829. end next
  17830. prefix (save-excursion
  17831. (goto-char beg)
  17832. (skip-chars-forward " \t")
  17833. (make-string (current-column) ?\ )))
  17834. (throw 'exit nil))))))
  17835. ;; Use `fill-paragraph' with buffer narrowed to item
  17836. ;; without any child, and with our computed PREFIX.
  17837. (flet ((fill-context-prefix (from to &optional flr) prefix))
  17838. (save-restriction
  17839. (narrow-to-region beg end)
  17840. (save-excursion (fill-paragraph justify)))) t))
  17841. ;; Special case where point is not in a list but is on
  17842. ;; a paragraph adjacent to a list: make sure this paragraph
  17843. ;; doesn't get merged with the end of the list by narrowing
  17844. ;; buffer first.
  17845. ((save-excursion (forward-paragraph -1)
  17846. (setq itemp (org-in-item-p)))
  17847. (let ((struct (save-excursion (goto-char itemp)
  17848. (org-list-struct))))
  17849. (save-restriction
  17850. (narrow-to-region (org-list-get-bottom-point struct)
  17851. (save-excursion (forward-paragraph 1)
  17852. (point)))
  17853. (fill-paragraph justify) t)))
  17854. ;; Else simply call `fill-paragraph'.
  17855. (t nil))))
  17856. ;; For reference, this is the default value of adaptive-fill-regexp
  17857. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  17858. (defun org-adaptive-fill-function ()
  17859. "Return a fill prefix for org-mode files."
  17860. (let (itemp)
  17861. (save-excursion
  17862. (cond
  17863. ;; Comment line
  17864. ((looking-at "#[ \t]+")
  17865. (match-string-no-properties 0))
  17866. ;; Plain list item
  17867. ((org-at-item-p)
  17868. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  17869. ;; Point is in a list after `backward-paragraph': original
  17870. ;; point wasn't in the list, or filling would have been taken
  17871. ;; care of by `org-auto-fill-function', but the list and the
  17872. ;; real paragraph are not separated by a blank line. Thus, move
  17873. ;; point after the list to go back to real paragraph and
  17874. ;; determine fill-prefix.
  17875. ((setq itemp (org-in-item-p))
  17876. (goto-char itemp)
  17877. (let* ((struct (org-list-struct))
  17878. (bottom (org-list-get-bottom-point struct)))
  17879. (goto-char bottom)
  17880. (make-string (org-get-indentation) ?\ )))
  17881. ;; Other text
  17882. ((looking-at org-adaptive-fill-regexp-backup)
  17883. (match-string-no-properties 0))))))
  17884. (defun org-auto-fill-function ()
  17885. "Auto-fill function."
  17886. (let (itemp prefix)
  17887. ;; When in a list, compute an appropriate fill-prefix and make
  17888. ;; sure it will be used by `do-auto-fill'.
  17889. (if (setq itemp (org-in-item-p))
  17890. (progn
  17891. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  17892. (flet ((fill-context-prefix (from to &optional flr) prefix))
  17893. (do-auto-fill)))
  17894. ;; Else just use `do-auto-fill'.
  17895. (do-auto-fill))))
  17896. ;;; Other stuff.
  17897. (defun org-toggle-fixed-width-section (arg)
  17898. "Toggle the fixed-width export.
  17899. If there is no active region, the QUOTE keyword at the current headline is
  17900. inserted or removed. When present, it causes the text between this headline
  17901. and the next to be exported as fixed-width text, and unmodified.
  17902. If there is an active region, this command adds or removes a colon as the
  17903. first character of this line. If the first character of a line is a colon,
  17904. this line is also exported in fixed-width font."
  17905. (interactive "P")
  17906. (let* ((cc 0)
  17907. (regionp (org-region-active-p))
  17908. (beg (if regionp (region-beginning) (point)))
  17909. (end (if regionp (region-end)))
  17910. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  17911. (case-fold-search nil)
  17912. (re "[ \t]*\\(: \\)")
  17913. off)
  17914. (if regionp
  17915. (save-excursion
  17916. (goto-char beg)
  17917. (setq cc (current-column))
  17918. (beginning-of-line 1)
  17919. (setq off (looking-at re))
  17920. (while (> nlines 0)
  17921. (setq nlines (1- nlines))
  17922. (beginning-of-line 1)
  17923. (cond
  17924. (arg
  17925. (org-move-to-column cc t)
  17926. (insert ": \n")
  17927. (forward-line -1))
  17928. ((and off (looking-at re))
  17929. (replace-match "" t t nil 1))
  17930. ((not off) (org-move-to-column cc t) (insert ": ")))
  17931. (forward-line 1)))
  17932. (save-excursion
  17933. (org-back-to-heading)
  17934. (if (looking-at (concat outline-regexp
  17935. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  17936. (replace-match "" t t nil 1)
  17937. (if (looking-at outline-regexp)
  17938. (progn
  17939. (goto-char (match-end 0))
  17940. (insert org-quote-string " "))))))))
  17941. (defun org-reftex-citation ()
  17942. "Use reftex-citation to insert a citation into the buffer.
  17943. This looks for a line like
  17944. #+BIBLIOGRAPHY: foo plain option:-d
  17945. and derives from it that foo.bib is the bibliography file relevant
  17946. for this document. It then installs the necessary environment for RefTeX
  17947. to work in this buffer and calls `reftex-citation' to insert a citation
  17948. into the buffer.
  17949. Export of such citations to both LaTeX and HTML is handled by the contributed
  17950. package org-exp-bibtex by Taru Karttunen."
  17951. (interactive)
  17952. (let ((reftex-docstruct-symbol 'rds)
  17953. (reftex-cite-format "\\cite{%l}")
  17954. rds bib)
  17955. (save-excursion
  17956. (save-restriction
  17957. (widen)
  17958. (let ((case-fold-search t)
  17959. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  17960. (if (not (save-excursion
  17961. (or (re-search-forward re nil t)
  17962. (re-search-backward re nil t))))
  17963. (error "No bibliography defined in file")
  17964. (setq bib (concat (match-string 1) ".bib")
  17965. rds (list (list 'bib bib)))))))
  17966. (call-interactively 'reftex-citation)))
  17967. ;;;; Functions extending outline functionality
  17968. (defun org-beginning-of-line (&optional arg)
  17969. "Go to the beginning of the current line. If that is invisible, continue
  17970. to a visible line beginning. This makes the function of C-a more intuitive.
  17971. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17972. first attempt, and only move to after the tags when the cursor is already
  17973. beyond the end of the headline."
  17974. (interactive "P")
  17975. (let ((pos (point))
  17976. (special (if (consp org-special-ctrl-a/e)
  17977. (car org-special-ctrl-a/e)
  17978. org-special-ctrl-a/e))
  17979. refpos)
  17980. (if (org-bound-and-true-p line-move-visual)
  17981. (beginning-of-visual-line 1)
  17982. (beginning-of-line 1))
  17983. (if (and arg (fboundp 'move-beginning-of-line))
  17984. (call-interactively 'move-beginning-of-line)
  17985. (if (bobp)
  17986. nil
  17987. (backward-char 1)
  17988. (if (org-truely-invisible-p)
  17989. (while (and (not (bobp)) (org-truely-invisible-p))
  17990. (backward-char 1)
  17991. (beginning-of-line 1))
  17992. (forward-char 1))))
  17993. (when special
  17994. (cond
  17995. ((and (looking-at org-complex-heading-regexp)
  17996. (= (char-after (match-end 1)) ?\ ))
  17997. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  17998. (point-at-eol)))
  17999. (goto-char
  18000. (if (eq special t)
  18001. (cond ((> pos refpos) refpos)
  18002. ((= pos (point)) refpos)
  18003. (t (point)))
  18004. (cond ((> pos (point)) (point))
  18005. ((not (eq last-command this-command)) (point))
  18006. (t refpos)))))
  18007. ((org-at-item-p)
  18008. (goto-char
  18009. (if (eq special t)
  18010. (cond ((> pos (match-end 0)) (match-end 0))
  18011. ((= pos (point)) (match-end 0))
  18012. (t (point)))
  18013. (cond ((> pos (point)) (point))
  18014. ((not (eq last-command this-command)) (point))
  18015. (t (match-end 0))))))))
  18016. (org-no-warnings
  18017. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18018. (defun org-end-of-line (&optional arg)
  18019. "Go to the end of the line.
  18020. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  18021. first attempt, and only move to after the tags when the cursor is already
  18022. beyond the end of the headline."
  18023. (interactive "P")
  18024. (let ((special (if (consp org-special-ctrl-a/e)
  18025. (cdr org-special-ctrl-a/e)
  18026. org-special-ctrl-a/e)))
  18027. (if (or (not special)
  18028. (not (org-on-heading-p))
  18029. arg)
  18030. (call-interactively
  18031. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  18032. ((fboundp 'move-end-of-line) 'move-end-of-line)
  18033. (t 'end-of-line)))
  18034. (let ((pos (point)))
  18035. (beginning-of-line 1)
  18036. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  18037. (if (eq special t)
  18038. (if (or (< pos (match-beginning 1))
  18039. (= pos (match-end 0)))
  18040. (goto-char (match-beginning 1))
  18041. (goto-char (match-end 0)))
  18042. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  18043. (goto-char (match-end 0))
  18044. (goto-char (match-beginning 1))))
  18045. (call-interactively (if (fboundp 'move-end-of-line)
  18046. 'move-end-of-line
  18047. 'end-of-line)))))
  18048. (org-no-warnings
  18049. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  18050. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  18051. (define-key org-mode-map "\C-e" 'org-end-of-line)
  18052. (defun org-backward-sentence (&optional arg)
  18053. "Go to beginning of sentence, or beginning of table field.
  18054. This will call `backward-sentence' or `org-table-beginning-of-field',
  18055. depending on context."
  18056. (interactive "P")
  18057. (cond
  18058. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  18059. (t (call-interactively 'backward-sentence))))
  18060. (defun org-forward-sentence (&optional arg)
  18061. "Go to end of sentence, or end of table field.
  18062. This will call `forward-sentence' or `org-table-end-of-field',
  18063. depending on context."
  18064. (interactive "P")
  18065. (cond
  18066. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  18067. (t (call-interactively 'forward-sentence))))
  18068. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  18069. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  18070. (defun org-kill-line (&optional arg)
  18071. "Kill line, to tags or end of line."
  18072. (interactive "P")
  18073. (cond
  18074. ((or (not org-special-ctrl-k)
  18075. (bolp)
  18076. (not (org-on-heading-p)))
  18077. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  18078. org-ctrl-k-protect-subtree)
  18079. (if (or (eq org-ctrl-k-protect-subtree 'error)
  18080. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  18081. (error "C-k aborted - would kill hidden subtree")))
  18082. (call-interactively 'kill-line))
  18083. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  18084. (kill-region (point) (match-beginning 1))
  18085. (org-set-tags nil t))
  18086. (t (kill-region (point) (point-at-eol)))))
  18087. (define-key org-mode-map "\C-k" 'org-kill-line)
  18088. (defun org-yank (&optional arg)
  18089. "Yank. If the kill is a subtree, treat it specially.
  18090. This command will look at the current kill and check if is a single
  18091. subtree, or a series of subtrees[1]. If it passes the test, and if the
  18092. cursor is at the beginning of a line or after the stars of a currently
  18093. empty headline, then the yank is handled specially. How exactly depends
  18094. on the value of the following variables, both set by default.
  18095. org-yank-folded-subtrees
  18096. When set, the subtree(s) will be folded after insertion, but only
  18097. if doing so would now swallow text after the yanked text.
  18098. org-yank-adjusted-subtrees
  18099. When set, the subtree will be promoted or demoted in order to
  18100. fit into the local outline tree structure, which means that the level
  18101. will be adjusted so that it becomes the smaller one of the two
  18102. *visible* surrounding headings.
  18103. Any prefix to this command will cause `yank' to be called directly with
  18104. no special treatment. In particular, a simple \\[universal-argument] prefix \
  18105. will just
  18106. plainly yank the text as it is.
  18107. \[1] The test checks if the first non-white line is a heading
  18108. and if there are no other headings with fewer stars."
  18109. (interactive "P")
  18110. (org-yank-generic 'yank arg))
  18111. (defun org-yank-generic (command arg)
  18112. "Perform some yank-like command.
  18113. This function implements the behavior described in the `org-yank'
  18114. documentation. However, it has been generalized to work for any
  18115. interactive command with similar behavior."
  18116. ;; pretend to be command COMMAND
  18117. (setq this-command command)
  18118. (if arg
  18119. (call-interactively command)
  18120. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  18121. (and (org-kill-is-subtree-p)
  18122. (or (bolp)
  18123. (and (looking-at "[ \t]*$")
  18124. (string-match
  18125. "\\`\\*+\\'"
  18126. (buffer-substring (point-at-bol) (point)))))))
  18127. swallowp)
  18128. (cond
  18129. ((and subtreep org-yank-folded-subtrees)
  18130. (let ((beg (point))
  18131. end)
  18132. (if (and subtreep org-yank-adjusted-subtrees)
  18133. (org-paste-subtree nil nil 'for-yank)
  18134. (call-interactively command))
  18135. (setq end (point))
  18136. (goto-char beg)
  18137. (when (and (bolp) subtreep
  18138. (not (setq swallowp
  18139. (org-yank-folding-would-swallow-text beg end))))
  18140. (or (looking-at outline-regexp)
  18141. (re-search-forward (concat "^" outline-regexp) end t))
  18142. (while (and (< (point) end) (looking-at outline-regexp))
  18143. (hide-subtree)
  18144. (org-cycle-show-empty-lines 'folded)
  18145. (condition-case nil
  18146. (outline-forward-same-level 1)
  18147. (error (goto-char end)))))
  18148. (when swallowp
  18149. (message
  18150. "Inserted text not folded because that would swallow text"))
  18151. (goto-char end)
  18152. (skip-chars-forward " \t\n\r")
  18153. (beginning-of-line 1)
  18154. (push-mark beg 'nomsg)))
  18155. ((and subtreep org-yank-adjusted-subtrees)
  18156. (let ((beg (point-at-bol)))
  18157. (org-paste-subtree nil nil 'for-yank)
  18158. (push-mark beg 'nomsg)))
  18159. (t
  18160. (call-interactively command))))))
  18161. (defun org-yank-folding-would-swallow-text (beg end)
  18162. "Would hide-subtree at BEG swallow any text after END?"
  18163. (let (level)
  18164. (save-excursion
  18165. (goto-char beg)
  18166. (when (or (looking-at outline-regexp)
  18167. (re-search-forward (concat "^" outline-regexp) end t))
  18168. (setq level (org-outline-level)))
  18169. (goto-char end)
  18170. (skip-chars-forward " \t\r\n\v\f")
  18171. (if (or (eobp)
  18172. (and (bolp) (looking-at org-outline-regexp)
  18173. (<= (org-outline-level) level)))
  18174. nil ; Nothing would be swallowed
  18175. t)))) ; something would swallow
  18176. (define-key org-mode-map "\C-y" 'org-yank)
  18177. (defun org-truely-invisible-p ()
  18178. "Check if point is at a character currently not visible.
  18179. This version does not only check the character property, but also
  18180. `visible-mode'."
  18181. ;; Early versions of noutline don't have `outline-invisible-p'.
  18182. (if (org-bound-and-true-p visible-mode)
  18183. nil
  18184. (outline-invisible-p)))
  18185. (defun org-invisible-p2 ()
  18186. "Check if point is at a character currently not visible."
  18187. (save-excursion
  18188. (if (and (eolp) (not (bobp))) (backward-char 1))
  18189. ;; Early versions of noutline don't have `outline-invisible-p'.
  18190. (outline-invisible-p)))
  18191. (defun org-back-to-heading (&optional invisible-ok)
  18192. "Call `outline-back-to-heading', but provide a better error message."
  18193. (condition-case nil
  18194. (outline-back-to-heading invisible-ok)
  18195. (error (error "Before first headline at position %d in buffer %s"
  18196. (point) (current-buffer)))))
  18197. (defun org-beginning-of-defun ()
  18198. "Go to the beginning of the subtree, i.e. back to the heading."
  18199. (org-back-to-heading))
  18200. (defun org-end-of-defun ()
  18201. "Go to the end of the subtree."
  18202. (org-end-of-subtree nil t))
  18203. (defun org-before-first-heading-p ()
  18204. "Before first heading?"
  18205. (save-excursion
  18206. (end-of-line)
  18207. (null (re-search-backward "^\\*+ " nil t))))
  18208. (defun org-on-heading-p (&optional ignored)
  18209. (outline-on-heading-p t))
  18210. (defun org-at-heading-p (&optional ignored)
  18211. (outline-on-heading-p t))
  18212. (defun org-point-at-end-of-empty-headline ()
  18213. "If point is at the end of an empty headline, return t, else nil.
  18214. If the heading only contains a TODO keyword, it is still still considered
  18215. empty."
  18216. (and (looking-at "[ \t]*$")
  18217. (save-excursion
  18218. (beginning-of-line 1)
  18219. (let ((case-fold-search nil))
  18220. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  18221. "\\)?[ \t]*$"))))))
  18222. (defun org-at-heading-or-item-p ()
  18223. (or (org-on-heading-p) (org-at-item-p)))
  18224. (defun org-on-target-p ()
  18225. (or (org-in-regexp org-radio-target-regexp)
  18226. (org-in-regexp org-target-regexp)))
  18227. (defun org-up-heading-all (arg)
  18228. "Move to the heading line of which the present line is a subheading.
  18229. This function considers both visible and invisible heading lines.
  18230. With argument, move up ARG levels."
  18231. (if (fboundp 'outline-up-heading-all)
  18232. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  18233. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  18234. (defun org-up-heading-safe ()
  18235. "Move to the heading line of which the present line is a subheading.
  18236. This version will not throw an error. It will return the level of the
  18237. headline found, or nil if no higher level is found.
  18238. Also, this function will be a lot faster than `outline-up-heading',
  18239. because it relies on stars being the outline starters. This can really
  18240. make a significant difference in outlines with very many siblings."
  18241. (let (start-level re)
  18242. (org-back-to-heading t)
  18243. (setq start-level (funcall outline-level))
  18244. (if (equal start-level 1)
  18245. nil
  18246. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  18247. (if (re-search-backward re nil t)
  18248. (funcall outline-level)))))
  18249. (defun org-first-sibling-p ()
  18250. "Is this heading the first child of its parents?"
  18251. (interactive)
  18252. (let ((re (concat "^" outline-regexp))
  18253. level l)
  18254. (unless (org-at-heading-p t)
  18255. (error "Not at a heading"))
  18256. (setq level (funcall outline-level))
  18257. (save-excursion
  18258. (if (not (re-search-backward re nil t))
  18259. t
  18260. (setq l (funcall outline-level))
  18261. (< l level)))))
  18262. (defun org-goto-sibling (&optional previous)
  18263. "Goto the next sibling, even if it is invisible.
  18264. When PREVIOUS is set, go to the previous sibling instead. Returns t
  18265. when a sibling was found. When none is found, return nil and don't
  18266. move point."
  18267. (let ((fun (if previous 're-search-backward 're-search-forward))
  18268. (pos (point))
  18269. (re (concat "^" outline-regexp))
  18270. level l)
  18271. (when (condition-case nil (org-back-to-heading t) (error nil))
  18272. (setq level (funcall outline-level))
  18273. (catch 'exit
  18274. (or previous (forward-char 1))
  18275. (while (funcall fun re nil t)
  18276. (setq l (funcall outline-level))
  18277. (when (< l level) (goto-char pos) (throw 'exit nil))
  18278. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  18279. (goto-char pos)
  18280. nil))))
  18281. (defun org-show-siblings ()
  18282. "Show all siblings of the current headline."
  18283. (save-excursion
  18284. (while (org-goto-sibling) (org-flag-heading nil)))
  18285. (save-excursion
  18286. (while (org-goto-sibling 'previous)
  18287. (org-flag-heading nil))))
  18288. (defun org-goto-first-child ()
  18289. "Goto the first child, even if it is invisible.
  18290. Return t when a child was found. Otherwise don't move point and
  18291. return nil."
  18292. (let (level (pos (point)) (re (concat "^" outline-regexp)))
  18293. (when (condition-case nil (org-back-to-heading t) (error nil))
  18294. (setq level (outline-level))
  18295. (forward-char 1)
  18296. (if (and (re-search-forward re nil t) (> (outline-level) level))
  18297. (progn (goto-char (match-beginning 0)) t)
  18298. (goto-char pos) nil))))
  18299. (defun org-show-hidden-entry ()
  18300. "Show an entry where even the heading is hidden."
  18301. (save-excursion
  18302. (org-show-entry)))
  18303. (defun org-flag-heading (flag &optional entry)
  18304. "Flag the current heading. FLAG non-nil means make invisible.
  18305. When ENTRY is non-nil, show the entire entry."
  18306. (save-excursion
  18307. (org-back-to-heading t)
  18308. ;; Check if we should show the entire entry
  18309. (if entry
  18310. (progn
  18311. (org-show-entry)
  18312. (save-excursion
  18313. (and (outline-next-heading)
  18314. (org-flag-heading nil))))
  18315. (outline-flag-region (max (point-min) (1- (point)))
  18316. (save-excursion (outline-end-of-heading) (point))
  18317. flag))))
  18318. (defun org-get-next-sibling ()
  18319. "Move to next heading of the same level, and return point.
  18320. If there is no such heading, return nil.
  18321. This is like outline-next-sibling, but invisible headings are ok."
  18322. (let ((level (funcall outline-level)))
  18323. (outline-next-heading)
  18324. (while (and (not (eobp)) (> (funcall outline-level) level))
  18325. (outline-next-heading))
  18326. (if (or (eobp) (< (funcall outline-level) level))
  18327. nil
  18328. (point))))
  18329. (defun org-get-last-sibling ()
  18330. "Move to previous heading of the same level, and return point.
  18331. If there is no such heading, return nil."
  18332. (let ((opoint (point))
  18333. (level (funcall outline-level)))
  18334. (outline-previous-heading)
  18335. (when (and (/= (point) opoint) (outline-on-heading-p t))
  18336. (while (and (> (funcall outline-level) level)
  18337. (not (bobp)))
  18338. (outline-previous-heading))
  18339. (if (< (funcall outline-level) level)
  18340. nil
  18341. (point)))))
  18342. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  18343. ;; This contains an exact copy of the original function, but it uses
  18344. ;; `org-back-to-heading', to make it work also in invisible
  18345. ;; trees. And is uses an invisible-OK argument.
  18346. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  18347. ;; Furthermore, when used inside Org, finding the end of a large subtree
  18348. ;; with many children and grandchildren etc, this can be much faster
  18349. ;; than the outline version.
  18350. (org-back-to-heading invisible-OK)
  18351. (let ((first t)
  18352. (level (funcall outline-level)))
  18353. (if (and (org-mode-p) (< level 1000))
  18354. ;; A true heading (not a plain list item), in Org-mode
  18355. ;; This means we can easily find the end by looking
  18356. ;; only for the right number of stars. Using a regexp to do
  18357. ;; this is so much faster than using a Lisp loop.
  18358. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  18359. (forward-char 1)
  18360. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  18361. ;; something else, do it the slow way
  18362. (while (and (not (eobp))
  18363. (or first (> (funcall outline-level) level)))
  18364. (setq first nil)
  18365. (outline-next-heading)))
  18366. (unless to-heading
  18367. (if (memq (preceding-char) '(?\n ?\^M))
  18368. (progn
  18369. ;; Go to end of line before heading
  18370. (forward-char -1)
  18371. (if (memq (preceding-char) '(?\n ?\^M))
  18372. ;; leave blank line before heading
  18373. (forward-char -1))))))
  18374. (point))
  18375. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  18376. "Use Org version in org-mode, for dramatic speed-up."
  18377. (if (org-mode-p)
  18378. (progn
  18379. (org-end-of-subtree nil t)
  18380. (unless (eobp) (backward-char 1)))
  18381. ad-do-it))
  18382. (defun org-end-of-meta-data-and-drawers ()
  18383. "Jump to the first text after meta data and drawers in the current entry.
  18384. This will move over empty lines, lines with planning time stamps,
  18385. clocking lines, and drawers."
  18386. (org-back-to-heading t)
  18387. (let ((end (save-excursion (outline-next-heading) (point)))
  18388. (re (concat "[ \t]*$"
  18389. "\\|" org-drawer-regexp
  18390. "\\|" "[ \t]*" org-keyword-time-regexp)))
  18391. (while (re-search-forward re end 'move)
  18392. (forward-line 1))))
  18393. (defun org-forward-same-level (arg &optional invisible-ok)
  18394. "Move forward to the arg'th subheading at same level as this one.
  18395. Stop at the first and last subheadings of a superior heading.
  18396. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  18397. it wil also look at invisible ones."
  18398. (interactive "p")
  18399. (org-back-to-heading invisible-ok)
  18400. (org-on-heading-p)
  18401. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  18402. (re (format "^\\*\\{1,%d\\} " level))
  18403. l)
  18404. (forward-char 1)
  18405. (while (> arg 0)
  18406. (while (and (re-search-forward re nil 'move)
  18407. (setq l (- (match-end 0) (match-beginning 0) 1))
  18408. (= l level)
  18409. (not invisible-ok)
  18410. (progn (backward-char 1) (outline-invisible-p)))
  18411. (if (< l level) (setq arg 1)))
  18412. (setq arg (1- arg)))
  18413. (beginning-of-line 1)))
  18414. (defun org-backward-same-level (arg &optional invisible-ok)
  18415. "Move backward to the arg'th subheading at same level as this one.
  18416. Stop at the first and last subheadings of a superior heading."
  18417. (interactive "p")
  18418. (org-back-to-heading)
  18419. (org-on-heading-p)
  18420. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  18421. (re (format "^\\*\\{1,%d\\} " level))
  18422. l)
  18423. (while (> arg 0)
  18424. (while (and (re-search-backward re nil 'move)
  18425. (setq l (- (match-end 0) (match-beginning 0) 1))
  18426. (= l level)
  18427. (not invisible-ok)
  18428. (outline-invisible-p))
  18429. (if (< l level) (setq arg 1)))
  18430. (setq arg (1- arg)))))
  18431. (defun org-show-subtree ()
  18432. "Show everything after this heading at deeper levels."
  18433. (outline-flag-region
  18434. (point)
  18435. (save-excursion
  18436. (org-end-of-subtree t t))
  18437. nil))
  18438. (defun org-show-entry ()
  18439. "Show the body directly following this heading.
  18440. Show the heading too, if it is currently invisible."
  18441. (interactive)
  18442. (save-excursion
  18443. (condition-case nil
  18444. (progn
  18445. (org-back-to-heading t)
  18446. (outline-flag-region
  18447. (max (point-min) (1- (point)))
  18448. (save-excursion
  18449. (if (re-search-forward
  18450. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  18451. (match-beginning 1)
  18452. (point-max)))
  18453. nil)
  18454. (org-cycle-hide-drawers 'children))
  18455. (error nil))))
  18456. (defun org-make-options-regexp (kwds &optional extra)
  18457. "Make a regular expression for keyword lines."
  18458. (concat
  18459. "^"
  18460. "#?[ \t]*\\+\\("
  18461. (mapconcat 'regexp-quote kwds "\\|")
  18462. (if extra (concat "\\|" extra))
  18463. "\\):[ \t]*"
  18464. "\\(.*\\)"))
  18465. ;; Make isearch reveal the necessary context
  18466. (defun org-isearch-end ()
  18467. "Reveal context after isearch exits."
  18468. (when isearch-success ; only if search was successful
  18469. (if (featurep 'xemacs)
  18470. ;; Under XEmacs, the hook is run in the correct place,
  18471. ;; we directly show the context.
  18472. (org-show-context 'isearch)
  18473. ;; In Emacs the hook runs *before* restoring the overlays.
  18474. ;; So we have to use a one-time post-command-hook to do this.
  18475. ;; (Emacs 22 has a special variable, see function `org-mode')
  18476. (unless (and (boundp 'isearch-mode-end-hook-quit)
  18477. isearch-mode-end-hook-quit)
  18478. ;; Only when the isearch was not quitted.
  18479. (org-add-hook 'post-command-hook 'org-isearch-post-command
  18480. 'append 'local)))))
  18481. (defun org-isearch-post-command ()
  18482. "Remove self from hook, and show context."
  18483. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  18484. (org-show-context 'isearch))
  18485. ;;;; Integration with and fixes for other packages
  18486. ;;; Imenu support
  18487. (defvar org-imenu-markers nil
  18488. "All markers currently used by Imenu.")
  18489. (make-variable-buffer-local 'org-imenu-markers)
  18490. (defun org-imenu-new-marker (&optional pos)
  18491. "Return a new marker for use by Imenu, and remember the marker."
  18492. (let ((m (make-marker)))
  18493. (move-marker m (or pos (point)))
  18494. (push m org-imenu-markers)
  18495. m))
  18496. (defun org-imenu-get-tree ()
  18497. "Produce the index for Imenu."
  18498. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  18499. (setq org-imenu-markers nil)
  18500. (let* ((n org-imenu-depth)
  18501. (re (concat "^" outline-regexp))
  18502. (subs (make-vector (1+ n) nil))
  18503. (last-level 0)
  18504. m level head)
  18505. (save-excursion
  18506. (save-restriction
  18507. (widen)
  18508. (goto-char (point-max))
  18509. (while (re-search-backward re nil t)
  18510. (setq level (org-reduced-level (funcall outline-level)))
  18511. (when (<= level n)
  18512. (looking-at org-complex-heading-regexp)
  18513. (setq head (org-link-display-format
  18514. (org-match-string-no-properties 4))
  18515. m (org-imenu-new-marker))
  18516. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  18517. (if (>= level last-level)
  18518. (push (cons head m) (aref subs level))
  18519. (push (cons head (aref subs (1+ level))) (aref subs level))
  18520. (loop for i from (1+ level) to n do (aset subs i nil)))
  18521. (setq last-level level)))))
  18522. (aref subs 1)))
  18523. (eval-after-load "imenu"
  18524. '(progn
  18525. (add-hook 'imenu-after-jump-hook
  18526. (lambda ()
  18527. (if (org-mode-p)
  18528. (org-show-context 'org-goto))))))
  18529. (defun org-link-display-format (link)
  18530. "Replace a link with either the description, or the link target
  18531. if no description is present"
  18532. (save-match-data
  18533. (if (string-match org-bracket-link-analytic-regexp link)
  18534. (replace-match (if (match-end 5)
  18535. (match-string 5 link)
  18536. (concat (match-string 1 link)
  18537. (match-string 3 link)))
  18538. nil t link)
  18539. link)))
  18540. ;; Speedbar support
  18541. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  18542. "Overlay marking the agenda restriction line in speedbar.")
  18543. (overlay-put org-speedbar-restriction-lock-overlay
  18544. 'face 'org-agenda-restriction-lock)
  18545. (overlay-put org-speedbar-restriction-lock-overlay
  18546. 'help-echo "Agendas are currently limited to this item.")
  18547. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  18548. (defun org-speedbar-set-agenda-restriction ()
  18549. "Restrict future agenda commands to the location at point in speedbar.
  18550. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  18551. (interactive)
  18552. (require 'org-agenda)
  18553. (let (p m tp np dir txt)
  18554. (cond
  18555. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  18556. 'org-imenu t))
  18557. (setq m (get-text-property p 'org-imenu-marker))
  18558. (with-current-buffer (marker-buffer m)
  18559. (goto-char m)
  18560. (org-agenda-set-restriction-lock 'subtree)))
  18561. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  18562. 'speedbar-function 'speedbar-find-file))
  18563. (setq tp (previous-single-property-change
  18564. (1+ p) 'speedbar-function)
  18565. np (next-single-property-change
  18566. tp 'speedbar-function)
  18567. dir (speedbar-line-directory)
  18568. txt (buffer-substring-no-properties (or tp (point-min))
  18569. (or np (point-max))))
  18570. (with-current-buffer (find-file-noselect
  18571. (let ((default-directory dir))
  18572. (expand-file-name txt)))
  18573. (unless (org-mode-p)
  18574. (error "Cannot restrict to non-Org-mode file"))
  18575. (org-agenda-set-restriction-lock 'file)))
  18576. (t (error "Don't know how to restrict Org-mode's agenda")))
  18577. (move-overlay org-speedbar-restriction-lock-overlay
  18578. (point-at-bol) (point-at-eol))
  18579. (setq current-prefix-arg nil)
  18580. (org-agenda-maybe-redo)))
  18581. (eval-after-load "speedbar"
  18582. '(progn
  18583. (speedbar-add-supported-extension ".org")
  18584. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  18585. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  18586. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  18587. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18588. (add-hook 'speedbar-visiting-tag-hook
  18589. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  18590. ;;; Fixes and Hacks for problems with other packages
  18591. ;; Make flyspell not check words in links, to not mess up our keymap
  18592. (defun org-mode-flyspell-verify ()
  18593. "Don't let flyspell put overlays at active buttons, or on
  18594. {todo,all-time,additional-option-like}-keywords."
  18595. (let ((pos (max (1- (point)) (point-min)))
  18596. (word (thing-at-point 'word)))
  18597. (and (not (get-text-property pos 'keymap))
  18598. (not (get-text-property pos 'org-no-flyspell))
  18599. (not (member word org-todo-keywords-1))
  18600. (not (member word org-all-time-keywords))
  18601. (not (member word org-additional-option-like-keywords)))))
  18602. (defun org-remove-flyspell-overlays-in (beg end)
  18603. "Remove flyspell overlays in region."
  18604. (and (org-bound-and-true-p flyspell-mode)
  18605. (fboundp 'flyspell-delete-region-overlays)
  18606. (flyspell-delete-region-overlays beg end))
  18607. (add-text-properties beg end '(org-no-flyspell t)))
  18608. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  18609. (eval-after-load "bookmark"
  18610. '(if (boundp 'bookmark-after-jump-hook)
  18611. ;; We can use the hook
  18612. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  18613. ;; Hook not available, use advice
  18614. (defadvice bookmark-jump (after org-make-visible activate)
  18615. "Make the position visible."
  18616. (org-bookmark-jump-unhide))))
  18617. ;; Make sure saveplace shows the location if it was hidden
  18618. (eval-after-load "saveplace"
  18619. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  18620. "Make the position visible."
  18621. (org-bookmark-jump-unhide)))
  18622. ;; Make sure ecb shows the location if it was hidden
  18623. (eval-after-load "ecb"
  18624. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  18625. "Make hierarchy visible when jumping into location from ECB tree buffer."
  18626. (if (org-mode-p)
  18627. (org-show-context))))
  18628. (defun org-bookmark-jump-unhide ()
  18629. "Unhide the current position, to show the bookmark location."
  18630. (and (org-mode-p)
  18631. (or (outline-invisible-p)
  18632. (save-excursion (goto-char (max (point-min) (1- (point))))
  18633. (outline-invisible-p)))
  18634. (org-show-context 'bookmark-jump)))
  18635. ;; Make session.el ignore our circular variable
  18636. (eval-after-load "session"
  18637. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  18638. ;;;; Experimental code
  18639. (defun org-closed-in-range ()
  18640. "Sparse tree of items closed in a certain time range.
  18641. Still experimental, may disappear in the future."
  18642. (interactive)
  18643. ;; Get the time interval from the user.
  18644. (let* ((time1 (org-float-time
  18645. (org-read-date nil 'to-time nil "Starting date: ")))
  18646. (time2 (org-float-time
  18647. (org-read-date nil 'to-time nil "End date:")))
  18648. ;; callback function
  18649. (callback (lambda ()
  18650. (let ((time
  18651. (org-float-time
  18652. (apply 'encode-time
  18653. (org-parse-time-string
  18654. (match-string 1))))))
  18655. ;; check if time in interval
  18656. (and (>= time time1) (<= time time2))))))
  18657. ;; make tree, check each match with the callback
  18658. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  18659. ;;;; Finish up
  18660. (provide 'org)
  18661. (run-hooks 'org-load-hook)
  18662. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  18663. ;;; org.el ends here