org.el 845 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. (load "org-loaddefs.el" t t)
  74. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  75. ;; some places -- e.g. see the macro org-with-limited-levels.
  76. ;;
  77. ;; In Org buffers, the value of `outline-regexp' is that of
  78. ;; `org-outline-regexp'. The only function still directly relying on
  79. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  80. ;; job when `orgstruct-mode' is active.
  81. (defvar org-outline-regexp "\\*+ "
  82. "Regexp to match Org headlines.")
  83. (defvar org-outline-regexp-bol "^\\*+ "
  84. "Regexp to match Org headlines.
  85. This is similar to `org-outline-regexp' but additionally makes
  86. sure that we are at the beginning of the line.")
  87. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  88. "Matches an headline, putting stars and text into groups.
  89. Stars are put in group 1 and the trimmed body in group 2.")
  90. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  91. (when (fboundp 'defvaralias)
  92. (unless (boundp 'calendar-view-holidays-initially-flag)
  93. (defvaralias 'calendar-view-holidays-initially-flag
  94. 'view-calendar-holidays-initially))
  95. (unless (boundp 'calendar-view-diary-initially-flag)
  96. (defvaralias 'calendar-view-diary-initially-flag
  97. 'view-diary-entries-initially))
  98. (unless (boundp 'diary-fancy-buffer)
  99. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  100. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  101. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  102. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  103. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  104. (declare-function org-clock-timestamps-up "org-clock" ())
  105. (declare-function org-clock-timestamps-down "org-clock" ())
  106. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  107. (declare-function orgtbl-mode "org-table" (&optional arg))
  108. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  109. (declare-function org-beamer-mode "org-beamer" ())
  110. (declare-function org-table-edit-field "org-table" (arg))
  111. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  112. (declare-function org-id-get-create "org-id" (&optional force))
  113. (declare-function org-id-find-id-file "org-id" (id))
  114. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  115. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  116. (declare-function org-table-align "org-table" ())
  117. (declare-function org-table-paste-rectangle "org-table" ())
  118. (declare-function org-table-maybe-eval-formula "org-table" ())
  119. (declare-function org-table-maybe-recalculate-line "org-table" ())
  120. ;; load languages based on value of `org-babel-load-languages'
  121. (defvar org-babel-load-languages)
  122. ;;;###autoload
  123. (defun org-babel-do-load-languages (sym value)
  124. "Load the languages defined in `org-babel-load-languages'."
  125. (set-default sym value)
  126. (mapc (lambda (pair)
  127. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  128. (if active
  129. (progn
  130. (require (intern (concat "ob-" lang))))
  131. (progn
  132. (funcall 'fmakunbound
  133. (intern (concat "org-babel-execute:" lang)))
  134. (funcall 'fmakunbound
  135. (intern (concat "org-babel-expand-body:" lang)))))))
  136. org-babel-load-languages))
  137. (defcustom org-babel-load-languages '((emacs-lisp . t))
  138. "Languages which can be evaluated in Org-mode buffers.
  139. This list can be used to load support for any of the languages
  140. below, note that each language will depend on a different set of
  141. system executables and/or Emacs modes. When a language is
  142. \"loaded\", then code blocks in that language can be evaluated
  143. with `org-babel-execute-src-block' bound by default to C-c
  144. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  145. be set to remove code block evaluation from the C-c C-c
  146. keybinding. By default only Emacs Lisp (which has no
  147. requirements) is loaded."
  148. :group 'org-babel
  149. :set 'org-babel-do-load-languages
  150. :version "24.1"
  151. :type '(alist :tag "Babel Languages"
  152. :key-type
  153. (choice
  154. (const :tag "Awk" awk)
  155. (const :tag "C" C)
  156. (const :tag "R" R)
  157. (const :tag "Asymptote" asymptote)
  158. (const :tag "Calc" calc)
  159. (const :tag "Clojure" clojure)
  160. (const :tag "CSS" css)
  161. (const :tag "Ditaa" ditaa)
  162. (const :tag "Dot" dot)
  163. (const :tag "Emacs Lisp" emacs-lisp)
  164. (const :tag "Fortran" fortran)
  165. (const :tag "Gnuplot" gnuplot)
  166. (const :tag "Haskell" haskell)
  167. (const :tag "IO" io)
  168. (const :tag "Java" java)
  169. (const :tag "Javascript" js)
  170. (const :tag "LaTeX" latex)
  171. (const :tag "Ledger" ledger)
  172. (const :tag "Lilypond" lilypond)
  173. (const :tag "Lisp" lisp)
  174. (const :tag "Maxima" maxima)
  175. (const :tag "Matlab" matlab)
  176. (const :tag "Mscgen" mscgen)
  177. (const :tag "Ocaml" ocaml)
  178. (const :tag "Octave" octave)
  179. (const :tag "Org" org)
  180. (const :tag "Perl" perl)
  181. (const :tag "Pico Lisp" picolisp)
  182. (const :tag "PlantUML" plantuml)
  183. (const :tag "Python" python)
  184. (const :tag "Ruby" ruby)
  185. (const :tag "Sass" sass)
  186. (const :tag "Scala" scala)
  187. (const :tag "Scheme" scheme)
  188. (const :tag "Screen" screen)
  189. (const :tag "Shell Script" sh)
  190. (const :tag "Shen" shen)
  191. (const :tag "Sql" sql)
  192. (const :tag "Sqlite" sqlite))
  193. :value-type (boolean :tag "Activate" :value t)))
  194. ;;;; Customization variables
  195. (defcustom org-clone-delete-id nil
  196. "Remove ID property of clones of a subtree.
  197. When non-nil, clones of a subtree don't inherit the ID property.
  198. Otherwise they inherit the ID property with a new unique
  199. identifier."
  200. :type 'boolean
  201. :version "24.1"
  202. :group 'org-id)
  203. ;;; Version
  204. (require 'org-compat)
  205. (org-check-version)
  206. ;;;###autoload
  207. (defun org-version (&optional here full message)
  208. "Show the org-mode version in the echo area.
  209. With prefix argument HERE, insert it at point.
  210. When FULL is non-nil, use a verbose version string.
  211. When MESSAGE is non-nil, display a message with the version."
  212. (interactive "P")
  213. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  214. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs.el")))
  215. (org-trash (or
  216. (and (fboundp 'org-release) (fboundp 'org-git-version))
  217. (load (concat org-dir "org-version.el")
  218. 'noerror 'nomessage 'nosuffix)))
  219. (org-version (org-release))
  220. (git-version (org-git-version))
  221. (version (format "Org-mode version %s (%s @ %s)"
  222. org-version
  223. git-version
  224. (if org-install-dir
  225. (if (string= org-dir org-install-dir)
  226. org-install-dir
  227. (concat "mixed installation! " org-install-dir " and " org-dir))
  228. "org-loaddefs.el can not be found!")))
  229. (_version (if full version org-version)))
  230. (if (org-called-interactively-p 'interactive)
  231. (if here
  232. (insert version)
  233. (message version))
  234. (if message (message _version))
  235. _version)))
  236. (defconst org-version (org-version))
  237. ;;; Compatibility constants
  238. ;;; The custom variables
  239. (defgroup org nil
  240. "Outline-based notes management and organizer."
  241. :tag "Org"
  242. :group 'outlines
  243. :group 'calendar)
  244. (defcustom org-mode-hook nil
  245. "Mode hook for Org-mode, run after the mode was turned on."
  246. :group 'org
  247. :type 'hook)
  248. (defcustom org-load-hook nil
  249. "Hook that is run after org.el has been loaded."
  250. :group 'org
  251. :type 'hook)
  252. (defcustom org-log-buffer-setup-hook nil
  253. "Hook that is run after an Org log buffer is created."
  254. :group 'org
  255. :version "24.1"
  256. :type 'hook)
  257. (defvar org-modules) ; defined below
  258. (defvar org-modules-loaded nil
  259. "Have the modules been loaded already?")
  260. (defun org-load-modules-maybe (&optional force)
  261. "Load all extensions listed in `org-modules'."
  262. (when (or force (not org-modules-loaded))
  263. (mapc (lambda (ext)
  264. (condition-case nil (require ext)
  265. (error (message "Problems while trying to load feature `%s'" ext))))
  266. org-modules)
  267. (setq org-modules-loaded t)))
  268. (defun org-set-modules (var value)
  269. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  270. (set var value)
  271. (when (featurep 'org)
  272. (org-load-modules-maybe 'force)))
  273. (when (org-bound-and-true-p org-modules)
  274. (let ((a (member 'org-infojs org-modules)))
  275. (and a (setcar a 'org-jsinfo))))
  276. (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)
  277. "Modules that should always be loaded together with org.el.
  278. If a description starts with <C>, the file is not part of Emacs
  279. and loading it will require that you have downloaded and properly installed
  280. the org-mode distribution.
  281. You can also use this system to load external packages (i.e. neither Org
  282. core modules, nor modules from the CONTRIB directory). Just add symbols
  283. to the end of the list. If the package is called org-xyz.el, then you need
  284. to add the symbol `xyz', and the package must have a call to
  285. (provide 'org-xyz)"
  286. :group 'org
  287. :set 'org-set-modules
  288. :type
  289. '(set :greedy t
  290. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  291. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  292. (const :tag " crypt: Encryption of subtrees" org-crypt)
  293. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  294. (const :tag " docview: Links to doc-view buffers" org-docview)
  295. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  296. (const :tag " id: Global IDs for identifying entries" org-id)
  297. (const :tag " info: Links to Info nodes" org-info)
  298. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  299. (const :tag " habit: Track your consistency with habits" org-habit)
  300. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  301. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  302. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  303. (const :tag " mew Links to Mew folders/messages" org-mew)
  304. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  305. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  306. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  307. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  308. (const :tag " vm: Links to VM folders/messages" org-vm)
  309. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  310. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  311. (const :tag " mouse: Additional mouse support" org-mouse)
  312. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  313. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  314. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  315. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  316. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  317. (const :tag "C collector: Collect properties into tables" org-collector)
  318. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  319. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  320. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  321. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  322. (const :tag "C eval: Include command output as text" org-eval)
  323. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  324. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  325. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  326. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  327. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  328. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  329. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  330. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  331. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  332. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  333. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  334. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  335. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  336. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  337. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  338. (const :tag "C registry: A registry for Org-mode links" org-registry)
  339. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  340. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  341. (const :tag "C secretary: Team management with org-mode" org-secretary)
  342. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  343. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  344. (const :tag "C track: Keep up with Org-mode development" org-track)
  345. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  346. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  347. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  348. (defcustom org-support-shift-select nil
  349. "Non-nil means make shift-cursor commands select text when possible.
  350. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  351. start selecting a region, or enlarge regions started in this way.
  352. In Org-mode, in special contexts, these same keys are used for
  353. other purposes, important enough to compete with shift selection.
  354. Org tries to balance these needs by supporting `shift-select-mode'
  355. outside these special contexts, under control of this variable.
  356. The default of this variable is nil, to avoid confusing behavior. Shifted
  357. cursor keys will then execute Org commands in the following contexts:
  358. - on a headline, changing TODO state (left/right) and priority (up/down)
  359. - on a time stamp, changing the time
  360. - in a plain list item, changing the bullet type
  361. - in a property definition line, switching between allowed values
  362. - in the BEGIN line of a clock table (changing the time block).
  363. Outside these contexts, the commands will throw an error.
  364. When this variable is t and the cursor is not in a special
  365. context, Org-mode will support shift-selection for making and
  366. enlarging regions. To make this more effective, the bullet
  367. cycling will no longer happen anywhere in an item line, but only
  368. if the cursor is exactly on the bullet.
  369. If you set this variable to the symbol `always', then the keys
  370. will not be special in headlines, property lines, and item lines,
  371. to make shift selection work there as well. If this is what you
  372. want, you can use the following alternative commands: `C-c C-t'
  373. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  374. can be used to switch TODO sets, `C-c -' to cycle item bullet
  375. types, and properties can be edited by hand or in column view.
  376. However, when the cursor is on a timestamp, shift-cursor commands
  377. will still edit the time stamp - this is just too good to give up.
  378. XEmacs user should have this variable set to nil, because
  379. `shift-select-mode' is in Emacs 23 or later only."
  380. :group 'org
  381. :type '(choice
  382. (const :tag "Never" nil)
  383. (const :tag "When outside special context" t)
  384. (const :tag "Everywhere except timestamps" always)))
  385. (defcustom org-loop-over-headlines-in-active-region nil
  386. "Shall some commands act upon headlines in the active region?
  387. When set to `t', some commands will be performed in all headlines
  388. within the active region.
  389. When set to `start-level', some commands will be performed in all
  390. headlines within the active region, provided that these headlines
  391. are of the same level than the first one.
  392. When set to a string, those commands will be performed on the
  393. matching headlines within the active region. Such string must be
  394. a tags/property/todo match as it is used in the agenda tags view.
  395. The list of commands is: `org-schedule', `org-deadline',
  396. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  397. `org-archive-to-archive-sibling'. The archiving commands skip
  398. already archived entries."
  399. :type '(choice (const :tag "Don't loop" nil)
  400. (const :tag "All headlines in active region" t)
  401. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  402. (string :tag "Tags/Property/Todo matcher"))
  403. :version "24.1"
  404. :group 'org-todo
  405. :group 'org-archive)
  406. (defgroup org-startup nil
  407. "Options concerning startup of Org-mode."
  408. :tag "Org Startup"
  409. :group 'org)
  410. (defcustom org-startup-folded t
  411. "Non-nil means entering Org-mode will switch to OVERVIEW.
  412. This can also be configured on a per-file basis by adding one of
  413. the following lines anywhere in the buffer:
  414. #+STARTUP: fold (or `overview', this is equivalent)
  415. #+STARTUP: nofold (or `showall', this is equivalent)
  416. #+STARTUP: content
  417. #+STARTUP: showeverything
  418. By default, this option is ignored when Org opens agenda files
  419. for the first time. If you want the agenda to honor the startup
  420. option, set `org-agenda-inhibit-startup' to nil."
  421. :group 'org-startup
  422. :type '(choice
  423. (const :tag "nofold: show all" nil)
  424. (const :tag "fold: overview" t)
  425. (const :tag "content: all headlines" content)
  426. (const :tag "show everything, even drawers" showeverything)))
  427. (defcustom org-startup-truncated t
  428. "Non-nil means entering Org-mode will set `truncate-lines'.
  429. This is useful since some lines containing links can be very long and
  430. uninteresting. Also tables look terrible when wrapped."
  431. :group 'org-startup
  432. :type 'boolean)
  433. (defcustom org-startup-indented nil
  434. "Non-nil means turn on `org-indent-mode' on startup.
  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: indent
  438. #+STARTUP: noindent"
  439. :group 'org-structure
  440. :type '(choice
  441. (const :tag "Not" nil)
  442. (const :tag "Globally (slow on startup in large files)" t)))
  443. (defcustom org-use-sub-superscripts t
  444. "Non-nil means interpret \"_\" and \"^\" for export.
  445. When this option is turned on, you can use TeX-like syntax for sub- and
  446. superscripts. Several characters after \"_\" or \"^\" will be
  447. considered as a single item - so grouping with {} is normally not
  448. needed. For example, the following things will be parsed as single
  449. sub- or superscripts.
  450. 10^24 or 10^tau several digits will be considered 1 item.
  451. 10^-12 or 10^-tau a leading sign with digits or a word
  452. x^2-y^3 will be read as x^2 - y^3, because items are
  453. terminated by almost any nonword/nondigit char.
  454. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  455. Still, ambiguity is possible - so when in doubt use {} to enclose the
  456. sub/superscript. If you set this variable to the symbol `{}',
  457. the braces are *required* in order to trigger interpretations as
  458. sub/superscript. This can be helpful in documents that need \"_\"
  459. frequently in plain text.
  460. Not all export backends support this, but HTML does.
  461. This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
  462. :group 'org-startup
  463. :group 'org-export-translation
  464. :version "24.1"
  465. :type '(choice
  466. (const :tag "Always interpret" t)
  467. (const :tag "Only with braces" {})
  468. (const :tag "Never interpret" nil)))
  469. (if (fboundp 'defvaralias)
  470. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  471. (defcustom org-startup-with-beamer-mode nil
  472. "Non-nil means turn on `org-beamer-mode' on startup.
  473. This can also be configured on a per-file basis by adding one of
  474. the following lines anywhere in the buffer:
  475. #+STARTUP: beamer"
  476. :group 'org-startup
  477. :version "24.1"
  478. :type 'boolean)
  479. (defcustom org-startup-align-all-tables nil
  480. "Non-nil means align all tables when visiting a file.
  481. This is useful when the column width in tables is forced with <N> cookies
  482. in table fields. Such tables will look correct only after the first re-align.
  483. This can also be configured on a per-file basis by adding one of
  484. the following lines anywhere in the buffer:
  485. #+STARTUP: align
  486. #+STARTUP: noalign"
  487. :group 'org-startup
  488. :type 'boolean)
  489. (defcustom org-startup-with-inline-images nil
  490. "Non-nil means show inline images when loading a new Org file.
  491. This can also be configured on a per-file basis by adding one of
  492. the following lines anywhere in the buffer:
  493. #+STARTUP: inlineimages
  494. #+STARTUP: noinlineimages"
  495. :group 'org-startup
  496. :version "24.1"
  497. :type 'boolean)
  498. (defcustom org-insert-mode-line-in-empty-file nil
  499. "Non-nil means insert the first line setting Org-mode in empty files.
  500. When the function `org-mode' is called interactively in an empty file, this
  501. normally means that the file name does not automatically trigger Org-mode.
  502. To ensure that the file will always be in Org-mode in the future, a
  503. line enforcing Org-mode will be inserted into the buffer, if this option
  504. has been set."
  505. :group 'org-startup
  506. :type 'boolean)
  507. (defcustom org-replace-disputed-keys nil
  508. "Non-nil means use alternative key bindings for some keys.
  509. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  510. These keys are also used by other packages like shift-selection-mode'
  511. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  512. If you want to use Org-mode together with one of these other modes,
  513. or more generally if you would like to move some Org-mode commands to
  514. other keys, set this variable and configure the keys with the variable
  515. `org-disputed-keys'.
  516. This option is only relevant at load-time of Org-mode, and must be set
  517. *before* org.el is loaded. Changing it requires a restart of Emacs to
  518. become effective."
  519. :group 'org-startup
  520. :type 'boolean)
  521. (defcustom org-use-extra-keys nil
  522. "Non-nil means use extra key sequence definitions for certain commands.
  523. This happens automatically if you run XEmacs or if `window-system'
  524. is nil. This variable lets you do the same manually. You must
  525. set it before loading org.
  526. Example: on Carbon Emacs 22 running graphically, with an external
  527. keyboard on a Powerbook, the default way of setting M-left might
  528. not work for either Alt or ESC. Setting this variable will make
  529. it work for ESC."
  530. :group 'org-startup
  531. :type 'boolean)
  532. (if (fboundp 'defvaralias)
  533. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  534. (defcustom org-disputed-keys
  535. '(([(shift up)] . [(meta p)])
  536. ([(shift down)] . [(meta n)])
  537. ([(shift left)] . [(meta -)])
  538. ([(shift right)] . [(meta +)])
  539. ([(control shift right)] . [(meta shift +)])
  540. ([(control shift left)] . [(meta shift -)]))
  541. "Keys for which Org-mode and other modes compete.
  542. This is an alist, cars are the default keys, second element specifies
  543. the alternative to use when `org-replace-disputed-keys' is t.
  544. Keys can be specified in any syntax supported by `define-key'.
  545. The value of this option takes effect only at Org-mode's startup,
  546. therefore you'll have to restart Emacs to apply it after changing."
  547. :group 'org-startup
  548. :type 'alist)
  549. (defun org-key (key)
  550. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  551. Or return the original if not disputed.
  552. Also apply the translations defined in `org-xemacs-key-equivalents'."
  553. (when org-replace-disputed-keys
  554. (let* ((nkey (key-description key))
  555. (x (org-find-if (lambda (x)
  556. (equal (key-description (car x)) nkey))
  557. org-disputed-keys)))
  558. (setq key (if x (cdr x) key))))
  559. (when (featurep 'xemacs)
  560. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  561. key)
  562. (defun org-find-if (predicate seq)
  563. (catch 'exit
  564. (while seq
  565. (if (funcall predicate (car seq))
  566. (throw 'exit (car seq))
  567. (pop seq)))))
  568. (defun org-defkey (keymap key def)
  569. "Define a key, possibly translated, as returned by `org-key'."
  570. (define-key keymap (org-key key) def))
  571. (defcustom org-ellipsis nil
  572. "The ellipsis to use in the Org-mode outline.
  573. When nil, just use the standard three dots. When a string, use that instead,
  574. When a face, use the standard 3 dots, but with the specified face.
  575. The change affects only Org-mode (which will then use its own display table).
  576. Changing this requires executing `M-x org-mode' in a buffer to become
  577. effective."
  578. :group 'org-startup
  579. :type '(choice (const :tag "Default" nil)
  580. (face :tag "Face" :value org-warning)
  581. (string :tag "String" :value "...#")))
  582. (defvar org-display-table nil
  583. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  584. (defgroup org-keywords nil
  585. "Keywords in Org-mode."
  586. :tag "Org Keywords"
  587. :group 'org)
  588. (defcustom org-deadline-string "DEADLINE:"
  589. "String to mark deadline entries.
  590. A deadline is this string, followed by a time stamp. Should be a word,
  591. terminated by a colon. You can insert a schedule keyword and
  592. a timestamp with \\[org-deadline].
  593. Changes become only effective after restarting Emacs."
  594. :group 'org-keywords
  595. :type 'string)
  596. (defcustom org-scheduled-string "SCHEDULED:"
  597. "String to mark scheduled TODO entries.
  598. A schedule is this string, followed by a time stamp. Should be a word,
  599. terminated by a colon. You can insert a schedule keyword and
  600. a timestamp with \\[org-schedule].
  601. Changes become only effective after restarting Emacs."
  602. :group 'org-keywords
  603. :type 'string)
  604. (defcustom org-closed-string "CLOSED:"
  605. "String used as the prefix for timestamps logging closing a TODO entry."
  606. :group 'org-keywords
  607. :type 'string)
  608. (defcustom org-clock-string "CLOCK:"
  609. "String used as prefix for timestamps clocking work hours on an item."
  610. :group 'org-keywords
  611. :type 'string)
  612. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  613. org-scheduled-string "\\|"
  614. org-deadline-string "\\|"
  615. org-closed-string "\\|"
  616. org-clock-string "\\)")
  617. "Matches a line with planning or clock info.")
  618. (defcustom org-comment-string "COMMENT"
  619. "Entries starting with this keyword will never be exported.
  620. An entry can be toggled between COMMENT and normal with
  621. \\[org-toggle-comment].
  622. Changes become only effective after restarting Emacs."
  623. :group 'org-keywords
  624. :type 'string)
  625. (defcustom org-quote-string "QUOTE"
  626. "Entries starting with this keyword will be exported in fixed-width font.
  627. Quoting applies only to the text in the entry following the headline, and does
  628. not extend beyond the next headline, even if that is lower level.
  629. An entry can be toggled between QUOTE and normal with
  630. \\[org-toggle-fixed-width-section]."
  631. :group 'org-keywords
  632. :type 'string)
  633. (defconst org-repeat-re
  634. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  635. "Regular expression for specifying repeated events.
  636. After a match, group 1 contains the repeat expression.")
  637. (defgroup org-structure nil
  638. "Options concerning the general structure of Org-mode files."
  639. :tag "Org Structure"
  640. :group 'org)
  641. (defgroup org-reveal-location nil
  642. "Options about how to make context of a location visible."
  643. :tag "Org Reveal Location"
  644. :group 'org-structure)
  645. (defconst org-context-choice
  646. '(choice
  647. (const :tag "Always" t)
  648. (const :tag "Never" nil)
  649. (repeat :greedy t :tag "Individual contexts"
  650. (cons
  651. (choice :tag "Context"
  652. (const agenda)
  653. (const org-goto)
  654. (const occur-tree)
  655. (const tags-tree)
  656. (const link-search)
  657. (const mark-goto)
  658. (const bookmark-jump)
  659. (const isearch)
  660. (const default))
  661. (boolean))))
  662. "Contexts for the reveal options.")
  663. (defcustom org-show-hierarchy-above '((default . t))
  664. "Non-nil means show full hierarchy when revealing a location.
  665. Org-mode often shows locations in an org-mode file which might have
  666. been invisible before. When this is set, the hierarchy of headings
  667. above the exposed location is shown.
  668. Turning this off for example for sparse trees makes them very compact.
  669. Instead of t, this can also be an alist specifying this option for different
  670. contexts. Valid contexts are
  671. agenda when exposing an entry from the agenda
  672. org-goto when using the command `org-goto' on key C-c C-j
  673. occur-tree when using the command `org-occur' on key C-c /
  674. tags-tree when constructing a sparse tree based on tags matches
  675. link-search when exposing search matches associated with a link
  676. mark-goto when exposing the jump goal of a mark
  677. bookmark-jump when exposing a bookmark location
  678. isearch when exiting from an incremental search
  679. default default for all contexts not set explicitly"
  680. :group 'org-reveal-location
  681. :type org-context-choice)
  682. (defcustom org-show-following-heading '((default . nil))
  683. "Non-nil means show following heading when revealing a location.
  684. Org-mode often shows locations in an org-mode file which might have
  685. been invisible before. When this is set, the heading following the
  686. match is shown.
  687. Turning this off for example for sparse trees makes them very compact,
  688. but makes it harder to edit the location of the match. In such a case,
  689. use the command \\[org-reveal] to show more context.
  690. Instead of t, this can also be an alist specifying this option for different
  691. contexts. See `org-show-hierarchy-above' for valid contexts."
  692. :group 'org-reveal-location
  693. :type org-context-choice)
  694. (defcustom org-show-siblings '((default . nil) (isearch t))
  695. "Non-nil means show all sibling heading when revealing a location.
  696. Org-mode often shows locations in an org-mode file which might have
  697. been invisible before. When this is set, the sibling of the current entry
  698. heading are all made visible. If `org-show-hierarchy-above' is t,
  699. the same happens on each level of the hierarchy above the current entry.
  700. By default this is on for the isearch context, off for all other contexts.
  701. Turning this off for example for sparse trees makes them very compact,
  702. but makes it harder to edit the location of the match. In such a case,
  703. use the command \\[org-reveal] to show more context.
  704. Instead of t, this can also be an alist specifying this option for different
  705. contexts. See `org-show-hierarchy-above' for valid contexts."
  706. :group 'org-reveal-location
  707. :type org-context-choice)
  708. (defcustom org-show-entry-below '((default . nil))
  709. "Non-nil means show the entry below a headline when revealing a location.
  710. Org-mode often shows locations in an org-mode file which might have
  711. been invisible before. When this is set, the text below the headline that is
  712. exposed is also shown.
  713. By default this is off for all contexts.
  714. Instead of t, this can also be an alist specifying this option for different
  715. contexts. See `org-show-hierarchy-above' for valid contexts."
  716. :group 'org-reveal-location
  717. :type org-context-choice)
  718. (defcustom org-indirect-buffer-display 'other-window
  719. "How should indirect tree buffers be displayed?
  720. This applies to indirect buffers created with the commands
  721. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  722. Valid values are:
  723. current-window Display in the current window
  724. other-window Just display in another window.
  725. dedicated-frame Create one new frame, and re-use it each time.
  726. new-frame Make a new frame each time. Note that in this case
  727. previously-made indirect buffers are kept, and you need to
  728. kill these buffers yourself."
  729. :group 'org-structure
  730. :group 'org-agenda-windows
  731. :type '(choice
  732. (const :tag "In current window" current-window)
  733. (const :tag "In current frame, other window" other-window)
  734. (const :tag "Each time a new frame" new-frame)
  735. (const :tag "One dedicated frame" dedicated-frame)))
  736. (defcustom org-use-speed-commands nil
  737. "Non-nil means activate single letter commands at beginning of a headline.
  738. This may also be a function to test for appropriate locations where speed
  739. commands should be active."
  740. :group 'org-structure
  741. :type '(choice
  742. (const :tag "Never" nil)
  743. (const :tag "At beginning of headline stars" t)
  744. (function)))
  745. (defcustom org-speed-commands-user nil
  746. "Alist of additional speed commands.
  747. This list will be checked before `org-speed-commands-default'
  748. when the variable `org-use-speed-commands' is non-nil
  749. and when the cursor is at the beginning of a headline.
  750. The car if each entry is a string with a single letter, which must
  751. be assigned to `self-insert-command' in the global map.
  752. The cdr is either a command to be called interactively, a function
  753. to be called, or a form to be evaluated.
  754. An entry that is just a list with a single string will be interpreted
  755. as a descriptive headline that will be added when listing the speed
  756. commands in the Help buffer using the `?' speed command."
  757. :group 'org-structure
  758. :type '(repeat :value ("k" . ignore)
  759. (choice :value ("k" . ignore)
  760. (list :tag "Descriptive Headline" (string :tag "Headline"))
  761. (cons :tag "Letter and Command"
  762. (string :tag "Command letter")
  763. (choice
  764. (function)
  765. (sexp))))))
  766. (defgroup org-cycle nil
  767. "Options concerning visibility cycling in Org-mode."
  768. :tag "Org Cycle"
  769. :group 'org-structure)
  770. (defcustom org-cycle-skip-children-state-if-no-children t
  771. "Non-nil means skip CHILDREN state in entries that don't have any."
  772. :group 'org-cycle
  773. :type 'boolean)
  774. (defcustom org-cycle-max-level nil
  775. "Maximum level which should still be subject to visibility cycling.
  776. Levels higher than this will, for cycling, be treated as text, not a headline.
  777. When `org-odd-levels-only' is set, a value of N in this variable actually
  778. means 2N-1 stars as the limiting headline.
  779. When nil, cycle all levels.
  780. Note that the limiting level of cycling is also influenced by
  781. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  782. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  783. than its value."
  784. :group 'org-cycle
  785. :type '(choice
  786. (const :tag "No limit" nil)
  787. (integer :tag "Maximum level")))
  788. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  789. "Names of drawers. Drawers are not opened by cycling on the headline above.
  790. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  791. this:
  792. :DRAWERNAME:
  793. .....
  794. :END:
  795. The drawer \"PROPERTIES\" is special for capturing properties through
  796. the property API.
  797. Drawers can be defined on the per-file basis with a line like:
  798. #+DRAWERS: HIDDEN STATE PROPERTIES"
  799. :group 'org-structure
  800. :group 'org-cycle
  801. :type '(repeat (string :tag "Drawer Name")))
  802. (defcustom org-hide-block-startup nil
  803. "Non-nil means entering Org-mode will fold all blocks.
  804. This can also be set in on a per-file basis with
  805. #+STARTUP: hideblocks
  806. #+STARTUP: showblocks"
  807. :group 'org-startup
  808. :group 'org-cycle
  809. :type 'boolean)
  810. (defcustom org-cycle-global-at-bob nil
  811. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  812. This makes it possible to do global cycling without having to use S-TAB or
  813. \\[universal-argument] TAB. For this special case to work, the first line
  814. of the buffer must not be a headline -- it may be empty or some other text.
  815. When used in this way, `org-cycle-hook' is disabled temporarily to make
  816. sure the cursor stays at the beginning of the buffer. When this option is
  817. nil, don't do anything special at the beginning of the buffer."
  818. :group 'org-cycle
  819. :type 'boolean)
  820. (defcustom org-cycle-level-after-item/entry-creation t
  821. "Non-nil means cycle entry level or item indentation in new empty entries.
  822. When the cursor is at the end of an empty headline, i.e., with only stars
  823. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  824. and then all possible ancestor states, before returning to the original state.
  825. This makes data entry extremely fast: M-RET to create a new headline,
  826. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  827. When the cursor is at the end of an empty plain list item, one TAB will
  828. make it a subitem, two or more tabs will back up to make this an item
  829. higher up in the item hierarchy."
  830. :group 'org-cycle
  831. :type 'boolean)
  832. (defcustom org-cycle-emulate-tab t
  833. "Where should `org-cycle' emulate TAB.
  834. nil Never
  835. white Only in completely white lines
  836. whitestart Only at the beginning of lines, before the first non-white char
  837. t Everywhere except in headlines
  838. exc-hl-bol Everywhere except at the start of a headline
  839. If TAB is used in a place where it does not emulate TAB, the current subtree
  840. visibility is cycled."
  841. :group 'org-cycle
  842. :type '(choice (const :tag "Never" nil)
  843. (const :tag "Only in completely white lines" white)
  844. (const :tag "Before first char in a line" whitestart)
  845. (const :tag "Everywhere except in headlines" t)
  846. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  847. ))
  848. (defcustom org-cycle-separator-lines 2
  849. "Number of empty lines needed to keep an empty line between collapsed trees.
  850. If you leave an empty line between the end of a subtree and the following
  851. headline, this empty line is hidden when the subtree is folded.
  852. Org-mode will leave (exactly) one empty line visible if the number of
  853. empty lines is equal or larger to the number given in this variable.
  854. So the default 2 means at least 2 empty lines after the end of a subtree
  855. are needed to produce free space between a collapsed subtree and the
  856. following headline.
  857. If the number is negative, and the number of empty lines is at least -N,
  858. all empty lines are shown.
  859. Special case: when 0, never leave empty lines in collapsed view."
  860. :group 'org-cycle
  861. :type 'integer)
  862. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  863. (defcustom org-pre-cycle-hook nil
  864. "Hook that is run before visibility cycling is happening.
  865. The function(s) in this hook must accept a single argument which indicates
  866. the new state that will be set right after running this hook. The
  867. argument is a symbol. Before a global state change, it can have the values
  868. `overview', `content', or `all'. Before a local state change, it can have
  869. the values `folded', `children', or `subtree'."
  870. :group 'org-cycle
  871. :type 'hook)
  872. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  873. org-cycle-hide-drawers
  874. org-cycle-show-empty-lines
  875. org-optimize-window-after-visibility-change)
  876. "Hook that is run after `org-cycle' has changed the buffer visibility.
  877. The function(s) in this hook must accept a single argument which indicates
  878. the new state that was set by the most recent `org-cycle' command. The
  879. argument is a symbol. After a global state change, it can have the values
  880. `overview', `contents', or `all'. After a local state change, it can have
  881. the values `folded', `children', or `subtree'."
  882. :group 'org-cycle
  883. :type 'hook)
  884. (defgroup org-edit-structure nil
  885. "Options concerning structure editing in Org-mode."
  886. :tag "Org Edit Structure"
  887. :group 'org-structure)
  888. (defcustom org-odd-levels-only nil
  889. "Non-nil means skip even levels and only use odd levels for the outline.
  890. This has the effect that two stars are being added/taken away in
  891. promotion/demotion commands. It also influences how levels are
  892. handled by the exporters.
  893. Changing it requires restart of `font-lock-mode' to become effective
  894. for fontification also in regions already fontified.
  895. You may also set this on a per-file basis by adding one of the following
  896. lines to the buffer:
  897. #+STARTUP: odd
  898. #+STARTUP: oddeven"
  899. :group 'org-edit-structure
  900. :group 'org-appearance
  901. :type 'boolean)
  902. (defcustom org-adapt-indentation t
  903. "Non-nil means adapt indentation to outline node level.
  904. When this variable is set, Org assumes that you write outlines by
  905. indenting text in each node to align with the headline (after the stars).
  906. The following issues are influenced by this variable:
  907. - When this is set and the *entire* text in an entry is indented, the
  908. indentation is increased by one space in a demotion command, and
  909. decreased by one in a promotion command. If any line in the entry
  910. body starts with text at column 0, indentation is not changed at all.
  911. - Property drawers and planning information is inserted indented when
  912. this variable s set. When nil, they will not be indented.
  913. - TAB indents a line relative to context. The lines below a headline
  914. will be indented when this variable is set.
  915. Note that this is all about true indentation, by adding and removing
  916. space characters. See also `org-indent.el' which does level-dependent
  917. indentation in a virtual way, i.e. at display time in Emacs."
  918. :group 'org-edit-structure
  919. :type 'boolean)
  920. (defcustom org-special-ctrl-a/e nil
  921. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  922. When t, `C-a' will bring back the cursor to the beginning of the
  923. headline text, i.e. after the stars and after a possible TODO
  924. keyword. In an item, this will be the position after bullet and
  925. check-box, if any. When the cursor is already at that position,
  926. another `C-a' will bring it to the beginning of the line.
  927. `C-e' will jump to the end of the headline, ignoring the presence
  928. of tags in the headline. A second `C-e' will then jump to the
  929. true end of the line, after any tags. This also means that, when
  930. this variable is non-nil, `C-e' also will never jump beyond the
  931. end of the heading of a folded section, i.e. not after the
  932. ellipses.
  933. When set to the symbol `reversed', the first `C-a' or `C-e' works
  934. normally, going to the true line boundary first. Only a directly
  935. following, identical keypress will bring the cursor to the
  936. special positions.
  937. This may also be a cons cell where the behavior for `C-a' and
  938. `C-e' is set separately."
  939. :group 'org-edit-structure
  940. :type '(choice
  941. (const :tag "off" nil)
  942. (const :tag "on: after stars/bullet and before tags first" t)
  943. (const :tag "reversed: true line boundary first" reversed)
  944. (cons :tag "Set C-a and C-e separately"
  945. (choice :tag "Special C-a"
  946. (const :tag "off" nil)
  947. (const :tag "on: after stars/bullet first" t)
  948. (const :tag "reversed: before stars/bullet first" reversed))
  949. (choice :tag "Special C-e"
  950. (const :tag "off" nil)
  951. (const :tag "on: before tags first" t)
  952. (const :tag "reversed: after tags first" reversed)))))
  953. (if (fboundp 'defvaralias)
  954. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  955. (defcustom org-special-ctrl-k nil
  956. "Non-nil means `C-k' will behave specially in headlines.
  957. When nil, `C-k' will call the default `kill-line' command.
  958. When t, the following will happen while the cursor is in the headline:
  959. - When the cursor is at the beginning of a headline, kill the entire
  960. line and possible the folded subtree below the line.
  961. - When in the middle of the headline text, kill the headline up to the tags.
  962. - When after the headline text, kill the tags."
  963. :group 'org-edit-structure
  964. :type 'boolean)
  965. (defcustom org-ctrl-k-protect-subtree nil
  966. "Non-nil means, do not delete a hidden subtree with C-k.
  967. When set to the symbol `error', simply throw an error when C-k is
  968. used to kill (part-of) a headline that has hidden text behind it.
  969. Any other non-nil value will result in a query to the user, if it is
  970. OK to kill that hidden subtree. When nil, kill without remorse."
  971. :group 'org-edit-structure
  972. :version "24.1"
  973. :type '(choice
  974. (const :tag "Do not protect hidden subtrees" nil)
  975. (const :tag "Protect hidden subtrees with a security query" t)
  976. (const :tag "Never kill a hidden subtree with C-k" error)))
  977. (defcustom org-catch-invisible-edits nil
  978. "Check if in invisible region before inserting or deleting a character.
  979. Valid values are:
  980. nil Do not check, so just do invisible edits.
  981. error Throw an error and do nothing.
  982. show Make point visible, and do the requested edit.
  983. show-and-error Make point visible, then throw an error and abort the edit.
  984. smart Make point visible, and do insertion/deletion if it is
  985. adjacent to visible text and the change feels predictable.
  986. Never delete a previously invisible character or add in the
  987. middle or right after an invisible region. Basically, this
  988. allows insertion and backward-delete right before ellipses.
  989. FIXME: maybe in this case we should not even show?"
  990. :group 'org-edit-structure
  991. :version "24.1"
  992. :type '(choice
  993. (const :tag "Do not check" nil)
  994. (const :tag "Throw error when trying to edit" error)
  995. (const :tag "Unhide, but do not do the edit" show-and-error)
  996. (const :tag "Show invisible part and do the edit" show)
  997. (const :tag "Be smart and do the right thing" smart)))
  998. (defcustom org-yank-folded-subtrees t
  999. "Non-nil means when yanking subtrees, fold them.
  1000. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1001. it starts with a heading and all other headings in it are either children
  1002. or siblings, then fold all the subtrees. However, do this only if no
  1003. text after the yank would be swallowed into a folded tree by this action."
  1004. :group 'org-edit-structure
  1005. :type 'boolean)
  1006. (defcustom org-yank-adjusted-subtrees nil
  1007. "Non-nil means when yanking subtrees, adjust the level.
  1008. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1009. this function for details."
  1010. :group 'org-edit-structure
  1011. :type 'boolean)
  1012. (defcustom org-M-RET-may-split-line '((default . t))
  1013. "Non-nil means M-RET will split the line at the cursor position.
  1014. When nil, it will go to the end of the line before making a
  1015. new line.
  1016. You may also set this option in a different way for different
  1017. contexts. Valid contexts are:
  1018. headline when creating a new headline
  1019. item when creating a new item
  1020. table in a table field
  1021. default the value to be used for all contexts not explicitly
  1022. customized"
  1023. :group 'org-structure
  1024. :group 'org-table
  1025. :type '(choice
  1026. (const :tag "Always" t)
  1027. (const :tag "Never" nil)
  1028. (repeat :greedy t :tag "Individual contexts"
  1029. (cons
  1030. (choice :tag "Context"
  1031. (const headline)
  1032. (const item)
  1033. (const table)
  1034. (const default))
  1035. (boolean)))))
  1036. (defcustom org-insert-heading-respect-content nil
  1037. "Non-nil means insert new headings after the current subtree.
  1038. When nil, the new heading is created directly after the current line.
  1039. The commands \\[org-insert-heading-respect-content] and
  1040. \\[org-insert-todo-heading-respect-content] turn this variable on
  1041. for the duration of the command."
  1042. :group 'org-structure
  1043. :type 'boolean)
  1044. (defcustom org-blank-before-new-entry '((heading . auto)
  1045. (plain-list-item . auto))
  1046. "Should `org-insert-heading' leave a blank line before new heading/item?
  1047. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1048. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1049. which case Org will look at the surrounding headings/items and try to
  1050. make an intelligent decision whether to insert a blank line or not.
  1051. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1052. set, the setting here is ignored and no empty line is inserted, to avoid
  1053. breaking the list structure."
  1054. :group 'org-edit-structure
  1055. :type '(list
  1056. (cons (const heading)
  1057. (choice (const :tag "Never" nil)
  1058. (const :tag "Always" t)
  1059. (const :tag "Auto" auto)))
  1060. (cons (const plain-list-item)
  1061. (choice (const :tag "Never" nil)
  1062. (const :tag "Always" t)
  1063. (const :tag "Auto" auto)))))
  1064. (defcustom org-insert-heading-hook nil
  1065. "Hook being run after inserting a new heading."
  1066. :group 'org-edit-structure
  1067. :type 'hook)
  1068. (defcustom org-enable-fixed-width-editor t
  1069. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1070. This currently only means they are never auto-wrapped.
  1071. When nil, such lines will be treated like ordinary lines.
  1072. See also the QUOTE keyword."
  1073. :group 'org-edit-structure
  1074. :type 'boolean)
  1075. (defcustom org-goto-auto-isearch t
  1076. "Non-nil means typing characters in `org-goto' starts incremental search.
  1077. When nil, you can use these keybindings to navigate the buffer:
  1078. q Quit the org-goto interface
  1079. n Go to the next visible heading
  1080. p Go to the previous visible heading
  1081. f Go one heading forward on same level
  1082. b Go one heading backward on same level
  1083. u Go one heading up"
  1084. :group 'org-edit-structure
  1085. :type 'boolean)
  1086. (defgroup org-sparse-trees nil
  1087. "Options concerning sparse trees in Org-mode."
  1088. :tag "Org Sparse Trees"
  1089. :group 'org-structure)
  1090. (defcustom org-highlight-sparse-tree-matches t
  1091. "Non-nil means highlight all matches that define a sparse tree.
  1092. The highlights will automatically disappear the next time the buffer is
  1093. changed by an edit command."
  1094. :group 'org-sparse-trees
  1095. :type 'boolean)
  1096. (defcustom org-remove-highlights-with-change t
  1097. "Non-nil means any change to the buffer will remove temporary highlights.
  1098. Such highlights are created by `org-occur' and `org-clock-display'.
  1099. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1100. The highlights created by `org-preview-latex-fragment' always need
  1101. `C-c C-c' to be removed."
  1102. :group 'org-sparse-trees
  1103. :group 'org-time
  1104. :type 'boolean)
  1105. (defcustom org-occur-hook '(org-first-headline-recenter)
  1106. "Hook that is run after `org-occur' has constructed a sparse tree.
  1107. This can be used to recenter the window to show as much of the structure
  1108. as possible."
  1109. :group 'org-sparse-trees
  1110. :type 'hook)
  1111. (defgroup org-imenu-and-speedbar nil
  1112. "Options concerning imenu and speedbar in Org-mode."
  1113. :tag "Org Imenu and Speedbar"
  1114. :group 'org-structure)
  1115. (defcustom org-imenu-depth 2
  1116. "The maximum level for Imenu access to Org-mode headlines.
  1117. This also applied for speedbar access."
  1118. :group 'org-imenu-and-speedbar
  1119. :type 'integer)
  1120. (defgroup org-table nil
  1121. "Options concerning tables in Org-mode."
  1122. :tag "Org Table"
  1123. :group 'org)
  1124. (defcustom org-enable-table-editor 'optimized
  1125. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1126. When nil, such lines will be treated like ordinary lines.
  1127. When equal to the symbol `optimized', the table editor will be optimized to
  1128. do the following:
  1129. - Automatic overwrite mode in front of whitespace in table fields.
  1130. This makes the structure of the table stay in tact as long as the edited
  1131. field does not exceed the column width.
  1132. - Minimize the number of realigns. Normally, the table is aligned each time
  1133. TAB or RET are pressed to move to another field. With optimization this
  1134. happens only if changes to a field might have changed the column width.
  1135. Optimization requires replacing the functions `self-insert-command',
  1136. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1137. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1138. very good at guessing when a re-align will be necessary, but you can always
  1139. force one with \\[org-ctrl-c-ctrl-c].
  1140. If you would like to use the optimized version in Org-mode, but the
  1141. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1142. This variable can be used to turn on and off the table editor during a session,
  1143. but in order to toggle optimization, a restart is required.
  1144. See also the variable `org-table-auto-blank-field'."
  1145. :group 'org-table
  1146. :type '(choice
  1147. (const :tag "off" nil)
  1148. (const :tag "on" t)
  1149. (const :tag "on, optimized" optimized)))
  1150. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1151. (version<= emacs-version "24.1"))
  1152. "Non-nil means cluster self-insert commands for undo when possible.
  1153. If this is set, then, like in the Emacs command loop, 20 consecutive
  1154. characters will be undone together.
  1155. This is configurable, because there is some impact on typing performance."
  1156. :group 'org-table
  1157. :type 'boolean)
  1158. (defcustom org-table-tab-recognizes-table.el t
  1159. "Non-nil means TAB will automatically notice a table.el table.
  1160. When it sees such a table, it moves point into it and - if necessary -
  1161. calls `table-recognize-table'."
  1162. :group 'org-table-editing
  1163. :type 'boolean)
  1164. (defgroup org-link nil
  1165. "Options concerning links in Org-mode."
  1166. :tag "Org Link"
  1167. :group 'org)
  1168. (defvar org-link-abbrev-alist-local nil
  1169. "Buffer-local version of `org-link-abbrev-alist', which see.
  1170. The value of this is taken from the #+LINK lines.")
  1171. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1172. (defcustom org-link-abbrev-alist nil
  1173. "Alist of link abbreviations.
  1174. The car of each element is a string, to be replaced at the start of a link.
  1175. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1176. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1177. [[linkkey:tag][description]]
  1178. The 'linkkey' must be a word word, starting with a letter, followed
  1179. by letters, numbers, '-' or '_'.
  1180. If REPLACE is a string, the tag will simply be appended to create the link.
  1181. If the string contains \"%s\", the tag will be inserted there. If the string
  1182. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1183. at that point (see the function `url-hexify-string'). If the string contains
  1184. the specifier \"%(my-function)\", then the custom function `my-function' will
  1185. be invoked: this function takes the tag as its only argument and must return
  1186. a string.
  1187. REPLACE may also be a function that will be called with the tag as the
  1188. only argument to create the link, which should be returned as a string.
  1189. See the manual for examples."
  1190. :group 'org-link
  1191. :type '(repeat
  1192. (cons
  1193. (string :tag "Protocol")
  1194. (choice
  1195. (string :tag "Format")
  1196. (function)))))
  1197. (defcustom org-descriptive-links t
  1198. "Non-nil means Org will display descriptive links.
  1199. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1200. \"Org Website\", hiding the link itself and just displaying its
  1201. description. When set to `nil', Org will display the full links
  1202. literally.
  1203. You can interactively set the value of this variable by calling
  1204. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1205. :group 'org-link
  1206. :type 'boolean)
  1207. (defcustom org-link-file-path-type 'adaptive
  1208. "How the path name in file links should be stored.
  1209. Valid values are:
  1210. relative Relative to the current directory, i.e. the directory of the file
  1211. into which the link is being inserted.
  1212. absolute Absolute path, if possible with ~ for home directory.
  1213. noabbrev Absolute path, no abbreviation of home directory.
  1214. adaptive Use relative path for files in the current directory and sub-
  1215. directories of it. For other files, use an absolute path."
  1216. :group 'org-link
  1217. :type '(choice
  1218. (const relative)
  1219. (const absolute)
  1220. (const noabbrev)
  1221. (const adaptive)))
  1222. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1223. "Types of links that should be activated in Org-mode files.
  1224. This is a list of symbols, each leading to the activation of a certain link
  1225. type. In principle, it does not hurt to turn on most link types - there may
  1226. be a small gain when turning off unused link types. The types are:
  1227. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1228. angle Links in angular brackets that may contain whitespace like
  1229. <bbdb:Carsten Dominik>.
  1230. plain Plain links in normal text, no whitespace, like http://google.com.
  1231. radio Text that is matched by a radio target, see manual for details.
  1232. tag Tag settings in a headline (link to tag search).
  1233. date Time stamps (link to calendar).
  1234. footnote Footnote labels.
  1235. Changing this variable requires a restart of Emacs to become effective."
  1236. :group 'org-link
  1237. :type '(set :greedy t
  1238. (const :tag "Double bracket links" bracket)
  1239. (const :tag "Angular bracket links" angle)
  1240. (const :tag "Plain text links" plain)
  1241. (const :tag "Radio target matches" radio)
  1242. (const :tag "Tags" tag)
  1243. (const :tag "Timestamps" date)
  1244. (const :tag "Footnotes" footnote)))
  1245. (defcustom org-make-link-description-function nil
  1246. "Function to use for generating link descriptions from links.
  1247. When nil, the link location will be used. This function must take
  1248. two parameters: the first one is the link, the second one is the
  1249. description generated by `org-insert-link'. The function should
  1250. return the description to use."
  1251. :group 'org-link
  1252. :type 'function)
  1253. (defgroup org-link-store nil
  1254. "Options concerning storing links in Org-mode."
  1255. :tag "Org Store Link"
  1256. :group 'org-link)
  1257. (defcustom org-url-hexify-p t
  1258. "When non-nil, hexify URL when creating a link."
  1259. :type 'boolean
  1260. :version "24.3"
  1261. :group 'org-link-store)
  1262. (defcustom org-email-link-description-format "Email %c: %.30s"
  1263. "Format of the description part of a link to an email or usenet message.
  1264. The following %-escapes will be replaced by corresponding information:
  1265. %F full \"From\" field
  1266. %f name, taken from \"From\" field, address if no name
  1267. %T full \"To\" field
  1268. %t first name in \"To\" field, address if no name
  1269. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1270. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1271. %s subject
  1272. %d date
  1273. %m message-id.
  1274. You may use normal field width specification between the % and the letter.
  1275. This is for example useful to limit the length of the subject.
  1276. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1277. :group 'org-link-store
  1278. :type 'string)
  1279. (defcustom org-from-is-user-regexp
  1280. (let (r1 r2)
  1281. (when (and user-mail-address (not (string= user-mail-address "")))
  1282. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1283. (when (and user-full-name (not (string= user-full-name "")))
  1284. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1285. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1286. "Regexp matched against the \"From:\" header of an email or usenet message.
  1287. It should match if the message is from the user him/herself."
  1288. :group 'org-link-store
  1289. :type 'regexp)
  1290. (defcustom org-context-in-file-links t
  1291. "Non-nil means file links from `org-store-link' contain context.
  1292. A search string will be added to the file name with :: as separator and
  1293. used to find the context when the link is activated by the command
  1294. `org-open-at-point'. When this option is t, the entire active region
  1295. will be placed in the search string of the file link. If set to a
  1296. positive integer, only the first n lines of context will be stored.
  1297. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1298. negates this setting for the duration of the command."
  1299. :group 'org-link-store
  1300. :type '(choice boolean integer))
  1301. (defcustom org-keep-stored-link-after-insertion nil
  1302. "Non-nil means keep link in list for entire session.
  1303. The command `org-store-link' adds a link pointing to the current
  1304. location to an internal list. These links accumulate during a session.
  1305. The command `org-insert-link' can be used to insert links into any
  1306. Org-mode file (offering completion for all stored links). When this
  1307. option is nil, every link which has been inserted once using \\[org-insert-link]
  1308. will be removed from the list, to make completing the unused links
  1309. more efficient."
  1310. :group 'org-link-store
  1311. :type 'boolean)
  1312. (defgroup org-link-follow nil
  1313. "Options concerning following links in Org-mode."
  1314. :tag "Org Follow Link"
  1315. :group 'org-link)
  1316. (defcustom org-link-translation-function nil
  1317. "Function to translate links with different syntax to Org syntax.
  1318. This can be used to translate links created for example by the Planner
  1319. or emacs-wiki packages to Org syntax.
  1320. The function must accept two parameters, a TYPE containing the link
  1321. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1322. which is everything after the link protocol. It should return a cons
  1323. with possibly modified values of type and path.
  1324. Org contains a function for this, so if you set this variable to
  1325. `org-translate-link-from-planner', you should be able follow many
  1326. links created by planner."
  1327. :group 'org-link-follow
  1328. :type 'function)
  1329. (defcustom org-follow-link-hook nil
  1330. "Hook that is run after a link has been followed."
  1331. :group 'org-link-follow
  1332. :type 'hook)
  1333. (defcustom org-tab-follows-link nil
  1334. "Non-nil means on links TAB will follow the link.
  1335. Needs to be set before org.el is loaded.
  1336. This really should not be used, it does not make sense, and the
  1337. implementation is bad."
  1338. :group 'org-link-follow
  1339. :type 'boolean)
  1340. (defcustom org-return-follows-link nil
  1341. "Non-nil means on links RET will follow the link."
  1342. :group 'org-link-follow
  1343. :type 'boolean)
  1344. (defcustom org-mouse-1-follows-link
  1345. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1346. "Non-nil means mouse-1 on a link will follow the link.
  1347. A longer mouse click will still set point. Does not work on XEmacs.
  1348. Needs to be set before org.el is loaded."
  1349. :group 'org-link-follow
  1350. :type 'boolean)
  1351. (defcustom org-mark-ring-length 4
  1352. "Number of different positions to be recorded in the ring.
  1353. Changing this requires a restart of Emacs to work correctly."
  1354. :group 'org-link-follow
  1355. :type 'integer)
  1356. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1357. "Non-nil means internal links in Org files must exactly match a headline.
  1358. When nil, the link search tries to match a phrase with all words
  1359. in the search text."
  1360. :group 'org-link-follow
  1361. :version "24.1"
  1362. :type '(choice
  1363. (const :tag "Use fuzzy text search" nil)
  1364. (const :tag "Match only exact headline" t)
  1365. (const :tag "Match exact headline or query to create it"
  1366. query-to-create)))
  1367. (defcustom org-link-frame-setup
  1368. '((vm . vm-visit-folder-other-frame)
  1369. (vm-imap . vm-visit-imap-folder-other-frame)
  1370. (gnus . org-gnus-no-new-news)
  1371. (file . find-file-other-window)
  1372. (wl . wl-other-frame))
  1373. "Setup the frame configuration for following links.
  1374. When following a link with Emacs, it may often be useful to display
  1375. this link in another window or frame. This variable can be used to
  1376. set this up for the different types of links.
  1377. For VM, use any of
  1378. `vm-visit-folder'
  1379. `vm-visit-folder-other-window'
  1380. `vm-visit-folder-other-frame'
  1381. For Gnus, use any of
  1382. `gnus'
  1383. `gnus-other-frame'
  1384. `org-gnus-no-new-news'
  1385. For FILE, use any of
  1386. `find-file'
  1387. `find-file-other-window'
  1388. `find-file-other-frame'
  1389. For Wanderlust use any of
  1390. `wl'
  1391. `wl-other-frame'
  1392. For the calendar, use the variable `calendar-setup'.
  1393. For BBDB, it is currently only possible to display the matches in
  1394. another window."
  1395. :group 'org-link-follow
  1396. :type '(list
  1397. (cons (const vm)
  1398. (choice
  1399. (const vm-visit-folder)
  1400. (const vm-visit-folder-other-window)
  1401. (const vm-visit-folder-other-frame)))
  1402. (cons (const gnus)
  1403. (choice
  1404. (const gnus)
  1405. (const gnus-other-frame)
  1406. (const org-gnus-no-new-news)))
  1407. (cons (const file)
  1408. (choice
  1409. (const find-file)
  1410. (const find-file-other-window)
  1411. (const find-file-other-frame)))
  1412. (cons (const wl)
  1413. (choice
  1414. (const wl)
  1415. (const wl-other-frame)))))
  1416. (defcustom org-display-internal-link-with-indirect-buffer nil
  1417. "Non-nil means use indirect buffer to display infile links.
  1418. Activating internal links (from one location in a file to another location
  1419. in the same file) normally just jumps to the location. When the link is
  1420. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1421. is displayed in
  1422. another window. When this option is set, the other window actually displays
  1423. an indirect buffer clone of the current buffer, to avoid any visibility
  1424. changes to the current buffer."
  1425. :group 'org-link-follow
  1426. :type 'boolean)
  1427. (defcustom org-open-non-existing-files nil
  1428. "Non-nil means `org-open-file' will open non-existing files.
  1429. When nil, an error will be generated.
  1430. This variable applies only to external applications because they
  1431. might choke on non-existing files. If the link is to a file that
  1432. will be opened in Emacs, the variable is ignored."
  1433. :group 'org-link-follow
  1434. :type 'boolean)
  1435. (defcustom org-open-directory-means-index-dot-org nil
  1436. "Non-nil means a link to a directory really means to index.org.
  1437. When nil, following a directory link will run dired or open a finder/explorer
  1438. window on that directory."
  1439. :group 'org-link-follow
  1440. :type 'boolean)
  1441. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1442. "Function and arguments to call for following mailto links.
  1443. This is a list with the first element being a Lisp function, and the
  1444. remaining elements being arguments to the function. In string arguments,
  1445. %a will be replaced by the address, and %s will be replaced by the subject
  1446. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1447. :group 'org-link-follow
  1448. :type '(choice
  1449. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1450. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1451. (const :tag "message-mail" (message-mail "%a" "%s"))
  1452. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1453. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1454. "Non-nil means ask for confirmation before executing shell links.
  1455. Shell links can be dangerous: just think about a link
  1456. [[shell:rm -rf ~/*][Google Search]]
  1457. This link would show up in your Org-mode document as \"Google Search\",
  1458. but really it would remove your entire home directory.
  1459. Therefore we advise against setting this variable to nil.
  1460. Just change it to `y-or-n-p' if you want to confirm with a
  1461. single keystroke rather than having to type \"yes\"."
  1462. :group 'org-link-follow
  1463. :type '(choice
  1464. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1465. (const :tag "with y-or-n (faster)" y-or-n-p)
  1466. (const :tag "no confirmation (dangerous)" nil)))
  1467. (put 'org-confirm-shell-link-function
  1468. 'safe-local-variable
  1469. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1470. (defcustom org-confirm-shell-link-not-regexp ""
  1471. "A regexp to skip confirmation for shell links."
  1472. :group 'org-link-follow
  1473. :version "24.1"
  1474. :type 'regexp)
  1475. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1476. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1477. Elisp links can be dangerous: just think about a link
  1478. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1479. This link would show up in your Org-mode document as \"Google Search\",
  1480. but really it would remove your entire home directory.
  1481. Therefore we advise against setting this variable to nil.
  1482. Just change it to `y-or-n-p' if you want to confirm with a
  1483. single keystroke rather than having to type \"yes\"."
  1484. :group 'org-link-follow
  1485. :type '(choice
  1486. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1487. (const :tag "with y-or-n (faster)" y-or-n-p)
  1488. (const :tag "no confirmation (dangerous)" nil)))
  1489. (put 'org-confirm-shell-link-function
  1490. 'safe-local-variable
  1491. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1492. (defcustom org-confirm-elisp-link-not-regexp ""
  1493. "A regexp to skip confirmation for Elisp links."
  1494. :group 'org-link-follow
  1495. :version "24.1"
  1496. :type 'regexp)
  1497. (defconst org-file-apps-defaults-gnu
  1498. '((remote . emacs)
  1499. (system . mailcap)
  1500. (t . mailcap))
  1501. "Default file applications on a UNIX or GNU/Linux system.
  1502. See `org-file-apps'.")
  1503. (defconst org-file-apps-defaults-macosx
  1504. '((remote . emacs)
  1505. (t . "open %s")
  1506. (system . "open %s")
  1507. ("ps.gz" . "gv %s")
  1508. ("eps.gz" . "gv %s")
  1509. ("dvi" . "xdvi %s")
  1510. ("fig" . "xfig %s"))
  1511. "Default file applications on a MacOS X system.
  1512. The system \"open\" is known as a default, but we use X11 applications
  1513. for some files for which the OS does not have a good default.
  1514. See `org-file-apps'.")
  1515. (defconst org-file-apps-defaults-windowsnt
  1516. (list
  1517. '(remote . emacs)
  1518. (cons t
  1519. (list (if (featurep 'xemacs)
  1520. 'mswindows-shell-execute
  1521. 'w32-shell-execute)
  1522. "open" 'file))
  1523. (cons 'system
  1524. (list (if (featurep 'xemacs)
  1525. 'mswindows-shell-execute
  1526. 'w32-shell-execute)
  1527. "open" 'file)))
  1528. "Default file applications on a Windows NT system.
  1529. The system \"open\" is used for most files.
  1530. See `org-file-apps'.")
  1531. (defcustom org-file-apps
  1532. '(
  1533. (auto-mode . emacs)
  1534. ("\\.mm\\'" . default)
  1535. ("\\.x?html?\\'" . default)
  1536. ("\\.pdf\\'" . default)
  1537. )
  1538. "External applications for opening `file:path' items in a document.
  1539. Org-mode uses system defaults for different file types, but
  1540. you can use this variable to set the application for a given file
  1541. extension. The entries in this list are cons cells where the car identifies
  1542. files and the cdr the corresponding command. Possible values for the
  1543. file identifier are
  1544. \"string\" A string as a file identifier can be interpreted in different
  1545. ways, depending on its contents:
  1546. - Alphanumeric characters only:
  1547. Match links with this file extension.
  1548. Example: (\"pdf\" . \"evince %s\")
  1549. to open PDFs with evince.
  1550. - Regular expression: Match links where the
  1551. filename matches the regexp. If you want to
  1552. use groups here, use shy groups.
  1553. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1554. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1555. to open *.html and *.xhtml with firefox.
  1556. - Regular expression which contains (non-shy) groups:
  1557. Match links where the whole link, including \"::\", and
  1558. anything after that, matches the regexp.
  1559. In a custom command string, %1, %2, etc. are replaced with
  1560. the parts of the link that were matched by the groups.
  1561. For backwards compatibility, if a command string is given
  1562. that does not use any of the group matches, this case is
  1563. handled identically to the second one (i.e. match against
  1564. file name only).
  1565. In a custom lisp form, you can access the group matches with
  1566. (match-string n link).
  1567. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1568. to open [[file:document.pdf::5]] with evince at page 5.
  1569. `directory' Matches a directory
  1570. `remote' Matches a remote file, accessible through tramp or efs.
  1571. Remote files most likely should be visited through Emacs
  1572. because external applications cannot handle such paths.
  1573. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1574. so all files Emacs knows how to handle. Using this with
  1575. command `emacs' will open most files in Emacs. Beware that this
  1576. will also open html files inside Emacs, unless you add
  1577. (\"html\" . default) to the list as well.
  1578. t Default for files not matched by any of the other options.
  1579. `system' The system command to open files, like `open' on Windows
  1580. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1581. that will be selected if you call `C-c C-o' with a double
  1582. \\[universal-argument] \\[universal-argument] prefix.
  1583. Possible values for the command are:
  1584. `emacs' The file will be visited by the current Emacs process.
  1585. `default' Use the default application for this file type, which is the
  1586. association for t in the list, most likely in the system-specific
  1587. part.
  1588. This can be used to overrule an unwanted setting in the
  1589. system-specific variable.
  1590. `system' Use the system command for opening files, like \"open\".
  1591. This command is specified by the entry whose car is `system'.
  1592. Most likely, the system-specific version of this variable
  1593. does define this command, but you can overrule/replace it
  1594. here.
  1595. string A command to be executed by a shell; %s will be replaced
  1596. by the path to the file.
  1597. sexp A Lisp form which will be evaluated. The file path will
  1598. be available in the Lisp variable `file'.
  1599. For more examples, see the system specific constants
  1600. `org-file-apps-defaults-macosx'
  1601. `org-file-apps-defaults-windowsnt'
  1602. `org-file-apps-defaults-gnu'."
  1603. :group 'org-link-follow
  1604. :type '(repeat
  1605. (cons (choice :value ""
  1606. (string :tag "Extension")
  1607. (const :tag "System command to open files" system)
  1608. (const :tag "Default for unrecognized files" t)
  1609. (const :tag "Remote file" remote)
  1610. (const :tag "Links to a directory" directory)
  1611. (const :tag "Any files that have Emacs modes"
  1612. auto-mode))
  1613. (choice :value ""
  1614. (const :tag "Visit with Emacs" emacs)
  1615. (const :tag "Use default" default)
  1616. (const :tag "Use the system command" system)
  1617. (string :tag "Command")
  1618. (sexp :tag "Lisp form")))))
  1619. (defcustom org-doi-server-url "http://dx.doi.org/"
  1620. "The URL of the DOI server."
  1621. :type 'string
  1622. :version "24.3"
  1623. :group 'org-link-follow)
  1624. (defgroup org-refile nil
  1625. "Options concerning refiling entries in Org-mode."
  1626. :tag "Org Refile"
  1627. :group 'org)
  1628. (defcustom org-directory "~/org"
  1629. "Directory with org files.
  1630. This is just a default location to look for Org files. There is no need
  1631. at all to put your files into this directory. It is only used in the
  1632. following situations:
  1633. 1. When a capture template specifies a target file that is not an
  1634. absolute path. The path will then be interpreted relative to
  1635. `org-directory'
  1636. 2. When a capture note is filed away in an interactive way (when exiting the
  1637. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1638. with `org-directory' as the default path."
  1639. :group 'org-refile
  1640. :group 'org-remember
  1641. :group 'org-capture
  1642. :type 'directory)
  1643. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1644. "Default target for storing notes.
  1645. Used as a fall back file for org-remember.el and org-capture.el, for
  1646. templates that do not specify a target file."
  1647. :group 'org-refile
  1648. :group 'org-remember
  1649. :group 'org-capture
  1650. :type '(choice
  1651. (const :tag "Default from remember-data-file" nil)
  1652. file))
  1653. (defcustom org-goto-interface 'outline
  1654. "The default interface to be used for `org-goto'.
  1655. Allowed values are:
  1656. outline The interface shows an outline of the relevant file
  1657. and the correct heading is found by moving through
  1658. the outline or by searching with incremental search.
  1659. outline-path-completion Headlines in the current buffer are offered via
  1660. completion. This is the interface also used by
  1661. the refile command."
  1662. :group 'org-refile
  1663. :type '(choice
  1664. (const :tag "Outline" outline)
  1665. (const :tag "Outline-path-completion" outline-path-completion)))
  1666. (defcustom org-goto-max-level 5
  1667. "Maximum target level when running `org-goto' with refile interface."
  1668. :group 'org-refile
  1669. :type 'integer)
  1670. (defcustom org-reverse-note-order nil
  1671. "Non-nil means store new notes at the beginning of a file or entry.
  1672. When nil, new notes will be filed to the end of a file or entry.
  1673. This can also be a list with cons cells of regular expressions that
  1674. are matched against file names, and values."
  1675. :group 'org-remember
  1676. :group 'org-capture
  1677. :group 'org-refile
  1678. :type '(choice
  1679. (const :tag "Reverse always" t)
  1680. (const :tag "Reverse never" nil)
  1681. (repeat :tag "By file name regexp"
  1682. (cons regexp boolean))))
  1683. (defcustom org-log-refile nil
  1684. "Information to record when a task is refiled.
  1685. Possible values are:
  1686. nil Don't add anything
  1687. time Add a time stamp to the task
  1688. note Prompt for a note and add it with template `org-log-note-headings'
  1689. This option can also be set with on a per-file-basis with
  1690. #+STARTUP: nologrefile
  1691. #+STARTUP: logrefile
  1692. #+STARTUP: lognoterefile
  1693. You can have local logging settings for a subtree by setting the LOGGING
  1694. property to one or more of these keywords.
  1695. When bulk-refiling from the agenda, the value `note' is forbidden and
  1696. will temporarily be changed to `time'."
  1697. :group 'org-refile
  1698. :group 'org-progress
  1699. :version "24.1"
  1700. :type '(choice
  1701. (const :tag "No logging" nil)
  1702. (const :tag "Record timestamp" time)
  1703. (const :tag "Record timestamp with note." note)))
  1704. (defcustom org-refile-targets nil
  1705. "Targets for refiling entries with \\[org-refile].
  1706. This is a list of cons cells. Each cell contains:
  1707. - a specification of the files to be considered, either a list of files,
  1708. or a symbol whose function or variable value will be used to retrieve
  1709. a file name or a list of file names. If you use `org-agenda-files' for
  1710. that, all agenda files will be scanned for targets. Nil means consider
  1711. headings in the current buffer.
  1712. - A specification of how to find candidate refile targets. This may be
  1713. any of:
  1714. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1715. This tag has to be present in all target headlines, inheritance will
  1716. not be considered.
  1717. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1718. todo keyword.
  1719. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1720. headlines that are refiling targets.
  1721. - a cons cell (:level . N). Any headline of level N is considered a target.
  1722. Note that, when `org-odd-levels-only' is set, level corresponds to
  1723. order in hierarchy, not to the number of stars.
  1724. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1725. Note that, when `org-odd-levels-only' is set, level corresponds to
  1726. order in hierarchy, not to the number of stars.
  1727. Each element of this list generates a set of possible targets.
  1728. The union of these sets is presented (with completion) to
  1729. the user by `org-refile'.
  1730. You can set the variable `org-refile-target-verify-function' to a function
  1731. to verify each headline found by the simple criteria above.
  1732. When this variable is nil, all top-level headlines in the current buffer
  1733. are used, equivalent to the value `((nil . (:level . 1))'."
  1734. :group 'org-refile
  1735. :type '(repeat
  1736. (cons
  1737. (choice :value org-agenda-files
  1738. (const :tag "All agenda files" org-agenda-files)
  1739. (const :tag "Current buffer" nil)
  1740. (function) (variable) (file))
  1741. (choice :tag "Identify target headline by"
  1742. (cons :tag "Specific tag" (const :value :tag) (string))
  1743. (cons :tag "TODO keyword" (const :value :todo) (string))
  1744. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1745. (cons :tag "Level number" (const :value :level) (integer))
  1746. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1747. (defcustom org-refile-target-verify-function nil
  1748. "Function to verify if the headline at point should be a refile target.
  1749. The function will be called without arguments, with point at the
  1750. beginning of the headline. It should return t and leave point
  1751. where it is if the headline is a valid target for refiling.
  1752. If the target should not be selected, the function must return nil.
  1753. In addition to this, it may move point to a place from where the search
  1754. should be continued. For example, the function may decide that the entire
  1755. subtree of the current entry should be excluded and move point to the end
  1756. of the subtree."
  1757. :group 'org-refile
  1758. :type 'function)
  1759. (defcustom org-refile-use-cache nil
  1760. "Non-nil means cache refile targets to speed up the process.
  1761. The cache for a particular file will be updated automatically when
  1762. the buffer has been killed, or when any of the marker used for flagging
  1763. refile targets no longer points at a live buffer.
  1764. If you have added new entries to a buffer that might themselves be targets,
  1765. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1766. find that easier, `C-u C-u C-u C-c C-w'."
  1767. :group 'org-refile
  1768. :version "24.1"
  1769. :type 'boolean)
  1770. (defcustom org-refile-use-outline-path nil
  1771. "Non-nil means provide refile targets as paths.
  1772. So a level 3 headline will be available as level1/level2/level3.
  1773. When the value is `file', also include the file name (without directory)
  1774. into the path. In this case, you can also stop the completion after
  1775. the file name, to get entries inserted as top level in the file.
  1776. When `full-file-path', include the full file path."
  1777. :group 'org-refile
  1778. :type '(choice
  1779. (const :tag "Not" nil)
  1780. (const :tag "Yes" t)
  1781. (const :tag "Start with file name" file)
  1782. (const :tag "Start with full file path" full-file-path)))
  1783. (defcustom org-outline-path-complete-in-steps t
  1784. "Non-nil means complete the outline path in hierarchical steps.
  1785. When Org-mode uses the refile interface to select an outline path
  1786. \(see variable `org-refile-use-outline-path'), the completion of
  1787. the path can be done is a single go, or if can be done in steps down
  1788. the headline hierarchy. Going in steps is probably the best if you
  1789. do not use a special completion package like `ido' or `icicles'.
  1790. However, when using these packages, going in one step can be very
  1791. fast, while still showing the whole path to the entry."
  1792. :group 'org-refile
  1793. :type 'boolean)
  1794. (defcustom org-refile-allow-creating-parent-nodes nil
  1795. "Non-nil means allow to create new nodes as refile targets.
  1796. New nodes are then created by adding \"/new node name\" to the completion
  1797. of an existing node. When the value of this variable is `confirm',
  1798. new node creation must be confirmed by the user (recommended)
  1799. When nil, the completion must match an existing entry.
  1800. Note that, if the new heading is not seen by the criteria
  1801. listed in `org-refile-targets', multiple instances of the same
  1802. heading would be created by trying again to file under the new
  1803. heading."
  1804. :group 'org-refile
  1805. :type '(choice
  1806. (const :tag "Never" nil)
  1807. (const :tag "Always" t)
  1808. (const :tag "Prompt for confirmation" confirm)))
  1809. (defcustom org-refile-active-region-within-subtree nil
  1810. "Non-nil means also refile active region within a subtree.
  1811. By default `org-refile' doesn't allow refiling regions if they
  1812. don't contain a set of subtrees, but it might be convenient to
  1813. do so sometimes: in that case, the first line of the region is
  1814. converted to a headline before refiling."
  1815. :group 'org-refile
  1816. :version "24.1"
  1817. :type 'boolean)
  1818. (defgroup org-todo nil
  1819. "Options concerning TODO items in Org-mode."
  1820. :tag "Org TODO"
  1821. :group 'org)
  1822. (defgroup org-progress nil
  1823. "Options concerning Progress logging in Org-mode."
  1824. :tag "Org Progress"
  1825. :group 'org-time)
  1826. (defvar org-todo-interpretation-widgets
  1827. '((:tag "Sequence (cycling hits every state)" sequence)
  1828. (:tag "Type (cycling directly to DONE)" type))
  1829. "The available interpretation symbols for customizing `org-todo-keywords'.
  1830. Interested libraries should add to this list.")
  1831. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1832. "List of TODO entry keyword sequences and their interpretation.
  1833. \\<org-mode-map>This is a list of sequences.
  1834. Each sequence starts with a symbol, either `sequence' or `type',
  1835. indicating if the keywords should be interpreted as a sequence of
  1836. action steps, or as different types of TODO items. The first
  1837. keywords are states requiring action - these states will select a headline
  1838. for inclusion into the global TODO list Org-mode produces. If one of
  1839. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1840. signify that no further action is necessary. If \"|\" is not found,
  1841. the last keyword is treated as the only DONE state of the sequence.
  1842. The command \\[org-todo] cycles an entry through these states, and one
  1843. additional state where no keyword is present. For details about this
  1844. cycling, see the manual.
  1845. TODO keywords and interpretation can also be set on a per-file basis with
  1846. the special #+SEQ_TODO and #+TYP_TODO lines.
  1847. Each keyword can optionally specify a character for fast state selection
  1848. \(in combination with the variable `org-use-fast-todo-selection')
  1849. and specifiers for state change logging, using the same syntax that
  1850. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1851. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1852. indicates to record a time stamp each time this state is selected.
  1853. Each keyword may also specify if a timestamp or a note should be
  1854. recorded when entering or leaving the state, by adding additional
  1855. characters in the parenthesis after the keyword. This looks like this:
  1856. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1857. record only the time of the state change. With X and Y being either
  1858. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1859. Y when leaving the state if and only if the *target* state does not
  1860. define X. You may omit any of the fast-selection key or X or /Y,
  1861. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1862. For backward compatibility, this variable may also be just a list
  1863. of keywords. In this case the interpretation (sequence or type) will be
  1864. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1865. :group 'org-todo
  1866. :group 'org-keywords
  1867. :type '(choice
  1868. (repeat :tag "Old syntax, just keywords"
  1869. (string :tag "Keyword"))
  1870. (repeat :tag "New syntax"
  1871. (cons
  1872. (choice
  1873. :tag "Interpretation"
  1874. ;;Quick and dirty way to see
  1875. ;;`org-todo-interpretations'. This takes the
  1876. ;;place of item arguments
  1877. :convert-widget
  1878. (lambda (widget)
  1879. (widget-put widget
  1880. :args (mapcar
  1881. #'(lambda (x)
  1882. (widget-convert
  1883. (cons 'const x)))
  1884. org-todo-interpretation-widgets))
  1885. widget))
  1886. (repeat
  1887. (string :tag "Keyword"))))))
  1888. (defvar org-todo-keywords-1 nil
  1889. "All TODO and DONE keywords active in a buffer.")
  1890. (make-variable-buffer-local 'org-todo-keywords-1)
  1891. (defvar org-todo-keywords-for-agenda nil)
  1892. (defvar org-done-keywords-for-agenda nil)
  1893. (defvar org-drawers-for-agenda nil)
  1894. (defvar org-todo-keyword-alist-for-agenda nil)
  1895. (defvar org-tag-alist-for-agenda nil)
  1896. (defvar org-agenda-contributing-files nil)
  1897. (defvar org-not-done-keywords nil)
  1898. (make-variable-buffer-local 'org-not-done-keywords)
  1899. (defvar org-done-keywords nil)
  1900. (make-variable-buffer-local 'org-done-keywords)
  1901. (defvar org-todo-heads nil)
  1902. (make-variable-buffer-local 'org-todo-heads)
  1903. (defvar org-todo-sets nil)
  1904. (make-variable-buffer-local 'org-todo-sets)
  1905. (defvar org-todo-log-states nil)
  1906. (make-variable-buffer-local 'org-todo-log-states)
  1907. (defvar org-todo-kwd-alist nil)
  1908. (make-variable-buffer-local 'org-todo-kwd-alist)
  1909. (defvar org-todo-key-alist nil)
  1910. (make-variable-buffer-local 'org-todo-key-alist)
  1911. (defvar org-todo-key-trigger nil)
  1912. (make-variable-buffer-local 'org-todo-key-trigger)
  1913. (defcustom org-todo-interpretation 'sequence
  1914. "Controls how TODO keywords are interpreted.
  1915. This variable is in principle obsolete and is only used for
  1916. backward compatibility, if the interpretation of todo keywords is
  1917. not given already in `org-todo-keywords'. See that variable for
  1918. more information."
  1919. :group 'org-todo
  1920. :group 'org-keywords
  1921. :type '(choice (const sequence)
  1922. (const type)))
  1923. (defcustom org-use-fast-todo-selection t
  1924. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1925. This variable describes if and under what circumstances the cycling
  1926. mechanism for TODO keywords will be replaced by a single-key, direct
  1927. selection scheme.
  1928. When nil, fast selection is never used.
  1929. When the symbol `prefix', it will be used when `org-todo' is called
  1930. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  1931. `C-u t' in an agenda buffer.
  1932. When t, fast selection is used by default. In this case, the prefix
  1933. argument forces cycling instead.
  1934. In all cases, the special interface is only used if access keys have
  1935. actually been assigned by the user, i.e. if keywords in the configuration
  1936. are followed by a letter in parenthesis, like TODO(t)."
  1937. :group 'org-todo
  1938. :type '(choice
  1939. (const :tag "Never" nil)
  1940. (const :tag "By default" t)
  1941. (const :tag "Only with C-u C-c C-t" prefix)))
  1942. (defcustom org-provide-todo-statistics t
  1943. "Non-nil means update todo statistics after insert and toggle.
  1944. ALL-HEADLINES means update todo statistics by including headlines
  1945. with no TODO keyword as well, counting them as not done.
  1946. A list of TODO keywords means the same, but skip keywords that are
  1947. not in this list.
  1948. When this is set, todo statistics is updated in the parent of the
  1949. current entry each time a todo state is changed."
  1950. :group 'org-todo
  1951. :type '(choice
  1952. (const :tag "Yes, only for TODO entries" t)
  1953. (const :tag "Yes, including all entries" 'all-headlines)
  1954. (repeat :tag "Yes, for TODOs in this list"
  1955. (string :tag "TODO keyword"))
  1956. (other :tag "No TODO statistics" nil)))
  1957. (defcustom org-hierarchical-todo-statistics t
  1958. "Non-nil means TODO statistics covers just direct children.
  1959. When nil, all entries in the subtree are considered.
  1960. This has only an effect if `org-provide-todo-statistics' is set.
  1961. To set this to nil for only a single subtree, use a COOKIE_DATA
  1962. property and include the word \"recursive\" into the value."
  1963. :group 'org-todo
  1964. :type 'boolean)
  1965. (defcustom org-after-todo-state-change-hook nil
  1966. "Hook which is run after the state of a TODO item was changed.
  1967. The new state (a string with a TODO keyword, or nil) is available in the
  1968. Lisp variable `org-state'."
  1969. :group 'org-todo
  1970. :type 'hook)
  1971. (defvar org-blocker-hook nil
  1972. "Hook for functions that are allowed to block a state change.
  1973. Functions in this hook should not modify the buffer.
  1974. Each function gets as its single argument a property list,
  1975. see `org-trigger-hook' for more information about this list.
  1976. If any of the functions in this hook returns nil, the state change
  1977. is blocked.")
  1978. (defvar org-trigger-hook nil
  1979. "Hook for functions that are triggered by a state change.
  1980. Each function gets as its single argument a property list with at
  1981. least the following elements:
  1982. (:type type-of-change :position pos-at-entry-start
  1983. :from old-state :to new-state)
  1984. Depending on the type, more properties may be present.
  1985. This mechanism is currently implemented for:
  1986. TODO state changes
  1987. ------------------
  1988. :type todo-state-change
  1989. :from previous state (keyword as a string), or nil, or a symbol
  1990. 'todo' or 'done', to indicate the general type of state.
  1991. :to new state, like in :from")
  1992. (defcustom org-enforce-todo-dependencies nil
  1993. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1994. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1995. be blocked if any prior sibling is not yet done.
  1996. Finally, if the parent is blocked because of ordered siblings of its own,
  1997. the child will also be blocked."
  1998. :set (lambda (var val)
  1999. (set var val)
  2000. (if val
  2001. (add-hook 'org-blocker-hook
  2002. 'org-block-todo-from-children-or-siblings-or-parent)
  2003. (remove-hook 'org-blocker-hook
  2004. 'org-block-todo-from-children-or-siblings-or-parent)))
  2005. :group 'org-todo
  2006. :type 'boolean)
  2007. (defcustom org-enforce-todo-checkbox-dependencies nil
  2008. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2009. When this is nil, checkboxes have no influence on switching TODO states.
  2010. When non-nil, you first need to check off all check boxes before the TODO
  2011. entry can be switched to DONE.
  2012. This variable needs to be set before org.el is loaded, and you need to
  2013. restart Emacs after a change to make the change effective. The only way
  2014. to change is while Emacs is running is through the customize interface."
  2015. :set (lambda (var val)
  2016. (set var val)
  2017. (if val
  2018. (add-hook 'org-blocker-hook
  2019. 'org-block-todo-from-checkboxes)
  2020. (remove-hook 'org-blocker-hook
  2021. 'org-block-todo-from-checkboxes)))
  2022. :group 'org-todo
  2023. :type 'boolean)
  2024. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2025. "Non-nil means inserting a TODO heading is treated as state change.
  2026. So when the command \\[org-insert-todo-heading] is used, state change
  2027. logging will apply if appropriate. When nil, the new TODO item will
  2028. be inserted directly, and no logging will take place."
  2029. :group 'org-todo
  2030. :type 'boolean)
  2031. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2032. "Non-nil means switching TODO states with S-cursor counts as state change.
  2033. This is the default behavior. However, setting this to nil allows a
  2034. convenient way to select a TODO state and bypass any logging associated
  2035. with that."
  2036. :group 'org-todo
  2037. :type 'boolean)
  2038. (defcustom org-todo-state-tags-triggers nil
  2039. "Tag changes that should be triggered by TODO state changes.
  2040. This is a list. Each entry is
  2041. (state-change (tag . flag) .......)
  2042. State-change can be a string with a state, and empty string to indicate the
  2043. state that has no TODO keyword, or it can be one of the symbols `todo'
  2044. or `done', meaning any not-done or done state, respectively."
  2045. :group 'org-todo
  2046. :group 'org-tags
  2047. :type '(repeat
  2048. (cons (choice :tag "When changing to"
  2049. (const :tag "Not-done state" todo)
  2050. (const :tag "Done state" done)
  2051. (string :tag "State"))
  2052. (repeat
  2053. (cons :tag "Tag action"
  2054. (string :tag "Tag")
  2055. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2056. (defcustom org-log-done nil
  2057. "Information to record when a task moves to the DONE state.
  2058. Possible values are:
  2059. nil Don't add anything, just change the keyword
  2060. time Add a time stamp to the task
  2061. note Prompt for a note and add it with template `org-log-note-headings'
  2062. This option can also be set with on a per-file-basis with
  2063. #+STARTUP: nologdone
  2064. #+STARTUP: logdone
  2065. #+STARTUP: lognotedone
  2066. You can have local logging settings for a subtree by setting the LOGGING
  2067. property to one or more of these keywords."
  2068. :group 'org-todo
  2069. :group 'org-progress
  2070. :type '(choice
  2071. (const :tag "No logging" nil)
  2072. (const :tag "Record CLOSED timestamp" time)
  2073. (const :tag "Record CLOSED timestamp with note." note)))
  2074. ;; Normalize old uses of org-log-done.
  2075. (cond
  2076. ((eq org-log-done t) (setq org-log-done 'time))
  2077. ((and (listp org-log-done) (memq 'done org-log-done))
  2078. (setq org-log-done 'note)))
  2079. (defcustom org-log-reschedule nil
  2080. "Information to record when the scheduling date of a tasks is modified.
  2081. Possible values are:
  2082. nil Don't add anything, just change the date
  2083. time Add a time stamp to the task
  2084. note Prompt for a note and add it with template `org-log-note-headings'
  2085. This option can also be set with on a per-file-basis with
  2086. #+STARTUP: nologreschedule
  2087. #+STARTUP: logreschedule
  2088. #+STARTUP: lognotereschedule"
  2089. :group 'org-todo
  2090. :group 'org-progress
  2091. :type '(choice
  2092. (const :tag "No logging" nil)
  2093. (const :tag "Record timestamp" time)
  2094. (const :tag "Record timestamp with note." note)))
  2095. (defcustom org-log-redeadline nil
  2096. "Information to record when the deadline date of a tasks is modified.
  2097. Possible values are:
  2098. nil Don't add anything, just change the date
  2099. time Add a time stamp to the task
  2100. note Prompt for a note and add it with template `org-log-note-headings'
  2101. This option can also be set with on a per-file-basis with
  2102. #+STARTUP: nologredeadline
  2103. #+STARTUP: logredeadline
  2104. #+STARTUP: lognoteredeadline
  2105. You can have local logging settings for a subtree by setting the LOGGING
  2106. property to one or more of these keywords."
  2107. :group 'org-todo
  2108. :group 'org-progress
  2109. :type '(choice
  2110. (const :tag "No logging" nil)
  2111. (const :tag "Record timestamp" time)
  2112. (const :tag "Record timestamp with note." note)))
  2113. (defcustom org-log-note-clock-out nil
  2114. "Non-nil means record a note when clocking out of an item.
  2115. This can also be configured on a per-file basis by adding one of
  2116. the following lines anywhere in the buffer:
  2117. #+STARTUP: lognoteclock-out
  2118. #+STARTUP: nolognoteclock-out"
  2119. :group 'org-todo
  2120. :group 'org-progress
  2121. :type 'boolean)
  2122. (defcustom org-log-done-with-time t
  2123. "Non-nil means the CLOSED time stamp will contain date and time.
  2124. When nil, only the date will be recorded."
  2125. :group 'org-progress
  2126. :type 'boolean)
  2127. (defcustom org-log-note-headings
  2128. '((done . "CLOSING NOTE %t")
  2129. (state . "State %-12s from %-12S %t")
  2130. (note . "Note taken on %t")
  2131. (reschedule . "Rescheduled from %S on %t")
  2132. (delschedule . "Not scheduled, was %S on %t")
  2133. (redeadline . "New deadline from %S on %t")
  2134. (deldeadline . "Removed deadline, was %S on %t")
  2135. (refile . "Refiled on %t")
  2136. (clock-out . ""))
  2137. "Headings for notes added to entries.
  2138. The value is an alist, with the car being a symbol indicating the note
  2139. context, and the cdr is the heading to be used. The heading may also be the
  2140. empty string.
  2141. %t in the heading will be replaced by a time stamp.
  2142. %T will be an active time stamp instead the default inactive one
  2143. %d will be replaced by a short-format time stamp.
  2144. %D will be replaced by an active short-format time stamp.
  2145. %s will be replaced by the new TODO state, in double quotes.
  2146. %S will be replaced by the old TODO state, in double quotes.
  2147. %u will be replaced by the user name.
  2148. %U will be replaced by the full user name.
  2149. In fact, it is not a good idea to change the `state' entry, because
  2150. agenda log mode depends on the format of these entries."
  2151. :group 'org-todo
  2152. :group 'org-progress
  2153. :type '(list :greedy t
  2154. (cons (const :tag "Heading when closing an item" done) string)
  2155. (cons (const :tag
  2156. "Heading when changing todo state (todo sequence only)"
  2157. state) string)
  2158. (cons (const :tag "Heading when just taking a note" note) string)
  2159. (cons (const :tag "Heading when clocking out" clock-out) string)
  2160. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2161. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2162. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2163. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2164. (cons (const :tag "Heading when refiling" refile) string)))
  2165. (unless (assq 'note org-log-note-headings)
  2166. (push '(note . "%t") org-log-note-headings))
  2167. (defcustom org-log-into-drawer nil
  2168. "Non-nil means insert state change notes and time stamps into a drawer.
  2169. When nil, state changes notes will be inserted after the headline and
  2170. any scheduling and clock lines, but not inside a drawer.
  2171. The value of this variable should be the name of the drawer to use.
  2172. LOGBOOK is proposed as the default drawer for this purpose, you can
  2173. also set this to a string to define the drawer of your choice.
  2174. A value of t is also allowed, representing \"LOGBOOK\".
  2175. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2176. will be ignored.
  2177. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2178. a subtree."
  2179. :group 'org-todo
  2180. :group 'org-progress
  2181. :type '(choice
  2182. (const :tag "Not into a drawer" nil)
  2183. (const :tag "LOGBOOK" t)
  2184. (string :tag "Other")))
  2185. (if (fboundp 'defvaralias)
  2186. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2187. (defun org-log-into-drawer ()
  2188. "Return the value of `org-log-into-drawer', but let properties overrule.
  2189. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2190. used instead of the default value."
  2191. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2192. (cond
  2193. ((not p) org-log-into-drawer)
  2194. ((equal p "nil") nil)
  2195. ((equal p "t") "LOGBOOK")
  2196. (t p))))
  2197. (defcustom org-log-state-notes-insert-after-drawers nil
  2198. "Non-nil means insert state change notes after any drawers in entry.
  2199. Only the drawers that *immediately* follow the headline and the
  2200. deadline/scheduled line are skipped.
  2201. When nil, insert notes right after the heading and perhaps the line
  2202. with deadline/scheduling if present.
  2203. This variable will have no effect if `org-log-into-drawer' is
  2204. set."
  2205. :group 'org-todo
  2206. :group 'org-progress
  2207. :type 'boolean)
  2208. (defcustom org-log-states-order-reversed t
  2209. "Non-nil means the latest state note will be directly after heading.
  2210. When nil, the state change notes will be ordered according to time."
  2211. :group 'org-todo
  2212. :group 'org-progress
  2213. :type 'boolean)
  2214. (defcustom org-todo-repeat-to-state nil
  2215. "The TODO state to which a repeater should return the repeating task.
  2216. By default this is the first task in a TODO sequence, or the previous state
  2217. in a TODO_TYP set. But you can specify another task here.
  2218. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2219. :group 'org-todo
  2220. :version "24.1"
  2221. :type '(choice (const :tag "Head of sequence" nil)
  2222. (string :tag "Specific state")))
  2223. (defcustom org-log-repeat 'time
  2224. "Non-nil means record moving through the DONE state when triggering repeat.
  2225. An auto-repeating task is immediately switched back to TODO when
  2226. marked DONE. If you are not logging state changes (by adding \"@\"
  2227. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2228. record a closing note, there will be no record of the task moving
  2229. through DONE. This variable forces taking a note anyway.
  2230. nil Don't force a record
  2231. time Record a time stamp
  2232. note Prompt for a note and add it with template `org-log-note-headings'
  2233. This option can also be set with on a per-file-basis with
  2234. #+STARTUP: nologrepeat
  2235. #+STARTUP: logrepeat
  2236. #+STARTUP: lognoterepeat
  2237. You can have local logging settings for a subtree by setting the LOGGING
  2238. property to one or more of these keywords."
  2239. :group 'org-todo
  2240. :group 'org-progress
  2241. :type '(choice
  2242. (const :tag "Don't force a record" nil)
  2243. (const :tag "Force recording the DONE state" time)
  2244. (const :tag "Force recording a note with the DONE state" note)))
  2245. (defgroup org-priorities nil
  2246. "Priorities in Org-mode."
  2247. :tag "Org Priorities"
  2248. :group 'org-todo)
  2249. (defcustom org-enable-priority-commands t
  2250. "Non-nil means priority commands are active.
  2251. When nil, these commands will be disabled, so that you never accidentally
  2252. set a priority."
  2253. :group 'org-priorities
  2254. :type 'boolean)
  2255. (defcustom org-highest-priority ?A
  2256. "The highest priority of TODO items. A character like ?A, ?B etc.
  2257. Must have a smaller ASCII number than `org-lowest-priority'."
  2258. :group 'org-priorities
  2259. :type 'character)
  2260. (defcustom org-lowest-priority ?C
  2261. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2262. Must have a larger ASCII number than `org-highest-priority'."
  2263. :group 'org-priorities
  2264. :type 'character)
  2265. (defcustom org-default-priority ?B
  2266. "The default priority of TODO items.
  2267. This is the priority an item gets if no explicit priority is given.
  2268. When starting to cycle on an empty priority the first step in the cycle
  2269. depends on `org-priority-start-cycle-with-default'. The resulting first
  2270. step priority must not exceed the range from `org-highest-priority' to
  2271. `org-lowest-priority' which means that `org-default-priority' has to be
  2272. in this range exclusive or inclusive the range boundaries. Else the
  2273. first step refuses to set the default and the second will fall back
  2274. to (depending on the command used) the highest or lowest priority."
  2275. :group 'org-priorities
  2276. :type 'character)
  2277. (defcustom org-priority-start-cycle-with-default t
  2278. "Non-nil means start with default priority when starting to cycle.
  2279. When this is nil, the first step in the cycle will be (depending on the
  2280. command used) one higher or lower than the default priority.
  2281. See also `org-default-priority'."
  2282. :group 'org-priorities
  2283. :type 'boolean)
  2284. (defcustom org-get-priority-function nil
  2285. "Function to extract the priority from a string.
  2286. The string is normally the headline. If this is nil Org computes the
  2287. priority from the priority cookie like [#A] in the headline. It returns
  2288. an integer, increasing by 1000 for each priority level.
  2289. The user can set a different function here, which should take a string
  2290. as an argument and return the numeric priority."
  2291. :group 'org-priorities
  2292. :version "24.1"
  2293. :type 'function)
  2294. (defgroup org-time nil
  2295. "Options concerning time stamps and deadlines in Org-mode."
  2296. :tag "Org Time"
  2297. :group 'org)
  2298. (defcustom org-insert-labeled-timestamps-at-point nil
  2299. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2300. When nil, these labeled time stamps are forces into the second line of an
  2301. entry, just after the headline. When scheduling from the global TODO list,
  2302. the time stamp will always be forced into the second line."
  2303. :group 'org-time
  2304. :type 'boolean)
  2305. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2306. "Formats for `format-time-string' which are used for time stamps.
  2307. It is not recommended to change this constant.")
  2308. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2309. "Number of minutes to round time stamps to.
  2310. These are two values, the first applies when first creating a time stamp.
  2311. The second applies when changing it with the commands `S-up' and `S-down'.
  2312. When changing the time stamp, this means that it will change in steps
  2313. of N minutes, as given by the second value.
  2314. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2315. numbers should be factors of 60, so for example 5, 10, 15.
  2316. When this is larger than 1, you can still force an exact time stamp by using
  2317. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2318. and by using a prefix arg to `S-up/down' to specify the exact number
  2319. of minutes to shift."
  2320. :group 'org-time
  2321. :get #'(lambda (var) ; Make sure both elements are there
  2322. (if (integerp (default-value var))
  2323. (list (default-value var) 5)
  2324. (default-value var)))
  2325. :type '(list
  2326. (integer :tag "when inserting times")
  2327. (integer :tag "when modifying times")))
  2328. ;; Normalize old customizations of this variable.
  2329. (when (integerp org-time-stamp-rounding-minutes)
  2330. (setq org-time-stamp-rounding-minutes
  2331. (list org-time-stamp-rounding-minutes
  2332. org-time-stamp-rounding-minutes)))
  2333. (defcustom org-display-custom-times nil
  2334. "Non-nil means overlay custom formats over all time stamps.
  2335. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2336. To turn this on on a per-file basis, insert anywhere in the file:
  2337. #+STARTUP: customtime"
  2338. :group 'org-time
  2339. :set 'set-default
  2340. :type 'sexp)
  2341. (make-variable-buffer-local 'org-display-custom-times)
  2342. (defcustom org-time-stamp-custom-formats
  2343. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2344. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2345. These are overlaid over the default ISO format if the variable
  2346. `org-display-custom-times' is set. Time like %H:%M should be at the
  2347. end of the second format. The custom formats are also honored by export
  2348. commands, if custom time display is turned on at the time of export."
  2349. :group 'org-time
  2350. :type 'sexp)
  2351. (defun org-time-stamp-format (&optional long inactive)
  2352. "Get the right format for a time string."
  2353. (let ((f (if long (cdr org-time-stamp-formats)
  2354. (car org-time-stamp-formats))))
  2355. (if inactive
  2356. (concat "[" (substring f 1 -1) "]")
  2357. f)))
  2358. (defcustom org-time-clocksum-format "%d:%02d"
  2359. "The format string used when creating CLOCKSUM lines.
  2360. This is also used when org-mode generates a time duration."
  2361. :group 'org-time
  2362. :type 'string)
  2363. (defcustom org-time-clocksum-use-fractional nil
  2364. "If non-nil, \\[org-clock-display] uses fractional times.
  2365. org-mode generates a time duration."
  2366. :group 'org-time
  2367. :type 'boolean)
  2368. (defcustom org-time-clocksum-fractional-format "%.2f"
  2369. "The format string used when creating CLOCKSUM lines, or when
  2370. org-mode generates a time duration."
  2371. :group 'org-time
  2372. :type 'string)
  2373. (defcustom org-deadline-warning-days 14
  2374. "No. of days before expiration during which a deadline becomes active.
  2375. This variable governs the display in sparse trees and in the agenda.
  2376. When 0 or negative, it means use this number (the absolute value of it)
  2377. even if a deadline has a different individual lead time specified.
  2378. Custom commands can set this variable in the options section."
  2379. :group 'org-time
  2380. :group 'org-agenda-daily/weekly
  2381. :type 'integer)
  2382. (defcustom org-read-date-prefer-future t
  2383. "Non-nil means assume future for incomplete date input from user.
  2384. This affects the following situations:
  2385. 1. The user gives a month but not a year.
  2386. For example, if it is April and you enter \"feb 2\", this will be read
  2387. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2388. 2. The user gives a day, but no month.
  2389. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2390. read this as the third of *next* month. However, if you enter \"17\",
  2391. it will be considered as *this* month.
  2392. If you set this variable to the symbol `time', then also the following
  2393. will work:
  2394. 3. If the user gives a time.
  2395. If the time is before now, it will be interpreted as tomorrow.
  2396. Currently none of this works for ISO week specifications.
  2397. When this option is nil, the current day, month and year will always be
  2398. used as defaults.
  2399. See also `org-agenda-jump-prefer-future'."
  2400. :group 'org-time
  2401. :type '(choice
  2402. (const :tag "Never" nil)
  2403. (const :tag "Check month and day" t)
  2404. (const :tag "Check month, day, and time" time)))
  2405. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2406. "Should the agenda jump command prefer the future for incomplete dates?
  2407. The default is to do the same as configured in `org-read-date-prefer-future'.
  2408. But you can also set a deviating value here.
  2409. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2410. :group 'org-agenda
  2411. :group 'org-time
  2412. :version "24.1"
  2413. :type '(choice
  2414. (const :tag "Use org-read-date-prefer-future"
  2415. org-read-date-prefer-future)
  2416. (const :tag "Never" nil)
  2417. (const :tag "Always" t)))
  2418. (defcustom org-read-date-force-compatible-dates t
  2419. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2420. Depending on the system Emacs is running on, certain dates cannot
  2421. be represented with the type used internally to represent time.
  2422. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2423. correctly. Some systems allow for earlier dates, some for later,
  2424. some for both. One way to find out it to insert any date into an
  2425. Org buffer, putting the cursor on the year and hitting S-up and
  2426. S-down to test the range.
  2427. When this variable is set to t, the date/time prompt will not let
  2428. you specify dates outside the 1970-2037 range, so it is certain that
  2429. these dates will work in whatever version of Emacs you are
  2430. running, and also that you can move a file from one Emacs implementation
  2431. to another. WHenever Org is forcing the year for you, it will display
  2432. a message and beep.
  2433. When this variable is nil, Org will check if the date is
  2434. representable in the specific Emacs implementation you are using.
  2435. If not, it will force a year, usually the current year, and beep
  2436. to remind you. Currently this setting is not recommended because
  2437. the likelihood that you will open your Org files in an Emacs that
  2438. has limited date range is not negligible.
  2439. A workaround for this problem is to use diary sexp dates for time
  2440. stamps outside of this range."
  2441. :group 'org-time
  2442. :version "24.1"
  2443. :type 'boolean)
  2444. (defcustom org-read-date-display-live t
  2445. "Non-nil means display current interpretation of date prompt live.
  2446. This display will be in an overlay, in the minibuffer."
  2447. :group 'org-time
  2448. :type 'boolean)
  2449. (defcustom org-read-date-popup-calendar t
  2450. "Non-nil means pop up a calendar when prompting for a date.
  2451. In the calendar, the date can be selected with mouse-1. However, the
  2452. minibuffer will also be active, and you can simply enter the date as well.
  2453. When nil, only the minibuffer will be available."
  2454. :group 'org-time
  2455. :type 'boolean)
  2456. (if (fboundp 'defvaralias)
  2457. (defvaralias 'org-popup-calendar-for-date-prompt
  2458. 'org-read-date-popup-calendar))
  2459. (defcustom org-read-date-minibuffer-setup-hook nil
  2460. "Hook to be used to set up keys for the date/time interface.
  2461. Add key definitions to `minibuffer-local-map', which will be a temporary
  2462. copy."
  2463. :group 'org-time
  2464. :type 'hook)
  2465. (defcustom org-extend-today-until 0
  2466. "The hour when your day really ends. Must be an integer.
  2467. This has influence for the following applications:
  2468. - When switching the agenda to \"today\". It it is still earlier than
  2469. the time given here, the day recognized as TODAY is actually yesterday.
  2470. - When a date is read from the user and it is still before the time given
  2471. here, the current date and time will be assumed to be yesterday, 23:59.
  2472. Also, timestamps inserted in capture templates follow this rule.
  2473. IMPORTANT: This is a feature whose implementation is and likely will
  2474. remain incomplete. Really, it is only here because past midnight seems to
  2475. be the favorite working time of John Wiegley :-)"
  2476. :group 'org-time
  2477. :type 'integer)
  2478. (defcustom org-use-effective-time nil
  2479. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2480. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2481. \"effective time\" of any timestamps between midnight and 8am will be
  2482. 23:59 of the previous day."
  2483. :group 'org-time
  2484. :version "24.1"
  2485. :type 'boolean)
  2486. (defcustom org-edit-timestamp-down-means-later nil
  2487. "Non-nil means S-down will increase the time in a time stamp.
  2488. When nil, S-up will increase."
  2489. :group 'org-time
  2490. :type 'boolean)
  2491. (defcustom org-calendar-follow-timestamp-change t
  2492. "Non-nil means make the calendar window follow timestamp changes.
  2493. When a timestamp is modified and the calendar window is visible, it will be
  2494. moved to the new date."
  2495. :group 'org-time
  2496. :type 'boolean)
  2497. (defgroup org-tags nil
  2498. "Options concerning tags in Org-mode."
  2499. :tag "Org Tags"
  2500. :group 'org)
  2501. (defcustom org-tag-alist nil
  2502. "List of tags allowed in Org-mode files.
  2503. When this list is nil, Org-mode will base TAG input on what is already in the
  2504. buffer.
  2505. The value of this variable is an alist, the car of each entry must be a
  2506. keyword as a string, the cdr may be a character that is used to select
  2507. that tag through the fast-tag-selection interface.
  2508. See the manual for details."
  2509. :group 'org-tags
  2510. :type '(repeat
  2511. (choice
  2512. (cons (string :tag "Tag name")
  2513. (character :tag "Access char"))
  2514. (list :tag "Start radio group"
  2515. (const :startgroup)
  2516. (option (string :tag "Group description")))
  2517. (list :tag "End radio group"
  2518. (const :endgroup)
  2519. (option (string :tag "Group description")))
  2520. (const :tag "New line" (:newline)))))
  2521. (defcustom org-tag-persistent-alist nil
  2522. "List of tags that will always appear in all Org-mode files.
  2523. This is in addition to any in buffer settings or customizations
  2524. of `org-tag-alist'.
  2525. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2526. The value of this variable is an alist, the car of each entry must be a
  2527. keyword as a string, the cdr may be a character that is used to select
  2528. that tag through the fast-tag-selection interface.
  2529. See the manual for details.
  2530. To disable these tags on a per-file basis, insert anywhere in the file:
  2531. #+STARTUP: noptag"
  2532. :group 'org-tags
  2533. :type '(repeat
  2534. (choice
  2535. (cons (string :tag "Tag name")
  2536. (character :tag "Access char"))
  2537. (const :tag "Start radio group" (:startgroup))
  2538. (const :tag "End radio group" (:endgroup))
  2539. (const :tag "New line" (:newline)))))
  2540. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2541. "If non-nil, always offer completion for all tags of all agenda files.
  2542. Instead of customizing this variable directly, you might want to
  2543. set it locally for capture buffers, because there no list of
  2544. tags in that file can be created dynamically (there are none).
  2545. (add-hook 'org-capture-mode-hook
  2546. (lambda ()
  2547. (set (make-local-variable
  2548. 'org-complete-tags-always-offer-all-agenda-tags)
  2549. t)))"
  2550. :group 'org-tags
  2551. :version "24.1"
  2552. :type 'boolean)
  2553. (defvar org-file-tags nil
  2554. "List of tags that can be inherited by all entries in the file.
  2555. The tags will be inherited if the variable `org-use-tag-inheritance'
  2556. says they should be.
  2557. This variable is populated from #+FILETAGS lines.")
  2558. (defcustom org-use-fast-tag-selection 'auto
  2559. "Non-nil means use fast tag selection scheme.
  2560. This is a special interface to select and deselect tags with single keys.
  2561. When nil, fast selection is never used.
  2562. When the symbol `auto', fast selection is used if and only if selection
  2563. characters for tags have been configured, either through the variable
  2564. `org-tag-alist' or through a #+TAGS line in the buffer.
  2565. When t, fast selection is always used and selection keys are assigned
  2566. automatically if necessary."
  2567. :group 'org-tags
  2568. :type '(choice
  2569. (const :tag "Always" t)
  2570. (const :tag "Never" nil)
  2571. (const :tag "When selection characters are configured" 'auto)))
  2572. (defcustom org-fast-tag-selection-single-key nil
  2573. "Non-nil means fast tag selection exits after first change.
  2574. When nil, you have to press RET to exit it.
  2575. During fast tag selection, you can toggle this flag with `C-c'.
  2576. This variable can also have the value `expert'. In this case, the window
  2577. displaying the tags menu is not even shown, until you press C-c again."
  2578. :group 'org-tags
  2579. :type '(choice
  2580. (const :tag "No" nil)
  2581. (const :tag "Yes" t)
  2582. (const :tag "Expert" expert)))
  2583. (defvar org-fast-tag-selection-include-todo nil
  2584. "Non-nil means fast tags selection interface will also offer TODO states.
  2585. This is an undocumented feature, you should not rely on it.")
  2586. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2587. "The column to which tags should be indented in a headline.
  2588. If this number is positive, it specifies the column. If it is negative,
  2589. it means that the tags should be flushright to that column. For example,
  2590. -80 works well for a normal 80 character screen.
  2591. When 0, place tags directly after headline text, with only one space in
  2592. between."
  2593. :group 'org-tags
  2594. :type 'integer)
  2595. (defcustom org-auto-align-tags t
  2596. "Non-nil keeps tags aligned when modifying headlines.
  2597. Some operations (i.e. demoting) change the length of a headline and
  2598. therefore shift the tags around. With this option turned on, after
  2599. each such operation the tags are again aligned to `org-tags-column'."
  2600. :group 'org-tags
  2601. :type 'boolean)
  2602. (defcustom org-use-tag-inheritance t
  2603. "Non-nil means tags in levels apply also for sublevels.
  2604. When nil, only the tags directly given in a specific line apply there.
  2605. This may also be a list of tags that should be inherited, or a regexp that
  2606. matches tags that should be inherited. Additional control is possible
  2607. with the variable `org-tags-exclude-from-inheritance' which gives an
  2608. explicit list of tags to be excluded from inheritance, even if the value of
  2609. `org-use-tag-inheritance' would select it for inheritance.
  2610. If this option is t, a match early-on in a tree can lead to a large
  2611. number of matches in the subtree when constructing the agenda or creating
  2612. a sparse tree. If you only want to see the first match in a tree during
  2613. a search, check out the variable `org-tags-match-list-sublevels'."
  2614. :group 'org-tags
  2615. :type '(choice
  2616. (const :tag "Not" nil)
  2617. (const :tag "Always" t)
  2618. (repeat :tag "Specific tags" (string :tag "Tag"))
  2619. (regexp :tag "Tags matched by regexp")))
  2620. (defcustom org-tags-exclude-from-inheritance nil
  2621. "List of tags that should never be inherited.
  2622. This is a way to exclude a few tags from inheritance. For way to do
  2623. the opposite, to actively allow inheritance for selected tags,
  2624. see the variable `org-use-tag-inheritance'."
  2625. :group 'org-tags
  2626. :type '(repeat (string :tag "Tag")))
  2627. (defun org-tag-inherit-p (tag)
  2628. "Check if TAG is one that should be inherited."
  2629. (cond
  2630. ((member tag org-tags-exclude-from-inheritance) nil)
  2631. ((eq org-use-tag-inheritance t) t)
  2632. ((not org-use-tag-inheritance) nil)
  2633. ((stringp org-use-tag-inheritance)
  2634. (string-match org-use-tag-inheritance tag))
  2635. ((listp org-use-tag-inheritance)
  2636. (member tag org-use-tag-inheritance))
  2637. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2638. (defcustom org-tags-match-list-sublevels t
  2639. "Non-nil means list also sublevels of headlines matching a search.
  2640. This variable applies to tags/property searches, and also to stuck
  2641. projects because this search is based on a tags match as well.
  2642. When set to the symbol `indented', sublevels are indented with
  2643. leading dots.
  2644. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2645. the sublevels of a headline matching a tag search often also match
  2646. the same search. Listing all of them can create very long lists.
  2647. Setting this variable to nil causes subtrees of a match to be skipped.
  2648. This variable is semi-obsolete and probably should always be true. It
  2649. is better to limit inheritance to certain tags using the variables
  2650. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2651. :group 'org-tags
  2652. :type '(choice
  2653. (const :tag "No, don't list them" nil)
  2654. (const :tag "Yes, do list them" t)
  2655. (const :tag "List them, indented with leading dots" indented)))
  2656. (defcustom org-tags-sort-function nil
  2657. "When set, tags are sorted using this function as a comparator."
  2658. :group 'org-tags
  2659. :type '(choice
  2660. (const :tag "No sorting" nil)
  2661. (const :tag "Alphabetical" string<)
  2662. (const :tag "Reverse alphabetical" string>)
  2663. (function :tag "Custom function" nil)))
  2664. (defvar org-tags-history nil
  2665. "History of minibuffer reads for tags.")
  2666. (defvar org-last-tags-completion-table nil
  2667. "The last used completion table for tags.")
  2668. (defvar org-after-tags-change-hook nil
  2669. "Hook that is run after the tags in a line have changed.")
  2670. (defgroup org-properties nil
  2671. "Options concerning properties in Org-mode."
  2672. :tag "Org Properties"
  2673. :group 'org)
  2674. (defcustom org-property-format "%-10s %s"
  2675. "How property key/value pairs should be formatted by `indent-line'.
  2676. When `indent-line' hits a property definition, it will format the line
  2677. according to this format, mainly to make sure that the values are
  2678. lined-up with respect to each other."
  2679. :group 'org-properties
  2680. :type 'string)
  2681. (defcustom org-properties-postprocess-alist nil
  2682. "Alist of properties and functions to adjust inserted values.
  2683. Elements of this alist must be of the form
  2684. ([string] [function])
  2685. where [string] must be a property name and [function] must be a
  2686. lambda expression: this lambda expression must take one argument,
  2687. the value to adjust, and return the new value as a string.
  2688. For example, this element will allow the property \"Remaining\"
  2689. to be updated wrt the relation between the \"Effort\" property
  2690. and the clock summary:
  2691. ((\"Remaining\" (lambda(value)
  2692. (let ((clocksum (org-clock-sum-current-item))
  2693. (effort (org-duration-string-to-minutes
  2694. (org-entry-get (point) \"Effort\"))))
  2695. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2696. :group 'org-properties
  2697. :version "24.1"
  2698. :type '(alist :key-type (string :tag "Property")
  2699. :value-type (function :tag "Function")))
  2700. (defcustom org-use-property-inheritance nil
  2701. "Non-nil means properties apply also for sublevels.
  2702. This setting is chiefly used during property searches. Turning it on can
  2703. cause significant overhead when doing a search, which is why it is not
  2704. on by default.
  2705. When nil, only the properties directly given in the current entry count.
  2706. When t, every property is inherited. The value may also be a list of
  2707. properties that should have inheritance, or a regular expression matching
  2708. properties that should be inherited.
  2709. However, note that some special properties use inheritance under special
  2710. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2711. and the properties ending in \"_ALL\" when they are used as descriptor
  2712. for valid values of a property.
  2713. Note for programmers:
  2714. When querying an entry with `org-entry-get', you can control if inheritance
  2715. should be used. By default, `org-entry-get' looks only at the local
  2716. properties. You can request inheritance by setting the inherit argument
  2717. to t (to force inheritance) or to `selective' (to respect the setting
  2718. in this variable)."
  2719. :group 'org-properties
  2720. :type '(choice
  2721. (const :tag "Not" nil)
  2722. (const :tag "Always" t)
  2723. (repeat :tag "Specific properties" (string :tag "Property"))
  2724. (regexp :tag "Properties matched by regexp")))
  2725. (defun org-property-inherit-p (property)
  2726. "Check if PROPERTY is one that should be inherited."
  2727. (cond
  2728. ((eq org-use-property-inheritance t) t)
  2729. ((not org-use-property-inheritance) nil)
  2730. ((stringp org-use-property-inheritance)
  2731. (string-match org-use-property-inheritance property))
  2732. ((listp org-use-property-inheritance)
  2733. (member property org-use-property-inheritance))
  2734. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2735. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2736. "The default column format, if no other format has been defined.
  2737. This variable can be set on the per-file basis by inserting a line
  2738. #+COLUMNS: %25ITEM ....."
  2739. :group 'org-properties
  2740. :type 'string)
  2741. (defcustom org-columns-ellipses ".."
  2742. "The ellipses to be used when a field in column view is truncated.
  2743. When this is the empty string, as many characters as possible are shown,
  2744. but then there will be no visual indication that the field has been truncated.
  2745. When this is a string of length N, the last N characters of a truncated
  2746. field are replaced by this string. If the column is narrower than the
  2747. ellipses string, only part of the ellipses string will be shown."
  2748. :group 'org-properties
  2749. :type 'string)
  2750. (defcustom org-columns-modify-value-for-display-function nil
  2751. "Function that modifies values for display in column view.
  2752. For example, it can be used to cut out a certain part from a time stamp.
  2753. The function must take 2 arguments:
  2754. column-title The title of the column (*not* the property name)
  2755. value The value that should be modified.
  2756. The function should return the value that should be displayed,
  2757. or nil if the normal value should be used."
  2758. :group 'org-properties
  2759. :type 'function)
  2760. (defcustom org-effort-property "Effort"
  2761. "The property that is being used to keep track of effort estimates.
  2762. Effort estimates given in this property need to have the format H:MM."
  2763. :group 'org-properties
  2764. :group 'org-progress
  2765. :type '(string :tag "Property"))
  2766. (defconst org-global-properties-fixed
  2767. '(("VISIBILITY_ALL" . "folded children content all")
  2768. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2769. "List of property/value pairs that can be inherited by any entry.
  2770. These are fixed values, for the preset properties. The user variable
  2771. that can be used to add to this list is `org-global-properties'.
  2772. The entries in this list are cons cells where the car is a property
  2773. name and cdr is a string with the value. If the value represents
  2774. multiple items like an \"_ALL\" property, separate the items by
  2775. spaces.")
  2776. (defcustom org-global-properties nil
  2777. "List of property/value pairs that can be inherited by any entry.
  2778. This list will be combined with the constant `org-global-properties-fixed'.
  2779. The entries in this list are cons cells where the car is a property
  2780. name and cdr is a string with the value.
  2781. You can set buffer-local values for the same purpose in the variable
  2782. `org-file-properties' this by adding lines like
  2783. #+PROPERTY: NAME VALUE"
  2784. :group 'org-properties
  2785. :type '(repeat
  2786. (cons (string :tag "Property")
  2787. (string :tag "Value"))))
  2788. (defvar org-file-properties nil
  2789. "List of property/value pairs that can be inherited by any entry.
  2790. Valid for the current buffer.
  2791. This variable is populated from #+PROPERTY lines.")
  2792. (make-variable-buffer-local 'org-file-properties)
  2793. (defgroup org-agenda nil
  2794. "Options concerning agenda views in Org-mode."
  2795. :tag "Org Agenda"
  2796. :group 'org)
  2797. (defvar org-category nil
  2798. "Variable used by org files to set a category for agenda display.
  2799. Such files should use a file variable to set it, for example
  2800. # -*- mode: org; org-category: \"ELisp\"
  2801. or contain a special line
  2802. #+CATEGORY: ELisp
  2803. If the file does not specify a category, then file's base name
  2804. is used instead.")
  2805. (make-variable-buffer-local 'org-category)
  2806. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2807. (defcustom org-agenda-files nil
  2808. "The files to be used for agenda display.
  2809. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2810. \\[org-remove-file]. You can also use customize to edit the list.
  2811. If an entry is a directory, all files in that directory that are matched by
  2812. `org-agenda-file-regexp' will be part of the file list.
  2813. If the value of the variable is not a list but a single file name, then
  2814. the list of agenda files is actually stored and maintained in that file, one
  2815. agenda file per line. In this file paths can be given relative to
  2816. `org-directory'. Tilde expansion and environment variable substitution
  2817. are also made."
  2818. :group 'org-agenda
  2819. :type '(choice
  2820. (repeat :tag "List of files and directories" file)
  2821. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2822. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2823. "Regular expression to match files for `org-agenda-files'.
  2824. If any element in the list in that variable contains a directory instead
  2825. of a normal file, all files in that directory that are matched by this
  2826. regular expression will be included."
  2827. :group 'org-agenda
  2828. :type 'regexp)
  2829. (defcustom org-agenda-text-search-extra-files nil
  2830. "List of extra files to be searched by text search commands.
  2831. These files will be search in addition to the agenda files by the
  2832. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2833. Note that these files will only be searched for text search commands,
  2834. not for the other agenda views like todo lists, tag searches or the weekly
  2835. agenda. This variable is intended to list notes and possibly archive files
  2836. that should also be searched by these two commands.
  2837. In fact, if the first element in the list is the symbol `agenda-archives',
  2838. than all archive files of all agenda files will be added to the search
  2839. scope."
  2840. :group 'org-agenda
  2841. :type '(set :greedy t
  2842. (const :tag "Agenda Archives" agenda-archives)
  2843. (repeat :inline t (file))))
  2844. (if (fboundp 'defvaralias)
  2845. (defvaralias 'org-agenda-multi-occur-extra-files
  2846. 'org-agenda-text-search-extra-files))
  2847. (defcustom org-agenda-skip-unavailable-files nil
  2848. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2849. A nil value means to remove them, after a query, from the list."
  2850. :group 'org-agenda
  2851. :type 'boolean)
  2852. (defcustom org-calendar-to-agenda-key [?c]
  2853. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2854. The command `org-calendar-goto-agenda' will be bound to this key. The
  2855. default is the character `c' because then `c' can be used to switch back and
  2856. forth between agenda and calendar."
  2857. :group 'org-agenda
  2858. :type 'sexp)
  2859. (defcustom org-calendar-insert-diary-entry-key [?i]
  2860. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2861. This option is irrelevant until `org-agenda-diary-file' has been configured
  2862. to point to an Org-mode file. When that is the case, the command
  2863. `org-agenda-diary-entry' will be bound to the key given here, by default
  2864. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2865. if you want to continue doing this, you need to change this to a different
  2866. key."
  2867. :group 'org-agenda
  2868. :type 'sexp)
  2869. (defcustom org-agenda-diary-file 'diary-file
  2870. "File to which to add new entries with the `i' key in agenda and calendar.
  2871. When this is the symbol `diary-file', the functionality in the Emacs
  2872. calendar will be used to add entries to the `diary-file'. But when this
  2873. points to a file, `org-agenda-diary-entry' will be used instead."
  2874. :group 'org-agenda
  2875. :type '(choice
  2876. (const :tag "The standard Emacs diary file" diary-file)
  2877. (file :tag "Special Org file diary entries")))
  2878. (eval-after-load "calendar"
  2879. '(progn
  2880. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2881. 'org-calendar-goto-agenda)
  2882. (add-hook 'calendar-mode-hook
  2883. (lambda ()
  2884. (unless (eq org-agenda-diary-file 'diary-file)
  2885. (define-key calendar-mode-map
  2886. org-calendar-insert-diary-entry-key
  2887. 'org-agenda-diary-entry))))))
  2888. (defgroup org-latex nil
  2889. "Options for embedding LaTeX code into Org-mode."
  2890. :tag "Org LaTeX"
  2891. :group 'org)
  2892. (defcustom org-format-latex-options
  2893. '(:foreground default :background default :scale 1.0
  2894. :html-foreground "Black" :html-background "Transparent"
  2895. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2896. "Options for creating images from LaTeX fragments.
  2897. This is a property list with the following properties:
  2898. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2899. `default' means use the foreground of the default face.
  2900. :background the background color, or \"Transparent\".
  2901. `default' means use the background of the default face.
  2902. :scale a scaling factor for the size of the images, to get more pixels
  2903. :html-foreground, :html-background, :html-scale
  2904. the same numbers for HTML export.
  2905. :matchers a list indicating which matchers should be used to
  2906. find LaTeX fragments. Valid members of this list are:
  2907. \"begin\" find environments
  2908. \"$1\" find single characters surrounded by $.$
  2909. \"$\" find math expressions surrounded by $...$
  2910. \"$$\" find math expressions surrounded by $$....$$
  2911. \"\\(\" find math expressions surrounded by \\(...\\)
  2912. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2913. :group 'org-latex
  2914. :type 'plist)
  2915. (defcustom org-format-latex-signal-error t
  2916. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2917. When nil, just push out a message."
  2918. :group 'org-latex
  2919. :version "24.1"
  2920. :type 'boolean)
  2921. (defcustom org-latex-to-mathml-jar-file nil
  2922. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2923. Use this to specify additional executable file say a jar file.
  2924. When using MathToWeb as the converter, specify the full-path to
  2925. your mathtoweb.jar file."
  2926. :group 'org-latex
  2927. :version "24.1"
  2928. :type '(choice
  2929. (const :tag "None" nil)
  2930. (file :tag "JAR file" :must-match t)))
  2931. (defcustom org-latex-to-mathml-convert-command nil
  2932. "Command to convert LaTeX fragments to MathML.
  2933. Replace format-specifiers in the command as noted below and use
  2934. `shell-command' to convert LaTeX to MathML.
  2935. %j: Executable file in fully expanded form as specified by
  2936. `org-latex-to-mathml-jar-file'.
  2937. %I: Input LaTeX file in fully expanded form
  2938. %o: Output MathML file
  2939. This command is used by `org-create-math-formula'.
  2940. When using MathToWeb as the converter, set this to
  2941. \"java -jar %j -unicode -force -df %o %I\"."
  2942. :group 'org-latex
  2943. :version "24.1"
  2944. :type '(choice
  2945. (const :tag "None" nil)
  2946. (string :tag "\nShell command")))
  2947. (defcustom org-latex-create-formula-image-program 'dvipng
  2948. "Program to convert LaTeX fragments with.
  2949. dvipng Process the LaTeX fragments to dvi file, then convert
  2950. dvi files to png files using dvipng.
  2951. This will also include processing of non-math environments.
  2952. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  2953. to convert pdf files to png files"
  2954. :group 'org-latex
  2955. :version "24.1"
  2956. :type '(choice
  2957. (const :tag "dvipng" dvipng)
  2958. (const :tag "imagemagick" imagemagick)))
  2959. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  2960. "Path to store latex preview images. A relative path here creates many
  2961. directories relative to the processed org files paths. An absolute path
  2962. puts all preview images at the same place."
  2963. :group 'org-latex
  2964. :version "24.3"
  2965. :type 'string)
  2966. (defun org-format-latex-mathml-available-p ()
  2967. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2968. (save-match-data
  2969. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2970. org-latex-to-mathml-convert-command)
  2971. (let ((executable (car (split-string
  2972. org-latex-to-mathml-convert-command))))
  2973. (when (executable-find executable)
  2974. (if (string-match
  2975. "%j" org-latex-to-mathml-convert-command)
  2976. (file-readable-p org-latex-to-mathml-jar-file)
  2977. t))))))
  2978. (defcustom org-format-latex-header "\\documentclass{article}
  2979. \\usepackage[usenames]{color}
  2980. \\usepackage{amsmath}
  2981. \\usepackage[mathscr]{eucal}
  2982. \\pagestyle{empty} % do not remove
  2983. \[PACKAGES]
  2984. \[DEFAULT-PACKAGES]
  2985. % The settings below are copied from fullpage.sty
  2986. \\setlength{\\textwidth}{\\paperwidth}
  2987. \\addtolength{\\textwidth}{-3cm}
  2988. \\setlength{\\oddsidemargin}{1.5cm}
  2989. \\addtolength{\\oddsidemargin}{-2.54cm}
  2990. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2991. \\setlength{\\textheight}{\\paperheight}
  2992. \\addtolength{\\textheight}{-\\headheight}
  2993. \\addtolength{\\textheight}{-\\headsep}
  2994. \\addtolength{\\textheight}{-\\footskip}
  2995. \\addtolength{\\textheight}{-3cm}
  2996. \\setlength{\\topmargin}{1.5cm}
  2997. \\addtolength{\\topmargin}{-2.54cm}"
  2998. "The document header used for processing LaTeX fragments.
  2999. It is imperative that this header make sure that no page number
  3000. appears on the page. The package defined in the variables
  3001. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  3002. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  3003. will be appended."
  3004. :group 'org-latex
  3005. :type 'string)
  3006. (defvar org-format-latex-header-extra nil)
  3007. (defun org-set-packages-alist (var val)
  3008. "Set the packages alist and make sure it has 3 elements per entry."
  3009. (set var (mapcar (lambda (x)
  3010. (if (and (consp x) (= (length x) 2))
  3011. (list (car x) (nth 1 x) t)
  3012. x))
  3013. val)))
  3014. (defun org-get-packages-alist (var)
  3015. "Get the packages alist and make sure it has 3 elements per entry."
  3016. (mapcar (lambda (x)
  3017. (if (and (consp x) (= (length x) 2))
  3018. (list (car x) (nth 1 x) t)
  3019. x))
  3020. (default-value var)))
  3021. ;; The following variables are defined here because is it also used
  3022. ;; when formatting latex fragments. Originally it was part of the
  3023. ;; LaTeX exporter, which is why the name includes "export".
  3024. (defcustom org-export-latex-default-packages-alist
  3025. '(("AUTO" "inputenc" t)
  3026. ("T1" "fontenc" t)
  3027. ("" "fixltx2e" nil)
  3028. ("" "graphicx" t)
  3029. ("" "longtable" nil)
  3030. ("" "float" nil)
  3031. ("" "wrapfig" nil)
  3032. ("" "soul" t)
  3033. ("" "textcomp" t)
  3034. ("" "marvosym" t)
  3035. ("" "wasysym" t)
  3036. ("" "latexsym" t)
  3037. ("" "amssymb" t)
  3038. ("" "hyperref" nil)
  3039. "\\tolerance=1000"
  3040. )
  3041. "Alist of default packages to be inserted in the header.
  3042. Change this only if one of the packages here causes an incompatibility
  3043. with another package you are using.
  3044. The packages in this list are needed by one part or another of Org-mode
  3045. to function properly.
  3046. - inputenc, fontenc: for basic font and character selection
  3047. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3048. for interpreting the entities in `org-entities'. You can skip some of these
  3049. packages if you don't use any of the symbols in it.
  3050. - graphicx: for including images
  3051. - float, wrapfig: for figure placement
  3052. - longtable: for long tables
  3053. - hyperref: for cross references
  3054. Therefore you should not modify this variable unless you know what you
  3055. are doing. The one reason to change it anyway is that you might be loading
  3056. some other package that conflicts with one of the default packages.
  3057. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3058. If SNIPPET-FLAG is t, the package also needs to be included when
  3059. compiling LaTeX snippets into images for inclusion into HTML."
  3060. :group 'org-export-latex
  3061. :set 'org-set-packages-alist
  3062. :get 'org-get-packages-alist
  3063. :version "24.1"
  3064. :type '(repeat
  3065. (choice
  3066. (list :tag "options/package pair"
  3067. (string :tag "options")
  3068. (string :tag "package")
  3069. (boolean :tag "Snippet"))
  3070. (string :tag "A line of LaTeX"))))
  3071. (defcustom org-export-latex-packages-alist nil
  3072. "Alist of packages to be inserted in every LaTeX header.
  3073. These will be inserted after `org-export-latex-default-packages-alist'.
  3074. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3075. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3076. turning LaTeX snippets into images for inclusion into HTML.
  3077. Make sure that you only list packages here which:
  3078. - you want in every file
  3079. - do not conflict with the default packages in
  3080. `org-export-latex-default-packages-alist'
  3081. - do not conflict with the setup in `org-format-latex-header'."
  3082. :group 'org-export-latex
  3083. :set 'org-set-packages-alist
  3084. :get 'org-get-packages-alist
  3085. :type '(repeat
  3086. (choice
  3087. (list :tag "options/package pair"
  3088. (string :tag "options")
  3089. (string :tag "package")
  3090. (boolean :tag "Snippet"))
  3091. (string :tag "A line of LaTeX"))))
  3092. (defgroup org-appearance nil
  3093. "Settings for Org-mode appearance."
  3094. :tag "Org Appearance"
  3095. :group 'org)
  3096. (defcustom org-level-color-stars-only nil
  3097. "Non-nil means fontify only the stars in each headline.
  3098. When nil, the entire headline is fontified.
  3099. Changing it requires restart of `font-lock-mode' to become effective
  3100. also in regions already fontified."
  3101. :group 'org-appearance
  3102. :type 'boolean)
  3103. (defcustom org-hide-leading-stars nil
  3104. "Non-nil means hide the first N-1 stars in a headline.
  3105. This works by using the face `org-hide' for these stars. This
  3106. face is white for a light background, and black for a dark
  3107. background. You may have to customize the face `org-hide' to
  3108. make this work.
  3109. Changing it requires restart of `font-lock-mode' to become effective
  3110. also in regions already fontified.
  3111. You may also set this on a per-file basis by adding one of the following
  3112. lines to the buffer:
  3113. #+STARTUP: hidestars
  3114. #+STARTUP: showstars"
  3115. :group 'org-appearance
  3116. :type 'boolean)
  3117. (defcustom org-hidden-keywords nil
  3118. "List of symbols corresponding to keywords to be hidden the org buffer.
  3119. For example, a value '(title) for this list will make the document's title
  3120. appear in the buffer without the initial #+TITLE: keyword."
  3121. :group 'org-appearance
  3122. :version "24.1"
  3123. :type '(set (const :tag "#+AUTHOR" author)
  3124. (const :tag "#+DATE" date)
  3125. (const :tag "#+EMAIL" email)
  3126. (const :tag "#+TITLE" title)))
  3127. (defcustom org-custom-properties nil
  3128. "List of properties (as strings) with a special meaning.
  3129. The default use of these custom properties is to let the user
  3130. hide them with `org-toggle-custom-properties-visibility'."
  3131. :group 'org-properties
  3132. :group 'org-appearance
  3133. :version "24.3"
  3134. :type '(repeat (string :tag "Property Name")))
  3135. (defcustom org-fontify-done-headline nil
  3136. "Non-nil means change the face of a headline if it is marked DONE.
  3137. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3138. When this is non-nil, the headline after the keyword is set to the
  3139. `org-headline-done' as an additional indication."
  3140. :group 'org-appearance
  3141. :type 'boolean)
  3142. (defcustom org-fontify-emphasized-text t
  3143. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3144. Changing this variable requires a restart of Emacs to take effect."
  3145. :group 'org-appearance
  3146. :type 'boolean)
  3147. (defcustom org-fontify-whole-heading-line nil
  3148. "Non-nil means fontify the whole line for headings.
  3149. This is useful when setting a background color for the
  3150. org-level-* faces."
  3151. :group 'org-appearance
  3152. :type 'boolean)
  3153. (defcustom org-highlight-latex-fragments-and-specials nil
  3154. "Non-nil means fontify what is treated specially by the exporters."
  3155. :group 'org-appearance
  3156. :type 'boolean)
  3157. (defcustom org-hide-emphasis-markers nil
  3158. "Non-nil mean font-lock should hide the emphasis marker characters."
  3159. :group 'org-appearance
  3160. :type 'boolean)
  3161. (defcustom org-pretty-entities nil
  3162. "Non-nil means show entities as UTF8 characters.
  3163. When nil, the \\name form remains in the buffer."
  3164. :group 'org-appearance
  3165. :version "24.1"
  3166. :type 'boolean)
  3167. (defcustom org-pretty-entities-include-sub-superscripts t
  3168. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3169. :group 'org-appearance
  3170. :version "24.1"
  3171. :type 'boolean)
  3172. (defvar org-emph-re nil
  3173. "Regular expression for matching emphasis.
  3174. After a match, the match groups contain these elements:
  3175. 0 The match of the full regular expression, including the characters
  3176. before and after the proper match
  3177. 1 The character before the proper match, or empty at beginning of line
  3178. 2 The proper match, including the leading and trailing markers
  3179. 3 The leading marker like * or /, indicating the type of highlighting
  3180. 4 The text between the emphasis markers, not including the markers
  3181. 5 The character after the match, empty at the end of a line")
  3182. (defvar org-verbatim-re nil
  3183. "Regular expression for matching verbatim text.")
  3184. (defvar org-emphasis-regexp-components) ; defined just below
  3185. (defvar org-emphasis-alist) ; defined just below
  3186. (defun org-set-emph-re (var val)
  3187. "Set variable and compute the emphasis regular expression."
  3188. (set var val)
  3189. (when (and (boundp 'org-emphasis-alist)
  3190. (boundp 'org-emphasis-regexp-components)
  3191. org-emphasis-alist org-emphasis-regexp-components)
  3192. (let* ((e org-emphasis-regexp-components)
  3193. (pre (car e))
  3194. (post (nth 1 e))
  3195. (border (nth 2 e))
  3196. (body (nth 3 e))
  3197. (nl (nth 4 e))
  3198. (body1 (concat body "*?"))
  3199. (markers (mapconcat 'car org-emphasis-alist ""))
  3200. (vmarkers (mapconcat
  3201. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3202. org-emphasis-alist "")))
  3203. ;; make sure special characters appear at the right position in the class
  3204. (if (string-match "\\^" markers)
  3205. (setq markers (concat (replace-match "" t t markers) "^")))
  3206. (if (string-match "-" markers)
  3207. (setq markers (concat (replace-match "" t t markers) "-")))
  3208. (if (string-match "\\^" vmarkers)
  3209. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3210. (if (string-match "-" vmarkers)
  3211. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3212. (if (> nl 0)
  3213. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3214. (int-to-string nl) "\\}")))
  3215. ;; Make the regexp
  3216. (setq org-emph-re
  3217. (concat "\\([" pre "]\\|^\\)"
  3218. "\\("
  3219. "\\([" markers "]\\)"
  3220. "\\("
  3221. "[^" border "]\\|"
  3222. "[^" border "]"
  3223. body1
  3224. "[^" border "]"
  3225. "\\)"
  3226. "\\3\\)"
  3227. "\\([" post "]\\|$\\)"))
  3228. (setq org-verbatim-re
  3229. (concat "\\([" pre "]\\|^\\)"
  3230. "\\("
  3231. "\\([" vmarkers "]\\)"
  3232. "\\("
  3233. "[^" border "]\\|"
  3234. "[^" border "]"
  3235. body1
  3236. "[^" border "]"
  3237. "\\)"
  3238. "\\3\\)"
  3239. "\\([" post "]\\|$\\)")))))
  3240. (defcustom org-emphasis-regexp-components
  3241. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3242. "Components used to build the regular expression for emphasis.
  3243. This is a list with five entries. Terminology: In an emphasis string
  3244. like \" *strong word* \", we call the initial space PREMATCH, the final
  3245. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3246. and \"trong wor\" is the body. The different components in this variable
  3247. specify what is allowed/forbidden in each part:
  3248. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3249. post Chars allowed as postmatch. End of line will be allowed too.
  3250. border The chars *forbidden* as border characters.
  3251. body-regexp A regexp like \".\" to match a body character. Don't use
  3252. non-shy groups here, and don't allow newline here.
  3253. newline The maximum number of newlines allowed in an emphasis exp.
  3254. Use customize to modify this, or restart Emacs after changing it."
  3255. :group 'org-appearance
  3256. :set 'org-set-emph-re
  3257. :type '(list
  3258. (sexp :tag "Allowed chars in pre ")
  3259. (sexp :tag "Allowed chars in post ")
  3260. (sexp :tag "Forbidden chars in border ")
  3261. (sexp :tag "Regexp for body ")
  3262. (integer :tag "number of newlines allowed")
  3263. (option (boolean :tag "Please ignore this button"))))
  3264. (defcustom org-emphasis-alist
  3265. `(("*" bold "<b>" "</b>")
  3266. ("/" italic "<i>" "</i>")
  3267. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3268. ("=" org-code "<code>" "</code>" verbatim)
  3269. ("~" org-verbatim "<code>" "</code>" verbatim)
  3270. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3271. "<del>" "</del>")
  3272. )
  3273. "Special syntax for emphasized text.
  3274. Text starting and ending with a special character will be emphasized, for
  3275. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3276. characters, the face to be used by font-lock for highlighting in Org-mode
  3277. Emacs buffers, and the HTML tags to be used for this.
  3278. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3279. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3280. Use customize to modify this, or restart Emacs after changing it."
  3281. :group 'org-appearance
  3282. :set 'org-set-emph-re
  3283. :type '(repeat
  3284. (list
  3285. (string :tag "Marker character")
  3286. (choice
  3287. (face :tag "Font-lock-face")
  3288. (plist :tag "Face property list"))
  3289. (string :tag "HTML start tag")
  3290. (string :tag "HTML end tag")
  3291. (option (const verbatim)))))
  3292. (defvar org-protecting-blocks
  3293. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3294. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3295. This is needed for font-lock setup.")
  3296. ;;; Miscellaneous options
  3297. (defgroup org-completion nil
  3298. "Completion in Org-mode."
  3299. :tag "Org Completion"
  3300. :group 'org)
  3301. (defcustom org-completion-use-ido nil
  3302. "Non-nil means use ido completion wherever possible.
  3303. Note that `ido-mode' must be active for this variable to be relevant.
  3304. If you decide to turn this variable on, you might well want to turn off
  3305. `org-outline-path-complete-in-steps'.
  3306. See also `org-completion-use-iswitchb'."
  3307. :group 'org-completion
  3308. :type 'boolean)
  3309. (defcustom org-completion-use-iswitchb nil
  3310. "Non-nil means use iswitchb completion wherever possible.
  3311. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3312. If you decide to turn this variable on, you might well want to turn off
  3313. `org-outline-path-complete-in-steps'.
  3314. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3315. :group 'org-completion
  3316. :type 'boolean)
  3317. (defcustom org-completion-fallback-command 'hippie-expand
  3318. "The expansion command called by \\[pcomplete] in normal context.
  3319. Normal means, no org-mode-specific context."
  3320. :group 'org-completion
  3321. :type 'function)
  3322. ;;; Functions and variables from their packages
  3323. ;; Declared here to avoid compiler warnings
  3324. ;; XEmacs only
  3325. (defvar outline-mode-menu-heading)
  3326. (defvar outline-mode-menu-show)
  3327. (defvar outline-mode-menu-hide)
  3328. (defvar zmacs-regions) ; XEmacs regions
  3329. ;; Emacs only
  3330. (defvar mark-active)
  3331. ;; Various packages
  3332. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3333. (declare-function calendar-forward-day "cal-move" (arg))
  3334. (declare-function calendar-goto-date "cal-move" (date))
  3335. (declare-function calendar-goto-today "cal-move" ())
  3336. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3337. (defvar calc-embedded-close-formula)
  3338. (defvar calc-embedded-open-formula)
  3339. (declare-function cdlatex-tab "ext:cdlatex" ())
  3340. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3341. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3342. (defvar font-lock-unfontify-region-function)
  3343. (declare-function iswitchb-read-buffer "iswitchb"
  3344. (prompt &optional default require-match start matches-set))
  3345. (defvar iswitchb-temp-buflist)
  3346. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3347. (defvar org-agenda-tags-todo-honor-ignore-options)
  3348. (declare-function org-agenda-skip "org-agenda" ())
  3349. (declare-function
  3350. org-agenda-format-item "org-agenda"
  3351. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3352. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3353. (declare-function org-agenda-change-all-lines "org-agenda"
  3354. (newhead hdmarker &optional fixface just-this))
  3355. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3356. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3357. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3358. (beg end))
  3359. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3360. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3361. "org-agenda" (&optional end))
  3362. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3363. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3364. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3365. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3366. (declare-function org-indent-mode "org-indent" (&optional arg))
  3367. (declare-function parse-time-string "parse-time" (string))
  3368. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3369. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3370. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3371. (defvar remember-data-file)
  3372. (defvar texmathp-why)
  3373. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3374. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3375. (defvar w3m-current-url)
  3376. (defvar w3m-current-title)
  3377. (defvar org-latex-regexps)
  3378. ;;; Autoload and prepare some org modules
  3379. ;; Some table stuff that needs to be defined here, because it is used
  3380. ;; by the functions setting up org-mode or checking for table context.
  3381. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3382. "Detect an org-type or table-type table.")
  3383. (defconst org-table-line-regexp "^[ \t]*|"
  3384. "Detect an org-type table line.")
  3385. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3386. "Detect an org-type table line.")
  3387. (defconst org-table-hline-regexp "^[ \t]*|-"
  3388. "Detect an org-type table hline.")
  3389. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3390. "Detect a table-type table hline.")
  3391. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3392. "Detect the first line outside a table when searching from within it.
  3393. This works for both table types.")
  3394. ;; Autoload the functions in org-table.el that are needed by functions here.
  3395. (eval-and-compile
  3396. (org-autoload "org-table"
  3397. '(org-table-begin org-table-blank-field org-table-end)))
  3398. ;;;###autoload
  3399. (defun turn-on-orgtbl ()
  3400. "Unconditionally turn on `orgtbl-mode'."
  3401. (require 'org-table)
  3402. (orgtbl-mode 1))
  3403. (defun org-at-table-p (&optional table-type)
  3404. "Return t if the cursor is inside an org-type table.
  3405. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3406. (if org-enable-table-editor
  3407. (save-excursion
  3408. (beginning-of-line 1)
  3409. (looking-at (if table-type org-table-any-line-regexp
  3410. org-table-line-regexp)))
  3411. nil))
  3412. (defsubst org-table-p () (org-at-table-p))
  3413. (defun org-at-table.el-p ()
  3414. "Return t if and only if we are at a table.el table."
  3415. (and (org-at-table-p 'any)
  3416. (save-excursion
  3417. (goto-char (org-table-begin 'any))
  3418. (looking-at org-table1-hline-regexp))))
  3419. (defun org-table-recognize-table.el ()
  3420. "If there is a table.el table nearby, recognize it and move into it."
  3421. (if org-table-tab-recognizes-table.el
  3422. (if (org-at-table.el-p)
  3423. (progn
  3424. (beginning-of-line 1)
  3425. (if (looking-at org-table-dataline-regexp)
  3426. nil
  3427. (if (looking-at org-table1-hline-regexp)
  3428. (progn
  3429. (beginning-of-line 2)
  3430. (if (looking-at org-table-any-border-regexp)
  3431. (beginning-of-line -1)))))
  3432. (if (re-search-forward "|" (org-table-end t) t)
  3433. (progn
  3434. (require 'table)
  3435. (if (table--at-cell-p (point))
  3436. t
  3437. (message "recognizing table.el table...")
  3438. (table-recognize-table)
  3439. (message "recognizing table.el table...done")))
  3440. (error "This should not happen"))
  3441. t)
  3442. nil)
  3443. nil))
  3444. (defun org-at-table-hline-p ()
  3445. "Return t if the cursor is inside a hline in a table."
  3446. (if org-enable-table-editor
  3447. (save-excursion
  3448. (beginning-of-line 1)
  3449. (looking-at org-table-hline-regexp))
  3450. nil))
  3451. (defvar org-table-clean-did-remove-column nil)
  3452. (defun org-table-map-tables (function &optional quietly)
  3453. "Apply FUNCTION to the start of all tables in the buffer."
  3454. (save-excursion
  3455. (save-restriction
  3456. (widen)
  3457. (goto-char (point-min))
  3458. (while (re-search-forward org-table-any-line-regexp nil t)
  3459. (unless quietly
  3460. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3461. (beginning-of-line 1)
  3462. (when (and (looking-at org-table-line-regexp)
  3463. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3464. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3465. (save-excursion (funcall function))
  3466. (or (looking-at org-table-line-regexp)
  3467. (forward-char 1)))
  3468. (re-search-forward org-table-any-border-regexp nil 1))))
  3469. (unless quietly (message "Mapping tables: done")))
  3470. ;; Declare and autoload functions from org-exp.el & Co
  3471. (declare-function org-default-export-plist "org-exp")
  3472. (declare-function org-infile-export-plist "org-exp")
  3473. (declare-function org-get-current-options "org-exp")
  3474. ;; Declare and autoload functions from org-agenda.el
  3475. (eval-and-compile
  3476. (org-autoload "org-agenda"
  3477. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3478. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3479. (declare-function org-clock-update-mode-line "org-clock" ())
  3480. (declare-function org-resolve-clocks "org-clock"
  3481. (&optional also-non-dangling-p prompt last-valid))
  3482. (defvar org-clock-start-time)
  3483. (defvar org-clock-marker (make-marker)
  3484. "Marker recording the last clock-in.")
  3485. (defvar org-clock-hd-marker (make-marker)
  3486. "Marker recording the last clock-in, but the headline position.")
  3487. (defvar org-clock-heading ""
  3488. "The heading of the current clock entry.")
  3489. (defun org-clock-is-active ()
  3490. "Return non-nil if clock is currently running.
  3491. The return value is actually the clock marker."
  3492. (marker-buffer org-clock-marker))
  3493. (eval-and-compile
  3494. (org-autoload "org-clock" '(org-clock-remove-overlays
  3495. org-clock-update-time-maybe
  3496. org-clocktable-shift)))
  3497. (defun org-check-running-clock ()
  3498. "Check if the current buffer contains the running clock.
  3499. If yes, offer to stop it and to save the buffer with the changes."
  3500. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3501. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3502. (buffer-name))))
  3503. (org-clock-out)
  3504. (when (y-or-n-p "Save changed buffer?")
  3505. (save-buffer))))
  3506. (defun org-clocktable-try-shift (dir n)
  3507. "Check if this line starts a clock table, if yes, shift the time block."
  3508. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3509. (org-clocktable-shift dir n)))
  3510. ;;;###autoload
  3511. (defun org-clock-persistence-insinuate ()
  3512. "Set up hooks for clock persistence."
  3513. (require 'org-clock)
  3514. (add-hook 'org-mode-hook 'org-clock-load)
  3515. (add-hook 'kill-emacs-hook 'org-clock-save))
  3516. ;; Define the variable already here, to make sure we have it.
  3517. (defvar org-indent-mode nil
  3518. "Non-nil if Org-Indent mode is enabled.
  3519. Use the command `org-indent-mode' to change this variable.")
  3520. ;; Autoload archiving code
  3521. ;; The stuff that is needed for cycling and tags has to be defined here.
  3522. (defgroup org-archive nil
  3523. "Options concerning archiving in Org-mode."
  3524. :tag "Org Archive"
  3525. :group 'org-structure)
  3526. (defcustom org-archive-location "%s_archive::"
  3527. "The location where subtrees should be archived.
  3528. The value of this variable is a string, consisting of two parts,
  3529. separated by a double-colon. The first part is a filename and
  3530. the second part is a headline.
  3531. When the filename is omitted, archiving happens in the same file.
  3532. %s in the filename will be replaced by the current file
  3533. name (without the directory part). Archiving to a different file
  3534. is useful to keep archived entries from contributing to the
  3535. Org-mode Agenda.
  3536. The archived entries will be filed as subtrees of the specified
  3537. headline. When the headline is omitted, the subtrees are simply
  3538. filed away at the end of the file, as top-level entries. Also in
  3539. the heading you can use %s to represent the file name, this can be
  3540. useful when using the same archive for a number of different files.
  3541. Here are a few examples:
  3542. \"%s_archive::\"
  3543. If the current file is Projects.org, archive in file
  3544. Projects.org_archive, as top-level trees. This is the default.
  3545. \"::* Archived Tasks\"
  3546. Archive in the current file, under the top-level headline
  3547. \"* Archived Tasks\".
  3548. \"~/org/archive.org::\"
  3549. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3550. \"~/org/archive.org::* From %s\"
  3551. Archive in file ~/org/archive.org (absolute path), under headlines
  3552. \"From FILENAME\" where file name is the current file name.
  3553. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3554. The \"datetree/\" string is special, signifying to archive
  3555. items to the datetree. Items are placed in either the CLOSED
  3556. date of the item, or the current date if there is no CLOSED date.
  3557. The heading will be a subentry to the current date. There doesn't
  3558. need to be a heading, but there always needs to be a slash after
  3559. datetree. For example, to store archived items directly in the
  3560. datetree, use \"~/org/datetree.org::datetree/\".
  3561. \"basement::** Finished Tasks\"
  3562. Archive in file ./basement (relative path), as level 3 trees
  3563. below the level 2 heading \"** Finished Tasks\".
  3564. You may set this option on a per-file basis by adding to the buffer a
  3565. line like
  3566. #+ARCHIVE: basement::** Finished Tasks
  3567. You may also define it locally for a subtree by setting an ARCHIVE property
  3568. in the entry. If such a property is found in an entry, or anywhere up
  3569. the hierarchy, it will be used."
  3570. :group 'org-archive
  3571. :type 'string)
  3572. (defcustom org-archive-tag "ARCHIVE"
  3573. "The tag that marks a subtree as archived.
  3574. An archived subtree does not open during visibility cycling, and does
  3575. not contribute to the agenda listings.
  3576. After changing this, font-lock must be restarted in the relevant buffers to
  3577. get the proper fontification."
  3578. :group 'org-archive
  3579. :group 'org-keywords
  3580. :type 'string)
  3581. (defcustom org-agenda-skip-archived-trees t
  3582. "Non-nil means the agenda will skip any items located in archived trees.
  3583. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3584. variable is no longer recommended, you should leave it at the value t.
  3585. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3586. :group 'org-archive
  3587. :group 'org-agenda-skip
  3588. :type 'boolean)
  3589. (defcustom org-columns-skip-archived-trees t
  3590. "Non-nil means ignore archived trees when creating column view."
  3591. :group 'org-archive
  3592. :group 'org-properties
  3593. :type 'boolean)
  3594. (defcustom org-cycle-open-archived-trees nil
  3595. "Non-nil means `org-cycle' will open archived trees.
  3596. An archived tree is a tree marked with the tag ARCHIVE.
  3597. When nil, archived trees will stay folded. You can still open them with
  3598. normal outline commands like `show-all', but not with the cycling commands."
  3599. :group 'org-archive
  3600. :group 'org-cycle
  3601. :type 'boolean)
  3602. (defcustom org-sparse-tree-open-archived-trees nil
  3603. "Non-nil means sparse tree construction shows matches in archived trees.
  3604. When nil, matches in these trees are highlighted, but the trees are kept in
  3605. collapsed state."
  3606. :group 'org-archive
  3607. :group 'org-sparse-trees
  3608. :type 'boolean)
  3609. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3610. "The default date type when building a sparse tree.
  3611. When this is nil, a date is a scheduled or a deadline timestamp.
  3612. Otherwise, these types are allowed:
  3613. all: all timestamps
  3614. active: only active timestamps (<...>)
  3615. inactive: only inactive timestamps (<...)
  3616. scheduled: only scheduled timestamps
  3617. deadline: only deadline timestamps"
  3618. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3619. (const :tag "All timestamps" all)
  3620. (const :tag "Only active timestamps" active)
  3621. (const :tag "Only inactive timestamps" inactive)
  3622. (const :tag "Only scheduled timestamps" scheduled)
  3623. (const :tag "Only deadline timestamps" deadline))
  3624. :version "24.3"
  3625. :group 'org-sparse-trees)
  3626. (defun org-cycle-hide-archived-subtrees (state)
  3627. "Re-hide all archived subtrees after a visibility state change."
  3628. (when (and (not org-cycle-open-archived-trees)
  3629. (not (memq state '(overview folded))))
  3630. (save-excursion
  3631. (let* ((globalp (memq state '(contents all)))
  3632. (beg (if globalp (point-min) (point)))
  3633. (end (if globalp (point-max) (org-end-of-subtree t))))
  3634. (org-hide-archived-subtrees beg end)
  3635. (goto-char beg)
  3636. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3637. (message "%s" (substitute-command-keys
  3638. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3639. (defun org-force-cycle-archived ()
  3640. "Cycle subtree even if it is archived."
  3641. (interactive)
  3642. (setq this-command 'org-cycle)
  3643. (let ((org-cycle-open-archived-trees t))
  3644. (call-interactively 'org-cycle)))
  3645. (defun org-hide-archived-subtrees (beg end)
  3646. "Re-hide all archived subtrees after a visibility state change."
  3647. (save-excursion
  3648. (let* ((re (concat ":" org-archive-tag ":")))
  3649. (goto-char beg)
  3650. (while (re-search-forward re end t)
  3651. (when (org-at-heading-p)
  3652. (org-flag-subtree t)
  3653. (org-end-of-subtree t))))))
  3654. (declare-function outline-end-of-heading "outline" ())
  3655. (declare-function outline-flag-region "outline" (from to flag))
  3656. (defun org-flag-subtree (flag)
  3657. (save-excursion
  3658. (org-back-to-heading t)
  3659. (outline-end-of-heading)
  3660. (outline-flag-region (point)
  3661. (progn (org-end-of-subtree t) (point))
  3662. flag)))
  3663. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3664. (eval-and-compile
  3665. (org-autoload "org-archive"
  3666. '(org-add-archive-files)))
  3667. ;; Autoload Column View Code
  3668. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3669. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3670. (declare-function org-columns-compute "org-colview" (property))
  3671. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3672. '(org-columns-number-to-string
  3673. org-columns-get-format-and-top-level
  3674. org-columns-compute
  3675. org-columns-remove-overlays))
  3676. ;; Autoload ID code
  3677. (declare-function org-id-store-link "org-id")
  3678. (declare-function org-id-locations-load "org-id")
  3679. (declare-function org-id-locations-save "org-id")
  3680. (defvar org-id-track-globally)
  3681. (org-autoload "org-id"
  3682. '(org-id-new
  3683. org-id-copy
  3684. org-id-get-with-outline-path-completion
  3685. org-id-get-with-outline-drilling))
  3686. ;;; Variables for pre-computed regular expressions, all buffer local
  3687. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3688. "Matches first line of a hidden block.")
  3689. (make-variable-buffer-local 'org-drawer-regexp)
  3690. (defvar org-todo-regexp nil
  3691. "Matches any of the TODO state keywords.")
  3692. (make-variable-buffer-local 'org-todo-regexp)
  3693. (defvar org-not-done-regexp nil
  3694. "Matches any of the TODO state keywords except the last one.")
  3695. (make-variable-buffer-local 'org-not-done-regexp)
  3696. (defvar org-not-done-heading-regexp nil
  3697. "Matches a TODO headline that is not done.")
  3698. (make-variable-buffer-local 'org-not-done-regexp)
  3699. (defvar org-todo-line-regexp nil
  3700. "Matches a headline and puts TODO state into group 2 if present.")
  3701. (make-variable-buffer-local 'org-todo-line-regexp)
  3702. (defvar org-complex-heading-regexp nil
  3703. "Matches a headline and puts everything into groups:
  3704. group 1: the stars
  3705. group 2: The todo keyword, maybe
  3706. group 3: Priority cookie
  3707. group 4: True headline
  3708. group 5: Tags")
  3709. (make-variable-buffer-local 'org-complex-heading-regexp)
  3710. (defvar org-complex-heading-regexp-format nil
  3711. "Printf format to make regexp to match an exact headline.
  3712. This regexp will match the headline of any node which has the
  3713. exact headline text that is put into the format, but may have any
  3714. TODO state, priority and tags.")
  3715. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3716. (defvar org-todo-line-tags-regexp nil
  3717. "Matches a headline and puts TODO state into group 2 if present.
  3718. Also put tags into group 4 if tags are present.")
  3719. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3720. (defvar org-ds-keyword-length 12
  3721. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3722. (make-variable-buffer-local 'org-ds-keyword-length)
  3723. (defvar org-deadline-regexp nil
  3724. "Matches the DEADLINE keyword.")
  3725. (make-variable-buffer-local 'org-deadline-regexp)
  3726. (defvar org-deadline-time-regexp nil
  3727. "Matches the DEADLINE keyword together with a time stamp.")
  3728. (make-variable-buffer-local 'org-deadline-time-regexp)
  3729. (defvar org-deadline-line-regexp nil
  3730. "Matches the DEADLINE keyword and the rest of the line.")
  3731. (make-variable-buffer-local 'org-deadline-line-regexp)
  3732. (defvar org-scheduled-regexp nil
  3733. "Matches the SCHEDULED keyword.")
  3734. (make-variable-buffer-local 'org-scheduled-regexp)
  3735. (defvar org-scheduled-time-regexp nil
  3736. "Matches the SCHEDULED keyword together with a time stamp.")
  3737. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3738. (defvar org-closed-time-regexp nil
  3739. "Matches the CLOSED keyword together with a time stamp.")
  3740. (make-variable-buffer-local 'org-closed-time-regexp)
  3741. (defvar org-keyword-time-regexp nil
  3742. "Matches any of the 4 keywords, together with the time stamp.")
  3743. (make-variable-buffer-local 'org-keyword-time-regexp)
  3744. (defvar org-keyword-time-not-clock-regexp nil
  3745. "Matches any of the 3 keywords, together with the time stamp.")
  3746. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3747. (defvar org-maybe-keyword-time-regexp nil
  3748. "Matches a timestamp, possibly preceded by a keyword.")
  3749. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3750. (defvar org-all-time-keywords nil
  3751. "List of time keywords.")
  3752. (make-variable-buffer-local 'org-all-time-keywords)
  3753. (defconst org-plain-time-of-day-regexp
  3754. (concat
  3755. "\\(\\<[012]?[0-9]"
  3756. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3757. "\\(--?"
  3758. "\\(\\<[012]?[0-9]"
  3759. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3760. "\\)?")
  3761. "Regular expression to match a plain time or time range.
  3762. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3763. groups carry important information:
  3764. 0 the full match
  3765. 1 the first time, range or not
  3766. 8 the second time, if it is a range.")
  3767. (defconst org-plain-time-extension-regexp
  3768. (concat
  3769. "\\(\\<[012]?[0-9]"
  3770. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3771. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3772. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3773. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3774. groups carry important information:
  3775. 0 the full match
  3776. 7 hours of duration
  3777. 9 minutes of duration")
  3778. (defconst org-stamp-time-of-day-regexp
  3779. (concat
  3780. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3781. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3782. "\\(--?"
  3783. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3784. "Regular expression to match a timestamp time or time range.
  3785. After a match, the following groups carry important information:
  3786. 0 the full match
  3787. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3788. 2 the first time, range or not
  3789. 4 the second time, if it is a range.")
  3790. (defconst org-startup-options
  3791. '(("fold" org-startup-folded t)
  3792. ("overview" org-startup-folded t)
  3793. ("nofold" org-startup-folded nil)
  3794. ("showall" org-startup-folded nil)
  3795. ("showeverything" org-startup-folded showeverything)
  3796. ("content" org-startup-folded content)
  3797. ("indent" org-startup-indented t)
  3798. ("noindent" org-startup-indented nil)
  3799. ("hidestars" org-hide-leading-stars t)
  3800. ("showstars" org-hide-leading-stars nil)
  3801. ("odd" org-odd-levels-only t)
  3802. ("oddeven" org-odd-levels-only nil)
  3803. ("align" org-startup-align-all-tables t)
  3804. ("noalign" org-startup-align-all-tables nil)
  3805. ("inlineimages" org-startup-with-inline-images t)
  3806. ("noinlineimages" org-startup-with-inline-images nil)
  3807. ("customtime" org-display-custom-times t)
  3808. ("logdone" org-log-done time)
  3809. ("lognotedone" org-log-done note)
  3810. ("nologdone" org-log-done nil)
  3811. ("lognoteclock-out" org-log-note-clock-out t)
  3812. ("nolognoteclock-out" org-log-note-clock-out nil)
  3813. ("logrepeat" org-log-repeat state)
  3814. ("lognoterepeat" org-log-repeat note)
  3815. ("nologrepeat" org-log-repeat nil)
  3816. ("logreschedule" org-log-reschedule time)
  3817. ("lognotereschedule" org-log-reschedule note)
  3818. ("nologreschedule" org-log-reschedule nil)
  3819. ("logredeadline" org-log-redeadline time)
  3820. ("lognoteredeadline" org-log-redeadline note)
  3821. ("nologredeadline" org-log-redeadline nil)
  3822. ("logrefile" org-log-refile time)
  3823. ("lognoterefile" org-log-refile note)
  3824. ("nologrefile" org-log-refile nil)
  3825. ("fninline" org-footnote-define-inline t)
  3826. ("nofninline" org-footnote-define-inline nil)
  3827. ("fnlocal" org-footnote-section nil)
  3828. ("fnauto" org-footnote-auto-label t)
  3829. ("fnprompt" org-footnote-auto-label nil)
  3830. ("fnconfirm" org-footnote-auto-label confirm)
  3831. ("fnplain" org-footnote-auto-label plain)
  3832. ("fnadjust" org-footnote-auto-adjust t)
  3833. ("nofnadjust" org-footnote-auto-adjust nil)
  3834. ("constcgs" constants-unit-system cgs)
  3835. ("constSI" constants-unit-system SI)
  3836. ("noptag" org-tag-persistent-alist nil)
  3837. ("hideblocks" org-hide-block-startup t)
  3838. ("nohideblocks" org-hide-block-startup nil)
  3839. ("beamer" org-startup-with-beamer-mode t)
  3840. ("entitiespretty" org-pretty-entities t)
  3841. ("entitiesplain" org-pretty-entities nil))
  3842. "Variable associated with STARTUP options for org-mode.
  3843. Each element is a list of three items: the startup options (as written
  3844. in the #+STARTUP line), the corresponding variable, and the value to set
  3845. this variable to if the option is found. An optional forth element PUSH
  3846. means to push this value onto the list in the variable.")
  3847. (defun org-update-property-plist (key val props)
  3848. "Update PROPS with KEY and VAL."
  3849. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3850. (key (if appending (substring key 0 (- (length key) 1)) key))
  3851. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3852. (previous (cdr (assoc key props))))
  3853. (if appending
  3854. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3855. (cons (cons key val) remainder))))
  3856. (defconst org-block-regexp
  3857. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3858. "Regular expression for hiding blocks.")
  3859. (defconst org-heading-keyword-regexp-format
  3860. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3861. "Printf format for a regexp matching an headline with some keyword.
  3862. This regexp will match the headline of any node which has the
  3863. exact keyword that is put into the format. The keyword isn't in
  3864. any group by default, but the stars and the body are.")
  3865. (defconst org-heading-keyword-maybe-regexp-format
  3866. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3867. "Printf format for a regexp matching an headline, possibly with some keyword.
  3868. This regexp can match any headline with the specified keyword, or
  3869. without a keyword. The keyword isn't in any group by default,
  3870. but the stars and the body are.")
  3871. (defun org-set-regexps-and-options ()
  3872. "Precompute regular expressions for current buffer."
  3873. (when (derived-mode-p 'org-mode)
  3874. (org-set-local 'org-todo-kwd-alist nil)
  3875. (org-set-local 'org-todo-key-alist nil)
  3876. (org-set-local 'org-todo-key-trigger nil)
  3877. (org-set-local 'org-todo-keywords-1 nil)
  3878. (org-set-local 'org-done-keywords nil)
  3879. (org-set-local 'org-todo-heads nil)
  3880. (org-set-local 'org-todo-sets nil)
  3881. (org-set-local 'org-todo-log-states nil)
  3882. (org-set-local 'org-file-properties nil)
  3883. (org-set-local 'org-file-tags nil)
  3884. (let ((re (org-make-options-regexp
  3885. '("CATEGORY" "TODO" "COLUMNS"
  3886. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3887. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3888. "OPTIONS")
  3889. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3890. (splitre "[ \t]+")
  3891. (scripts org-use-sub-superscripts)
  3892. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3893. tail sep kws1 prio props ftags drawers beamer-p
  3894. ext-setup-or-nil setup-contents (start 0))
  3895. (save-excursion
  3896. (save-restriction
  3897. (widen)
  3898. (goto-char (point-min))
  3899. (while (or (and ext-setup-or-nil
  3900. (string-match re ext-setup-or-nil start)
  3901. (setq start (match-end 0)))
  3902. (and (setq ext-setup-or-nil nil start 0)
  3903. (re-search-forward re nil t)))
  3904. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3905. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3906. (if (stringp value) (setq value (org-trim value)))
  3907. (cond
  3908. ((equal key "CATEGORY")
  3909. (setq cat value))
  3910. ((member key '("SEQ_TODO" "TODO"))
  3911. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3912. ((equal key "TYP_TODO")
  3913. (push (cons 'type (org-split-string value splitre)) kwds))
  3914. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3915. ;; general TODO-like setup
  3916. (push (cons (intern (downcase (match-string 1 key)))
  3917. (org-split-string value splitre)) kwds))
  3918. ((equal key "TAGS")
  3919. (setq tags (append tags (if tags '("\\n") nil)
  3920. (org-split-string value splitre))))
  3921. ((equal key "COLUMNS")
  3922. (org-set-local 'org-columns-default-format value))
  3923. ((equal key "LINK")
  3924. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3925. (push (cons (match-string 1 value)
  3926. (org-trim (match-string 2 value)))
  3927. links)))
  3928. ((equal key "PRIORITIES")
  3929. (setq prio (org-split-string value " +")))
  3930. ((equal key "PROPERTY")
  3931. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3932. (setq props (org-update-property-plist (match-string 1 value)
  3933. (match-string 2 value)
  3934. props))))
  3935. ((equal key "FILETAGS")
  3936. (when (string-match "\\S-" value)
  3937. (setq ftags
  3938. (append
  3939. ftags
  3940. (apply 'append
  3941. (mapcar (lambda (x) (org-split-string x ":"))
  3942. (org-split-string value)))))))
  3943. ((equal key "DRAWERS")
  3944. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  3945. ((equal key "CONSTANTS")
  3946. (setq const (append const (org-split-string value splitre))))
  3947. ((equal key "STARTUP")
  3948. (let ((opts (org-split-string value splitre))
  3949. l var val)
  3950. (while (setq l (pop opts))
  3951. (when (setq l (assoc l org-startup-options))
  3952. (setq var (nth 1 l) val (nth 2 l))
  3953. (if (not (nth 3 l))
  3954. (set (make-local-variable var) val)
  3955. (if (not (listp (symbol-value var)))
  3956. (set (make-local-variable var) nil))
  3957. (set (make-local-variable var) (symbol-value var))
  3958. (add-to-list var val))))))
  3959. ((equal key "ARCHIVE")
  3960. (setq arch value)
  3961. (remove-text-properties 0 (length arch)
  3962. '(face t fontified t) arch))
  3963. ((equal key "LATEX_CLASS")
  3964. (setq beamer-p (equal value "beamer")))
  3965. ((equal key "OPTIONS")
  3966. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3967. (setq scripts (read (match-string 2 value)))))
  3968. ((equal key "SETUPFILE")
  3969. (setq setup-contents (org-file-contents
  3970. (expand-file-name
  3971. (org-remove-double-quotes value))
  3972. 'noerror))
  3973. (if (not ext-setup-or-nil)
  3974. (setq ext-setup-or-nil setup-contents start 0)
  3975. (setq ext-setup-or-nil
  3976. (concat (substring ext-setup-or-nil 0 start)
  3977. "\n" setup-contents "\n"
  3978. (substring ext-setup-or-nil start)))))))
  3979. ;; search for property blocks
  3980. (goto-char (point-min))
  3981. (while (re-search-forward org-block-regexp nil t)
  3982. (when (equal "PROPERTY" (upcase (match-string 1)))
  3983. (setq value (replace-regexp-in-string
  3984. "[\n\r]" " " (match-string 4)))
  3985. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3986. (setq props (org-update-property-plist (match-string 1 value)
  3987. (match-string 2 value)
  3988. props)))))))
  3989. (org-set-local 'org-use-sub-superscripts scripts)
  3990. (when cat
  3991. (org-set-local 'org-category (intern cat))
  3992. (push (cons "CATEGORY" cat) props))
  3993. (when prio
  3994. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3995. (setq prio (mapcar 'string-to-char prio))
  3996. (org-set-local 'org-highest-priority (nth 0 prio))
  3997. (org-set-local 'org-lowest-priority (nth 1 prio))
  3998. (org-set-local 'org-default-priority (nth 2 prio)))
  3999. (and props (org-set-local 'org-file-properties (nreverse props)))
  4000. (and ftags (org-set-local 'org-file-tags
  4001. (mapcar 'org-add-prop-inherited ftags)))
  4002. (and drawers (org-set-local 'org-drawers drawers))
  4003. (and arch (org-set-local 'org-archive-location arch))
  4004. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4005. ;; Process the TODO keywords
  4006. (unless kwds
  4007. ;; Use the global values as if they had been given locally.
  4008. (setq kwds (default-value 'org-todo-keywords))
  4009. (if (stringp (car kwds))
  4010. (setq kwds (list (cons org-todo-interpretation
  4011. (default-value 'org-todo-keywords)))))
  4012. (setq kwds (reverse kwds)))
  4013. (setq kwds (nreverse kwds))
  4014. (let (inter kws kw)
  4015. (while (setq kws (pop kwds))
  4016. (let ((kws (or
  4017. (run-hook-with-args-until-success
  4018. 'org-todo-setup-filter-hook kws)
  4019. kws)))
  4020. (setq inter (pop kws) sep (member "|" kws)
  4021. kws0 (delete "|" (copy-sequence kws))
  4022. kwsa nil
  4023. kws1 (mapcar
  4024. (lambda (x)
  4025. ;; 1 2
  4026. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4027. (progn
  4028. (setq kw (match-string 1 x)
  4029. key (and (match-end 2) (match-string 2 x))
  4030. log (org-extract-log-state-settings x))
  4031. (push (cons kw (and key (string-to-char key))) kwsa)
  4032. (and log (push log org-todo-log-states))
  4033. kw)
  4034. (error "Invalid TODO keyword %s" x)))
  4035. kws0)
  4036. kwsa (if kwsa (append '((:startgroup))
  4037. (nreverse kwsa)
  4038. '((:endgroup))))
  4039. hw (car kws1)
  4040. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4041. tail (list inter hw (car dws) (org-last dws))))
  4042. (add-to-list 'org-todo-heads hw 'append)
  4043. (push kws1 org-todo-sets)
  4044. (setq org-done-keywords (append org-done-keywords dws nil))
  4045. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4046. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4047. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4048. (setq org-todo-sets (nreverse org-todo-sets)
  4049. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4050. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4051. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4052. ;; Process the constants
  4053. (when const
  4054. (let (e cst)
  4055. (while (setq e (pop const))
  4056. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4057. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4058. (setq org-table-formula-constants-local cst)))
  4059. ;; Process the tags.
  4060. (when tags
  4061. (let (e tgs)
  4062. (while (setq e (pop tags))
  4063. (cond
  4064. ((equal e "{") (push '(:startgroup) tgs))
  4065. ((equal e "}") (push '(:endgroup) tgs))
  4066. ((equal e "\\n") (push '(:newline) tgs))
  4067. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4068. (push (cons (match-string 1 e)
  4069. (string-to-char (match-string 2 e)))
  4070. tgs))
  4071. (t (push (list e) tgs))))
  4072. (org-set-local 'org-tag-alist nil)
  4073. (while (setq e (pop tgs))
  4074. (or (and (stringp (car e))
  4075. (assoc (car e) org-tag-alist))
  4076. (push e org-tag-alist)))))
  4077. ;; Compute the regular expressions and other local variables.
  4078. ;; Using `org-outline-regexp-bol' would complicate them much,
  4079. ;; because of the fixed white space at the end of that string.
  4080. (if (not org-done-keywords)
  4081. (setq org-done-keywords (and org-todo-keywords-1
  4082. (list (org-last org-todo-keywords-1)))))
  4083. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4084. (length org-scheduled-string)
  4085. (length org-clock-string)
  4086. (length org-closed-string)))
  4087. org-drawer-regexp
  4088. (concat "^[ \t]*:\\("
  4089. (mapconcat 'regexp-quote org-drawers "\\|")
  4090. "\\):[ \t]*$")
  4091. org-not-done-keywords
  4092. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4093. org-todo-regexp
  4094. (concat "\\("
  4095. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4096. "\\)")
  4097. org-not-done-regexp
  4098. (concat "\\("
  4099. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4100. "\\)")
  4101. org-not-done-heading-regexp
  4102. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4103. org-todo-line-regexp
  4104. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4105. org-complex-heading-regexp
  4106. (concat "^\\(\\*+\\)"
  4107. "\\(?: +" org-todo-regexp "\\)?"
  4108. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4109. "\\(?: +\\(.*?\\)\\)??"
  4110. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4111. "[ \t]*$")
  4112. org-complex-heading-regexp-format
  4113. (concat "^\\(\\*+\\)"
  4114. "\\(?: +" org-todo-regexp "\\)?"
  4115. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4116. "\\(?: +"
  4117. ;; Stats cookies can be stuck to body.
  4118. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4119. "\\(%s\\)"
  4120. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4121. "\\)"
  4122. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4123. "[ \t]*$")
  4124. org-todo-line-tags-regexp
  4125. (concat "^\\(\\*+\\)"
  4126. "\\(?: +" org-todo-regexp "\\)?"
  4127. "\\(?: +\\(.*?\\)\\)??"
  4128. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4129. "[ \t]*$")
  4130. org-deadline-regexp (concat "\\<" org-deadline-string)
  4131. org-deadline-time-regexp
  4132. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4133. org-deadline-line-regexp
  4134. (concat "\\<\\(" org-deadline-string "\\).*")
  4135. org-scheduled-regexp
  4136. (concat "\\<" org-scheduled-string)
  4137. org-scheduled-time-regexp
  4138. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4139. org-closed-time-regexp
  4140. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4141. org-keyword-time-regexp
  4142. (concat "\\<\\(" org-scheduled-string
  4143. "\\|" org-deadline-string
  4144. "\\|" org-closed-string
  4145. "\\|" org-clock-string "\\)"
  4146. " *[[<]\\([^]>]+\\)[]>]")
  4147. org-keyword-time-not-clock-regexp
  4148. (concat "\\<\\(" org-scheduled-string
  4149. "\\|" org-deadline-string
  4150. "\\|" org-closed-string
  4151. "\\)"
  4152. " *[[<]\\([^]>]+\\)[]>]")
  4153. org-maybe-keyword-time-regexp
  4154. (concat "\\(\\<\\(" org-scheduled-string
  4155. "\\|" org-deadline-string
  4156. "\\|" org-closed-string
  4157. "\\|" org-clock-string "\\)\\)?"
  4158. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4159. org-all-time-keywords
  4160. (mapcar (lambda (w) (substring w 0 -1))
  4161. (list org-scheduled-string org-deadline-string
  4162. org-clock-string org-closed-string))
  4163. )
  4164. (org-compute-latex-and-specials-regexp)
  4165. (org-set-font-lock-defaults))))
  4166. (defun org-file-contents (file &optional noerror)
  4167. "Return the contents of FILE, as a string."
  4168. (if (or (not file)
  4169. (not (file-readable-p file)))
  4170. (if noerror
  4171. (progn
  4172. (message "Cannot read file \"%s\"" file)
  4173. (ding) (sit-for 2)
  4174. "")
  4175. (error "Cannot read file \"%s\"" file))
  4176. (with-temp-buffer
  4177. (insert-file-contents file)
  4178. (buffer-string))))
  4179. (defun org-extract-log-state-settings (x)
  4180. "Extract the log state setting from a TODO keyword string.
  4181. This will extract info from a string like \"WAIT(w@/!)\"."
  4182. (let (kw key log1 log2)
  4183. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4184. (setq kw (match-string 1 x)
  4185. key (and (match-end 2) (match-string 2 x))
  4186. log1 (and (match-end 3) (match-string 3 x))
  4187. log2 (and (match-end 4) (match-string 4 x)))
  4188. (and (or log1 log2)
  4189. (list kw
  4190. (and log1 (if (equal log1 "!") 'time 'note))
  4191. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4192. (defun org-remove-keyword-keys (list)
  4193. "Remove a pair of parenthesis at the end of each string in LIST."
  4194. (mapcar (lambda (x)
  4195. (if (string-match "(.*)$" x)
  4196. (substring x 0 (match-beginning 0))
  4197. x))
  4198. list))
  4199. (defun org-assign-fast-keys (alist)
  4200. "Assign fast keys to a keyword-key alist.
  4201. Respect keys that are already there."
  4202. (let (new e (alt ?0))
  4203. (while (setq e (pop alist))
  4204. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4205. (cdr e)) ;; Key already assigned.
  4206. (push e new)
  4207. (let ((clist (string-to-list (downcase (car e))))
  4208. (used (append new alist)))
  4209. (when (= (car clist) ?@)
  4210. (pop clist))
  4211. (while (and clist (rassoc (car clist) used))
  4212. (pop clist))
  4213. (unless clist
  4214. (while (rassoc alt used)
  4215. (incf alt)))
  4216. (push (cons (car e) (or (car clist) alt)) new))))
  4217. (nreverse new)))
  4218. ;;; Some variables used in various places
  4219. (defvar org-window-configuration nil
  4220. "Used in various places to store a window configuration.")
  4221. (defvar org-selected-window nil
  4222. "Used in various places to store a window configuration.")
  4223. (defvar org-finish-function nil
  4224. "Function to be called when `C-c C-c' is used.
  4225. This is for getting out of special buffers like capture.")
  4226. ;; FIXME: Occasionally check by commenting these, to make sure
  4227. ;; no other functions uses these, forgetting to let-bind them.
  4228. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4229. (defvar org-last-state)
  4230. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4231. ;; Defined somewhere in this file, but used before definition.
  4232. (defvar org-entities) ;; defined in org-entities.el
  4233. (defvar org-struct-menu)
  4234. (defvar org-org-menu)
  4235. (defvar org-tbl-menu)
  4236. ;;;; Define the Org-mode
  4237. ;; We use a before-change function to check if a table might need
  4238. ;; an update.
  4239. (defvar org-table-may-need-update t
  4240. "Indicates that a table might need an update.
  4241. This variable is set by `org-before-change-function'.
  4242. `org-table-align' sets it back to nil.")
  4243. (defun org-before-change-function (beg end)
  4244. "Every change indicates that a table might need an update."
  4245. (setq org-table-may-need-update t))
  4246. (defvar org-mode-map)
  4247. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4248. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4249. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4250. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4251. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4252. (defvar org-table-buffer-is-an nil)
  4253. (defvar bidi-paragraph-direction)
  4254. (defvar buffer-face-mode-face)
  4255. (require 'outline)
  4256. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4257. (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"))
  4258. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4259. ;; Other stuff we need.
  4260. (require 'time-date)
  4261. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4262. (require 'easymenu)
  4263. (require 'overlay)
  4264. (require 'org-macs)
  4265. (require 'org-entities)
  4266. ;; (require 'org-compat) moved higher up in the file before it is first used
  4267. (require 'org-faces)
  4268. (require 'org-list)
  4269. (require 'org-pcomplete)
  4270. (require 'org-src)
  4271. (require 'org-footnote)
  4272. ;; babel
  4273. (require 'ob)
  4274. (require 'ob-table)
  4275. (require 'ob-lob)
  4276. (require 'ob-ref)
  4277. (require 'ob-tangle)
  4278. (require 'ob-comint)
  4279. (require 'ob-keys)
  4280. ;;;###autoload
  4281. (define-derived-mode org-mode outline-mode "Org"
  4282. "Outline-based notes management and organizer, alias
  4283. \"Carsten's outline-mode for keeping track of everything.\"
  4284. Org-mode develops organizational tasks around a NOTES file which
  4285. contains information about projects as plain text. Org-mode is
  4286. implemented on top of outline-mode, which is ideal to keep the content
  4287. of large files well structured. It supports ToDo items, deadlines and
  4288. time stamps, which magically appear in the diary listing of the Emacs
  4289. calendar. Tables are easily created with a built-in table editor.
  4290. Plain text URL-like links connect to websites, emails (VM), Usenet
  4291. messages (Gnus), BBDB entries, and any files related to the project.
  4292. For printing and sharing of notes, an Org-mode file (or a part of it)
  4293. can be exported as a structured ASCII or HTML file.
  4294. The following commands are available:
  4295. \\{org-mode-map}"
  4296. ;; Get rid of Outline menus, they are not needed
  4297. ;; Need to do this here because define-derived-mode sets up
  4298. ;; the keymap so late. Still, it is a waste to call this each time
  4299. ;; we switch another buffer into org-mode.
  4300. (if (featurep 'xemacs)
  4301. (when (boundp 'outline-mode-menu-heading)
  4302. ;; Assume this is Greg's port, it uses easymenu
  4303. (easy-menu-remove outline-mode-menu-heading)
  4304. (easy-menu-remove outline-mode-menu-show)
  4305. (easy-menu-remove outline-mode-menu-hide))
  4306. (define-key org-mode-map [menu-bar headings] 'undefined)
  4307. (define-key org-mode-map [menu-bar hide] 'undefined)
  4308. (define-key org-mode-map [menu-bar show] 'undefined))
  4309. (org-load-modules-maybe)
  4310. (easy-menu-add org-org-menu)
  4311. (easy-menu-add org-tbl-menu)
  4312. (org-install-agenda-files-menu)
  4313. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4314. (add-to-invisibility-spec '(org-cwidth))
  4315. (add-to-invisibility-spec '(org-hide-block . t))
  4316. (when (featurep 'xemacs)
  4317. (org-set-local 'line-move-ignore-invisible t))
  4318. (org-set-local 'outline-regexp org-outline-regexp)
  4319. (org-set-local 'outline-level 'org-outline-level)
  4320. (setq bidi-paragraph-direction 'left-to-right)
  4321. (when (and org-ellipsis
  4322. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4323. (fboundp 'make-glyph-code))
  4324. (unless org-display-table
  4325. (setq org-display-table (make-display-table)))
  4326. (set-display-table-slot
  4327. org-display-table 4
  4328. (vconcat (mapcar
  4329. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4330. org-ellipsis)))
  4331. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4332. (setq buffer-display-table org-display-table))
  4333. (org-set-regexps-and-options)
  4334. (when (and org-tag-faces (not org-tags-special-faces-re))
  4335. ;; tag faces set outside customize.... force initialization.
  4336. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4337. ;; Calc embedded
  4338. (org-set-local 'calc-embedded-open-mode "# ")
  4339. (modify-syntax-entry ?@ "w")
  4340. (if org-startup-truncated (setq truncate-lines t))
  4341. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4342. (org-set-local 'font-lock-unfontify-region-function
  4343. 'org-unfontify-region)
  4344. ;; Activate before-change-function
  4345. (org-set-local 'org-table-may-need-update t)
  4346. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4347. 'local)
  4348. ;; Check for running clock before killing a buffer
  4349. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4350. ;; Indentation.
  4351. (org-set-local 'indent-line-function 'org-indent-line)
  4352. (org-set-local 'indent-region-function 'org-indent-region)
  4353. ;; Initialize radio targets.
  4354. (org-update-radio-target-regexp)
  4355. ;; Filling and auto-filling.
  4356. (org-setup-filling)
  4357. ;; Comments.
  4358. (org-setup-comments-handling)
  4359. ;; Beginning/end of defun
  4360. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4361. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4362. ;; Next error for sparse trees
  4363. (org-set-local 'next-error-function 'org-occur-next-match)
  4364. ;; Make sure dependence stuff works reliably, even for users who set it
  4365. ;; too late :-(
  4366. (if org-enforce-todo-dependencies
  4367. (add-hook 'org-blocker-hook
  4368. 'org-block-todo-from-children-or-siblings-or-parent)
  4369. (remove-hook 'org-blocker-hook
  4370. 'org-block-todo-from-children-or-siblings-or-parent))
  4371. (if org-enforce-todo-checkbox-dependencies
  4372. (add-hook 'org-blocker-hook
  4373. 'org-block-todo-from-checkboxes)
  4374. (remove-hook 'org-blocker-hook
  4375. 'org-block-todo-from-checkboxes))
  4376. ;; Align options lines
  4377. (org-set-local
  4378. 'align-mode-rules-list
  4379. '((org-in-buffer-settings
  4380. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4381. (modes . '(org-mode)))))
  4382. ;; Imenu
  4383. (org-set-local 'imenu-create-index-function
  4384. 'org-imenu-get-tree)
  4385. ;; Make isearch reveal context
  4386. (if (or (featurep 'xemacs)
  4387. (not (boundp 'outline-isearch-open-invisible-function)))
  4388. ;; Emacs 21 and XEmacs make use of the hook
  4389. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4390. ;; Emacs 22 deals with this through a special variable
  4391. (org-set-local 'outline-isearch-open-invisible-function
  4392. (lambda (&rest ignore) (org-show-context 'isearch))))
  4393. ;; Setup the pcomplete hooks
  4394. (set (make-local-variable 'pcomplete-command-completion-function)
  4395. 'org-pcomplete-initial)
  4396. (set (make-local-variable 'pcomplete-command-name-function)
  4397. 'org-command-at-point)
  4398. (set (make-local-variable 'pcomplete-default-completion-function)
  4399. 'ignore)
  4400. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4401. 'org-parse-arguments)
  4402. (set (make-local-variable 'pcomplete-termination-string) "")
  4403. (when (>= emacs-major-version 23)
  4404. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4405. ;; If empty file that did not turn on org-mode automatically, make it to.
  4406. (if (and org-insert-mode-line-in-empty-file
  4407. (org-called-interactively-p 'any)
  4408. (= (point-min) (point-max)))
  4409. (insert "# -*- mode: org -*-\n\n"))
  4410. (unless org-inhibit-startup
  4411. (and org-startup-with-beamer-mode (org-beamer-mode))
  4412. (when org-startup-align-all-tables
  4413. (let ((bmp (buffer-modified-p)))
  4414. (org-table-map-tables 'org-table-align 'quietly)
  4415. (set-buffer-modified-p bmp)))
  4416. (when org-startup-with-inline-images
  4417. (org-display-inline-images))
  4418. (unless org-inhibit-startup-visibility-stuff
  4419. (org-set-startup-visibility)))
  4420. ;; Try to set org-hide correctly
  4421. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4422. (when (fboundp 'abbrev-table-put)
  4423. (abbrev-table-put org-mode-abbrev-table
  4424. :parents (list text-mode-abbrev-table)))
  4425. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4426. (defun org-find-invisible-foreground ()
  4427. (let ((candidates (remove
  4428. "unspecified-bg"
  4429. (nconc
  4430. (list (face-background 'default)
  4431. (face-background 'org-default))
  4432. (mapcar
  4433. (lambda (alist)
  4434. (when (boundp alist)
  4435. (cdr (assoc 'background-color (symbol-value alist)))))
  4436. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4437. (list (face-foreground 'org-hide))))))
  4438. (car (remove nil candidates))))
  4439. (defun org-current-time ()
  4440. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4441. (if (> (car org-time-stamp-rounding-minutes) 1)
  4442. (let ((r (car org-time-stamp-rounding-minutes))
  4443. (time (decode-time)))
  4444. (apply 'encode-time
  4445. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4446. (nthcdr 2 time))))
  4447. (current-time)))
  4448. (defun org-today ()
  4449. "Return today date, considering `org-extend-today-until'."
  4450. (time-to-days
  4451. (time-subtract (current-time)
  4452. (list 0 (* 3600 org-extend-today-until) 0))))
  4453. ;;;; Font-Lock stuff, including the activators
  4454. (defvar org-mouse-map (make-sparse-keymap))
  4455. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4456. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4457. (when org-mouse-1-follows-link
  4458. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4459. (when org-tab-follows-link
  4460. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4461. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4462. (require 'font-lock)
  4463. (defconst org-non-link-chars "]\t\n\r<>")
  4464. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4465. "shell" "elisp" "doi" "message"))
  4466. (defvar org-link-types-re nil
  4467. "Matches a link that has a url-like prefix like \"http:\"")
  4468. (defvar org-link-re-with-space nil
  4469. "Matches a link with spaces, optional angular brackets around it.")
  4470. (defvar org-link-re-with-space2 nil
  4471. "Matches a link with spaces, optional angular brackets around it.")
  4472. (defvar org-link-re-with-space3 nil
  4473. "Matches a link with spaces, only for internal part in bracket links.")
  4474. (defvar org-angle-link-re nil
  4475. "Matches link with angular brackets, spaces are allowed.")
  4476. (defvar org-plain-link-re nil
  4477. "Matches plain link, without spaces.")
  4478. (defvar org-bracket-link-regexp nil
  4479. "Matches a link in double brackets.")
  4480. (defvar org-bracket-link-analytic-regexp nil
  4481. "Regular expression used to analyze links.
  4482. Here is what the match groups contain after a match:
  4483. 1: http:
  4484. 2: http
  4485. 3: path
  4486. 4: [desc]
  4487. 5: desc")
  4488. (defvar org-bracket-link-analytic-regexp++ nil
  4489. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4490. (defvar org-any-link-re nil
  4491. "Regular expression matching any link.")
  4492. (defcustom org-match-sexp-depth 3
  4493. "Number of stacked braces for sub/superscript matching.
  4494. This has to be set before loading org.el to be effective."
  4495. :group 'org-export-translation ; ??????????????????????????/
  4496. :type 'integer)
  4497. (defun org-create-multibrace-regexp (left right n)
  4498. "Create a regular expression which will match a balanced sexp.
  4499. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4500. as single character strings.
  4501. The regexp returned will match the entire expression including the
  4502. delimiters. It will also define a single group which contains the
  4503. match except for the outermost delimiters. The maximum depth of
  4504. stacked delimiters is N. Escaping delimiters is not possible."
  4505. (let* ((nothing (concat "[^" left right "]*?"))
  4506. (or "\\|")
  4507. (re nothing)
  4508. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4509. (while (> n 1)
  4510. (setq n (1- n)
  4511. re (concat re or next)
  4512. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4513. (concat left "\\(" re "\\)" right)))
  4514. (defvar org-match-substring-regexp
  4515. (concat
  4516. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4517. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4518. "\\|"
  4519. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4520. "\\|"
  4521. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4522. "The regular expression matching a sub- or superscript.")
  4523. (defvar org-match-substring-with-braces-regexp
  4524. (concat
  4525. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4526. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4527. "\\)")
  4528. "The regular expression matching a sub- or superscript, forcing braces.")
  4529. (defun org-make-link-regexps ()
  4530. "Update the link regular expressions.
  4531. This should be called after the variable `org-link-types' has changed."
  4532. (setq org-link-types-re
  4533. (concat
  4534. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4535. org-link-re-with-space
  4536. (concat
  4537. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4538. "\\([^" org-non-link-chars " ]"
  4539. "[^" org-non-link-chars "]*"
  4540. "[^" org-non-link-chars " ]\\)>?")
  4541. org-link-re-with-space2
  4542. (concat
  4543. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4544. "\\([^" org-non-link-chars " ]"
  4545. "[^\t\n\r]*"
  4546. "[^" org-non-link-chars " ]\\)>?")
  4547. org-link-re-with-space3
  4548. (concat
  4549. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4550. "\\([^" org-non-link-chars " ]"
  4551. "[^\t\n\r]*\\)")
  4552. org-angle-link-re
  4553. (concat
  4554. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4555. "\\([^" org-non-link-chars " ]"
  4556. "[^" org-non-link-chars "]*"
  4557. "\\)>")
  4558. org-plain-link-re
  4559. (concat
  4560. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4561. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4562. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4563. org-bracket-link-regexp
  4564. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4565. org-bracket-link-analytic-regexp
  4566. (concat
  4567. "\\[\\["
  4568. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4569. "\\([^]]+\\)"
  4570. "\\]"
  4571. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4572. "\\]")
  4573. org-bracket-link-analytic-regexp++
  4574. (concat
  4575. "\\[\\["
  4576. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4577. "\\([^]]+\\)"
  4578. "\\]"
  4579. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4580. "\\]")
  4581. org-any-link-re
  4582. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4583. org-angle-link-re "\\)\\|\\("
  4584. org-plain-link-re "\\)")))
  4585. (org-make-link-regexps)
  4586. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4587. "Regular expression for fast time stamp matching.")
  4588. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4589. "Regular expression for fast time stamp matching.")
  4590. (defconst org-ts-regexp0
  4591. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4592. "Regular expression matching time strings for analysis.
  4593. This one does not require the space after the date, so it can be used
  4594. on a string that terminates immediately after the date.")
  4595. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4596. "Regular expression matching time strings for analysis.")
  4597. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4598. "Regular expression matching time stamps, with groups.")
  4599. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4600. "Regular expression matching time stamps (also [..]), with groups.")
  4601. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4602. "Regular expression matching a time stamp range.")
  4603. (defconst org-tr-regexp-both
  4604. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4605. "Regular expression matching a time stamp range.")
  4606. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4607. org-ts-regexp "\\)?")
  4608. "Regular expression matching a time stamp or time stamp range.")
  4609. (defconst org-tsr-regexp-both
  4610. (concat "[^][]" ;; Don't activate dates in links
  4611. org-ts-regexp-both "\\(--?-?"
  4612. org-ts-regexp-both "\\)?")
  4613. "Regular expression matching a time stamp or time stamp range.
  4614. The time stamps may be either active or inactive.")
  4615. (defvar org-emph-face nil)
  4616. (defun org-do-emphasis-faces (limit)
  4617. "Run through the buffer and add overlays to emphasized strings."
  4618. (let (rtn a)
  4619. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4620. (if (not (= (char-after (match-beginning 3))
  4621. (char-after (match-beginning 4))))
  4622. (progn
  4623. (setq rtn t)
  4624. (setq a (assoc (match-string 3) org-emphasis-alist))
  4625. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4626. 'face
  4627. (nth 1 a))
  4628. (and (nth 4 a)
  4629. (org-remove-flyspell-overlays-in
  4630. (match-beginning 0) (match-end 0)))
  4631. (add-text-properties (match-beginning 2) (match-end 2)
  4632. '(font-lock-multiline t org-emphasis t))
  4633. (when org-hide-emphasis-markers
  4634. (add-text-properties (match-end 4) (match-beginning 5)
  4635. '(invisible org-link))
  4636. (add-text-properties (match-beginning 3) (match-end 3)
  4637. '(invisible org-link)))))
  4638. (backward-char 1))
  4639. rtn))
  4640. (defun org-emphasize (&optional char)
  4641. "Insert or change an emphasis, i.e. a font like bold or italic.
  4642. If there is an active region, change that region to a new emphasis.
  4643. If there is no region, just insert the marker characters and position
  4644. the cursor between them.
  4645. CHAR should be either the marker character, or the first character of the
  4646. HTML tag associated with that emphasis. If CHAR is a space, the means
  4647. to remove the emphasis of the selected region.
  4648. If char is not given (for example in an interactive call) it
  4649. will be prompted for."
  4650. (interactive)
  4651. (let ((eal org-emphasis-alist) e det
  4652. (erc org-emphasis-regexp-components)
  4653. (prompt "")
  4654. (string "") beg end move tag c s)
  4655. (if (org-region-active-p)
  4656. (setq beg (region-beginning) end (region-end)
  4657. string (buffer-substring beg end))
  4658. (setq move t))
  4659. (while (setq e (pop eal))
  4660. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4661. c (aref tag 0))
  4662. (push (cons c (string-to-char (car e))) det)
  4663. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4664. (substring tag 1)))))
  4665. (setq det (nreverse det))
  4666. (unless char
  4667. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4668. (setq char (read-char-exclusive)))
  4669. (setq char (or (cdr (assoc char det)) char))
  4670. (if (equal char ?\ )
  4671. (setq s "" move nil)
  4672. (unless (assoc (char-to-string char) org-emphasis-alist)
  4673. (error "No such emphasis marker: \"%c\"" char))
  4674. (setq s (char-to-string char)))
  4675. (while (and (> (length string) 1)
  4676. (equal (substring string 0 1) (substring string -1))
  4677. (assoc (substring string 0 1) org-emphasis-alist))
  4678. (setq string (substring string 1 -1)))
  4679. (setq string (concat s string s))
  4680. (if beg (delete-region beg end))
  4681. (unless (or (bolp)
  4682. (string-match (concat "[" (nth 0 erc) "\n]")
  4683. (char-to-string (char-before (point)))))
  4684. (insert " "))
  4685. (unless (or (eobp)
  4686. (string-match (concat "[" (nth 1 erc) "\n]")
  4687. (char-to-string (char-after (point)))))
  4688. (insert " ") (backward-char 1))
  4689. (insert string)
  4690. (and move (backward-char 1))))
  4691. (defconst org-nonsticky-props
  4692. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4693. (defsubst org-rear-nonsticky-at (pos)
  4694. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4695. (defun org-activate-plain-links (limit)
  4696. "Run through the buffer and add overlays to links."
  4697. (catch 'exit
  4698. (let (f)
  4699. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4700. (not (org-in-src-block-p)))
  4701. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4702. (setq f (get-text-property (match-beginning 0) 'face))
  4703. (unless (or (org-in-src-block-p)
  4704. (eq f 'org-tag)
  4705. (and (listp f) (memq 'org-tag f)))
  4706. (add-text-properties (match-beginning 0) (match-end 0)
  4707. (list 'mouse-face 'highlight
  4708. 'face 'org-link
  4709. 'keymap org-mouse-map))
  4710. (org-rear-nonsticky-at (match-end 0)))
  4711. t))))
  4712. (defun org-activate-code (limit)
  4713. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4714. (progn
  4715. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4716. (remove-text-properties (match-beginning 0) (match-end 0)
  4717. '(display t invisible t intangible t))
  4718. t)))
  4719. (defcustom org-src-fontify-natively nil
  4720. "When non-nil, fontify code in code blocks."
  4721. :type 'boolean
  4722. :version "24.1"
  4723. :group 'org-appearance
  4724. :group 'org-babel)
  4725. (defcustom org-allow-promoting-top-level-subtree nil
  4726. "When non-nil, allow promoting a top level subtree.
  4727. The leading star of the top level headline will be replaced
  4728. by a #."
  4729. :type 'boolean
  4730. :version "24.1"
  4731. :group 'org-appearance)
  4732. (defun org-fontify-meta-lines-and-blocks (limit)
  4733. (condition-case nil
  4734. (org-fontify-meta-lines-and-blocks-1 limit)
  4735. (error (message "org-mode fontification error"))))
  4736. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4737. "Fontify #+ lines and blocks, in the correct ways."
  4738. (let ((case-fold-search t))
  4739. (if (re-search-forward
  4740. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4741. limit t)
  4742. (let ((beg (match-beginning 0))
  4743. (block-start (match-end 0))
  4744. (block-end nil)
  4745. (lang (match-string 7))
  4746. (beg1 (line-beginning-position 2))
  4747. (dc1 (downcase (match-string 2)))
  4748. (dc3 (downcase (match-string 3)))
  4749. end end1 quoting block-type ovl)
  4750. (cond
  4751. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4752. ;; a single line of backend-specific content
  4753. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4754. (remove-text-properties (match-beginning 0) (match-end 0)
  4755. '(display t invisible t intangible t))
  4756. (add-text-properties (match-beginning 1) (match-end 3)
  4757. '(font-lock-fontified t face org-meta-line))
  4758. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4759. '(font-lock-fontified t face org-block))
  4760. ; for backend-specific code
  4761. t)
  4762. ((and (match-end 4) (equal dc3 "+begin"))
  4763. ;; Truly a block
  4764. (setq block-type (downcase (match-string 5))
  4765. quoting (member block-type org-protecting-blocks))
  4766. (when (re-search-forward
  4767. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4768. nil t) ;; on purpose, we look further than LIMIT
  4769. (setq end (min (point-max) (match-end 0))
  4770. end1 (min (point-max) (1- (match-beginning 0))))
  4771. (setq block-end (match-beginning 0))
  4772. (when quoting
  4773. (remove-text-properties beg end
  4774. '(display t invisible t intangible t)))
  4775. (add-text-properties
  4776. beg end
  4777. '(font-lock-fontified t font-lock-multiline t))
  4778. (add-text-properties beg beg1 '(face org-meta-line))
  4779. (add-text-properties end1 (min (point-max) (1+ end))
  4780. '(face org-meta-line)) ; for end_src
  4781. (cond
  4782. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4783. (org-src-font-lock-fontify-block lang block-start block-end)
  4784. ;; remove old background overlays
  4785. (mapc (lambda (ov)
  4786. (if (eq (overlay-get ov 'face) 'org-block-background)
  4787. (delete-overlay ov)))
  4788. (overlays-at (/ (+ beg1 block-end) 2)))
  4789. ;; add a background overlay
  4790. (setq ovl (make-overlay beg1 block-end))
  4791. (overlay-put ovl 'face 'org-block-background)
  4792. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4793. (quoting
  4794. (add-text-properties beg1 (min (point-max) (1+ end1))
  4795. '(face org-block))) ; end of source block
  4796. ((not org-fontify-quote-and-verse-blocks))
  4797. ((string= block-type "quote")
  4798. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4799. ((string= block-type "verse")
  4800. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4801. (add-text-properties beg beg1 '(face org-block-begin-line))
  4802. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4803. '(face org-block-end-line))
  4804. t))
  4805. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  4806. (add-text-properties
  4807. beg (match-end 3)
  4808. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4809. '(font-lock-fontified t invisible t)
  4810. '(font-lock-fontified t face org-document-info-keyword)))
  4811. (add-text-properties
  4812. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  4813. (if (string-equal dc1 "+title:")
  4814. '(font-lock-fontified t face org-document-title)
  4815. '(font-lock-fontified t face org-document-info))))
  4816. ((or (equal dc1 "+results")
  4817. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  4818. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  4819. "+call:" "+header:" "+headers:" "+name:"))
  4820. (and (match-end 4) (equal dc3 "+attr")))
  4821. (add-text-properties
  4822. beg (match-end 0)
  4823. '(font-lock-fontified t face org-meta-line))
  4824. t)
  4825. ((member dc3 '(" " ""))
  4826. (add-text-properties
  4827. beg (match-end 0)
  4828. '(font-lock-fontified t face font-lock-comment-face)))
  4829. ((not (member (char-after beg) '(?\ ?\t)))
  4830. ;; just any other in-buffer setting, but not indented
  4831. (add-text-properties
  4832. beg (match-end 0)
  4833. '(font-lock-fontified t face org-meta-line))
  4834. t)
  4835. (t nil))))))
  4836. (defun org-activate-angle-links (limit)
  4837. "Run through the buffer and add overlays to links."
  4838. (if (and (re-search-forward org-angle-link-re limit t)
  4839. (not (org-in-src-block-p)))
  4840. (progn
  4841. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4842. (add-text-properties (match-beginning 0) (match-end 0)
  4843. (list 'mouse-face 'highlight
  4844. 'keymap org-mouse-map))
  4845. (org-rear-nonsticky-at (match-end 0))
  4846. t)))
  4847. (defun org-activate-footnote-links (limit)
  4848. "Run through the buffer and add overlays to footnotes."
  4849. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4850. (when fn
  4851. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4852. (org-remove-flyspell-overlays-in beg end)
  4853. (add-text-properties beg end
  4854. (list 'mouse-face 'highlight
  4855. 'keymap org-mouse-map
  4856. 'help-echo
  4857. (if (= (point-at-bol) beg)
  4858. "Footnote definition"
  4859. "Footnote reference")
  4860. 'font-lock-fontified t
  4861. 'font-lock-multiline t
  4862. 'face 'org-footnote))))))
  4863. (defun org-activate-bracket-links (limit)
  4864. "Run through the buffer and add overlays to bracketed links."
  4865. (if (and (re-search-forward org-bracket-link-regexp limit t)
  4866. (not (org-in-src-block-p)))
  4867. (let* ((help (concat "LINK: "
  4868. (org-match-string-no-properties 1)))
  4869. ;; FIXME: above we should remove the escapes.
  4870. ;; but that requires another match, protecting match data,
  4871. ;; a lot of overhead for font-lock.
  4872. (ip (org-maybe-intangible
  4873. (list 'invisible 'org-link
  4874. 'keymap org-mouse-map 'mouse-face 'highlight
  4875. 'font-lock-multiline t 'help-echo help)))
  4876. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4877. 'font-lock-multiline t 'help-echo help)))
  4878. ;; We need to remove the invisible property here. Table narrowing
  4879. ;; may have made some of this invisible.
  4880. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4881. (remove-text-properties (match-beginning 0) (match-end 0)
  4882. '(invisible nil))
  4883. (if (match-end 3)
  4884. (progn
  4885. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4886. (org-rear-nonsticky-at (match-beginning 3))
  4887. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4888. (org-rear-nonsticky-at (match-end 3))
  4889. (add-text-properties (match-end 3) (match-end 0) ip)
  4890. (org-rear-nonsticky-at (match-end 0)))
  4891. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4892. (org-rear-nonsticky-at (match-beginning 1))
  4893. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4894. (org-rear-nonsticky-at (match-end 1))
  4895. (add-text-properties (match-end 1) (match-end 0) ip)
  4896. (org-rear-nonsticky-at (match-end 0)))
  4897. t)))
  4898. (defun org-activate-dates (limit)
  4899. "Run through the buffer and add overlays to dates."
  4900. (if (re-search-forward org-tsr-regexp-both limit t)
  4901. (progn
  4902. (org-remove-flyspell-overlays-in (1+ (match-beginning 0)) (match-end 0))
  4903. (add-text-properties (1+ (match-beginning 0)) (match-end 0)
  4904. (list 'mouse-face 'highlight
  4905. 'keymap org-mouse-map))
  4906. (org-rear-nonsticky-at (match-end 0))
  4907. (when org-display-custom-times
  4908. (if (match-end 3)
  4909. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4910. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4911. t)))
  4912. (defvar org-target-link-regexp nil
  4913. "Regular expression matching radio targets in plain text.")
  4914. (make-variable-buffer-local 'org-target-link-regexp)
  4915. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4916. "Regular expression matching a link target.")
  4917. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4918. "Regular expression matching a radio target.")
  4919. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4920. "Regular expression matching any target.")
  4921. (defun org-activate-target-links (limit)
  4922. "Run through the buffer and add overlays to target matches."
  4923. (when org-target-link-regexp
  4924. (let ((case-fold-search t))
  4925. (if (re-search-forward org-target-link-regexp limit t)
  4926. (progn
  4927. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4928. (add-text-properties (match-beginning 0) (match-end 0)
  4929. (list 'mouse-face 'highlight
  4930. 'keymap org-mouse-map
  4931. 'help-echo "Radio target link"
  4932. 'org-linked-text t))
  4933. (org-rear-nonsticky-at (match-end 0))
  4934. t)))))
  4935. (defun org-update-radio-target-regexp ()
  4936. "Find all radio targets in this file and update the regular expression."
  4937. (interactive)
  4938. (when (memq 'radio org-activate-links)
  4939. (setq org-target-link-regexp
  4940. (org-make-target-link-regexp (org-all-targets 'radio)))
  4941. (org-restart-font-lock)))
  4942. (defun org-hide-wide-columns (limit)
  4943. (let (s e)
  4944. (setq s (text-property-any (point) (or limit (point-max))
  4945. 'org-cwidth t))
  4946. (when s
  4947. (setq e (next-single-property-change s 'org-cwidth))
  4948. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4949. (goto-char e)
  4950. t)))
  4951. (defvar org-latex-and-specials-regexp nil
  4952. "Regular expression for highlighting export special stuff.")
  4953. (defvar org-match-substring-regexp)
  4954. (defvar org-match-substring-with-braces-regexp)
  4955. ;; This should be with the exporter code, but we also use if for font-locking
  4956. (defconst org-export-html-special-string-regexps
  4957. '(("\\\\-" . "&shy;")
  4958. ("---\\([^-]\\)" . "&mdash;\\1")
  4959. ("--\\([^-]\\)" . "&ndash;\\1")
  4960. ("\\.\\.\\." . "&hellip;"))
  4961. "Regular expressions for special string conversion.")
  4962. (defun org-compute-latex-and-specials-regexp ()
  4963. "Compute regular expression for stuff treated specially by exporters."
  4964. (if (not org-highlight-latex-fragments-and-specials)
  4965. (org-set-local 'org-latex-and-specials-regexp nil)
  4966. (require 'org-exp)
  4967. (let*
  4968. ((matchers (plist-get org-format-latex-options :matchers))
  4969. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4970. org-latex-regexps)))
  4971. (org-export-allow-BIND nil)
  4972. (options (org-combine-plists (org-default-export-plist)
  4973. (org-infile-export-plist)))
  4974. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4975. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4976. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4977. (org-export-html-expand (plist-get options :expand-quoted-html))
  4978. (org-export-with-special-strings (plist-get options :special-strings))
  4979. (re-sub
  4980. (cond
  4981. ((equal org-export-with-sub-superscripts '{})
  4982. (list org-match-substring-with-braces-regexp))
  4983. (org-export-with-sub-superscripts
  4984. (list org-match-substring-regexp))))
  4985. (re-latex
  4986. (if org-export-with-LaTeX-fragments
  4987. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4988. (re-macros
  4989. (if org-export-with-TeX-macros
  4990. (list (concat "\\\\"
  4991. (regexp-opt
  4992. (append
  4993. (delq nil
  4994. (mapcar 'car-safe
  4995. (append org-entities-user
  4996. org-entities)))
  4997. (if (boundp 'org-latex-entities)
  4998. (mapcar (lambda (x)
  4999. (or (car-safe x) x))
  5000. org-latex-entities)
  5001. nil))
  5002. 'words))) ; FIXME
  5003. ))
  5004. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5005. (re-special (if org-export-with-special-strings
  5006. (mapcar (lambda (x) (car x))
  5007. org-export-html-special-string-regexps)))
  5008. (re-rest
  5009. (delq nil
  5010. (list
  5011. (if org-export-html-expand "@<[^>\n]+>")
  5012. ))))
  5013. (org-set-local
  5014. 'org-latex-and-specials-regexp
  5015. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5016. re-rest) "\\|")))))
  5017. (defun org-do-latex-and-special-faces (limit)
  5018. "Run through the buffer and add overlays to links."
  5019. (when org-latex-and-specials-regexp
  5020. (let (rtn d)
  5021. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5022. limit t))
  5023. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5024. 'face))
  5025. '(org-code org-verbatim underline)))
  5026. (progn
  5027. (setq rtn t
  5028. d (cond ((member (char-after (1+ (match-beginning 0)))
  5029. '(?_ ?^)) 1)
  5030. (t 0)))
  5031. (font-lock-prepend-text-property
  5032. (+ d (match-beginning 0)) (match-end 0)
  5033. 'face 'org-latex-and-export-specials)
  5034. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5035. '(font-lock-multiline t)))))
  5036. rtn)))
  5037. (defun org-restart-font-lock ()
  5038. "Restart `font-lock-mode', to force refontification."
  5039. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5040. (font-lock-mode -1)
  5041. (font-lock-mode 1)))
  5042. (defun org-all-targets (&optional radio)
  5043. "Return a list of all targets in this file.
  5044. With optional argument RADIO, only find radio targets."
  5045. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5046. rtn)
  5047. (save-excursion
  5048. (goto-char (point-min))
  5049. (while (re-search-forward re nil t)
  5050. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5051. rtn)))
  5052. (defun org-make-target-link-regexp (targets)
  5053. "Make regular expression matching all strings in TARGETS.
  5054. The regular expression finds the targets also if there is a line break
  5055. between words."
  5056. (and targets
  5057. (concat
  5058. "\\<\\("
  5059. (mapconcat
  5060. (lambda (x)
  5061. (setq x (regexp-quote x))
  5062. (while (string-match " +" x)
  5063. (setq x (replace-match "\\s-+" t t x)))
  5064. x)
  5065. targets
  5066. "\\|")
  5067. "\\)\\>")))
  5068. (defun org-activate-tags (limit)
  5069. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5070. (progn
  5071. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5072. (add-text-properties (match-beginning 1) (match-end 1)
  5073. (list 'mouse-face 'highlight
  5074. 'keymap org-mouse-map))
  5075. (org-rear-nonsticky-at (match-end 1))
  5076. t)))
  5077. (defun org-outline-level ()
  5078. "Compute the outline level of the heading at point.
  5079. This function assumes that the cursor is at the beginning of a line matched
  5080. by `outline-regexp'. Otherwise it returns garbage.
  5081. If this is called at a normal headline, the level is the number of stars.
  5082. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5083. (save-excursion
  5084. (looking-at org-outline-regexp)
  5085. (1- (- (match-end 0) (match-beginning 0)))))
  5086. (defvar org-font-lock-keywords nil)
  5087. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5088. "Regular expression matching a property line.")
  5089. (defvar org-font-lock-hook nil
  5090. "Functions to be called for special font lock stuff.")
  5091. (defvar org-font-lock-set-keywords-hook nil
  5092. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5093. This is called after `org-font-lock-extra-keywords' is defined, but before
  5094. it is installed to be used by font lock. This can be useful if something
  5095. needs to be inserted at a specific position in the font-lock sequence.")
  5096. (defun org-font-lock-hook (limit)
  5097. "Run `org-font-lock-hook' within LIMIT."
  5098. (run-hook-with-args 'org-font-lock-hook limit))
  5099. (defun org-set-font-lock-defaults ()
  5100. "Set font lock defaults for the current buffer."
  5101. (let* ((em org-fontify-emphasized-text)
  5102. (lk org-activate-links)
  5103. (org-font-lock-extra-keywords
  5104. (list
  5105. ;; Call the hook
  5106. '(org-font-lock-hook)
  5107. ;; Headlines
  5108. `(,(if org-fontify-whole-heading-line
  5109. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5110. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5111. (1 (org-get-level-face 1))
  5112. (2 (org-get-level-face 2))
  5113. (3 (org-get-level-face 3)))
  5114. ;; Table lines
  5115. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5116. (1 'org-table t))
  5117. ;; Table internals
  5118. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5119. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5120. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5121. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5122. ;; Drawers
  5123. (list org-drawer-regexp '(0 'org-special-keyword t))
  5124. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5125. ;; Properties
  5126. (list org-property-re
  5127. '(1 'org-special-keyword t)
  5128. '(3 'org-property-value t))
  5129. ;; Links
  5130. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5131. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5132. (if (memq 'plain lk) '(org-activate-plain-links))
  5133. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5134. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5135. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5136. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5137. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5138. '(org-hide-wide-columns (0 nil append))
  5139. ;; TODO keyword
  5140. (list (format org-heading-keyword-regexp-format
  5141. org-todo-regexp)
  5142. '(2 (org-get-todo-face 2) t))
  5143. ;; DONE
  5144. (if org-fontify-done-headline
  5145. (list (format org-heading-keyword-regexp-format
  5146. (concat
  5147. "\\(?:"
  5148. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5149. "\\)"))
  5150. '(2 'org-headline-done t))
  5151. nil)
  5152. ;; Priorities
  5153. '(org-font-lock-add-priority-faces)
  5154. ;; Tags
  5155. '(org-font-lock-add-tag-faces)
  5156. ;; Special keywords
  5157. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5158. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5159. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5160. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5161. ;; Emphasis
  5162. (if em
  5163. (if (featurep 'xemacs)
  5164. '(org-do-emphasis-faces (0 nil append))
  5165. '(org-do-emphasis-faces)))
  5166. ;; Checkboxes
  5167. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5168. 1 'org-checkbox prepend)
  5169. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5170. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5171. (0 (org-get-checkbox-statistics-face) t)))
  5172. ;; Description list items
  5173. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5174. 1 'org-list-dt prepend)
  5175. ;; ARCHIVEd headings
  5176. (list (concat
  5177. org-outline-regexp-bol
  5178. "\\(.*:" org-archive-tag ":.*\\)")
  5179. '(1 'org-archived prepend))
  5180. ;; Specials
  5181. '(org-do-latex-and-special-faces)
  5182. '(org-fontify-entities)
  5183. '(org-raise-scripts)
  5184. ;; Code
  5185. '(org-activate-code (1 'org-code t))
  5186. ;; COMMENT
  5187. (list (format org-heading-keyword-regexp-format
  5188. (concat "\\("
  5189. org-comment-string "\\|" org-quote-string
  5190. "\\)"))
  5191. '(2 'org-special-keyword t))
  5192. ;; Blocks and meta lines
  5193. '(org-fontify-meta-lines-and-blocks)
  5194. )))
  5195. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5196. (run-hooks 'org-font-lock-set-keywords-hook)
  5197. ;; Now set the full font-lock-keywords
  5198. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5199. (org-set-local 'font-lock-defaults
  5200. '(org-font-lock-keywords t nil nil backward-paragraph))
  5201. (kill-local-variable 'font-lock-keywords) nil))
  5202. (defun org-toggle-pretty-entities ()
  5203. "Toggle the composition display of entities as UTF8 characters."
  5204. (interactive)
  5205. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5206. (org-restart-font-lock)
  5207. (if org-pretty-entities
  5208. (message "Entities are displayed as UTF8 characters")
  5209. (save-restriction
  5210. (widen)
  5211. (org-decompose-region (point-min) (point-max))
  5212. (message "Entities are displayed plain"))))
  5213. (defvar org-custom-properties-overlays nil
  5214. "List of overlays used for custom properties.")
  5215. (make-variable-buffer-local 'org-custom-properties-overlays)
  5216. (defun org-toggle-custom-properties-visibility ()
  5217. "Display or hide properties in `org-custom-properties'."
  5218. (interactive)
  5219. (if org-custom-properties-overlays
  5220. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5221. (setq org-custom-properties-overlays nil))
  5222. (unless (not org-custom-properties)
  5223. (save-excursion
  5224. (save-restriction
  5225. (widen)
  5226. (goto-char (point-min))
  5227. (while (re-search-forward org-property-re nil t)
  5228. (mapc (lambda(p)
  5229. (when (equal p (substring (match-string 1) 1 -1))
  5230. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5231. (overlay-put o 'invisible t)
  5232. (overlay-put o 'org-custom-property t)
  5233. (push o org-custom-properties-overlays))))
  5234. org-custom-properties)))))))
  5235. (defun org-fontify-entities (limit)
  5236. "Find an entity to fontify."
  5237. (let (ee)
  5238. (when org-pretty-entities
  5239. (catch 'match
  5240. (while (re-search-forward
  5241. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5242. limit t)
  5243. (if (and (not (org-in-indented-comment-line))
  5244. (setq ee (org-entity-get (match-string 1)))
  5245. (= (length (nth 6 ee)) 1))
  5246. (let*
  5247. ((end (if (equal (match-string 2) "{}")
  5248. (match-end 2)
  5249. (match-end 1))))
  5250. (add-text-properties
  5251. (match-beginning 0) end
  5252. (list 'font-lock-fontified t))
  5253. (compose-region (match-beginning 0) end
  5254. (nth 6 ee) nil)
  5255. (backward-char 1)
  5256. (throw 'match t))))
  5257. nil))))
  5258. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5259. "Fontify string S like in Org-mode."
  5260. (with-temp-buffer
  5261. (insert s)
  5262. (let ((org-odd-levels-only odd-levels))
  5263. (org-mode)
  5264. (font-lock-fontify-buffer)
  5265. (buffer-string))))
  5266. (defvar org-m nil)
  5267. (defvar org-l nil)
  5268. (defvar org-f nil)
  5269. (defun org-get-level-face (n)
  5270. "Get the right face for match N in font-lock matching of headlines."
  5271. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5272. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5273. (if org-cycle-level-faces
  5274. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5275. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5276. (cond
  5277. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5278. ((eq n 2) org-f)
  5279. (t (if org-level-color-stars-only nil org-f))))
  5280. (defun org-get-todo-face (kwd)
  5281. "Get the right face for a TODO keyword KWD.
  5282. If KWD is a number, get the corresponding match group."
  5283. (if (numberp kwd) (setq kwd (match-string kwd)))
  5284. (or (org-face-from-face-or-color
  5285. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5286. (and (member kwd org-done-keywords) 'org-done)
  5287. 'org-todo))
  5288. (defun org-face-from-face-or-color (context inherit face-or-color)
  5289. "Create a face list that inherits INHERIT, but sets the foreground color.
  5290. When FACE-OR-COLOR is not a string, just return it."
  5291. (if (stringp face-or-color)
  5292. (list :inherit inherit
  5293. (cdr (assoc context org-faces-easy-properties))
  5294. face-or-color)
  5295. face-or-color))
  5296. (defun org-font-lock-add-tag-faces (limit)
  5297. "Add the special tag faces."
  5298. (when (and org-tag-faces org-tags-special-faces-re)
  5299. (while (re-search-forward org-tags-special-faces-re limit t)
  5300. (add-text-properties (match-beginning 1) (match-end 1)
  5301. (list 'face (org-get-tag-face 1)
  5302. 'font-lock-fontified t))
  5303. (backward-char 1))))
  5304. (defun org-font-lock-add-priority-faces (limit)
  5305. "Add the special priority faces."
  5306. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5307. (when (save-match-data (org-at-heading-p))
  5308. (add-text-properties
  5309. (match-beginning 0) (match-end 0)
  5310. (list 'face (or (org-face-from-face-or-color
  5311. 'priority 'org-special-keyword
  5312. (cdr (assoc (char-after (match-beginning 1))
  5313. org-priority-faces)))
  5314. 'org-special-keyword)
  5315. 'font-lock-fontified t)))))
  5316. (defun org-get-tag-face (kwd)
  5317. "Get the right face for a TODO keyword KWD.
  5318. If KWD is a number, get the corresponding match group."
  5319. (if (numberp kwd) (setq kwd (match-string kwd)))
  5320. (or (org-face-from-face-or-color
  5321. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5322. 'org-tag))
  5323. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5324. "Remove fontification and activation overlays from links."
  5325. (font-lock-default-unfontify-region beg end)
  5326. (let* ((buffer-undo-list t)
  5327. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5328. (inhibit-modification-hooks t)
  5329. deactivate-mark buffer-file-name buffer-file-truename)
  5330. (org-decompose-region beg end)
  5331. (remove-text-properties beg end
  5332. '(mouse-face t keymap t org-linked-text t
  5333. invisible t intangible t
  5334. org-no-flyspell t org-emphasis t))
  5335. (org-remove-font-lock-display-properties beg end)))
  5336. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5337. ((raise 0.3) (height 0.7))
  5338. ((raise -0.5))
  5339. ((raise 0.5)))
  5340. "Display properties for showing superscripts and subscripts.")
  5341. (defun org-remove-font-lock-display-properties (beg end)
  5342. "Remove specific display properties that have been added by font lock.
  5343. The will remove the raise properties that are used to show superscripts
  5344. and subscripts."
  5345. (let (next prop)
  5346. (while (< beg end)
  5347. (setq next (next-single-property-change beg 'display nil end)
  5348. prop (get-text-property beg 'display))
  5349. (if (member prop org-script-display)
  5350. (put-text-property beg next 'display nil))
  5351. (setq beg next))))
  5352. (defun org-raise-scripts (limit)
  5353. "Add raise properties to sub/superscripts."
  5354. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5355. (if (re-search-forward
  5356. (if (eq org-use-sub-superscripts t)
  5357. org-match-substring-regexp
  5358. org-match-substring-with-braces-regexp)
  5359. limit t)
  5360. (let* ((pos (point)) table-p comment-p
  5361. (mpos (match-beginning 3))
  5362. (emph-p (get-text-property mpos 'org-emphasis))
  5363. (link-p (get-text-property mpos 'mouse-face))
  5364. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5365. (goto-char (point-at-bol))
  5366. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5367. comment-p (org-looking-at-p "[ \t]*#"))
  5368. (goto-char pos)
  5369. ;; FIXME: Should we go back one character here, for a_b^c
  5370. ;; (goto-char (1- pos)) ;????????????????????
  5371. (if (or comment-p emph-p link-p keyw-p)
  5372. t
  5373. (put-text-property (match-beginning 3) (match-end 0)
  5374. 'display
  5375. (if (equal (char-after (match-beginning 2)) ?^)
  5376. (nth (if table-p 3 1) org-script-display)
  5377. (nth (if table-p 2 0) org-script-display)))
  5378. (add-text-properties (match-beginning 2) (match-end 2)
  5379. (list 'invisible t
  5380. 'org-dwidth t 'org-dwidth-n 1))
  5381. (if (and (eq (char-after (match-beginning 3)) ?{)
  5382. (eq (char-before (match-end 3)) ?}))
  5383. (progn
  5384. (add-text-properties
  5385. (match-beginning 3) (1+ (match-beginning 3))
  5386. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5387. (add-text-properties
  5388. (1- (match-end 3)) (match-end 3)
  5389. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5390. t)))))
  5391. ;;;; Visibility cycling, including org-goto and indirect buffer
  5392. ;;; Cycling
  5393. (defvar org-cycle-global-status nil)
  5394. (make-variable-buffer-local 'org-cycle-global-status)
  5395. (defvar org-cycle-subtree-status nil)
  5396. (make-variable-buffer-local 'org-cycle-subtree-status)
  5397. (defvar org-inlinetask-min-level)
  5398. ;;;###autoload
  5399. (defun org-cycle (&optional arg)
  5400. "TAB-action and visibility cycling for Org-mode.
  5401. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5402. is outline visibility cycling, but it also invokes other actions
  5403. in special contexts.
  5404. - When this function is called with a prefix argument, rotate the entire
  5405. buffer through 3 states (global cycling)
  5406. 1. OVERVIEW: Show only top-level headlines.
  5407. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5408. 3. SHOW ALL: Show everything.
  5409. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5410. determined by the variable `org-startup-folded', and by any VISIBILITY
  5411. properties in the buffer.
  5412. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5413. including any drawers.
  5414. - When inside a table, re-align the table and move to the next field.
  5415. - When point is at the beginning of a headline, rotate the subtree started
  5416. by this line through 3 different states (local cycling)
  5417. 1. FOLDED: Only the main headline is shown.
  5418. 2. CHILDREN: The main headline and the direct children are shown.
  5419. From this state, you can move to one of the children
  5420. and zoom in further.
  5421. 3. SUBTREE: Show the entire subtree, including body text.
  5422. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5423. - When point is at the beginning of an empty headline and the variable
  5424. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5425. of the headline by demoting and promoting it to likely levels. This
  5426. speeds up creation document structure by pressing TAB once or several
  5427. times right after creating a new headline.
  5428. - When there is a numeric prefix, go up to a heading with level ARG, do
  5429. a `show-subtree' and return to the previous cursor position. If ARG
  5430. is negative, go up that many levels.
  5431. - When point is not at the beginning of a headline, execute the global
  5432. binding for TAB, which is re-indenting the line. See the option
  5433. `org-cycle-emulate-tab' for details.
  5434. - Special case: if point is at the beginning of the buffer and there is
  5435. no headline in line 1, this function will act as if called with prefix arg
  5436. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5437. But only if also the variable `org-cycle-global-at-bob' is t."
  5438. (interactive "P")
  5439. (org-load-modules-maybe)
  5440. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5441. (and org-cycle-level-after-item/entry-creation
  5442. (or (org-cycle-level)
  5443. (org-cycle-item-indentation))))
  5444. (let* ((limit-level
  5445. (or org-cycle-max-level
  5446. (and (boundp 'org-inlinetask-min-level)
  5447. org-inlinetask-min-level
  5448. (1- org-inlinetask-min-level))))
  5449. (nstars (and limit-level
  5450. (if org-odd-levels-only
  5451. (and limit-level (1- (* limit-level 2)))
  5452. limit-level)))
  5453. (org-outline-regexp
  5454. (if (not (derived-mode-p 'org-mode))
  5455. outline-regexp
  5456. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5457. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5458. (not (looking-at org-outline-regexp))))
  5459. (org-cycle-hook
  5460. (if bob-special
  5461. (delq 'org-optimize-window-after-visibility-change
  5462. (copy-sequence org-cycle-hook))
  5463. org-cycle-hook))
  5464. (pos (point)))
  5465. (if (or bob-special (equal arg '(4)))
  5466. ;; special case: use global cycling
  5467. (setq arg t))
  5468. (cond
  5469. ((equal arg '(16))
  5470. (setq last-command 'dummy)
  5471. (org-set-startup-visibility)
  5472. (message "Startup visibility, plus VISIBILITY properties"))
  5473. ((equal arg '(64))
  5474. (show-all)
  5475. (message "Entire buffer visible, including drawers"))
  5476. ;; Table: enter it or move to the next field.
  5477. ((org-at-table-p 'any)
  5478. (if (org-at-table.el-p)
  5479. (message "Use C-c ' to edit table.el tables")
  5480. (if arg (org-table-edit-field t)
  5481. (org-table-justify-field-maybe)
  5482. (call-interactively 'org-table-next-field))))
  5483. ((run-hook-with-args-until-success
  5484. 'org-tab-after-check-for-table-hook))
  5485. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5486. ((eq arg t) (org-cycle-internal-global))
  5487. ;; Drawers: delegate to `org-flag-drawer'.
  5488. ((and org-drawers org-drawer-regexp
  5489. (save-excursion
  5490. (beginning-of-line 1)
  5491. (looking-at org-drawer-regexp)))
  5492. (org-flag-drawer ; toggle block visibility
  5493. (not (get-char-property (match-end 0) 'invisible))))
  5494. ;; Show-subtree, ARG levels up from here.
  5495. ((integerp arg)
  5496. (save-excursion
  5497. (org-back-to-heading)
  5498. (outline-up-heading (if (< arg 0) (- arg)
  5499. (- (funcall outline-level) arg)))
  5500. (org-show-subtree)))
  5501. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5502. ((and (featurep 'org-inlinetask)
  5503. (org-inlinetask-at-task-p)
  5504. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5505. (org-inlinetask-toggle-visibility))
  5506. ((org-try-cdlatex-tab))
  5507. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5508. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5509. (save-excursion (beginning-of-line 1)
  5510. (looking-at org-outline-regexp)))
  5511. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5512. (org-cycle-internal-local))
  5513. ;; From there: TAB emulation and template completion.
  5514. (buffer-read-only (org-back-to-heading))
  5515. ((run-hook-with-args-until-success
  5516. 'org-tab-after-check-for-cycling-hook))
  5517. ((org-try-structure-completion))
  5518. ((run-hook-with-args-until-success
  5519. 'org-tab-before-tab-emulation-hook))
  5520. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5521. (or (not (bolp))
  5522. (not (looking-at org-outline-regexp))))
  5523. (call-interactively (global-key-binding "\t")))
  5524. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5525. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5526. (or (and (eq org-cycle-emulate-tab 'white)
  5527. (= (match-end 0) (point-at-eol)))
  5528. (and (eq org-cycle-emulate-tab 'whitestart)
  5529. (>= (match-end 0) pos))))
  5530. t
  5531. (eq org-cycle-emulate-tab t))
  5532. (call-interactively (global-key-binding "\t")))
  5533. (t (save-excursion
  5534. (org-back-to-heading)
  5535. (org-cycle)))))))
  5536. (defun org-cycle-internal-global ()
  5537. "Do the global cycling action."
  5538. ;; Hack to avoid display of messages for .org attachments in Gnus
  5539. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5540. (cond
  5541. ((and (eq last-command this-command)
  5542. (eq org-cycle-global-status 'overview))
  5543. ;; We just created the overview - now do table of contents
  5544. ;; This can be slow in very large buffers, so indicate action
  5545. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5546. (unless ga (message "CONTENTS..."))
  5547. (org-content)
  5548. (unless ga (message "CONTENTS...done"))
  5549. (setq org-cycle-global-status 'contents)
  5550. (run-hook-with-args 'org-cycle-hook 'contents))
  5551. ((and (eq last-command this-command)
  5552. (eq org-cycle-global-status 'contents))
  5553. ;; We just showed the table of contents - now show everything
  5554. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5555. (show-all)
  5556. (unless ga (message "SHOW ALL"))
  5557. (setq org-cycle-global-status 'all)
  5558. (run-hook-with-args 'org-cycle-hook 'all))
  5559. (t
  5560. ;; Default action: go to overview
  5561. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5562. (org-overview)
  5563. (unless ga (message "OVERVIEW"))
  5564. (setq org-cycle-global-status 'overview)
  5565. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5566. (defun org-cycle-internal-local ()
  5567. "Do the local cycling action."
  5568. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5569. ;; First, determine end of headline (EOH), end of subtree or item
  5570. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5571. (save-excursion
  5572. (if (org-at-item-p)
  5573. (progn
  5574. (beginning-of-line)
  5575. (setq struct (org-list-struct))
  5576. (setq eoh (point-at-eol))
  5577. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5578. (setq has-children (org-list-has-child-p (point) struct)))
  5579. (org-back-to-heading)
  5580. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5581. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5582. (setq has-children
  5583. (or (save-excursion
  5584. (let ((level (funcall outline-level)))
  5585. (outline-next-heading)
  5586. (and (org-at-heading-p t)
  5587. (> (funcall outline-level) level))))
  5588. (save-excursion
  5589. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5590. ;; Determine end invisible part of buffer (EOL)
  5591. (beginning-of-line 2)
  5592. ;; XEmacs doesn't have `next-single-char-property-change'
  5593. (if (featurep 'xemacs)
  5594. (while (and (not (eobp)) ;; this is like `next-line'
  5595. (get-char-property (1- (point)) 'invisible))
  5596. (beginning-of-line 2))
  5597. (while (and (not (eobp)) ;; this is like `next-line'
  5598. (get-char-property (1- (point)) 'invisible))
  5599. (goto-char (next-single-char-property-change (point) 'invisible))
  5600. (and (eolp) (beginning-of-line 2))))
  5601. (setq eol (point)))
  5602. ;; Find out what to do next and set `this-command'
  5603. (cond
  5604. ((= eos eoh)
  5605. ;; Nothing is hidden behind this heading
  5606. (unless (org-before-first-heading-p)
  5607. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5608. (message "EMPTY ENTRY")
  5609. (setq org-cycle-subtree-status nil)
  5610. (save-excursion
  5611. (goto-char eos)
  5612. (outline-next-heading)
  5613. (if (outline-invisible-p) (org-flag-heading nil))))
  5614. ((and (or (>= eol eos)
  5615. (not (string-match "\\S-" (buffer-substring eol eos))))
  5616. (or has-children
  5617. (not (setq children-skipped
  5618. org-cycle-skip-children-state-if-no-children))))
  5619. ;; Entire subtree is hidden in one line: children view
  5620. (unless (org-before-first-heading-p)
  5621. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5622. (if (org-at-item-p)
  5623. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5624. (org-show-entry)
  5625. (org-with-limited-levels (show-children))
  5626. ;; FIXME: This slows down the func way too much.
  5627. ;; How keep drawers hidden in subtree anyway?
  5628. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5629. ;; (org-cycle-hide-drawers 'subtree))
  5630. ;; Fold every list in subtree to top-level items.
  5631. (when (eq org-cycle-include-plain-lists 'integrate)
  5632. (save-excursion
  5633. (org-back-to-heading)
  5634. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5635. (beginning-of-line 1)
  5636. (let* ((struct (org-list-struct))
  5637. (prevs (org-list-prevs-alist struct))
  5638. (end (org-list-get-bottom-point struct)))
  5639. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5640. (org-list-get-all-items (point) struct prevs))
  5641. (goto-char end))))))
  5642. (message "CHILDREN")
  5643. (save-excursion
  5644. (goto-char eos)
  5645. (outline-next-heading)
  5646. (if (outline-invisible-p) (org-flag-heading nil)))
  5647. (setq org-cycle-subtree-status 'children)
  5648. (unless (org-before-first-heading-p)
  5649. (run-hook-with-args 'org-cycle-hook 'children)))
  5650. ((or children-skipped
  5651. (and (eq last-command this-command)
  5652. (eq org-cycle-subtree-status 'children)))
  5653. ;; We just showed the children, or no children are there,
  5654. ;; now show everything.
  5655. (unless (org-before-first-heading-p)
  5656. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5657. (outline-flag-region eoh eos nil)
  5658. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5659. (setq org-cycle-subtree-status 'subtree)
  5660. (unless (org-before-first-heading-p)
  5661. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5662. (t
  5663. ;; Default action: hide the subtree.
  5664. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5665. (outline-flag-region eoh eos t)
  5666. (message "FOLDED")
  5667. (setq org-cycle-subtree-status 'folded)
  5668. (unless (org-before-first-heading-p)
  5669. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5670. ;;;###autoload
  5671. (defun org-global-cycle (&optional arg)
  5672. "Cycle the global visibility. For details see `org-cycle'.
  5673. With \\[universal-argument] prefix arg, switch to startup visibility.
  5674. With a numeric prefix, show all headlines up to that level."
  5675. (interactive "P")
  5676. (let ((org-cycle-include-plain-lists
  5677. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5678. (cond
  5679. ((integerp arg)
  5680. (show-all)
  5681. (hide-sublevels arg)
  5682. (setq org-cycle-global-status 'contents))
  5683. ((equal arg '(4))
  5684. (org-set-startup-visibility)
  5685. (message "Startup visibility, plus VISIBILITY properties."))
  5686. (t
  5687. (org-cycle '(4))))))
  5688. (defun org-set-startup-visibility ()
  5689. "Set the visibility required by startup options and properties."
  5690. (cond
  5691. ((eq org-startup-folded t)
  5692. (org-cycle '(4)))
  5693. ((eq org-startup-folded 'content)
  5694. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5695. (org-cycle '(4)) (org-cycle '(4)))))
  5696. (unless (eq org-startup-folded 'showeverything)
  5697. (if org-hide-block-startup (org-hide-block-all))
  5698. (org-set-visibility-according-to-property 'no-cleanup)
  5699. (org-cycle-hide-archived-subtrees 'all)
  5700. (org-cycle-hide-drawers 'all)
  5701. (org-cycle-show-empty-lines t)))
  5702. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5703. "Switch subtree visibilities according to :VISIBILITY: property."
  5704. (interactive)
  5705. (let (org-show-entry-below state)
  5706. (save-excursion
  5707. (goto-char (point-min))
  5708. (while (re-search-forward
  5709. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5710. nil t)
  5711. (setq state (match-string 1))
  5712. (save-excursion
  5713. (org-back-to-heading t)
  5714. (hide-subtree)
  5715. (org-reveal)
  5716. (cond
  5717. ((equal state '("fold" "folded"))
  5718. (hide-subtree))
  5719. ((equal state "children")
  5720. (org-show-hidden-entry)
  5721. (show-children))
  5722. ((equal state "content")
  5723. (save-excursion
  5724. (save-restriction
  5725. (org-narrow-to-subtree)
  5726. (org-content))))
  5727. ((member state '("all" "showall"))
  5728. (show-subtree)))))
  5729. (unless no-cleanup
  5730. (org-cycle-hide-archived-subtrees 'all)
  5731. (org-cycle-hide-drawers 'all)
  5732. (org-cycle-show-empty-lines 'all)))))
  5733. ;; This function uses outline-regexp instead of the more fundamental
  5734. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5735. ;; buffers, where outline-regexp is needed.
  5736. (defun org-overview ()
  5737. "Switch to overview mode, showing only top-level headlines.
  5738. Really, this shows all headlines with level equal or greater than the level
  5739. of the first headline in the buffer. This is important, because if the
  5740. first headline is not level one, then (hide-sublevels 1) gives confusing
  5741. results."
  5742. (interactive)
  5743. (let ((level (save-excursion
  5744. (goto-char (point-min))
  5745. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5746. (progn
  5747. (goto-char (match-beginning 0))
  5748. (funcall outline-level))))))
  5749. (and level (hide-sublevels level))))
  5750. (defun org-content (&optional arg)
  5751. "Show all headlines in the buffer, like a table of contents.
  5752. With numerical argument N, show content up to level N."
  5753. (interactive "P")
  5754. (save-excursion
  5755. ;; Visit all headings and show their offspring
  5756. (and (integerp arg) (org-overview))
  5757. (goto-char (point-max))
  5758. (catch 'exit
  5759. (while (and (progn (condition-case nil
  5760. (outline-previous-visible-heading 1)
  5761. (error (goto-char (point-min))))
  5762. t)
  5763. (looking-at org-outline-regexp))
  5764. (if (integerp arg)
  5765. (show-children (1- arg))
  5766. (show-branches))
  5767. (if (bobp) (throw 'exit nil))))))
  5768. (defun org-optimize-window-after-visibility-change (state)
  5769. "Adjust the window after a change in outline visibility.
  5770. This function is the default value of the hook `org-cycle-hook'."
  5771. (when (get-buffer-window (current-buffer))
  5772. (cond
  5773. ((eq state 'content) nil)
  5774. ((eq state 'all) nil)
  5775. ((eq state 'folded) nil)
  5776. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5777. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5778. (defun org-remove-empty-overlays-at (pos)
  5779. "Remove outline overlays that do not contain non-white stuff."
  5780. (mapc
  5781. (lambda (o)
  5782. (and (eq 'outline (overlay-get o 'invisible))
  5783. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5784. (overlay-end o))))
  5785. (delete-overlay o)))
  5786. (overlays-at pos)))
  5787. (defun org-clean-visibility-after-subtree-move ()
  5788. "Fix visibility issues after moving a subtree."
  5789. ;; First, find a reasonable region to look at:
  5790. ;; Start two siblings above, end three below
  5791. (let* ((beg (save-excursion
  5792. (and (org-get-last-sibling)
  5793. (org-get-last-sibling))
  5794. (point)))
  5795. (end (save-excursion
  5796. (and (org-get-next-sibling)
  5797. (org-get-next-sibling)
  5798. (org-get-next-sibling))
  5799. (if (org-at-heading-p)
  5800. (point-at-eol)
  5801. (point))))
  5802. (level (looking-at "\\*+"))
  5803. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5804. (save-excursion
  5805. (save-restriction
  5806. (narrow-to-region beg end)
  5807. (when re
  5808. ;; Properly fold already folded siblings
  5809. (goto-char (point-min))
  5810. (while (re-search-forward re nil t)
  5811. (if (and (not (outline-invisible-p))
  5812. (save-excursion
  5813. (goto-char (point-at-eol)) (outline-invisible-p)))
  5814. (hide-entry))))
  5815. (org-cycle-show-empty-lines 'overview)
  5816. (org-cycle-hide-drawers 'overview)))))
  5817. (defun org-cycle-show-empty-lines (state)
  5818. "Show empty lines above all visible headlines.
  5819. The region to be covered depends on STATE when called through
  5820. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5821. entire buffer covered. Note that an empty line is only shown if there
  5822. are at least `org-cycle-separator-lines' empty lines before the headline."
  5823. (when (not (= org-cycle-separator-lines 0))
  5824. (save-excursion
  5825. (let* ((n (abs org-cycle-separator-lines))
  5826. (re (cond
  5827. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5828. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5829. (t (let ((ns (number-to-string (- n 2))))
  5830. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5831. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5832. beg end b e)
  5833. (cond
  5834. ((memq state '(overview contents t))
  5835. (setq beg (point-min) end (point-max)))
  5836. ((memq state '(children folded))
  5837. (setq beg (point) end (progn (org-end-of-subtree t t)
  5838. (beginning-of-line 2)
  5839. (point)))))
  5840. (when beg
  5841. (goto-char beg)
  5842. (while (re-search-forward re end t)
  5843. (unless (get-char-property (match-end 1) 'invisible)
  5844. (setq e (match-end 1))
  5845. (if (< org-cycle-separator-lines 0)
  5846. (setq b (save-excursion
  5847. (goto-char (match-beginning 0))
  5848. (org-back-over-empty-lines)
  5849. (if (save-excursion
  5850. (goto-char (max (point-min) (1- (point))))
  5851. (org-at-heading-p))
  5852. (1- (point))
  5853. (point))))
  5854. (setq b (match-beginning 1)))
  5855. (outline-flag-region b e nil)))))))
  5856. ;; Never hide empty lines at the end of the file.
  5857. (save-excursion
  5858. (goto-char (point-max))
  5859. (outline-previous-heading)
  5860. (outline-end-of-heading)
  5861. (if (and (looking-at "[ \t\n]+")
  5862. (= (match-end 0) (point-max)))
  5863. (outline-flag-region (point) (match-end 0) nil))))
  5864. (defun org-show-empty-lines-in-parent ()
  5865. "Move to the parent and re-show empty lines before visible headlines."
  5866. (save-excursion
  5867. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5868. (org-cycle-show-empty-lines context))))
  5869. (defun org-files-list ()
  5870. "Return `org-agenda-files' list, plus all open org-mode files.
  5871. This is useful for operations that need to scan all of a user's
  5872. open and agenda-wise Org files."
  5873. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5874. (dolist (buf (buffer-list))
  5875. (with-current-buffer buf
  5876. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  5877. (let ((file (expand-file-name (buffer-file-name))))
  5878. (unless (member file files)
  5879. (push file files))))))
  5880. files))
  5881. (defsubst org-entry-beginning-position ()
  5882. "Return the beginning position of the current entry."
  5883. (save-excursion (outline-back-to-heading t) (point)))
  5884. (defsubst org-entry-end-position ()
  5885. "Return the end position of the current entry."
  5886. (save-excursion (outline-next-heading) (point)))
  5887. (defun org-cycle-hide-drawers (state)
  5888. "Re-hide all drawers after a visibility state change."
  5889. (when (and (derived-mode-p 'org-mode)
  5890. (not (memq state '(overview folded contents))))
  5891. (save-excursion
  5892. (let* ((globalp (memq state '(contents all)))
  5893. (beg (if globalp (point-min) (point)))
  5894. (end (if globalp (point-max)
  5895. (if (eq state 'children)
  5896. (save-excursion (outline-next-heading) (point))
  5897. (org-end-of-subtree t)))))
  5898. (goto-char beg)
  5899. (while (re-search-forward org-drawer-regexp end t)
  5900. (org-flag-drawer t))))))
  5901. (defun org-flag-drawer (flag)
  5902. "When FLAG is non-nil, hide the drawer we are within.
  5903. Otherwise make it visible."
  5904. (save-excursion
  5905. (beginning-of-line 1)
  5906. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5907. (let ((b (match-end 0)))
  5908. (if (re-search-forward
  5909. "^[ \t]*:END:"
  5910. (save-excursion (outline-next-heading) (point)) t)
  5911. (outline-flag-region b (point-at-eol) flag)
  5912. (error ":END: line missing at position %s" b))))))
  5913. (defun org-subtree-end-visible-p ()
  5914. "Is the end of the current subtree visible?"
  5915. (pos-visible-in-window-p
  5916. (save-excursion (org-end-of-subtree t) (point))))
  5917. (defun org-first-headline-recenter (&optional N)
  5918. "Move cursor to the first headline and recenter the headline.
  5919. Optional argument N means put the headline into the Nth line of the window."
  5920. (goto-char (point-min))
  5921. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5922. (beginning-of-line)
  5923. (recenter (prefix-numeric-value N))))
  5924. ;;; Saving and restoring visibility
  5925. (defun org-outline-overlay-data (&optional use-markers)
  5926. "Return a list of the locations of all outline overlays.
  5927. These are overlays with the `invisible' property value `outline'.
  5928. The return value is a list of cons cells, with start and stop
  5929. positions for each overlay.
  5930. If USE-MARKERS is set, return the positions as markers."
  5931. (let (beg end)
  5932. (save-excursion
  5933. (save-restriction
  5934. (widen)
  5935. (delq nil
  5936. (mapcar (lambda (o)
  5937. (when (eq (overlay-get o 'invisible) 'outline)
  5938. (setq beg (overlay-start o)
  5939. end (overlay-end o))
  5940. (and beg end (> end beg)
  5941. (if use-markers
  5942. (cons (move-marker (make-marker) beg)
  5943. (move-marker (make-marker) end))
  5944. (cons beg end)))))
  5945. (overlays-in (point-min) (point-max))))))))
  5946. (defun org-set-outline-overlay-data (data)
  5947. "Create visibility overlays for all positions in DATA.
  5948. DATA should have been made by `org-outline-overlay-data'."
  5949. (let (o)
  5950. (save-excursion
  5951. (save-restriction
  5952. (widen)
  5953. (show-all)
  5954. (mapc (lambda (c)
  5955. (outline-flag-region (car c) (cdr c) t))
  5956. data)))))
  5957. ;;; Folding of blocks
  5958. (defvar org-hide-block-overlays nil
  5959. "Overlays hiding blocks.")
  5960. (make-variable-buffer-local 'org-hide-block-overlays)
  5961. (defun org-block-map (function &optional start end)
  5962. "Call FUNCTION at the head of all source blocks in the current buffer.
  5963. Optional arguments START and END can be used to limit the range."
  5964. (let ((start (or start (point-min)))
  5965. (end (or end (point-max))))
  5966. (save-excursion
  5967. (goto-char start)
  5968. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5969. (save-excursion
  5970. (save-match-data
  5971. (goto-char (match-beginning 0))
  5972. (funcall function)))))))
  5973. (defun org-hide-block-toggle-all ()
  5974. "Toggle the visibility of all blocks in the current buffer."
  5975. (org-block-map #'org-hide-block-toggle))
  5976. (defun org-hide-block-all ()
  5977. "Fold all blocks in the current buffer."
  5978. (interactive)
  5979. (org-show-block-all)
  5980. (org-block-map #'org-hide-block-toggle-maybe))
  5981. (defun org-show-block-all ()
  5982. "Unfold all blocks in the current buffer."
  5983. (interactive)
  5984. (mapc 'delete-overlay org-hide-block-overlays)
  5985. (setq org-hide-block-overlays nil))
  5986. (defun org-hide-block-toggle-maybe ()
  5987. "Toggle visibility of block at point."
  5988. (interactive)
  5989. (let ((case-fold-search t))
  5990. (if (save-excursion
  5991. (beginning-of-line 1)
  5992. (looking-at org-block-regexp))
  5993. (progn (org-hide-block-toggle)
  5994. t) ;; to signal that we took action
  5995. nil))) ;; to signal that we did not
  5996. (defun org-hide-block-toggle (&optional force)
  5997. "Toggle the visibility of the current block."
  5998. (interactive)
  5999. (save-excursion
  6000. (beginning-of-line)
  6001. (if (re-search-forward org-block-regexp nil t)
  6002. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6003. (end (match-end 0)) ;; end of entire body
  6004. ov)
  6005. (if (memq t (mapcar (lambda (overlay)
  6006. (eq (overlay-get overlay 'invisible)
  6007. 'org-hide-block))
  6008. (overlays-at start)))
  6009. (if (or (not force) (eq force 'off))
  6010. (mapc (lambda (ov)
  6011. (when (member ov org-hide-block-overlays)
  6012. (setq org-hide-block-overlays
  6013. (delq ov org-hide-block-overlays)))
  6014. (when (eq (overlay-get ov 'invisible)
  6015. 'org-hide-block)
  6016. (delete-overlay ov)))
  6017. (overlays-at start)))
  6018. (setq ov (make-overlay start end))
  6019. (overlay-put ov 'invisible 'org-hide-block)
  6020. ;; make the block accessible to isearch
  6021. (overlay-put
  6022. ov 'isearch-open-invisible
  6023. (lambda (ov)
  6024. (when (member ov org-hide-block-overlays)
  6025. (setq org-hide-block-overlays
  6026. (delq ov org-hide-block-overlays)))
  6027. (when (eq (overlay-get ov 'invisible)
  6028. 'org-hide-block)
  6029. (delete-overlay ov))))
  6030. (push ov org-hide-block-overlays)))
  6031. (error "Not looking at a source block"))))
  6032. ;; org-tab-after-check-for-cycling-hook
  6033. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6034. ;; Remove overlays when changing major mode
  6035. (add-hook 'org-mode-hook
  6036. (lambda () (org-add-hook 'change-major-mode-hook
  6037. 'org-show-block-all 'append 'local)))
  6038. ;;; Org-goto
  6039. (defvar org-goto-window-configuration nil)
  6040. (defvar org-goto-marker nil)
  6041. (defvar org-goto-map)
  6042. (defun org-goto-map ()
  6043. "Set the keymap `org-goto'."
  6044. (setq org-goto-map
  6045. (let ((map (make-sparse-keymap)))
  6046. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6047. mouse-drag-region universal-argument org-occur))
  6048. cmd)
  6049. (while (setq cmd (pop cmds))
  6050. (substitute-key-definition cmd cmd map global-map)))
  6051. (suppress-keymap map)
  6052. (org-defkey map "\C-m" 'org-goto-ret)
  6053. (org-defkey map [(return)] 'org-goto-ret)
  6054. (org-defkey map [(left)] 'org-goto-left)
  6055. (org-defkey map [(right)] 'org-goto-right)
  6056. (org-defkey map [(control ?g)] 'org-goto-quit)
  6057. (org-defkey map "\C-i" 'org-cycle)
  6058. (org-defkey map [(tab)] 'org-cycle)
  6059. (org-defkey map [(down)] 'outline-next-visible-heading)
  6060. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6061. (if org-goto-auto-isearch
  6062. (if (fboundp 'define-key-after)
  6063. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6064. nil)
  6065. (org-defkey map "q" 'org-goto-quit)
  6066. (org-defkey map "n" 'outline-next-visible-heading)
  6067. (org-defkey map "p" 'outline-previous-visible-heading)
  6068. (org-defkey map "f" 'outline-forward-same-level)
  6069. (org-defkey map "b" 'outline-backward-same-level)
  6070. (org-defkey map "u" 'outline-up-heading))
  6071. (org-defkey map "/" 'org-occur)
  6072. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6073. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6074. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6075. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6076. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6077. map)))
  6078. (defconst org-goto-help
  6079. "Browse buffer copy, to find location or copy text.%s
  6080. RET=jump to location C-g=quit and return to previous location
  6081. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6082. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6083. ;; FIXME: Docstring does not mention both interfaces
  6084. (defun org-goto (&optional alternative-interface)
  6085. "Look up a different location in the current file, keeping current visibility.
  6086. When you want look-up or go to a different location in a
  6087. document, the fastest way is often to fold the entire buffer and
  6088. then dive into the tree. This method has the disadvantage, that
  6089. the previous location will be folded, which may not be what you
  6090. want.
  6091. This command works around this by showing a copy of the current
  6092. buffer in an indirect buffer, in overview mode. You can dive
  6093. into the tree in that copy, use org-occur and incremental search
  6094. to find a location. When pressing RET or `Q', the command
  6095. returns to the original buffer in which the visibility is still
  6096. unchanged. After RET it will also jump to the location selected
  6097. in the indirect buffer and expose the headline hierarchy above.
  6098. With a prefix argument, use the alternative interface: e.g. if
  6099. `org-goto-interface' is 'outline use 'outline-path-completion."
  6100. (interactive "P")
  6101. (org-goto-map)
  6102. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6103. (org-refile-use-outline-path t)
  6104. (org-refile-target-verify-function nil)
  6105. (interface
  6106. (if (not alternative-interface)
  6107. org-goto-interface
  6108. (if (eq org-goto-interface 'outline)
  6109. 'outline-path-completion
  6110. 'outline)))
  6111. (org-goto-start-pos (point))
  6112. (selected-point
  6113. (if (eq interface 'outline)
  6114. (car (org-get-location (current-buffer) org-goto-help))
  6115. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6116. (org-refile-check-position pa)
  6117. (nth 3 pa)))))
  6118. (if selected-point
  6119. (progn
  6120. (org-mark-ring-push org-goto-start-pos)
  6121. (goto-char selected-point)
  6122. (if (or (outline-invisible-p) (org-invisible-p2))
  6123. (org-show-context 'org-goto)))
  6124. (message "Quit"))))
  6125. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6126. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6127. (defvar org-goto-local-auto-isearch-map) ; defined below
  6128. (defun org-get-location (buf help)
  6129. "Let the user select a location in the Org-mode buffer BUF.
  6130. This function uses a recursive edit. It returns the selected position
  6131. or nil."
  6132. (org-no-popups
  6133. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6134. (isearch-hide-immediately nil)
  6135. (isearch-search-fun-function
  6136. (lambda () 'org-goto-local-search-headings))
  6137. (org-goto-selected-point org-goto-exit-command))
  6138. (save-excursion
  6139. (save-window-excursion
  6140. (delete-other-windows)
  6141. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6142. (org-pop-to-buffer-same-window
  6143. (condition-case nil
  6144. (make-indirect-buffer (current-buffer) "*org-goto*")
  6145. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6146. (with-output-to-temp-buffer "*Org Help*"
  6147. (princ (format help (if org-goto-auto-isearch
  6148. " Just type for auto-isearch."
  6149. " n/p/f/b/u to navigate, q to quit."))))
  6150. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6151. (setq buffer-read-only nil)
  6152. (let ((org-startup-truncated t)
  6153. (org-startup-folded nil)
  6154. (org-startup-align-all-tables nil))
  6155. (org-mode)
  6156. (org-overview))
  6157. (setq buffer-read-only t)
  6158. (if (and (boundp 'org-goto-start-pos)
  6159. (integer-or-marker-p org-goto-start-pos))
  6160. (let ((org-show-hierarchy-above t)
  6161. (org-show-siblings t)
  6162. (org-show-following-heading t))
  6163. (goto-char org-goto-start-pos)
  6164. (and (outline-invisible-p) (org-show-context)))
  6165. (goto-char (point-min)))
  6166. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6167. (message "Select location and press RET")
  6168. (use-local-map org-goto-map)
  6169. (recursive-edit)))
  6170. (kill-buffer "*org-goto*")
  6171. (cons org-goto-selected-point org-goto-exit-command))))
  6172. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6173. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6174. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6175. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6176. (defun org-goto-local-search-headings (string bound noerror)
  6177. "Search and make sure that any matches are in headlines."
  6178. (catch 'return
  6179. (while (if isearch-forward
  6180. (search-forward string bound noerror)
  6181. (search-backward string bound noerror))
  6182. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6183. (and (member :headline context)
  6184. (not (member :tags context))))
  6185. (throw 'return (point))))))
  6186. (defun org-goto-local-auto-isearch ()
  6187. "Start isearch."
  6188. (interactive)
  6189. (goto-char (point-min))
  6190. (let ((keys (this-command-keys)))
  6191. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6192. (isearch-mode t)
  6193. (isearch-process-search-char (string-to-char keys)))))
  6194. (defun org-goto-ret (&optional arg)
  6195. "Finish `org-goto' by going to the new location."
  6196. (interactive "P")
  6197. (setq org-goto-selected-point (point)
  6198. org-goto-exit-command 'return)
  6199. (throw 'exit nil))
  6200. (defun org-goto-left ()
  6201. "Finish `org-goto' by going to the new location."
  6202. (interactive)
  6203. (if (org-at-heading-p)
  6204. (progn
  6205. (beginning-of-line 1)
  6206. (setq org-goto-selected-point (point)
  6207. org-goto-exit-command 'left)
  6208. (throw 'exit nil))
  6209. (error "Not on a heading")))
  6210. (defun org-goto-right ()
  6211. "Finish `org-goto' by going to the new location."
  6212. (interactive)
  6213. (if (org-at-heading-p)
  6214. (progn
  6215. (setq org-goto-selected-point (point)
  6216. org-goto-exit-command 'right)
  6217. (throw 'exit nil))
  6218. (error "Not on a heading")))
  6219. (defun org-goto-quit ()
  6220. "Finish `org-goto' without cursor motion."
  6221. (interactive)
  6222. (setq org-goto-selected-point nil)
  6223. (setq org-goto-exit-command 'quit)
  6224. (throw 'exit nil))
  6225. ;;; Indirect buffer display of subtrees
  6226. (defvar org-indirect-dedicated-frame nil
  6227. "This is the frame being used for indirect tree display.")
  6228. (defvar org-last-indirect-buffer nil)
  6229. (defun org-tree-to-indirect-buffer (&optional arg)
  6230. "Create indirect buffer and narrow it to current subtree.
  6231. With a numerical prefix ARG, go up to this level and then take that tree.
  6232. If ARG is negative, go up that many levels.
  6233. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6234. indirect buffer previously made with this command, to avoid proliferation of
  6235. indirect buffers. However, when you call the command with a \
  6236. \\[universal-argument] prefix, or
  6237. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6238. is kept so that you can work with several indirect buffers at the same time.
  6239. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6240. \\[universal-argument] prefix also
  6241. requests that a new frame be made for the new buffer, so that the dedicated
  6242. frame is not changed."
  6243. (interactive "P")
  6244. (let ((cbuf (current-buffer))
  6245. (cwin (selected-window))
  6246. (pos (point))
  6247. beg end level heading ibuf)
  6248. (save-excursion
  6249. (org-back-to-heading t)
  6250. (when (numberp arg)
  6251. (setq level (org-outline-level))
  6252. (if (< arg 0) (setq arg (+ level arg)))
  6253. (while (> (setq level (org-outline-level)) arg)
  6254. (org-up-heading-safe)))
  6255. (setq beg (point)
  6256. heading (org-get-heading))
  6257. (org-end-of-subtree t t)
  6258. (if (org-at-heading-p) (backward-char 1))
  6259. (setq end (point)))
  6260. (if (and (buffer-live-p org-last-indirect-buffer)
  6261. (not (eq org-indirect-buffer-display 'new-frame))
  6262. (not arg))
  6263. (kill-buffer org-last-indirect-buffer))
  6264. (setq ibuf (org-get-indirect-buffer cbuf)
  6265. org-last-indirect-buffer ibuf)
  6266. (cond
  6267. ((or (eq org-indirect-buffer-display 'new-frame)
  6268. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6269. (select-frame (make-frame))
  6270. (delete-other-windows)
  6271. (org-pop-to-buffer-same-window ibuf)
  6272. (org-set-frame-title heading))
  6273. ((eq org-indirect-buffer-display 'dedicated-frame)
  6274. (raise-frame
  6275. (select-frame (or (and org-indirect-dedicated-frame
  6276. (frame-live-p org-indirect-dedicated-frame)
  6277. org-indirect-dedicated-frame)
  6278. (setq org-indirect-dedicated-frame (make-frame)))))
  6279. (delete-other-windows)
  6280. (org-pop-to-buffer-same-window ibuf)
  6281. (org-set-frame-title (concat "Indirect: " heading)))
  6282. ((eq org-indirect-buffer-display 'current-window)
  6283. (org-pop-to-buffer-same-window ibuf))
  6284. ((eq org-indirect-buffer-display 'other-window)
  6285. (pop-to-buffer ibuf))
  6286. (t (error "Invalid value")))
  6287. (if (featurep 'xemacs)
  6288. (save-excursion (org-mode) (turn-on-font-lock)))
  6289. (narrow-to-region beg end)
  6290. (show-all)
  6291. (goto-char pos)
  6292. (run-hook-with-args 'org-cycle-hook 'all)
  6293. (and (window-live-p cwin) (select-window cwin))))
  6294. (defun org-get-indirect-buffer (&optional buffer)
  6295. (setq buffer (or buffer (current-buffer)))
  6296. (let ((n 1) (base (buffer-name buffer)) bname)
  6297. (while (buffer-live-p
  6298. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6299. (setq n (1+ n)))
  6300. (condition-case nil
  6301. (make-indirect-buffer buffer bname 'clone)
  6302. (error (make-indirect-buffer buffer bname)))))
  6303. (defun org-set-frame-title (title)
  6304. "Set the title of the current frame to the string TITLE."
  6305. ;; FIXME: how to name a single frame in XEmacs???
  6306. (unless (featurep 'xemacs)
  6307. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6308. ;;;; Structure editing
  6309. ;;; Inserting headlines
  6310. (defun org-previous-line-empty-p ()
  6311. (save-excursion
  6312. (and (not (bobp))
  6313. (or (beginning-of-line 0) t)
  6314. (save-match-data
  6315. (looking-at "[ \t]*$")))))
  6316. (defun org-insert-heading (&optional force-heading invisible-ok)
  6317. "Insert a new heading or item with same depth at point.
  6318. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6319. If point is at the beginning of a headline, insert a sibling before the
  6320. current headline. If point is not at the beginning, split the line,
  6321. create the new headline with the text in the current line after point
  6322. \(but see also the variable `org-M-RET-may-split-line').
  6323. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6324. This is important for non-interactive uses of the command."
  6325. (interactive "P")
  6326. (if (or (= (buffer-size) 0)
  6327. (and (not (save-excursion
  6328. (and (ignore-errors (org-back-to-heading invisible-ok))
  6329. (org-at-heading-p))))
  6330. (or force-heading (not (org-in-item-p)))))
  6331. (progn
  6332. (insert "\n* ")
  6333. (run-hooks 'org-insert-heading-hook))
  6334. (when (or force-heading (not (org-insert-item)))
  6335. (let* ((empty-line-p nil)
  6336. (level nil)
  6337. (on-heading (org-at-heading-p))
  6338. (head (save-excursion
  6339. (condition-case nil
  6340. (progn
  6341. (org-back-to-heading invisible-ok)
  6342. (when (and (not on-heading)
  6343. (featurep 'org-inlinetask)
  6344. (integerp org-inlinetask-min-level)
  6345. (>= (length (match-string 0))
  6346. org-inlinetask-min-level))
  6347. ;; Find a heading level before the inline task
  6348. (while (and (setq level (org-up-heading-safe))
  6349. (>= level org-inlinetask-min-level)))
  6350. (if (org-at-heading-p)
  6351. (org-back-to-heading invisible-ok)
  6352. (error "This should not happen")))
  6353. (setq empty-line-p (org-previous-line-empty-p))
  6354. (match-string 0))
  6355. (error "*"))))
  6356. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6357. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6358. pos hide-previous previous-pos)
  6359. (cond
  6360. ((and (org-at-heading-p) (bolp)
  6361. (or (bobp)
  6362. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6363. ;; insert before the current line
  6364. (open-line (if blank 2 1)))
  6365. ((and (bolp)
  6366. (not org-insert-heading-respect-content)
  6367. (or (bobp)
  6368. (save-excursion
  6369. (backward-char 1) (not (outline-invisible-p)))))
  6370. ;; insert right here
  6371. nil)
  6372. (t
  6373. ;; somewhere in the line
  6374. (save-excursion
  6375. (setq previous-pos (point-at-bol))
  6376. (end-of-line)
  6377. (setq hide-previous (outline-invisible-p)))
  6378. (and org-insert-heading-respect-content (org-show-subtree))
  6379. (let ((split
  6380. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6381. (save-excursion
  6382. (let ((p (point)))
  6383. (goto-char (point-at-bol))
  6384. (and (looking-at org-complex-heading-regexp)
  6385. (match-beginning 4)
  6386. (> p (match-beginning 4)))))))
  6387. tags pos)
  6388. (cond
  6389. (org-insert-heading-respect-content
  6390. (org-end-of-subtree nil t)
  6391. (when (featurep 'org-inlinetask)
  6392. (while (and (not (eobp))
  6393. (looking-at "\\(\\*+\\)[ \t]+")
  6394. (>= (length (match-string 1))
  6395. org-inlinetask-min-level))
  6396. (org-end-of-subtree nil t)))
  6397. (or (bolp) (newline))
  6398. (or (org-previous-line-empty-p)
  6399. (and blank (newline)))
  6400. (open-line 1))
  6401. ((org-at-heading-p)
  6402. (when hide-previous
  6403. (show-children)
  6404. (org-show-entry))
  6405. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6406. (setq tags (and (match-end 2) (match-string 2)))
  6407. (and (match-end 1)
  6408. (delete-region (match-beginning 1) (match-end 1)))
  6409. (setq pos (point-at-bol))
  6410. (or split (end-of-line 1))
  6411. (delete-horizontal-space)
  6412. (if (string-match "\\`\\*+\\'"
  6413. (buffer-substring (point-at-bol) (point)))
  6414. (insert " "))
  6415. (newline (if blank 2 1))
  6416. (when tags
  6417. (save-excursion
  6418. (goto-char pos)
  6419. (end-of-line 1)
  6420. (insert " " tags)
  6421. (org-set-tags nil 'align))))
  6422. (t
  6423. (or split (end-of-line 1))
  6424. (newline (if blank 2 1)))))))
  6425. (insert head) (just-one-space)
  6426. (setq pos (point))
  6427. (end-of-line 1)
  6428. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6429. (when (and org-insert-heading-respect-content hide-previous)
  6430. (save-excursion
  6431. (goto-char previous-pos)
  6432. (hide-subtree)))
  6433. (run-hooks 'org-insert-heading-hook)))))
  6434. (defun org-get-heading (&optional no-tags no-todo)
  6435. "Return the heading of the current entry, without the stars.
  6436. When NO-TAGS is non-nil, don't include tags.
  6437. When NO-TODO is non-nil, don't include TODO keywords."
  6438. (save-excursion
  6439. (org-back-to-heading t)
  6440. (cond
  6441. ((and no-tags no-todo)
  6442. (looking-at org-complex-heading-regexp)
  6443. (match-string 4))
  6444. (no-tags
  6445. (looking-at (concat org-outline-regexp
  6446. "\\(.*?\\)"
  6447. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6448. (match-string 1))
  6449. (no-todo
  6450. (looking-at org-todo-line-regexp)
  6451. (match-string 3))
  6452. (t (looking-at org-heading-regexp)
  6453. (match-string 2)))))
  6454. (defun org-heading-components ()
  6455. "Return the components of the current heading.
  6456. This is a list with the following elements:
  6457. - the level as an integer
  6458. - the reduced level, different if `org-odd-levels-only' is set.
  6459. - the TODO keyword, or nil
  6460. - the priority character, like ?A, or nil if no priority is given
  6461. - the headline text itself, or the tags string if no headline text
  6462. - the tags string, or nil."
  6463. (save-excursion
  6464. (org-back-to-heading t)
  6465. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6466. (list (length (match-string 1))
  6467. (org-reduced-level (length (match-string 1)))
  6468. (org-match-string-no-properties 2)
  6469. (and (match-end 3) (aref (match-string 3) 2))
  6470. (org-match-string-no-properties 4)
  6471. (org-match-string-no-properties 5)))))
  6472. (defun org-get-entry ()
  6473. "Get the entry text, after heading, entire subtree."
  6474. (save-excursion
  6475. (org-back-to-heading t)
  6476. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6477. (defun org-insert-heading-after-current ()
  6478. "Insert a new heading with same level as current, after current subtree."
  6479. (interactive)
  6480. (org-back-to-heading)
  6481. (org-insert-heading)
  6482. (org-move-subtree-down)
  6483. (end-of-line 1))
  6484. (defun org-insert-heading-respect-content (invisible-ok)
  6485. "Insert heading with `org-insert-heading-respect-content' set to t."
  6486. (interactive "P")
  6487. (let ((org-insert-heading-respect-content t))
  6488. (org-insert-heading t invisible-ok)))
  6489. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6490. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6491. (interactive "P")
  6492. (let ((org-insert-heading-respect-content t))
  6493. (org-insert-todo-heading force-state t)))
  6494. (defun org-insert-todo-heading (arg &optional force-heading)
  6495. "Insert a new heading with the same level and TODO state as current heading.
  6496. If the heading has no TODO state, or if the state is DONE, use the first
  6497. state (TODO by default). Also with prefix arg, force first state."
  6498. (interactive "P")
  6499. (when (or force-heading (not (org-insert-item 'checkbox)))
  6500. (org-insert-heading force-heading)
  6501. (save-excursion
  6502. (org-back-to-heading)
  6503. (outline-previous-heading)
  6504. (looking-at org-todo-line-regexp))
  6505. (let*
  6506. ((new-mark-x
  6507. (if (or arg
  6508. (not (match-beginning 2))
  6509. (member (match-string 2) org-done-keywords))
  6510. (car org-todo-keywords-1)
  6511. (match-string 2)))
  6512. (new-mark
  6513. (or
  6514. (run-hook-with-args-until-success
  6515. 'org-todo-get-default-hook new-mark-x nil)
  6516. new-mark-x)))
  6517. (beginning-of-line 1)
  6518. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6519. (if org-treat-insert-todo-heading-as-state-change
  6520. (org-todo new-mark)
  6521. (insert new-mark " "))))
  6522. (when org-provide-todo-statistics
  6523. (org-update-parent-todo-statistics))))
  6524. (defun org-insert-subheading (arg)
  6525. "Insert a new subheading and demote it.
  6526. Works for outline headings and for plain lists alike."
  6527. (interactive "P")
  6528. (org-insert-heading arg)
  6529. (cond
  6530. ((org-at-heading-p) (org-do-demote))
  6531. ((org-at-item-p) (org-indent-item))))
  6532. (defun org-insert-todo-subheading (arg)
  6533. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6534. Works for outline headings and for plain lists alike."
  6535. (interactive "P")
  6536. (org-insert-todo-heading arg)
  6537. (cond
  6538. ((org-at-heading-p) (org-do-demote))
  6539. ((org-at-item-p) (org-indent-item))))
  6540. ;;; Promotion and Demotion
  6541. (defvar org-after-demote-entry-hook nil
  6542. "Hook run after an entry has been demoted.
  6543. The cursor will be at the beginning of the entry.
  6544. When a subtree is being demoted, the hook will be called for each node.")
  6545. (defvar org-after-promote-entry-hook nil
  6546. "Hook run after an entry has been promoted.
  6547. The cursor will be at the beginning of the entry.
  6548. When a subtree is being promoted, the hook will be called for each node.")
  6549. (defun org-promote-subtree ()
  6550. "Promote the entire subtree.
  6551. See also `org-promote'."
  6552. (interactive)
  6553. (save-excursion
  6554. (org-with-limited-levels (org-map-tree 'org-promote)))
  6555. (org-fix-position-after-promote))
  6556. (defun org-demote-subtree ()
  6557. "Demote the entire subtree. See `org-demote'.
  6558. See also `org-promote'."
  6559. (interactive)
  6560. (save-excursion
  6561. (org-with-limited-levels (org-map-tree 'org-demote)))
  6562. (org-fix-position-after-promote))
  6563. (defun org-do-promote ()
  6564. "Promote the current heading higher up the tree.
  6565. If the region is active in `transient-mark-mode', promote all headings
  6566. in the region."
  6567. (interactive)
  6568. (save-excursion
  6569. (if (org-region-active-p)
  6570. (org-map-region 'org-promote (region-beginning) (region-end))
  6571. (org-promote)))
  6572. (org-fix-position-after-promote))
  6573. (defun org-do-demote ()
  6574. "Demote the current heading lower down the tree.
  6575. If the region is active in `transient-mark-mode', demote all headings
  6576. in the region."
  6577. (interactive)
  6578. (save-excursion
  6579. (if (org-region-active-p)
  6580. (org-map-region 'org-demote (region-beginning) (region-end))
  6581. (org-demote)))
  6582. (org-fix-position-after-promote))
  6583. (defun org-fix-position-after-promote ()
  6584. "Make sure that after pro/demotion cursor position is right."
  6585. (let ((pos (point)))
  6586. (when (save-excursion
  6587. (beginning-of-line 1)
  6588. (looking-at org-todo-line-regexp)
  6589. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6590. (cond ((eobp) (insert " "))
  6591. ((eolp) (insert " "))
  6592. ((equal (char-after) ?\ ) (forward-char 1))))))
  6593. (defun org-current-level ()
  6594. "Return the level of the current entry, or nil if before the first headline.
  6595. The level is the number of stars at the beginning of the headline."
  6596. (save-excursion
  6597. (org-with-limited-levels
  6598. (if (ignore-errors (org-back-to-heading t))
  6599. (funcall outline-level)))))
  6600. (defun org-get-previous-line-level ()
  6601. "Return the outline depth of the last headline before the current line.
  6602. Returns 0 for the first headline in the buffer, and nil if before the
  6603. first headline."
  6604. (let ((current-level (org-current-level))
  6605. (prev-level (when (> (line-number-at-pos) 1)
  6606. (save-excursion
  6607. (beginning-of-line 0)
  6608. (org-current-level)))))
  6609. (cond ((null current-level) nil) ; Before first headline
  6610. ((null prev-level) 0) ; At first headline
  6611. (prev-level))))
  6612. (defun org-reduced-level (l)
  6613. "Compute the effective level of a heading.
  6614. This takes into account the setting of `org-odd-levels-only'."
  6615. (cond
  6616. ((zerop l) 0)
  6617. (org-odd-levels-only (1+ (floor (/ l 2))))
  6618. (t l)))
  6619. (defun org-level-increment ()
  6620. "Return the number of stars that will be added or removed at a
  6621. time to headlines when structure editing, based on the value of
  6622. `org-odd-levels-only'."
  6623. (if org-odd-levels-only 2 1))
  6624. (defun org-get-valid-level (level &optional change)
  6625. "Rectify a level change under the influence of `org-odd-levels-only'
  6626. LEVEL is a current level, CHANGE is by how much the level should be
  6627. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6628. even level numbers will become the next higher odd number."
  6629. (if org-odd-levels-only
  6630. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6631. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6632. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6633. (max 1 (+ level (or change 0)))))
  6634. (if (boundp 'define-obsolete-function-alias)
  6635. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6636. (define-obsolete-function-alias 'org-get-legal-level
  6637. 'org-get-valid-level)
  6638. (define-obsolete-function-alias 'org-get-legal-level
  6639. 'org-get-valid-level "23.1")))
  6640. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6641. ;; ̀org-with-limited-levels'
  6642. (defun org-promote ()
  6643. "Promote the current heading higher up the tree.
  6644. If the region is active in `transient-mark-mode', promote all headings
  6645. in the region."
  6646. (org-back-to-heading t)
  6647. (let* ((level (save-match-data (funcall outline-level)))
  6648. (after-change-functions (remove 'flyspell-after-change-function
  6649. after-change-functions))
  6650. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6651. (diff (abs (- level (length up-head) -1))))
  6652. (cond ((and (= level 1) org-called-with-limited-levels
  6653. org-allow-promoting-top-level-subtree)
  6654. (replace-match "# " nil t))
  6655. ((= level 1)
  6656. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6657. (t (replace-match up-head nil t)))
  6658. ;; Fixup tag positioning
  6659. (unless (= level 1)
  6660. (and org-auto-align-tags (org-set-tags nil t))
  6661. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6662. (run-hooks 'org-after-promote-entry-hook)))
  6663. (defun org-demote ()
  6664. "Demote the current heading lower down the tree.
  6665. If the region is active in `transient-mark-mode', demote all headings
  6666. in the region."
  6667. (org-back-to-heading t)
  6668. (let* ((level (save-match-data (funcall outline-level)))
  6669. (after-change-functions (remove 'flyspell-after-change-function
  6670. after-change-functions))
  6671. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6672. (diff (abs (- level (length down-head) -1))))
  6673. (replace-match down-head nil t)
  6674. ;; Fixup tag positioning
  6675. (and org-auto-align-tags (org-set-tags nil t))
  6676. (if org-adapt-indentation (org-fixup-indentation diff))
  6677. (run-hooks 'org-after-demote-entry-hook)))
  6678. (defun org-cycle-level ()
  6679. "Cycle the level of an empty headline through possible states.
  6680. This goes first to child, then to parent, level, then up the hierarchy.
  6681. After top level, it switches back to sibling level."
  6682. (interactive)
  6683. (let ((org-adapt-indentation nil))
  6684. (when (org-point-at-end-of-empty-headline)
  6685. (setq this-command 'org-cycle-level) ; Only needed for caching
  6686. (let ((cur-level (org-current-level))
  6687. (prev-level (org-get-previous-line-level)))
  6688. (cond
  6689. ;; If first headline in file, promote to top-level.
  6690. ((= prev-level 0)
  6691. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6692. do (org-do-promote)))
  6693. ;; If same level as prev, demote one.
  6694. ((= prev-level cur-level)
  6695. (org-do-demote))
  6696. ;; If parent is top-level, promote to top level if not already.
  6697. ((= prev-level 1)
  6698. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6699. do (org-do-promote)))
  6700. ;; If top-level, return to prev-level.
  6701. ((= cur-level 1)
  6702. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6703. do (org-do-demote)))
  6704. ;; If less than prev-level, promote one.
  6705. ((< cur-level prev-level)
  6706. (org-do-promote))
  6707. ;; If deeper than prev-level, promote until higher than
  6708. ;; prev-level.
  6709. ((> cur-level prev-level)
  6710. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6711. do (org-do-promote))))
  6712. t))))
  6713. (defun org-map-tree (fun)
  6714. "Call FUN for every heading underneath the current one."
  6715. (org-back-to-heading)
  6716. (let ((level (funcall outline-level)))
  6717. (save-excursion
  6718. (funcall fun)
  6719. (while (and (progn
  6720. (outline-next-heading)
  6721. (> (funcall outline-level) level))
  6722. (not (eobp)))
  6723. (funcall fun)))))
  6724. (defun org-map-region (fun beg end)
  6725. "Call FUN for every heading between BEG and END."
  6726. (let ((org-ignore-region t))
  6727. (save-excursion
  6728. (setq end (copy-marker end))
  6729. (goto-char beg)
  6730. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6731. (< (point) end))
  6732. (funcall fun))
  6733. (while (and (progn
  6734. (outline-next-heading)
  6735. (< (point) end))
  6736. (not (eobp)))
  6737. (funcall fun)))))
  6738. (defvar org-property-end-re) ; silence byte-compiler
  6739. (defun org-fixup-indentation (diff)
  6740. "Change the indentation in the current entry by DIFF.
  6741. However, if any line in the current entry has no indentation, or if it
  6742. would end up with no indentation after the change, nothing at all is done."
  6743. (save-excursion
  6744. (let ((end (save-excursion (outline-next-heading)
  6745. (point-marker)))
  6746. (prohibit (if (> diff 0)
  6747. "^\\S-"
  6748. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6749. col)
  6750. (unless (save-excursion (end-of-line 1)
  6751. (re-search-forward prohibit end t))
  6752. (while (and (< (point) end)
  6753. (re-search-forward "^[ \t]+" end t))
  6754. (goto-char (match-end 0))
  6755. (setq col (current-column))
  6756. (if (< diff 0) (replace-match ""))
  6757. (org-indent-to-column (+ diff col))))
  6758. (move-marker end nil))))
  6759. (defun org-convert-to-odd-levels ()
  6760. "Convert an org-mode file with all levels allowed to one with odd levels.
  6761. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6762. level 5 etc."
  6763. (interactive)
  6764. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6765. (let ((outline-level 'org-outline-level)
  6766. (org-odd-levels-only nil) n)
  6767. (save-excursion
  6768. (goto-char (point-min))
  6769. (while (re-search-forward "^\\*\\*+ " nil t)
  6770. (setq n (- (length (match-string 0)) 2))
  6771. (while (>= (setq n (1- n)) 0)
  6772. (org-demote))
  6773. (end-of-line 1))))))
  6774. (defun org-convert-to-oddeven-levels ()
  6775. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6776. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6777. file contains a section with an even level, conversion would
  6778. destroy the structure of the file. An error is signaled in this
  6779. case."
  6780. (interactive)
  6781. (goto-char (point-min))
  6782. ;; First check if there are no even levels
  6783. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6784. (org-show-context t)
  6785. (error "Not all levels are odd in this file. Conversion not possible"))
  6786. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6787. (let ((outline-regexp org-outline-regexp)
  6788. (outline-level 'org-outline-level)
  6789. (org-odd-levels-only nil) n)
  6790. (save-excursion
  6791. (goto-char (point-min))
  6792. (while (re-search-forward "^\\*\\*+ " nil t)
  6793. (setq n (/ (1- (length (match-string 0))) 2))
  6794. (while (>= (setq n (1- n)) 0)
  6795. (org-promote))
  6796. (end-of-line 1))))))
  6797. (defun org-tr-level (n)
  6798. "Make N odd if required."
  6799. (if org-odd-levels-only (1+ (/ n 2)) n))
  6800. ;;; Vertical tree motion, cutting and pasting of subtrees
  6801. (defun org-move-subtree-up (&optional arg)
  6802. "Move the current subtree up past ARG headlines of the same level."
  6803. (interactive "p")
  6804. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6805. (defun org-move-subtree-down (&optional arg)
  6806. "Move the current subtree down past ARG headlines of the same level."
  6807. (interactive "p")
  6808. (setq arg (prefix-numeric-value arg))
  6809. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6810. 'org-get-last-sibling))
  6811. (ins-point (make-marker))
  6812. (cnt (abs arg))
  6813. (col (current-column))
  6814. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6815. ;; Select the tree
  6816. (org-back-to-heading)
  6817. (setq beg0 (point))
  6818. (save-excursion
  6819. (setq ne-beg (org-back-over-empty-lines))
  6820. (setq beg (point)))
  6821. (save-match-data
  6822. (save-excursion (outline-end-of-heading)
  6823. (setq folded (outline-invisible-p)))
  6824. (outline-end-of-subtree))
  6825. (outline-next-heading)
  6826. (setq ne-end (org-back-over-empty-lines))
  6827. (setq end (point))
  6828. (goto-char beg0)
  6829. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6830. ;; include less whitespace
  6831. (save-excursion
  6832. (goto-char beg)
  6833. (forward-line (- ne-beg ne-end))
  6834. (setq beg (point))))
  6835. ;; Find insertion point, with error handling
  6836. (while (> cnt 0)
  6837. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6838. (progn (goto-char beg0)
  6839. (error "Cannot move past superior level or buffer limit")))
  6840. (setq cnt (1- cnt)))
  6841. (if (> arg 0)
  6842. ;; Moving forward - still need to move over subtree
  6843. (progn (org-end-of-subtree t t)
  6844. (save-excursion
  6845. (org-back-over-empty-lines)
  6846. (or (bolp) (newline)))))
  6847. (setq ne-ins (org-back-over-empty-lines))
  6848. (move-marker ins-point (point))
  6849. (setq txt (buffer-substring beg end))
  6850. (org-save-markers-in-region beg end)
  6851. (delete-region beg end)
  6852. (org-remove-empty-overlays-at beg)
  6853. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6854. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6855. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6856. (let ((bbb (point)))
  6857. (insert-before-markers txt)
  6858. (org-reinstall-markers-in-region bbb)
  6859. (move-marker ins-point bbb))
  6860. (or (bolp) (insert "\n"))
  6861. (setq ins-end (point))
  6862. (goto-char ins-point)
  6863. (org-skip-whitespace)
  6864. (when (and (< arg 0)
  6865. (org-first-sibling-p)
  6866. (> ne-ins ne-beg))
  6867. ;; Move whitespace back to beginning
  6868. (save-excursion
  6869. (goto-char ins-end)
  6870. (let ((kill-whole-line t))
  6871. (kill-line (- ne-ins ne-beg)) (point)))
  6872. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6873. (move-marker ins-point nil)
  6874. (if folded
  6875. (hide-subtree)
  6876. (org-show-entry)
  6877. (show-children)
  6878. (org-cycle-hide-drawers 'children))
  6879. (org-clean-visibility-after-subtree-move)
  6880. ;; move back to the initial column we were at
  6881. (move-to-column col)))
  6882. (defvar org-subtree-clip ""
  6883. "Clipboard for cut and paste of subtrees.
  6884. This is actually only a copy of the kill, because we use the normal kill
  6885. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6886. (defvar org-subtree-clip-folded nil
  6887. "Was the last copied subtree folded?
  6888. This is used to fold the tree back after pasting.")
  6889. (defun org-cut-subtree (&optional n)
  6890. "Cut the current subtree into the clipboard.
  6891. With prefix arg N, cut this many sequential subtrees.
  6892. This is a short-hand for marking the subtree and then cutting it."
  6893. (interactive "p")
  6894. (org-copy-subtree n 'cut))
  6895. (defun org-copy-subtree (&optional n cut force-store-markers)
  6896. "Cut the current subtree into the clipboard.
  6897. With prefix arg N, cut this many sequential subtrees.
  6898. This is a short-hand for marking the subtree and then copying it.
  6899. If CUT is non-nil, actually cut the subtree.
  6900. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6901. of some markers in the region, even if CUT is non-nil. This is
  6902. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6903. (interactive "p")
  6904. (let (beg end folded (beg0 (point)))
  6905. (if (org-called-interactively-p 'any)
  6906. (org-back-to-heading nil) ; take what looks like a subtree
  6907. (org-back-to-heading t)) ; take what is really there
  6908. (setq beg (point))
  6909. (skip-chars-forward " \t\r\n")
  6910. (save-match-data
  6911. (save-excursion (outline-end-of-heading)
  6912. (setq folded (outline-invisible-p)))
  6913. (condition-case nil
  6914. (org-forward-heading-same-level (1- n) t)
  6915. (error nil))
  6916. (org-end-of-subtree t t))
  6917. (setq end (point))
  6918. (goto-char beg0)
  6919. (when (> end beg)
  6920. (setq org-subtree-clip-folded folded)
  6921. (when (or cut force-store-markers)
  6922. (org-save-markers-in-region beg end))
  6923. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6924. (setq org-subtree-clip (current-kill 0))
  6925. (message "%s: Subtree(s) with %d characters"
  6926. (if cut "Cut" "Copied")
  6927. (length org-subtree-clip)))))
  6928. (defun org-paste-subtree (&optional level tree for-yank)
  6929. "Paste the clipboard as a subtree, with modification of headline level.
  6930. The entire subtree is promoted or demoted in order to match a new headline
  6931. level.
  6932. If the cursor is at the beginning of a headline, the same level as
  6933. that headline is used to paste the tree
  6934. If not, the new level is derived from the *visible* headings
  6935. before and after the insertion point, and taken to be the inferior headline
  6936. level of the two. So if the previous visible heading is level 3 and the
  6937. next is level 4 (or vice versa), level 4 will be used for insertion.
  6938. This makes sure that the subtree remains an independent subtree and does
  6939. not swallow low level entries.
  6940. You can also force a different level, either by using a numeric prefix
  6941. argument, or by inserting the heading marker by hand. For example, if the
  6942. cursor is after \"*****\", then the tree will be shifted to level 5.
  6943. If optional TREE is given, use this text instead of the kill ring.
  6944. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6945. move back over whitespace before inserting, and move point to the end of
  6946. the inserted text when done."
  6947. (interactive "P")
  6948. (setq tree (or tree (and kill-ring (current-kill 0))))
  6949. (unless (org-kill-is-subtree-p tree)
  6950. (error "%s"
  6951. (substitute-command-keys
  6952. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6953. (org-with-limited-levels
  6954. (let* ((visp (not (outline-invisible-p)))
  6955. (txt tree)
  6956. (^re_ "\\(\\*+\\)[ \t]*")
  6957. (old-level (if (string-match org-outline-regexp-bol txt)
  6958. (- (match-end 0) (match-beginning 0) 1)
  6959. -1))
  6960. (force-level (cond (level (prefix-numeric-value level))
  6961. ((and (looking-at "[ \t]*$")
  6962. (string-match
  6963. "^\\*+$" (buffer-substring
  6964. (point-at-bol) (point))))
  6965. (- (match-end 1) (match-beginning 1)))
  6966. ((and (bolp)
  6967. (looking-at org-outline-regexp))
  6968. (- (match-end 0) (point) 1))))
  6969. (previous-level (save-excursion
  6970. (condition-case nil
  6971. (progn
  6972. (outline-previous-visible-heading 1)
  6973. (if (looking-at ^re_)
  6974. (- (match-end 0) (match-beginning 0) 1)
  6975. 1))
  6976. (error 1))))
  6977. (next-level (save-excursion
  6978. (condition-case nil
  6979. (progn
  6980. (or (looking-at org-outline-regexp)
  6981. (outline-next-visible-heading 1))
  6982. (if (looking-at ^re_)
  6983. (- (match-end 0) (match-beginning 0) 1)
  6984. 1))
  6985. (error 1))))
  6986. (new-level (or force-level (max previous-level next-level)))
  6987. (shift (if (or (= old-level -1)
  6988. (= new-level -1)
  6989. (= old-level new-level))
  6990. 0
  6991. (- new-level old-level)))
  6992. (delta (if (> shift 0) -1 1))
  6993. (func (if (> shift 0) 'org-demote 'org-promote))
  6994. (org-odd-levels-only nil)
  6995. beg end newend)
  6996. ;; Remove the forced level indicator
  6997. (if force-level
  6998. (delete-region (point-at-bol) (point)))
  6999. ;; Paste
  7000. (beginning-of-line (if (bolp) 1 2))
  7001. (setq beg (point))
  7002. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7003. (insert-before-markers txt)
  7004. (unless (string-match "\n\\'" txt) (insert "\n"))
  7005. (setq newend (point))
  7006. (org-reinstall-markers-in-region beg)
  7007. (setq end (point))
  7008. (goto-char beg)
  7009. (skip-chars-forward " \t\n\r")
  7010. (setq beg (point))
  7011. (if (and (outline-invisible-p) visp)
  7012. (save-excursion (outline-show-heading)))
  7013. ;; Shift if necessary
  7014. (unless (= shift 0)
  7015. (save-restriction
  7016. (narrow-to-region beg end)
  7017. (while (not (= shift 0))
  7018. (org-map-region func (point-min) (point-max))
  7019. (setq shift (+ delta shift)))
  7020. (goto-char (point-min))
  7021. (setq newend (point-max))))
  7022. (when (or (org-called-interactively-p 'interactive) for-yank)
  7023. (message "Clipboard pasted as level %d subtree" new-level))
  7024. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7025. kill-ring
  7026. (eq org-subtree-clip (current-kill 0))
  7027. org-subtree-clip-folded)
  7028. ;; The tree was folded before it was killed/copied
  7029. (hide-subtree))
  7030. (and for-yank (goto-char newend)))))
  7031. (defun org-kill-is-subtree-p (&optional txt)
  7032. "Check if the current kill is an outline subtree, or a set of trees.
  7033. Returns nil if kill does not start with a headline, or if the first
  7034. headline level is not the largest headline level in the tree.
  7035. So this will actually accept several entries of equal levels as well,
  7036. which is OK for `org-paste-subtree'.
  7037. If optional TXT is given, check this string instead of the current kill."
  7038. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7039. (re (org-get-limited-outline-regexp))
  7040. (^re (concat "^" re))
  7041. (start-level (and kill
  7042. (string-match
  7043. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7044. kill)
  7045. (- (match-end 2) (match-beginning 2) 1)))
  7046. (start (1+ (or (match-beginning 2) -1))))
  7047. (if (not start-level)
  7048. (progn
  7049. nil) ;; does not even start with a heading
  7050. (catch 'exit
  7051. (while (setq start (string-match ^re kill (1+ start)))
  7052. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7053. (throw 'exit nil)))
  7054. t))))
  7055. (defvar org-markers-to-move nil
  7056. "Markers that should be moved with a cut-and-paste operation.
  7057. Those markers are stored together with their positions relative to
  7058. the start of the region.")
  7059. (defun org-save-markers-in-region (beg end)
  7060. "Check markers in region.
  7061. If these markers are between BEG and END, record their position relative
  7062. to BEG, so that after moving the block of text, we can put the markers back
  7063. into place.
  7064. This function gets called just before an entry or tree gets cut from the
  7065. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7066. called immediately, to move the markers with the entries."
  7067. (setq org-markers-to-move nil)
  7068. (when (featurep 'org-clock)
  7069. (org-clock-save-markers-for-cut-and-paste beg end))
  7070. (when (featurep 'org-agenda)
  7071. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7072. (defun org-check-and-save-marker (marker beg end)
  7073. "Check if MARKER is between BEG and END.
  7074. If yes, remember the marker and the distance to BEG."
  7075. (when (and (marker-buffer marker)
  7076. (equal (marker-buffer marker) (current-buffer)))
  7077. (if (and (>= marker beg) (< marker end))
  7078. (push (cons marker (- marker beg)) org-markers-to-move))))
  7079. (defun org-reinstall-markers-in-region (beg)
  7080. "Move all remembered markers to their position relative to BEG."
  7081. (mapc (lambda (x)
  7082. (move-marker (car x) (+ beg (cdr x))))
  7083. org-markers-to-move)
  7084. (setq org-markers-to-move nil))
  7085. (defun org-narrow-to-subtree ()
  7086. "Narrow buffer to the current subtree."
  7087. (interactive)
  7088. (save-excursion
  7089. (save-match-data
  7090. (org-with-limited-levels
  7091. (narrow-to-region
  7092. (progn (org-back-to-heading t) (point))
  7093. (progn (org-end-of-subtree t t)
  7094. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7095. (point)))))))
  7096. (defun org-narrow-to-block ()
  7097. "Narrow buffer to the current block."
  7098. (interactive)
  7099. (let* ((case-fold-search t)
  7100. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7101. "^[ \t]*#\\+end_.*")))
  7102. (if blockp
  7103. (narrow-to-region (car blockp) (cdr blockp))
  7104. (error "Not in a block"))))
  7105. (eval-when-compile
  7106. (defvar org-property-drawer-re))
  7107. (defvar org-property-start-re) ;; defined below
  7108. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7109. "Clone the task (subtree) at point N times.
  7110. The clones will be inserted as siblings.
  7111. In interactive use, the user will be prompted for the number of
  7112. clones to be produced, and for a time SHIFT, which may be a
  7113. repeater as used in time stamps, for example `+3d'.
  7114. When a valid repeater is given and the entry contains any time
  7115. stamps, the clones will become a sequence in time, with time
  7116. stamps in the subtree shifted for each clone produced. If SHIFT
  7117. is nil or the empty string, time stamps will be left alone. The
  7118. ID property of the original subtree is removed.
  7119. If the original subtree did contain time stamps with a repeater,
  7120. the following will happen:
  7121. - the repeater will be removed in each clone
  7122. - an additional clone will be produced, with the current, unshifted
  7123. date(s) in the entry.
  7124. - the original entry will be placed *after* all the clones, with
  7125. repeater intact.
  7126. - the start days in the repeater in the original entry will be shifted
  7127. to past the last clone.
  7128. In this way you can spell out a number of instances of a repeating task,
  7129. and still retain the repeater to cover future instances of the task."
  7130. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7131. (let (beg end template task idprop
  7132. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7133. (drawer-re org-drawer-regexp))
  7134. (if (not (and (integerp n) (> n 0)))
  7135. (error "Invalid number of replications %s" n))
  7136. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7137. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7138. shift)))
  7139. (error "Invalid shift specification %s" shift))
  7140. (when doshift
  7141. (setq shift-n (string-to-number (match-string 1 shift))
  7142. shift-what (cdr (assoc (match-string 2 shift)
  7143. '(("d" . day) ("w" . week)
  7144. ("m" . month) ("y" . year))))))
  7145. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7146. (setq nmin 1 nmax n)
  7147. (org-back-to-heading t)
  7148. (setq beg (point))
  7149. (setq idprop (org-entry-get nil "ID"))
  7150. (org-end-of-subtree t t)
  7151. (or (bolp) (insert "\n"))
  7152. (setq end (point))
  7153. (setq template (buffer-substring beg end))
  7154. (when (and doshift
  7155. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7156. (delete-region beg end)
  7157. (setq end beg)
  7158. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7159. (goto-char end)
  7160. (loop for n from nmin to nmax do
  7161. ;; prepare clone
  7162. (with-temp-buffer
  7163. (insert template)
  7164. (org-mode)
  7165. (goto-char (point-min))
  7166. (org-show-subtree)
  7167. (and idprop (if org-clone-delete-id
  7168. (org-entry-delete nil "ID")
  7169. (org-id-get-create t)))
  7170. (unless (= n 0)
  7171. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7172. (kill-whole-line))
  7173. (goto-char (point-min))
  7174. (while (re-search-forward drawer-re nil t)
  7175. (mapc (lambda (d)
  7176. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7177. (goto-char (point-min))
  7178. (when doshift
  7179. (while (re-search-forward org-ts-regexp-both nil t)
  7180. (org-timestamp-change (* n shift-n) shift-what))
  7181. (unless (= n n-no-remove)
  7182. (goto-char (point-min))
  7183. (while (re-search-forward org-ts-regexp nil t)
  7184. (save-excursion
  7185. (goto-char (match-beginning 0))
  7186. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7187. (delete-region (match-beginning 1) (match-end 1)))))))
  7188. (setq task (buffer-string)))
  7189. (insert task))
  7190. (goto-char beg)))
  7191. ;;; Outline Sorting
  7192. (defun org-sort (with-case)
  7193. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7194. Optional argument WITH-CASE means sort case-sensitively."
  7195. (interactive "P")
  7196. (cond
  7197. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7198. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7199. (t
  7200. (org-call-with-arg 'org-sort-entries with-case))))
  7201. (defun org-sort-remove-invisible (s)
  7202. (remove-text-properties 0 (length s) org-rm-props s)
  7203. (while (string-match org-bracket-link-regexp s)
  7204. (setq s (replace-match (if (match-end 2)
  7205. (match-string 3 s)
  7206. (match-string 1 s)) t t s)))
  7207. s)
  7208. (defvar org-priority-regexp) ; defined later in the file
  7209. (defvar org-after-sorting-entries-or-items-hook nil
  7210. "Hook that is run after a bunch of entries or items have been sorted.
  7211. When children are sorted, the cursor is in the parent line when this
  7212. hook gets called. When a region or a plain list is sorted, the cursor
  7213. will be in the first entry of the sorted region/list.")
  7214. (defun org-sort-entries
  7215. (&optional with-case sorting-type getkey-func compare-func property)
  7216. "Sort entries on a certain level of an outline tree.
  7217. If there is an active region, the entries in the region are sorted.
  7218. Else, if the cursor is before the first entry, sort the top-level items.
  7219. Else, the children of the entry at point are sorted.
  7220. Sorting can be alphabetically, numerically, by date/time as given by
  7221. a time stamp, by a property or by priority.
  7222. The command prompts for the sorting type unless it has been given to the
  7223. function through the SORTING-TYPE argument, which needs to be a character,
  7224. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7225. precise meaning of each character:
  7226. n Numerically, by converting the beginning of the entry/item to a number.
  7227. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7228. o By order of TODO keywords.
  7229. t By date/time, either the first active time stamp in the entry, or, if
  7230. none exist, by the first inactive one.
  7231. s By the scheduled date/time.
  7232. d By deadline date/time.
  7233. c By creation time, which is assumed to be the first inactive time stamp
  7234. at the beginning of a line.
  7235. p By priority according to the cookie.
  7236. r By the value of a property.
  7237. Capital letters will reverse the sort order.
  7238. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7239. called with point at the beginning of the record. It must return either
  7240. a string or a number that should serve as the sorting key for that record.
  7241. Comparing entries ignores case by default. However, with an optional argument
  7242. WITH-CASE, the sorting considers case as well."
  7243. (interactive "P")
  7244. (let ((case-func (if with-case 'identity 'downcase))
  7245. (cmstr
  7246. ;; The clock marker is lost when using `sort-subr', let's
  7247. ;; store the clocking string.
  7248. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7249. (save-excursion
  7250. (goto-char org-clock-marker)
  7251. (looking-back "^.*") (match-string-no-properties 0))))
  7252. start beg end stars re re2
  7253. txt what tmp)
  7254. ;; Find beginning and end of region to sort
  7255. (cond
  7256. ((org-region-active-p)
  7257. ;; we will sort the region
  7258. (setq end (region-end)
  7259. what "region")
  7260. (goto-char (region-beginning))
  7261. (if (not (org-at-heading-p)) (outline-next-heading))
  7262. (setq start (point)))
  7263. ((or (org-at-heading-p)
  7264. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7265. ;; we will sort the children of the current headline
  7266. (org-back-to-heading)
  7267. (setq start (point)
  7268. end (progn (org-end-of-subtree t t)
  7269. (or (bolp) (insert "\n"))
  7270. (org-back-over-empty-lines)
  7271. (point))
  7272. what "children")
  7273. (goto-char start)
  7274. (show-subtree)
  7275. (outline-next-heading))
  7276. (t
  7277. ;; we will sort the top-level entries in this file
  7278. (goto-char (point-min))
  7279. (or (org-at-heading-p) (outline-next-heading))
  7280. (setq start (point))
  7281. (goto-char (point-max))
  7282. (beginning-of-line 1)
  7283. (when (looking-at ".*?\\S-")
  7284. ;; File ends in a non-white line
  7285. (end-of-line 1)
  7286. (insert "\n"))
  7287. (setq end (point-max))
  7288. (setq what "top-level")
  7289. (goto-char start)
  7290. (show-all)))
  7291. (setq beg (point))
  7292. (if (>= beg end) (error "Nothing to sort"))
  7293. (looking-at "\\(\\*+\\)")
  7294. (setq stars (match-string 1)
  7295. re (concat "^" (regexp-quote stars) " +")
  7296. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7297. txt (buffer-substring beg end))
  7298. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7299. (if (and (not (equal stars "*")) (string-match re2 txt))
  7300. (error "Region to sort contains a level above the first entry"))
  7301. (unless sorting-type
  7302. (message
  7303. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7304. [t]ime [s]cheduled [d]eadline [c]reated
  7305. A/N/P/R/O/F/T/S/D/C means reversed:"
  7306. what)
  7307. (setq sorting-type (read-char-exclusive))
  7308. (and (= (downcase sorting-type) ?f)
  7309. (setq getkey-func
  7310. (org-icompleting-read "Sort using function: "
  7311. obarray 'fboundp t nil nil))
  7312. (setq getkey-func (intern getkey-func)))
  7313. (and (= (downcase sorting-type) ?r)
  7314. (setq property
  7315. (org-icompleting-read "Property: "
  7316. (mapcar 'list (org-buffer-property-keys t))
  7317. nil t))))
  7318. (message "Sorting entries...")
  7319. (save-restriction
  7320. (narrow-to-region start end)
  7321. (let ((dcst (downcase sorting-type))
  7322. (case-fold-search nil)
  7323. (now (current-time)))
  7324. (sort-subr
  7325. (/= dcst sorting-type)
  7326. ;; This function moves to the beginning character of the "record" to
  7327. ;; be sorted.
  7328. (lambda nil
  7329. (if (re-search-forward re nil t)
  7330. (goto-char (match-beginning 0))
  7331. (goto-char (point-max))))
  7332. ;; This function moves to the last character of the "record" being
  7333. ;; sorted.
  7334. (lambda nil
  7335. (save-match-data
  7336. (condition-case nil
  7337. (outline-forward-same-level 1)
  7338. (error
  7339. (goto-char (point-max))))))
  7340. ;; This function returns the value that gets sorted against.
  7341. (lambda nil
  7342. (cond
  7343. ((= dcst ?n)
  7344. (if (looking-at org-complex-heading-regexp)
  7345. (string-to-number (match-string 4))
  7346. nil))
  7347. ((= dcst ?a)
  7348. (if (looking-at org-complex-heading-regexp)
  7349. (funcall case-func (match-string 4))
  7350. nil))
  7351. ((= dcst ?t)
  7352. (let ((end (save-excursion (outline-next-heading) (point))))
  7353. (if (or (re-search-forward org-ts-regexp end t)
  7354. (re-search-forward org-ts-regexp-both end t))
  7355. (org-time-string-to-seconds (match-string 0))
  7356. (org-float-time now))))
  7357. ((= dcst ?c)
  7358. (let ((end (save-excursion (outline-next-heading) (point))))
  7359. (if (re-search-forward
  7360. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7361. end t)
  7362. (org-time-string-to-seconds (match-string 0))
  7363. (org-float-time now))))
  7364. ((= dcst ?s)
  7365. (let ((end (save-excursion (outline-next-heading) (point))))
  7366. (if (re-search-forward org-scheduled-time-regexp end t)
  7367. (org-time-string-to-seconds (match-string 1))
  7368. (org-float-time now))))
  7369. ((= dcst ?d)
  7370. (let ((end (save-excursion (outline-next-heading) (point))))
  7371. (if (re-search-forward org-deadline-time-regexp end t)
  7372. (org-time-string-to-seconds (match-string 1))
  7373. (org-float-time now))))
  7374. ((= dcst ?p)
  7375. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7376. (string-to-char (match-string 2))
  7377. org-default-priority))
  7378. ((= dcst ?r)
  7379. (or (org-entry-get nil property) ""))
  7380. ((= dcst ?o)
  7381. (if (looking-at org-complex-heading-regexp)
  7382. (- 9999 (length (member (match-string 2)
  7383. org-todo-keywords-1)))))
  7384. ((= dcst ?f)
  7385. (if getkey-func
  7386. (progn
  7387. (setq tmp (funcall getkey-func))
  7388. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7389. tmp)
  7390. (error "Invalid key function `%s'" getkey-func)))
  7391. (t (error "Invalid sorting type `%c'" sorting-type))))
  7392. nil
  7393. (cond
  7394. ((= dcst ?a) 'string<)
  7395. ((= dcst ?f) compare-func)
  7396. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7397. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7398. ;; Reset the clock marker if needed
  7399. (when cmstr
  7400. (save-excursion
  7401. (goto-char start)
  7402. (search-forward cmstr nil t)
  7403. (move-marker org-clock-marker (point))))
  7404. (message "Sorting entries...done")))
  7405. (defun org-do-sort (table what &optional with-case sorting-type)
  7406. "Sort TABLE of WHAT according to SORTING-TYPE.
  7407. The user will be prompted for the SORTING-TYPE if the call to this
  7408. function does not specify it. WHAT is only for the prompt, to indicate
  7409. what is being sorted. The sorting key will be extracted from
  7410. the car of the elements of the table.
  7411. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7412. (unless sorting-type
  7413. (message
  7414. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7415. what)
  7416. (setq sorting-type (read-char-exclusive)))
  7417. (let ((dcst (downcase sorting-type))
  7418. extractfun comparefun)
  7419. ;; Define the appropriate functions
  7420. (cond
  7421. ((= dcst ?n)
  7422. (setq extractfun 'string-to-number
  7423. comparefun (if (= dcst sorting-type) '< '>)))
  7424. ((= dcst ?a)
  7425. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7426. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7427. comparefun (if (= dcst sorting-type)
  7428. 'string<
  7429. (lambda (a b) (and (not (string< a b))
  7430. (not (string= a b)))))))
  7431. ((= dcst ?t)
  7432. (setq extractfun
  7433. (lambda (x)
  7434. (if (or (string-match org-ts-regexp x)
  7435. (string-match org-ts-regexp-both x))
  7436. (org-float-time
  7437. (org-time-string-to-time (match-string 0 x)))
  7438. 0))
  7439. comparefun (if (= dcst sorting-type) '< '>)))
  7440. (t (error "Invalid sorting type `%c'" sorting-type)))
  7441. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7442. table)
  7443. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7444. ;;; The orgstruct minor mode
  7445. ;; Define a minor mode which can be used in other modes in order to
  7446. ;; integrate the org-mode structure editing commands.
  7447. ;; This is really a hack, because the org-mode structure commands use
  7448. ;; keys which normally belong to the major mode. Here is how it
  7449. ;; works: The minor mode defines all the keys necessary to operate the
  7450. ;; structure commands, but wraps the commands into a function which
  7451. ;; tests if the cursor is currently at a headline or a plain list
  7452. ;; item. If that is the case, the structure command is used,
  7453. ;; temporarily setting many Org-mode variables like regular
  7454. ;; expressions for filling etc. However, when any of those keys is
  7455. ;; used at a different location, function uses `key-binding' to look
  7456. ;; up if the key has an associated command in another currently active
  7457. ;; keymap (minor modes, major mode, global), and executes that
  7458. ;; command. There might be problems if any of the keys is otherwise
  7459. ;; used as a prefix key.
  7460. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7461. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7462. ;; addresses this by checking explicitly for both bindings.
  7463. (defvar orgstruct-mode-map (make-sparse-keymap)
  7464. "Keymap for the minor `orgstruct-mode'.")
  7465. (defvar org-local-vars nil
  7466. "List of local variables, for use by `orgstruct-mode'.")
  7467. ;;;###autoload
  7468. (define-minor-mode orgstruct-mode
  7469. "Toggle the minor mode `orgstruct-mode'.
  7470. This mode is for using Org-mode structure commands in other
  7471. modes. The following keys behave as if Org-mode were active, if
  7472. the cursor is on a headline, or on a plain list item (both as
  7473. defined by Org-mode).
  7474. M-up Move entry/item up
  7475. M-down Move entry/item down
  7476. M-left Promote
  7477. M-right Demote
  7478. M-S-up Move entry/item up
  7479. M-S-down Move entry/item down
  7480. M-S-left Promote subtree
  7481. M-S-right Demote subtree
  7482. M-q Fill paragraph and items like in Org-mode
  7483. C-c ^ Sort entries
  7484. C-c - Cycle list bullet
  7485. TAB Cycle item visibility
  7486. M-RET Insert new heading/item
  7487. S-M-RET Insert new TODO heading / Checkbox item
  7488. C-c C-c Set tags / toggle checkbox"
  7489. nil " OrgStruct" nil
  7490. (org-load-modules-maybe)
  7491. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7492. ;;;###autoload
  7493. (defun turn-on-orgstruct ()
  7494. "Unconditionally turn on `orgstruct-mode'."
  7495. (orgstruct-mode 1))
  7496. (defvar org-fb-vars nil)
  7497. (make-variable-buffer-local 'org-fb-vars)
  7498. (defun orgstruct++-mode (&optional arg)
  7499. "Toggle `orgstruct-mode', the enhanced version of it.
  7500. In addition to setting orgstruct-mode, this also exports all
  7501. indentation and autofilling variables from org-mode into the
  7502. buffer. It will also recognize item context in multiline items."
  7503. (interactive "P")
  7504. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7505. (if (< arg 1)
  7506. (progn (orgstruct-mode -1)
  7507. (mapc (lambda(v)
  7508. (org-set-local (car v)
  7509. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7510. org-fb-vars))
  7511. (orgstruct-mode 1)
  7512. (setq org-fb-vars nil)
  7513. (let (var val)
  7514. (mapc
  7515. (lambda (x)
  7516. (when (string-match
  7517. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7518. (symbol-name (car x)))
  7519. (setq var (car x) val (nth 1 x))
  7520. (push (list var `(quote ,(eval var))) org-fb-vars)
  7521. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7522. org-local-vars)
  7523. (org-set-local 'orgstruct-is-++ t))))
  7524. (defvar orgstruct-is-++ nil
  7525. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7526. (make-variable-buffer-local 'orgstruct-is-++)
  7527. ;;;###autoload
  7528. (defun turn-on-orgstruct++ ()
  7529. "Unconditionally turn on `orgstruct++-mode'."
  7530. (orgstruct++-mode 1))
  7531. (defun orgstruct-error ()
  7532. "Error when there is no default binding for a structure key."
  7533. (interactive)
  7534. (error "This key has no function outside structure elements"))
  7535. (defun orgstruct-setup ()
  7536. "Setup orgstruct keymaps."
  7537. (let ((nfunc 0)
  7538. (bindings
  7539. (list
  7540. '([(meta up)] org-metaup)
  7541. '([(meta down)] org-metadown)
  7542. '([(meta left)] org-metaleft)
  7543. '([(meta right)] org-metaright)
  7544. '([(meta shift up)] org-shiftmetaup)
  7545. '([(meta shift down)] org-shiftmetadown)
  7546. '([(meta shift left)] org-shiftmetaleft)
  7547. '([(meta shift right)] org-shiftmetaright)
  7548. '([?\e (up)] org-metaup)
  7549. '([?\e (down)] org-metadown)
  7550. '([?\e (left)] org-metaleft)
  7551. '([?\e (right)] org-metaright)
  7552. '([?\e (shift up)] org-shiftmetaup)
  7553. '([?\e (shift down)] org-shiftmetadown)
  7554. '([?\e (shift left)] org-shiftmetaleft)
  7555. '([?\e (shift right)] org-shiftmetaright)
  7556. '([(shift up)] org-shiftup)
  7557. '([(shift down)] org-shiftdown)
  7558. '([(shift left)] org-shiftleft)
  7559. '([(shift right)] org-shiftright)
  7560. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7561. '("\M-q" fill-paragraph)
  7562. '("\C-c^" org-sort)
  7563. '("\C-c-" org-cycle-list-bullet)))
  7564. elt key fun cmd)
  7565. (while (setq elt (pop bindings))
  7566. (setq nfunc (1+ nfunc))
  7567. (setq key (org-key (car elt))
  7568. fun (nth 1 elt)
  7569. cmd (orgstruct-make-binding fun nfunc key))
  7570. (org-defkey orgstruct-mode-map key cmd))
  7571. ;; Prevent an error for users who forgot to make autoloads
  7572. (require 'org-element)
  7573. ;; Special treatment needed for TAB and RET
  7574. (org-defkey orgstruct-mode-map [(tab)]
  7575. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7576. (org-defkey orgstruct-mode-map "\C-i"
  7577. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7578. (org-defkey orgstruct-mode-map "\M-\C-m"
  7579. (orgstruct-make-binding 'org-insert-heading 105
  7580. "\M-\C-m" [(meta return)]))
  7581. (org-defkey orgstruct-mode-map [(meta return)]
  7582. (orgstruct-make-binding 'org-insert-heading 106
  7583. [(meta return)] "\M-\C-m"))
  7584. (org-defkey orgstruct-mode-map [(shift meta return)]
  7585. (orgstruct-make-binding 'org-insert-todo-heading 107
  7586. [(meta return)] "\M-\C-m"))
  7587. (org-defkey orgstruct-mode-map "\e\C-m"
  7588. (orgstruct-make-binding 'org-insert-heading 108
  7589. "\e\C-m" [?\e (return)]))
  7590. (org-defkey orgstruct-mode-map [?\e (return)]
  7591. (orgstruct-make-binding 'org-insert-heading 109
  7592. [?\e (return)] "\e\C-m"))
  7593. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7594. (orgstruct-make-binding 'org-insert-todo-heading 110
  7595. [?\e (return)] "\e\C-m"))
  7596. (unless org-local-vars
  7597. (setq org-local-vars (org-get-local-variables)))
  7598. t))
  7599. (defun orgstruct-make-binding (fun n &rest keys)
  7600. "Create a function for binding in the structure minor mode.
  7601. FUN is the command to call inside a table. N is used to create a unique
  7602. command name. KEYS are keys that should be checked in for a command
  7603. to execute outside of tables."
  7604. (eval
  7605. (list 'defun
  7606. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7607. '(arg)
  7608. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7609. "Outside of structure, run the binding of `"
  7610. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7611. "'.")
  7612. '(interactive "p")
  7613. (list 'if
  7614. `(org-context-p 'headline 'item
  7615. (and orgstruct-is-++
  7616. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7617. 'item-body))
  7618. (list 'org-run-like-in-org-mode (list 'quote fun))
  7619. (list 'let '(orgstruct-mode)
  7620. (list 'call-interactively
  7621. (append '(or)
  7622. (mapcar (lambda (k)
  7623. (list 'key-binding k))
  7624. keys)
  7625. '('orgstruct-error))))))))
  7626. (defun org-contextualize-keys (alist contexts)
  7627. "Return valid elements in ALIST depending on CONTEXTS.
  7628. `org-agenda-custom-commands' or `org-capture-templates' are the
  7629. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7630. or `org-capture-templates-contexts' are the associated contexts
  7631. definitions."
  7632. (let ((contexts
  7633. ;; normalize contexts
  7634. (mapcar
  7635. (lambda(c) (cond ((listp (cadr c))
  7636. (list (car c) (car c) (cadr c)))
  7637. ((string= "" (cadr c))
  7638. (list (car c) (car c) (caddr c)))
  7639. (t c))) contexts))
  7640. (a alist) c r s)
  7641. ;; loop over all commands or templates
  7642. (while (setq c (pop a))
  7643. (let (vrules repl)
  7644. (cond
  7645. ((not (assoc (car c) contexts))
  7646. (push c r))
  7647. ((and (assoc (car c) contexts)
  7648. (setq vrules (org-contextualize-validate-key
  7649. (car c) contexts)))
  7650. (mapc (lambda (vr)
  7651. (when (not (equal (car vr) (cadr vr)))
  7652. (setq repl vr))) vrules)
  7653. (if (not repl) (push c r)
  7654. (push (cadr repl) s)
  7655. (push
  7656. (cons (car c)
  7657. (cdr (or (assoc (cadr repl) alist)
  7658. (error "Undefined key `%s' as contextual replacement for `%s'"
  7659. (cadr repl) (car c)))))
  7660. r))))))
  7661. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7662. (delq
  7663. nil
  7664. (delete-dups
  7665. (mapcar (lambda (x)
  7666. (let ((tpl (car x)))
  7667. (when (not (delq
  7668. nil
  7669. (mapcar (lambda(y)
  7670. (equal y tpl)) s))) x)))
  7671. (reverse r))))))
  7672. (defun org-contextualize-validate-key (key contexts)
  7673. "Check CONTEXTS for agenda or capture KEY."
  7674. (let (r rr res)
  7675. (while (setq r (pop contexts))
  7676. (mapc
  7677. (lambda (rr)
  7678. (when
  7679. (and (equal key (car r))
  7680. (if (functionp rr) (funcall rr)
  7681. (or (and (eq (car rr) 'in-file)
  7682. (buffer-file-name)
  7683. (string-match (cdr rr) (buffer-file-name)))
  7684. (and (eq (car rr) 'in-mode)
  7685. (string-match (cdr rr) (symbol-name major-mode)))
  7686. (when (and (eq (car rr) 'not-in-file)
  7687. (buffer-file-name))
  7688. (not (string-match (cdr rr) (buffer-file-name))))
  7689. (when (eq (car rr) 'not-in-mode)
  7690. (not (string-match (cdr rr) (symbol-name major-mode)))))))
  7691. (push r res)))
  7692. (car (last r))))
  7693. (delete-dups (delq nil res))))
  7694. (defun org-context-p (&rest contexts)
  7695. "Check if local context is any of CONTEXTS.
  7696. Possible values in the list of contexts are `table', `headline', and `item'."
  7697. (let ((pos (point)))
  7698. (goto-char (point-at-bol))
  7699. (prog1 (or (and (memq 'table contexts)
  7700. (looking-at "[ \t]*|"))
  7701. (and (memq 'headline contexts)
  7702. (looking-at org-outline-regexp))
  7703. (and (memq 'item contexts)
  7704. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7705. (and (memq 'item-body contexts)
  7706. (org-in-item-p)))
  7707. (goto-char pos))))
  7708. (defun org-get-local-variables ()
  7709. "Return a list of all local variables in an Org mode buffer."
  7710. (let (varlist)
  7711. (with-current-buffer (get-buffer-create "*Org tmp*")
  7712. (erase-buffer)
  7713. (org-mode)
  7714. (setq varlist (buffer-local-variables)))
  7715. (kill-buffer "*Org tmp*")
  7716. (delq nil
  7717. (mapcar
  7718. (lambda (x)
  7719. (setq x
  7720. (if (symbolp x)
  7721. (list x)
  7722. (list (car x) (list 'quote (cdr x)))))
  7723. (if (string-match
  7724. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7725. (symbol-name (car x)))
  7726. x nil))
  7727. varlist))))
  7728. (defun org-clone-local-variables (from-buffer &optional regexp)
  7729. "Clone local variables from FROM-BUFFER.
  7730. Optional argument REGEXP selects variables to clone."
  7731. (mapc
  7732. (lambda (pair)
  7733. (and (symbolp (car pair))
  7734. (or (null regexp)
  7735. (string-match regexp (symbol-name (car pair))))
  7736. (set (make-local-variable (car pair))
  7737. (cdr pair))))
  7738. (buffer-local-variables from-buffer)))
  7739. ;;;###autoload
  7740. (defun org-run-like-in-org-mode (cmd)
  7741. "Run a command, pretending that the current buffer is in Org-mode.
  7742. This will temporarily bind local variables that are typically bound in
  7743. Org-mode to the values they have in Org-mode, and then interactively
  7744. call CMD."
  7745. (org-load-modules-maybe)
  7746. (unless org-local-vars
  7747. (setq org-local-vars (org-get-local-variables)))
  7748. (eval (list 'let org-local-vars
  7749. (list 'call-interactively (list 'quote cmd)))))
  7750. ;;;; Archiving
  7751. (defun org-get-category (&optional pos force-refresh)
  7752. "Get the category applying to position POS."
  7753. (save-match-data
  7754. (if force-refresh (org-refresh-category-properties))
  7755. (let ((pos (or pos (point))))
  7756. (or (get-text-property pos 'org-category)
  7757. (progn (org-refresh-category-properties)
  7758. (get-text-property pos 'org-category))))))
  7759. (defun org-refresh-category-properties ()
  7760. "Refresh category text properties in the buffer."
  7761. (let ((case-fold-search t)
  7762. (inhibit-read-only t)
  7763. (def-cat (cond
  7764. ((null org-category)
  7765. (if buffer-file-name
  7766. (file-name-sans-extension
  7767. (file-name-nondirectory buffer-file-name))
  7768. "???"))
  7769. ((symbolp org-category) (symbol-name org-category))
  7770. (t org-category)))
  7771. beg end cat pos optionp)
  7772. (org-unmodified
  7773. (save-excursion
  7774. (save-restriction
  7775. (widen)
  7776. (goto-char (point-min))
  7777. (put-text-property (point) (point-max) 'org-category def-cat)
  7778. (while (re-search-forward
  7779. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7780. (setq pos (match-end 0)
  7781. optionp (equal (char-after (match-beginning 0)) ?#)
  7782. cat (org-trim (match-string 2)))
  7783. (if optionp
  7784. (setq beg (point-at-bol) end (point-max))
  7785. (org-back-to-heading t)
  7786. (setq beg (point) end (org-end-of-subtree t t)))
  7787. (put-text-property beg end 'org-category cat)
  7788. (put-text-property beg end 'org-category-position beg)
  7789. (goto-char pos)))))))
  7790. (defun org-refresh-properties (dprop tprop)
  7791. "Refresh buffer text properties.
  7792. DPROP is the drawer property and TPROP is the corresponding text
  7793. property to set."
  7794. (let ((case-fold-search t)
  7795. (inhibit-read-only t) p)
  7796. (org-unmodified
  7797. (save-excursion
  7798. (save-restriction
  7799. (widen)
  7800. (goto-char (point-min))
  7801. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  7802. (setq p (org-match-string-no-properties 1))
  7803. (save-excursion
  7804. (org-back-to-heading t)
  7805. (put-text-property
  7806. (point-at-bol) (point-at-eol) tprop p))))))))
  7807. ;;;; Link Stuff
  7808. ;;; Link abbreviations
  7809. (defun org-link-expand-abbrev (link)
  7810. "Apply replacements as defined in `org-link-abbrev-alist'."
  7811. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7812. (let* ((key (match-string 1 link))
  7813. (as (or (assoc key org-link-abbrev-alist-local)
  7814. (assoc key org-link-abbrev-alist)))
  7815. (tag (and (match-end 2) (match-string 3 link)))
  7816. rpl)
  7817. (if (not as)
  7818. link
  7819. (setq rpl (cdr as))
  7820. (cond
  7821. ((symbolp rpl) (funcall rpl tag))
  7822. ((string-match "%(\\([^)]+\\))" rpl)
  7823. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7824. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7825. ((string-match "%h" rpl)
  7826. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7827. (t (concat rpl tag)))))
  7828. link))
  7829. ;;; Storing and inserting links
  7830. (defvar org-insert-link-history nil
  7831. "Minibuffer history for links inserted with `org-insert-link'.")
  7832. (defvar org-stored-links nil
  7833. "Contains the links stored with `org-store-link'.")
  7834. (defvar org-store-link-plist nil
  7835. "Plist with info about the most recently link created with `org-store-link'.")
  7836. (defvar org-link-protocols nil
  7837. "Link protocols added to Org-mode using `org-add-link-type'.")
  7838. (defvar org-store-link-functions nil
  7839. "List of functions that are called to create and store a link.
  7840. Each function will be called in turn until one returns a non-nil
  7841. value. Each function should check if it is responsible for creating
  7842. this link (for example by looking at the major mode).
  7843. If not, it must exit and return nil.
  7844. If yes, it should return a non-nil value after a calling
  7845. `org-store-link-props' with a list of properties and values.
  7846. Special properties are:
  7847. :type The link prefix, like \"http\". This must be given.
  7848. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7849. This is obligatory as well.
  7850. :description Optional default description for the second pair
  7851. of brackets in an Org-mode link. The user can still change
  7852. this when inserting this link into an Org-mode buffer.
  7853. In addition to these, any additional properties can be specified
  7854. and then used in capture templates.")
  7855. (defun org-add-link-type (type &optional follow export)
  7856. "Add TYPE to the list of `org-link-types'.
  7857. Re-compute all regular expressions depending on `org-link-types'
  7858. FOLLOW and EXPORT are two functions.
  7859. FOLLOW should take the link path as the single argument and do whatever
  7860. is necessary to follow the link, for example find a file or display
  7861. a mail message.
  7862. EXPORT should format the link path for export to one of the export formats.
  7863. It should be a function accepting three arguments:
  7864. path the path of the link, the text after the prefix (like \"http:\")
  7865. desc the description of the link, if any, or a description added by
  7866. org-export-normalize-links if there is none
  7867. format the export format, a symbol like `html' or `latex' or `ascii'..
  7868. The function may use the FORMAT information to return different values
  7869. depending on the format. The return value will be put literally into
  7870. the exported file. If the return value is nil, this means Org should
  7871. do what it normally does with links which do not have EXPORT defined.
  7872. Org-mode has a built-in default for exporting links. If you are happy with
  7873. this default, there is no need to define an export function for the link
  7874. type. For a simple example of an export function, see `org-bbdb.el'."
  7875. (add-to-list 'org-link-types type t)
  7876. (org-make-link-regexps)
  7877. (if (assoc type org-link-protocols)
  7878. (setcdr (assoc type org-link-protocols) (list follow export))
  7879. (push (list type follow export) org-link-protocols)))
  7880. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  7881. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  7882. ;;;###autoload
  7883. (defun org-store-link (arg)
  7884. "\\<org-mode-map>Store an org-link to the current location.
  7885. This link is added to `org-stored-links' and can later be inserted
  7886. into an org-buffer with \\[org-insert-link].
  7887. For some link types, a prefix arg is interpreted:
  7888. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7889. For file links, arg negates `org-context-in-file-links'."
  7890. (interactive "P")
  7891. (org-load-modules-maybe)
  7892. (setq org-store-link-plist nil) ; reset
  7893. (org-with-limited-levels
  7894. (let (link cpltxt desc description search txt custom-id agenda-link)
  7895. (cond
  7896. ((run-hook-with-args-until-success 'org-store-link-functions)
  7897. (setq link (plist-get org-store-link-plist :link)
  7898. desc (or (plist-get org-store-link-plist :description) link)))
  7899. ((org-src-edit-buffer-p)
  7900. (let (label gc)
  7901. (while (or (not label)
  7902. (save-excursion
  7903. (save-restriction
  7904. (widen)
  7905. (goto-char (point-min))
  7906. (re-search-forward
  7907. (regexp-quote (format org-coderef-label-format label))
  7908. nil t))))
  7909. (when label (message "Label exists already") (sit-for 2))
  7910. (setq label (read-string "Code line label: " label)))
  7911. (end-of-line 1)
  7912. (setq link (format org-coderef-label-format label))
  7913. (setq gc (- 79 (length link)))
  7914. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7915. (insert link)
  7916. (setq link (concat "(" label ")") desc nil)))
  7917. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7918. ;; We are in the agenda, link to referenced location
  7919. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7920. (get-text-property (point) 'org-marker))))
  7921. (when m
  7922. (org-with-point-at m
  7923. (setq agenda-link
  7924. (if (org-called-interactively-p 'any)
  7925. (call-interactively 'org-store-link)
  7926. (org-store-link nil)))))))
  7927. ((eq major-mode 'calendar-mode)
  7928. (let ((cd (calendar-cursor-to-date)))
  7929. (setq link
  7930. (format-time-string
  7931. (car org-time-stamp-formats)
  7932. (apply 'encode-time
  7933. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7934. nil nil nil))))
  7935. (org-store-link-props :type "calendar" :date cd)))
  7936. ((eq major-mode 'help-mode)
  7937. (setq link (concat "help:" (save-excursion
  7938. (goto-char (point-min))
  7939. (looking-at "^[^ ]+")
  7940. (match-string 0))))
  7941. (org-store-link-props :type "help"))
  7942. ((eq major-mode 'w3-mode)
  7943. (setq cpltxt (if (and (buffer-name)
  7944. (not (string-match "Untitled" (buffer-name))))
  7945. (buffer-name)
  7946. (url-view-url t))
  7947. link (url-view-url t))
  7948. (org-store-link-props :type "w3" :url (url-view-url t)))
  7949. ((eq major-mode 'w3m-mode)
  7950. (setq cpltxt (or w3m-current-title w3m-current-url)
  7951. link w3m-current-url)
  7952. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7953. ((setq search (run-hook-with-args-until-success
  7954. 'org-create-file-search-functions))
  7955. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7956. "::" search))
  7957. (setq cpltxt (or description link)))
  7958. ((eq major-mode 'image-mode)
  7959. (setq cpltxt (concat "file:"
  7960. (abbreviate-file-name buffer-file-name))
  7961. link cpltxt)
  7962. (org-store-link-props :type "image" :file buffer-file-name))
  7963. ((eq major-mode 'dired-mode)
  7964. ;; link to the file in the current line
  7965. (let ((file (dired-get-filename nil t)))
  7966. (setq file (if file
  7967. (abbreviate-file-name
  7968. (expand-file-name (dired-get-filename nil t)))
  7969. ;; otherwise, no file so use current directory.
  7970. default-directory))
  7971. (setq cpltxt (concat "file:" file)
  7972. link cpltxt)))
  7973. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  7974. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7975. (cond
  7976. ((org-in-regexp "<<\\(.*?\\)>>")
  7977. (setq cpltxt
  7978. (concat "file:"
  7979. (abbreviate-file-name
  7980. (buffer-file-name (buffer-base-buffer)))
  7981. "::" (match-string 1))
  7982. link cpltxt))
  7983. ((and (featurep 'org-id)
  7984. (or (eq org-id-link-to-org-use-id t)
  7985. (and (org-called-interactively-p 'any)
  7986. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  7987. (and (eq org-id-link-to-org-use-id
  7988. 'create-if-interactive-and-no-custom-id)
  7989. (not custom-id))))
  7990. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  7991. ;; We can make a link using the ID.
  7992. (setq link (condition-case nil
  7993. (prog1 (org-id-store-link)
  7994. (setq desc (plist-get org-store-link-plist :description)))
  7995. (error
  7996. ;; probably before first headline, link to file only
  7997. (concat "file:"
  7998. (abbreviate-file-name
  7999. (buffer-file-name (buffer-base-buffer))))))))
  8000. (t
  8001. ;; Just link to current headline
  8002. (setq cpltxt (concat "file:"
  8003. (abbreviate-file-name
  8004. (buffer-file-name (buffer-base-buffer)))))
  8005. ;; Add a context search string
  8006. (when (org-xor org-context-in-file-links arg)
  8007. (setq txt (cond
  8008. ((org-at-heading-p) nil)
  8009. ((org-region-active-p)
  8010. (buffer-substring (region-beginning) (region-end)))))
  8011. (when (or (null txt) (string-match "\\S-" txt))
  8012. (setq cpltxt
  8013. (concat cpltxt "::"
  8014. (condition-case nil
  8015. (org-make-org-heading-search-string txt)
  8016. (error "")))
  8017. desc (or (nth 4 (ignore-errors
  8018. (org-heading-components))) "NONE"))))
  8019. (if (string-match "::\\'" cpltxt)
  8020. (setq cpltxt (substring cpltxt 0 -2)))
  8021. (setq link cpltxt))))
  8022. ((buffer-file-name (buffer-base-buffer))
  8023. ;; Just link to this file here.
  8024. (setq cpltxt (concat "file:"
  8025. (abbreviate-file-name
  8026. (buffer-file-name (buffer-base-buffer)))))
  8027. ;; Add a context string
  8028. (when (org-xor org-context-in-file-links arg)
  8029. (setq txt (if (org-region-active-p)
  8030. (buffer-substring (region-beginning) (region-end))
  8031. (buffer-substring (point-at-bol) (point-at-eol))))
  8032. ;; Only use search option if there is some text.
  8033. (when (string-match "\\S-" txt)
  8034. (setq cpltxt
  8035. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8036. desc "NONE")))
  8037. (setq link cpltxt))
  8038. ((org-called-interactively-p 'interactive)
  8039. (error "Cannot link to a buffer which is not visiting a file"))
  8040. (t (setq link nil)))
  8041. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8042. (setq link (or link cpltxt)
  8043. desc (or desc cpltxt))
  8044. (if (equal desc "NONE") (setq desc nil))
  8045. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8046. (progn
  8047. (setq org-stored-links
  8048. (cons (list link desc) org-stored-links))
  8049. (message "Stored: %s" (or desc link))
  8050. (when custom-id
  8051. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8052. "::#" custom-id))
  8053. (setq org-stored-links
  8054. (cons (list link desc) org-stored-links))))
  8055. (or agenda-link (and link (org-make-link-string link desc)))))))
  8056. (defun org-store-link-props (&rest plist)
  8057. "Store link properties, extract names and addresses."
  8058. (let (x adr)
  8059. (when (setq x (plist-get plist :from))
  8060. (setq adr (mail-extract-address-components x))
  8061. (setq plist (plist-put plist :fromname (car adr)))
  8062. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8063. (when (setq x (plist-get plist :to))
  8064. (setq adr (mail-extract-address-components x))
  8065. (setq plist (plist-put plist :toname (car adr)))
  8066. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8067. (let ((from (plist-get plist :from))
  8068. (to (plist-get plist :to)))
  8069. (when (and from to org-from-is-user-regexp)
  8070. (setq plist
  8071. (plist-put plist :fromto
  8072. (if (string-match org-from-is-user-regexp from)
  8073. (concat "to %t")
  8074. (concat "from %f"))))))
  8075. (setq org-store-link-plist plist))
  8076. (defun org-add-link-props (&rest plist)
  8077. "Add these properties to the link property list."
  8078. (let (key value)
  8079. (while plist
  8080. (setq key (pop plist) value (pop plist))
  8081. (setq org-store-link-plist
  8082. (plist-put org-store-link-plist key value)))))
  8083. (defun org-email-link-description (&optional fmt)
  8084. "Return the description part of an email link.
  8085. This takes information from `org-store-link-plist' and formats it
  8086. according to FMT (default from `org-email-link-description-format')."
  8087. (setq fmt (or fmt org-email-link-description-format))
  8088. (let* ((p org-store-link-plist)
  8089. (to (plist-get p :toaddress))
  8090. (from (plist-get p :fromaddress))
  8091. (table
  8092. (list
  8093. (cons "%c" (plist-get p :fromto))
  8094. (cons "%F" (plist-get p :from))
  8095. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8096. (cons "%T" (plist-get p :to))
  8097. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8098. (cons "%s" (plist-get p :subject))
  8099. (cons "%d" (plist-get p :date))
  8100. (cons "%m" (plist-get p :message-id)))))
  8101. (when (string-match "%c" fmt)
  8102. ;; Check if the user wrote this message
  8103. (if (and org-from-is-user-regexp from to
  8104. (save-match-data (string-match org-from-is-user-regexp from)))
  8105. (setq fmt (replace-match "to %t" t t fmt))
  8106. (setq fmt (replace-match "from %f" t t fmt))))
  8107. (org-replace-escapes fmt table)))
  8108. (defun org-make-org-heading-search-string (&optional string heading)
  8109. "Make search string for STRING or current headline."
  8110. (interactive)
  8111. (let ((s (or string (org-get-heading)))
  8112. (lines org-context-in-file-links))
  8113. (unless (and string (not heading))
  8114. ;; We are using a headline, clean up garbage in there.
  8115. (if (string-match org-todo-regexp s)
  8116. (setq s (replace-match "" t t s)))
  8117. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8118. (setq s (replace-match "" t t s)))
  8119. (setq s (org-trim s))
  8120. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8121. org-comment-string "\\)") s)
  8122. (setq s (replace-match "" t t s)))
  8123. (while (string-match org-ts-regexp s)
  8124. (setq s (replace-match "" t t s))))
  8125. (or string (setq s (concat "*" s))) ; Add * for headlines
  8126. (when (and string (integerp lines) (> lines 0))
  8127. (let ((slines (org-split-string s "\n")))
  8128. (when (< lines (length slines))
  8129. (setq s (mapconcat
  8130. 'identity
  8131. (reverse (nthcdr (- (length slines) lines)
  8132. (reverse slines))) "\n")))))
  8133. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8134. (defun org-make-link-string (link &optional description)
  8135. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8136. (unless (string-match "\\S-" link)
  8137. (error "Empty link"))
  8138. (when (and description
  8139. (stringp description)
  8140. (not (string-match "\\S-" description)))
  8141. (setq description nil))
  8142. (when (stringp description)
  8143. ;; Remove brackets from the description, they are fatal.
  8144. (while (string-match "\\[" description)
  8145. (setq description (replace-match "{" t t description)))
  8146. (while (string-match "\\]" description)
  8147. (setq description (replace-match "}" t t description))))
  8148. (when (equal link description)
  8149. ;; No description needed, it is identical
  8150. (setq description nil))
  8151. (when (and (not description)
  8152. (not (string-match (org-image-file-name-regexp) link))
  8153. (not (equal link (org-link-escape link))))
  8154. (setq description (org-extract-attributes link)))
  8155. (setq link
  8156. (cond ((string-match (org-image-file-name-regexp) link) link)
  8157. ((string-match org-link-types-re link)
  8158. (concat (match-string 1 link)
  8159. (org-link-escape (substring link (match-end 1)))))
  8160. (t (org-link-escape link))))
  8161. (concat "[[" link "]"
  8162. (if description (concat "[" description "]") "")
  8163. "]"))
  8164. (defconst org-link-escape-chars
  8165. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8166. "List of characters that should be escaped in link.
  8167. This is the list that is used for internal purposes.")
  8168. (defconst org-link-escape-chars-browser
  8169. '(?\ )
  8170. "List of escapes for characters that are problematic in links.
  8171. This is the list that is used before handing over to the browser.")
  8172. (defun org-link-escape (text &optional table merge)
  8173. "Return percent escaped representation of TEXT.
  8174. TEXT is a string with the text to escape.
  8175. Optional argument TABLE is a list with characters that should be
  8176. escaped. When nil, `org-link-escape-chars' is used.
  8177. If optional argument MERGE is set, merge TABLE into
  8178. `org-link-escape-chars'."
  8179. (cond
  8180. ((and table merge)
  8181. (mapc (lambda (defchr)
  8182. (unless (member defchr table)
  8183. (setq table (cons defchr table)))) org-link-escape-chars))
  8184. ((null table)
  8185. (setq table org-link-escape-chars)))
  8186. (mapconcat
  8187. (lambda (char)
  8188. (if (or (member char table)
  8189. (and (or (< char 32) (= char 37) (> char 126))
  8190. org-url-hexify-p))
  8191. (mapconcat (lambda (sequence-element)
  8192. (format "%%%.2X" sequence-element))
  8193. (or (encode-coding-char char 'utf-8)
  8194. (error "Unable to percent escape character: %s"
  8195. (char-to-string char))) "")
  8196. (char-to-string char))) text ""))
  8197. (defun org-link-unescape (str)
  8198. "Unhex hexified Unicode strings as returned from the JavaScript function
  8199. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8200. (unless (and (null str) (string= "" str))
  8201. (let ((pos 0) (case-fold-search t) unhexed)
  8202. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8203. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8204. (setq str (replace-match unhexed t t str))
  8205. (setq pos (+ pos (length unhexed))))))
  8206. str)
  8207. (defun org-link-unescape-compound (hex)
  8208. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8209. Note: this function also decodes single byte encodings like
  8210. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8211. (save-match-data
  8212. (let* ((bytes (cdr (split-string hex "%")))
  8213. (ret "")
  8214. (eat 0)
  8215. (sum 0))
  8216. (while bytes
  8217. (let* ((val (string-to-number (pop bytes) 16))
  8218. (shift-xor
  8219. (if (= 0 eat)
  8220. (cond
  8221. ((>= val 252) (cons 6 252))
  8222. ((>= val 248) (cons 5 248))
  8223. ((>= val 240) (cons 4 240))
  8224. ((>= val 224) (cons 3 224))
  8225. ((>= val 192) (cons 2 192))
  8226. (t (cons 0 0)))
  8227. (cons 6 128))))
  8228. (if (>= val 192) (setq eat (car shift-xor)))
  8229. (setq val (logxor val (cdr shift-xor)))
  8230. (setq sum (+ (lsh sum (car shift-xor)) val))
  8231. (if (> eat 0) (setq eat (- eat 1)))
  8232. (cond
  8233. ((= 0 eat) ;multi byte
  8234. (setq ret (concat ret (org-char-to-string sum)))
  8235. (setq sum 0))
  8236. ((not bytes) ; single byte(s)
  8237. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8238. )) ;; end (while bytes
  8239. ret )))
  8240. (defun org-link-unescape-single-byte-sequence (hex)
  8241. "Unhexify hex-encoded single byte character sequences."
  8242. (mapconcat (lambda (byte)
  8243. (char-to-string (string-to-number byte 16)))
  8244. (cdr (split-string hex "%")) ""))
  8245. (defun org-xor (a b)
  8246. "Exclusive or."
  8247. (if a (not b) b))
  8248. (defun org-fixup-message-id-for-http (s)
  8249. "Replace special characters in a message id, so it can be used in an http query."
  8250. (when (string-match "%" s)
  8251. (setq s (mapconcat (lambda (c)
  8252. (if (eq c ?%)
  8253. "%25"
  8254. (char-to-string c)))
  8255. s "")))
  8256. (while (string-match "<" s)
  8257. (setq s (replace-match "%3C" t t s)))
  8258. (while (string-match ">" s)
  8259. (setq s (replace-match "%3E" t t s)))
  8260. (while (string-match "@" s)
  8261. (setq s (replace-match "%40" t t s)))
  8262. s)
  8263. (defun org-link-prettify (link)
  8264. "Return a human-readable representation of LINK.
  8265. The car of LINK must be a raw link the cdr of LINK must be either
  8266. a link description or nil."
  8267. (let ((desc (or (cadr link) "<no description>")))
  8268. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8269. "<" (car link) ">")))
  8270. ;;;###autoload
  8271. (defun org-insert-link-global ()
  8272. "Insert a link like Org-mode does.
  8273. This command can be called in any mode to insert a link in Org-mode syntax."
  8274. (interactive)
  8275. (org-load-modules-maybe)
  8276. (org-run-like-in-org-mode 'org-insert-link))
  8277. (defun org-insert-all-links (&optional keep)
  8278. "Insert all links in `org-stored-links'."
  8279. (interactive "P")
  8280. (let ((links (copy-sequence org-stored-links)) l)
  8281. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8282. (insert "- ")
  8283. (org-insert-link nil (car l) (cadr l))
  8284. (insert "\n"))))
  8285. (defun org-link-fontify-links-to-this-file ()
  8286. "Fontify links to the current file in `org-stored-links'."
  8287. (let ((f (buffer-file-name)) a b)
  8288. (setq a (mapcar (lambda(l)
  8289. (let ((ll (car l)))
  8290. (when (and (string-match "^file:\\(.+\\)::" ll)
  8291. (equal f (expand-file-name (match-string 1 ll))))
  8292. ll)))
  8293. org-stored-links))
  8294. (when (featurep 'org-id)
  8295. (setq b (mapcar (lambda(l)
  8296. (let ((ll (car l)))
  8297. (when (and (string-match "^id:\\(.+\\)$" ll)
  8298. (equal f (expand-file-name
  8299. (or (org-id-find-id-file
  8300. (match-string 1 ll)) ""))))
  8301. ll)))
  8302. org-stored-links)))
  8303. (mapcar (lambda(l)
  8304. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8305. (delq nil (append a b)))))
  8306. (defvar org-link-links-in-this-file nil)
  8307. (defun org-insert-link (&optional complete-file link-location default-description)
  8308. "Insert a link. At the prompt, enter the link.
  8309. Completion can be used to insert any of the link protocol prefixes like
  8310. http or ftp in use.
  8311. The history can be used to select a link previously stored with
  8312. `org-store-link'. When the empty string is entered (i.e. if you just
  8313. press RET at the prompt), the link defaults to the most recently
  8314. stored link. As SPC triggers completion in the minibuffer, you need to
  8315. use M-SPC or C-q SPC to force the insertion of a space character.
  8316. You will also be prompted for a description, and if one is given, it will
  8317. be displayed in the buffer instead of the link.
  8318. If there is already a link at point, this command will allow you to edit link
  8319. and description parts.
  8320. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8321. be selected using completion. The path to the file will be relative to the
  8322. current directory if the file is in the current directory or a subdirectory.
  8323. Otherwise, the link will be the absolute path as completed in the minibuffer
  8324. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8325. option `org-link-file-path-type'.
  8326. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8327. the current directory or below.
  8328. With three \\[universal-argument] prefixes, negate the meaning of
  8329. `org-keep-stored-link-after-insertion'.
  8330. If `org-make-link-description-function' is non-nil, this function will be
  8331. called with the link target, and the result will be the default
  8332. link description.
  8333. If the LINK-LOCATION parameter is non-nil, this value will be
  8334. used as the link location instead of reading one interactively.
  8335. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8336. be used as the default description."
  8337. (interactive "P")
  8338. (let* ((wcf (current-window-configuration))
  8339. (region (if (org-region-active-p)
  8340. (buffer-substring (region-beginning) (region-end))))
  8341. (remove (and region (list (region-beginning) (region-end))))
  8342. (desc region)
  8343. tmphist ; byte-compile incorrectly complains about this
  8344. (link link-location)
  8345. (abbrevs org-link-abbrev-alist-local)
  8346. entry file all-prefixes auto-desc)
  8347. (cond
  8348. (link-location) ; specified by arg, just use it.
  8349. ((org-in-regexp org-bracket-link-regexp 1)
  8350. ;; We do have a link at point, and we are going to edit it.
  8351. (setq remove (list (match-beginning 0) (match-end 0)))
  8352. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8353. (setq link (read-string "Link: "
  8354. (org-link-unescape
  8355. (org-match-string-no-properties 1)))))
  8356. ((or (org-in-regexp org-angle-link-re)
  8357. (org-in-regexp org-plain-link-re))
  8358. ;; Convert to bracket link
  8359. (setq remove (list (match-beginning 0) (match-end 0))
  8360. link (read-string "Link: "
  8361. (org-remove-angle-brackets (match-string 0)))))
  8362. ((member complete-file '((4) (16)))
  8363. ;; Completing read for file names.
  8364. (setq link (org-file-complete-link complete-file)))
  8365. (t
  8366. ;; Read link, with completion for stored links.
  8367. (org-link-fontify-links-to-this-file)
  8368. (org-switch-to-buffer-other-window "*Org Links*")
  8369. (with-current-buffer "*Org Links*"
  8370. (erase-buffer)
  8371. (insert "Insert a link.
  8372. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8373. (when org-stored-links
  8374. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8375. (insert (mapconcat 'org-link-prettify
  8376. (reverse org-stored-links) "\n")))
  8377. (goto-char (point-min)))
  8378. (let ((cw (selected-window)))
  8379. (select-window (get-buffer-window "*Org Links*" 'visible))
  8380. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8381. (unless (pos-visible-in-window-p (point-max))
  8382. (org-fit-window-to-buffer))
  8383. (and (window-live-p cw) (select-window cw)))
  8384. ;; Fake a link history, containing the stored links.
  8385. (setq tmphist (append (mapcar 'car org-stored-links)
  8386. org-insert-link-history))
  8387. (setq all-prefixes (append (mapcar 'car abbrevs)
  8388. (mapcar 'car org-link-abbrev-alist)
  8389. org-link-types))
  8390. (unwind-protect
  8391. (progn
  8392. (setq link
  8393. (let ((org-completion-use-ido nil)
  8394. (org-completion-use-iswitchb nil))
  8395. (org-completing-read
  8396. "Link: "
  8397. (append
  8398. (mapcar (lambda (x) (list (concat x ":")))
  8399. all-prefixes)
  8400. (mapcar 'car org-stored-links)
  8401. (mapcar 'cadr org-stored-links))
  8402. nil nil nil
  8403. 'tmphist
  8404. (caar org-stored-links))))
  8405. (if (not (string-match "\\S-" link))
  8406. (error "No link selected"))
  8407. (mapc (lambda(l)
  8408. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8409. org-stored-links)
  8410. (if (or (member link all-prefixes)
  8411. (and (equal ":" (substring link -1))
  8412. (member (substring link 0 -1) all-prefixes)
  8413. (setq link (substring link 0 -1))))
  8414. (setq link (org-link-try-special-completion link))))
  8415. (set-window-configuration wcf)
  8416. (kill-buffer "*Org Links*"))
  8417. (setq entry (assoc link org-stored-links))
  8418. (or entry (push link org-insert-link-history))
  8419. (setq desc (or desc (nth 1 entry)))))
  8420. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8421. (not org-keep-stored-link-after-insertion))
  8422. (setq org-stored-links (delq (assoc link org-stored-links)
  8423. org-stored-links)))
  8424. (if (string-match org-plain-link-re link)
  8425. ;; URL-like link, normalize the use of angular brackets.
  8426. (setq link (org-remove-angle-brackets link)))
  8427. ;; Check if we are linking to the current file with a search
  8428. ;; option If yes, simplify the link by using only the search
  8429. ;; option.
  8430. (when (and buffer-file-name
  8431. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8432. (let* ((path (match-string 1 link))
  8433. (case-fold-search nil)
  8434. (search (match-string 2 link)))
  8435. (save-match-data
  8436. (if (equal (file-truename buffer-file-name) (file-truename path))
  8437. ;; We are linking to this same file, with a search option
  8438. (setq link search)))))
  8439. ;; Check if we can/should use a relative path. If yes, simplify the link
  8440. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8441. (let* ((type (match-string 1 link))
  8442. (path (match-string 2 link))
  8443. (origpath path)
  8444. (case-fold-search nil))
  8445. (cond
  8446. ((or (eq org-link-file-path-type 'absolute)
  8447. (equal complete-file '(16)))
  8448. (setq path (abbreviate-file-name (expand-file-name path))))
  8449. ((eq org-link-file-path-type 'noabbrev)
  8450. (setq path (expand-file-name path)))
  8451. ((eq org-link-file-path-type 'relative)
  8452. (setq path (file-relative-name path)))
  8453. (t
  8454. (save-match-data
  8455. (if (string-match (concat "^" (regexp-quote
  8456. (expand-file-name
  8457. (file-name-as-directory
  8458. default-directory))))
  8459. (expand-file-name path))
  8460. ;; We are linking a file with relative path name.
  8461. (setq path (substring (expand-file-name path)
  8462. (match-end 0)))
  8463. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8464. (setq link (concat type path))
  8465. (if (equal desc origpath)
  8466. (setq desc path))))
  8467. (if org-make-link-description-function
  8468. (setq desc
  8469. (or (condition-case nil
  8470. (funcall org-make-link-description-function link desc)
  8471. (error (progn (message "Can't get link description from `%s'"
  8472. (symbol-name org-make-link-description-function))
  8473. (sit-for 2) nil)))
  8474. (read-string "Description: " default-description)))
  8475. (if default-description (setq desc default-description)
  8476. (setq desc (or (and auto-desc desc)
  8477. (read-string "Description: " desc)))))
  8478. (unless (string-match "\\S-" desc) (setq desc nil))
  8479. (if remove (apply 'delete-region remove))
  8480. (insert (org-make-link-string link desc))))
  8481. (defun org-link-try-special-completion (type)
  8482. "If there is completion support for link type TYPE, offer it."
  8483. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8484. (if (functionp fun)
  8485. (funcall fun)
  8486. (read-string "Link (no completion support): " (concat type ":")))))
  8487. (defun org-file-complete-link (&optional arg)
  8488. "Create a file link using completion."
  8489. (let (file link)
  8490. (setq file (read-file-name "File: "))
  8491. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8492. (pwd1 (file-name-as-directory (abbreviate-file-name
  8493. (expand-file-name ".")))))
  8494. (cond
  8495. ((equal arg '(16))
  8496. (setq link (concat
  8497. "file:"
  8498. (abbreviate-file-name (expand-file-name file)))))
  8499. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8500. (setq link (concat "file:" (match-string 1 file))))
  8501. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8502. (expand-file-name file))
  8503. (setq link (concat
  8504. "file:" (match-string 1 (expand-file-name file)))))
  8505. (t (setq link (concat "file:" file)))))
  8506. link))
  8507. (defun org-completing-read (&rest args)
  8508. "Completing-read with SPACE being a normal character."
  8509. (let ((enable-recursive-minibuffers t)
  8510. (minibuffer-local-completion-map
  8511. (copy-keymap minibuffer-local-completion-map)))
  8512. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8513. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8514. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8515. (apply 'org-icompleting-read args)))
  8516. (defun org-completing-read-no-i (&rest args)
  8517. (let (org-completion-use-ido org-completion-use-iswitchb)
  8518. (apply 'org-completing-read args)))
  8519. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8520. "Use iswitch as a completing-read replacement to choose from choices.
  8521. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8522. from."
  8523. (let* ((iswitchb-use-virtual-buffers nil)
  8524. (iswitchb-make-buflist-hook
  8525. (lambda ()
  8526. (setq iswitchb-temp-buflist choices))))
  8527. (iswitchb-read-buffer prompt)))
  8528. (defun org-icompleting-read (&rest args)
  8529. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8530. (org-without-partial-completion
  8531. (if (and org-completion-use-ido
  8532. (fboundp 'ido-completing-read)
  8533. (boundp 'ido-mode) ido-mode
  8534. (listp (second args)))
  8535. (let ((ido-enter-matching-directory nil))
  8536. (apply 'ido-completing-read (concat (car args))
  8537. (if (consp (car (nth 1 args)))
  8538. (mapcar 'car (nth 1 args))
  8539. (nth 1 args))
  8540. (cddr args)))
  8541. (if (and org-completion-use-iswitchb
  8542. (boundp 'iswitchb-mode) iswitchb-mode
  8543. (listp (second args)))
  8544. (apply 'org-iswitchb-completing-read (concat (car args))
  8545. (if (consp (car (nth 1 args)))
  8546. (mapcar 'car (nth 1 args))
  8547. (nth 1 args))
  8548. (cddr args))
  8549. (apply 'completing-read args)))))
  8550. (defun org-extract-attributes (s)
  8551. "Extract the attributes cookie from a string and set as text property."
  8552. (let (a attr (start 0) key value)
  8553. (save-match-data
  8554. (when (string-match "{{\\([^}]+\\)}}$" s)
  8555. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8556. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8557. (setq key (match-string 1 a) value (match-string 2 a)
  8558. start (match-end 0)
  8559. attr (plist-put attr (intern key) value))))
  8560. (org-add-props s nil 'org-attr attr))
  8561. s))
  8562. (defun org-extract-attributes-from-string (tag)
  8563. (let (key value attr)
  8564. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8565. (setq key (match-string 1 tag) value (match-string 2 tag)
  8566. tag (replace-match "" t t tag)
  8567. attr (plist-put attr (intern key) value)))
  8568. (cons tag attr)))
  8569. (defun org-attributes-to-string (plist)
  8570. "Format a property list into an HTML attribute list."
  8571. (let ((s "") key value)
  8572. (while plist
  8573. (setq key (pop plist) value (pop plist))
  8574. (and value
  8575. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8576. s))
  8577. ;;; Opening/following a link
  8578. (defvar org-link-search-failed nil)
  8579. (defvar org-open-link-functions nil
  8580. "Hook for functions finding a plain text link.
  8581. These functions must take a single argument, the link content.
  8582. They will be called for links that look like [[link text][description]]
  8583. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8584. like a filename (e.g. \"./blue.png\").
  8585. These functions will be called *before* Org attempts to resolve the
  8586. link by doing text searches in the current buffer - so if you want a
  8587. link \"[[target]]\" to still find \"<<target>>\", your function should
  8588. handle this as a special case.
  8589. When the function does handle the link, it must return a non-nil value.
  8590. If it decides that it is not responsible for this link, it must return
  8591. nil to indicate that that Org-mode can continue with other options
  8592. like exact and fuzzy text search.")
  8593. (defun org-next-link ()
  8594. "Move forward to the next link.
  8595. If the link is in hidden text, expose it."
  8596. (interactive)
  8597. (when (and org-link-search-failed (eq this-command last-command))
  8598. (goto-char (point-min))
  8599. (message "Link search wrapped back to beginning of buffer"))
  8600. (setq org-link-search-failed nil)
  8601. (let* ((pos (point))
  8602. (ct (org-context))
  8603. (a (assoc :link ct)))
  8604. (if a (goto-char (nth 2 a)))
  8605. (if (re-search-forward org-any-link-re nil t)
  8606. (progn
  8607. (goto-char (match-beginning 0))
  8608. (if (outline-invisible-p) (org-show-context)))
  8609. (goto-char pos)
  8610. (setq org-link-search-failed t)
  8611. (error "No further link found"))))
  8612. (defun org-previous-link ()
  8613. "Move backward to the previous link.
  8614. If the link is in hidden text, expose it."
  8615. (interactive)
  8616. (when (and org-link-search-failed (eq this-command last-command))
  8617. (goto-char (point-max))
  8618. (message "Link search wrapped back to end of buffer"))
  8619. (setq org-link-search-failed nil)
  8620. (let* ((pos (point))
  8621. (ct (org-context))
  8622. (a (assoc :link ct)))
  8623. (if a (goto-char (nth 1 a)))
  8624. (if (re-search-backward org-any-link-re nil t)
  8625. (progn
  8626. (goto-char (match-beginning 0))
  8627. (if (outline-invisible-p) (org-show-context)))
  8628. (goto-char pos)
  8629. (setq org-link-search-failed t)
  8630. (error "No further link found"))))
  8631. (defun org-translate-link (s)
  8632. "Translate a link string if a translation function has been defined."
  8633. (if (and org-link-translation-function
  8634. (fboundp org-link-translation-function)
  8635. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8636. (progn
  8637. (setq s (funcall org-link-translation-function
  8638. (match-string 1 s) (match-string 2 s)))
  8639. (concat (car s) ":" (cdr s)))
  8640. s))
  8641. (defun org-translate-link-from-planner (type path)
  8642. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8643. This is still an experimental function, your mileage may vary."
  8644. (cond
  8645. ((member type '("http" "https" "news" "ftp"))
  8646. ;; standard Internet links are the same.
  8647. nil)
  8648. ((and (equal type "irc") (string-match "^//" path))
  8649. ;; Planner has two / at the beginning of an irc link, we have 1.
  8650. ;; We should have zero, actually....
  8651. (setq path (substring path 1)))
  8652. ((and (equal type "lisp") (string-match "^/" path))
  8653. ;; Planner has a slash, we do not.
  8654. (setq type "elisp" path (substring path 1)))
  8655. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8656. ;; A typical message link. Planner has the id after the final slash,
  8657. ;; we separate it with a hash mark
  8658. (setq path (concat (match-string 1 path) "#"
  8659. (org-remove-angle-brackets (match-string 2 path)))))
  8660. )
  8661. (cons type path))
  8662. (defun org-find-file-at-mouse (ev)
  8663. "Open file link or URL at mouse."
  8664. (interactive "e")
  8665. (mouse-set-point ev)
  8666. (org-open-at-point 'in-emacs))
  8667. (defun org-open-at-mouse (ev)
  8668. "Open file link or URL at mouse.
  8669. See the docstring of `org-open-file' for details."
  8670. (interactive "e")
  8671. (mouse-set-point ev)
  8672. (if (eq major-mode 'org-agenda-mode)
  8673. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8674. (org-open-at-point))
  8675. (defvar org-window-config-before-follow-link nil
  8676. "The window configuration before following a link.
  8677. This is saved in case the need arises to restore it.")
  8678. (defvar org-open-link-marker (make-marker)
  8679. "Marker pointing to the location where `org-open-at-point; was called.")
  8680. ;;;###autoload
  8681. (defun org-open-at-point-global ()
  8682. "Follow a link like Org-mode does.
  8683. This command can be called in any mode to follow a link that has
  8684. Org-mode syntax."
  8685. (interactive)
  8686. (org-run-like-in-org-mode 'org-open-at-point))
  8687. ;;;###autoload
  8688. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8689. "Open a link in the string S, as if it was in Org-mode."
  8690. (interactive "sLink: \nP")
  8691. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8692. (with-temp-buffer
  8693. (let ((org-inhibit-startup (not reference-buffer)))
  8694. (org-mode)
  8695. (insert s)
  8696. (goto-char (point-min))
  8697. (when reference-buffer
  8698. (setq org-link-abbrev-alist-local
  8699. (with-current-buffer reference-buffer
  8700. org-link-abbrev-alist-local)))
  8701. (org-open-at-point arg reference-buffer)))))
  8702. (defvar org-open-at-point-functions nil
  8703. "Hook that is run when following a link at point.
  8704. Functions in this hook must return t if they identify and follow
  8705. a link at point. If they don't find anything interesting at point,
  8706. they must return nil.")
  8707. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8708. (defun org-open-at-point (&optional arg reference-buffer)
  8709. "Open link at or after point.
  8710. If there is no link at point, this function will search forward up to
  8711. the end of the current line.
  8712. Normally, files will be opened by an appropriate application. If the
  8713. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8714. With a double prefix argument, try to open outside of Emacs, in the
  8715. application the system uses for this file type."
  8716. (interactive "P")
  8717. ;; if in a code block, then open the block's results
  8718. (unless (call-interactively #'org-babel-open-src-block-result)
  8719. (org-load-modules-maybe)
  8720. (move-marker org-open-link-marker (point))
  8721. (setq org-window-config-before-follow-link (current-window-configuration))
  8722. (org-remove-occur-highlights nil nil t)
  8723. (cond
  8724. ((and (org-at-heading-p)
  8725. (not (org-at-timestamp-p t))
  8726. (not (org-in-regexp
  8727. (concat org-plain-link-re "\\|"
  8728. org-bracket-link-regexp "\\|"
  8729. org-angle-link-re "\\|"
  8730. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8731. (not (get-text-property (point) 'org-linked-text)))
  8732. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  8733. (lk0 (car lkall))
  8734. (lk (if (stringp lk0) (list lk0) lk0))
  8735. (lkend (cdr lkall)))
  8736. (mapcar (lambda(l)
  8737. (search-forward l nil lkend)
  8738. (goto-char (match-beginning 0))
  8739. (org-open-at-point))
  8740. lk))
  8741. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8742. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8743. ((and (org-at-timestamp-p t)
  8744. (not (org-in-regexp org-bracket-link-regexp)))
  8745. (org-follow-timestamp-link))
  8746. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8747. (not (org-in-regexp org-any-link-re)))
  8748. (org-footnote-action))
  8749. (t
  8750. (let (type path link line search (pos (point)))
  8751. (catch 'match
  8752. (save-excursion
  8753. (skip-chars-forward "^]\n\r")
  8754. (when (org-in-regexp org-bracket-link-regexp 1)
  8755. (setq link (org-extract-attributes
  8756. (org-link-unescape (org-match-string-no-properties 1))))
  8757. (while (string-match " *\n *" link)
  8758. (setq link (replace-match " " t t link)))
  8759. (setq link (org-link-expand-abbrev link))
  8760. (cond
  8761. ((or (file-name-absolute-p link)
  8762. (string-match "^\\.\\.?/" link))
  8763. (setq type "file" path link))
  8764. ((string-match org-link-re-with-space3 link)
  8765. (setq type (match-string 1 link) path (match-string 2 link)))
  8766. ((string-match "^help:+\\(.+\\)" link)
  8767. (setq type "help" path (match-string 1 link)))
  8768. (t (setq type "thisfile" path link)))
  8769. (throw 'match t)))
  8770. (when (get-text-property (point) 'org-linked-text)
  8771. (setq type "thisfile"
  8772. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8773. (1+ (point)) (point))
  8774. path (buffer-substring
  8775. (or (previous-single-property-change pos 'org-linked-text)
  8776. (point-min))
  8777. (or (next-single-property-change pos 'org-linked-text)
  8778. (point-max))))
  8779. (throw 'match t))
  8780. (save-excursion
  8781. (let ((plinkpos (org-in-regexp org-plain-link-re)))
  8782. (when (or (org-in-regexp org-angle-link-re)
  8783. (and plinkpos (goto-char (car plinkpos))
  8784. (save-match-data (not (looking-back "\\[\\[")))))
  8785. (setq type (match-string 1)
  8786. path (org-link-unescape (match-string 2)))
  8787. (throw 'match t))))
  8788. (save-excursion
  8789. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8790. (setq type "tags"
  8791. path (match-string 1))
  8792. (while (string-match ":" path)
  8793. (setq path (replace-match "+" t t path)))
  8794. (throw 'match t)))
  8795. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8796. (setq type "tree-match"
  8797. path (match-string 1))
  8798. (throw 'match t)))
  8799. (unless path
  8800. (user-error "No link found"))
  8801. ;; switch back to reference buffer
  8802. ;; needed when if called in a temporary buffer through
  8803. ;; org-open-link-from-string
  8804. (with-current-buffer (or reference-buffer (current-buffer))
  8805. ;; Remove any trailing spaces in path
  8806. (if (string-match " +\\'" path)
  8807. (setq path (replace-match "" t t path)))
  8808. (if (and org-link-translation-function
  8809. (fboundp org-link-translation-function))
  8810. ;; Check if we need to translate the link
  8811. (let ((tmp (funcall org-link-translation-function type path)))
  8812. (setq type (car tmp) path (cdr tmp))))
  8813. (cond
  8814. ((assoc type org-link-protocols)
  8815. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8816. ((equal type "help")
  8817. (let ((f-or-v (intern path)))
  8818. (cond ((fboundp f-or-v)
  8819. (describe-function f-or-v))
  8820. ((boundp f-or-v)
  8821. (describe-variable f-or-v))
  8822. (t (error "Not a known function or variable")))))
  8823. ((equal type "mailto")
  8824. (let ((cmd (car org-link-mailto-program))
  8825. (args (cdr org-link-mailto-program)) args1
  8826. (address path) (subject "") a)
  8827. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8828. (setq address (match-string 1 path)
  8829. subject (org-link-escape (match-string 2 path))))
  8830. (while args
  8831. (cond
  8832. ((not (stringp (car args))) (push (pop args) args1))
  8833. (t (setq a (pop args))
  8834. (if (string-match "%a" a)
  8835. (setq a (replace-match address t t a)))
  8836. (if (string-match "%s" a)
  8837. (setq a (replace-match subject t t a)))
  8838. (push a args1))))
  8839. (apply cmd (nreverse args1))))
  8840. ((member type '("http" "https" "ftp" "news"))
  8841. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8842. (org-link-escape
  8843. path org-link-escape-chars-browser)
  8844. path))))
  8845. ((string= type "doi")
  8846. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  8847. (org-link-escape
  8848. path org-link-escape-chars-browser)
  8849. path))))
  8850. ((member type '("message"))
  8851. (browse-url (concat type ":" path)))
  8852. ((string= type "tags")
  8853. (org-tags-view arg path))
  8854. ((string= type "tree-match")
  8855. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8856. ((string= type "file")
  8857. (if (string-match "::\\([0-9]+\\)\\'" path)
  8858. (setq line (string-to-number (match-string 1 path))
  8859. path (substring path 0 (match-beginning 0)))
  8860. (if (string-match "::\\(.+\\)\\'" path)
  8861. (setq search (match-string 1 path)
  8862. path (substring path 0 (match-beginning 0)))))
  8863. (if (string-match "[*?{]" (file-name-nondirectory path))
  8864. (dired path)
  8865. (org-open-file path arg line search)))
  8866. ((string= type "shell")
  8867. (let ((buf (generate-new-buffer "*Org Shell Output"))
  8868. (cmd path))
  8869. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8870. (string-match org-confirm-shell-link-not-regexp cmd))
  8871. (not org-confirm-shell-link-function)
  8872. (funcall org-confirm-shell-link-function
  8873. (format "Execute \"%s\" in shell? "
  8874. (org-add-props cmd nil
  8875. 'face 'org-warning))))
  8876. (progn
  8877. (message "Executing %s" cmd)
  8878. (shell-command cmd buf)
  8879. (if (featurep 'midnight)
  8880. (setq clean-buffer-list-kill-buffer-names
  8881. (cons buf clean-buffer-list-kill-buffer-names))))
  8882. (error "Abort"))))
  8883. ((string= type "elisp")
  8884. (let ((cmd path))
  8885. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8886. (string-match org-confirm-elisp-link-not-regexp cmd))
  8887. (not org-confirm-elisp-link-function)
  8888. (funcall org-confirm-elisp-link-function
  8889. (format "Execute \"%s\" as elisp? "
  8890. (org-add-props cmd nil
  8891. 'face 'org-warning))))
  8892. (message "%s => %s" cmd
  8893. (if (equal (string-to-char cmd) ?\()
  8894. (eval (read cmd))
  8895. (call-interactively (read cmd))))
  8896. (error "Abort"))))
  8897. ((and (string= type "thisfile")
  8898. (run-hook-with-args-until-success
  8899. 'org-open-link-functions path)))
  8900. ((string= type "thisfile")
  8901. (if arg
  8902. (switch-to-buffer-other-window
  8903. (org-get-buffer-for-internal-link (current-buffer)))
  8904. (org-mark-ring-push))
  8905. (let ((cmd `(org-link-search
  8906. ,path
  8907. ,(cond ((equal arg '(4)) ''occur)
  8908. ((equal arg '(16)) ''org-occur))
  8909. ,pos)))
  8910. (condition-case nil (let ((org-link-search-inhibit-query t))
  8911. (eval cmd))
  8912. (error (progn (widen) (eval cmd))))))
  8913. (t (browse-url-at-point)))))))
  8914. (move-marker org-open-link-marker nil)
  8915. (run-hook-with-args 'org-follow-link-hook)))
  8916. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  8917. "Offer links in the current entry and return the selected link.
  8918. If there is only one link, return it.
  8919. If NTH is an integer, return the NTH link found.
  8920. If ZERO is a string, check also this string for a link, and if
  8921. there is one, return it."
  8922. (with-current-buffer buffer
  8923. (save-excursion
  8924. (save-restriction
  8925. (widen)
  8926. (goto-char marker)
  8927. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8928. "\\(" org-angle-link-re "\\)\\|"
  8929. "\\(" org-plain-link-re "\\)"))
  8930. (cnt ?0)
  8931. (in-emacs (if (integerp nth) nil nth))
  8932. have-zero end links link c)
  8933. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8934. (push (match-string 0 zero) links)
  8935. (setq cnt (1- cnt) have-zero t))
  8936. (save-excursion
  8937. (org-back-to-heading t)
  8938. (setq end (save-excursion (outline-next-heading) (point)))
  8939. (while (re-search-forward re end t)
  8940. (push (match-string 0) links))
  8941. (setq links (org-uniquify (reverse links))))
  8942. (cond
  8943. ((null links)
  8944. (message "No links"))
  8945. ((equal (length links) 1)
  8946. (setq link (car links)))
  8947. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8948. (setq link (nth (if have-zero nth (1- nth)) links)))
  8949. (t ; we have to select a link
  8950. (save-excursion
  8951. (save-window-excursion
  8952. (delete-other-windows)
  8953. (with-output-to-temp-buffer "*Select Link*"
  8954. (mapc (lambda (l)
  8955. (if (not (string-match org-bracket-link-regexp l))
  8956. (princ (format "[%c] %s\n" (incf cnt)
  8957. (org-remove-angle-brackets l)))
  8958. (if (match-end 3)
  8959. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8960. (match-string 3 l) (match-string 1 l)))
  8961. (princ (format "[%c] %s\n" (incf cnt)
  8962. (match-string 1 l))))))
  8963. links))
  8964. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8965. (message "Select link to open, RET to open all:")
  8966. (setq c (read-char-exclusive))
  8967. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8968. (when (equal c ?q) (error "Abort"))
  8969. (if (equal c ?\C-m)
  8970. (setq link links)
  8971. (setq nth (- c ?0))
  8972. (if have-zero (setq nth (1+ nth)))
  8973. (unless (and (integerp nth) (>= (length links) nth))
  8974. (error "Invalid link selection"))
  8975. (setq link (nth (1- nth) links)))))
  8976. (cons link end))))))
  8977. ;; Add special file links that specify the way of opening
  8978. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8979. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8980. (defun org-open-file-with-system (path)
  8981. "Open file at PATH using the system way of opening it."
  8982. (org-open-file path 'system))
  8983. (defun org-open-file-with-emacs (path)
  8984. "Open file at PATH in Emacs."
  8985. (org-open-file path 'emacs))
  8986. (defun org-remove-file-link-modifiers ()
  8987. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8988. (goto-char (point-min))
  8989. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8990. (org-if-unprotected
  8991. (replace-match "file:" t t))))
  8992. (eval-after-load "org-exp"
  8993. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8994. 'org-remove-file-link-modifiers))
  8995. ;;; File search
  8996. (defvar org-create-file-search-functions nil
  8997. "List of functions to construct the right search string for a file link.
  8998. These functions are called in turn with point at the location to
  8999. which the link should point.
  9000. A function in the hook should first test if it would like to
  9001. handle this file type, for example by checking the `major-mode'
  9002. or the file extension. If it decides not to handle this file, it
  9003. should just return nil to give other functions a chance. If it
  9004. does handle the file, it must return the search string to be used
  9005. when following the link. The search string will be part of the
  9006. file link, given after a double colon, and `org-open-at-point'
  9007. will automatically search for it. If special measures must be
  9008. taken to make the search successful, another function should be
  9009. added to the companion hook `org-execute-file-search-functions',
  9010. which see.
  9011. A function in this hook may also use `setq' to set the variable
  9012. `description' to provide a suggestion for the descriptive text to
  9013. be used for this link when it gets inserted into an Org-mode
  9014. buffer with \\[org-insert-link].")
  9015. (defvar org-execute-file-search-functions nil
  9016. "List of functions to execute a file search triggered by a link.
  9017. Functions added to this hook must accept a single argument, the
  9018. search string that was part of the file link, the part after the
  9019. double colon. The function must first check if it would like to
  9020. handle this search, for example by checking the `major-mode' or
  9021. the file extension. If it decides not to handle this search, it
  9022. should just return nil to give other functions a chance. If it
  9023. does handle the search, it must return a non-nil value to keep
  9024. other functions from trying.
  9025. Each function can access the current prefix argument through the
  9026. variable `current-prefix-argument'. Note that a single prefix is
  9027. used to force opening a link in Emacs, so it may be good to only
  9028. use a numeric or double prefix to guide the search function.
  9029. In case this is needed, a function in this hook can also restore
  9030. the window configuration before `org-open-at-point' was called using:
  9031. (set-window-configuration org-window-config-before-follow-link)")
  9032. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9033. (defun org-link-search (s &optional type avoid-pos stealth)
  9034. "Search for a link search option.
  9035. If S is surrounded by forward slashes, it is interpreted as a
  9036. regular expression. In org-mode files, this will create an `org-occur'
  9037. sparse tree. In ordinary files, `occur' will be used to list matches.
  9038. If the current buffer is in `dired-mode', grep will be used to search
  9039. in all files. If AVOID-POS is given, ignore matches near that position.
  9040. When optional argument STEALTH is non-nil, do not modify
  9041. visibility around point, thus ignoring
  9042. `org-show-hierarchy-above', `org-show-following-heading' and
  9043. `org-show-siblings' variables."
  9044. (let ((case-fold-search t)
  9045. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9046. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9047. (append '(("") (" ") ("\t") ("\n"))
  9048. org-emphasis-alist)
  9049. "\\|") "\\)"))
  9050. (pos (point))
  9051. (pre nil) (post nil)
  9052. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9053. (cond
  9054. ;; First check if there are any special search functions
  9055. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9056. ;; Now try the builtin stuff
  9057. ((and (equal (string-to-char s0) ?#)
  9058. (> (length s0) 1)
  9059. (save-excursion
  9060. (goto-char (point-min))
  9061. (and
  9062. (re-search-forward
  9063. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9064. (setq type 'dedicated
  9065. pos (match-beginning 0))))
  9066. ;; There is an exact target for this
  9067. (goto-char pos)
  9068. (org-back-to-heading t)))
  9069. ((save-excursion
  9070. (goto-char (point-min))
  9071. (and
  9072. (re-search-forward
  9073. (concat "<<" (regexp-quote s0) ">>") nil t)
  9074. (setq type 'dedicated
  9075. pos (match-beginning 0))))
  9076. ;; There is an exact target for this
  9077. (goto-char pos))
  9078. ((save-excursion
  9079. (goto-char (point-min))
  9080. (and
  9081. (re-search-forward
  9082. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9083. (setq type 'dedicated pos (match-beginning 0))))
  9084. ;; Found an invisible target.
  9085. (goto-char pos))
  9086. ((save-excursion
  9087. (goto-char (point-min))
  9088. (and
  9089. (re-search-forward
  9090. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9091. (setq type 'dedicated pos (match-beginning 0))))
  9092. ;; Found an element with a matching #+name affiliated keyword.
  9093. (goto-char pos))
  9094. ((and (string-match "^(\\(.*\\))$" s0)
  9095. (save-excursion
  9096. (goto-char (point-min))
  9097. (and
  9098. (re-search-forward
  9099. (concat "[^[]" (regexp-quote
  9100. (format org-coderef-label-format
  9101. (match-string 1 s0))))
  9102. nil t)
  9103. (setq type 'dedicated
  9104. pos (1+ (match-beginning 0))))))
  9105. ;; There is a coderef target for this
  9106. (goto-char pos))
  9107. ((string-match "^/\\(.*\\)/$" s)
  9108. ;; A regular expression
  9109. (cond
  9110. ((derived-mode-p 'org-mode)
  9111. (org-occur (match-string 1 s)))
  9112. ;;((eq major-mode 'dired-mode)
  9113. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9114. (t (org-do-occur (match-string 1 s)))))
  9115. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9116. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9117. (goto-char (point-min))
  9118. (cond
  9119. ((let (case-fold-search)
  9120. (re-search-forward (format org-complex-heading-regexp-format
  9121. (regexp-quote s))
  9122. nil t))
  9123. ;; OK, found a match
  9124. (setq type 'dedicated)
  9125. (goto-char (match-beginning 0)))
  9126. ((and (not org-link-search-inhibit-query)
  9127. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9128. (y-or-n-p "No match - create this as a new heading? "))
  9129. (goto-char (point-max))
  9130. (or (bolp) (newline))
  9131. (insert "* " s "\n")
  9132. (beginning-of-line 0))
  9133. (t
  9134. (goto-char pos)
  9135. (error "No match"))))
  9136. (t
  9137. ;; A normal search string
  9138. (when (equal (string-to-char s) ?*)
  9139. ;; Anchor on headlines, post may include tags.
  9140. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9141. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9142. s (substring s 1)))
  9143. (remove-text-properties
  9144. 0 (length s)
  9145. '(face nil mouse-face nil keymap nil fontified nil) s)
  9146. ;; Make a series of regular expressions to find a match
  9147. (setq words (org-split-string s "[ \n\r\t]+")
  9148. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9149. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9150. "\\)" markers)
  9151. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9152. re2a (concat "[ \t\r\n]" re2a_)
  9153. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9154. re4 (concat "[^a-zA-Z_]" re4_)
  9155. re1 (concat pre re2 post)
  9156. re3 (concat pre (if pre re4_ re4) post)
  9157. re5 (concat pre ".*" re4)
  9158. re2 (concat pre re2)
  9159. re2a (concat pre (if pre re2a_ re2a))
  9160. re4 (concat pre (if pre re4_ re4))
  9161. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9162. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9163. re5 "\\)"
  9164. ))
  9165. (cond
  9166. ((eq type 'org-occur) (org-occur reall))
  9167. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9168. (t (goto-char (point-min))
  9169. (setq type 'fuzzy)
  9170. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9171. (org-search-not-self 1 re1 nil t)
  9172. (org-search-not-self 1 re2 nil t)
  9173. (org-search-not-self 1 re2a nil t)
  9174. (org-search-not-self 1 re3 nil t)
  9175. (org-search-not-self 1 re4 nil t)
  9176. (org-search-not-self 1 re5 nil t)
  9177. )
  9178. (goto-char (match-beginning 1))
  9179. (goto-char pos)
  9180. (error "No match"))))))
  9181. (and (derived-mode-p 'org-mode)
  9182. (not stealth)
  9183. (org-show-context 'link-search))
  9184. type))
  9185. (defun org-search-not-self (group &rest args)
  9186. "Execute `re-search-forward', but only accept matches that do not
  9187. enclose the position of `org-open-link-marker'."
  9188. (let ((m org-open-link-marker))
  9189. (catch 'exit
  9190. (while (apply 're-search-forward args)
  9191. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9192. (goto-char (match-end group))
  9193. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9194. (> (match-beginning 0) (marker-position m))
  9195. (< (match-end 0) (marker-position m)))
  9196. (save-match-data
  9197. (or (not (org-in-regexp
  9198. org-bracket-link-analytic-regexp 1))
  9199. (not (match-end 4)) ; no description
  9200. (and (<= (match-beginning 4) (point))
  9201. (>= (match-end 4) (point))))))
  9202. (throw 'exit (point))))))))
  9203. (defun org-get-buffer-for-internal-link (buffer)
  9204. "Return a buffer to be used for displaying the link target of internal links."
  9205. (cond
  9206. ((not org-display-internal-link-with-indirect-buffer)
  9207. buffer)
  9208. ((string-match "(Clone)$" (buffer-name buffer))
  9209. (message "Buffer is already a clone, not making another one")
  9210. ;; we also do not modify visibility in this case
  9211. buffer)
  9212. (t ; make a new indirect buffer for displaying the link
  9213. (let* ((bn (buffer-name buffer))
  9214. (ibn (concat bn "(Clone)"))
  9215. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9216. (with-current-buffer ib (org-overview))
  9217. ib))))
  9218. (defun org-do-occur (regexp &optional cleanup)
  9219. "Call the Emacs command `occur'.
  9220. If CLEANUP is non-nil, remove the printout of the regular expression
  9221. in the *Occur* buffer. This is useful if the regex is long and not useful
  9222. to read."
  9223. (occur regexp)
  9224. (when cleanup
  9225. (let ((cwin (selected-window)) win beg end)
  9226. (when (setq win (get-buffer-window "*Occur*"))
  9227. (select-window win))
  9228. (goto-char (point-min))
  9229. (when (re-search-forward "match[a-z]+" nil t)
  9230. (setq beg (match-end 0))
  9231. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9232. (setq end (1- (match-beginning 0)))))
  9233. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9234. (goto-char (point-min))
  9235. (select-window cwin))))
  9236. ;;; The mark ring for links jumps
  9237. (defvar org-mark-ring nil
  9238. "Mark ring for positions before jumps in Org-mode.")
  9239. (defvar org-mark-ring-last-goto nil
  9240. "Last position in the mark ring used to go back.")
  9241. ;; Fill and close the ring
  9242. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9243. (loop for i from 1 to org-mark-ring-length do
  9244. (push (make-marker) org-mark-ring))
  9245. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9246. org-mark-ring)
  9247. (defun org-mark-ring-push (&optional pos buffer)
  9248. "Put the current position or POS into the mark ring and rotate it."
  9249. (interactive)
  9250. (setq pos (or pos (point)))
  9251. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9252. (move-marker (car org-mark-ring)
  9253. (or pos (point))
  9254. (or buffer (current-buffer)))
  9255. (message "%s"
  9256. (substitute-command-keys
  9257. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9258. (defun org-mark-ring-goto (&optional n)
  9259. "Jump to the previous position in the mark ring.
  9260. With prefix arg N, jump back that many stored positions. When
  9261. called several times in succession, walk through the entire ring.
  9262. Org-mode commands jumping to a different position in the current file,
  9263. or to another Org-mode file, automatically push the old position
  9264. onto the ring."
  9265. (interactive "p")
  9266. (let (p m)
  9267. (if (eq last-command this-command)
  9268. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9269. (setq p org-mark-ring))
  9270. (setq org-mark-ring-last-goto p)
  9271. (setq m (car p))
  9272. (org-pop-to-buffer-same-window (marker-buffer m))
  9273. (goto-char m)
  9274. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9275. (defun org-remove-angle-brackets (s)
  9276. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9277. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9278. s)
  9279. (defun org-add-angle-brackets (s)
  9280. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9281. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9282. s)
  9283. (defun org-remove-double-quotes (s)
  9284. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9285. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9286. s)
  9287. ;;; Following specific links
  9288. (defun org-follow-timestamp-link ()
  9289. "Open an agenda view for the time-stamp date/range at point."
  9290. (cond
  9291. ((org-at-date-range-p t)
  9292. (let ((org-agenda-start-on-weekday)
  9293. (t1 (match-string 1))
  9294. (t2 (match-string 2)) tt1 tt2)
  9295. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9296. tt2 (time-to-days (org-time-string-to-time t2)))
  9297. (let ((org-agenda-buffer-tmp-name
  9298. (format "*Org Agenda(a:%s)"
  9299. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9300. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9301. ((org-at-timestamp-p t)
  9302. (let ((org-agenda-buffer-tmp-name
  9303. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9304. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9305. (substring (match-string 1) 0 10)))
  9306. 1)))
  9307. (t (error "This should not happen"))))
  9308. ;;; Following file links
  9309. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9310. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9311. (declare-function mailcap-mime-info
  9312. "mailcap" (string &optional request no-decode))
  9313. (defvar org-wait nil)
  9314. (defun org-open-file (path &optional in-emacs line search)
  9315. "Open the file at PATH.
  9316. First, this expands any special file name abbreviations. Then the
  9317. configuration variable `org-file-apps' is checked if it contains an
  9318. entry for this file type, and if yes, the corresponding command is launched.
  9319. If no application is found, Emacs simply visits the file.
  9320. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9321. With a double \\[universal-argument] \\[universal-argument] \
  9322. prefix arg, Org tries to avoid opening in Emacs
  9323. and to use an external application to visit the file.
  9324. Optional LINE specifies a line to go to, optional SEARCH a string
  9325. to search for. If LINE or SEARCH is given, the file will be
  9326. opened in Emacs, unless an entry from org-file-apps that makes
  9327. use of groups in a regexp matches.
  9328. If you want to change the way frames are used when following a
  9329. link, please customize `org-link-frame-setup'.
  9330. If the file does not exist, an error is thrown."
  9331. (let* ((file (if (equal path "")
  9332. buffer-file-name
  9333. (substitute-in-file-name (expand-file-name path))))
  9334. (file-apps (append org-file-apps (org-default-apps)))
  9335. (apps (org-remove-if
  9336. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9337. (apps-dlink (org-remove-if-not
  9338. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9339. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9340. (dirp (if remp nil (file-directory-p file)))
  9341. (file (if (and dirp org-open-directory-means-index-dot-org)
  9342. (concat (file-name-as-directory file) "index.org")
  9343. file))
  9344. (a-m-a-p (assq 'auto-mode apps))
  9345. (dfile (downcase file))
  9346. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9347. (link (cond ((and (eq line nil)
  9348. (eq search nil))
  9349. file)
  9350. (line
  9351. (concat file "::" (number-to-string line)))
  9352. (search
  9353. (concat file "::" search))))
  9354. (dlink (downcase link))
  9355. (old-buffer (current-buffer))
  9356. (old-pos (point))
  9357. (old-mode major-mode)
  9358. ext cmd link-match-data)
  9359. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9360. (setq ext (match-string 1 dfile))
  9361. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9362. (setq ext (match-string 1 dfile))))
  9363. (cond
  9364. ((member in-emacs '((16) system))
  9365. (setq cmd (cdr (assoc 'system apps))))
  9366. (in-emacs (setq cmd 'emacs))
  9367. (t
  9368. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9369. (and dirp (cdr (assoc 'directory apps)))
  9370. ; first, try matching against apps-dlink
  9371. ; if we get a match here, store the match data for later
  9372. (let ((match (assoc-default dlink apps-dlink
  9373. 'string-match)))
  9374. (if match
  9375. (progn (setq link-match-data (match-data))
  9376. match)
  9377. (progn (setq in-emacs (or in-emacs line search))
  9378. nil))) ; if we have no match in apps-dlink,
  9379. ; always open the file in emacs if line or search
  9380. ; is given (for backwards compatibility)
  9381. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9382. 'string-match)
  9383. (cdr (assoc ext apps))
  9384. (cdr (assoc t apps))))))
  9385. (when (eq cmd 'system)
  9386. (setq cmd (cdr (assoc 'system apps))))
  9387. (when (eq cmd 'default)
  9388. (setq cmd (cdr (assoc t apps))))
  9389. (when (eq cmd 'mailcap)
  9390. (require 'mailcap)
  9391. (mailcap-parse-mailcaps)
  9392. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9393. (command (mailcap-mime-info mime-type)))
  9394. (if (stringp command)
  9395. (setq cmd command)
  9396. (setq cmd 'emacs))))
  9397. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9398. (not (file-exists-p file))
  9399. (not org-open-non-existing-files))
  9400. (error "No such file: %s" file))
  9401. (cond
  9402. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9403. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9404. (while (string-match "['\"]%s['\"]" cmd)
  9405. (setq cmd (replace-match "%s" t t cmd)))
  9406. (while (string-match "%s" cmd)
  9407. (setq cmd (replace-match
  9408. (save-match-data
  9409. (shell-quote-argument
  9410. (convert-standard-filename file)))
  9411. t t cmd)))
  9412. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9413. (save-match-data
  9414. (let ((match-index 1)
  9415. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9416. (set-match-data link-match-data)
  9417. (while (<= match-index number-of-groups)
  9418. (let ((regex (concat "%" (number-to-string match-index)))
  9419. (replace-with (match-string match-index dlink)))
  9420. (while (string-match regex cmd)
  9421. (setq cmd (replace-match replace-with t t cmd))))
  9422. (setq match-index (+ match-index 1)))))
  9423. (save-window-excursion
  9424. (start-process-shell-command cmd nil cmd)
  9425. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9426. ))
  9427. ((or (stringp cmd)
  9428. (eq cmd 'emacs))
  9429. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9430. (widen)
  9431. (if line (org-goto-line line)
  9432. (if search (org-link-search search))))
  9433. ((consp cmd)
  9434. (let ((file (convert-standard-filename file)))
  9435. (save-match-data
  9436. (set-match-data link-match-data)
  9437. (eval cmd))))
  9438. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9439. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9440. (or (not (equal old-buffer (current-buffer)))
  9441. (not (equal old-pos (point))))
  9442. (org-mark-ring-push old-pos old-buffer))))
  9443. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9444. "This function returns non-nil if `entry' uses a regular
  9445. expression which should be matched against the whole link by
  9446. org-open-file.
  9447. It assumes that is the case when the entry uses a regular
  9448. expression which has at least one grouping construct and the
  9449. action is either a lisp form or a command string containing
  9450. '%1', i.e. using at least one subexpression match as a
  9451. parameter."
  9452. (let ((selector (car entry))
  9453. (action (cdr entry)))
  9454. (if (stringp selector)
  9455. (and (> (regexp-opt-depth selector) 0)
  9456. (or (and (stringp action)
  9457. (string-match "%[0-9]" action))
  9458. (consp action)))
  9459. nil)))
  9460. (defun org-default-apps ()
  9461. "Return the default applications for this operating system."
  9462. (cond
  9463. ((eq system-type 'darwin)
  9464. org-file-apps-defaults-macosx)
  9465. ((eq system-type 'windows-nt)
  9466. org-file-apps-defaults-windowsnt)
  9467. (t org-file-apps-defaults-gnu)))
  9468. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9469. "Convert extensions to regular expressions in the cars of LIST.
  9470. Also, weed out any non-string entries, because the return value is used
  9471. only for regexp matching.
  9472. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9473. point to the symbol `emacs', indicating that the file should
  9474. be opened in Emacs."
  9475. (append
  9476. (delq nil
  9477. (mapcar (lambda (x)
  9478. (if (not (stringp (car x)))
  9479. nil
  9480. (if (string-match "\\W" (car x))
  9481. x
  9482. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9483. list))
  9484. (if add-auto-mode
  9485. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9486. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9487. (defun org-file-remote-p (file)
  9488. "Test whether FILE specifies a location on a remote system.
  9489. Return non-nil if the location is indeed remote.
  9490. For example, the filename \"/user@host:/foo\" specifies a location
  9491. on the system \"/user@host:\"."
  9492. (cond ((fboundp 'file-remote-p)
  9493. (file-remote-p file))
  9494. ((fboundp 'tramp-handle-file-remote-p)
  9495. (tramp-handle-file-remote-p file))
  9496. ((and (boundp 'ange-ftp-name-format)
  9497. (string-match (car ange-ftp-name-format) file))
  9498. t)))
  9499. ;;;; Refiling
  9500. (defun org-get-org-file ()
  9501. "Read a filename, with default directory `org-directory'."
  9502. (let ((default (or org-default-notes-file remember-data-file)))
  9503. (read-file-name (format "File name [%s]: " default)
  9504. (file-name-as-directory org-directory)
  9505. default)))
  9506. (defun org-notes-order-reversed-p ()
  9507. "Check if the current file should receive notes in reversed order."
  9508. (cond
  9509. ((not org-reverse-note-order) nil)
  9510. ((eq t org-reverse-note-order) t)
  9511. ((not (listp org-reverse-note-order)) nil)
  9512. (t (catch 'exit
  9513. (let ((all org-reverse-note-order)
  9514. entry)
  9515. (while (setq entry (pop all))
  9516. (if (string-match (car entry) buffer-file-name)
  9517. (throw 'exit (cdr entry))))
  9518. nil)))))
  9519. (defvar org-refile-target-table nil
  9520. "The list of refile targets, created by `org-refile'.")
  9521. (defvar org-agenda-new-buffers nil
  9522. "Buffers created to visit agenda files.")
  9523. (defvar org-refile-cache nil
  9524. "Cache for refile targets.")
  9525. (defvar org-refile-markers nil
  9526. "All the markers used for caching refile locations.")
  9527. (defun org-refile-marker (pos)
  9528. "Get a new refile marker, but only if caching is in use."
  9529. (if (not org-refile-use-cache)
  9530. pos
  9531. (let ((m (make-marker)))
  9532. (move-marker m pos)
  9533. (push m org-refile-markers)
  9534. m)))
  9535. (defun org-refile-cache-clear ()
  9536. "Clear the refile cache and disable all the markers."
  9537. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9538. (setq org-refile-markers nil)
  9539. (setq org-refile-cache nil)
  9540. (message "Refile cache has been cleared"))
  9541. (defun org-refile-cache-check-set (set)
  9542. "Check if all the markers in the cache still have live buffers."
  9543. (let (marker)
  9544. (catch 'exit
  9545. (while (and set (setq marker (nth 3 (pop set))))
  9546. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9547. (when (and marker (null (marker-buffer marker)))
  9548. (message "not found") (sit-for 3)
  9549. (throw 'exit nil)))
  9550. t)))
  9551. (defun org-refile-cache-put (set &rest identifiers)
  9552. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9553. (let* ((key (sha1 (prin1-to-string identifiers)))
  9554. (entry (assoc key org-refile-cache)))
  9555. (if entry
  9556. (setcdr entry set)
  9557. (push (cons key set) org-refile-cache))))
  9558. (defun org-refile-cache-get (&rest identifiers)
  9559. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9560. (cond
  9561. ((not org-refile-cache) nil)
  9562. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9563. (t
  9564. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9565. org-refile-cache))))
  9566. (and set (org-refile-cache-check-set set) set)))))
  9567. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9568. "Produce a table with refile targets."
  9569. (let ((case-fold-search nil)
  9570. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9571. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9572. targets tgs txt re files f desc descre fast-path-p level pos0)
  9573. (message "Getting targets...")
  9574. (with-current-buffer (or default-buffer (current-buffer))
  9575. (while (setq entry (pop entries))
  9576. (setq files (car entry) desc (cdr entry))
  9577. (setq fast-path-p nil)
  9578. (cond
  9579. ((null files) (setq files (list (current-buffer))))
  9580. ((eq files 'org-agenda-files)
  9581. (setq files (org-agenda-files 'unrestricted)))
  9582. ((and (symbolp files) (fboundp files))
  9583. (setq files (funcall files)))
  9584. ((and (symbolp files) (boundp files))
  9585. (setq files (symbol-value files))))
  9586. (if (stringp files) (setq files (list files)))
  9587. (cond
  9588. ((eq (car desc) :tag)
  9589. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9590. ((eq (car desc) :todo)
  9591. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9592. ((eq (car desc) :regexp)
  9593. (setq descre (cdr desc)))
  9594. ((eq (car desc) :level)
  9595. (setq descre (concat "^\\*\\{" (number-to-string
  9596. (if org-odd-levels-only
  9597. (1- (* 2 (cdr desc)))
  9598. (cdr desc)))
  9599. "\\}[ \t]")))
  9600. ((eq (car desc) :maxlevel)
  9601. (setq fast-path-p t)
  9602. (setq descre (concat "^\\*\\{1," (number-to-string
  9603. (if org-odd-levels-only
  9604. (1- (* 2 (cdr desc)))
  9605. (cdr desc)))
  9606. "\\}[ \t]")))
  9607. (t (error "Bad refiling target description %s" desc)))
  9608. (while (setq f (pop files))
  9609. (with-current-buffer
  9610. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9611. (or
  9612. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9613. (progn
  9614. (if (bufferp f) (setq f (buffer-file-name
  9615. (buffer-base-buffer f))))
  9616. (setq f (and f (expand-file-name f)))
  9617. (if (eq org-refile-use-outline-path 'file)
  9618. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9619. (save-excursion
  9620. (save-restriction
  9621. (widen)
  9622. (goto-char (point-min))
  9623. (while (re-search-forward descre nil t)
  9624. (goto-char (setq pos0 (point-at-bol)))
  9625. (catch 'next
  9626. (when org-refile-target-verify-function
  9627. (save-match-data
  9628. (or (funcall org-refile-target-verify-function)
  9629. (throw 'next t))))
  9630. (when (and (looking-at org-complex-heading-regexp)
  9631. (not (member (match-string 4) excluded-entries))
  9632. (match-string 4))
  9633. (setq level (org-reduced-level
  9634. (- (match-end 1) (match-beginning 1)))
  9635. txt (org-link-display-format (match-string 4))
  9636. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9637. re (format org-complex-heading-regexp-format
  9638. (regexp-quote (match-string 4))))
  9639. (when org-refile-use-outline-path
  9640. (setq txt (mapconcat
  9641. 'org-protect-slash
  9642. (append
  9643. (if (eq org-refile-use-outline-path
  9644. 'file)
  9645. (list (file-name-nondirectory
  9646. (buffer-file-name
  9647. (buffer-base-buffer))))
  9648. (if (eq org-refile-use-outline-path
  9649. 'full-file-path)
  9650. (list (buffer-file-name
  9651. (buffer-base-buffer)))))
  9652. (org-get-outline-path fast-path-p
  9653. level txt)
  9654. (list txt))
  9655. "/")))
  9656. (push (list txt f re (org-refile-marker (point)))
  9657. tgs)))
  9658. (when (= (point) pos0)
  9659. ;; verification function has not moved point
  9660. (goto-char (point-at-eol))))))))
  9661. (when org-refile-use-cache
  9662. (org-refile-cache-put tgs (buffer-file-name) descre))
  9663. (setq targets (append tgs targets))
  9664. ))))
  9665. (message "Getting targets...done")
  9666. (nreverse targets)))
  9667. (defun org-protect-slash (s)
  9668. (while (string-match "/" s)
  9669. (setq s (replace-match "\\" t t s)))
  9670. s)
  9671. (defvar org-olpa (make-vector 20 nil))
  9672. (defun org-get-outline-path (&optional fastp level heading)
  9673. "Return the outline path to the current entry, as a list.
  9674. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9675. routine which makes outline path derivations for an entire file,
  9676. avoiding backtracing. Refile target collection makes use of that."
  9677. (if fastp
  9678. (progn
  9679. (if (> level 19)
  9680. (error "Outline path failure, more than 19 levels"))
  9681. (loop for i from level upto 19 do
  9682. (aset org-olpa i nil))
  9683. (prog1
  9684. (delq nil (append org-olpa nil))
  9685. (aset org-olpa level heading)))
  9686. (let (rtn case-fold-search)
  9687. (save-excursion
  9688. (save-restriction
  9689. (widen)
  9690. (while (org-up-heading-safe)
  9691. (when (looking-at org-complex-heading-regexp)
  9692. (push (org-match-string-no-properties 4) rtn)))
  9693. rtn)))))
  9694. (defun org-format-outline-path (path &optional width prefix)
  9695. "Format the outline path PATH for display.
  9696. Width is the maximum number of characters that is available.
  9697. Prefix is a prefix to be included in the returned string,
  9698. such as the file name."
  9699. (setq width (or width 79))
  9700. (if prefix (setq width (- width (length prefix))))
  9701. (if (not path)
  9702. (or prefix "")
  9703. (let* ((nsteps (length path))
  9704. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9705. (maxwidth (if (<= total-width width)
  9706. 10000 ;; everything fits
  9707. ;; we need to shorten the level headings
  9708. (/ (- width nsteps) nsteps)))
  9709. (org-odd-levels-only nil)
  9710. (n 0)
  9711. (total (1+ (length prefix))))
  9712. (setq maxwidth (max maxwidth 10))
  9713. (concat prefix
  9714. (mapconcat
  9715. (lambda (h)
  9716. (setq n (1+ n))
  9717. (if (and (= n nsteps) (< maxwidth 10000))
  9718. (setq maxwidth (- total-width total)))
  9719. (if (< (length h) maxwidth)
  9720. (progn (setq total (+ total (length h) 1)) h)
  9721. (setq h (substring h 0 (- maxwidth 2))
  9722. total (+ total maxwidth 1))
  9723. (if (string-match "[ \t]+\\'" h)
  9724. (setq h (substring h 0 (match-beginning 0))))
  9725. (setq h (concat h "..")))
  9726. (org-add-props h nil 'face
  9727. (nth (% (1- n) org-n-level-faces)
  9728. org-level-faces))
  9729. h)
  9730. path "/")))))
  9731. (defun org-display-outline-path (&optional file current)
  9732. "Display the current outline path in the echo area."
  9733. (interactive "P")
  9734. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9735. (case-fold-search nil)
  9736. (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
  9737. (if current (setq path (append path
  9738. (save-excursion
  9739. (org-back-to-heading t)
  9740. (if (looking-at org-complex-heading-regexp)
  9741. (list (match-string 4)))))))
  9742. (message "%s"
  9743. (org-format-outline-path
  9744. path
  9745. (1- (frame-width))
  9746. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9747. (defvar org-refile-history nil
  9748. "History for refiling operations.")
  9749. (defvar org-after-refile-insert-hook nil
  9750. "Hook run after `org-refile' has inserted its stuff at the new location.
  9751. Note that this is still *before* the stuff will be removed from
  9752. the *old* location.")
  9753. (defvar org-capture-last-stored-marker)
  9754. (defun org-refile (&optional goto default-buffer rfloc)
  9755. "Move the entry or entries at point to another heading.
  9756. The list of target headings is compiled using the information in
  9757. `org-refile-targets', which see.
  9758. At the target location, the entry is filed as a subitem of the target
  9759. heading. Depending on `org-reverse-note-order', the new subitem will
  9760. either be the first or the last subitem.
  9761. If there is an active region, all entries in that region will be moved.
  9762. However, the region must fulfill the requirement that the first heading
  9763. is the first one sets the top-level of the moved text - at most siblings
  9764. below it are allowed.
  9765. With prefix arg GOTO, the command will only visit the target location
  9766. and not actually move anything.
  9767. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9768. go to the location where the last refiling operation has put the subtree.
  9769. With a prefix argument of `2', refile to the running clock.
  9770. RFLOC can be a refile location obtained in a different way.
  9771. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9772. If you are using target caching (see `org-refile-use-cache'),
  9773. you have to clear the target cache in order to find new targets.
  9774. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9775. prefix argument (`C-u C-u C-u C-c C-w')."
  9776. (interactive "P")
  9777. (if (member goto '(0 (64)))
  9778. (org-refile-cache-clear)
  9779. (let* ((cbuf (current-buffer))
  9780. (regionp (org-region-active-p))
  9781. (region-start (and regionp (region-beginning)))
  9782. (region-end (and regionp (region-end)))
  9783. (region-length (and regionp (- region-end region-start)))
  9784. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9785. pos it nbuf file re level reversed)
  9786. (setq last-command nil)
  9787. (when regionp
  9788. (goto-char region-start)
  9789. (or (bolp) (goto-char (point-at-bol)))
  9790. (setq region-start (point))
  9791. (unless (or (org-kill-is-subtree-p
  9792. (buffer-substring region-start region-end))
  9793. (prog1 org-refile-active-region-within-subtree
  9794. (org-toggle-heading)))
  9795. (error "The region is not a (sequence of) subtree(s)")))
  9796. (if (equal goto '(16))
  9797. (org-refile-goto-last-stored)
  9798. (when (or
  9799. (and (equal goto 2)
  9800. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9801. (prog1
  9802. (setq it (list (or org-clock-heading "running clock")
  9803. (buffer-file-name
  9804. (marker-buffer org-clock-hd-marker))
  9805. ""
  9806. (marker-position org-clock-hd-marker)))
  9807. (setq goto nil)))
  9808. (setq it (or rfloc
  9809. (let (heading-text)
  9810. (save-excursion
  9811. (unless goto
  9812. (org-back-to-heading t)
  9813. (setq heading-text
  9814. (nth 4 (org-heading-components))))
  9815. (org-refile-get-location
  9816. (cond (goto "Goto")
  9817. (regionp "Refile region to")
  9818. (t (concat "Refile subtree \""
  9819. heading-text "\" to")))
  9820. default-buffer
  9821. (and (not (equal '(4) goto))
  9822. org-refile-allow-creating-parent-nodes)
  9823. goto))))))
  9824. (setq file (nth 1 it)
  9825. re (nth 2 it)
  9826. pos (nth 3 it))
  9827. (if (and (not goto)
  9828. pos
  9829. (equal (buffer-file-name) file)
  9830. (if regionp
  9831. (and (>= pos region-start)
  9832. (<= pos region-end))
  9833. (and (>= pos (point))
  9834. (< pos (save-excursion
  9835. (org-end-of-subtree t t))))))
  9836. (error "Cannot refile to position inside the tree or region"))
  9837. (setq nbuf (or (find-buffer-visiting file)
  9838. (find-file-noselect file)))
  9839. (if goto
  9840. (progn
  9841. (org-pop-to-buffer-same-window nbuf)
  9842. (goto-char pos)
  9843. (org-show-context 'org-goto))
  9844. (if regionp
  9845. (progn
  9846. (org-kill-new (buffer-substring region-start region-end))
  9847. (org-save-markers-in-region region-start region-end))
  9848. (org-copy-subtree 1 nil t))
  9849. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9850. (find-file-noselect file)))
  9851. (setq reversed (org-notes-order-reversed-p))
  9852. (save-excursion
  9853. (save-restriction
  9854. (widen)
  9855. (if pos
  9856. (progn
  9857. (goto-char pos)
  9858. (looking-at org-outline-regexp)
  9859. (setq level (org-get-valid-level (funcall outline-level) 1))
  9860. (goto-char
  9861. (if reversed
  9862. (or (outline-next-heading) (point-max))
  9863. (or (save-excursion (org-get-next-sibling))
  9864. (org-end-of-subtree t t)
  9865. (point-max)))))
  9866. (setq level 1)
  9867. (if (not reversed)
  9868. (goto-char (point-max))
  9869. (goto-char (point-min))
  9870. (or (outline-next-heading) (goto-char (point-max)))))
  9871. (if (not (bolp)) (newline))
  9872. (org-paste-subtree level)
  9873. (when org-log-refile
  9874. (org-add-log-setup 'refile nil nil 'findpos
  9875. org-log-refile)
  9876. (unless (eq org-log-refile 'note)
  9877. (save-excursion (org-add-log-note))))
  9878. (and org-auto-align-tags
  9879. (let ((org-loop-over-headlines-in-active-region nil))
  9880. (org-set-tags nil t)))
  9881. (with-demoted-errors
  9882. (bookmark-set "org-refile-last-stored"))
  9883. ;; If we are refiling for capture, make sure that the
  9884. ;; last-capture pointers point here
  9885. (when (org-bound-and-true-p org-refile-for-capture)
  9886. (with-demoted-errors
  9887. (bookmark-set "org-capture-last-stored-marker"))
  9888. (move-marker org-capture-last-stored-marker (point)))
  9889. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9890. (run-hooks 'org-after-refile-insert-hook))))
  9891. (if regionp
  9892. (delete-region (point) (+ (point) region-length))
  9893. (org-cut-subtree))
  9894. (when (featurep 'org-inlinetask)
  9895. (org-inlinetask-remove-END-maybe))
  9896. (setq org-markers-to-move nil)
  9897. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9898. (defun org-refile-goto-last-stored ()
  9899. "Go to the location where the last refile was stored."
  9900. (interactive)
  9901. (bookmark-jump "org-refile-last-stored")
  9902. (message "This is the location of the last refile"))
  9903. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9904. no-exclude)
  9905. "Prompt the user for a refile location, using PROMPT.
  9906. PROMPT should not be suffixed with a colon and a space, because
  9907. this function appends the default value from
  9908. `org-refile-history' automatically, if that is not empty.
  9909. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9910. this is used for the GOTO interface."
  9911. (let ((org-refile-targets org-refile-targets)
  9912. (org-refile-use-outline-path org-refile-use-outline-path)
  9913. excluded-entries)
  9914. (when (and (derived-mode-p 'org-mode)
  9915. (not org-refile-use-cache)
  9916. (not no-exclude))
  9917. (org-map-tree
  9918. (lambda()
  9919. (setq excluded-entries
  9920. (append excluded-entries (list (org-get-heading t t)))))))
  9921. (setq org-refile-target-table
  9922. (org-refile-get-targets default-buffer excluded-entries)))
  9923. (unless org-refile-target-table
  9924. (error "No refile targets"))
  9925. (let* ((prompt (concat prompt
  9926. (and (car org-refile-history)
  9927. (concat " (default " (car org-refile-history) ")"))
  9928. ": "))
  9929. (cbuf (current-buffer))
  9930. (partial-completion-mode nil)
  9931. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9932. (cfunc (if (and org-refile-use-outline-path
  9933. org-outline-path-complete-in-steps)
  9934. 'org-olpath-completing-read
  9935. 'org-icompleting-read))
  9936. (extra (if org-refile-use-outline-path "/" ""))
  9937. (filename (and cfn (expand-file-name cfn)))
  9938. (tbl (mapcar
  9939. (lambda (x)
  9940. (if (and (not (member org-refile-use-outline-path
  9941. '(file full-file-path)))
  9942. (not (equal filename (nth 1 x))))
  9943. (cons (concat (car x) extra " ("
  9944. (file-name-nondirectory (nth 1 x)) ")")
  9945. (cdr x))
  9946. (cons (concat (car x) extra) (cdr x))))
  9947. org-refile-target-table))
  9948. (completion-ignore-case t)
  9949. pa answ parent-target child parent old-hist)
  9950. (setq old-hist org-refile-history)
  9951. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9952. nil 'org-refile-history (car org-refile-history)))
  9953. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9954. (org-refile-check-position pa)
  9955. (if pa
  9956. (progn
  9957. (when (or (not org-refile-history)
  9958. (not (eq old-hist org-refile-history))
  9959. (not (equal (car pa) (car org-refile-history))))
  9960. (setq org-refile-history
  9961. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9962. org-refile-history
  9963. (cdr org-refile-history))))
  9964. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9965. (pop org-refile-history)))
  9966. pa)
  9967. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9968. (progn
  9969. (setq parent (match-string 1 answ)
  9970. child (match-string 2 answ))
  9971. (setq parent-target (or (assoc parent tbl)
  9972. (assoc (concat parent "/") tbl)))
  9973. (when (and parent-target
  9974. (or (eq new-nodes t)
  9975. (and (eq new-nodes 'confirm)
  9976. (y-or-n-p (format "Create new node \"%s\"? "
  9977. child)))))
  9978. (org-refile-new-child parent-target child)))
  9979. (error "Invalid target location")))))
  9980. (declare-function org-string-nw-p "org-macs" (s))
  9981. (defun org-refile-check-position (refile-pointer)
  9982. "Check if the refile pointer matches the headline to which it points."
  9983. (let* ((file (nth 1 refile-pointer))
  9984. (re (nth 2 refile-pointer))
  9985. (pos (nth 3 refile-pointer))
  9986. buffer)
  9987. (if (and (not (markerp pos)) (not file))
  9988. (error "Please save the buffer to a file before refiling")
  9989. (when (org-string-nw-p re)
  9990. (setq buffer (if (markerp pos)
  9991. (marker-buffer pos)
  9992. (or (find-buffer-visiting file)
  9993. (find-file-noselect file))))
  9994. (with-current-buffer buffer
  9995. (save-excursion
  9996. (save-restriction
  9997. (widen)
  9998. (goto-char pos)
  9999. (beginning-of-line 1)
  10000. (unless (org-looking-at-p re)
  10001. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10002. (defun org-refile-new-child (parent-target child)
  10003. "Use refile target PARENT-TARGET to add new CHILD below it."
  10004. (unless parent-target
  10005. (error "Cannot find parent for new node"))
  10006. (let ((file (nth 1 parent-target))
  10007. (pos (nth 3 parent-target))
  10008. level)
  10009. (with-current-buffer (or (find-buffer-visiting file)
  10010. (find-file-noselect file))
  10011. (save-excursion
  10012. (save-restriction
  10013. (widen)
  10014. (if pos
  10015. (goto-char pos)
  10016. (goto-char (point-max))
  10017. (if (not (bolp)) (newline)))
  10018. (when (looking-at org-outline-regexp)
  10019. (setq level (funcall outline-level))
  10020. (org-end-of-subtree t t))
  10021. (org-back-over-empty-lines)
  10022. (insert "\n" (make-string
  10023. (if pos (org-get-valid-level level 1) 1) ?*)
  10024. " " child "\n")
  10025. (beginning-of-line 0)
  10026. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10027. (defun org-olpath-completing-read (prompt collection &rest args)
  10028. "Read an outline path like a file name."
  10029. (let ((thetable collection)
  10030. (org-completion-use-ido nil) ; does not work with ido.
  10031. (org-completion-use-iswitchb nil)) ; or iswitchb
  10032. (apply
  10033. 'org-icompleting-read prompt
  10034. (lambda (string predicate &optional flag)
  10035. (let (rtn r f (l (length string)))
  10036. (cond
  10037. ((eq flag nil)
  10038. ;; try completion
  10039. (try-completion string thetable))
  10040. ((eq flag t)
  10041. ;; all-completions
  10042. (setq rtn (all-completions string thetable predicate))
  10043. (mapcar
  10044. (lambda (x)
  10045. (setq r (substring x l))
  10046. (if (string-match " ([^)]*)$" x)
  10047. (setq f (match-string 0 x))
  10048. (setq f ""))
  10049. (if (string-match "/" r)
  10050. (concat string (substring r 0 (match-end 0)) f)
  10051. x))
  10052. rtn))
  10053. ((eq flag 'lambda)
  10054. ;; exact match?
  10055. (assoc string thetable)))))
  10056. args)))
  10057. ;;;; Dynamic blocks
  10058. (defun org-find-dblock (name)
  10059. "Find the first dynamic block with name NAME in the buffer.
  10060. If not found, stay at current position and return nil."
  10061. (let ((case-fold-search t) pos)
  10062. (save-excursion
  10063. (goto-char (point-min))
  10064. (setq pos (and (re-search-forward
  10065. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10066. (match-beginning 0))))
  10067. (if pos (goto-char pos))
  10068. pos))
  10069. (defconst org-dblock-start-re
  10070. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10071. "Matches the start line of a dynamic block, with parameters.")
  10072. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10073. "Matches the end of a dynamic block.")
  10074. (defun org-create-dblock (plist)
  10075. "Create a dynamic block section, with parameters taken from PLIST.
  10076. PLIST must contain a :name entry which is used as name of the block."
  10077. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10078. (end-of-line 1)
  10079. (newline))
  10080. (let ((col (current-column))
  10081. (name (plist-get plist :name)))
  10082. (insert "#+BEGIN: " name)
  10083. (while plist
  10084. (if (eq (car plist) :name)
  10085. (setq plist (cddr plist))
  10086. (insert " " (prin1-to-string (pop plist)))))
  10087. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10088. (beginning-of-line -2)))
  10089. (defun org-prepare-dblock ()
  10090. "Prepare dynamic block for refresh.
  10091. This empties the block, puts the cursor at the insert position and returns
  10092. the property list including an extra property :name with the block name."
  10093. (unless (looking-at org-dblock-start-re)
  10094. (error "Not at a dynamic block"))
  10095. (let* ((begdel (1+ (match-end 0)))
  10096. (name (org-no-properties (match-string 1)))
  10097. (params (append (list :name name)
  10098. (read (concat "(" (match-string 3) ")")))))
  10099. (save-excursion
  10100. (beginning-of-line 1)
  10101. (skip-chars-forward " \t")
  10102. (setq params (plist-put params :indentation-column (current-column))))
  10103. (unless (re-search-forward org-dblock-end-re nil t)
  10104. (error "Dynamic block not terminated"))
  10105. (setq params
  10106. (append params
  10107. (list :content (buffer-substring
  10108. begdel (match-beginning 0)))))
  10109. (delete-region begdel (match-beginning 0))
  10110. (goto-char begdel)
  10111. (open-line 1)
  10112. params))
  10113. (defun org-map-dblocks (&optional command)
  10114. "Apply COMMAND to all dynamic blocks in the current buffer.
  10115. If COMMAND is not given, use `org-update-dblock'."
  10116. (let ((cmd (or command 'org-update-dblock)))
  10117. (save-excursion
  10118. (goto-char (point-min))
  10119. (while (re-search-forward org-dblock-start-re nil t)
  10120. (goto-char (match-beginning 0))
  10121. (save-excursion
  10122. (condition-case nil
  10123. (funcall cmd)
  10124. (error (message "Error during update of dynamic block"))))
  10125. (unless (re-search-forward org-dblock-end-re nil t)
  10126. (error "Dynamic block not terminated"))))))
  10127. (defun org-dblock-update (&optional arg)
  10128. "User command for updating dynamic blocks.
  10129. Update the dynamic block at point. With prefix ARG, update all dynamic
  10130. blocks in the buffer."
  10131. (interactive "P")
  10132. (if arg
  10133. (org-update-all-dblocks)
  10134. (or (looking-at org-dblock-start-re)
  10135. (org-beginning-of-dblock))
  10136. (org-update-dblock)))
  10137. (defun org-update-dblock ()
  10138. "Update the dynamic block at point.
  10139. This means to empty the block, parse for parameters and then call
  10140. the correct writing function."
  10141. (interactive)
  10142. (save-window-excursion
  10143. (let* ((pos (point))
  10144. (line (org-current-line))
  10145. (params (org-prepare-dblock))
  10146. (name (plist-get params :name))
  10147. (indent (plist-get params :indentation-column))
  10148. (cmd (intern (concat "org-dblock-write:" name))))
  10149. (message "Updating dynamic block `%s' at line %d..." name line)
  10150. (funcall cmd params)
  10151. (message "Updating dynamic block `%s' at line %d...done" name line)
  10152. (goto-char pos)
  10153. (when (and indent (> indent 0))
  10154. (setq indent (make-string indent ?\ ))
  10155. (save-excursion
  10156. (org-beginning-of-dblock)
  10157. (forward-line 1)
  10158. (while (not (looking-at org-dblock-end-re))
  10159. (insert indent)
  10160. (beginning-of-line 2))
  10161. (when (looking-at org-dblock-end-re)
  10162. (and (looking-at "[ \t]+")
  10163. (replace-match ""))
  10164. (insert indent)))))))
  10165. (defun org-beginning-of-dblock ()
  10166. "Find the beginning of the dynamic block at point.
  10167. Error if there is no such block at point."
  10168. (let ((pos (point))
  10169. beg)
  10170. (end-of-line 1)
  10171. (if (and (re-search-backward org-dblock-start-re nil t)
  10172. (setq beg (match-beginning 0))
  10173. (re-search-forward org-dblock-end-re nil t)
  10174. (> (match-end 0) pos))
  10175. (goto-char beg)
  10176. (goto-char pos)
  10177. (error "Not in a dynamic block"))))
  10178. (defun org-update-all-dblocks ()
  10179. "Update all dynamic blocks in the buffer.
  10180. This function can be used in a hook."
  10181. (interactive)
  10182. (when (derived-mode-p 'org-mode)
  10183. (org-map-dblocks 'org-update-dblock)))
  10184. ;;;; Completion
  10185. (defconst org-additional-option-like-keywords
  10186. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10187. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10188. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10189. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10190. "BEGIN:" "END:"
  10191. "ORGTBL" "TBLFM:" "TBLNAME:"
  10192. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10193. "BEGIN_VERBATIM" "END_VERBATIM"
  10194. "BEGIN_QUOTE" "END_QUOTE"
  10195. "BEGIN_VERSE" "END_VERSE"
  10196. "BEGIN_CENTER" "END_CENTER"
  10197. "BEGIN_SRC" "END_SRC"
  10198. "BEGIN_RESULT" "END_RESULT"
  10199. "BEGIN_lstlisting" "END_lstlisting"
  10200. "NAME:" "RESULTS:"
  10201. "HEADER:" "HEADERS:"
  10202. "COLUMNS:" "PROPERTY:"
  10203. "CAPTION:" "LABEL:"
  10204. "SETUPFILE:"
  10205. "INCLUDE:" "INDEX:"
  10206. "BIND:"
  10207. "MACRO:"))
  10208. (defconst org-options-keywords
  10209. '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
  10210. "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
  10211. "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
  10212. "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
  10213. "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
  10214. "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
  10215. "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
  10216. (defconst org-additional-option-like-keywords-for-flyspell
  10217. (delete-dups
  10218. (split-string
  10219. (mapconcat (lambda(k)
  10220. (replace-regexp-in-string
  10221. "_\\|:" " "
  10222. (concat k " " (downcase k) " " (upcase k))))
  10223. (append org-options-keywords org-additional-option-like-keywords)
  10224. " ")
  10225. " +" t)))
  10226. (defcustom org-structure-template-alist
  10227. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10228. "<src lang=\"?\">\n\n</src>")
  10229. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10230. "<example>\n?\n</example>")
  10231. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10232. "<quote>\n?\n</quote>")
  10233. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10234. "<verse>\n?\n</verse>")
  10235. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10236. "<verbatim>\n?\n</verbatim>")
  10237. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10238. "<center>\n?\n</center>")
  10239. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10240. "<literal style=\"latex\">\n?\n</literal>")
  10241. ("L" "#+LaTeX: "
  10242. "<literal style=\"latex\">?</literal>")
  10243. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10244. "<literal style=\"html\">\n?\n</literal>")
  10245. ("H" "#+HTML: "
  10246. "<literal style=\"html\">?</literal>")
  10247. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10248. ("A" "#+ASCII: ")
  10249. ("i" "#+INDEX: ?"
  10250. "#+INDEX: ?")
  10251. ("I" "#+INCLUDE: %file ?"
  10252. "<include file=%file markup=\"?\">"))
  10253. "Structure completion elements.
  10254. This is a list of abbreviation keys and values. The value gets inserted
  10255. if you type `<' followed by the key and then press the completion key,
  10256. usually `M-TAB'. %file will be replaced by a file name after prompting
  10257. for the file using completion. The cursor will be placed at the position
  10258. of the `?` in the template.
  10259. There are two templates for each key, the first uses the original Org syntax,
  10260. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10261. the default when the /org-mtags.el/ module has been loaded. See also the
  10262. variable `org-mtags-prefer-muse-templates'."
  10263. :group 'org-completion
  10264. :type '(repeat
  10265. (string :tag "Key")
  10266. (string :tag "Template")
  10267. (string :tag "Muse Template")))
  10268. (defun org-try-structure-completion ()
  10269. "Try to complete a structure template before point.
  10270. This looks for strings like \"<e\" on an otherwise empty line and
  10271. expands them."
  10272. (let ((l (buffer-substring (point-at-bol) (point)))
  10273. a)
  10274. (when (and (looking-at "[ \t]*$")
  10275. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10276. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10277. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10278. (match-beginning 1)) a)
  10279. t)))
  10280. (defun org-complete-expand-structure-template (start cell)
  10281. "Expand a structure template."
  10282. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10283. (rpl (nth (if musep 2 1) cell))
  10284. (ind ""))
  10285. (delete-region start (point))
  10286. (when (string-match "\\`#\\+" rpl)
  10287. (cond
  10288. ((bolp))
  10289. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10290. (setq ind (buffer-substring (point-at-bol) (point))))
  10291. (t (newline))))
  10292. (setq start (point))
  10293. (if (string-match "%file" rpl)
  10294. (setq rpl (replace-match
  10295. (concat
  10296. "\""
  10297. (save-match-data
  10298. (abbreviate-file-name (read-file-name "Include file: ")))
  10299. "\"")
  10300. t t rpl)))
  10301. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10302. (concat "\n" ind)))
  10303. (insert rpl)
  10304. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10305. ;;;; TODO, DEADLINE, Comments
  10306. (defun org-toggle-comment ()
  10307. "Change the COMMENT state of an entry."
  10308. (interactive)
  10309. (save-excursion
  10310. (org-back-to-heading)
  10311. (let (case-fold-search)
  10312. (cond
  10313. ((looking-at (format org-heading-keyword-regexp-format
  10314. org-comment-string))
  10315. (goto-char (match-end 1))
  10316. (looking-at (concat " +" org-comment-string))
  10317. (replace-match "" t t)
  10318. (when (eolp) (insert " ")))
  10319. ((looking-at org-outline-regexp)
  10320. (goto-char (match-end 0))
  10321. (insert org-comment-string " "))))))
  10322. (defvar org-last-todo-state-is-todo nil
  10323. "This is non-nil when the last TODO state change led to a TODO state.
  10324. If the last change removed the TODO tag or switched to DONE, then
  10325. this is nil.")
  10326. (defvar org-setting-tags nil) ; dynamically skipped
  10327. (defvar org-todo-setup-filter-hook nil
  10328. "Hook for functions that pre-filter todo specs.
  10329. Each function takes a todo spec and returns either nil or the spec
  10330. transformed into canonical form." )
  10331. (defvar org-todo-get-default-hook nil
  10332. "Hook for functions that get a default item for todo.
  10333. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10334. nil or a string to be used for the todo mark." )
  10335. (defvar org-agenda-headline-snapshot-before-repeat)
  10336. (defun org-current-effective-time ()
  10337. "Return current time adjusted for `org-extend-today-until' variable."
  10338. (let* ((ct (org-current-time))
  10339. (dct (decode-time ct))
  10340. (ct1
  10341. (if (and org-use-effective-time
  10342. (< (nth 2 dct) org-extend-today-until))
  10343. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10344. ct)))
  10345. ct1))
  10346. (defun org-todo-yesterday (&optional arg)
  10347. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10348. (interactive "P")
  10349. (if (eq major-mode 'org-agenda-mode)
  10350. (apply 'org-agenda-todo-yesterday arg)
  10351. (let* ((hour (third (decode-time
  10352. (org-current-time))))
  10353. (org-extend-today-until (1+ hour)))
  10354. (org-todo arg))))
  10355. (defun org-todo (&optional arg)
  10356. "Change the TODO state of an item.
  10357. The state of an item is given by a keyword at the start of the heading,
  10358. like
  10359. *** TODO Write paper
  10360. *** DONE Call mom
  10361. The different keywords are specified in the variable `org-todo-keywords'.
  10362. By default the available states are \"TODO\" and \"DONE\".
  10363. So for this example: when the item starts with TODO, it is changed to DONE.
  10364. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10365. DONE are present, add TODO at the beginning of the heading.
  10366. With \\[universal-argument] prefix arg, use completion to determine the new \
  10367. state.
  10368. With numeric prefix arg, switch to that state.
  10369. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10370. keywords (nextset).
  10371. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10372. With a numeric prefix arg of 0, inhibit note taking for the change.
  10373. For calling through lisp, arg is also interpreted in the following way:
  10374. 'none -> empty state
  10375. \"\"(empty string) -> switch to empty state
  10376. 'done -> switch to DONE
  10377. 'nextset -> switch to the next set of keywords
  10378. 'previousset -> switch to the previous set of keywords
  10379. \"WAITING\" -> switch to the specified keyword, but only if it
  10380. really is a member of `org-todo-keywords'."
  10381. (interactive "P")
  10382. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10383. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10384. 'region-start-level 'region))
  10385. org-loop-over-headlines-in-active-region)
  10386. (org-map-entries
  10387. `(org-todo ,arg)
  10388. org-loop-over-headlines-in-active-region
  10389. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10390. (if (equal arg '(16)) (setq arg 'nextset))
  10391. (let ((org-blocker-hook org-blocker-hook)
  10392. commentp
  10393. case-fold-search)
  10394. (when (equal arg '(64))
  10395. (setq arg nil org-blocker-hook nil))
  10396. (when (and org-blocker-hook
  10397. (or org-inhibit-blocking
  10398. (org-entry-get nil "NOBLOCKING")))
  10399. (setq org-blocker-hook nil))
  10400. (save-excursion
  10401. (catch 'exit
  10402. (org-back-to-heading t)
  10403. (when (looking-at (concat "^\\*+ " org-comment-string))
  10404. (org-toggle-comment)
  10405. (setq commentp t))
  10406. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10407. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10408. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10409. (let* ((match-data (match-data))
  10410. (startpos (point-at-bol))
  10411. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10412. (org-log-done org-log-done)
  10413. (org-log-repeat org-log-repeat)
  10414. (org-todo-log-states org-todo-log-states)
  10415. (org-inhibit-logging
  10416. (if (equal arg 0)
  10417. (progn (setq arg nil) 'note) org-inhibit-logging))
  10418. (this (match-string 1))
  10419. (hl-pos (match-beginning 0))
  10420. (head (org-get-todo-sequence-head this))
  10421. (ass (assoc head org-todo-kwd-alist))
  10422. (interpret (nth 1 ass))
  10423. (done-word (nth 3 ass))
  10424. (final-done-word (nth 4 ass))
  10425. (org-last-state (or this ""))
  10426. (completion-ignore-case t)
  10427. (member (member this org-todo-keywords-1))
  10428. (tail (cdr member))
  10429. (org-state (cond
  10430. ((and org-todo-key-trigger
  10431. (or (and (equal arg '(4))
  10432. (eq org-use-fast-todo-selection 'prefix))
  10433. (and (not arg) org-use-fast-todo-selection
  10434. (not (eq org-use-fast-todo-selection
  10435. 'prefix)))))
  10436. ;; Use fast selection
  10437. (org-fast-todo-selection))
  10438. ((and (equal arg '(4))
  10439. (or (not org-use-fast-todo-selection)
  10440. (not org-todo-key-trigger)))
  10441. ;; Read a state with completion
  10442. (org-icompleting-read
  10443. "State: " (mapcar (lambda(x) (list x))
  10444. org-todo-keywords-1)
  10445. nil t))
  10446. ((eq arg 'right)
  10447. (if this
  10448. (if tail (car tail) nil)
  10449. (car org-todo-keywords-1)))
  10450. ((eq arg 'left)
  10451. (if (equal member org-todo-keywords-1)
  10452. nil
  10453. (if this
  10454. (nth (- (length org-todo-keywords-1)
  10455. (length tail) 2)
  10456. org-todo-keywords-1)
  10457. (org-last org-todo-keywords-1))))
  10458. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10459. (setq arg nil))) ; hack to fall back to cycling
  10460. (arg
  10461. ;; user or caller requests a specific state
  10462. (cond
  10463. ((equal arg "") nil)
  10464. ((eq arg 'none) nil)
  10465. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10466. ((eq arg 'nextset)
  10467. (or (car (cdr (member head org-todo-heads)))
  10468. (car org-todo-heads)))
  10469. ((eq arg 'previousset)
  10470. (let ((org-todo-heads (reverse org-todo-heads)))
  10471. (or (car (cdr (member head org-todo-heads)))
  10472. (car org-todo-heads))))
  10473. ((car (member arg org-todo-keywords-1)))
  10474. ((stringp arg)
  10475. (error "State `%s' not valid in this file" arg))
  10476. ((nth (1- (prefix-numeric-value arg))
  10477. org-todo-keywords-1))))
  10478. ((null member) (or head (car org-todo-keywords-1)))
  10479. ((equal this final-done-word) nil) ;; -> make empty
  10480. ((null tail) nil) ;; -> first entry
  10481. ((memq interpret '(type priority))
  10482. (if (eq this-command last-command)
  10483. (car tail)
  10484. (if (> (length tail) 0)
  10485. (or done-word (car org-done-keywords))
  10486. nil)))
  10487. (t
  10488. (car tail))))
  10489. (org-state (or
  10490. (run-hook-with-args-until-success
  10491. 'org-todo-get-default-hook org-state org-last-state)
  10492. org-state))
  10493. (next (if org-state (concat " " org-state " ") " "))
  10494. (change-plist (list :type 'todo-state-change :from this :to org-state
  10495. :position startpos))
  10496. dolog now-done-p)
  10497. (when org-blocker-hook
  10498. (setq org-last-todo-state-is-todo
  10499. (not (member this org-done-keywords)))
  10500. (unless (save-excursion
  10501. (save-match-data
  10502. (org-with-wide-buffer
  10503. (run-hook-with-args-until-failure
  10504. 'org-blocker-hook change-plist))))
  10505. (if (org-called-interactively-p 'interactive)
  10506. (error "TODO state change from %s to %s blocked" this org-state)
  10507. ;; fail silently
  10508. (message "TODO state change from %s to %s blocked" this org-state)
  10509. (throw 'exit nil))))
  10510. (store-match-data match-data)
  10511. (replace-match next t t)
  10512. (unless (pos-visible-in-window-p hl-pos)
  10513. (message "TODO state changed to %s" (org-trim next)))
  10514. (unless head
  10515. (setq head (org-get-todo-sequence-head org-state)
  10516. ass (assoc head org-todo-kwd-alist)
  10517. interpret (nth 1 ass)
  10518. done-word (nth 3 ass)
  10519. final-done-word (nth 4 ass)))
  10520. (when (memq arg '(nextset previousset))
  10521. (message "Keyword-Set %d/%d: %s"
  10522. (- (length org-todo-sets) -1
  10523. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10524. (length org-todo-sets)
  10525. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10526. (setq org-last-todo-state-is-todo
  10527. (not (member org-state org-done-keywords)))
  10528. (setq now-done-p (and (member org-state org-done-keywords)
  10529. (not (member this org-done-keywords))))
  10530. (and logging (org-local-logging logging))
  10531. (when (and (or org-todo-log-states org-log-done)
  10532. (not (eq org-inhibit-logging t))
  10533. (not (memq arg '(nextset previousset))))
  10534. ;; we need to look at recording a time and note
  10535. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10536. (nth 2 (assoc this org-todo-log-states))))
  10537. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10538. (setq dolog 'time))
  10539. (when (and org-state
  10540. (member org-state org-not-done-keywords)
  10541. (not (member this org-not-done-keywords)))
  10542. ;; This is now a todo state and was not one before
  10543. ;; If there was a CLOSED time stamp, get rid of it.
  10544. (org-add-planning-info nil nil 'closed))
  10545. (when (and now-done-p org-log-done)
  10546. ;; It is now done, and it was not done before
  10547. (org-add-planning-info 'closed (org-current-effective-time))
  10548. (if (and (not dolog) (eq 'note org-log-done))
  10549. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10550. (when (and org-state dolog)
  10551. ;; This is a non-nil state, and we need to log it
  10552. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10553. ;; Fixup tag positioning
  10554. (org-todo-trigger-tag-changes org-state)
  10555. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10556. (when org-provide-todo-statistics
  10557. (org-update-parent-todo-statistics))
  10558. (run-hooks 'org-after-todo-state-change-hook)
  10559. (if (and arg (not (member org-state org-done-keywords)))
  10560. (setq head (org-get-todo-sequence-head org-state)))
  10561. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10562. ;; Do we need to trigger a repeat?
  10563. (when now-done-p
  10564. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10565. ;; This is for the agenda, take a snapshot of the headline.
  10566. (save-match-data
  10567. (setq org-agenda-headline-snapshot-before-repeat
  10568. (org-get-heading))))
  10569. (org-auto-repeat-maybe org-state))
  10570. ;; Fixup cursor location if close to the keyword
  10571. (if (and (outline-on-heading-p)
  10572. (not (bolp))
  10573. (save-excursion (beginning-of-line 1)
  10574. (looking-at org-todo-line-regexp))
  10575. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10576. (progn
  10577. (goto-char (or (match-end 2) (match-end 1)))
  10578. (and (looking-at " ") (just-one-space))))
  10579. (when org-trigger-hook
  10580. (save-excursion
  10581. (run-hook-with-args 'org-trigger-hook change-plist)))
  10582. (when commentp (org-toggle-comment))))))))
  10583. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10584. "Block turning an entry into a TODO, using the hierarchy.
  10585. This checks whether the current task should be blocked from state
  10586. changes. Such blocking occurs when:
  10587. 1. The task has children which are not all in a completed state.
  10588. 2. A task has a parent with the property :ORDERED:, and there
  10589. are siblings prior to the current task with incomplete
  10590. status.
  10591. 3. The parent of the task is blocked because it has siblings that should
  10592. be done first, or is child of a block grandparent TODO entry."
  10593. (if (not org-enforce-todo-dependencies)
  10594. t ; if locally turned off don't block
  10595. (catch 'dont-block
  10596. ;; If this is not a todo state change, or if this entry is already DONE,
  10597. ;; do not block
  10598. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10599. (member (plist-get change-plist :from)
  10600. (cons 'done org-done-keywords))
  10601. (member (plist-get change-plist :to)
  10602. (cons 'todo org-not-done-keywords))
  10603. (not (plist-get change-plist :to)))
  10604. (throw 'dont-block t))
  10605. ;; If this task has children, and any are undone, it's blocked
  10606. (save-excursion
  10607. (org-back-to-heading t)
  10608. (let ((this-level (funcall outline-level)))
  10609. (outline-next-heading)
  10610. (let ((child-level (funcall outline-level)))
  10611. (while (and (not (eobp))
  10612. (> child-level this-level))
  10613. ;; this todo has children, check whether they are all
  10614. ;; completed
  10615. (if (and (not (org-entry-is-done-p))
  10616. (org-entry-is-todo-p))
  10617. (throw 'dont-block nil))
  10618. (outline-next-heading)
  10619. (setq child-level (funcall outline-level))))))
  10620. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10621. ;; any previous siblings are undone, it's blocked
  10622. (save-excursion
  10623. (org-back-to-heading t)
  10624. (let* ((pos (point))
  10625. (parent-pos (and (org-up-heading-safe) (point))))
  10626. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10627. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10628. (forward-line 1)
  10629. (re-search-forward org-not-done-heading-regexp pos t))
  10630. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10631. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10632. (while t
  10633. (goto-char parent-pos)
  10634. (if (not (looking-at org-not-done-heading-regexp))
  10635. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10636. (setq pos (point))
  10637. (setq parent-pos (and (org-up-heading-safe) (point)))
  10638. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10639. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10640. (forward-line 1)
  10641. (re-search-forward org-not-done-heading-regexp pos t))
  10642. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10643. (defcustom org-track-ordered-property-with-tag nil
  10644. "Should the ORDERED property also be shown as a tag?
  10645. The ORDERED property decides if an entry should require subtasks to be
  10646. completed in sequence. Since a property is not very visible, setting
  10647. this option means that toggling the ORDERED property with the command
  10648. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10649. not relevant for the behavior, but it makes things more visible.
  10650. Note that toggling the tag with tags commands will not change the property
  10651. and therefore not influence behavior!
  10652. This can be t, meaning the tag ORDERED should be used, It can also be a
  10653. string to select a different tag for this task."
  10654. :group 'org-todo
  10655. :type '(choice
  10656. (const :tag "No tracking" nil)
  10657. (const :tag "Track with ORDERED tag" t)
  10658. (string :tag "Use other tag")))
  10659. (defun org-toggle-ordered-property ()
  10660. "Toggle the ORDERED property of the current entry.
  10661. For better visibility, you can track the value of this property with a tag.
  10662. See variable `org-track-ordered-property-with-tag'."
  10663. (interactive)
  10664. (let* ((t1 org-track-ordered-property-with-tag)
  10665. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10666. (save-excursion
  10667. (org-back-to-heading)
  10668. (if (org-entry-get nil "ORDERED")
  10669. (progn
  10670. (org-delete-property "ORDERED")
  10671. (and tag (org-toggle-tag tag 'off))
  10672. (message "Subtasks can be completed in arbitrary order"))
  10673. (org-entry-put nil "ORDERED" "t")
  10674. (and tag (org-toggle-tag tag 'on))
  10675. (message "Subtasks must be completed in sequence")))))
  10676. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10677. (defun org-block-todo-from-checkboxes (change-plist)
  10678. "Block turning an entry into a TODO, using checkboxes.
  10679. This checks whether the current task should be blocked from state
  10680. changes because there are unchecked boxes in this entry."
  10681. (if (not org-enforce-todo-checkbox-dependencies)
  10682. t ; if locally turned off don't block
  10683. (catch 'dont-block
  10684. ;; If this is not a todo state change, or if this entry is already DONE,
  10685. ;; do not block
  10686. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10687. (member (plist-get change-plist :from)
  10688. (cons 'done org-done-keywords))
  10689. (member (plist-get change-plist :to)
  10690. (cons 'todo org-not-done-keywords))
  10691. (not (plist-get change-plist :to)))
  10692. (throw 'dont-block t))
  10693. ;; If this task has checkboxes that are not checked, it's blocked
  10694. (save-excursion
  10695. (org-back-to-heading t)
  10696. (let ((beg (point)) end)
  10697. (outline-next-heading)
  10698. (setq end (point))
  10699. (goto-char beg)
  10700. (if (org-list-search-forward
  10701. (concat (org-item-beginning-re)
  10702. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10703. "\\[[- ]\\]")
  10704. end t)
  10705. (progn
  10706. (if (boundp 'org-blocked-by-checkboxes)
  10707. (setq org-blocked-by-checkboxes t))
  10708. (throw 'dont-block nil)))))
  10709. t))) ; do not block
  10710. (defun org-entry-blocked-p ()
  10711. "Is the current entry blocked?"
  10712. (org-with-buffer-modified-unmodified
  10713. (if (org-entry-get nil "NOBLOCKING")
  10714. nil ;; Never block this entry
  10715. (not
  10716. (run-hook-with-args-until-failure
  10717. 'org-blocker-hook
  10718. (list :type 'todo-state-change
  10719. :position (point)
  10720. :from 'todo
  10721. :to 'done))))))
  10722. (defun org-update-statistics-cookies (all)
  10723. "Update the statistics cookie, either from TODO or from checkboxes.
  10724. This should be called with the cursor in a line with a statistics cookie."
  10725. (interactive "P")
  10726. (if all
  10727. (progn
  10728. (org-update-checkbox-count 'all)
  10729. (org-map-entries 'org-update-parent-todo-statistics))
  10730. (if (not (org-at-heading-p))
  10731. (org-update-checkbox-count)
  10732. (let ((pos (point-marker))
  10733. end l1 l2)
  10734. (ignore-errors (org-back-to-heading t))
  10735. (if (not (org-at-heading-p))
  10736. (org-update-checkbox-count)
  10737. (setq l1 (org-outline-level))
  10738. (setq end (save-excursion
  10739. (outline-next-heading)
  10740. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10741. (point)))
  10742. (if (and (save-excursion
  10743. (re-search-forward
  10744. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10745. (not (save-excursion (re-search-forward
  10746. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10747. (org-update-checkbox-count)
  10748. (if (and l2 (> l2 l1))
  10749. (progn
  10750. (goto-char end)
  10751. (org-update-parent-todo-statistics))
  10752. (goto-char pos)
  10753. (beginning-of-line 1)
  10754. (while (re-search-forward
  10755. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10756. (point-at-eol) t)
  10757. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10758. (goto-char pos)
  10759. (move-marker pos nil)))))
  10760. (defvar org-entry-property-inherited-from) ;; defined below
  10761. (defun org-update-parent-todo-statistics ()
  10762. "Update any statistics cookie in the parent of the current headline.
  10763. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10764. the entire subtree and this will travel up the hierarchy and update
  10765. statistics everywhere."
  10766. (let* ((prop (save-excursion (org-up-heading-safe)
  10767. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10768. (recursive (or (not org-hierarchical-todo-statistics)
  10769. (and prop (string-match "\\<recursive\\>" prop))))
  10770. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10771. 0))
  10772. (first t)
  10773. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10774. level ltoggle l1 new ndel
  10775. (cnt-all 0) (cnt-done 0) is-percent kwd
  10776. checkbox-beg ov ovs ove cookie-present)
  10777. (catch 'exit
  10778. (save-excursion
  10779. (beginning-of-line 1)
  10780. (setq ltoggle (funcall outline-level))
  10781. ;; Three situations are to consider:
  10782. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10783. ;; to the top-level ancestor on the headline;
  10784. ;; 2. If parent has "recursive" property, repeat up to the
  10785. ;; headline setting that property, taking inheritance into
  10786. ;; account;
  10787. ;; 3. Else, move up to direct parent and proceed only once.
  10788. (while (and (setq level (org-up-heading-safe))
  10789. (or recursive first)
  10790. (>= (point) lim))
  10791. (setq first nil cookie-present nil)
  10792. (unless (and level
  10793. (not (string-match
  10794. "\\<checkbox\\>"
  10795. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10796. "")))))
  10797. (throw 'exit nil))
  10798. (while (re-search-forward box-re (point-at-eol) t)
  10799. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10800. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10801. (save-match-data
  10802. (unless (outline-next-heading) (throw 'exit nil))
  10803. (while (and (looking-at org-complex-heading-regexp)
  10804. (> (setq l1 (length (match-string 1))) level))
  10805. (setq kwd (and (or recursive (= l1 ltoggle))
  10806. (match-string 2)))
  10807. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10808. (and (listp org-provide-todo-statistics)
  10809. (or (member kwd org-provide-todo-statistics)
  10810. (member kwd org-done-keywords))))
  10811. (setq cnt-all (1+ cnt-all))
  10812. (if (eq org-provide-todo-statistics t)
  10813. (and kwd (setq cnt-all (1+ cnt-all)))))
  10814. (and (member kwd org-done-keywords)
  10815. (setq cnt-done (1+ cnt-done)))
  10816. (outline-next-heading)))
  10817. (setq new
  10818. (if is-percent
  10819. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10820. (format "[%d/%d]" cnt-done cnt-all))
  10821. ndel (- (match-end 0) checkbox-beg))
  10822. ;; handle overlays when updating cookie from column view
  10823. (when (setq ov (car (overlays-at checkbox-beg)))
  10824. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10825. (delete-overlay ov))
  10826. (goto-char checkbox-beg)
  10827. (insert new)
  10828. (delete-region (point) (+ (point) ndel))
  10829. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10830. (when ov (move-overlay ov ovs ove)))
  10831. (when cookie-present
  10832. (run-hook-with-args 'org-after-todo-statistics-hook
  10833. cnt-done (- cnt-all cnt-done))))))
  10834. (run-hooks 'org-todo-statistics-hook)))
  10835. (defvar org-after-todo-statistics-hook nil
  10836. "Hook that is called after a TODO statistics cookie has been updated.
  10837. Each function is called with two arguments: the number of not-done entries
  10838. and the number of done entries.
  10839. For example, the following function, when added to this hook, will switch
  10840. an entry to DONE when all children are done, and back to TODO when new
  10841. entries are set to a TODO status. Note that this hook is only called
  10842. when there is a statistics cookie in the headline!
  10843. (defun org-summary-todo (n-done n-not-done)
  10844. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10845. (let (org-log-done org-log-states) ; turn off logging
  10846. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10847. ")
  10848. (defvar org-todo-statistics-hook nil
  10849. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10850. This hook runs even if there is no statistics cookie present, in which case
  10851. `org-after-todo-statistics-hook' would not run.")
  10852. (defun org-todo-trigger-tag-changes (state)
  10853. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10854. (let ((l org-todo-state-tags-triggers)
  10855. changes)
  10856. (when (or (not state) (equal state ""))
  10857. (setq changes (append changes (cdr (assoc "" l)))))
  10858. (when (and (stringp state) (> (length state) 0))
  10859. (setq changes (append changes (cdr (assoc state l)))))
  10860. (when (member state org-not-done-keywords)
  10861. (setq changes (append changes (cdr (assoc 'todo l)))))
  10862. (when (member state org-done-keywords)
  10863. (setq changes (append changes (cdr (assoc 'done l)))))
  10864. (dolist (c changes)
  10865. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10866. (defun org-local-logging (value)
  10867. "Get logging settings from a property VALUE."
  10868. (let* (words w a)
  10869. ;; directly set the variables, they are already local.
  10870. (setq org-log-done nil
  10871. org-log-repeat nil
  10872. org-todo-log-states nil)
  10873. (setq words (org-split-string value))
  10874. (while (setq w (pop words))
  10875. (cond
  10876. ((setq a (assoc w org-startup-options))
  10877. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10878. (set (nth 1 a) (nth 2 a))))
  10879. ((setq a (org-extract-log-state-settings w))
  10880. (and (member (car a) org-todo-keywords-1)
  10881. (push a org-todo-log-states)))))))
  10882. (defun org-get-todo-sequence-head (kwd)
  10883. "Return the head of the TODO sequence to which KWD belongs.
  10884. If KWD is not set, check if there is a text property remembering the
  10885. right sequence."
  10886. (let (p)
  10887. (cond
  10888. ((not kwd)
  10889. (or (get-text-property (point-at-bol) 'org-todo-head)
  10890. (progn
  10891. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10892. nil (point-at-eol)))
  10893. (get-text-property p 'org-todo-head))))
  10894. ((not (member kwd org-todo-keywords-1))
  10895. (car org-todo-keywords-1))
  10896. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10897. (defun org-fast-todo-selection ()
  10898. "Fast TODO keyword selection with single keys.
  10899. Returns the new TODO keyword, or nil if no state change should occur."
  10900. (let* ((fulltable org-todo-key-alist)
  10901. (done-keywords org-done-keywords) ;; needed for the faces.
  10902. (maxlen (apply 'max (mapcar
  10903. (lambda (x)
  10904. (if (stringp (car x)) (string-width (car x)) 0))
  10905. fulltable)))
  10906. (expert nil)
  10907. (fwidth (+ maxlen 3 1 3))
  10908. (ncol (/ (- (window-width) 4) fwidth))
  10909. tg cnt e c tbl
  10910. groups ingroup)
  10911. (save-excursion
  10912. (save-window-excursion
  10913. (if expert
  10914. (set-buffer (get-buffer-create " *Org todo*"))
  10915. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10916. (erase-buffer)
  10917. (org-set-local 'org-done-keywords done-keywords)
  10918. (setq tbl fulltable cnt 0)
  10919. (while (setq e (pop tbl))
  10920. (cond
  10921. ((equal e '(:startgroup))
  10922. (push '() groups) (setq ingroup t)
  10923. (when (not (= cnt 0))
  10924. (setq cnt 0)
  10925. (insert "\n"))
  10926. (insert "{ "))
  10927. ((equal e '(:endgroup))
  10928. (setq ingroup nil cnt 0)
  10929. (insert "}\n"))
  10930. ((equal e '(:newline))
  10931. (when (not (= cnt 0))
  10932. (setq cnt 0)
  10933. (insert "\n")
  10934. (setq e (car tbl))
  10935. (while (equal (car tbl) '(:newline))
  10936. (insert "\n")
  10937. (setq tbl (cdr tbl)))))
  10938. (t
  10939. (setq tg (car e) c (cdr e))
  10940. (if ingroup (push tg (car groups)))
  10941. (setq tg (org-add-props tg nil 'face
  10942. (org-get-todo-face tg)))
  10943. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10944. (insert "[" c "] " tg (make-string
  10945. (- fwidth 4 (length tg)) ?\ ))
  10946. (when (= (setq cnt (1+ cnt)) ncol)
  10947. (insert "\n")
  10948. (if ingroup (insert " "))
  10949. (setq cnt 0)))))
  10950. (insert "\n")
  10951. (goto-char (point-min))
  10952. (if (not expert) (org-fit-window-to-buffer))
  10953. (message "[a-z..]:Set [SPC]:clear")
  10954. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10955. (cond
  10956. ((or (= c ?\C-g)
  10957. (and (= c ?q) (not (rassoc c fulltable))))
  10958. (setq quit-flag t))
  10959. ((= c ?\ ) nil)
  10960. ((setq e (rassoc c fulltable) tg (car e))
  10961. tg)
  10962. (t (setq quit-flag t)))))))
  10963. (defun org-entry-is-todo-p ()
  10964. (member (org-get-todo-state) org-not-done-keywords))
  10965. (defun org-entry-is-done-p ()
  10966. (member (org-get-todo-state) org-done-keywords))
  10967. (defun org-get-todo-state ()
  10968. (save-excursion
  10969. (org-back-to-heading t)
  10970. (and (looking-at org-todo-line-regexp)
  10971. (match-end 2)
  10972. (match-string 2))))
  10973. (defun org-at-date-range-p (&optional inactive-ok)
  10974. "Is the cursor inside a date range?"
  10975. (interactive)
  10976. (save-excursion
  10977. (catch 'exit
  10978. (let ((pos (point)))
  10979. (skip-chars-backward "^[<\r\n")
  10980. (skip-chars-backward "<[")
  10981. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10982. (>= (match-end 0) pos)
  10983. (throw 'exit t))
  10984. (skip-chars-backward "^<[\r\n")
  10985. (skip-chars-backward "<[")
  10986. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10987. (>= (match-end 0) pos)
  10988. (throw 'exit t)))
  10989. nil)))
  10990. (defun org-get-repeat (&optional tagline)
  10991. "Check if there is a deadline/schedule with repeater in this entry."
  10992. (save-match-data
  10993. (save-excursion
  10994. (org-back-to-heading t)
  10995. (and (re-search-forward (if tagline
  10996. (concat tagline "\\s-*" org-repeat-re)
  10997. org-repeat-re)
  10998. (org-entry-end-position) t)
  10999. (match-string-no-properties 1)))))
  11000. (defvar org-last-changed-timestamp)
  11001. (defvar org-last-inserted-timestamp)
  11002. (defvar org-log-post-message)
  11003. (defvar org-log-note-purpose)
  11004. (defvar org-log-note-how)
  11005. (defvar org-log-note-extra)
  11006. (defun org-auto-repeat-maybe (done-word)
  11007. "Check if the current headline contains a repeated deadline/schedule.
  11008. If yes, set TODO state back to what it was and change the base date
  11009. of repeating deadline/scheduled time stamps to new date.
  11010. This function is run automatically after each state change to a DONE state."
  11011. ;; last-state is dynamically scoped into this function
  11012. (let* ((repeat (org-get-repeat))
  11013. (aa (assoc org-last-state org-todo-kwd-alist))
  11014. (interpret (nth 1 aa))
  11015. (head (nth 2 aa))
  11016. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11017. (msg "Entry repeats: ")
  11018. (org-log-done nil)
  11019. (org-todo-log-states nil)
  11020. re type n what ts time to-state)
  11021. (when repeat
  11022. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11023. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11024. org-todo-repeat-to-state))
  11025. (unless (and to-state (member to-state org-todo-keywords-1))
  11026. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11027. (org-todo to-state)
  11028. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11029. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11030. (org-time-stamp-format t t))))
  11031. (when org-log-repeat
  11032. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11033. (memq 'org-add-log-note post-command-hook))
  11034. ;; OK, we are already setup for some record
  11035. (if (eq org-log-repeat 'note)
  11036. ;; make sure we take a note, not only a time stamp
  11037. (setq org-log-note-how 'note))
  11038. ;; Set up for taking a record
  11039. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11040. org-last-state
  11041. 'findpos org-log-repeat)))
  11042. (org-back-to-heading t)
  11043. (org-add-planning-info nil nil 'closed)
  11044. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11045. org-deadline-time-regexp "\\)\\|\\("
  11046. org-ts-regexp "\\)"))
  11047. (while (re-search-forward
  11048. re (save-excursion (outline-next-heading) (point)) t)
  11049. (setq type (if (match-end 1) org-scheduled-string
  11050. (if (match-end 3) org-deadline-string "Plain:"))
  11051. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11052. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11053. (setq n (string-to-number (match-string 2 ts))
  11054. what (match-string 3 ts))
  11055. (if (equal what "w") (setq n (* n 7) what "d"))
  11056. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11057. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11058. ;; Preparation, see if we need to modify the start date for the change
  11059. (when (match-end 1)
  11060. (setq time (save-match-data (org-time-string-to-time ts)))
  11061. (cond
  11062. ((equal (match-string 1 ts) ".")
  11063. ;; Shift starting date to today
  11064. (org-timestamp-change
  11065. (- (org-today) (time-to-days time))
  11066. 'day))
  11067. ((equal (match-string 1 ts) "+")
  11068. (let ((nshiftmax 10) (nshift 0))
  11069. (while (or (= nshift 0)
  11070. (<= (time-to-days time)
  11071. (time-to-days (current-time))))
  11072. (when (= (incf nshift) nshiftmax)
  11073. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11074. (error "Abort")))
  11075. (org-timestamp-change n (cdr (assoc what whata)))
  11076. (org-at-timestamp-p t)
  11077. (setq ts (match-string 1))
  11078. (setq time (save-match-data (org-time-string-to-time ts)))))
  11079. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11080. ;; rematch, so that we have everything in place for the real shift
  11081. (org-at-timestamp-p t)
  11082. (setq ts (match-string 1))
  11083. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11084. (org-timestamp-change n (cdr (assoc what whata)))
  11085. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11086. (setq org-log-post-message msg)
  11087. (message "%s" msg))))
  11088. (defun org-show-todo-tree (arg)
  11089. "Make a compact tree which shows all headlines marked with TODO.
  11090. The tree will show the lines where the regexp matches, and all higher
  11091. headlines above the match.
  11092. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11093. With a numeric prefix N, construct a sparse tree for the Nth element
  11094. of `org-todo-keywords-1'."
  11095. (interactive "P")
  11096. (let ((case-fold-search nil)
  11097. (kwd-re
  11098. (cond ((null arg) org-not-done-regexp)
  11099. ((equal arg '(4))
  11100. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11101. (mapcar 'list org-todo-keywords-1))))
  11102. (concat "\\("
  11103. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11104. "\\)\\>")))
  11105. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11106. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11107. org-todo-keywords-1)))
  11108. (t (error "Invalid prefix argument: %s" arg)))))
  11109. (message "%d TODO entries found"
  11110. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11111. (defun org-deadline (&optional remove time)
  11112. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11113. With argument REMOVE, remove any deadline from the item.
  11114. With argument TIME, set the deadline at the corresponding date. TIME
  11115. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11116. (interactive "P")
  11117. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11118. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11119. 'region-start-level 'region))
  11120. org-loop-over-headlines-in-active-region)
  11121. (org-map-entries
  11122. `(org-deadline ',remove ,time)
  11123. org-loop-over-headlines-in-active-region
  11124. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11125. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11126. (repeater (and old-date
  11127. (string-match
  11128. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11129. old-date)
  11130. (match-string 1 old-date))))
  11131. (if remove
  11132. (progn
  11133. (when (and old-date org-log-redeadline)
  11134. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11135. org-log-redeadline))
  11136. (org-remove-timestamp-with-keyword org-deadline-string)
  11137. (message "Item no longer has a deadline."))
  11138. (org-add-planning-info 'deadline time 'closed)
  11139. (when (and old-date org-log-redeadline
  11140. (not (equal old-date
  11141. (substring org-last-inserted-timestamp 1 -1))))
  11142. (org-add-log-setup 'redeadline nil old-date 'findpos
  11143. org-log-redeadline))
  11144. (when repeater
  11145. (save-excursion
  11146. (org-back-to-heading t)
  11147. (when (re-search-forward (concat org-deadline-string " "
  11148. org-last-inserted-timestamp)
  11149. (save-excursion
  11150. (outline-next-heading) (point)) t)
  11151. (goto-char (1- (match-end 0)))
  11152. (insert " " repeater)
  11153. (setq org-last-inserted-timestamp
  11154. (concat (substring org-last-inserted-timestamp 0 -1)
  11155. " " repeater
  11156. (substring org-last-inserted-timestamp -1))))))
  11157. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11158. (defun org-schedule (&optional remove time)
  11159. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11160. With argument REMOVE, remove any scheduling date from the item.
  11161. With argument TIME, scheduled at the corresponding date. TIME can
  11162. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11163. (interactive "P")
  11164. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11165. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11166. 'region-start-level 'region))
  11167. org-loop-over-headlines-in-active-region)
  11168. (org-map-entries
  11169. `(org-schedule ',remove ,time)
  11170. org-loop-over-headlines-in-active-region
  11171. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11172. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11173. (repeater (and old-date
  11174. (string-match
  11175. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11176. old-date)
  11177. (match-string 1 old-date))))
  11178. (if remove
  11179. (progn
  11180. (when (and old-date org-log-reschedule)
  11181. (org-add-log-setup 'delschedule nil old-date 'findpos
  11182. org-log-reschedule))
  11183. (org-remove-timestamp-with-keyword org-scheduled-string)
  11184. (message "Item is no longer scheduled."))
  11185. (org-add-planning-info 'scheduled time 'closed)
  11186. (when (and old-date org-log-reschedule
  11187. (not (equal old-date
  11188. (substring org-last-inserted-timestamp 1 -1))))
  11189. (org-add-log-setup 'reschedule nil old-date 'findpos
  11190. org-log-reschedule))
  11191. (when repeater
  11192. (save-excursion
  11193. (org-back-to-heading t)
  11194. (when (re-search-forward (concat org-scheduled-string " "
  11195. org-last-inserted-timestamp)
  11196. (save-excursion
  11197. (outline-next-heading) (point)) t)
  11198. (goto-char (1- (match-end 0)))
  11199. (insert " " repeater)
  11200. (setq org-last-inserted-timestamp
  11201. (concat (substring org-last-inserted-timestamp 0 -1)
  11202. " " repeater
  11203. (substring org-last-inserted-timestamp -1))))))
  11204. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11205. (defun org-get-scheduled-time (pom &optional inherit)
  11206. "Get the scheduled time as a time tuple, of a format suitable
  11207. for calling org-schedule with, or if there is no scheduling,
  11208. returns nil."
  11209. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11210. (when time
  11211. (apply 'encode-time (org-parse-time-string time)))))
  11212. (defun org-get-deadline-time (pom &optional inherit)
  11213. "Get the deadline as a time tuple, of a format suitable for
  11214. calling org-deadline with, or if there is no scheduling, returns
  11215. nil."
  11216. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11217. (when time
  11218. (apply 'encode-time (org-parse-time-string time)))))
  11219. (defun org-remove-timestamp-with-keyword (keyword)
  11220. "Remove all time stamps with KEYWORD in the current entry."
  11221. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11222. beg)
  11223. (save-excursion
  11224. (org-back-to-heading t)
  11225. (setq beg (point))
  11226. (outline-next-heading)
  11227. (while (re-search-backward re beg t)
  11228. (replace-match "")
  11229. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11230. (equal (char-before) ?\ ))
  11231. (backward-delete-char 1)
  11232. (if (string-match "^[ \t]*$" (buffer-substring
  11233. (point-at-bol) (point-at-eol)))
  11234. (delete-region (point-at-bol)
  11235. (min (point-max) (1+ (point-at-eol))))))))))
  11236. (defun org-add-planning-info (what &optional time &rest remove)
  11237. "Insert new timestamp with keyword in the line directly after the headline.
  11238. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11239. If non is given, the user is prompted for a date.
  11240. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11241. be removed."
  11242. (interactive)
  11243. (let (org-time-was-given org-end-time-was-given ts
  11244. end default-time default-input)
  11245. (catch 'exit
  11246. (when (and (memq what '(scheduled deadline))
  11247. (or (not time)
  11248. (and (stringp time)
  11249. (string-match "^[-+]+[0-9]" time))))
  11250. ;; Try to get a default date/time from existing timestamp
  11251. (save-excursion
  11252. (org-back-to-heading t)
  11253. (setq end (save-excursion (outline-next-heading) (point)))
  11254. (when (re-search-forward (if (eq what 'scheduled)
  11255. org-scheduled-time-regexp
  11256. org-deadline-time-regexp)
  11257. end t)
  11258. (setq ts (match-string 1)
  11259. default-time
  11260. (apply 'encode-time (org-parse-time-string ts))
  11261. default-input (and ts (org-get-compact-tod ts))))))
  11262. (when what
  11263. (setq time
  11264. (if (stringp time)
  11265. ;; This is a string (relative or absolute), set proper date
  11266. (apply 'encode-time
  11267. (org-read-date-analyze
  11268. time default-time (decode-time default-time)))
  11269. ;; If necessary, get the time from the user
  11270. (or time (org-read-date nil 'to-time nil nil
  11271. default-time default-input)))))
  11272. (when (and org-insert-labeled-timestamps-at-point
  11273. (member what '(scheduled deadline)))
  11274. (insert
  11275. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11276. (org-insert-time-stamp time org-time-was-given
  11277. nil nil nil (list org-end-time-was-given))
  11278. (setq what nil))
  11279. (save-excursion
  11280. (save-restriction
  11281. (let (col list elt ts buffer-invisibility-spec)
  11282. (org-back-to-heading t)
  11283. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11284. (goto-char (match-end 1))
  11285. (setq col (current-column))
  11286. (goto-char (match-end 0))
  11287. (if (eobp) (insert "\n") (forward-char 1))
  11288. (when (and (not what)
  11289. (not (looking-at
  11290. (concat "[ \t]*"
  11291. org-keyword-time-not-clock-regexp))))
  11292. ;; Nothing to add, nothing to remove...... :-)
  11293. (throw 'exit nil))
  11294. (if (and (not (looking-at org-outline-regexp))
  11295. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11296. "[^\r\n]*"))
  11297. (not (equal (match-string 1) org-clock-string)))
  11298. (narrow-to-region (match-beginning 0) (match-end 0))
  11299. (insert-before-markers "\n")
  11300. (backward-char 1)
  11301. (narrow-to-region (point) (point))
  11302. (and org-adapt-indentation (org-indent-to-column col)))
  11303. ;; Check if we have to remove something.
  11304. (setq list (cons what remove))
  11305. (while list
  11306. (setq elt (pop list))
  11307. (when (or (and (eq elt 'scheduled)
  11308. (re-search-forward org-scheduled-time-regexp nil t))
  11309. (and (eq elt 'deadline)
  11310. (re-search-forward org-deadline-time-regexp nil t))
  11311. (and (eq elt 'closed)
  11312. (re-search-forward org-closed-time-regexp nil t)))
  11313. (replace-match "")
  11314. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11315. (and (looking-at "[ \t]+") (replace-match ""))
  11316. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11317. (when what
  11318. (insert
  11319. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11320. (cond ((eq what 'scheduled) org-scheduled-string)
  11321. ((eq what 'deadline) org-deadline-string)
  11322. ((eq what 'closed) org-closed-string))
  11323. " ")
  11324. (setq ts (org-insert-time-stamp
  11325. time
  11326. (or org-time-was-given
  11327. (and (eq what 'closed) org-log-done-with-time))
  11328. (eq what 'closed)
  11329. nil nil (list org-end-time-was-given)))
  11330. (insert
  11331. (if (not (or (bolp) (eq (char-before) ?\ )
  11332. (memq (char-after) '(32 10))
  11333. (eobp))) " " ""))
  11334. (end-of-line 1))
  11335. (goto-char (point-min))
  11336. (widen)
  11337. (if (and (looking-at "[ \t]*\n")
  11338. (equal (char-before) ?\n))
  11339. (delete-region (1- (point)) (point-at-eol)))
  11340. ts))))))
  11341. (defvar org-log-note-marker (make-marker))
  11342. (defvar org-log-note-purpose nil)
  11343. (defvar org-log-note-state nil)
  11344. (defvar org-log-note-previous-state nil)
  11345. (defvar org-log-note-how nil)
  11346. (defvar org-log-note-extra nil)
  11347. (defvar org-log-note-window-configuration nil)
  11348. (defvar org-log-note-return-to (make-marker))
  11349. (defvar org-log-note-effective-time nil
  11350. "Remembered current time so that dynamically scoped
  11351. `org-extend-today-until' affects tha timestamps in state change
  11352. log")
  11353. (defvar org-log-post-message nil
  11354. "Message to be displayed after a log note has been stored.
  11355. The auto-repeater uses this.")
  11356. (defun org-add-note ()
  11357. "Add a note to the current entry.
  11358. This is done in the same way as adding a state change note."
  11359. (interactive)
  11360. (org-add-log-setup 'note nil nil 'findpos nil))
  11361. (defvar org-property-end-re)
  11362. (defun org-add-log-setup (&optional purpose state prev-state
  11363. findpos how extra)
  11364. "Set up the post command hook to take a note.
  11365. If this is about to TODO state change, the new state is expected in STATE.
  11366. When FINDPOS is non-nil, find the correct position for the note in
  11367. the current entry. If not, assume that it can be inserted at point.
  11368. HOW is an indicator what kind of note should be created.
  11369. EXTRA is additional text that will be inserted into the notes buffer."
  11370. (let* ((org-log-into-drawer (org-log-into-drawer))
  11371. (drawer (cond ((stringp org-log-into-drawer)
  11372. org-log-into-drawer)
  11373. (org-log-into-drawer "LOGBOOK"))))
  11374. (save-restriction
  11375. (save-excursion
  11376. (when findpos
  11377. (org-back-to-heading t)
  11378. (narrow-to-region (point) (save-excursion
  11379. (outline-next-heading) (point)))
  11380. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11381. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11382. "[^\r\n]*\\)?"))
  11383. (goto-char (match-end 0))
  11384. (cond
  11385. (drawer
  11386. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11387. nil t)
  11388. (progn
  11389. (goto-char (match-end 0))
  11390. (or org-log-states-order-reversed
  11391. (and (re-search-forward org-property-end-re nil t)
  11392. (goto-char (1- (match-beginning 0))))))
  11393. (insert "\n:" drawer ":\n:END:")
  11394. (beginning-of-line 0)
  11395. (org-indent-line)
  11396. (beginning-of-line 2)
  11397. (org-indent-line)
  11398. (end-of-line 0)))
  11399. ((and org-log-state-notes-insert-after-drawers
  11400. (save-excursion
  11401. (forward-line) (looking-at org-drawer-regexp)))
  11402. (forward-line)
  11403. (while (looking-at org-drawer-regexp)
  11404. (goto-char (match-end 0))
  11405. (re-search-forward org-property-end-re (point-max) t)
  11406. (forward-line))
  11407. (forward-line -1)))
  11408. (unless org-log-states-order-reversed
  11409. (and (= (char-after) ?\n) (forward-char 1))
  11410. (org-skip-over-state-notes)
  11411. (skip-chars-backward " \t\n\r")))
  11412. (move-marker org-log-note-marker (point))
  11413. (setq org-log-note-purpose purpose
  11414. org-log-note-state state
  11415. org-log-note-previous-state prev-state
  11416. org-log-note-how how
  11417. org-log-note-extra extra
  11418. org-log-note-effective-time (org-current-effective-time))
  11419. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11420. (defun org-skip-over-state-notes ()
  11421. "Skip past the list of State notes in an entry."
  11422. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11423. (when (ignore-errors (goto-char (org-in-item-p)))
  11424. (let* ((struct (org-list-struct))
  11425. (prevs (org-list-prevs-alist struct)))
  11426. (while (looking-at "[ \t]*- State")
  11427. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11428. (org-list-get-item-end (point) struct)))))))
  11429. (defun org-add-log-note (&optional purpose)
  11430. "Pop up a window for taking a note, and add this note later at point."
  11431. (remove-hook 'post-command-hook 'org-add-log-note)
  11432. (setq org-log-note-window-configuration (current-window-configuration))
  11433. (delete-other-windows)
  11434. (move-marker org-log-note-return-to (point))
  11435. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11436. (goto-char org-log-note-marker)
  11437. (org-switch-to-buffer-other-window "*Org Note*")
  11438. (erase-buffer)
  11439. (if (memq org-log-note-how '(time state))
  11440. (let (current-prefix-arg) (org-store-log-note))
  11441. (let ((org-inhibit-startup t)) (org-mode))
  11442. (insert (format "# Insert note for %s.
  11443. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11444. (cond
  11445. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11446. ((eq org-log-note-purpose 'done) "closed todo item")
  11447. ((eq org-log-note-purpose 'state)
  11448. (format "state change from \"%s\" to \"%s\""
  11449. (or org-log-note-previous-state "")
  11450. (or org-log-note-state "")))
  11451. ((eq org-log-note-purpose 'reschedule)
  11452. "rescheduling")
  11453. ((eq org-log-note-purpose 'delschedule)
  11454. "no longer scheduled")
  11455. ((eq org-log-note-purpose 'redeadline)
  11456. "changing deadline")
  11457. ((eq org-log-note-purpose 'deldeadline)
  11458. "removing deadline")
  11459. ((eq org-log-note-purpose 'refile)
  11460. "refiling")
  11461. ((eq org-log-note-purpose 'note)
  11462. "this entry")
  11463. (t (error "This should not happen")))))
  11464. (if org-log-note-extra (insert org-log-note-extra))
  11465. (org-set-local 'org-finish-function 'org-store-log-note)
  11466. (run-hooks 'org-log-buffer-setup-hook)))
  11467. (defvar org-note-abort nil) ; dynamically scoped
  11468. (defun org-store-log-note ()
  11469. "Finish taking a log note, and insert it to where it belongs."
  11470. (let ((txt (buffer-string))
  11471. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11472. lines ind bul)
  11473. (kill-buffer (current-buffer))
  11474. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11475. (setq txt (replace-match "" t t txt)))
  11476. (if (string-match "\\s-+\\'" txt)
  11477. (setq txt (replace-match "" t t txt)))
  11478. (setq lines (org-split-string txt "\n"))
  11479. (when (and note (string-match "\\S-" note))
  11480. (setq note
  11481. (org-replace-escapes
  11482. note
  11483. (list (cons "%u" (user-login-name))
  11484. (cons "%U" user-full-name)
  11485. (cons "%t" (format-time-string
  11486. (org-time-stamp-format 'long 'inactive)
  11487. org-log-note-effective-time))
  11488. (cons "%T" (format-time-string
  11489. (org-time-stamp-format 'long nil)
  11490. org-log-note-effective-time))
  11491. (cons "%d" (format-time-string
  11492. (org-time-stamp-format nil 'inactive)
  11493. org-log-note-effective-time))
  11494. (cons "%D" (format-time-string
  11495. (org-time-stamp-format nil nil)
  11496. org-log-note-effective-time))
  11497. (cons "%s" (if org-log-note-state
  11498. (concat "\"" org-log-note-state "\"")
  11499. ""))
  11500. (cons "%S" (if org-log-note-previous-state
  11501. (concat "\"" org-log-note-previous-state "\"")
  11502. "\"\"")))))
  11503. (if lines (setq note (concat note " \\\\")))
  11504. (push note lines))
  11505. (when (or current-prefix-arg org-note-abort)
  11506. (when org-log-into-drawer
  11507. (org-remove-empty-drawer-at
  11508. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11509. org-log-note-marker))
  11510. (setq lines nil))
  11511. (when lines
  11512. (with-current-buffer (marker-buffer org-log-note-marker)
  11513. (save-excursion
  11514. (goto-char org-log-note-marker)
  11515. (move-marker org-log-note-marker nil)
  11516. (end-of-line 1)
  11517. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11518. (setq ind (save-excursion
  11519. (if (ignore-errors (goto-char (org-in-item-p)))
  11520. (let ((struct (org-list-struct)))
  11521. (org-list-get-ind
  11522. (org-list-get-top-point struct) struct))
  11523. (skip-chars-backward " \r\t\n")
  11524. (cond
  11525. ((and (org-at-heading-p)
  11526. org-adapt-indentation)
  11527. (1+ (org-current-level)))
  11528. ((org-at-heading-p) 0)
  11529. (t (org-get-indentation))))))
  11530. (setq bul (org-list-bullet-string "-"))
  11531. (org-indent-line-to ind)
  11532. (insert bul (pop lines))
  11533. (let ((ind-body (+ (length bul) ind)))
  11534. (while lines
  11535. (insert "\n")
  11536. (org-indent-line-to ind-body)
  11537. (insert (pop lines))))
  11538. (message "Note stored")
  11539. (org-back-to-heading t)
  11540. (org-cycle-hide-drawers 'children)))))
  11541. (set-window-configuration org-log-note-window-configuration)
  11542. (with-current-buffer (marker-buffer org-log-note-return-to)
  11543. (goto-char org-log-note-return-to))
  11544. (move-marker org-log-note-return-to nil)
  11545. (and org-log-post-message (message "%s" org-log-post-message)))
  11546. (defun org-remove-empty-drawer-at (drawer pos)
  11547. "Remove an empty drawer DRAWER at position POS.
  11548. POS may also be a marker."
  11549. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11550. (save-excursion
  11551. (save-restriction
  11552. (widen)
  11553. (goto-char pos)
  11554. (if (org-in-regexp
  11555. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11556. (replace-match ""))))))
  11557. (defvar org-ts-type nil)
  11558. (defun org-sparse-tree (&optional arg type)
  11559. "Create a sparse tree, prompt for the details.
  11560. This command can create sparse trees. You first need to select the type
  11561. of match used to create the tree:
  11562. t Show all TODO entries.
  11563. T Show entries with a specific TODO keyword.
  11564. m Show entries selected by a tags/property match.
  11565. p Enter a property name and its value (both with completion on existing
  11566. names/values) and show entries with that property.
  11567. r Show entries matching a regular expression (`/' can be used as well).
  11568. b Show deadlines and scheduled items before a date.
  11569. a Show deadlines and scheduled items after a date.
  11570. d Show deadlines due within `org-deadline-warning-days'.
  11571. D Show deadlines and scheduled items between a date range."
  11572. (interactive "P")
  11573. (let (ans kwd value ts-type)
  11574. (setq type (or type org-sparse-tree-default-date-type))
  11575. (setq org-ts-type type)
  11576. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
  11577. (cond ((eq type 'all) "all timestamps")
  11578. ((eq type 'scheduled) "only scheduled")
  11579. ((eq type 'deadline) "only deadline")
  11580. ((eq type 'active) "only active timestamps")
  11581. ((eq type 'inactive) "only inactive timestamps")
  11582. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11583. (t "scheduled/deadline")))
  11584. (setq ans (read-char-exclusive))
  11585. (cond
  11586. ((equal ans ?c)
  11587. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11588. ((equal ans ?d)
  11589. (call-interactively 'org-check-deadlines))
  11590. ((equal ans ?b)
  11591. (call-interactively 'org-check-before-date))
  11592. ((equal ans ?a)
  11593. (call-interactively 'org-check-after-date))
  11594. ((equal ans ?D)
  11595. (call-interactively 'org-check-dates-range))
  11596. ((equal ans ?t)
  11597. (call-interactively 'org-show-todo-tree))
  11598. ((equal ans ?T)
  11599. (org-show-todo-tree '(4)))
  11600. ((member ans '(?T ?m))
  11601. (call-interactively 'org-match-sparse-tree))
  11602. ((member ans '(?p ?P))
  11603. (setq kwd (org-icompleting-read "Property: "
  11604. (mapcar 'list (org-buffer-property-keys))))
  11605. (setq value (org-icompleting-read "Value: "
  11606. (mapcar 'list (org-property-values kwd))))
  11607. (unless (string-match "\\`{.*}\\'" value)
  11608. (setq value (concat "\"" value "\"")))
  11609. (org-match-sparse-tree arg (concat kwd "=" value)))
  11610. ((member ans '(?r ?R ?/))
  11611. (call-interactively 'org-occur))
  11612. (t (error "No such sparse tree command \"%c\"" ans)))))
  11613. (defvar org-occur-highlights nil
  11614. "List of overlays used for occur matches.")
  11615. (make-variable-buffer-local 'org-occur-highlights)
  11616. (defvar org-occur-parameters nil
  11617. "Parameters of the active org-occur calls.
  11618. This is a list, each call to org-occur pushes as cons cell,
  11619. containing the regular expression and the callback, onto the list.
  11620. The list can contain several entries if `org-occur' has been called
  11621. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11622. will only contain one set of parameters. When the highlights are
  11623. removed (for example with `C-c C-c', or with the next edit (depending
  11624. on `org-remove-highlights-with-change'), this variable is emptied
  11625. as well.")
  11626. (make-variable-buffer-local 'org-occur-parameters)
  11627. (defun org-occur (regexp &optional keep-previous callback)
  11628. "Make a compact tree which shows all matches of REGEXP.
  11629. The tree will show the lines where the regexp matches, and all higher
  11630. headlines above the match. It will also show the heading after the match,
  11631. to make sure editing the matching entry is easy.
  11632. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11633. call to `org-occur' will be kept, to allow stacking of calls to this
  11634. command.
  11635. If CALLBACK is non-nil, it is a function which is called to confirm
  11636. that the match should indeed be shown."
  11637. (interactive "sRegexp: \nP")
  11638. (when (equal regexp "")
  11639. (error "Regexp cannot be empty"))
  11640. (unless keep-previous
  11641. (org-remove-occur-highlights nil nil t))
  11642. (push (cons regexp callback) org-occur-parameters)
  11643. (let ((cnt 0))
  11644. (save-excursion
  11645. (goto-char (point-min))
  11646. (if (or (not keep-previous) ; do not want to keep
  11647. (not org-occur-highlights)) ; no previous matches
  11648. ;; hide everything
  11649. (org-overview))
  11650. (while (re-search-forward regexp nil t)
  11651. (when (or (not callback)
  11652. (save-match-data (funcall callback)))
  11653. (setq cnt (1+ cnt))
  11654. (when org-highlight-sparse-tree-matches
  11655. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11656. (org-show-context 'occur-tree))))
  11657. (when org-remove-highlights-with-change
  11658. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11659. nil 'local))
  11660. (unless org-sparse-tree-open-archived-trees
  11661. (org-hide-archived-subtrees (point-min) (point-max)))
  11662. (run-hooks 'org-occur-hook)
  11663. (if (org-called-interactively-p 'interactive)
  11664. (message "%d match(es) for regexp %s" cnt regexp))
  11665. cnt))
  11666. (defun org-occur-next-match (&optional n reset)
  11667. "Function for `next-error-function' to find sparse tree matches.
  11668. N is the number of matches to move, when negative move backwards.
  11669. RESET is entirely ignored - this function always goes back to the
  11670. starting point when no match is found."
  11671. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11672. (search-func (if (< n 0)
  11673. 'previous-single-char-property-change
  11674. 'next-single-char-property-change))
  11675. (n (abs n))
  11676. (pos (point))
  11677. p1)
  11678. (catch 'exit
  11679. (while (setq p1 (funcall search-func (point) 'org-type))
  11680. (when (equal p1 limit)
  11681. (goto-char pos)
  11682. (error "No more matches"))
  11683. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11684. (setq n (1- n))
  11685. (when (= n 0)
  11686. (goto-char p1)
  11687. (throw 'exit (point))))
  11688. (goto-char p1))
  11689. (goto-char p1)
  11690. (error "No more matches"))))
  11691. (defun org-show-context (&optional key)
  11692. "Make sure point and context are visible.
  11693. How much context is shown depends upon the variables
  11694. `org-show-hierarchy-above', `org-show-following-heading',
  11695. `org-show-entry-below' and `org-show-siblings'."
  11696. (let ((heading-p (org-at-heading-p t))
  11697. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11698. (following-p (org-get-alist-option org-show-following-heading key))
  11699. (entry-p (org-get-alist-option org-show-entry-below key))
  11700. (siblings-p (org-get-alist-option org-show-siblings key)))
  11701. (catch 'exit
  11702. ;; Show heading or entry text
  11703. (if (and heading-p (not entry-p))
  11704. (org-flag-heading nil) ; only show the heading
  11705. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11706. (org-show-hidden-entry))) ; show entire entry
  11707. (when following-p
  11708. ;; Show next sibling, or heading below text
  11709. (save-excursion
  11710. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11711. (org-flag-heading nil))))
  11712. (when siblings-p (org-show-siblings))
  11713. (when hierarchy-p
  11714. ;; show all higher headings, possibly with siblings
  11715. (save-excursion
  11716. (while (and (condition-case nil
  11717. (progn (org-up-heading-all 1) t)
  11718. (error nil))
  11719. (not (bobp)))
  11720. (org-flag-heading nil)
  11721. (when siblings-p (org-show-siblings))))))))
  11722. (defvar org-reveal-start-hook nil
  11723. "Hook run before revealing a location.")
  11724. (defun org-reveal (&optional siblings)
  11725. "Show current entry, hierarchy above it, and the following headline.
  11726. This can be used to show a consistent set of context around locations
  11727. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11728. not t for the search context.
  11729. With optional argument SIBLINGS, on each level of the hierarchy all
  11730. siblings are shown. This repairs the tree structure to what it would
  11731. look like when opened with hierarchical calls to `org-cycle'.
  11732. With double optional argument \\[universal-argument] \\[universal-argument], \
  11733. go to the parent and show the
  11734. entire tree."
  11735. (interactive "P")
  11736. (run-hooks 'org-reveal-start-hook)
  11737. (let ((org-show-hierarchy-above t)
  11738. (org-show-following-heading t)
  11739. (org-show-siblings (if siblings t org-show-siblings)))
  11740. (org-show-context nil))
  11741. (when (equal siblings '(16))
  11742. (save-excursion
  11743. (when (org-up-heading-safe)
  11744. (org-show-subtree)
  11745. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11746. (defun org-highlight-new-match (beg end)
  11747. "Highlight from BEG to END and mark the highlight is an occur headline."
  11748. (let ((ov (make-overlay beg end)))
  11749. (overlay-put ov 'face 'secondary-selection)
  11750. (overlay-put ov 'org-type 'org-occur)
  11751. (push ov org-occur-highlights)))
  11752. (defun org-remove-occur-highlights (&optional beg end noremove)
  11753. "Remove the occur highlights from the buffer.
  11754. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11755. from the `before-change-functions' in the current buffer."
  11756. (interactive)
  11757. (unless org-inhibit-highlight-removal
  11758. (mapc 'delete-overlay org-occur-highlights)
  11759. (setq org-occur-highlights nil)
  11760. (setq org-occur-parameters nil)
  11761. (unless noremove
  11762. (remove-hook 'before-change-functions
  11763. 'org-remove-occur-highlights 'local))))
  11764. ;;;; Priorities
  11765. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11766. "Regular expression matching the priority indicator.")
  11767. (defvar org-remove-priority-next-time nil)
  11768. (defun org-priority-up ()
  11769. "Increase the priority of the current item."
  11770. (interactive)
  11771. (org-priority 'up))
  11772. (defun org-priority-down ()
  11773. "Decrease the priority of the current item."
  11774. (interactive)
  11775. (org-priority 'down))
  11776. (defun org-priority (&optional action show)
  11777. "Change the priority of an item.
  11778. ACTION can be `set', `up', `down', or a character."
  11779. (interactive "P")
  11780. (if (equal action '(4))
  11781. (org-show-priority)
  11782. (unless org-enable-priority-commands
  11783. (error "Priority commands are disabled"))
  11784. (setq action (or action 'set))
  11785. (let (current new news have remove)
  11786. (save-excursion
  11787. (org-back-to-heading t)
  11788. (if (looking-at org-priority-regexp)
  11789. (setq current (string-to-char (match-string 2))
  11790. have t))
  11791. (cond
  11792. ((eq action 'remove)
  11793. (setq remove t new ?\ ))
  11794. ((or (eq action 'set)
  11795. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11796. (if (not (eq action 'set))
  11797. (setq new action)
  11798. (message "Priority %c-%c, SPC to remove: "
  11799. org-highest-priority org-lowest-priority)
  11800. (save-match-data
  11801. (setq new (read-char-exclusive))))
  11802. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11803. (= (upcase org-lowest-priority) org-lowest-priority))
  11804. (setq new (upcase new)))
  11805. (cond ((equal new ?\ ) (setq remove t))
  11806. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11807. (error "Priority must be between `%c' and `%c'"
  11808. org-highest-priority org-lowest-priority))))
  11809. ((eq action 'up)
  11810. (setq new (if have
  11811. (1- current) ; normal cycling
  11812. ;; last priority was empty
  11813. (if (eq last-command this-command)
  11814. org-lowest-priority ; wrap around empty to lowest
  11815. ;; default
  11816. (if org-priority-start-cycle-with-default
  11817. org-default-priority
  11818. (1- org-default-priority))))))
  11819. ((eq action 'down)
  11820. (setq new (if have
  11821. (1+ current) ; normal cycling
  11822. ;; last priority was empty
  11823. (if (eq last-command this-command)
  11824. org-highest-priority ; wrap around empty to highest
  11825. ;; default
  11826. (if org-priority-start-cycle-with-default
  11827. org-default-priority
  11828. (1+ org-default-priority))))))
  11829. (t (error "Invalid action")))
  11830. (if (or (< (upcase new) org-highest-priority)
  11831. (> (upcase new) org-lowest-priority))
  11832. (if (and (memq action '(up down))
  11833. (not have) (not (eq last-command this-command)))
  11834. ;; `new' is from default priority
  11835. (error
  11836. "The default can not be set, see `org-default-priority' why")
  11837. ;; normal cycling: `new' is beyond highest/lowest priority
  11838. ;; and is wrapped around to the empty priority
  11839. (setq remove t)))
  11840. (setq news (format "%c" new))
  11841. (if have
  11842. (if remove
  11843. (replace-match "" t t nil 1)
  11844. (replace-match news t t nil 2))
  11845. (if remove
  11846. (error "No priority cookie found in line")
  11847. (let ((case-fold-search nil))
  11848. (looking-at org-todo-line-regexp))
  11849. (if (match-end 2)
  11850. (progn
  11851. (goto-char (match-end 2))
  11852. (insert " [#" news "]"))
  11853. (goto-char (match-beginning 3))
  11854. (insert "[#" news "] "))))
  11855. (org-preserve-lc (org-set-tags nil 'align)))
  11856. (if remove
  11857. (message "Priority removed")
  11858. (message "Priority of current item set to %s" news)))))
  11859. (defun org-show-priority ()
  11860. "Show the priority of the current item.
  11861. This priority is composed of the main priority given with the [#A] cookies,
  11862. and by additional input from the age of a schedules or deadline entry."
  11863. (interactive)
  11864. (let ((pri (if (eq major-mode 'org-agenda-mode)
  11865. (org-get-at-bol 'priority)
  11866. (save-excursion
  11867. (save-match-data
  11868. (beginning-of-line)
  11869. (and (looking-at org-heading-regexp)
  11870. (org-get-priority (match-string 0))))))))
  11871. (message "Priority is %d" (if pri pri -1000))))
  11872. (defun org-get-priority (s)
  11873. "Find priority cookie and return priority."
  11874. (save-match-data
  11875. (if (functionp org-get-priority-function)
  11876. (funcall org-get-priority-function)
  11877. (if (not (string-match org-priority-regexp s))
  11878. (* 1000 (- org-lowest-priority org-default-priority))
  11879. (* 1000 (- org-lowest-priority
  11880. (string-to-char (match-string 2 s))))))))
  11881. ;;;; Tags
  11882. (defvar org-agenda-archives-mode)
  11883. (defvar org-map-continue-from nil
  11884. "Position from where mapping should continue.
  11885. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11886. (defvar org-scanner-tags nil
  11887. "The current tag list while the tags scanner is running.")
  11888. (defvar org-trust-scanner-tags nil
  11889. "Should `org-get-tags-at' use the tags for the scanner.
  11890. This is for internal dynamical scoping only.
  11891. When this is non-nil, the function `org-get-tags-at' will return the value
  11892. of `org-scanner-tags' instead of building the list by itself. This
  11893. can lead to large speed-ups when the tags scanner is used in a file with
  11894. many entries, and when the list of tags is retrieved, for example to
  11895. obtain a list of properties. Building the tags list for each entry in such
  11896. a file becomes an N^2 operation - but with this variable set, it scales
  11897. as N.")
  11898. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11899. "Scan headline tags with inheritance and produce output ACTION.
  11900. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11901. or `agenda' to produce an entry list for an agenda view. It can also be
  11902. a Lisp form or a function that should be called at each matched headline, in
  11903. this case the return value is a list of all return values from these calls.
  11904. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11905. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11906. only lines with a not-done TODO keyword are included in the output.
  11907. This should be the same variable that was scoped into
  11908. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11909. START-LEVEL can be a string with asterisks, reducing the scope to
  11910. headlines matching this string."
  11911. (require 'org-agenda)
  11912. (let* ((re (concat "^"
  11913. (if start-level
  11914. ;; Get the correct level to match
  11915. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11916. org-outline-regexp)
  11917. " *\\(\\<\\("
  11918. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11919. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11920. (props (list 'face 'default
  11921. 'done-face 'org-agenda-done
  11922. 'undone-face 'default
  11923. 'mouse-face 'highlight
  11924. 'org-not-done-regexp org-not-done-regexp
  11925. 'org-todo-regexp org-todo-regexp
  11926. 'org-complex-heading-regexp org-complex-heading-regexp
  11927. 'help-echo
  11928. (format "mouse-2 or RET jump to org file %s"
  11929. (abbreviate-file-name
  11930. (or (buffer-file-name (buffer-base-buffer))
  11931. (buffer-name (buffer-base-buffer)))))))
  11932. (case-fold-search nil)
  11933. (org-map-continue-from nil)
  11934. lspos tags tags-list
  11935. (tags-alist (list (cons 0 org-file-tags)))
  11936. (llast 0) rtn rtn1 level category i txt
  11937. todo marker entry priority)
  11938. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11939. (setq action (list 'lambda nil action)))
  11940. (save-excursion
  11941. (goto-char (point-min))
  11942. (when (eq action 'sparse-tree)
  11943. (org-overview)
  11944. (org-remove-occur-highlights))
  11945. (while (re-search-forward re nil t)
  11946. (setq org-map-continue-from nil)
  11947. (catch :skip
  11948. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11949. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11950. (goto-char (setq lspos (match-beginning 0)))
  11951. (setq level (org-reduced-level (funcall outline-level))
  11952. category (org-get-category))
  11953. (setq i llast llast level)
  11954. ;; remove tag lists from same and sublevels
  11955. (while (>= i level)
  11956. (when (setq entry (assoc i tags-alist))
  11957. (setq tags-alist (delete entry tags-alist)))
  11958. (setq i (1- i)))
  11959. ;; add the next tags
  11960. (when tags
  11961. (setq tags (org-split-string tags ":")
  11962. tags-alist
  11963. (cons (cons level tags) tags-alist)))
  11964. ;; compile tags for current headline
  11965. (setq tags-list
  11966. (if org-use-tag-inheritance
  11967. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11968. tags)
  11969. org-scanner-tags tags-list)
  11970. (when org-use-tag-inheritance
  11971. (setcdr (car tags-alist)
  11972. (mapcar (lambda (x)
  11973. (setq x (copy-sequence x))
  11974. (org-add-prop-inherited x))
  11975. (cdar tags-alist))))
  11976. (when (and tags org-use-tag-inheritance
  11977. (or (not (eq t org-use-tag-inheritance))
  11978. org-tags-exclude-from-inheritance))
  11979. ;; selective inheritance, remove uninherited ones
  11980. (setcdr (car tags-alist)
  11981. (org-remove-uninherited-tags (cdar tags-alist))))
  11982. (when (and
  11983. ;; eval matcher only when the todo condition is OK
  11984. (and (or (not todo-only) (member todo org-not-done-keywords))
  11985. (let ((case-fold-search t) (org-trust-scanner-tags t))
  11986. (eval matcher)))
  11987. ;; Call the skipper, but return t if it does not skip,
  11988. ;; so that the `and' form continues evaluating
  11989. (progn
  11990. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11991. t)
  11992. ;; Check if timestamps are deselecting this entry
  11993. (or (not todo-only)
  11994. (and (member todo org-not-done-keywords)
  11995. (or (not org-agenda-tags-todo-honor-ignore-options)
  11996. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  11997. ;; select this headline
  11998. (cond
  11999. ((eq action 'sparse-tree)
  12000. (and org-highlight-sparse-tree-matches
  12001. (org-get-heading) (match-end 0)
  12002. (org-highlight-new-match
  12003. (match-beginning 1) (match-end 1)))
  12004. (org-show-context 'tags-tree))
  12005. ((eq action 'agenda)
  12006. (setq txt (org-agenda-format-item
  12007. ""
  12008. (concat
  12009. (if (eq org-tags-match-list-sublevels 'indented)
  12010. (make-string (1- level) ?.) "")
  12011. (org-get-heading))
  12012. category
  12013. tags-list)
  12014. priority (org-get-priority txt))
  12015. (goto-char lspos)
  12016. (setq marker (org-agenda-new-marker))
  12017. (org-add-props txt props
  12018. 'org-marker marker 'org-hd-marker marker 'org-category category
  12019. 'todo-state todo
  12020. 'priority priority 'type "tagsmatch")
  12021. (push txt rtn))
  12022. ((functionp action)
  12023. (setq org-map-continue-from nil)
  12024. (save-excursion
  12025. (setq rtn1 (funcall action))
  12026. (push rtn1 rtn)))
  12027. (t (error "Invalid action")))
  12028. ;; if we are to skip sublevels, jump to end of subtree
  12029. (unless org-tags-match-list-sublevels
  12030. (org-end-of-subtree t)
  12031. (backward-char 1))))
  12032. ;; Get the correct position from where to continue
  12033. (if org-map-continue-from
  12034. (goto-char org-map-continue-from)
  12035. (and (= (point) lspos) (end-of-line 1)))))
  12036. (when (and (eq action 'sparse-tree)
  12037. (not org-sparse-tree-open-archived-trees))
  12038. (org-hide-archived-subtrees (point-min) (point-max)))
  12039. (nreverse rtn)))
  12040. (defun org-remove-uninherited-tags (tags)
  12041. "Remove all tags that are not inherited from the list TAGS."
  12042. (cond
  12043. ((eq org-use-tag-inheritance t)
  12044. (if org-tags-exclude-from-inheritance
  12045. (org-delete-all org-tags-exclude-from-inheritance tags)
  12046. tags))
  12047. ((not org-use-tag-inheritance) nil)
  12048. ((stringp org-use-tag-inheritance)
  12049. (delq nil (mapcar
  12050. (lambda (x)
  12051. (if (and (string-match org-use-tag-inheritance x)
  12052. (not (member x org-tags-exclude-from-inheritance)))
  12053. x nil))
  12054. tags)))
  12055. ((listp org-use-tag-inheritance)
  12056. (delq nil (mapcar
  12057. (lambda (x)
  12058. (if (member x org-use-tag-inheritance) x nil))
  12059. tags)))))
  12060. (defun org-match-sparse-tree (&optional todo-only match)
  12061. "Create a sparse tree according to tags string MATCH.
  12062. MATCH can contain positive and negative selection of tags, like
  12063. \"+WORK+URGENT-WITHBOSS\".
  12064. If optional argument TODO-ONLY is non-nil, only select lines that are
  12065. also TODO lines."
  12066. (interactive "P")
  12067. (org-agenda-prepare-buffers (list (current-buffer)))
  12068. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12069. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12070. (defvar org-cached-props nil)
  12071. (defun org-cached-entry-get (pom property)
  12072. (if (or (eq t org-use-property-inheritance)
  12073. (and (stringp org-use-property-inheritance)
  12074. (string-match org-use-property-inheritance property))
  12075. (and (listp org-use-property-inheritance)
  12076. (member property org-use-property-inheritance)))
  12077. ;; Caching is not possible, check it directly
  12078. (org-entry-get pom property 'inherit)
  12079. ;; Get all properties, so that we can do complicated checks easily
  12080. (cdr (assoc property (or org-cached-props
  12081. (setq org-cached-props
  12082. (org-entry-properties pom)))))))
  12083. (defun org-global-tags-completion-table (&optional files)
  12084. "Return the list of all tags in all agenda buffer/files.
  12085. Optional FILES argument is a list of files which can be used
  12086. instead of the agenda files."
  12087. (save-excursion
  12088. (org-uniquify
  12089. (delq nil
  12090. (apply 'append
  12091. (mapcar
  12092. (lambda (file)
  12093. (set-buffer (find-file-noselect file))
  12094. (append (org-get-buffer-tags)
  12095. (mapcar (lambda (x) (if (stringp (car-safe x))
  12096. (list (car-safe x)) nil))
  12097. org-tag-alist)))
  12098. (if (and files (car files))
  12099. files
  12100. (org-agenda-files))))))))
  12101. (defun org-make-tags-matcher (match)
  12102. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12103. The variable `todo-only' is scoped dynamically into this function.
  12104. It will be set to t if the matcher restricts matching to TODO entries,
  12105. otherwise will not be touched.
  12106. Returns a cons of the selection string MATCH and the constructed
  12107. lisp form implementing the matcher. The matcher is to be evaluated
  12108. at an Org entry, with point on the headline, and returns t if the
  12109. entry matches the selection string MATCH. The returned lisp form
  12110. references two variables with information about the entry, which
  12111. must be bound around the form's evaluation: todo, the TODO keyword
  12112. at the entry (or nil of none); and tags-list, the list of all tags
  12113. at the entry including inherited ones. Additionally, the category
  12114. of the entry (if any) must be specified as the text property
  12115. 'org-category on the headline.
  12116. See also `org-scan-tags'.
  12117. "
  12118. (declare (special todo-only))
  12119. (unless (boundp 'todo-only)
  12120. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12121. (unless match
  12122. ;; Get a new match request, with completion
  12123. (let ((org-last-tags-completion-table
  12124. (org-global-tags-completion-table)))
  12125. (setq match (org-completing-read-no-i
  12126. "Match: " 'org-tags-completion-function nil nil nil
  12127. 'org-tags-history))))
  12128. ;; Parse the string and create a lisp form
  12129. (let ((match0 match)
  12130. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12131. minus tag mm
  12132. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12133. orterms term orlist re-p str-p level-p level-op time-p
  12134. prop-p pn pv po gv rest)
  12135. (if (string-match "/+" match)
  12136. ;; match contains also a todo-matching request
  12137. (progn
  12138. (setq tagsmatch (substring match 0 (match-beginning 0))
  12139. todomatch (substring match (match-end 0)))
  12140. (if (string-match "^!" todomatch)
  12141. (setq todo-only t todomatch (substring todomatch 1)))
  12142. (if (string-match "^\\s-*$" todomatch)
  12143. (setq todomatch nil)))
  12144. ;; only matching tags
  12145. (setq tagsmatch match todomatch nil))
  12146. ;; Make the tags matcher
  12147. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12148. (setq tagsmatcher t)
  12149. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12150. (while (setq term (pop orterms))
  12151. (while (and (equal (substring term -1) "\\") orterms)
  12152. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12153. (while (string-match re term)
  12154. (setq rest (substring term (match-end 0))
  12155. minus (and (match-end 1)
  12156. (equal (match-string 1 term) "-"))
  12157. tag (save-match-data (replace-regexp-in-string
  12158. "\\\\-" "-"
  12159. (match-string 2 term)))
  12160. re-p (equal (string-to-char tag) ?{)
  12161. level-p (match-end 4)
  12162. prop-p (match-end 5)
  12163. mm (cond
  12164. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12165. (level-p
  12166. (setq level-op (org-op-to-function (match-string 3 term)))
  12167. `(,level-op level ,(string-to-number
  12168. (match-string 4 term))))
  12169. (prop-p
  12170. (setq pn (match-string 5 term)
  12171. po (match-string 6 term)
  12172. pv (match-string 7 term)
  12173. re-p (equal (string-to-char pv) ?{)
  12174. str-p (equal (string-to-char pv) ?\")
  12175. time-p (save-match-data
  12176. (string-match "^\"[[<].*[]>]\"$" pv))
  12177. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12178. (if time-p (setq pv (org-matcher-time pv)))
  12179. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12180. (cond
  12181. ((equal pn "CATEGORY")
  12182. (setq gv '(get-text-property (point) 'org-category)))
  12183. ((equal pn "TODO")
  12184. (setq gv 'todo))
  12185. (t
  12186. (setq gv `(org-cached-entry-get nil ,pn))))
  12187. (if re-p
  12188. (if (eq po 'org<>)
  12189. `(not (string-match ,pv (or ,gv "")))
  12190. `(string-match ,pv (or ,gv "")))
  12191. (if str-p
  12192. `(,po (or ,gv "") ,pv)
  12193. `(,po (string-to-number (or ,gv ""))
  12194. ,(string-to-number pv) ))))
  12195. (t `(member ,tag tags-list)))
  12196. mm (if minus (list 'not mm) mm)
  12197. term rest)
  12198. (push mm tagsmatcher))
  12199. (push (if (> (length tagsmatcher) 1)
  12200. (cons 'and tagsmatcher)
  12201. (car tagsmatcher))
  12202. orlist)
  12203. (setq tagsmatcher nil))
  12204. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12205. (setq tagsmatcher
  12206. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12207. ;; Make the todo matcher
  12208. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12209. (setq todomatcher t)
  12210. (setq orterms (org-split-string todomatch "|") orlist nil)
  12211. (while (setq term (pop orterms))
  12212. (while (string-match re term)
  12213. (setq minus (and (match-end 1)
  12214. (equal (match-string 1 term) "-"))
  12215. kwd (match-string 2 term)
  12216. re-p (equal (string-to-char kwd) ?{)
  12217. term (substring term (match-end 0))
  12218. mm (if re-p
  12219. `(string-match ,(substring kwd 1 -1) todo)
  12220. (list 'equal 'todo kwd))
  12221. mm (if minus (list 'not mm) mm))
  12222. (push mm todomatcher))
  12223. (push (if (> (length todomatcher) 1)
  12224. (cons 'and todomatcher)
  12225. (car todomatcher))
  12226. orlist)
  12227. (setq todomatcher nil))
  12228. (setq todomatcher (if (> (length orlist) 1)
  12229. (cons 'or orlist) (car orlist))))
  12230. ;; Return the string and lisp forms of the matcher
  12231. (setq matcher (if todomatcher
  12232. (list 'and tagsmatcher todomatcher)
  12233. tagsmatcher))
  12234. (when todo-only
  12235. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12236. matcher)))
  12237. (cons match0 matcher)))
  12238. (defun org-op-to-function (op &optional stringp)
  12239. "Turn an operator into the appropriate function."
  12240. (setq op
  12241. (cond
  12242. ((equal op "<" ) '(< string< org-time<))
  12243. ((equal op ">" ) '(> org-string> org-time>))
  12244. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12245. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12246. ((member op '("=" "==")) '(= string= org-time=))
  12247. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12248. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12249. (defun org<> (a b) (not (= a b)))
  12250. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12251. (defun org-string>= (a b) (not (string< a b)))
  12252. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12253. (defun org-string<> (a b) (not (string= a b)))
  12254. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12255. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12256. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12257. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12258. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12259. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12260. (defun org-2ft (s)
  12261. "Convert S to a floating point time.
  12262. If S is already a number, just return it. If it is a string, parse
  12263. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12264. (cond
  12265. ((numberp s) s)
  12266. ((stringp s)
  12267. (condition-case nil
  12268. (float-time (apply 'encode-time (org-parse-time-string s)))
  12269. (error 0.)))
  12270. (t 0.)))
  12271. (defun org-time-today ()
  12272. "Time in seconds today at 0:00.
  12273. Returns the float number of seconds since the beginning of the
  12274. epoch to the beginning of today (00:00)."
  12275. (float-time (apply 'encode-time
  12276. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12277. (defun org-matcher-time (s)
  12278. "Interpret a time comparison value."
  12279. (save-match-data
  12280. (cond
  12281. ((string= s "<now>") (float-time))
  12282. ((string= s "<today>") (org-time-today))
  12283. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12284. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12285. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12286. (+ (org-time-today)
  12287. (* (string-to-number (match-string 1 s))
  12288. (cdr (assoc (match-string 2 s)
  12289. '(("d" . 86400.0) ("w" . 604800.0)
  12290. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12291. (t (org-2ft s)))))
  12292. (defun org-match-any-p (re list)
  12293. "Does re match any element of list?"
  12294. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12295. (delq nil list))
  12296. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12297. (defvar org-tags-overlay (make-overlay 1 1))
  12298. (org-detach-overlay org-tags-overlay)
  12299. (defun org-get-local-tags-at (&optional pos)
  12300. "Get a list of tags defined in the current headline."
  12301. (org-get-tags-at pos 'local))
  12302. (defun org-get-local-tags ()
  12303. "Get a list of tags defined in the current headline."
  12304. (org-get-tags-at nil 'local))
  12305. (defun org-get-tags-at (&optional pos local)
  12306. "Get a list of all headline tags applicable at POS.
  12307. POS defaults to point. If tags are inherited, the list contains
  12308. the targets in the same sequence as the headlines appear, i.e.
  12309. the tags of the current headline come last.
  12310. When LOCAL is non-nil, only return tags from the current headline,
  12311. ignore inherited ones."
  12312. (interactive)
  12313. (if (and org-trust-scanner-tags
  12314. (or (not pos) (equal pos (point)))
  12315. (not local))
  12316. org-scanner-tags
  12317. (let (tags ltags lastpos parent)
  12318. (save-excursion
  12319. (save-restriction
  12320. (widen)
  12321. (goto-char (or pos (point)))
  12322. (save-match-data
  12323. (catch 'done
  12324. (condition-case nil
  12325. (progn
  12326. (org-back-to-heading t)
  12327. (while (not (equal lastpos (point)))
  12328. (setq lastpos (point))
  12329. (when (looking-at
  12330. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12331. (setq ltags (org-split-string
  12332. (org-match-string-no-properties 1) ":"))
  12333. (when parent
  12334. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12335. (setq tags (append
  12336. (if parent
  12337. (org-remove-uninherited-tags ltags)
  12338. ltags)
  12339. tags)))
  12340. (or org-use-tag-inheritance (throw 'done t))
  12341. (if local (throw 'done t))
  12342. (or (org-up-heading-safe) (error nil))
  12343. (setq parent t)))
  12344. (error nil)))))
  12345. (if local
  12346. tags
  12347. (reverse (delete-dups
  12348. (reverse (append
  12349. (org-remove-uninherited-tags
  12350. org-file-tags) tags)))))))))
  12351. (defun org-add-prop-inherited (s)
  12352. (add-text-properties 0 (length s) '(inherited t) s)
  12353. s)
  12354. (defun org-toggle-tag (tag &optional onoff)
  12355. "Toggle the tag TAG for the current line.
  12356. If ONOFF is `on' or `off', don't toggle but set to this state."
  12357. (let (res current)
  12358. (save-excursion
  12359. (org-back-to-heading t)
  12360. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12361. (point-at-eol) t)
  12362. (progn
  12363. (setq current (match-string 1))
  12364. (replace-match ""))
  12365. (setq current ""))
  12366. (setq current (nreverse (org-split-string current ":")))
  12367. (cond
  12368. ((eq onoff 'on)
  12369. (setq res t)
  12370. (or (member tag current) (push tag current)))
  12371. ((eq onoff 'off)
  12372. (or (not (member tag current)) (setq current (delete tag current))))
  12373. (t (if (member tag current)
  12374. (setq current (delete tag current))
  12375. (setq res t)
  12376. (push tag current))))
  12377. (end-of-line 1)
  12378. (if current
  12379. (progn
  12380. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12381. (org-set-tags nil t))
  12382. (delete-horizontal-space))
  12383. (run-hooks 'org-after-tags-change-hook))
  12384. res))
  12385. (defun org-align-tags-here (to-col)
  12386. ;; Assumes that this is a headline
  12387. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12388. (beginning-of-line 1)
  12389. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12390. (< pos (match-beginning 2)))
  12391. (progn
  12392. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12393. (goto-char (match-beginning 1))
  12394. (insert " ")
  12395. (delete-region (point) (1+ (match-beginning 2)))
  12396. (setq ncol (max (current-column)
  12397. (1+ col)
  12398. (if (> to-col 0)
  12399. to-col
  12400. (- (abs to-col) tags-l))))
  12401. (setq p (point))
  12402. (insert (make-string (- ncol (current-column)) ?\ ))
  12403. (setq ncol (current-column))
  12404. (when indent-tabs-mode (tabify p (point-at-eol)))
  12405. (org-move-to-column (min ncol col) t))
  12406. (goto-char pos))))
  12407. (defun org-set-tags-command (&optional arg just-align)
  12408. "Call the set-tags command for the current entry."
  12409. (interactive "P")
  12410. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12411. (org-set-tags arg just-align)
  12412. (save-excursion
  12413. (unless (and (org-region-active-p)
  12414. org-loop-over-headlines-in-active-region)
  12415. (org-back-to-heading t))
  12416. (org-set-tags arg just-align))))
  12417. (defun org-set-tags-to (data)
  12418. "Set the tags of the current entry to DATA, replacing the current tags.
  12419. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12420. If DATA is nil or the empty string, any tags will be removed."
  12421. (interactive "sTags: ")
  12422. (setq data
  12423. (cond
  12424. ((eq data nil) "")
  12425. ((equal data "") "")
  12426. ((stringp data)
  12427. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12428. ":"))
  12429. ((listp data)
  12430. (concat ":" (mapconcat 'identity data ":") ":"))))
  12431. (when data
  12432. (save-excursion
  12433. (org-back-to-heading t)
  12434. (when (looking-at org-complex-heading-regexp)
  12435. (if (match-end 5)
  12436. (progn
  12437. (goto-char (match-beginning 5))
  12438. (insert data)
  12439. (delete-region (point) (point-at-eol))
  12440. (org-set-tags nil 'align))
  12441. (goto-char (point-at-eol))
  12442. (insert " " data)
  12443. (org-set-tags nil 'align)))
  12444. (beginning-of-line 1)
  12445. (if (looking-at ".*?\\([ \t]+\\)$")
  12446. (delete-region (match-beginning 1) (match-end 1))))))
  12447. (defun org-align-all-tags ()
  12448. "Align the tags i all headings."
  12449. (interactive)
  12450. (save-excursion
  12451. (or (ignore-errors (org-back-to-heading t))
  12452. (outline-next-heading))
  12453. (if (org-at-heading-p)
  12454. (org-set-tags t)
  12455. (message "No headings"))))
  12456. (defvar org-indent-indentation-per-level)
  12457. (defun org-set-tags (&optional arg just-align)
  12458. "Set the tags for the current headline.
  12459. With prefix ARG, realign all tags in headings in the current buffer."
  12460. (interactive "P")
  12461. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12462. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12463. 'region-start-level 'region))
  12464. org-loop-over-headlines-in-active-region)
  12465. (org-map-entries
  12466. ;; We don't use ARG and JUST-ALIGN here these args are not
  12467. ;; useful when looping over headlines
  12468. `(org-set-tags)
  12469. org-loop-over-headlines-in-active-region
  12470. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12471. (let* ((re org-outline-regexp-bol)
  12472. (current (unless arg (org-get-tags-string)))
  12473. (col (current-column))
  12474. (org-setting-tags t)
  12475. table current-tags inherited-tags ; computed below when needed
  12476. tags p0 c0 c1 rpl di tc level)
  12477. (if arg
  12478. (save-excursion
  12479. (goto-char (point-min))
  12480. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12481. (while (re-search-forward re nil t)
  12482. (org-set-tags nil t)
  12483. (end-of-line 1)))
  12484. (message "All tags realigned to column %d" org-tags-column))
  12485. (if just-align
  12486. (setq tags current)
  12487. ;; Get a new set of tags from the user
  12488. (save-excursion
  12489. (setq table (append org-tag-persistent-alist
  12490. (or org-tag-alist (org-get-buffer-tags))
  12491. (and
  12492. org-complete-tags-always-offer-all-agenda-tags
  12493. (org-global-tags-completion-table
  12494. (org-agenda-files))))
  12495. org-last-tags-completion-table table
  12496. current-tags (org-split-string current ":")
  12497. inherited-tags (nreverse
  12498. (nthcdr (length current-tags)
  12499. (nreverse (org-get-tags-at))))
  12500. tags
  12501. (if (or (eq t org-use-fast-tag-selection)
  12502. (and org-use-fast-tag-selection
  12503. (delq nil (mapcar 'cdr table))))
  12504. (org-fast-tag-selection
  12505. current-tags inherited-tags table
  12506. (if org-fast-tag-selection-include-todo
  12507. org-todo-key-alist))
  12508. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12509. (org-trim
  12510. (org-icompleting-read "Tags: "
  12511. 'org-tags-completion-function
  12512. nil nil current 'org-tags-history))))))
  12513. (while (string-match "[-+&]+" tags)
  12514. ;; No boolean logic, just a list
  12515. (setq tags (replace-match ":" t t tags))))
  12516. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12517. (if org-tags-sort-function
  12518. (setq tags (mapconcat 'identity
  12519. (sort (org-split-string
  12520. tags (org-re "[^[:alnum:]_@#%]+"))
  12521. org-tags-sort-function) ":")))
  12522. (if (string-match "\\`[\t ]*\\'" tags)
  12523. (setq tags "")
  12524. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12525. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12526. ;; Insert new tags at the correct column
  12527. (beginning-of-line 1)
  12528. (setq level (or (and (looking-at org-outline-regexp)
  12529. (- (match-end 0) (point) 1))
  12530. 1))
  12531. (cond
  12532. ((and (equal current "") (equal tags "")))
  12533. ((re-search-forward
  12534. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12535. (point-at-eol) t)
  12536. (if (equal tags "")
  12537. (setq rpl "")
  12538. (goto-char (match-beginning 0))
  12539. (setq c0 (current-column)
  12540. ;; compute offset for the case of org-indent-mode active
  12541. di (if org-indent-mode
  12542. (* (1- org-indent-indentation-per-level) (1- level))
  12543. 0)
  12544. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12545. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12546. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12547. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12548. (replace-match rpl t t)
  12549. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12550. tags)
  12551. (t (error "Tags alignment failed")))
  12552. (org-move-to-column col)
  12553. (unless just-align
  12554. (run-hooks 'org-after-tags-change-hook))))))
  12555. (defun org-change-tag-in-region (beg end tag off)
  12556. "Add or remove TAG for each entry in the region.
  12557. This works in the agenda, and also in an org-mode buffer."
  12558. (interactive
  12559. (list (region-beginning) (region-end)
  12560. (let ((org-last-tags-completion-table
  12561. (if (derived-mode-p 'org-mode)
  12562. (org-get-buffer-tags)
  12563. (org-global-tags-completion-table))))
  12564. (org-icompleting-read
  12565. "Tag: " 'org-tags-completion-function nil nil nil
  12566. 'org-tags-history))
  12567. (progn
  12568. (message "[s]et or [r]emove? ")
  12569. (equal (read-char-exclusive) ?r))))
  12570. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12571. (let ((agendap (equal major-mode 'org-agenda-mode))
  12572. l1 l2 m buf pos newhead (cnt 0))
  12573. (goto-char end)
  12574. (setq l2 (1- (org-current-line)))
  12575. (goto-char beg)
  12576. (setq l1 (org-current-line))
  12577. (loop for l from l1 to l2 do
  12578. (org-goto-line l)
  12579. (setq m (get-text-property (point) 'org-hd-marker))
  12580. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12581. (and agendap m))
  12582. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12583. pos (if agendap m (point)))
  12584. (with-current-buffer buf
  12585. (save-excursion
  12586. (save-restriction
  12587. (goto-char pos)
  12588. (setq cnt (1+ cnt))
  12589. (org-toggle-tag tag (if off 'off 'on))
  12590. (setq newhead (org-get-heading)))))
  12591. (and agendap (org-agenda-change-all-lines newhead m))))
  12592. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12593. (defun org-tags-completion-function (string predicate &optional flag)
  12594. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12595. (confirm (lambda (x) (stringp (car x)))))
  12596. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12597. (setq s1 (match-string 1 string)
  12598. s2 (match-string 2 string))
  12599. (setq s1 "" s2 string))
  12600. (cond
  12601. ((eq flag nil)
  12602. ;; try completion
  12603. (setq rtn (try-completion s2 ctable confirm))
  12604. (if (stringp rtn)
  12605. (setq rtn
  12606. (concat s1 s2 (substring rtn (length s2))
  12607. (if (and org-add-colon-after-tag-completion
  12608. (assoc rtn ctable))
  12609. ":" ""))))
  12610. rtn)
  12611. ((eq flag t)
  12612. ;; all-completions
  12613. (all-completions s2 ctable confirm)
  12614. )
  12615. ((eq flag 'lambda)
  12616. ;; exact match?
  12617. (assoc s2 ctable)))
  12618. ))
  12619. (defun org-fast-tag-insert (kwd tags face &optional end)
  12620. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12621. (insert (format "%-12s" (concat kwd ":"))
  12622. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12623. (or end "")))
  12624. (defun org-fast-tag-show-exit (flag)
  12625. (save-excursion
  12626. (org-goto-line 3)
  12627. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12628. (replace-match ""))
  12629. (when flag
  12630. (end-of-line 1)
  12631. (org-move-to-column (- (window-width) 19) t)
  12632. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12633. (defun org-set-current-tags-overlay (current prefix)
  12634. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12635. (if (featurep 'xemacs)
  12636. (org-overlay-display org-tags-overlay (concat prefix s)
  12637. 'secondary-selection)
  12638. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12639. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12640. (defvar org-last-tag-selection-key nil)
  12641. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12642. "Fast tag selection with single keys.
  12643. CURRENT is the current list of tags in the headline, INHERITED is the
  12644. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12645. possibly with grouping information. TODO-TABLE is a similar table with
  12646. TODO keywords, should these have keys assigned to them.
  12647. If the keys are nil, a-z are automatically assigned.
  12648. Returns the new tags string, or nil to not change the current settings."
  12649. (let* ((fulltable (append table todo-table))
  12650. (maxlen (apply 'max (mapcar
  12651. (lambda (x)
  12652. (if (stringp (car x)) (string-width (car x)) 0))
  12653. fulltable)))
  12654. (buf (current-buffer))
  12655. (expert (eq org-fast-tag-selection-single-key 'expert))
  12656. (buffer-tags nil)
  12657. (fwidth (+ maxlen 3 1 3))
  12658. (ncol (/ (- (window-width) 4) fwidth))
  12659. (i-face 'org-done)
  12660. (c-face 'org-todo)
  12661. tg cnt e c char c1 c2 ntable tbl rtn
  12662. ov-start ov-end ov-prefix
  12663. (exit-after-next org-fast-tag-selection-single-key)
  12664. (done-keywords org-done-keywords)
  12665. groups ingroup)
  12666. (save-excursion
  12667. (beginning-of-line 1)
  12668. (if (looking-at
  12669. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12670. (setq ov-start (match-beginning 1)
  12671. ov-end (match-end 1)
  12672. ov-prefix "")
  12673. (setq ov-start (1- (point-at-eol))
  12674. ov-end (1+ ov-start))
  12675. (skip-chars-forward "^\n\r")
  12676. (setq ov-prefix
  12677. (concat
  12678. (buffer-substring (1- (point)) (point))
  12679. (if (> (current-column) org-tags-column)
  12680. " "
  12681. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12682. (move-overlay org-tags-overlay ov-start ov-end)
  12683. (save-window-excursion
  12684. (if expert
  12685. (set-buffer (get-buffer-create " *Org tags*"))
  12686. (delete-other-windows)
  12687. (split-window-vertically)
  12688. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12689. (erase-buffer)
  12690. (org-set-local 'org-done-keywords done-keywords)
  12691. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12692. (org-fast-tag-insert "Current" current c-face "\n\n")
  12693. (org-fast-tag-show-exit exit-after-next)
  12694. (org-set-current-tags-overlay current ov-prefix)
  12695. (setq tbl fulltable char ?a cnt 0)
  12696. (while (setq e (pop tbl))
  12697. (cond
  12698. ((equal (car e) :startgroup)
  12699. (push '() groups) (setq ingroup t)
  12700. (when (not (= cnt 0))
  12701. (setq cnt 0)
  12702. (insert "\n"))
  12703. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12704. ((equal (car e) :endgroup)
  12705. (setq ingroup nil cnt 0)
  12706. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12707. ((equal e '(:newline))
  12708. (when (not (= cnt 0))
  12709. (setq cnt 0)
  12710. (insert "\n")
  12711. (setq e (car tbl))
  12712. (while (equal (car tbl) '(:newline))
  12713. (insert "\n")
  12714. (setq tbl (cdr tbl)))))
  12715. (t
  12716. (setq tg (copy-sequence (car e)) c2 nil)
  12717. (if (cdr e)
  12718. (setq c (cdr e))
  12719. ;; automatically assign a character.
  12720. (setq c1 (string-to-char
  12721. (downcase (substring
  12722. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12723. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12724. (while (or (rassoc char ntable) (rassoc char table))
  12725. (setq char (1+ char)))
  12726. (setq c2 c1))
  12727. (setq c (or c2 char)))
  12728. (if ingroup (push tg (car groups)))
  12729. (setq tg (org-add-props tg nil 'face
  12730. (cond
  12731. ((not (assoc tg table))
  12732. (org-get-todo-face tg))
  12733. ((member tg current) c-face)
  12734. ((member tg inherited) i-face))))
  12735. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12736. (insert "[" c "] " tg (make-string
  12737. (- fwidth 4 (length tg)) ?\ ))
  12738. (push (cons tg c) ntable)
  12739. (when (= (setq cnt (1+ cnt)) ncol)
  12740. (insert "\n")
  12741. (if ingroup (insert " "))
  12742. (setq cnt 0)))))
  12743. (setq ntable (nreverse ntable))
  12744. (insert "\n")
  12745. (goto-char (point-min))
  12746. (if (not expert) (org-fit-window-to-buffer))
  12747. (setq rtn
  12748. (catch 'exit
  12749. (while t
  12750. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12751. (if (not groups) "no " "")
  12752. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12753. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12754. (setq org-last-tag-selection-key c)
  12755. (cond
  12756. ((= c ?\r) (throw 'exit t))
  12757. ((= c ?!)
  12758. (setq groups (not groups))
  12759. (goto-char (point-min))
  12760. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12761. ((= c ?\C-c)
  12762. (if (not expert)
  12763. (org-fast-tag-show-exit
  12764. (setq exit-after-next (not exit-after-next)))
  12765. (setq expert nil)
  12766. (delete-other-windows)
  12767. (set-window-buffer (split-window-vertically) " *Org tags*")
  12768. (org-switch-to-buffer-other-window " *Org tags*")
  12769. (org-fit-window-to-buffer)))
  12770. ((or (= c ?\C-g)
  12771. (and (= c ?q) (not (rassoc c ntable))))
  12772. (org-detach-overlay org-tags-overlay)
  12773. (setq quit-flag t))
  12774. ((= c ?\ )
  12775. (setq current nil)
  12776. (if exit-after-next (setq exit-after-next 'now)))
  12777. ((= c ?\t)
  12778. (condition-case nil
  12779. (setq tg (org-icompleting-read
  12780. "Tag: "
  12781. (or buffer-tags
  12782. (with-current-buffer buf
  12783. (org-get-buffer-tags)))))
  12784. (quit (setq tg "")))
  12785. (when (string-match "\\S-" tg)
  12786. (add-to-list 'buffer-tags (list tg))
  12787. (if (member tg current)
  12788. (setq current (delete tg current))
  12789. (push tg current)))
  12790. (if exit-after-next (setq exit-after-next 'now)))
  12791. ((setq e (rassoc c todo-table) tg (car e))
  12792. (with-current-buffer buf
  12793. (save-excursion (org-todo tg)))
  12794. (if exit-after-next (setq exit-after-next 'now)))
  12795. ((setq e (rassoc c ntable) tg (car e))
  12796. (if (member tg current)
  12797. (setq current (delete tg current))
  12798. (loop for g in groups do
  12799. (if (member tg g)
  12800. (mapc (lambda (x)
  12801. (setq current (delete x current)))
  12802. g)))
  12803. (push tg current))
  12804. (if exit-after-next (setq exit-after-next 'now))))
  12805. ;; Create a sorted list
  12806. (setq current
  12807. (sort current
  12808. (lambda (a b)
  12809. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12810. (if (eq exit-after-next 'now) (throw 'exit t))
  12811. (goto-char (point-min))
  12812. (beginning-of-line 2)
  12813. (delete-region (point) (point-at-eol))
  12814. (org-fast-tag-insert "Current" current c-face)
  12815. (org-set-current-tags-overlay current ov-prefix)
  12816. (while (re-search-forward
  12817. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12818. (setq tg (match-string 1))
  12819. (add-text-properties
  12820. (match-beginning 1) (match-end 1)
  12821. (list 'face
  12822. (cond
  12823. ((member tg current) c-face)
  12824. ((member tg inherited) i-face)
  12825. (t (get-text-property (match-beginning 1) 'face))))))
  12826. (goto-char (point-min)))))
  12827. (org-detach-overlay org-tags-overlay)
  12828. (if rtn
  12829. (mapconcat 'identity current ":")
  12830. nil))))
  12831. (defun org-get-tags-string ()
  12832. "Get the TAGS string in the current headline."
  12833. (unless (org-at-heading-p t)
  12834. (error "Not on a heading"))
  12835. (save-excursion
  12836. (beginning-of-line 1)
  12837. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12838. (org-match-string-no-properties 1)
  12839. "")))
  12840. (defun org-get-tags ()
  12841. "Get the list of tags specified in the current headline."
  12842. (org-split-string (org-get-tags-string) ":"))
  12843. (defun org-get-buffer-tags ()
  12844. "Get a table of all tags used in the buffer, for completion."
  12845. (let (tags)
  12846. (save-excursion
  12847. (goto-char (point-min))
  12848. (while (re-search-forward
  12849. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12850. (when (equal (char-after (point-at-bol 0)) ?*)
  12851. (mapc (lambda (x) (add-to-list 'tags x))
  12852. (org-split-string (org-match-string-no-properties 1) ":")))))
  12853. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12854. (mapcar 'list tags)))
  12855. ;;;; The mapping API
  12856. (defun org-map-entries (func &optional match scope &rest skip)
  12857. "Call FUNC at each headline selected by MATCH in SCOPE.
  12858. FUNC is a function or a lisp form. The function will be called without
  12859. arguments, with the cursor positioned at the beginning of the headline.
  12860. The return values of all calls to the function will be collected and
  12861. returned as a list.
  12862. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12863. does not need to preserve point. After evaluation, the cursor will be
  12864. moved to the end of the line (presumably of the headline of the
  12865. processed entry) and search continues from there. Under some
  12866. circumstances, this may not produce the wanted results. For example,
  12867. if you have removed (e.g. archived) the current (sub)tree it could
  12868. mean that the next entry will be skipped entirely. In such cases, you
  12869. can specify the position from where search should continue by making
  12870. FUNC set the variable `org-map-continue-from' to the desired buffer
  12871. position.
  12872. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12873. Only headlines that are matched by this query will be considered during
  12874. the iteration. When MATCH is nil or t, all headlines will be
  12875. visited by the iteration.
  12876. SCOPE determines the scope of this command. It can be any of:
  12877. nil The current buffer, respecting the restriction if any
  12878. tree The subtree started with the entry at point
  12879. region The entries within the active region, if any
  12880. region-start-level
  12881. The entries within the active region, but only those at
  12882. the same level than the first one.
  12883. file The current buffer, without restriction
  12884. file-with-archives
  12885. The current buffer, and any archives associated with it
  12886. agenda All agenda files
  12887. agenda-with-archives
  12888. All agenda files with any archive files associated with them
  12889. \(file1 file2 ...)
  12890. If this is a list, all files in the list will be scanned
  12891. The remaining args are treated as settings for the skipping facilities of
  12892. the scanner. The following items can be given here:
  12893. archive skip trees with the archive tag.
  12894. comment skip trees with the COMMENT keyword
  12895. function or Emacs Lisp form:
  12896. will be used as value for `org-agenda-skip-function', so whenever
  12897. the function returns t, FUNC will not be called for that
  12898. entry and search will continue from the point where the
  12899. function leaves it.
  12900. If your function needs to retrieve the tags including inherited tags
  12901. at the *current* entry, you can use the value of the variable
  12902. `org-scanner-tags' which will be much faster than getting the value
  12903. with `org-get-tags-at'. If your function gets properties with
  12904. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12905. to t around the call to `org-entry-properties' to get the same speedup.
  12906. Note that if your function moves around to retrieve tags and properties at
  12907. a *different* entry, you cannot use these techniques."
  12908. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12909. (not (org-region-active-p)))
  12910. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12911. (org-agenda-skip-archived-trees (memq 'archive skip))
  12912. (org-agenda-skip-comment-trees (memq 'comment skip))
  12913. (org-agenda-skip-function
  12914. (car (org-delete-all '(comment archive) skip)))
  12915. (org-tags-match-list-sublevels t)
  12916. (start-level (eq scope 'region-start-level))
  12917. matcher file res
  12918. org-todo-keywords-for-agenda
  12919. org-done-keywords-for-agenda
  12920. org-todo-keyword-alist-for-agenda
  12921. org-drawers-for-agenda
  12922. org-tag-alist-for-agenda
  12923. todo-only)
  12924. (cond
  12925. ((eq match t) (setq matcher t))
  12926. ((eq match nil) (setq matcher t))
  12927. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12928. (save-excursion
  12929. (save-restriction
  12930. (cond ((eq scope 'tree)
  12931. (org-back-to-heading t)
  12932. (org-narrow-to-subtree)
  12933. (setq scope nil))
  12934. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12935. (org-region-active-p))
  12936. ;; If needed, set start-level to a string like "2"
  12937. (when start-level
  12938. (save-excursion
  12939. (goto-char (region-beginning))
  12940. (unless (org-at-heading-p) (outline-next-heading))
  12941. (setq start-level (org-current-level))))
  12942. (narrow-to-region (region-beginning)
  12943. (save-excursion
  12944. (goto-char (region-end))
  12945. (unless (and (bolp) (org-at-heading-p))
  12946. (outline-next-heading))
  12947. (point)))
  12948. (setq scope nil)))
  12949. (if (not scope)
  12950. (progn
  12951. (org-agenda-prepare-buffers
  12952. (list (buffer-file-name (current-buffer))))
  12953. (setq res (org-scan-tags func matcher todo-only start-level)))
  12954. ;; Get the right scope
  12955. (cond
  12956. ((and scope (listp scope) (symbolp (car scope)))
  12957. (setq scope (eval scope)))
  12958. ((eq scope 'agenda)
  12959. (setq scope (org-agenda-files t)))
  12960. ((eq scope 'agenda-with-archives)
  12961. (setq scope (org-agenda-files t))
  12962. (setq scope (org-add-archive-files scope)))
  12963. ((eq scope 'file)
  12964. (setq scope (list (buffer-file-name))))
  12965. ((eq scope 'file-with-archives)
  12966. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12967. (org-agenda-prepare-buffers scope)
  12968. (while (setq file (pop scope))
  12969. (with-current-buffer (org-find-base-buffer-visiting file)
  12970. (save-excursion
  12971. (save-restriction
  12972. (widen)
  12973. (goto-char (point-min))
  12974. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  12975. res)))
  12976. ;;;; Properties
  12977. ;;; Setting and retrieving properties
  12978. (defconst org-special-properties
  12979. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12980. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  12981. "The special properties valid in Org-mode.
  12982. These are properties that are not defined in the property drawer,
  12983. but in some other way.")
  12984. (defconst org-default-properties
  12985. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12986. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12987. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12988. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12989. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12990. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12991. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12992. "Some properties that are used by Org-mode for various purposes.
  12993. Being in this list makes sure that they are offered for completion.")
  12994. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12995. "Regular expression matching the first line of a property drawer.")
  12996. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12997. "Regular expression matching the last line of a property drawer.")
  12998. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12999. "Regular expression matching the first line of a property drawer.")
  13000. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13001. "Regular expression matching the first line of a property drawer.")
  13002. (defconst org-property-drawer-re
  13003. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13004. org-property-end-re "\\)\n?")
  13005. "Matches an entire property drawer.")
  13006. (defconst org-clock-drawer-re
  13007. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13008. org-property-end-re "\\)\n?")
  13009. "Matches an entire clock drawer.")
  13010. (defsubst org-re-property (property)
  13011. "Return a regexp matching a PROPERTY line.
  13012. Match group 1 will be set to the value."
  13013. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13014. (defsubst org-re-property-keyword (property)
  13015. "Return a regexp matching a PROPERTY line, possibly with no
  13016. value for the property."
  13017. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13018. (defun org-property-action ()
  13019. "Do an action on properties."
  13020. (interactive)
  13021. (let (c)
  13022. (org-at-property-p)
  13023. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13024. (setq c (read-char-exclusive))
  13025. (cond
  13026. ((equal c ?s)
  13027. (call-interactively 'org-set-property))
  13028. ((equal c ?d)
  13029. (call-interactively 'org-delete-property))
  13030. ((equal c ?D)
  13031. (call-interactively 'org-delete-property-globally))
  13032. ((equal c ?c)
  13033. (call-interactively 'org-compute-property-at-point))
  13034. (t (error "No such property action %c" c)))))
  13035. (defun org-inc-effort ()
  13036. "Increment the value of the effort property in the current entry."
  13037. (interactive)
  13038. (org-set-effort nil t))
  13039. (defun org-set-effort (&optional value increment)
  13040. "Set the effort property of the current entry.
  13041. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13042. 10th allowed value.
  13043. When INCREMENT is non-nil, set the property to the next allowed value."
  13044. (interactive "P")
  13045. (if (equal value 0) (setq value 10))
  13046. (let* ((completion-ignore-case t)
  13047. (prop org-effort-property)
  13048. (cur (org-entry-get nil prop))
  13049. (allowed (org-property-get-allowed-values nil prop 'table))
  13050. (existing (mapcar 'list (org-property-values prop)))
  13051. rpl
  13052. (val (cond
  13053. ((stringp value) value)
  13054. ((and allowed (integerp value))
  13055. (or (car (nth (1- value) allowed))
  13056. (car (org-last allowed))))
  13057. ((and allowed increment)
  13058. (or (caadr (member (list cur) allowed))
  13059. (error "Allowed effort values are not set")))
  13060. (allowed
  13061. (message "Select 1-9,0, [RET%s]: %s"
  13062. (if cur (concat "=" cur) "")
  13063. (mapconcat 'car allowed " "))
  13064. (setq rpl (read-char-exclusive))
  13065. (if (equal rpl ?\r)
  13066. cur
  13067. (setq rpl (- rpl ?0))
  13068. (if (equal rpl 0) (setq rpl 10))
  13069. (if (and (> rpl 0) (<= rpl (length allowed)))
  13070. (car (nth (1- rpl) allowed))
  13071. (org-completing-read "Effort: " allowed nil))))
  13072. (t
  13073. (let (org-completion-use-ido org-completion-use-iswitchb)
  13074. (org-completing-read
  13075. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13076. (concat "[" cur "]") "")
  13077. ": ")
  13078. existing nil nil "" nil cur))))))
  13079. (unless (equal (org-entry-get nil prop) val)
  13080. (org-entry-put nil prop val))
  13081. (save-excursion
  13082. (org-back-to-heading t)
  13083. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13084. (message "%s is now %s" prop val)))
  13085. (defun org-at-property-p ()
  13086. "Is cursor inside a property drawer?"
  13087. (save-excursion
  13088. (beginning-of-line 1)
  13089. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13090. (save-match-data ;; Used by calling procedures
  13091. (let ((p (point))
  13092. (range (unless (org-before-first-heading-p)
  13093. (org-get-property-block))))
  13094. (and range (<= (car range) p) (< p (cdr range))))))))
  13095. (defun org-get-property-block (&optional beg end force)
  13096. "Return the (beg . end) range of the body of the property drawer.
  13097. BEG and END are the beginning and end of the current subtree, or of
  13098. the part before the first headline. If they are not given, they will
  13099. be found. If the drawer does not exist and FORCE is non-nil, create
  13100. the drawer."
  13101. (catch 'exit
  13102. (save-excursion
  13103. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13104. (progn (org-back-to-heading t) (point))))
  13105. (end (or end (and (not (outline-next-heading)) (point-max))
  13106. (point))))
  13107. (goto-char beg)
  13108. (if (re-search-forward org-property-start-re end t)
  13109. (setq beg (1+ (match-end 0)))
  13110. (if force
  13111. (save-excursion
  13112. (org-insert-property-drawer)
  13113. (setq end (progn (outline-next-heading) (point))))
  13114. (throw 'exit nil))
  13115. (goto-char beg)
  13116. (if (re-search-forward org-property-start-re end t)
  13117. (setq beg (1+ (match-end 0)))))
  13118. (if (re-search-forward org-property-end-re end t)
  13119. (setq end (match-beginning 0))
  13120. (or force (throw 'exit nil))
  13121. (goto-char beg)
  13122. (setq end beg)
  13123. (org-indent-line)
  13124. (insert ":END:\n"))
  13125. (cons beg end)))))
  13126. (defun org-entry-properties (&optional pom which specific)
  13127. "Get all properties of the entry at point-or-marker POM.
  13128. This includes the TODO keyword, the tags, time strings for deadline,
  13129. scheduled, and clocking, and any additional properties defined in the
  13130. entry. The return value is an alist, keys may occur multiple times
  13131. if the property key was used several times.
  13132. POM may also be nil, in which case the current entry is used.
  13133. If WHICH is nil or `all', get all properties. If WHICH is
  13134. `special' or `standard', only get that subclass. If WHICH
  13135. is a string only get exactly this property. SPECIFIC can be a string, the
  13136. specific property we are interested in. Specifying it can speed
  13137. things up because then unnecessary parsing is avoided."
  13138. (setq which (or which 'all))
  13139. (org-with-point-at pom
  13140. (let ((clockstr (substring org-clock-string 0 -1))
  13141. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13142. (case-fold-search nil)
  13143. beg end range props sum-props key key1 value string clocksum clocksumt)
  13144. (save-excursion
  13145. (when (condition-case nil
  13146. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13147. (error nil))
  13148. (setq beg (point))
  13149. (setq sum-props (get-text-property (point) 'org-summaries))
  13150. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13151. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13152. (outline-next-heading)
  13153. (setq end (point))
  13154. (when (memq which '(all special))
  13155. ;; Get the special properties, like TODO and tags
  13156. (goto-char beg)
  13157. (when (and (or (not specific) (string= specific "TODO"))
  13158. (looking-at org-todo-line-regexp) (match-end 2))
  13159. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13160. (when (and (or (not specific) (string= specific "PRIORITY"))
  13161. (looking-at org-priority-regexp))
  13162. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13163. (when (or (not specific) (string= specific "FILE"))
  13164. (push (cons "FILE" buffer-file-name) props))
  13165. (when (and (or (not specific) (string= specific "TAGS"))
  13166. (setq value (org-get-tags-string))
  13167. (string-match "\\S-" value))
  13168. (push (cons "TAGS" value) props))
  13169. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13170. (setq value (org-get-tags-at)))
  13171. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13172. ":"))
  13173. props))
  13174. (when (or (not specific) (string= specific "BLOCKED"))
  13175. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13176. (when (or (not specific)
  13177. (member specific
  13178. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13179. "TIMESTAMP" "TIMESTAMP_IA")))
  13180. (catch 'match
  13181. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13182. (setq key (if (match-end 1)
  13183. (substring (org-match-string-no-properties 1)
  13184. 0 -1))
  13185. string (if (equal key clockstr)
  13186. (org-trim
  13187. (buffer-substring-no-properties
  13188. (match-beginning 3) (goto-char
  13189. (point-at-eol))))
  13190. (substring (org-match-string-no-properties 3)
  13191. 1 -1)))
  13192. ;; Get the correct property name from the key. This is
  13193. ;; necessary if the user has configured time keywords.
  13194. (setq key1 (concat key ":"))
  13195. (cond
  13196. ((not key)
  13197. (setq key
  13198. (if (= (char-after (match-beginning 3)) ?\[)
  13199. "TIMESTAMP_IA" "TIMESTAMP")))
  13200. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13201. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13202. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13203. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13204. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13205. (progn
  13206. (push (cons key string) props)
  13207. ;; no need to search further if match is found
  13208. (throw 'match t))
  13209. (when (or (equal key "CLOCK") (not (assoc key props)))
  13210. (push (cons key string) props)))))))
  13211. (when (memq which '(all standard))
  13212. ;; Get the standard properties, like :PROP: ...
  13213. (setq range (org-get-property-block beg end))
  13214. (when range
  13215. (goto-char (car range))
  13216. (while (re-search-forward
  13217. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13218. (cdr range) t)
  13219. (setq key (org-match-string-no-properties 1)
  13220. value (org-trim (or (org-match-string-no-properties 2) "")))
  13221. (unless (member key excluded)
  13222. (push (cons key (or value "")) props)))))
  13223. (if clocksum
  13224. (push (cons "CLOCKSUM"
  13225. (org-columns-number-to-string (/ (float clocksum) 60.)
  13226. 'add_times))
  13227. props))
  13228. (if clocksumt
  13229. (push (cons "CLOCKSUM_T"
  13230. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13231. 'add_times))
  13232. props))
  13233. (unless (assoc "CATEGORY" props)
  13234. (push (cons "CATEGORY" (org-get-category)) props))
  13235. (append sum-props (nreverse props)))))))
  13236. (defun org-entry-get (pom property &optional inherit literal-nil)
  13237. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13238. If INHERIT is non-nil and the entry does not have the property,
  13239. then also check higher levels of the hierarchy.
  13240. If INHERIT is the symbol `selective', use inheritance only if the setting
  13241. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13242. If the property is present but empty, the return value is the empty string.
  13243. If the property is not present at all, nil is returned.
  13244. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13245. do not interpret it as the list atom nil. This is used for inheritance
  13246. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13247. (org-with-point-at pom
  13248. (if (and inherit (if (eq inherit 'selective)
  13249. (org-property-inherit-p property)
  13250. t))
  13251. (org-entry-get-with-inheritance property literal-nil)
  13252. (if (member property org-special-properties)
  13253. ;; We need a special property. Use `org-entry-properties' to
  13254. ;; retrieve it, but specify the wanted property
  13255. (cdr (assoc property (org-entry-properties nil 'special property)))
  13256. (let ((range (org-get-property-block)))
  13257. (when (and range (not (eq (car range) (cdr range))))
  13258. (let* ((props (list (or (assoc property org-file-properties)
  13259. (assoc property org-global-properties)
  13260. (assoc property org-global-properties-fixed))))
  13261. (ap (lambda (key)
  13262. (when (re-search-forward
  13263. (org-re-property key) (cdr range) t)
  13264. (setq props
  13265. (org-update-property-plist
  13266. key
  13267. (if (match-end 1)
  13268. (org-match-string-no-properties 1) "")
  13269. props)))))
  13270. val)
  13271. (goto-char (car range))
  13272. (funcall ap property)
  13273. (goto-char (car range))
  13274. (while (funcall ap (concat property "+")))
  13275. (setq val (cdr (assoc property props)))
  13276. (when val (if literal-nil val (org-not-nil val))))))))))
  13277. (defun org-property-or-variable-value (var &optional inherit)
  13278. "Check if there is a property fixing the value of VAR.
  13279. If yes, return this value. If not, return the current value of the variable."
  13280. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13281. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13282. (read prop)
  13283. (symbol-value var))))
  13284. (defun org-entry-delete (pom property)
  13285. "Delete the property PROPERTY from entry at point-or-marker POM."
  13286. (org-with-point-at pom
  13287. (if (member property org-special-properties)
  13288. nil ; cannot delete these properties.
  13289. (let ((range (org-get-property-block)))
  13290. (if (and range
  13291. (goto-char (car range))
  13292. (re-search-forward
  13293. (org-re-property property)
  13294. (cdr range) t))
  13295. (progn
  13296. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13297. t)
  13298. nil)))))
  13299. ;; Multi-values properties are properties that contain multiple values
  13300. ;; These values are assumed to be single words, separated by whitespace.
  13301. (defun org-entry-add-to-multivalued-property (pom property value)
  13302. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13303. (let* ((old (org-entry-get pom property))
  13304. (values (and old (org-split-string old "[ \t]"))))
  13305. (setq value (org-entry-protect-space value))
  13306. (unless (member value values)
  13307. (setq values (cons value values))
  13308. (org-entry-put pom property
  13309. (mapconcat 'identity values " ")))))
  13310. (defun org-entry-remove-from-multivalued-property (pom property value)
  13311. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13312. (let* ((old (org-entry-get pom property))
  13313. (values (and old (org-split-string old "[ \t]"))))
  13314. (setq value (org-entry-protect-space value))
  13315. (when (member value values)
  13316. (setq values (delete value values))
  13317. (org-entry-put pom property
  13318. (mapconcat 'identity values " ")))))
  13319. (defun org-entry-member-in-multivalued-property (pom property value)
  13320. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13321. (let* ((old (org-entry-get pom property))
  13322. (values (and old (org-split-string old "[ \t]"))))
  13323. (setq value (org-entry-protect-space value))
  13324. (member value values)))
  13325. (defun org-entry-get-multivalued-property (pom property)
  13326. "Return a list of values in a multivalued property."
  13327. (let* ((value (org-entry-get pom property))
  13328. (values (and value (org-split-string value "[ \t]"))))
  13329. (mapcar 'org-entry-restore-space values)))
  13330. (defun org-entry-put-multivalued-property (pom property &rest values)
  13331. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13332. VALUES should be a list of strings. Spaces will be protected."
  13333. (org-entry-put pom property
  13334. (mapconcat 'org-entry-protect-space values " "))
  13335. (let* ((value (org-entry-get pom property))
  13336. (values (and value (org-split-string value "[ \t]"))))
  13337. (mapcar 'org-entry-restore-space values)))
  13338. (defun org-entry-protect-space (s)
  13339. "Protect spaces and newline in string S."
  13340. (while (string-match " " s)
  13341. (setq s (replace-match "%20" t t s)))
  13342. (while (string-match "\n" s)
  13343. (setq s (replace-match "%0A" t t s)))
  13344. s)
  13345. (defun org-entry-restore-space (s)
  13346. "Restore spaces and newline in string S."
  13347. (while (string-match "%20" s)
  13348. (setq s (replace-match " " t t s)))
  13349. (while (string-match "%0A" s)
  13350. (setq s (replace-match "\n" t t s)))
  13351. s)
  13352. (defvar org-entry-property-inherited-from (make-marker)
  13353. "Marker pointing to the entry from where a property was inherited.
  13354. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13355. location of the entry where the inheritance search matched. If there was
  13356. no match, the marker will point nowhere.
  13357. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13358. is set.")
  13359. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13360. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13361. The search will stop at the first ancestor which has the property defined.
  13362. If the value found is \"nil\", return nil to show that the property
  13363. should be considered as undefined (this is the meaning of nil here).
  13364. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13365. (move-marker org-entry-property-inherited-from nil)
  13366. (let (tmp)
  13367. (save-excursion
  13368. (save-restriction
  13369. (widen)
  13370. (catch 'ex
  13371. (while t
  13372. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13373. (or (ignore-errors (org-back-to-heading t))
  13374. (goto-char (point-min)))
  13375. (move-marker org-entry-property-inherited-from (point))
  13376. (throw 'ex tmp))
  13377. (or (ignore-errors (org-up-heading-safe))
  13378. (throw 'ex nil))))))
  13379. (setq tmp (or tmp
  13380. (cdr (assoc property org-file-properties))
  13381. (cdr (assoc property org-global-properties))
  13382. (cdr (assoc property org-global-properties-fixed))))
  13383. (if literal-nil tmp (org-not-nil tmp))))
  13384. (defvar org-property-changed-functions nil
  13385. "Hook called when the value of a property has changed.
  13386. Each hook function should accept two arguments, the name of the property
  13387. and the new value.")
  13388. (defun org-entry-put (pom property value)
  13389. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13390. (org-with-point-at pom
  13391. (org-back-to-heading t)
  13392. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13393. range)
  13394. (cond
  13395. ((equal property "TODO")
  13396. (when (and (stringp value) (string-match "\\S-" value)
  13397. (not (member value org-todo-keywords-1)))
  13398. (error "\"%s\" is not a valid TODO state" value))
  13399. (if (or (not value)
  13400. (not (string-match "\\S-" value)))
  13401. (setq value 'none))
  13402. (org-todo value)
  13403. (org-set-tags nil 'align))
  13404. ((equal property "PRIORITY")
  13405. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13406. (string-to-char value) ?\ ))
  13407. (org-set-tags nil 'align))
  13408. ((equal property "SCHEDULED")
  13409. (if (re-search-forward org-scheduled-time-regexp end t)
  13410. (cond
  13411. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13412. ((eq value 'later) (org-timestamp-change 1 'day))
  13413. (t (call-interactively 'org-schedule)))
  13414. (call-interactively 'org-schedule)))
  13415. ((equal property "DEADLINE")
  13416. (if (re-search-forward org-deadline-time-regexp end t)
  13417. (cond
  13418. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13419. ((eq value 'later) (org-timestamp-change 1 'day))
  13420. (t (call-interactively 'org-deadline)))
  13421. (call-interactively 'org-deadline)))
  13422. ((member property org-special-properties)
  13423. (error "The %s property can not yet be set with `org-entry-put'"
  13424. property))
  13425. (t ; a non-special property
  13426. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13427. (setq range (org-get-property-block beg end 'force))
  13428. (goto-char (car range))
  13429. (if (re-search-forward
  13430. (org-re-property-keyword property) (cdr range) t)
  13431. (progn
  13432. (delete-region (match-beginning 0) (match-end 0))
  13433. (goto-char (match-beginning 0)))
  13434. (goto-char (cdr range))
  13435. (insert "\n")
  13436. (backward-char 1)
  13437. (org-indent-line))
  13438. (insert ":" property ":")
  13439. (and value (insert " " value))
  13440. (org-indent-line)))))
  13441. (run-hook-with-args 'org-property-changed-functions property value)))
  13442. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13443. "Get all property keys in the current buffer.
  13444. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13445. like tags and TODO state.
  13446. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13447. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13448. and others.
  13449. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13450. formats in the current buffer."
  13451. (let (rtn range cfmt s p)
  13452. (save-excursion
  13453. (save-restriction
  13454. (widen)
  13455. (goto-char (point-min))
  13456. (while (re-search-forward org-property-start-re nil t)
  13457. (setq range (org-get-property-block))
  13458. (goto-char (car range))
  13459. (while (re-search-forward
  13460. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13461. (cdr range) t)
  13462. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13463. (outline-next-heading))))
  13464. (when include-specials
  13465. (setq rtn (append org-special-properties rtn)))
  13466. (when include-defaults
  13467. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13468. (add-to-list 'rtn org-effort-property))
  13469. (when include-columns
  13470. (save-excursion
  13471. (save-restriction
  13472. (widen)
  13473. (goto-char (point-min))
  13474. (while (re-search-forward
  13475. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13476. nil t)
  13477. (setq cfmt (match-string 2) s 0)
  13478. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13479. cfmt s)
  13480. (setq s (match-end 0)
  13481. p (match-string 1 cfmt))
  13482. (unless (or (equal p "ITEM")
  13483. (member p org-special-properties))
  13484. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13485. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13486. (defun org-property-values (key)
  13487. "Return a list of all values of property KEY in the current buffer."
  13488. (save-excursion
  13489. (save-restriction
  13490. (widen)
  13491. (goto-char (point-min))
  13492. (let ((re (org-re-property key))
  13493. values)
  13494. (while (re-search-forward re nil t)
  13495. (add-to-list 'values (org-trim (match-string 1))))
  13496. (delete "" values)))))
  13497. (defun org-insert-property-drawer ()
  13498. "Insert a property drawer into the current entry."
  13499. (org-back-to-heading t)
  13500. (looking-at org-outline-regexp)
  13501. (let ((indent (if org-adapt-indentation
  13502. (- (match-end 0) (match-beginning 0))
  13503. 0))
  13504. (beg (point))
  13505. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13506. end hiddenp)
  13507. (outline-next-heading)
  13508. (setq end (point))
  13509. (goto-char beg)
  13510. (while (re-search-forward re end t))
  13511. (setq hiddenp (outline-invisible-p))
  13512. (end-of-line 1)
  13513. (and (equal (char-after) ?\n) (forward-char 1))
  13514. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13515. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13516. ;; just skip this line
  13517. (beginning-of-line 2)
  13518. ;; Drawer start, find the end
  13519. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13520. (beginning-of-line 1)))
  13521. (org-skip-over-state-notes)
  13522. (skip-chars-backward " \t\n\r")
  13523. (if (eq (char-before) ?*) (forward-char 1))
  13524. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13525. (beginning-of-line 0)
  13526. (org-indent-to-column indent)
  13527. (beginning-of-line 2)
  13528. (org-indent-to-column indent)
  13529. (beginning-of-line 0)
  13530. (if hiddenp
  13531. (save-excursion
  13532. (org-back-to-heading t)
  13533. (hide-entry))
  13534. (org-flag-drawer t))))
  13535. (defun org-insert-drawer (&optional arg drawer)
  13536. "Insert a drawer at point.
  13537. Optional argument DRAWER, when non-nil, is a string representing
  13538. drawer's name. Otherwise, the user is prompted for a name.
  13539. If a region is active, insert the drawer around that region
  13540. instead.
  13541. Point is left between drawer's boundaries."
  13542. (interactive "P")
  13543. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13544. "LOGBOOK"))
  13545. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13546. ;; internally by Org. They are meant to be inserted
  13547. ;; automatically.
  13548. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13549. ;; Remove system drawers from list. Note: For some reason,
  13550. ;; `org-completing-read' ignores the predicate while
  13551. ;; `completing-read' handles it fine.
  13552. (drawer (if arg "PROPERTIES"
  13553. (or drawer
  13554. (completing-read
  13555. "Drawer: " org-drawers
  13556. (lambda (d) (not (member d system-drawers))))))))
  13557. (cond
  13558. ;; With C-u, fall back on `org-insert-property-drawer'
  13559. (arg (org-insert-property-drawer))
  13560. ;; With an active region, insert a drawer at point.
  13561. ((not (org-region-active-p))
  13562. (progn
  13563. (unless (bolp) (insert "\n"))
  13564. (insert (format ":%s:\n\n:END:\n" drawer))
  13565. (forward-line -2)))
  13566. ;; Otherwise, insert the drawer at point
  13567. (t
  13568. (let ((rbeg (region-beginning))
  13569. (rend (copy-marker (region-end))))
  13570. (unwind-protect
  13571. (progn
  13572. (goto-char rbeg)
  13573. (beginning-of-line)
  13574. (when (save-excursion
  13575. (re-search-forward org-outline-regexp-bol rend t))
  13576. (error "Drawers cannot contain headlines"))
  13577. ;; Position point at the beginning of the first
  13578. ;; non-blank line in region. Insert drawer's opening
  13579. ;; there, then indent it.
  13580. (org-skip-whitespace)
  13581. (beginning-of-line)
  13582. (insert ":" drawer ":\n")
  13583. (forward-line -1)
  13584. (indent-for-tab-command)
  13585. ;; Move point to the beginning of the first blank line
  13586. ;; after the last non-blank line in region. Insert
  13587. ;; drawer's closing, then indent it.
  13588. (goto-char rend)
  13589. (skip-chars-backward " \r\t\n")
  13590. (insert "\n:END:")
  13591. (deactivate-mark t)
  13592. (indent-for-tab-command)
  13593. (unless (eolp) (insert "\n")))
  13594. ;; Clear marker, whatever the outcome of insertion is.
  13595. (set-marker rend nil)))))))
  13596. (defvar org-property-set-functions-alist nil
  13597. "Property set function alist.
  13598. Each entry should have the following format:
  13599. (PROPERTY . READ-FUNCTION)
  13600. The read function will be called with the same argument as
  13601. `org-completing-read'.")
  13602. (defun org-set-property-function (property)
  13603. "Get the function that should be used to set PROPERTY.
  13604. This is computed according to `org-property-set-functions-alist'."
  13605. (or (cdr (assoc property org-property-set-functions-alist))
  13606. 'org-completing-read))
  13607. (defun org-read-property-value (property)
  13608. "Read PROPERTY value from user."
  13609. (let* ((completion-ignore-case t)
  13610. (allowed (org-property-get-allowed-values nil property 'table))
  13611. (cur (org-entry-get nil property))
  13612. (prompt (concat property " value"
  13613. (if (and cur (string-match "\\S-" cur))
  13614. (concat " [" cur "]") "") ": "))
  13615. (set-function (org-set-property-function property))
  13616. (val (if allowed
  13617. (funcall set-function prompt allowed nil
  13618. (not (get-text-property 0 'org-unrestricted
  13619. (caar allowed))))
  13620. (let (org-completion-use-ido org-completion-use-iswitchb)
  13621. (funcall set-function prompt
  13622. (mapcar 'list (org-property-values property))
  13623. nil nil "" nil cur)))))
  13624. (if (equal val "")
  13625. cur
  13626. val)))
  13627. (defvar org-last-set-property nil)
  13628. (defun org-read-property-name ()
  13629. "Read a property name."
  13630. (let* ((completion-ignore-case t)
  13631. (keys (org-buffer-property-keys nil t t))
  13632. (default-prop (or (save-excursion
  13633. (save-match-data
  13634. (beginning-of-line)
  13635. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13636. (null (string= (match-string 1) "END"))
  13637. (match-string 1))))
  13638. org-last-set-property))
  13639. (property (org-icompleting-read
  13640. (concat "Property"
  13641. (if default-prop (concat " [" default-prop "]") "")
  13642. ": ")
  13643. (mapcar 'list keys)
  13644. nil nil nil nil
  13645. default-prop
  13646. )))
  13647. (if (member property keys)
  13648. property
  13649. (or (cdr (assoc (downcase property)
  13650. (mapcar (lambda (x) (cons (downcase x) x))
  13651. keys)))
  13652. property))))
  13653. (defun org-set-property (property value)
  13654. "In the current entry, set PROPERTY to VALUE.
  13655. When called interactively, this will prompt for a property name, offering
  13656. completion on existing and default properties. And then it will prompt
  13657. for a value, offering completion either on allowed values (via an inherited
  13658. xxx_ALL property) or on existing values in other instances of this property
  13659. in the current file."
  13660. (interactive (list nil nil))
  13661. (let* ((property (or property (org-read-property-name)))
  13662. (value (or value (org-read-property-value property)))
  13663. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13664. (setq org-last-set-property property)
  13665. ;; Possibly postprocess the inserted value:
  13666. (when fn (setq value (funcall fn value)))
  13667. (unless (equal (org-entry-get nil property) value)
  13668. (org-entry-put nil property value))))
  13669. (defun org-delete-property (property)
  13670. "In the current entry, delete PROPERTY."
  13671. (interactive
  13672. (let* ((completion-ignore-case t)
  13673. (prop (org-icompleting-read "Property: "
  13674. (org-entry-properties nil 'standard))))
  13675. (list prop)))
  13676. (message "Property %s %s" property
  13677. (if (org-entry-delete nil property)
  13678. "deleted"
  13679. "was not present in the entry")))
  13680. (defun org-delete-property-globally (property)
  13681. "Remove PROPERTY globally, from all entries."
  13682. (interactive
  13683. (let* ((completion-ignore-case t)
  13684. (prop (org-icompleting-read
  13685. "Globally remove property: "
  13686. (mapcar 'list (org-buffer-property-keys)))))
  13687. (list prop)))
  13688. (save-excursion
  13689. (save-restriction
  13690. (widen)
  13691. (goto-char (point-min))
  13692. (let ((cnt 0))
  13693. (while (re-search-forward
  13694. (org-re-property property)
  13695. nil t)
  13696. (setq cnt (1+ cnt))
  13697. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13698. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13699. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13700. (defun org-compute-property-at-point ()
  13701. "Compute the property at point.
  13702. This looks for an enclosing column format, extracts the operator and
  13703. then applies it to the property in the column format's scope."
  13704. (interactive)
  13705. (unless (org-at-property-p)
  13706. (error "Not at a property"))
  13707. (let ((prop (org-match-string-no-properties 2)))
  13708. (org-columns-get-format-and-top-level)
  13709. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13710. (error "No operator defined for property %s" prop))
  13711. (org-columns-compute prop)))
  13712. (defvar org-property-allowed-value-functions nil
  13713. "Hook for functions supplying allowed values for a specific property.
  13714. The functions must take a single argument, the name of the property, and
  13715. return a flat list of allowed values. If \":ETC\" is one of
  13716. the values, this means that these values are intended as defaults for
  13717. completion, but that other values should be allowed too.
  13718. The functions must return nil if they are not responsible for this
  13719. property.")
  13720. (defun org-property-get-allowed-values (pom property &optional table)
  13721. "Get allowed values for the property PROPERTY.
  13722. When TABLE is non-nil, return an alist that can directly be used for
  13723. completion."
  13724. (let (vals)
  13725. (cond
  13726. ((equal property "TODO")
  13727. (setq vals (org-with-point-at pom
  13728. (append org-todo-keywords-1 '("")))))
  13729. ((equal property "PRIORITY")
  13730. (let ((n org-lowest-priority))
  13731. (while (>= n org-highest-priority)
  13732. (push (char-to-string n) vals)
  13733. (setq n (1- n)))))
  13734. ((member property org-special-properties))
  13735. ((setq vals (run-hook-with-args-until-success
  13736. 'org-property-allowed-value-functions property)))
  13737. (t
  13738. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13739. (when (and vals (string-match "\\S-" vals))
  13740. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13741. (setq vals (mapcar (lambda (x)
  13742. (cond ((stringp x) x)
  13743. ((numberp x) (number-to-string x))
  13744. ((symbolp x) (symbol-name x))
  13745. (t "???")))
  13746. vals)))))
  13747. (when (member ":ETC" vals)
  13748. (setq vals (remove ":ETC" vals))
  13749. (org-add-props (car vals) '(org-unrestricted t)))
  13750. (if table (mapcar 'list vals) vals)))
  13751. (defun org-property-previous-allowed-value (&optional previous)
  13752. "Switch to the next allowed value for this property."
  13753. (interactive)
  13754. (org-property-next-allowed-value t))
  13755. (defun org-property-next-allowed-value (&optional previous)
  13756. "Switch to the next allowed value for this property."
  13757. (interactive)
  13758. (unless (org-at-property-p)
  13759. (error "Not at a property"))
  13760. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  13761. (key (match-string 2))
  13762. (value (match-string 3))
  13763. (allowed (or (org-property-get-allowed-values (point) key)
  13764. (and (member value '("[ ]" "[-]" "[X]"))
  13765. '("[ ]" "[X]"))))
  13766. nval)
  13767. (unless allowed
  13768. (error "Allowed values for this property have not been defined"))
  13769. (if previous (setq allowed (reverse allowed)))
  13770. (if (member value allowed)
  13771. (setq nval (car (cdr (member value allowed)))))
  13772. (setq nval (or nval (car allowed)))
  13773. (if (equal nval value)
  13774. (error "Only one allowed value for this property"))
  13775. (org-at-property-p)
  13776. (replace-match (concat " :" key ": " nval) t t)
  13777. (org-indent-line)
  13778. (beginning-of-line 1)
  13779. (skip-chars-forward " \t")
  13780. (when (equal prop org-effort-property)
  13781. (save-excursion
  13782. (org-back-to-heading t)
  13783. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval)))
  13784. (run-hook-with-args 'org-property-changed-functions key nval)))
  13785. (defun org-find-olp (path &optional this-buffer)
  13786. "Return a marker pointing to the entry at outline path OLP.
  13787. If anything goes wrong, throw an error.
  13788. You can wrap this call to catch the error like this:
  13789. (condition-case msg
  13790. (org-mobile-locate-entry (match-string 4))
  13791. (error (nth 1 msg)))
  13792. The return value will then be either a string with the error message,
  13793. or a marker if everything is OK.
  13794. If THIS-BUFFER is set, the outline path does not contain a file,
  13795. only headings."
  13796. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13797. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13798. (level 1)
  13799. (lmin 1)
  13800. (lmax 1)
  13801. limit re end found pos heading cnt flevel)
  13802. (unless buffer (error "File not found :%s" file))
  13803. (with-current-buffer buffer
  13804. (save-excursion
  13805. (save-restriction
  13806. (widen)
  13807. (setq limit (point-max))
  13808. (goto-char (point-min))
  13809. (while (setq heading (pop path))
  13810. (setq re (format org-complex-heading-regexp-format
  13811. (regexp-quote heading)))
  13812. (setq cnt 0 pos (point))
  13813. (while (re-search-forward re end t)
  13814. (setq level (- (match-end 1) (match-beginning 1)))
  13815. (if (and (>= level lmin) (<= level lmax))
  13816. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13817. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13818. lmax heading))
  13819. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13820. lmax heading))
  13821. (goto-char found)
  13822. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13823. (setq end (save-excursion (org-end-of-subtree t t))))
  13824. (when (org-at-heading-p)
  13825. (point-marker)))))))
  13826. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13827. "Find node HEADING in BUFFER.
  13828. Return a marker to the heading if it was found, or nil if not.
  13829. If POS-ONLY is set, return just the position instead of a marker.
  13830. The heading text must match exact, but it may have a TODO keyword,
  13831. a priority cookie and tags in the standard locations."
  13832. (with-current-buffer (or buffer (current-buffer))
  13833. (save-excursion
  13834. (save-restriction
  13835. (widen)
  13836. (goto-char (point-min))
  13837. (let (case-fold-search)
  13838. (if (re-search-forward
  13839. (format org-complex-heading-regexp-format
  13840. (regexp-quote heading)) nil t)
  13841. (if pos-only
  13842. (match-beginning 0)
  13843. (move-marker (make-marker) (match-beginning 0)))))))))
  13844. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13845. "Find Org node headline HEADING in all .org files in directory DIR.
  13846. When the target headline is found, return a marker to this location."
  13847. (let ((files (directory-files (or dir default-directory)
  13848. nil "\\`[^.#].*\\.org\\'"))
  13849. file visiting m buffer)
  13850. (catch 'found
  13851. (while (setq file (pop files))
  13852. (message "trying %s" file)
  13853. (setq visiting (org-find-base-buffer-visiting file))
  13854. (setq buffer (or visiting (find-file-noselect file)))
  13855. (setq m (org-find-exact-headline-in-buffer
  13856. heading buffer))
  13857. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13858. (and m (throw 'found m))))))
  13859. (defun org-find-entry-with-id (ident)
  13860. "Locate the entry that contains the ID property with exact value IDENT.
  13861. IDENT can be a string, a symbol or a number, this function will search for
  13862. the string representation of it.
  13863. Return the position where this entry starts, or nil if there is no such entry."
  13864. (interactive "sID: ")
  13865. (let ((id (cond
  13866. ((stringp ident) ident)
  13867. ((symbol-name ident) (symbol-name ident))
  13868. ((numberp ident) (number-to-string ident))
  13869. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13870. (case-fold-search nil))
  13871. (save-excursion
  13872. (save-restriction
  13873. (widen)
  13874. (goto-char (point-min))
  13875. (when (re-search-forward
  13876. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13877. nil t)
  13878. (org-back-to-heading t)
  13879. (point))))))
  13880. ;;;; Timestamps
  13881. (defvar org-last-changed-timestamp nil)
  13882. (defvar org-last-inserted-timestamp nil
  13883. "The last time stamp inserted with `org-insert-time-stamp'.")
  13884. (defvar org-time-was-given) ; dynamically scoped parameter
  13885. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13886. (defvar org-ts-what) ; dynamically scoped parameter
  13887. (defun org-time-stamp (arg &optional inactive)
  13888. "Prompt for a date/time and insert a time stamp.
  13889. If the user specifies a time like HH:MM or if this command is
  13890. called with at least one prefix argument, the time stamp contains
  13891. the date and the time. Otherwise, only the date is be included.
  13892. All parts of a date not specified by the user is filled in from
  13893. the current date/time. So if you just press return without
  13894. typing anything, the time stamp will represent the current
  13895. date/time.
  13896. If there is already a timestamp at the cursor, it will be
  13897. modified.
  13898. With two universal prefix arguments, insert an active timestamp
  13899. with the current time without prompting the user."
  13900. (interactive "P")
  13901. (let* ((ts nil)
  13902. (default-time
  13903. ;; Default time is either today, or, when entering a range,
  13904. ;; the range start.
  13905. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13906. (save-excursion
  13907. (re-search-backward
  13908. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13909. (- (point) 20) t)))
  13910. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13911. (current-time)))
  13912. (default-input (and ts (org-get-compact-tod ts)))
  13913. (repeater (save-excursion
  13914. (save-match-data
  13915. (beginning-of-line)
  13916. (when (re-search-forward
  13917. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  13918. (save-excursion (progn (end-of-line) (point))) t)
  13919. (match-string 0)))))
  13920. org-time-was-given org-end-time-was-given time)
  13921. (cond
  13922. ((and (org-at-timestamp-p t)
  13923. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13924. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13925. (insert "--")
  13926. (setq time (let ((this-command this-command))
  13927. (org-read-date arg 'totime nil nil
  13928. default-time default-input inactive)))
  13929. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13930. ((org-at-timestamp-p t)
  13931. (setq time (let ((this-command this-command))
  13932. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13933. (when (org-at-timestamp-p t) ; just to get the match data
  13934. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13935. (replace-match "")
  13936. (setq org-last-changed-timestamp
  13937. (org-insert-time-stamp
  13938. time (or org-time-was-given arg)
  13939. inactive nil nil (list org-end-time-was-given)))
  13940. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13941. (setq org-last-changed-timestamp
  13942. (concat (substring org-last-inserted-timestamp 0 -1)
  13943. " " repeater ">"))))
  13944. (message "Timestamp updated"))
  13945. ((equal arg '(16))
  13946. (org-insert-time-stamp (current-time) t))
  13947. (t
  13948. (setq time (let ((this-command this-command))
  13949. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13950. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13951. nil nil (list org-end-time-was-given))))))
  13952. ;; FIXME: can we use this for something else, like computing time differences?
  13953. (defun org-get-compact-tod (s)
  13954. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13955. (let* ((t1 (match-string 1 s))
  13956. (h1 (string-to-number (match-string 2 s)))
  13957. (m1 (string-to-number (match-string 3 s)))
  13958. (t2 (and (match-end 4) (match-string 5 s)))
  13959. (h2 (and t2 (string-to-number (match-string 6 s))))
  13960. (m2 (and t2 (string-to-number (match-string 7 s))))
  13961. dh dm)
  13962. (if (not t2)
  13963. t1
  13964. (setq dh (- h2 h1) dm (- m2 m1))
  13965. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13966. (concat t1 "+" (number-to-string dh)
  13967. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13968. (defun org-time-stamp-inactive (&optional arg)
  13969. "Insert an inactive time stamp.
  13970. An inactive time stamp is enclosed in square brackets instead of angle
  13971. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13972. does not link to the calendar and cannot be changed with the S-cursor keys.
  13973. So these are more for recording a certain time/date."
  13974. (interactive "P")
  13975. (org-time-stamp arg 'inactive))
  13976. (defvar org-date-ovl (make-overlay 1 1))
  13977. (overlay-put org-date-ovl 'face 'org-date-selected)
  13978. (org-detach-overlay org-date-ovl)
  13979. (defvar org-ans1) ; dynamically scoped parameter
  13980. (defvar org-ans2) ; dynamically scoped parameter
  13981. (defvar org-plain-time-of-day-regexp) ; defined below
  13982. (defvar org-overriding-default-time nil) ; dynamically scoped
  13983. (defvar org-read-date-overlay nil)
  13984. (defvar org-dcst nil) ; dynamically scoped
  13985. (defvar org-read-date-history nil)
  13986. (defvar org-read-date-final-answer nil)
  13987. (defvar org-read-date-analyze-futurep nil)
  13988. (defvar org-read-date-analyze-forced-year nil)
  13989. (defvar org-read-date-inactive)
  13990. (defun org-read-date (&optional org-with-time to-time from-string prompt
  13991. default-time default-input inactive)
  13992. "Read a date, possibly a time, and make things smooth for the user.
  13993. The prompt will suggest to enter an ISO date, but you can also enter anything
  13994. which will at least partially be understood by `parse-time-string'.
  13995. Unrecognized parts of the date will default to the current day, month, year,
  13996. hour and minute. If this command is called to replace a timestamp at point,
  13997. or to enter the second timestamp of a range, the default time is taken
  13998. from the existing stamp. Furthermore, the command prefers the future,
  13999. so if you are giving a date where the year is not given, and the day-month
  14000. combination is already past in the current year, it will assume you
  14001. mean next year. For details, see the manual. A few examples:
  14002. 3-2-5 --> 2003-02-05
  14003. feb 15 --> currentyear-02-15
  14004. 2/15 --> currentyear-02-15
  14005. sep 12 9 --> 2009-09-12
  14006. 12:45 --> today 12:45
  14007. 22 sept 0:34 --> currentyear-09-22 0:34
  14008. 12 --> currentyear-currentmonth-12
  14009. Fri --> nearest Friday (today or later)
  14010. etc.
  14011. Furthermore you can specify a relative date by giving, as the *first* thing
  14012. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14013. change in days weeks, months, years.
  14014. With a single plus or minus, the date is relative to today. With a double
  14015. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14016. +4d --> four days from today
  14017. +4 --> same as above
  14018. +2w --> two weeks from today
  14019. ++5 --> five days from default date
  14020. The function understands only English month and weekday abbreviations.
  14021. While prompting, a calendar is popped up - you can also select the
  14022. date with the mouse (button 1). The calendar shows a period of three
  14023. months. To scroll it to other months, use the keys `>' and `<'.
  14024. If you don't like the calendar, turn it off with
  14025. \(setq org-read-date-popup-calendar nil)
  14026. With optional argument TO-TIME, the date will immediately be converted
  14027. to an internal time.
  14028. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14029. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14030. still enter a time, and this function will inform the calling routine
  14031. about this change. The calling routine may then choose to change the
  14032. format used to insert the time stamp into the buffer to include the time.
  14033. With optional argument FROM-STRING, read from this string instead from
  14034. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14035. the time/date that is used for everything that is not specified by the
  14036. user."
  14037. (require 'parse-time)
  14038. (let* ((org-time-stamp-rounding-minutes
  14039. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14040. (org-dcst org-display-custom-times)
  14041. (ct (org-current-time))
  14042. (org-def (or org-overriding-default-time default-time ct))
  14043. (org-defdecode (decode-time org-def))
  14044. (dummy (progn
  14045. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14046. (setcar (nthcdr 2 org-defdecode) -1)
  14047. (setcar (nthcdr 1 org-defdecode) 59)
  14048. (setq org-def (apply 'encode-time org-defdecode)
  14049. org-defdecode (decode-time org-def)))))
  14050. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14051. (calendar-frame-setup nil)
  14052. (calendar-setup nil)
  14053. (calendar-move-hook nil)
  14054. (calendar-view-diary-initially-flag nil)
  14055. (calendar-view-holidays-initially-flag nil)
  14056. (timestr (format-time-string
  14057. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14058. (prompt (concat (if prompt (concat prompt " ") "")
  14059. (format "Date+time [%s]: " timestr)))
  14060. ans (org-ans0 "") org-ans1 org-ans2 final)
  14061. (cond
  14062. (from-string (setq ans from-string))
  14063. (org-read-date-popup-calendar
  14064. (save-excursion
  14065. (save-window-excursion
  14066. (calendar)
  14067. (org-eval-in-calendar '(setq cursor-type nil) t)
  14068. (unwind-protect
  14069. (progn
  14070. (calendar-forward-day (- (time-to-days org-def)
  14071. (calendar-absolute-from-gregorian
  14072. (calendar-current-date))))
  14073. (org-eval-in-calendar nil t)
  14074. (let* ((old-map (current-local-map))
  14075. (map (copy-keymap calendar-mode-map))
  14076. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14077. (org-defkey map (kbd "RET") 'org-calendar-select)
  14078. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14079. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14080. (org-defkey minibuffer-local-map [(meta shift left)]
  14081. (lambda () (interactive)
  14082. (org-eval-in-calendar '(calendar-backward-month 1))))
  14083. (org-defkey minibuffer-local-map [(meta shift right)]
  14084. (lambda () (interactive)
  14085. (org-eval-in-calendar '(calendar-forward-month 1))))
  14086. (org-defkey minibuffer-local-map [(meta shift up)]
  14087. (lambda () (interactive)
  14088. (org-eval-in-calendar '(calendar-backward-year 1))))
  14089. (org-defkey minibuffer-local-map [(meta shift down)]
  14090. (lambda () (interactive)
  14091. (org-eval-in-calendar '(calendar-forward-year 1))))
  14092. (org-defkey minibuffer-local-map [?\e (shift left)]
  14093. (lambda () (interactive)
  14094. (org-eval-in-calendar '(calendar-backward-month 1))))
  14095. (org-defkey minibuffer-local-map [?\e (shift right)]
  14096. (lambda () (interactive)
  14097. (org-eval-in-calendar '(calendar-forward-month 1))))
  14098. (org-defkey minibuffer-local-map [?\e (shift up)]
  14099. (lambda () (interactive)
  14100. (org-eval-in-calendar '(calendar-backward-year 1))))
  14101. (org-defkey minibuffer-local-map [?\e (shift down)]
  14102. (lambda () (interactive)
  14103. (org-eval-in-calendar '(calendar-forward-year 1))))
  14104. (org-defkey minibuffer-local-map [(shift up)]
  14105. (lambda () (interactive)
  14106. (org-eval-in-calendar '(calendar-backward-week 1))))
  14107. (org-defkey minibuffer-local-map [(shift down)]
  14108. (lambda () (interactive)
  14109. (org-eval-in-calendar '(calendar-forward-week 1))))
  14110. (org-defkey minibuffer-local-map [(shift left)]
  14111. (lambda () (interactive)
  14112. (org-eval-in-calendar '(calendar-backward-day 1))))
  14113. (org-defkey minibuffer-local-map [(shift right)]
  14114. (lambda () (interactive)
  14115. (org-eval-in-calendar '(calendar-forward-day 1))))
  14116. (org-defkey minibuffer-local-map ">"
  14117. (lambda () (interactive)
  14118. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14119. (org-defkey minibuffer-local-map "<"
  14120. (lambda () (interactive)
  14121. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14122. (org-defkey minibuffer-local-map "\C-v"
  14123. (lambda () (interactive)
  14124. (org-eval-in-calendar
  14125. '(calendar-scroll-left-three-months 1))))
  14126. (org-defkey minibuffer-local-map "\M-v"
  14127. (lambda () (interactive)
  14128. (org-eval-in-calendar
  14129. '(calendar-scroll-right-three-months 1))))
  14130. (run-hooks 'org-read-date-minibuffer-setup-hook)
  14131. (unwind-protect
  14132. (progn
  14133. (use-local-map map)
  14134. (setq org-read-date-inactive inactive)
  14135. (add-hook 'post-command-hook 'org-read-date-display)
  14136. (setq org-ans0 (read-string prompt default-input
  14137. 'org-read-date-history nil))
  14138. ;; org-ans0: from prompt
  14139. ;; org-ans1: from mouse click
  14140. ;; org-ans2: from calendar motion
  14141. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14142. (remove-hook 'post-command-hook 'org-read-date-display)
  14143. (use-local-map old-map)
  14144. (when org-read-date-overlay
  14145. (delete-overlay org-read-date-overlay)
  14146. (setq org-read-date-overlay nil)))))
  14147. (bury-buffer "*Calendar*")))))
  14148. (t ; Naked prompt only
  14149. (unwind-protect
  14150. (setq ans (read-string prompt default-input
  14151. 'org-read-date-history timestr))
  14152. (when org-read-date-overlay
  14153. (delete-overlay org-read-date-overlay)
  14154. (setq org-read-date-overlay nil)))))
  14155. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14156. (when org-read-date-analyze-forced-year
  14157. (message "Year was forced into %s"
  14158. (if org-read-date-force-compatible-dates
  14159. "compatible range (1970-2037)"
  14160. "range representable on this machine"))
  14161. (ding))
  14162. ;; One round trip to get rid of 34th of August and stuff like that....
  14163. (setq final (decode-time (apply 'encode-time final)))
  14164. (setq org-read-date-final-answer ans)
  14165. (if to-time
  14166. (apply 'encode-time final)
  14167. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14168. (format "%04d-%02d-%02d %02d:%02d"
  14169. (nth 5 final) (nth 4 final) (nth 3 final)
  14170. (nth 2 final) (nth 1 final))
  14171. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14172. (defvar org-def)
  14173. (defvar org-defdecode)
  14174. (defvar org-with-time)
  14175. (defun org-read-date-display ()
  14176. "Display the current date prompt interpretation in the minibuffer."
  14177. (when org-read-date-display-live
  14178. (when org-read-date-overlay
  14179. (delete-overlay org-read-date-overlay))
  14180. (when (minibufferp (current-buffer))
  14181. (save-excursion
  14182. (end-of-line 1)
  14183. (while (not (equal (buffer-substring
  14184. (max (point-min) (- (point) 4)) (point))
  14185. " "))
  14186. (insert " ")))
  14187. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14188. " " (or org-ans1 org-ans2)))
  14189. (org-end-time-was-given nil)
  14190. (f (org-read-date-analyze ans org-def org-defdecode))
  14191. (fmts (if org-dcst
  14192. org-time-stamp-custom-formats
  14193. org-time-stamp-formats))
  14194. (fmt (if (or org-with-time
  14195. (and (boundp 'org-time-was-given) org-time-was-given))
  14196. (cdr fmts)
  14197. (car fmts)))
  14198. (txt (format-time-string fmt (apply 'encode-time f)))
  14199. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14200. (txt (concat "=> " txt)))
  14201. (when (and org-end-time-was-given
  14202. (string-match org-plain-time-of-day-regexp txt))
  14203. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14204. org-end-time-was-given
  14205. (substring txt (match-end 0)))))
  14206. (when org-read-date-analyze-futurep
  14207. (setq txt (concat txt " (=>F)")))
  14208. (setq org-read-date-overlay
  14209. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14210. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14211. (defun org-read-date-analyze (ans org-def org-defdecode)
  14212. "Analyze the combined answer of the date prompt."
  14213. ;; FIXME: cleanup and comment
  14214. (let ((nowdecode (decode-time (current-time)))
  14215. delta deltan deltaw deltadef year month day
  14216. hour minute second wday pm h2 m2 tl wday1
  14217. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14218. (setq org-read-date-analyze-futurep nil
  14219. org-read-date-analyze-forced-year nil)
  14220. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14221. (setq ans "+0"))
  14222. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14223. (setq ans (replace-match "" t t ans)
  14224. deltan (car delta)
  14225. deltaw (nth 1 delta)
  14226. deltadef (nth 2 delta)))
  14227. ;; Check if there is an iso week date in there. If yes, store the
  14228. ;; info and postpone interpreting it until the rest of the parsing
  14229. ;; is done.
  14230. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14231. (setq iso-year (if (match-end 1)
  14232. (org-small-year-to-year
  14233. (string-to-number (match-string 1 ans))))
  14234. iso-weekday (if (match-end 3)
  14235. (string-to-number (match-string 3 ans)))
  14236. iso-week (string-to-number (match-string 2 ans)))
  14237. (setq ans (replace-match "" t t ans)))
  14238. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14239. (when (string-match
  14240. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14241. (setq year (if (match-end 2)
  14242. (string-to-number (match-string 2 ans))
  14243. (progn (setq kill-year t)
  14244. (string-to-number (format-time-string "%Y"))))
  14245. month (string-to-number (match-string 3 ans))
  14246. day (string-to-number (match-string 4 ans)))
  14247. (if (< year 100) (setq year (+ 2000 year)))
  14248. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14249. t nil ans)))
  14250. ;; Help matching dotted european dates
  14251. (when (string-match
  14252. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14253. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14254. (setq kill-year t)
  14255. (string-to-number (format-time-string "%Y")))
  14256. day (string-to-number (match-string 1 ans))
  14257. month (string-to-number (match-string 2 ans))
  14258. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14259. t nil ans)))
  14260. ;; Help matching american dates, like 5/30 or 5/30/7
  14261. (when (string-match
  14262. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14263. (setq year (if (match-end 4)
  14264. (string-to-number (match-string 4 ans))
  14265. (progn (setq kill-year t)
  14266. (string-to-number (format-time-string "%Y"))))
  14267. month (string-to-number (match-string 1 ans))
  14268. day (string-to-number (match-string 2 ans)))
  14269. (if (< year 100) (setq year (+ 2000 year)))
  14270. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14271. t nil ans)))
  14272. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14273. ;; If there is a time with am/pm, and *no* time without it, we convert
  14274. ;; so that matching will be successful.
  14275. (loop for i from 1 to 2 do ; twice, for end time as well
  14276. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14277. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14278. (setq hour (string-to-number (match-string 1 ans))
  14279. minute (if (match-end 3)
  14280. (string-to-number (match-string 3 ans))
  14281. 0)
  14282. pm (equal ?p
  14283. (string-to-char (downcase (match-string 4 ans)))))
  14284. (if (and (= hour 12) (not pm))
  14285. (setq hour 0)
  14286. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14287. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14288. t t ans))))
  14289. ;; Check if a time range is given as a duration
  14290. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14291. (setq hour (string-to-number (match-string 1 ans))
  14292. h2 (+ hour (string-to-number (match-string 3 ans)))
  14293. minute (string-to-number (match-string 2 ans))
  14294. m2 (+ minute (if (match-end 5) (string-to-number
  14295. (match-string 5 ans))0)))
  14296. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14297. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14298. t t ans)))
  14299. ;; Check if there is a time range
  14300. (when (boundp 'org-end-time-was-given)
  14301. (setq org-time-was-given nil)
  14302. (when (and (string-match org-plain-time-of-day-regexp ans)
  14303. (match-end 8))
  14304. (setq org-end-time-was-given (match-string 8 ans))
  14305. (setq ans (concat (substring ans 0 (match-beginning 7))
  14306. (substring ans (match-end 7))))))
  14307. (setq tl (parse-time-string ans)
  14308. day (or (nth 3 tl) (nth 3 org-defdecode))
  14309. month (or (nth 4 tl)
  14310. (if (and org-read-date-prefer-future
  14311. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14312. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14313. (nth 4 org-defdecode)))
  14314. year (or (and (not kill-year) (nth 5 tl))
  14315. (if (and org-read-date-prefer-future
  14316. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14317. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14318. (nth 5 org-defdecode)))
  14319. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14320. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14321. second (or (nth 0 tl) 0)
  14322. wday (nth 6 tl))
  14323. (when (and (eq org-read-date-prefer-future 'time)
  14324. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14325. (equal day (nth 3 nowdecode))
  14326. (equal month (nth 4 nowdecode))
  14327. (equal year (nth 5 nowdecode))
  14328. (nth 2 tl)
  14329. (or (< (nth 2 tl) (nth 2 nowdecode))
  14330. (and (= (nth 2 tl) (nth 2 nowdecode))
  14331. (nth 1 tl)
  14332. (< (nth 1 tl) (nth 1 nowdecode)))))
  14333. (setq day (1+ day)
  14334. futurep t))
  14335. ;; Special date definitions below
  14336. (cond
  14337. (iso-week
  14338. ;; There was an iso week
  14339. (require 'cal-iso)
  14340. (setq futurep nil)
  14341. (setq year (or iso-year year)
  14342. day (or iso-weekday wday 1)
  14343. wday nil ; to make sure that the trigger below does not match
  14344. iso-date (calendar-gregorian-from-absolute
  14345. (calendar-absolute-from-iso
  14346. (list iso-week day year))))
  14347. ; FIXME: Should we also push ISO weeks into the future?
  14348. ; (when (and org-read-date-prefer-future
  14349. ; (not iso-year)
  14350. ; (< (calendar-absolute-from-gregorian iso-date)
  14351. ; (time-to-days (current-time))))
  14352. ; (setq year (1+ year)
  14353. ; iso-date (calendar-gregorian-from-absolute
  14354. ; (calendar-absolute-from-iso
  14355. ; (list iso-week day year)))))
  14356. (setq month (car iso-date)
  14357. year (nth 2 iso-date)
  14358. day (nth 1 iso-date)))
  14359. (deltan
  14360. (setq futurep nil)
  14361. (unless deltadef
  14362. (let ((now (decode-time (current-time))))
  14363. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14364. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14365. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14366. ((equal deltaw "m") (setq month (+ month deltan)))
  14367. ((equal deltaw "y") (setq year (+ year deltan)))))
  14368. ((and wday (not (nth 3 tl)))
  14369. ;; Weekday was given, but no day, so pick that day in the week
  14370. ;; on or after the derived date.
  14371. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14372. (unless (equal wday wday1)
  14373. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14374. (if (and (boundp 'org-time-was-given)
  14375. (nth 2 tl))
  14376. (setq org-time-was-given t))
  14377. (if (< year 100) (setq year (+ 2000 year)))
  14378. ;; Check of the date is representable
  14379. (if org-read-date-force-compatible-dates
  14380. (progn
  14381. (if (< year 1970)
  14382. (setq year 1970 org-read-date-analyze-forced-year t))
  14383. (if (> year 2037)
  14384. (setq year 2037 org-read-date-analyze-forced-year t)))
  14385. (condition-case nil
  14386. (ignore (encode-time second minute hour day month year))
  14387. (error
  14388. (setq year (nth 5 org-defdecode))
  14389. (setq org-read-date-analyze-forced-year t))))
  14390. (setq org-read-date-analyze-futurep futurep)
  14391. (list second minute hour day month year)))
  14392. (defvar parse-time-weekdays)
  14393. (defun org-read-date-get-relative (s today default)
  14394. "Check string S for special relative date string.
  14395. TODAY and DEFAULT are internal times, for today and for a default.
  14396. Return shift list (N what def-flag)
  14397. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14398. N is the number of WHATs to shift.
  14399. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14400. the DEFAULT date rather than TODAY."
  14401. (require 'parse-time)
  14402. (when (and
  14403. (string-match
  14404. (concat
  14405. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14406. "\\([0-9]+\\)?"
  14407. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14408. "\\([ \t]\\|$\\)") s)
  14409. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14410. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14411. (string-to-char (substring (match-string 1 s) -1))
  14412. ?+))
  14413. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14414. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14415. (what (if (match-end 3) (match-string 3 s) "d"))
  14416. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14417. (date (if rel default today))
  14418. (wday (nth 6 (decode-time date)))
  14419. delta)
  14420. (if wday1
  14421. (progn
  14422. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14423. (if (= dir ?-) (setq delta (- delta 7)))
  14424. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14425. (list delta "d" rel))
  14426. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14427. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14428. "Turn a user-specified date into the internal representation.
  14429. The internal representation needed by the calendar is (month day year).
  14430. This is a wrapper to handle the brain-dead convention in calendar that
  14431. user function argument order change dependent on argument order."
  14432. (if (boundp 'calendar-date-style)
  14433. (cond
  14434. ((eq calendar-date-style 'american)
  14435. (list arg1 arg2 arg3))
  14436. ((eq calendar-date-style 'european)
  14437. (list arg2 arg1 arg3))
  14438. ((eq calendar-date-style 'iso)
  14439. (list arg2 arg3 arg1)))
  14440. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14441. (if (org-bound-and-true-p european-calendar-style)
  14442. (list arg2 arg1 arg3)
  14443. (list arg1 arg2 arg3)))))
  14444. (defun org-eval-in-calendar (form &optional keepdate)
  14445. "Eval FORM in the calendar window and return to current window.
  14446. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14447. otherwise stick to the current value of `org-ans2'."
  14448. (let ((sf (selected-frame))
  14449. (sw (selected-window)))
  14450. (select-window (get-buffer-window "*Calendar*" t))
  14451. (eval form)
  14452. (when (and (not keepdate) (calendar-cursor-to-date))
  14453. (let* ((date (calendar-cursor-to-date))
  14454. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14455. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14456. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14457. (select-window sw)
  14458. (org-select-frame-set-input-focus sf)))
  14459. (defun org-calendar-select ()
  14460. "Return to `org-read-date' with the date currently selected.
  14461. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14462. (interactive)
  14463. (when (calendar-cursor-to-date)
  14464. (let* ((date (calendar-cursor-to-date))
  14465. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14466. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14467. (if (active-minibuffer-window) (exit-minibuffer))))
  14468. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14469. "Insert a date stamp for the date given by the internal TIME.
  14470. WITH-HM means use the stamp format that includes the time of the day.
  14471. INACTIVE means use square brackets instead of angular ones, so that the
  14472. stamp will not contribute to the agenda.
  14473. PRE and POST are optional strings to be inserted before and after the
  14474. stamp.
  14475. The command returns the inserted time stamp."
  14476. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14477. stamp)
  14478. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14479. (insert-before-markers (or pre ""))
  14480. (when (listp extra)
  14481. (setq extra (car extra))
  14482. (if (and (stringp extra)
  14483. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14484. (setq extra (format "-%02d:%02d"
  14485. (string-to-number (match-string 1 extra))
  14486. (string-to-number (match-string 2 extra))))
  14487. (setq extra nil)))
  14488. (when extra
  14489. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14490. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14491. (insert-before-markers (or post ""))
  14492. (setq org-last-inserted-timestamp stamp)))
  14493. (defun org-toggle-time-stamp-overlays ()
  14494. "Toggle the use of custom time stamp formats."
  14495. (interactive)
  14496. (setq org-display-custom-times (not org-display-custom-times))
  14497. (unless org-display-custom-times
  14498. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14499. (while (setq p (next-single-property-change p 'display))
  14500. (if (and (get-text-property p 'display)
  14501. (eq (get-text-property p 'face) 'org-date))
  14502. (remove-text-properties
  14503. p (setq p (next-single-property-change p 'display))
  14504. '(display t))))
  14505. (set-buffer-modified-p bmp)))
  14506. (if (featurep 'xemacs)
  14507. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14508. (org-restart-font-lock)
  14509. (setq org-table-may-need-update t)
  14510. (if org-display-custom-times
  14511. (message "Time stamps are overlaid with custom format")
  14512. (message "Time stamp overlays removed")))
  14513. (defun org-display-custom-time (beg end)
  14514. "Overlay modified time stamp format over timestamp between BEG and END."
  14515. (let* ((ts (buffer-substring beg end))
  14516. t1 w1 with-hm tf time str w2 (off 0))
  14517. (save-match-data
  14518. (setq t1 (org-parse-time-string ts t))
  14519. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14520. (setq off (- (match-end 0) (match-beginning 0)))))
  14521. (setq end (- end off))
  14522. (setq w1 (- end beg)
  14523. with-hm (and (nth 1 t1) (nth 2 t1))
  14524. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14525. time (org-fix-decoded-time t1)
  14526. str (org-add-props
  14527. (format-time-string
  14528. (substring tf 1 -1) (apply 'encode-time time))
  14529. nil 'mouse-face 'highlight)
  14530. w2 (length str))
  14531. (if (not (= w2 w1))
  14532. (add-text-properties (1+ beg) (+ 2 beg)
  14533. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14534. (if (featurep 'xemacs)
  14535. (progn
  14536. (put-text-property beg end 'invisible t)
  14537. (put-text-property beg end 'end-glyph (make-glyph str)))
  14538. (put-text-property beg end 'display str))))
  14539. (defun org-translate-time (string)
  14540. "Translate all timestamps in STRING to custom format.
  14541. But do this only if the variable `org-display-custom-times' is set."
  14542. (when org-display-custom-times
  14543. (save-match-data
  14544. (let* ((start 0)
  14545. (re org-ts-regexp-both)
  14546. t1 with-hm inactive tf time str beg end)
  14547. (while (setq start (string-match re string start))
  14548. (setq beg (match-beginning 0)
  14549. end (match-end 0)
  14550. t1 (save-match-data
  14551. (org-parse-time-string (substring string beg end) t))
  14552. with-hm (and (nth 1 t1) (nth 2 t1))
  14553. inactive (equal (substring string beg (1+ beg)) "[")
  14554. tf (funcall (if with-hm 'cdr 'car)
  14555. org-time-stamp-custom-formats)
  14556. time (org-fix-decoded-time t1)
  14557. str (format-time-string
  14558. (concat
  14559. (if inactive "[" "<") (substring tf 1 -1)
  14560. (if inactive "]" ">"))
  14561. (apply 'encode-time time))
  14562. string (replace-match str t t string)
  14563. start (+ start (length str)))))))
  14564. string)
  14565. (defun org-fix-decoded-time (time)
  14566. "Set 0 instead of nil for the first 6 elements of time.
  14567. Don't touch the rest."
  14568. (let ((n 0))
  14569. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14570. (defun org-days-to-time (timestamp-string)
  14571. "Difference between TIMESTAMP-STRING and now in days."
  14572. (- (time-to-days (org-time-string-to-time timestamp-string))
  14573. (time-to-days (current-time))))
  14574. (defun org-deadline-close (timestamp-string &optional ndays)
  14575. "Is the time in TIMESTAMP-STRING close to the current date?"
  14576. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14577. (and (< (org-days-to-time timestamp-string) ndays)
  14578. (not (org-entry-is-done-p))))
  14579. (defun org-get-wdays (ts)
  14580. "Get the deadline lead time appropriate for timestring TS."
  14581. (cond
  14582. ((<= org-deadline-warning-days 0)
  14583. ;; 0 or negative, enforce this value no matter what
  14584. (- org-deadline-warning-days))
  14585. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14586. ;; lead time is specified.
  14587. (floor (* (string-to-number (match-string 1 ts))
  14588. (cdr (assoc (match-string 2 ts)
  14589. '(("d" . 1) ("w" . 7)
  14590. ("m" . 30.4) ("y" . 365.25)
  14591. ("h" . 0.041667)))))))
  14592. ;; go for the default.
  14593. (t org-deadline-warning-days)))
  14594. (defun org-calendar-select-mouse (ev)
  14595. "Return to `org-read-date' with the date currently selected.
  14596. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14597. (interactive "e")
  14598. (mouse-set-point ev)
  14599. (when (calendar-cursor-to-date)
  14600. (let* ((date (calendar-cursor-to-date))
  14601. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14602. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14603. (if (active-minibuffer-window) (exit-minibuffer))))
  14604. (defun org-check-deadlines (ndays)
  14605. "Check if there are any deadlines due or past due.
  14606. A deadline is considered due if it happens within `org-deadline-warning-days'
  14607. days from today's date. If the deadline appears in an entry marked DONE,
  14608. it is not shown. The prefix arg NDAYS can be used to test that many
  14609. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14610. (interactive "P")
  14611. (let* ((org-warn-days
  14612. (cond
  14613. ((equal ndays '(4)) 100000)
  14614. (ndays (prefix-numeric-value ndays))
  14615. (t (abs org-deadline-warning-days))))
  14616. (case-fold-search nil)
  14617. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14618. (callback
  14619. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14620. (message "%d deadlines past-due or due within %d days"
  14621. (org-occur regexp nil callback)
  14622. org-warn-days)))
  14623. (defsubst org-re-timestamp (type)
  14624. "Return a regexp for timestamp TYPE.
  14625. Allowed values for TYPE are:
  14626. all: all timestamps
  14627. active: only active timestamps (<...>)
  14628. inactive: only inactive timestamps ([...])
  14629. scheduled: only scheduled timestamps
  14630. deadline: only deadline timestamps
  14631. When TYPE is nil, fall back on returning a regexp that matches
  14632. both scheduled and deadline timestamps."
  14633. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14634. ((eq type 'active) org-ts-regexp)
  14635. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14636. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14637. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14638. ((eq type 'scheduled-or-deadline)
  14639. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14640. (defun org-check-before-date (date)
  14641. "Check if there are deadlines or scheduled entries before DATE."
  14642. (interactive (list (org-read-date)))
  14643. (let ((case-fold-search nil)
  14644. (regexp (org-re-timestamp org-ts-type))
  14645. (callback
  14646. (lambda () (time-less-p
  14647. (org-time-string-to-time (match-string 1))
  14648. (org-time-string-to-time date)))))
  14649. (message "%d entries before %s"
  14650. (org-occur regexp nil callback) date)))
  14651. (defun org-check-after-date (date)
  14652. "Check if there are deadlines or scheduled entries after DATE."
  14653. (interactive (list (org-read-date)))
  14654. (let ((case-fold-search nil)
  14655. (regexp (org-re-timestamp org-ts-type))
  14656. (callback
  14657. (lambda () (not
  14658. (time-less-p
  14659. (org-time-string-to-time (match-string 1))
  14660. (org-time-string-to-time date))))))
  14661. (message "%d entries after %s"
  14662. (org-occur regexp nil callback) date)))
  14663. (defun org-check-dates-range (start-date end-date)
  14664. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14665. (interactive (list (org-read-date nil nil nil "Range starts")
  14666. (org-read-date nil nil nil "Range end")))
  14667. (let ((case-fold-search nil)
  14668. (regexp (org-re-timestamp org-ts-type))
  14669. (callback
  14670. (lambda ()
  14671. (let ((match (match-string 1)))
  14672. (and
  14673. (not (time-less-p
  14674. (org-time-string-to-time match)
  14675. (org-time-string-to-time start-date)))
  14676. (time-less-p
  14677. (org-time-string-to-time match)
  14678. (org-time-string-to-time end-date)))))))
  14679. (message "%d entries between %s and %s"
  14680. (org-occur regexp nil callback) start-date end-date)))
  14681. (defun org-evaluate-time-range (&optional to-buffer)
  14682. "Evaluate a time range by computing the difference between start and end.
  14683. Normally the result is just printed in the echo area, but with prefix arg
  14684. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14685. If the time range is actually in a table, the result is inserted into the
  14686. next column.
  14687. For time difference computation, a year is assumed to be exactly 365
  14688. days in order to avoid rounding problems."
  14689. (interactive "P")
  14690. (or
  14691. (org-clock-update-time-maybe)
  14692. (save-excursion
  14693. (unless (org-at-date-range-p t)
  14694. (goto-char (point-at-bol))
  14695. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14696. (if (not (org-at-date-range-p t))
  14697. (error "Not at a time-stamp range, and none found in current line")))
  14698. (let* ((ts1 (match-string 1))
  14699. (ts2 (match-string 2))
  14700. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14701. (match-end (match-end 0))
  14702. (time1 (org-time-string-to-time ts1))
  14703. (time2 (org-time-string-to-time ts2))
  14704. (t1 (org-float-time time1))
  14705. (t2 (org-float-time time2))
  14706. (diff (abs (- t2 t1)))
  14707. (negative (< (- t2 t1) 0))
  14708. ;; (ys (floor (* 365 24 60 60)))
  14709. (ds (* 24 60 60))
  14710. (hs (* 60 60))
  14711. (fy "%dy %dd %02d:%02d")
  14712. (fy1 "%dy %dd")
  14713. (fd "%dd %02d:%02d")
  14714. (fd1 "%dd")
  14715. (fh "%02d:%02d")
  14716. y d h m align)
  14717. (if havetime
  14718. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14719. y 0
  14720. d (floor (/ diff ds)) diff (mod diff ds)
  14721. h (floor (/ diff hs)) diff (mod diff hs)
  14722. m (floor (/ diff 60)))
  14723. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14724. y 0
  14725. d (floor (+ (/ diff ds) 0.5))
  14726. h 0 m 0))
  14727. (if (not to-buffer)
  14728. (message "%s" (org-make-tdiff-string y d h m))
  14729. (if (org-at-table-p)
  14730. (progn
  14731. (goto-char match-end)
  14732. (setq align t)
  14733. (and (looking-at " *|") (goto-char (match-end 0))))
  14734. (goto-char match-end))
  14735. (if (looking-at
  14736. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14737. (replace-match ""))
  14738. (if negative (insert " -"))
  14739. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14740. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14741. (insert " " (format fh h m))))
  14742. (if align (org-table-align))
  14743. (message "Time difference inserted")))))
  14744. (defun org-make-tdiff-string (y d h m)
  14745. (let ((fmt "")
  14746. (l nil))
  14747. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14748. l (push y l)))
  14749. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14750. l (push d l)))
  14751. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14752. l (push h l)))
  14753. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14754. l (push m l)))
  14755. (apply 'format fmt (nreverse l))))
  14756. (defun org-time-string-to-time (s &optional buffer pos)
  14757. "Convert a timestamp string into internal time."
  14758. (condition-case errdata
  14759. (apply 'encode-time (org-parse-time-string s))
  14760. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14761. s (if (not (and buffer pos))
  14762. ""
  14763. (format " at %d in buffer `%s'" pos buffer))
  14764. (cdr errdata)))))
  14765. (defun org-time-string-to-seconds (s)
  14766. "Convert a timestamp string to a number of seconds."
  14767. (org-float-time (org-time-string-to-time s)))
  14768. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14769. "Convert a time stamp to an absolute day number.
  14770. If there is a specifier for a cyclic time stamp, get the closest date to
  14771. DAYNR.
  14772. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14773. The variable date is bound by the calendar when this is called."
  14774. (cond
  14775. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14776. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14777. daynr
  14778. (+ daynr 1000)))
  14779. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14780. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14781. (time-to-days (current-time))) (match-string 0 s)
  14782. prefer show-all))
  14783. (t (time-to-days
  14784. (condition-case errdata
  14785. (apply 'encode-time (org-parse-time-string s))
  14786. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14787. s (if (not (and buffer pos))
  14788. ""
  14789. (format " at %d in buffer `%s'" pos buffer))
  14790. (cdr errdata))))))))
  14791. (defun org-days-to-iso-week (days)
  14792. "Return the iso week number."
  14793. (require 'cal-iso)
  14794. (car (calendar-iso-from-absolute days)))
  14795. (defun org-small-year-to-year (year)
  14796. "Convert 2-digit years into 4-digit years.
  14797. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14798. The year 2000 cannot be abbreviated. Any year larger than 99
  14799. is returned unchanged."
  14800. (if (< year 38)
  14801. (setq year (+ 2000 year))
  14802. (if (< year 100)
  14803. (setq year (+ 1900 year))))
  14804. year)
  14805. (defun org-time-from-absolute (d)
  14806. "Return the time corresponding to date D.
  14807. D may be an absolute day number, or a calendar-type list (month day year)."
  14808. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14809. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14810. (defun org-calendar-holiday ()
  14811. "List of holidays, for Diary display in Org-mode."
  14812. (require 'holidays)
  14813. (let ((hl (funcall
  14814. (if (fboundp 'calendar-check-holidays)
  14815. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14816. (if hl (mapconcat 'identity hl "; "))))
  14817. (defun org-diary-sexp-entry (sexp entry date)
  14818. "Process a SEXP diary ENTRY for DATE."
  14819. (require 'diary-lib)
  14820. (let ((result (if calendar-debug-sexp
  14821. (let ((stack-trace-on-error t))
  14822. (eval (car (read-from-string sexp))))
  14823. (condition-case nil
  14824. (eval (car (read-from-string sexp)))
  14825. (error
  14826. (beep)
  14827. (message "Bad sexp at line %d in %s: %s"
  14828. (org-current-line)
  14829. (buffer-file-name) sexp)
  14830. (sleep-for 2))))))
  14831. (cond ((stringp result) (split-string result "; "))
  14832. ((and (consp result)
  14833. (not (consp (cdr result)))
  14834. (stringp (cdr result))) (cdr result))
  14835. ((and (consp result)
  14836. (stringp (car result))) result)
  14837. (result entry))))
  14838. (defun org-diary-to-ical-string (frombuf)
  14839. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14840. This uses the icalendar.el library."
  14841. (let* ((tmpdir (if (featurep 'xemacs)
  14842. (temp-directory)
  14843. temporary-file-directory))
  14844. (tmpfile (make-temp-name
  14845. (expand-file-name "orgics" tmpdir)))
  14846. buf rtn b e)
  14847. (with-current-buffer frombuf
  14848. (icalendar-export-region (point-min) (point-max) tmpfile)
  14849. (setq buf (find-buffer-visiting tmpfile))
  14850. (set-buffer buf)
  14851. (goto-char (point-min))
  14852. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14853. (setq b (match-beginning 0)))
  14854. (goto-char (point-max))
  14855. (if (re-search-backward "^END:VEVENT" nil t)
  14856. (setq e (match-end 0)))
  14857. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14858. (kill-buffer buf)
  14859. (delete-file tmpfile)
  14860. rtn))
  14861. (defun org-closest-date (start current change prefer show-all)
  14862. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14863. When PREFER is `past', return a date that is either CURRENT or past.
  14864. When PREFER is `future', return a date that is either CURRENT or future.
  14865. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14866. ;; Make the proper lists from the dates
  14867. (catch 'exit
  14868. (let ((a1 '(("h" . hour)
  14869. ("d" . day)
  14870. ("w" . week)
  14871. ("m" . month)
  14872. ("y" . year)))
  14873. (shour (nth 2 (org-parse-time-string start)))
  14874. dn dw sday cday n1 n2 n0
  14875. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14876. (setq start (org-date-to-gregorian start)
  14877. current (org-date-to-gregorian
  14878. (if show-all
  14879. current
  14880. (time-to-days (current-time))))
  14881. sday (calendar-absolute-from-gregorian start)
  14882. cday (calendar-absolute-from-gregorian current))
  14883. (if (<= cday sday) (throw 'exit sday))
  14884. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  14885. (setq dn (string-to-number (match-string 1 change))
  14886. dw (cdr (assoc (match-string 2 change) a1)))
  14887. (error "Invalid change specifier: %s" change))
  14888. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14889. (cond
  14890. ((eq dw 'hour)
  14891. (let ((missing-hours
  14892. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  14893. dn)))
  14894. (setq n1 (if (zerop missing-hours) cday
  14895. (- cday (1+ (floor (/ missing-hours 24)))))
  14896. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  14897. ((eq dw 'day)
  14898. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14899. n2 (+ n1 dn)))
  14900. ((eq dw 'year)
  14901. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14902. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14903. (setq date1 (list m d y1)
  14904. n1 (calendar-absolute-from-gregorian date1)
  14905. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14906. n2 (calendar-absolute-from-gregorian date2)))
  14907. ((eq dw 'month)
  14908. ;; approx number of month between the two dates
  14909. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14910. ;; How often does dn fit in there?
  14911. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14912. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14913. m (+ m nm)
  14914. ny (floor (/ m 12))
  14915. y (+ y ny)
  14916. m (- m (* ny 12)))
  14917. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14918. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14919. (setq m2 (+ m dn) y2 y)
  14920. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14921. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14922. (while (<= n2 cday)
  14923. (setq n1 n2 m m2 y y2)
  14924. (setq m2 (+ m dn) y2 y)
  14925. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14926. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14927. ;; Make sure n1 is the earlier date
  14928. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14929. (if show-all
  14930. (cond
  14931. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14932. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14933. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14934. (cond
  14935. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14936. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14937. (t (if (= cday n1) n1 n2)))))))
  14938. (defun org-date-to-gregorian (date)
  14939. "Turn any specification of DATE into a Gregorian date for the calendar."
  14940. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14941. ((and (listp date) (= (length date) 3)) date)
  14942. ((stringp date)
  14943. (setq date (org-parse-time-string date))
  14944. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14945. ((listp date)
  14946. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14947. (defun org-parse-time-string (s &optional nodefault)
  14948. "Parse the standard Org-mode time string.
  14949. This should be a lot faster than the normal `parse-time-string'.
  14950. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14951. hour and minute fields will be nil if not given."
  14952. (if (string-match org-ts-regexp0 s)
  14953. (list 0
  14954. (if (or (match-beginning 8) (not nodefault))
  14955. (string-to-number (or (match-string 8 s) "0")))
  14956. (if (or (match-beginning 7) (not nodefault))
  14957. (string-to-number (or (match-string 7 s) "0")))
  14958. (string-to-number (match-string 4 s))
  14959. (string-to-number (match-string 3 s))
  14960. (string-to-number (match-string 2 s))
  14961. nil nil nil)
  14962. (error "Not a standard Org-mode time string: %s" s)))
  14963. (defun org-timestamp-up (&optional arg)
  14964. "Increase the date item at the cursor by one.
  14965. If the cursor is on the year, change the year. If it is on the month,
  14966. the day or the time, change that.
  14967. With prefix ARG, change by that many units."
  14968. (interactive "p")
  14969. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14970. (defun org-timestamp-down (&optional arg)
  14971. "Decrease the date item at the cursor by one.
  14972. If the cursor is on the year, change the year. If it is on the month,
  14973. the day or the time, change that.
  14974. With prefix ARG, change by that many units."
  14975. (interactive "p")
  14976. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14977. (defun org-timestamp-up-day (&optional arg)
  14978. "Increase the date in the time stamp by one day.
  14979. With prefix ARG, change that many days."
  14980. (interactive "p")
  14981. (if (and (not (org-at-timestamp-p t))
  14982. (org-at-heading-p))
  14983. (org-todo 'up)
  14984. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14985. (defun org-timestamp-down-day (&optional arg)
  14986. "Decrease the date in the time stamp by one day.
  14987. With prefix ARG, change that many days."
  14988. (interactive "p")
  14989. (if (and (not (org-at-timestamp-p t))
  14990. (org-at-heading-p))
  14991. (org-todo 'down)
  14992. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14993. (defun org-at-timestamp-p (&optional inactive-ok)
  14994. "Determine if the cursor is in or at a timestamp."
  14995. (interactive)
  14996. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14997. (pos (point))
  14998. (ans (or (looking-at tsr)
  14999. (save-excursion
  15000. (skip-chars-backward "^[<\n\r\t")
  15001. (if (> (point) (point-min)) (backward-char 1))
  15002. (and (looking-at tsr)
  15003. (> (- (match-end 0) pos) -1))))))
  15004. (and ans
  15005. (boundp 'org-ts-what)
  15006. (setq org-ts-what
  15007. (cond
  15008. ((= pos (match-beginning 0)) 'bracket)
  15009. ;; Point is considered to be "on the bracket" whether
  15010. ;; it's really on it or right after it.
  15011. ((= pos (1- (match-end 0))) 'bracket)
  15012. ((= pos (match-end 0)) 'after)
  15013. ((org-pos-in-match-range pos 2) 'year)
  15014. ((org-pos-in-match-range pos 3) 'month)
  15015. ((org-pos-in-match-range pos 7) 'hour)
  15016. ((org-pos-in-match-range pos 8) 'minute)
  15017. ((or (org-pos-in-match-range pos 4)
  15018. (org-pos-in-match-range pos 5)) 'day)
  15019. ((and (> pos (or (match-end 8) (match-end 5)))
  15020. (< pos (match-end 0)))
  15021. (- pos (or (match-end 8) (match-end 5))))
  15022. (t 'day))))
  15023. ans))
  15024. (defun org-toggle-timestamp-type ()
  15025. "Toggle the type (<active> or [inactive]) of a time stamp."
  15026. (interactive)
  15027. (when (org-at-timestamp-p t)
  15028. (let ((beg (match-beginning 0)) (end (match-end 0))
  15029. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15030. (save-excursion
  15031. (goto-char beg)
  15032. (while (re-search-forward "[][<>]" end t)
  15033. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15034. t t)))
  15035. (message "Timestamp is now %sactive"
  15036. (if (equal (char-after beg) ?<) "" "in")))))
  15037. (defun org-at-clock-log-p nil
  15038. "Is the cursor on the clock log line?"
  15039. (save-excursion
  15040. (move-beginning-of-line 1)
  15041. (looking-at "^[ \t]*CLOCK:")))
  15042. (defvar org-clock-history) ; defined in org-clock.el
  15043. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15044. (defun org-timestamp-change (n &optional what updown)
  15045. "Change the date in the time stamp at point.
  15046. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15047. `year', `minute', `second'. If WHAT is not given, the cursor position
  15048. in the timestamp determines what will be changed."
  15049. (let ((origin (point)) origin-cat
  15050. with-hm inactive
  15051. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15052. org-ts-what
  15053. extra rem
  15054. ts time time0 fixnext clrgx)
  15055. (if (not (org-at-timestamp-p t))
  15056. (error "Not at a timestamp"))
  15057. (if (and (not what) (eq org-ts-what 'bracket))
  15058. (org-toggle-timestamp-type)
  15059. ;; Point isn't on brackets. Remember the part of the time-stamp
  15060. ;; the point was in. Indeed, size of time-stamps may change,
  15061. ;; but point must be kept in the same category nonetheless.
  15062. (setq origin-cat org-ts-what)
  15063. (if (and (not what) (not (eq org-ts-what 'day))
  15064. org-display-custom-times
  15065. (get-text-property (point) 'display)
  15066. (not (get-text-property (1- (point)) 'display)))
  15067. (setq org-ts-what 'day))
  15068. (setq org-ts-what (or what org-ts-what)
  15069. inactive (= (char-after (match-beginning 0)) ?\[)
  15070. ts (match-string 0))
  15071. (replace-match "")
  15072. (if (string-match
  15073. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15074. ts)
  15075. (setq extra (match-string 1 ts)))
  15076. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15077. (setq with-hm t))
  15078. (setq time0 (org-parse-time-string ts))
  15079. (when (and updown
  15080. (eq org-ts-what 'minute)
  15081. (not current-prefix-arg))
  15082. ;; This looks like s-up and s-down. Change by one rounding step.
  15083. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15084. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15085. (setcar (cdr time0) (+ (nth 1 time0)
  15086. (if (> n 0) (- rem) (- dm rem))))))
  15087. (setq time
  15088. (encode-time (or (car time0) 0)
  15089. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15090. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15091. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15092. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15093. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15094. (nthcdr 6 time0)))
  15095. (when (and (member org-ts-what '(hour minute))
  15096. extra
  15097. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15098. (setq extra (org-modify-ts-extra
  15099. extra
  15100. (if (eq org-ts-what 'hour) 2 5)
  15101. n dm)))
  15102. (when (integerp org-ts-what)
  15103. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15104. (if (eq what 'calendar)
  15105. (let ((cal-date (org-get-date-from-calendar)))
  15106. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15107. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15108. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15109. (setcar time0 (or (car time0) 0))
  15110. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15111. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15112. (setq time (apply 'encode-time time0))))
  15113. ;; Insert the new time-stamp, and ensure point stays in the same
  15114. ;; category as before (i.e. not after the last position in that
  15115. ;; category).
  15116. (let ((pos (point)))
  15117. ;; Stay before inserted string. `save-excursion' is of no use.
  15118. (setq org-last-changed-timestamp
  15119. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15120. (goto-char pos))
  15121. (save-match-data
  15122. (looking-at org-ts-regexp3)
  15123. (goto-char (cond
  15124. ;; `day' category ends before `hour' if any, or at
  15125. ;; the end of the day name.
  15126. ((eq origin-cat 'day)
  15127. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15128. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15129. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15130. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15131. ;; `year' and `month' have both fixed size: point
  15132. ;; couldn't have moved into another part.
  15133. (t origin))))
  15134. ;; Update clock if on a CLOCK line.
  15135. (org-clock-update-time-maybe)
  15136. ;; Maybe adjust the closest clock in `org-clock-history'
  15137. (when org-clock-adjust-closest
  15138. (if (not (and (org-at-clock-log-p)
  15139. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15140. org-clock-history))))))
  15141. (message "No clock to adjust")
  15142. (cond ((save-excursion ; fix previous clock?
  15143. (re-search-backward org-ts-regexp0 nil t)
  15144. (org-looking-back (concat org-clock-string " \\[")))
  15145. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15146. ((save-excursion ; fix next clock?
  15147. (re-search-backward org-ts-regexp0 nil t)
  15148. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15149. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15150. (save-window-excursion
  15151. ;; Find closest clock to point, adjust the previous/next one in history
  15152. (let* ((p (save-excursion (org-back-to-heading t)))
  15153. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15154. (clfixnth
  15155. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15156. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15157. (if (not clfixpos)
  15158. (message "No clock to adjust")
  15159. (save-excursion
  15160. (org-goto-marker-or-bmk clfixpos)
  15161. (org-show-subtree)
  15162. (when (re-search-forward clrgx nil t)
  15163. (goto-char (match-beginning 1))
  15164. (let (org-clock-adjust-closest)
  15165. (org-timestamp-change n org-ts-what updown))
  15166. (message "Clock adjusted in %s for heading: %s"
  15167. (file-name-nondirectory (buffer-file-name))
  15168. (org-get-heading t t)))))))))
  15169. ;; Try to recenter the calendar window, if any.
  15170. (if (and org-calendar-follow-timestamp-change
  15171. (get-buffer-window "*Calendar*" t)
  15172. (memq org-ts-what '(day month year)))
  15173. (org-recenter-calendar (time-to-days time))))))
  15174. (defun org-modify-ts-extra (s pos n dm)
  15175. "Change the different parts of the lead-time and repeat fields in timestamp."
  15176. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15177. ng h m new rem)
  15178. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15179. (cond
  15180. ((or (org-pos-in-match-range pos 2)
  15181. (org-pos-in-match-range pos 3))
  15182. (setq m (string-to-number (match-string 3 s))
  15183. h (string-to-number (match-string 2 s)))
  15184. (if (org-pos-in-match-range pos 2)
  15185. (setq h (+ h n))
  15186. (setq n (* dm (org-no-warnings (signum n))))
  15187. (when (not (= 0 (setq rem (% m dm))))
  15188. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15189. (setq m (+ m n)))
  15190. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15191. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15192. (setq h (min 24 (max 0 h)))
  15193. (setq ng 1 new (format "-%02d:%02d" h m)))
  15194. ((org-pos-in-match-range pos 6)
  15195. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15196. ((org-pos-in-match-range pos 5)
  15197. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15198. ((org-pos-in-match-range pos 9)
  15199. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15200. ((org-pos-in-match-range pos 8)
  15201. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15202. (when ng
  15203. (setq s (concat
  15204. (substring s 0 (match-beginning ng))
  15205. new
  15206. (substring s (match-end ng))))))
  15207. s))
  15208. (defun org-recenter-calendar (date)
  15209. "If the calendar is visible, recenter it to DATE."
  15210. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15211. (when cwin
  15212. (let ((calendar-move-hook nil))
  15213. (with-selected-window cwin
  15214. (calendar-goto-date (if (listp date) date
  15215. (calendar-gregorian-from-absolute date))))))))
  15216. (defun org-goto-calendar (&optional arg)
  15217. "Go to the Emacs calendar at the current date.
  15218. If there is a time stamp in the current line, go to that date.
  15219. A prefix ARG can be used to force the current date."
  15220. (interactive "P")
  15221. (let ((tsr org-ts-regexp) diff
  15222. (calendar-move-hook nil)
  15223. (calendar-view-holidays-initially-flag nil)
  15224. (calendar-view-diary-initially-flag nil))
  15225. (if (or (org-at-timestamp-p)
  15226. (save-excursion
  15227. (beginning-of-line 1)
  15228. (looking-at (concat ".*" tsr))))
  15229. (let ((d1 (time-to-days (current-time)))
  15230. (d2 (time-to-days
  15231. (org-time-string-to-time (match-string 1)))))
  15232. (setq diff (- d2 d1))))
  15233. (calendar)
  15234. (calendar-goto-today)
  15235. (if (and diff (not arg)) (calendar-forward-day diff))))
  15236. (defun org-get-date-from-calendar ()
  15237. "Return a list (month day year) of date at point in calendar."
  15238. (with-current-buffer "*Calendar*"
  15239. (save-match-data
  15240. (calendar-cursor-to-date))))
  15241. (defun org-date-from-calendar ()
  15242. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15243. If there is already a time stamp at the cursor position, update it."
  15244. (interactive)
  15245. (if (org-at-timestamp-p t)
  15246. (org-timestamp-change 0 'calendar)
  15247. (let ((cal-date (org-get-date-from-calendar)))
  15248. (org-insert-time-stamp
  15249. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15250. (defun org-minutes-to-hh:mm-string (m)
  15251. "Compute H:MM from a number of minutes."
  15252. (let ((h (/ m 60)))
  15253. (setq m (- m (* 60 h)))
  15254. (format org-time-clocksum-format h m)))
  15255. (defun org-hh:mm-string-to-minutes (s)
  15256. "Convert a string H:MM to a number of minutes.
  15257. If the string is just a number, interpret it as minutes.
  15258. In fact, the first hh:mm or number in the string will be taken,
  15259. there can be extra stuff in the string.
  15260. If no number is found, the return value is 0."
  15261. (cond
  15262. ((integerp s) s)
  15263. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15264. (+ (* (string-to-number (match-string 1 s)) 60)
  15265. (string-to-number (match-string 2 s))))
  15266. ((string-match "\\([0-9]+\\)" s)
  15267. (string-to-number (match-string 1 s)))
  15268. (t 0)))
  15269. (defcustom org-effort-durations
  15270. `(("h" . 60)
  15271. ("d" . ,(* 60 8))
  15272. ("w" . ,(* 60 8 5))
  15273. ("m" . ,(* 60 8 5 4))
  15274. ("y" . ,(* 60 8 5 40)))
  15275. "Conversion factor to minutes for an effort modifier.
  15276. Each entry has the form (MODIFIER . MINUTES).
  15277. In an effort string, a number followed by MODIFIER is multiplied
  15278. by the specified number of MINUTES to obtain an effort in
  15279. minutes.
  15280. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15281. effort string \"2hours\" is equivalent to 120 minutes."
  15282. :group 'org-agenda
  15283. :version "24.1"
  15284. :type '(alist :key-type (string :tag "Modifier")
  15285. :value-type (number :tag "Minutes")))
  15286. (defcustom org-agenda-inhibit-startup t
  15287. "Inhibit startup when preparing agenda buffers.
  15288. When this variable is `t' (the default), the initialization of
  15289. the Org agenda buffers is inhibited: e.g. the visibility state
  15290. is not set, the tables are not re-aligned, etc."
  15291. :type 'boolean
  15292. :version "24.3"
  15293. :group 'org-agenda)
  15294. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15295. "Convert a duration string S to minutes.
  15296. A bare number is interpreted as minutes, modifiers can be set by
  15297. customizing `org-effort-durations' (which see).
  15298. Entries containing a colon are interpreted as H:MM by
  15299. `org-hh:mm-string-to-minutes'."
  15300. (let ((result 0)
  15301. (re (concat "\\([0-9.]+\\) *\\("
  15302. (regexp-opt (mapcar 'car org-effort-durations))
  15303. "\\)")))
  15304. (while (string-match re s)
  15305. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15306. (string-to-number (match-string 1 s))))
  15307. (setq s (replace-match "" nil t s)))
  15308. (setq result (floor result))
  15309. (incf result (org-hh:mm-string-to-minutes s))
  15310. (if output-to-string (number-to-string result) result)))
  15311. ;;;; Files
  15312. (defun org-save-all-org-buffers ()
  15313. "Save all Org-mode buffers without user confirmation."
  15314. (interactive)
  15315. (message "Saving all Org-mode buffers...")
  15316. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15317. (when (featurep 'org-id) (org-id-locations-save))
  15318. (message "Saving all Org-mode buffers... done"))
  15319. (defun org-revert-all-org-buffers ()
  15320. "Revert all Org-mode buffers.
  15321. Prompt for confirmation when there are unsaved changes.
  15322. Be sure you know what you are doing before letting this function
  15323. overwrite your changes.
  15324. This function is useful in a setup where one tracks org files
  15325. with a version control system, to revert on one machine after pulling
  15326. changes from another. I believe the procedure must be like this:
  15327. 1. M-x org-save-all-org-buffers
  15328. 2. Pull changes from the other machine, resolve conflicts
  15329. 3. M-x org-revert-all-org-buffers"
  15330. (interactive)
  15331. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15332. (error "Abort"))
  15333. (save-excursion
  15334. (save-window-excursion
  15335. (mapc
  15336. (lambda (b)
  15337. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15338. (with-current-buffer b buffer-file-name))
  15339. (org-pop-to-buffer-same-window b)
  15340. (revert-buffer t 'no-confirm)))
  15341. (buffer-list))
  15342. (when (and (featurep 'org-id) org-id-track-globally)
  15343. (org-id-locations-load)))))
  15344. ;;;; Agenda files
  15345. ;;;###autoload
  15346. (defun org-switchb (&optional arg)
  15347. "Switch between Org buffers.
  15348. With one prefix argument, restrict available buffers to files.
  15349. With two prefix arguments, restrict available buffers to agenda files.
  15350. Defaults to `iswitchb' for buffer name completion.
  15351. Set `org-completion-use-ido' to make it use ido instead."
  15352. (interactive "P")
  15353. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15354. ((equal arg '(16)) (org-buffer-list 'agenda))
  15355. (t (org-buffer-list))))
  15356. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15357. (org-completion-use-ido org-completion-use-ido))
  15358. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15359. (setq org-completion-use-iswitchb t))
  15360. (org-pop-to-buffer-same-window
  15361. (org-icompleting-read "Org buffer: "
  15362. (mapcar 'list (mapcar 'buffer-name blist))
  15363. nil t))))
  15364. ;;; Define some older names previously used for this functionality
  15365. ;;;###autoload
  15366. (defalias 'org-ido-switchb 'org-switchb)
  15367. ;;;###autoload
  15368. (defalias 'org-iswitchb 'org-switchb)
  15369. (defun org-buffer-list (&optional predicate exclude-tmp)
  15370. "Return a list of Org buffers.
  15371. PREDICATE can be `export', `files' or `agenda'.
  15372. export restrict the list to Export buffers.
  15373. files restrict the list to buffers visiting Org files.
  15374. agenda restrict the list to buffers visiting agenda files.
  15375. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15376. (let* ((bfn nil)
  15377. (agenda-files (and (eq predicate 'agenda)
  15378. (mapcar 'file-truename (org-agenda-files t))))
  15379. (filter
  15380. (cond
  15381. ((eq predicate 'files)
  15382. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15383. ((eq predicate 'export)
  15384. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15385. ((eq predicate 'agenda)
  15386. (lambda (b)
  15387. (with-current-buffer b
  15388. (and (derived-mode-p 'org-mode)
  15389. (setq bfn (buffer-file-name b))
  15390. (member (file-truename bfn) agenda-files)))))
  15391. (t (lambda (b) (with-current-buffer b
  15392. (or (derived-mode-p 'org-mode)
  15393. (string-match "\*Org .*Export"
  15394. (buffer-name b)))))))))
  15395. (delq nil
  15396. (mapcar
  15397. (lambda(b)
  15398. (if (and (funcall filter b)
  15399. (or (not exclude-tmp)
  15400. (not (string-match "tmp" (buffer-name b)))))
  15401. b
  15402. nil))
  15403. (buffer-list)))))
  15404. (defun org-agenda-files (&optional unrestricted archives)
  15405. "Get the list of agenda files.
  15406. Optional UNRESTRICTED means return the full list even if a restriction
  15407. is currently in place.
  15408. When ARCHIVES is t, include all archive files that are really being
  15409. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15410. `org-agenda-archives-mode' is t."
  15411. (let ((files
  15412. (cond
  15413. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15414. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15415. ((listp org-agenda-files) org-agenda-files)
  15416. (t (error "Invalid value of `org-agenda-files'")))))
  15417. (setq files (apply 'append
  15418. (mapcar (lambda (f)
  15419. (if (file-directory-p f)
  15420. (directory-files
  15421. f t org-agenda-file-regexp)
  15422. (list f)))
  15423. files)))
  15424. (when org-agenda-skip-unavailable-files
  15425. (setq files (delq nil
  15426. (mapcar (function
  15427. (lambda (file)
  15428. (and (file-readable-p file) file)))
  15429. files))))
  15430. (when (or (eq archives t)
  15431. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15432. (setq files (org-add-archive-files files)))
  15433. files))
  15434. (defun org-agenda-file-p (&optional file)
  15435. "Return non-nil, if FILE is an agenda file.
  15436. If FILE is omitted, use the file associated with the current
  15437. buffer."
  15438. (member (or file (buffer-file-name))
  15439. (org-agenda-files t)))
  15440. (defun org-edit-agenda-file-list ()
  15441. "Edit the list of agenda files.
  15442. Depending on setup, this either uses customize to edit the variable
  15443. `org-agenda-files', or it visits the file that is holding the list. In the
  15444. latter case, the buffer is set up in a way that saving it automatically kills
  15445. the buffer and restores the previous window configuration."
  15446. (interactive)
  15447. (if (stringp org-agenda-files)
  15448. (let ((cw (current-window-configuration)))
  15449. (find-file org-agenda-files)
  15450. (org-set-local 'org-window-configuration cw)
  15451. (org-add-hook 'after-save-hook
  15452. (lambda ()
  15453. (set-window-configuration
  15454. (prog1 org-window-configuration
  15455. (kill-buffer (current-buffer))))
  15456. (org-install-agenda-files-menu)
  15457. (message "New agenda file list installed"))
  15458. nil 'local)
  15459. (message "%s" (substitute-command-keys
  15460. "Edit list and finish with \\[save-buffer]")))
  15461. (customize-variable 'org-agenda-files)))
  15462. (defun org-store-new-agenda-file-list (list)
  15463. "Set new value for the agenda file list and save it correctly."
  15464. (if (stringp org-agenda-files)
  15465. (let ((fe (org-read-agenda-file-list t)) b u)
  15466. (while (setq b (find-buffer-visiting org-agenda-files))
  15467. (kill-buffer b))
  15468. (with-temp-file org-agenda-files
  15469. (insert
  15470. (mapconcat
  15471. (lambda (f) ;; Keep un-expanded entries.
  15472. (if (setq u (assoc f fe))
  15473. (cdr u)
  15474. f))
  15475. list "\n")
  15476. "\n")))
  15477. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15478. (org-insert-mode-line-in-empty-file nil))
  15479. (setq org-agenda-files list)
  15480. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15481. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15482. "Read the list of agenda files from a file.
  15483. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15484. filenames, used by `org-store-new-agenda-file-list' to write back
  15485. un-expanded file names."
  15486. (when (file-directory-p org-agenda-files)
  15487. (error "`org-agenda-files' cannot be a single directory"))
  15488. (when (stringp org-agenda-files)
  15489. (with-temp-buffer
  15490. (insert-file-contents org-agenda-files)
  15491. (mapcar
  15492. (lambda (f)
  15493. (let ((e (expand-file-name (substitute-in-file-name f)
  15494. org-directory)))
  15495. (if pair-with-expansion
  15496. (cons e f)
  15497. e)))
  15498. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15499. ;;;###autoload
  15500. (defun org-cycle-agenda-files ()
  15501. "Cycle through the files in `org-agenda-files'.
  15502. If the current buffer visits an agenda file, find the next one in the list.
  15503. If the current buffer does not, find the first agenda file."
  15504. (interactive)
  15505. (let* ((fs (org-agenda-files t))
  15506. (files (append fs (list (car fs))))
  15507. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15508. file)
  15509. (unless files (error "No agenda files"))
  15510. (catch 'exit
  15511. (while (setq file (pop files))
  15512. (if (equal (file-truename file) tcf)
  15513. (when (car files)
  15514. (find-file (car files))
  15515. (throw 'exit t))))
  15516. (find-file (car fs)))
  15517. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15518. (defun org-agenda-file-to-front (&optional to-end)
  15519. "Move/add the current file to the top of the agenda file list.
  15520. If the file is not present in the list, it is added to the front. If it is
  15521. present, it is moved there. With optional argument TO-END, add/move to the
  15522. end of the list."
  15523. (interactive "P")
  15524. (let ((org-agenda-skip-unavailable-files nil)
  15525. (file-alist (mapcar (lambda (x)
  15526. (cons (file-truename x) x))
  15527. (org-agenda-files t)))
  15528. (ctf (file-truename
  15529. (or buffer-file-name
  15530. (error "Please save the current buffer to a file"))))
  15531. x had)
  15532. (setq x (assoc ctf file-alist) had x)
  15533. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15534. (if to-end
  15535. (setq file-alist (append (delq x file-alist) (list x)))
  15536. (setq file-alist (cons x (delq x file-alist))))
  15537. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15538. (org-install-agenda-files-menu)
  15539. (message "File %s to %s of agenda file list"
  15540. (if had "moved" "added") (if to-end "end" "front"))))
  15541. (defun org-remove-file (&optional file)
  15542. "Remove current file from the list of files in variable `org-agenda-files'.
  15543. These are the files which are being checked for agenda entries.
  15544. Optional argument FILE means use this file instead of the current."
  15545. (interactive)
  15546. (let* ((org-agenda-skip-unavailable-files nil)
  15547. (file (or file buffer-file-name
  15548. (error "Current buffer does not visit a file")))
  15549. (true-file (file-truename file))
  15550. (afile (abbreviate-file-name file))
  15551. (files (delq nil (mapcar
  15552. (lambda (x)
  15553. (if (equal true-file
  15554. (file-truename x))
  15555. nil x))
  15556. (org-agenda-files t)))))
  15557. (if (not (= (length files) (length (org-agenda-files t))))
  15558. (progn
  15559. (org-store-new-agenda-file-list files)
  15560. (org-install-agenda-files-menu)
  15561. (message "Removed file: %s" afile))
  15562. (message "File was not in list: %s (not removed)" afile))))
  15563. (defun org-file-menu-entry (file)
  15564. (vector file (list 'find-file file) t))
  15565. (defun org-check-agenda-file (file)
  15566. "Make sure FILE exists. If not, ask user what to do."
  15567. (when (not (file-exists-p file))
  15568. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  15569. (abbreviate-file-name file))
  15570. (let ((r (downcase (read-char-exclusive))))
  15571. (cond
  15572. ((equal r ?r)
  15573. (org-remove-file file)
  15574. (throw 'nextfile t))
  15575. (t (error "Abort"))))))
  15576. (defun org-get-agenda-file-buffer (file)
  15577. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15578. it to the list of buffers which might be released later."
  15579. (let ((buf (org-find-base-buffer-visiting file)))
  15580. (if buf
  15581. buf ; just return it
  15582. ;; Make a new buffer and remember it
  15583. (setq buf (find-file-noselect file))
  15584. (if buf (push buf org-agenda-new-buffers))
  15585. buf)))
  15586. (defun org-release-buffers (blist)
  15587. "Release all buffers in list, asking the user for confirmation when needed.
  15588. When a buffer is unmodified, it is just killed. When modified, it is saved
  15589. \(if the user agrees) and then killed."
  15590. (let (buf file)
  15591. (while (setq buf (pop blist))
  15592. (setq file (buffer-file-name buf))
  15593. (when (and (buffer-modified-p buf)
  15594. file
  15595. (y-or-n-p (format "Save file %s? " file)))
  15596. (with-current-buffer buf (save-buffer)))
  15597. (kill-buffer buf))))
  15598. (defun org-agenda-prepare-buffers (files)
  15599. "Create buffers for all agenda files, protect archived trees and comments."
  15600. (interactive)
  15601. (let ((pa '(:org-archived t))
  15602. (pc '(:org-comment t))
  15603. (pall '(:org-archived t :org-comment t))
  15604. (inhibit-read-only t)
  15605. (org-inhibit-startup org-agenda-inhibit-startup)
  15606. (rea (concat ":" org-archive-tag ":"))
  15607. bmp file re)
  15608. (save-excursion
  15609. (save-restriction
  15610. (while (setq file (pop files))
  15611. (catch 'nextfile
  15612. (if (bufferp file)
  15613. (set-buffer file)
  15614. (org-check-agenda-file file)
  15615. (set-buffer (org-get-agenda-file-buffer file)))
  15616. (widen)
  15617. (setq bmp (buffer-modified-p))
  15618. (org-refresh-category-properties)
  15619. (org-refresh-properties org-effort-property 'org-effort)
  15620. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  15621. (setq org-todo-keywords-for-agenda
  15622. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15623. (setq org-done-keywords-for-agenda
  15624. (append org-done-keywords-for-agenda org-done-keywords))
  15625. (setq org-todo-keyword-alist-for-agenda
  15626. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15627. (setq org-drawers-for-agenda
  15628. (append org-drawers-for-agenda org-drawers))
  15629. (setq org-tag-alist-for-agenda
  15630. (append org-tag-alist-for-agenda org-tag-alist))
  15631. (save-excursion
  15632. (remove-text-properties (point-min) (point-max) pall)
  15633. (when org-agenda-skip-archived-trees
  15634. (goto-char (point-min))
  15635. (while (re-search-forward rea nil t)
  15636. (if (org-at-heading-p t)
  15637. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15638. (goto-char (point-min))
  15639. (setq re (format org-heading-keyword-regexp-format
  15640. org-comment-string))
  15641. (while (re-search-forward re nil t)
  15642. (add-text-properties
  15643. (match-beginning 0) (org-end-of-subtree t) pc)))
  15644. (set-buffer-modified-p bmp)))))
  15645. (setq org-todo-keywords-for-agenda
  15646. (org-uniquify org-todo-keywords-for-agenda))
  15647. (setq org-todo-keyword-alist-for-agenda
  15648. (org-uniquify org-todo-keyword-alist-for-agenda)
  15649. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15650. ;;;; Embedded LaTeX
  15651. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15652. "Keymap for the minor `org-cdlatex-mode'.")
  15653. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15654. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15655. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15656. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15657. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15658. (defvar org-cdlatex-texmathp-advice-is-done nil
  15659. "Flag remembering if we have applied the advice to texmathp already.")
  15660. (define-minor-mode org-cdlatex-mode
  15661. "Toggle the minor `org-cdlatex-mode'.
  15662. This mode supports entering LaTeX environment and math in LaTeX fragments
  15663. in Org-mode.
  15664. \\{org-cdlatex-mode-map}"
  15665. nil " OCDL" nil
  15666. (when org-cdlatex-mode
  15667. (require 'cdlatex)
  15668. (run-hooks 'cdlatex-mode-hook)
  15669. (cdlatex-compute-tables))
  15670. (unless org-cdlatex-texmathp-advice-is-done
  15671. (setq org-cdlatex-texmathp-advice-is-done t)
  15672. (defadvice texmathp (around org-math-always-on activate)
  15673. "Always return t in org-mode buffers.
  15674. This is because we want to insert math symbols without dollars even outside
  15675. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15676. an embedded LaTeX fragment, let texmathp do its job.
  15677. \\[org-cdlatex-mode-map]"
  15678. (interactive)
  15679. (let (p)
  15680. (cond
  15681. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15682. ((eq this-command 'cdlatex-math-symbol)
  15683. (setq ad-return-value t
  15684. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15685. (t
  15686. (let ((p (org-inside-LaTeX-fragment-p)))
  15687. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15688. (setq ad-return-value t
  15689. texmathp-why '("Org-mode embedded math" . 0))
  15690. (if p ad-do-it)))))))))
  15691. (defun turn-on-org-cdlatex ()
  15692. "Unconditionally turn on `org-cdlatex-mode'."
  15693. (org-cdlatex-mode 1))
  15694. (defun org-inside-LaTeX-fragment-p ()
  15695. "Test if point is inside a LaTeX fragment.
  15696. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15697. sequence appearing also before point.
  15698. Even though the matchers for math are configurable, this function assumes
  15699. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15700. delimiters are skipped when they have been removed by customization.
  15701. The return value is nil, or a cons cell with the delimiter and the
  15702. position of this delimiter.
  15703. This function does a reasonably good job, but can locally be fooled by
  15704. for example currency specifications. For example it will assume being in
  15705. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15706. fragments that are properly closed, but during editing, we have to live
  15707. with the uncertainty caused by missing closing delimiters. This function
  15708. looks only before point, not after."
  15709. (catch 'exit
  15710. (let ((pos (point))
  15711. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15712. (lim (progn
  15713. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15714. (point)))
  15715. dd-on str (start 0) m re)
  15716. (goto-char pos)
  15717. (when dodollar
  15718. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15719. re (nth 1 (assoc "$" org-latex-regexps)))
  15720. (while (string-match re str start)
  15721. (cond
  15722. ((= (match-end 0) (length str))
  15723. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15724. ((= (match-end 0) (- (length str) 5))
  15725. (throw 'exit nil))
  15726. (t (setq start (match-end 0))))))
  15727. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15728. (goto-char pos)
  15729. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15730. (and (match-beginning 2) (throw 'exit nil))
  15731. ;; count $$
  15732. (while (re-search-backward "\\$\\$" lim t)
  15733. (setq dd-on (not dd-on)))
  15734. (goto-char pos)
  15735. (if dd-on (cons "$$" m))))))
  15736. (defun org-inside-latex-macro-p ()
  15737. "Is point inside a LaTeX macro or its arguments?"
  15738. (save-match-data
  15739. (org-in-regexp
  15740. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15741. (defun org-try-cdlatex-tab ()
  15742. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15743. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15744. - inside a LaTeX fragment, or
  15745. - after the first word in a line, where an abbreviation expansion could
  15746. insert a LaTeX environment."
  15747. (when org-cdlatex-mode
  15748. (cond
  15749. ;; Before any word on the line: No expansion possible.
  15750. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15751. ;; Just after first word on the line: Expand it. Make sure it
  15752. ;; cannot happen on headlines, though.
  15753. ((save-excursion
  15754. (skip-chars-backward "a-zA-Z0-9*")
  15755. (skip-chars-backward " \t")
  15756. (and (bolp) (not (org-at-heading-p))))
  15757. (cdlatex-tab) t)
  15758. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15759. (defun org-cdlatex-underscore-caret (&optional arg)
  15760. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15761. Revert to the normal definition outside of these fragments."
  15762. (interactive "P")
  15763. (if (org-inside-LaTeX-fragment-p)
  15764. (call-interactively 'cdlatex-sub-superscript)
  15765. (let (org-cdlatex-mode)
  15766. (call-interactively (key-binding (vector last-input-event))))))
  15767. (defun org-cdlatex-math-modify (&optional arg)
  15768. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15769. Revert to the normal definition outside of these fragments."
  15770. (interactive "P")
  15771. (if (org-inside-LaTeX-fragment-p)
  15772. (call-interactively 'cdlatex-math-modify)
  15773. (let (org-cdlatex-mode)
  15774. (call-interactively (key-binding (vector last-input-event))))))
  15775. (defvar org-latex-fragment-image-overlays nil
  15776. "List of overlays carrying the images of latex fragments.")
  15777. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15778. (defun org-remove-latex-fragment-image-overlays ()
  15779. "Remove all overlays with LaTeX fragment images in current buffer."
  15780. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15781. (setq org-latex-fragment-image-overlays nil))
  15782. (defun org-preview-latex-fragment (&optional subtree)
  15783. "Preview the LaTeX fragment at point, or all locally or globally.
  15784. If the cursor is in a LaTeX fragment, create the image and overlay
  15785. it over the source code. If there is no fragment at point, display
  15786. all fragments in the current text, from one headline to the next. With
  15787. prefix SUBTREE, display all fragments in the current subtree. With a
  15788. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15789. the cursor is before the first headline,
  15790. display all fragments in the buffer.
  15791. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15792. (interactive "P")
  15793. (unless buffer-file-name
  15794. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15795. (org-remove-latex-fragment-image-overlays)
  15796. (save-excursion
  15797. (save-restriction
  15798. (let (beg end at msg)
  15799. (cond
  15800. ((or (equal subtree '(16))
  15801. (not (save-excursion
  15802. (re-search-backward org-outline-regexp-bol nil t))))
  15803. (setq beg (point-min) end (point-max)
  15804. msg "Creating images for buffer...%s"))
  15805. ((equal subtree '(4))
  15806. (org-back-to-heading)
  15807. (setq beg (point) end (org-end-of-subtree t)
  15808. msg "Creating images for subtree...%s"))
  15809. (t
  15810. (if (setq at (org-inside-LaTeX-fragment-p))
  15811. (goto-char (max (point-min) (- (cdr at) 2)))
  15812. (org-back-to-heading))
  15813. (setq beg (point) end (progn (outline-next-heading) (point))
  15814. msg (if at "Creating image...%s"
  15815. "Creating images for entry...%s"))))
  15816. (message msg "")
  15817. (narrow-to-region beg end)
  15818. (goto-char beg)
  15819. (org-format-latex
  15820. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  15821. (file-name-nondirectory
  15822. buffer-file-name)))
  15823. default-directory 'overlays msg at 'forbuffer
  15824. org-latex-create-formula-image-program)
  15825. (message msg "done. Use `C-c C-c' to remove images.")))))
  15826. (defvar org-latex-regexps
  15827. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15828. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15829. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15830. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15831. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15832. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15833. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15834. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15835. "Regular expressions for matching embedded LaTeX.")
  15836. (defvar org-export-have-math nil) ;; dynamic scoping
  15837. (defun org-format-latex (prefix &optional dir overlays msg at
  15838. forbuffer processing-type)
  15839. "Replace LaTeX fragments with links to an image, and produce images.
  15840. Some of the options can be changed using the variable
  15841. `org-format-latex-options'."
  15842. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15843. (let* ((prefixnodir (file-name-nondirectory prefix))
  15844. (absprefix (expand-file-name prefix dir))
  15845. (todir (file-name-directory absprefix))
  15846. (opt org-format-latex-options)
  15847. (matchers (plist-get opt :matchers))
  15848. (re-list org-latex-regexps)
  15849. (org-format-latex-header-extra
  15850. (plist-get (org-infile-export-plist) :latex-header-extra))
  15851. (cnt 0) txt hash link beg end re e checkdir
  15852. executables-checked string
  15853. m n block-type block linkfile movefile ov)
  15854. ;; Check the different regular expressions
  15855. (while (setq e (pop re-list))
  15856. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15857. block (if block-type "\n\n" ""))
  15858. (when (member m matchers)
  15859. (goto-char (point-min))
  15860. (while (re-search-forward re nil t)
  15861. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15862. (not (get-text-property (match-beginning n)
  15863. 'org-protected))
  15864. (or (not overlays)
  15865. (not (eq (get-char-property (match-beginning n)
  15866. 'org-overlay-type)
  15867. 'org-latex-overlay))))
  15868. (setq org-export-have-math t)
  15869. (cond
  15870. ((eq processing-type 'verbatim)
  15871. ;; Leave the text verbatim, just protect it
  15872. (add-text-properties (match-beginning n) (match-end n)
  15873. '(org-protected t)))
  15874. ((eq processing-type 'mathjax)
  15875. ;; Prepare for MathJax processing
  15876. (setq string (match-string n))
  15877. (if (member m '("$" "$1"))
  15878. (save-excursion
  15879. (delete-region (match-beginning n) (match-end n))
  15880. (goto-char (match-beginning n))
  15881. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15882. "\\)")
  15883. '(org-protected t))))
  15884. (add-text-properties (match-beginning n) (match-end n)
  15885. '(org-protected t))))
  15886. ((or (eq processing-type 'dvipng)
  15887. (eq processing-type 'imagemagick))
  15888. ;; Process to an image
  15889. (setq txt (match-string n)
  15890. beg (match-beginning n) end (match-end n)
  15891. cnt (1+ cnt))
  15892. (let (print-length print-level) ; make sure full list is printed
  15893. (setq hash (sha1 (prin1-to-string
  15894. (list org-format-latex-header
  15895. org-format-latex-header-extra
  15896. org-export-latex-default-packages-alist
  15897. org-export-latex-packages-alist
  15898. org-format-latex-options
  15899. forbuffer txt)))
  15900. linkfile (format "%s_%s.png" prefix hash)
  15901. movefile (format "%s_%s.png" absprefix hash)))
  15902. (setq link (concat block "[[file:" linkfile "]]" block))
  15903. (if msg (message msg cnt))
  15904. (goto-char beg)
  15905. (unless checkdir ; make sure the directory exists
  15906. (setq checkdir t)
  15907. (or (file-directory-p todir) (make-directory todir t)))
  15908. (cond
  15909. ((eq processing-type 'dvipng)
  15910. (unless executables-checked
  15911. (org-check-external-command
  15912. "latex" "needed to convert LaTeX fragments to images")
  15913. (org-check-external-command
  15914. "dvipng" "needed to convert LaTeX fragments to images")
  15915. (setq executables-checked t))
  15916. (unless (file-exists-p movefile)
  15917. (org-create-formula-image-with-dvipng
  15918. txt movefile opt forbuffer)))
  15919. ((eq processing-type 'imagemagick)
  15920. (unless executables-checked
  15921. (org-check-external-command
  15922. "convert" "you need to install imagemagick")
  15923. (setq executables-checked t))
  15924. (unless (file-exists-p movefile)
  15925. (org-create-formula-image-with-imagemagick
  15926. txt movefile opt forbuffer))))
  15927. (if overlays
  15928. (progn
  15929. (mapc (lambda (o)
  15930. (if (eq (overlay-get o 'org-overlay-type)
  15931. 'org-latex-overlay)
  15932. (delete-overlay o)))
  15933. (overlays-in beg end))
  15934. (setq ov (make-overlay beg end))
  15935. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15936. (if (featurep 'xemacs)
  15937. (progn
  15938. (overlay-put ov 'invisible t)
  15939. (overlay-put
  15940. ov 'end-glyph
  15941. (make-glyph (vector 'png :file movefile))))
  15942. (overlay-put
  15943. ov 'display
  15944. (list 'image :type 'png :file movefile :ascent 'center)))
  15945. (push ov org-latex-fragment-image-overlays)
  15946. (goto-char end))
  15947. (delete-region beg end)
  15948. (insert (org-add-props link
  15949. (list 'org-latex-src
  15950. (replace-regexp-in-string
  15951. "\"" "" txt)
  15952. 'org-latex-src-embed-type
  15953. (if block-type 'paragraph 'character))))))
  15954. ((eq processing-type 'mathml)
  15955. ;; Process to MathML
  15956. (unless executables-checked
  15957. (unless (save-match-data (org-format-latex-mathml-available-p))
  15958. (error "LaTeX to MathML converter not configured"))
  15959. (setq executables-checked t))
  15960. (setq txt (match-string n)
  15961. beg (match-beginning n) end (match-end n)
  15962. cnt (1+ cnt))
  15963. (if msg (message msg cnt))
  15964. (goto-char beg)
  15965. (delete-region beg end)
  15966. (insert (org-format-latex-as-mathml
  15967. txt block-type prefix dir)))
  15968. (t
  15969. (error "Unknown conversion type %s for latex fragments"
  15970. processing-type)))))))))
  15971. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15972. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15973. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15974. sucessful, return the portion between \"<math...> </math>\"
  15975. elements otherwise return nil. When MATHML-FILE is specified,
  15976. write the results in to that file. When invoked as an
  15977. interactive command, prompt for LATEX-FRAG, with initial value
  15978. set to the current active region and echo the results for user
  15979. inspection."
  15980. (interactive (list (let ((frag (when (org-region-active-p)
  15981. (buffer-substring-no-properties
  15982. (region-beginning) (region-end)))))
  15983. (read-string "LaTeX Fragment: " frag nil frag))))
  15984. (unless latex-frag (error "Invalid latex-frag"))
  15985. (let* ((tmp-in-file (file-relative-name
  15986. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15987. (ignore (write-region latex-frag nil tmp-in-file))
  15988. (tmp-out-file (file-relative-name
  15989. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15990. (cmd (format-spec
  15991. org-latex-to-mathml-convert-command
  15992. `((?j . ,(shell-quote-argument
  15993. (expand-file-name org-latex-to-mathml-jar-file)))
  15994. (?I . ,(shell-quote-argument tmp-in-file))
  15995. (?o . ,(shell-quote-argument tmp-out-file)))))
  15996. mathml shell-command-output)
  15997. (when (org-called-interactively-p 'any)
  15998. (unless (org-format-latex-mathml-available-p)
  15999. (error "LaTeX to MathML converter not configured")))
  16000. (message "Running %s" cmd)
  16001. (setq shell-command-output (shell-command-to-string cmd))
  16002. (setq mathml
  16003. (when (file-readable-p tmp-out-file)
  16004. (with-current-buffer (find-file-noselect tmp-out-file t)
  16005. (goto-char (point-min))
  16006. (when (re-search-forward
  16007. (concat
  16008. (regexp-quote
  16009. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16010. "\\(.\\|\n\\)*"
  16011. (regexp-quote "</math>")) nil t)
  16012. (prog1 (match-string 0) (kill-buffer))))))
  16013. (cond
  16014. (mathml
  16015. (setq mathml
  16016. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16017. (when mathml-file
  16018. (write-region mathml nil mathml-file))
  16019. (when (org-called-interactively-p 'any)
  16020. (message mathml)))
  16021. ((message "LaTeX to MathML conversion failed")
  16022. (message shell-command-output)))
  16023. (delete-file tmp-in-file)
  16024. (when (file-exists-p tmp-out-file)
  16025. (delete-file tmp-out-file))
  16026. mathml))
  16027. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16028. prefix &optional dir)
  16029. "Use `org-create-math-formula' but check local cache first."
  16030. (let* ((absprefix (expand-file-name prefix dir))
  16031. (print-length nil) (print-level nil)
  16032. (formula-id (concat
  16033. "formula-"
  16034. (sha1
  16035. (prin1-to-string
  16036. (list latex-frag
  16037. org-latex-to-mathml-convert-command)))))
  16038. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16039. (formula-cache-dir (file-name-directory formula-cache)))
  16040. (unless (file-directory-p formula-cache-dir)
  16041. (make-directory formula-cache-dir t))
  16042. (unless (file-exists-p formula-cache)
  16043. (org-create-math-formula latex-frag formula-cache))
  16044. (if (file-exists-p formula-cache)
  16045. ;; Successful conversion. Return the link to MathML file.
  16046. (org-add-props
  16047. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16048. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16049. 'org-latex-src-embed-type (if latex-frag-type
  16050. 'paragraph 'character)))
  16051. ;; Failed conversion. Return the LaTeX fragment verbatim
  16052. (add-text-properties
  16053. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  16054. latex-frag)))
  16055. ;; This function borrows from Ganesh Swami's latex2png.el
  16056. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16057. "This calls dvipng."
  16058. (require 'org-latex)
  16059. (let* ((tmpdir (if (featurep 'xemacs)
  16060. (temp-directory)
  16061. temporary-file-directory))
  16062. (texfilebase (make-temp-name
  16063. (expand-file-name "orgtex" tmpdir)))
  16064. (texfile (concat texfilebase ".tex"))
  16065. (dvifile (concat texfilebase ".dvi"))
  16066. (pngfile (concat texfilebase ".png"))
  16067. (fnh (if (featurep 'xemacs)
  16068. (font-height (face-font 'default))
  16069. (face-attribute 'default :height nil)))
  16070. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16071. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16072. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16073. "Black"))
  16074. (bg (or (plist-get options (if buffer :background :html-background))
  16075. "Transparent")))
  16076. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  16077. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  16078. (with-temp-file texfile
  16079. (insert (org-splice-latex-header
  16080. org-format-latex-header
  16081. org-export-latex-default-packages-alist
  16082. org-export-latex-packages-alist t
  16083. org-format-latex-header-extra))
  16084. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  16085. (require 'org-latex)
  16086. (org-export-latex-fix-inputenc))
  16087. (let ((dir default-directory))
  16088. (condition-case nil
  16089. (progn
  16090. (cd tmpdir)
  16091. (call-process "latex" nil nil nil texfile))
  16092. (error nil))
  16093. (cd dir))
  16094. (if (not (file-exists-p dvifile))
  16095. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16096. (condition-case nil
  16097. (if (featurep 'xemacs)
  16098. (call-process "dvipng" nil nil nil
  16099. "-fg" fg "-bg" bg
  16100. "-T" "tight"
  16101. "-o" pngfile
  16102. dvifile)
  16103. (call-process "dvipng" nil nil nil
  16104. "-fg" fg "-bg" bg
  16105. "-D" dpi
  16106. ;;"-x" scale "-y" scale
  16107. "-T" "tight"
  16108. "-o" pngfile
  16109. dvifile))
  16110. (error nil))
  16111. (if (not (file-exists-p pngfile))
  16112. (if org-format-latex-signal-error
  16113. (error "Failed to create png file from %s" texfile)
  16114. (message "Failed to create png file from %s" texfile)
  16115. nil)
  16116. ;; Use the requested file name and clean up
  16117. (copy-file pngfile tofile 'replace)
  16118. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16119. (if (file-exists-p (concat texfilebase e))
  16120. (delete-file (concat texfilebase e))))
  16121. pngfile))))
  16122. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  16123. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16124. "This calls convert, which is included into imagemagick."
  16125. (require 'org-latex)
  16126. (let* ((tmpdir (if (featurep 'xemacs)
  16127. (temp-directory)
  16128. temporary-file-directory))
  16129. (texfilebase (make-temp-name
  16130. (expand-file-name "orgtex" tmpdir)))
  16131. (texfile (concat texfilebase ".tex"))
  16132. (pdffile (concat texfilebase ".pdf"))
  16133. (pngfile (concat texfilebase ".png"))
  16134. (fnh (if (featurep 'xemacs)
  16135. (font-height (face-font 'default))
  16136. (face-attribute 'default :height nil)))
  16137. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16138. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16139. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16140. "black"))
  16141. (bg (or (plist-get options (if buffer :background :html-background))
  16142. "white")))
  16143. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16144. (setq fg (org-latex-color-format fg)))
  16145. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16146. (setq bg (org-latex-color-format
  16147. (if (string= bg "Transparent")(setq bg "white")))))
  16148. (with-temp-file texfile
  16149. (insert (org-splice-latex-header
  16150. org-format-latex-header
  16151. org-export-latex-default-packages-alist
  16152. org-export-latex-packages-alist t
  16153. org-format-latex-header-extra))
  16154. (insert "\n\\begin{document}\n"
  16155. "\\definecolor{fg}{rgb}{" fg "}\n"
  16156. "\\definecolor{bg}{rgb}{" bg "}\n"
  16157. "\n\\pagecolor{bg}\n"
  16158. "\n{\\color{fg}\n"
  16159. string
  16160. "\n}\n"
  16161. "\n\\end{document}\n" )
  16162. (require 'org-latex)
  16163. (org-export-latex-fix-inputenc))
  16164. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16165. (condition-case nil
  16166. (progn
  16167. (cd tmpdir)
  16168. (setq cmds org-latex-to-pdf-process)
  16169. (while cmds
  16170. (setq latex-frags-cmds (pop cmds))
  16171. (if (listp latex-frags-cmds)
  16172. (setq cmds nil)
  16173. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16174. (while latex-frags-cmds
  16175. (setq cmd (pop latex-frags-cmds))
  16176. (while (string-match "%b" cmd)
  16177. (setq cmd (replace-match
  16178. (save-match-data
  16179. (shell-quote-argument texfile))
  16180. t t cmd)))
  16181. (while (string-match "%f" cmd)
  16182. (setq cmd (replace-match
  16183. (save-match-data
  16184. (shell-quote-argument (file-name-nondirectory texfile)))
  16185. t t cmd)))
  16186. (while (string-match "%o" cmd)
  16187. (setq cmd (replace-match
  16188. (save-match-data
  16189. (shell-quote-argument (file-name-directory texfile)))
  16190. t t cmd)))
  16191. (setq cmd (split-string cmd))
  16192. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16193. (error nil))
  16194. (cd dir))
  16195. (if (not (file-exists-p pdffile))
  16196. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16197. (condition-case nil
  16198. (if (featurep 'xemacs)
  16199. (call-process "convert" nil nil nil
  16200. "-density" "96"
  16201. "-trim"
  16202. "-antialias"
  16203. pdffile
  16204. "-quality" "100"
  16205. ;; "-sharpen" "0x1.0"
  16206. pngfile)
  16207. (call-process "convert" nil nil nil
  16208. "-density" dpi
  16209. "-trim"
  16210. "-antialias"
  16211. pdffile
  16212. "-quality" "100"
  16213. ; "-sharpen" "0x1.0"
  16214. pngfile))
  16215. (error nil))
  16216. (if (not (file-exists-p pngfile))
  16217. (if org-format-latex-signal-error
  16218. (error "Failed to create png file from %s" texfile)
  16219. (message "Failed to create png file from %s" texfile)
  16220. nil)
  16221. ;; Use the requested file name and clean up
  16222. (copy-file pngfile tofile 'replace)
  16223. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16224. (if (file-exists-p (concat texfilebase e))
  16225. (delete-file (concat texfilebase e))))
  16226. pngfile))))
  16227. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16228. "Fill a LaTeX header template TPL.
  16229. In the template, the following place holders will be recognized:
  16230. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16231. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16232. [PACKAGES] \\usepackage statements for PKG
  16233. [NO-PACKAGES] do not include PKG
  16234. [EXTRA] the string EXTRA
  16235. [NO-EXTRA] do not include EXTRA
  16236. For backward compatibility, if both the positive and the negative place
  16237. holder is missing, the positive one (without the \"NO-\") will be
  16238. assumed to be present at the end of the template.
  16239. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16240. EXTRA is a string.
  16241. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16242. (let (rpl (end ""))
  16243. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16244. (setq rpl (if (or (match-end 1) (not def-pkg))
  16245. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16246. tpl (replace-match rpl t t tpl))
  16247. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16248. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16249. (setq rpl (if (or (match-end 1) (not pkg))
  16250. "" (org-latex-packages-to-string pkg snippets-p t))
  16251. tpl (replace-match rpl t t tpl))
  16252. (if pkg (setq end
  16253. (concat end "\n"
  16254. (org-latex-packages-to-string pkg snippets-p)))))
  16255. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16256. (setq rpl (if (or (match-end 1) (not extra))
  16257. "" (concat extra "\n"))
  16258. tpl (replace-match rpl t t tpl))
  16259. (if (and extra (string-match "\\S-" extra))
  16260. (setq end (concat end "\n" extra))))
  16261. (if (string-match "\\S-" end)
  16262. (concat tpl "\n" end)
  16263. tpl)))
  16264. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16265. "Turn an alist of packages into a string with the \\usepackage macros."
  16266. (setq pkg (mapconcat (lambda(p)
  16267. (cond
  16268. ((stringp p) p)
  16269. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16270. (format "%% Package %s omitted" (cadr p)))
  16271. ((equal "" (car p))
  16272. (format "\\usepackage{%s}" (cadr p)))
  16273. (t
  16274. (format "\\usepackage[%s]{%s}"
  16275. (car p) (cadr p)))))
  16276. pkg
  16277. "\n"))
  16278. (if newline (concat pkg "\n") pkg))
  16279. (defun org-dvipng-color (attr)
  16280. "Return a RGB color specification for dvipng."
  16281. (apply 'format "rgb %s %s %s"
  16282. (mapcar 'org-normalize-color
  16283. (if (featurep 'xemacs)
  16284. (color-rgb-components
  16285. (face-property 'default
  16286. (cond ((eq attr :foreground) 'foreground)
  16287. ((eq attr :background) 'background))))
  16288. (color-values (face-attribute 'default attr nil))))))
  16289. (defun org-latex-color (attr)
  16290. "Return a RGB color for the LaTeX color package."
  16291. (apply 'format "%s,%s,%s"
  16292. (mapcar 'org-normalize-color
  16293. (if (featurep 'xemacs)
  16294. (color-rgb-components
  16295. (face-property 'default
  16296. (cond ((eq attr :foreground) 'foreground)
  16297. ((eq attr :background) 'background))))
  16298. (color-values (face-attribute 'default attr nil))))))
  16299. (defun org-latex-color-format (color-name)
  16300. "Convert COLOR-NAME to a RGB color value."
  16301. (apply 'format "%s,%s,%s"
  16302. (mapcar 'org-normalize-color
  16303. (color-values color-name))))
  16304. (defun org-normalize-color (value)
  16305. "Return string to be used as color value for an RGB component."
  16306. (format "%g" (/ value 65535.0)))
  16307. ;; Image display
  16308. (defvar org-inline-image-overlays nil)
  16309. (make-variable-buffer-local 'org-inline-image-overlays)
  16310. (defun org-toggle-inline-images (&optional include-linked)
  16311. "Toggle the display of inline images.
  16312. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16313. (interactive "P")
  16314. (if org-inline-image-overlays
  16315. (progn
  16316. (org-remove-inline-images)
  16317. (message "Inline image display turned off"))
  16318. (org-display-inline-images include-linked)
  16319. (if org-inline-image-overlays
  16320. (message "%d images displayed inline"
  16321. (length org-inline-image-overlays))
  16322. (message "No images to display inline"))))
  16323. (defun org-redisplay-inline-images ()
  16324. "Refresh the display of inline images."
  16325. (interactive)
  16326. (if (not org-inline-image-overlays)
  16327. (org-toggle-inline-images)
  16328. (org-toggle-inline-images)
  16329. (org-toggle-inline-images)))
  16330. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16331. "Display inline images.
  16332. Normally only links without a description part are inlined, because this
  16333. is how it will work for export. When INCLUDE-LINKED is set, also links
  16334. with a description part will be inlined. This can be nice for a quick
  16335. look at those images, but it does not reflect what exported files will look
  16336. like.
  16337. When REFRESH is set, refresh existing images between BEG and END.
  16338. This will create new image displays only if necessary.
  16339. BEG and END default to the buffer boundaries."
  16340. (interactive "P")
  16341. (unless refresh
  16342. (org-remove-inline-images)
  16343. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16344. (save-excursion
  16345. (save-restriction
  16346. (widen)
  16347. (setq beg (or beg (point-min)) end (or end (point-max)))
  16348. (goto-char beg)
  16349. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16350. (substring (org-image-file-name-regexp) 0 -2)
  16351. "\\)\\]" (if include-linked "" "\\]")))
  16352. old file ov img)
  16353. (while (re-search-forward re end t)
  16354. (setq old (get-char-property-and-overlay (match-beginning 1)
  16355. 'org-image-overlay))
  16356. (setq file (expand-file-name
  16357. (concat (or (match-string 3) "") (match-string 4))))
  16358. (when (file-exists-p file)
  16359. (if (and (car-safe old) refresh)
  16360. (image-refresh (overlay-get (cdr old) 'display))
  16361. (setq img (save-match-data (create-image file)))
  16362. (when img
  16363. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16364. (overlay-put ov 'display img)
  16365. (overlay-put ov 'face 'default)
  16366. (overlay-put ov 'org-image-overlay t)
  16367. (overlay-put ov 'modification-hooks
  16368. (list 'org-display-inline-remove-overlay))
  16369. (push ov org-inline-image-overlays)))))))))
  16370. (define-obsolete-function-alias
  16371. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16372. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16373. "Remove inline-display overlay if a corresponding region is modified."
  16374. (let ((inhibit-modification-hooks t))
  16375. (when (and ov after)
  16376. (delete ov org-inline-image-overlays)
  16377. (delete-overlay ov))))
  16378. (defun org-remove-inline-images ()
  16379. "Remove inline display of images."
  16380. (interactive)
  16381. (mapc 'delete-overlay org-inline-image-overlays)
  16382. (setq org-inline-image-overlays nil))
  16383. ;;;; Key bindings
  16384. ;; Outline functions from `outline-mode-prefix-map'
  16385. ;; that can be remapped in Org:
  16386. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16387. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16388. (define-key org-mode-map [remap outline-forward-same-level]
  16389. 'org-forward-heading-same-level)
  16390. (define-key org-mode-map [remap outline-backward-same-level]
  16391. 'org-backward-heading-same-level)
  16392. (define-key org-mode-map [remap show-branches]
  16393. 'org-kill-note-or-show-branches)
  16394. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16395. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16396. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16397. ;; Outline functions from `outline-mode-prefix-map' that can not
  16398. ;; be remapped in Org:
  16399. ;;
  16400. ;; - the column "key binding" shows whether the Outline function is still
  16401. ;; available in Org mode on the same key that it has been bound to in
  16402. ;; Outline mode:
  16403. ;; - "overridden": key used for a different functionality in Org mode
  16404. ;; - else: key still bound to the same Outline function in Org mode
  16405. ;;
  16406. ;; | Outline function | key binding | Org replacement |
  16407. ;; |------------------------------------+-------------+-----------------------|
  16408. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16409. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16410. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16411. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16412. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16413. ;; | `show-entry' | overridden | no replacement |
  16414. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16415. ;; | `show-branches' | `C-c C-k' | still same function |
  16416. ;; | `show-subtree' | overridden | visibility cycling |
  16417. ;; | `show-all' | overridden | no replacement |
  16418. ;; | `hide-subtree' | overridden | visibility cycling |
  16419. ;; | `hide-body' | overridden | no replacement |
  16420. ;; | `hide-entry' | overridden | visibility cycling |
  16421. ;; | `hide-leaves' | overridden | no replacement |
  16422. ;; | `hide-sublevels' | overridden | no replacement |
  16423. ;; | `hide-other' | overridden | no replacement |
  16424. ;; Make `C-c C-x' a prefix key
  16425. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16426. ;; TAB key with modifiers
  16427. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16428. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16429. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16430. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16431. ;; The following line is necessary under Suse GNU/Linux
  16432. (unless (featurep 'xemacs)
  16433. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16434. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16435. (define-key org-mode-map [backtab] 'org-shifttab)
  16436. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16437. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16438. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16439. ;; Cursor keys with modifiers
  16440. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16441. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16442. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16443. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16444. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16445. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16446. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16447. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16448. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16449. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16450. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16451. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16452. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16453. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16454. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16455. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16456. ;; Babel keys
  16457. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16458. (mapc (lambda (pair)
  16459. (define-key org-babel-map (car pair) (cdr pair)))
  16460. org-babel-key-bindings)
  16461. ;;; Extra keys for tty access.
  16462. ;; We only set them when really needed because otherwise the
  16463. ;; menus don't show the simple keys
  16464. (when (or org-use-extra-keys
  16465. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16466. (not window-system))
  16467. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16468. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16469. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16470. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16471. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16472. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16473. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16474. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16475. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16476. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16477. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16478. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16479. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16480. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16481. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16482. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16483. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16484. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16485. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16486. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16487. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16488. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16489. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16490. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16491. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16492. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16493. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16494. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16495. ;; All the other keys
  16496. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16497. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16498. (if (boundp 'narrow-map)
  16499. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16500. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16501. (if (boundp 'narrow-map)
  16502. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16503. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16504. (if (boundp 'narrow-map)
  16505. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16506. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16507. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16508. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16509. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16510. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16511. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16512. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16513. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16514. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16515. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16516. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16517. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16518. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16519. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16520. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16521. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16522. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16523. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16524. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16525. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16526. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16527. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16528. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16529. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16530. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16531. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16532. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16533. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16534. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16535. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16536. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16537. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16538. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16539. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16540. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16541. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16542. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16543. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16544. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16545. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16546. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16547. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16548. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16549. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16550. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16551. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16552. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16553. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16554. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16555. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16556. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16557. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16558. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16559. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16560. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16561. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16562. (org-defkey org-mode-map "\C-m" 'org-return)
  16563. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16564. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16565. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16566. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16567. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16568. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16569. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16570. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16571. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16572. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16573. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16574. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16575. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16576. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16577. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16578. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16579. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16580. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16581. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16582. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16583. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  16584. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16585. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16586. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16587. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16588. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16589. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16590. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16591. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16592. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16593. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16594. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16595. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  16596. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16597. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16598. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16599. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16600. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16601. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  16602. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16603. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16604. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16605. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16606. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  16607. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16608. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16609. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16610. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16611. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16612. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16613. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16614. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16615. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16616. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16617. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16618. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16619. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16620. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16621. (when (featurep 'xemacs)
  16622. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16623. (defconst org-speed-commands-default
  16624. '(
  16625. ("Outline Navigation")
  16626. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16627. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16628. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  16629. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  16630. ("u" . (org-speed-move-safe 'outline-up-heading))
  16631. ("j" . org-goto)
  16632. ("g" . (org-refile t))
  16633. ("Outline Visibility")
  16634. ("c" . org-cycle)
  16635. ("C" . org-shifttab)
  16636. (" " . org-display-outline-path)
  16637. ("=" . org-columns)
  16638. ("Outline Structure Editing")
  16639. ("U" . org-shiftmetaup)
  16640. ("D" . org-shiftmetadown)
  16641. ("r" . org-metaright)
  16642. ("l" . org-metaleft)
  16643. ("R" . org-shiftmetaright)
  16644. ("L" . org-shiftmetaleft)
  16645. ("i" . (progn (forward-char 1) (call-interactively
  16646. 'org-insert-heading-respect-content)))
  16647. ("^" . org-sort)
  16648. ("w" . org-refile)
  16649. ("a" . org-archive-subtree-default-with-confirmation)
  16650. ("." . org-mark-subtree)
  16651. ("#" . org-toggle-comment)
  16652. ("Clock Commands")
  16653. ("I" . org-clock-in)
  16654. ("O" . org-clock-out)
  16655. ("Meta Data Editing")
  16656. ("t" . org-todo)
  16657. ("," . (org-priority))
  16658. ("0" . (org-priority ?\ ))
  16659. ("1" . (org-priority ?A))
  16660. ("2" . (org-priority ?B))
  16661. ("3" . (org-priority ?C))
  16662. (":" . org-set-tags-command)
  16663. ("e" . org-set-effort)
  16664. ("E" . org-inc-effort)
  16665. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  16666. (org-entry-put (point) "APPT_WARNTIME" m)))
  16667. ("Agenda Views etc")
  16668. ("v" . org-agenda)
  16669. ("/" . org-sparse-tree)
  16670. ("Misc")
  16671. ("o" . org-open-at-point)
  16672. ("?" . org-speed-command-help)
  16673. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16674. (">" . (org-agenda-remove-restriction-lock))
  16675. )
  16676. "The default speed commands.")
  16677. (defun org-print-speed-command (e)
  16678. (if (> (length (car e)) 1)
  16679. (progn
  16680. (princ "\n")
  16681. (princ (car e))
  16682. (princ "\n")
  16683. (princ (make-string (length (car e)) ?-))
  16684. (princ "\n"))
  16685. (princ (car e))
  16686. (princ " ")
  16687. (if (symbolp (cdr e))
  16688. (princ (symbol-name (cdr e)))
  16689. (prin1 (cdr e)))
  16690. (princ "\n")))
  16691. (defun org-speed-command-help ()
  16692. "Show the available speed commands."
  16693. (interactive)
  16694. (if (not org-use-speed-commands)
  16695. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16696. (with-output-to-temp-buffer "*Help*"
  16697. (princ "User-defined Speed commands\n===========================\n")
  16698. (mapc 'org-print-speed-command org-speed-commands-user)
  16699. (princ "\n")
  16700. (princ "Built-in Speed commands\n=======================\n")
  16701. (mapc 'org-print-speed-command org-speed-commands-default))
  16702. (with-current-buffer "*Help*"
  16703. (setq truncate-lines t))))
  16704. (defun org-speed-move-safe (cmd)
  16705. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16706. If not, return to the original position and throw an error."
  16707. (interactive)
  16708. (let ((pos (point)))
  16709. (call-interactively cmd)
  16710. (unless (and (bolp) (org-at-heading-p))
  16711. (goto-char pos)
  16712. (error "Boundary reached while executing %s" cmd))))
  16713. (defvar org-self-insert-command-undo-counter 0)
  16714. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16715. (defvar org-speed-command nil)
  16716. (define-obsolete-function-alias
  16717. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  16718. (defun org-speed-command-activate (keys)
  16719. "Hook for activating single-letter speed commands.
  16720. `org-speed-commands-default' specifies a minimal command set.
  16721. Use `org-speed-commands-user' for further customization."
  16722. (when (or (and (bolp) (looking-at org-outline-regexp))
  16723. (and (functionp org-use-speed-commands)
  16724. (funcall org-use-speed-commands)))
  16725. (cdr (assoc keys (append org-speed-commands-user
  16726. org-speed-commands-default)))))
  16727. (define-obsolete-function-alias
  16728. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  16729. (defun org-babel-speed-command-activate (keys)
  16730. "Hook for activating single-letter code block commands."
  16731. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16732. (cdr (assoc keys org-babel-key-bindings))))
  16733. (defcustom org-speed-command-hook
  16734. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16735. "Hook for activating speed commands at strategic locations.
  16736. Hook functions are called in sequence until a valid handler is
  16737. found.
  16738. Each hook takes a single argument, a user-pressed command key
  16739. which is also a `self-insert-command' from the global map.
  16740. Within the hook, examine the cursor position and the command key
  16741. and return nil or a valid handler as appropriate. Handler could
  16742. be one of an interactive command, a function, or a form.
  16743. Set `org-use-speed-commands' to non-nil value to enable this
  16744. hook. The default setting is `org-speed-command-activate'."
  16745. :group 'org-structure
  16746. :version "24.1"
  16747. :type 'hook)
  16748. (defun org-self-insert-command (N)
  16749. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16750. If the cursor is in a table looking at whitespace, the whitespace is
  16751. overwritten, and the table is not marked as requiring realignment."
  16752. (interactive "p")
  16753. (org-check-before-invisible-edit 'insert)
  16754. (cond
  16755. ((and org-use-speed-commands
  16756. (setq org-speed-command
  16757. (run-hook-with-args-until-success
  16758. 'org-speed-command-hook (this-command-keys))))
  16759. (cond
  16760. ((commandp org-speed-command)
  16761. (setq this-command org-speed-command)
  16762. (call-interactively org-speed-command))
  16763. ((functionp org-speed-command)
  16764. (funcall org-speed-command))
  16765. ((and org-speed-command (listp org-speed-command))
  16766. (eval org-speed-command))
  16767. (t (let (org-use-speed-commands)
  16768. (call-interactively 'org-self-insert-command)))))
  16769. ((and
  16770. (org-table-p)
  16771. (progn
  16772. ;; check if we blank the field, and if that triggers align
  16773. (and (featurep 'org-table) org-table-auto-blank-field
  16774. (member last-command
  16775. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16776. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16777. ;; got extra space, this field does not determine column width
  16778. (let (org-table-may-need-update) (org-table-blank-field))
  16779. ;; no extra space, this field may determine column width
  16780. (org-table-blank-field)))
  16781. t)
  16782. (eq N 1)
  16783. (looking-at "[^|\n]* |"))
  16784. (let (org-table-may-need-update)
  16785. (goto-char (1- (match-end 0)))
  16786. (backward-delete-char 1)
  16787. (goto-char (match-beginning 0))
  16788. (self-insert-command N)))
  16789. (t
  16790. (setq org-table-may-need-update t)
  16791. (self-insert-command N)
  16792. (org-fix-tags-on-the-fly)
  16793. (if org-self-insert-cluster-for-undo
  16794. (if (not (eq last-command 'org-self-insert-command))
  16795. (setq org-self-insert-command-undo-counter 1)
  16796. (if (>= org-self-insert-command-undo-counter 20)
  16797. (setq org-self-insert-command-undo-counter 1)
  16798. (and (> org-self-insert-command-undo-counter 0)
  16799. buffer-undo-list (listp buffer-undo-list)
  16800. (not (cadr buffer-undo-list)) ; remove nil entry
  16801. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16802. (setq org-self-insert-command-undo-counter
  16803. (1+ org-self-insert-command-undo-counter))))))))
  16804. (defun org-check-before-invisible-edit (kind)
  16805. "Check is editing if kind KIND would be dangerous with invisible text around.
  16806. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16807. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16808. (if (and org-catch-invisible-edits
  16809. (or (not (boundp 'visible-mode)) (not visible-mode))
  16810. (or (get-char-property (point) 'invisible)
  16811. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16812. ;; OK, we need to take a closer look
  16813. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16814. (invisible-before-point (if (bobp) nil (get-char-property
  16815. (1- (point)) 'invisible)))
  16816. (border-and-ok-direction
  16817. (or
  16818. ;; Check if we are acting predictably before invisible text
  16819. (and invisible-at-point (not invisible-before-point)
  16820. (memq kind '(insert delete-backward)))
  16821. ;; Check if we are acting predictably after invisible text
  16822. ;; This works not well, and I have turned it off. It seems
  16823. ;; better to always show and stop after invisible text.
  16824. ;; (and (not invisible-at-point) invisible-before-point
  16825. ;; (memq kind '(insert delete)))
  16826. )))
  16827. (when (or (memq invisible-at-point '(outline org-hide-block t))
  16828. (memq invisible-before-point '(outline org-hide-block t)))
  16829. (if (eq org-catch-invisible-edits 'error)
  16830. (error "Editing in invisible areas is prohibited - make visible first"))
  16831. (if (and org-custom-properties-overlays
  16832. (y-or-n-p "Display invisible properties in this buffer? "))
  16833. (org-toggle-custom-properties-visibility)
  16834. ;; Make the area visible
  16835. (save-excursion
  16836. (if invisible-before-point
  16837. (goto-char (previous-single-char-property-change
  16838. (point) 'invisible)))
  16839. (org-cycle))
  16840. (cond
  16841. ((eq org-catch-invisible-edits 'show)
  16842. ;; That's it, we do the edit after showing
  16843. (message
  16844. "Unfolding invisible region around point before editing")
  16845. (sit-for 1))
  16846. ((and (eq org-catch-invisible-edits 'smart)
  16847. border-and-ok-direction)
  16848. (message "Unfolding invisible region around point before editing"))
  16849. (t
  16850. ;; Don't do the edit, make the user repeat it in full visibility
  16851. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  16852. (defun org-fix-tags-on-the-fly ()
  16853. (when (and (equal (char-after (point-at-bol)) ?*)
  16854. (org-at-heading-p))
  16855. (org-align-tags-here org-tags-column)))
  16856. (defun org-delete-backward-char (N)
  16857. "Like `delete-backward-char', insert whitespace at field end in tables.
  16858. When deleting backwards, in tables this function will insert whitespace in
  16859. front of the next \"|\" separator, to keep the table aligned. The table will
  16860. still be marked for re-alignment if the field did fill the entire column,
  16861. because, in this case the deletion might narrow the column."
  16862. (interactive "p")
  16863. (save-match-data
  16864. (org-check-before-invisible-edit 'delete-backward)
  16865. (if (and (org-table-p)
  16866. (eq N 1)
  16867. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16868. (looking-at ".*?|"))
  16869. (let ((pos (point))
  16870. (noalign (looking-at "[^|\n\r]* |"))
  16871. (c org-table-may-need-update))
  16872. (backward-delete-char N)
  16873. (if (not overwrite-mode)
  16874. (progn
  16875. (skip-chars-forward "^|")
  16876. (insert " ")
  16877. (goto-char (1- pos))))
  16878. ;; noalign: if there were two spaces at the end, this field
  16879. ;; does not determine the width of the column.
  16880. (if noalign (setq org-table-may-need-update c)))
  16881. (backward-delete-char N)
  16882. (org-fix-tags-on-the-fly))))
  16883. (defun org-delete-char (N)
  16884. "Like `delete-char', but insert whitespace at field end in tables.
  16885. When deleting characters, in tables this function will insert whitespace in
  16886. front of the next \"|\" separator, to keep the table aligned. The table will
  16887. still be marked for re-alignment if the field did fill the entire column,
  16888. because, in this case the deletion might narrow the column."
  16889. (interactive "p")
  16890. (save-match-data
  16891. (org-check-before-invisible-edit 'delete)
  16892. (if (and (org-table-p)
  16893. (not (bolp))
  16894. (not (= (char-after) ?|))
  16895. (eq N 1))
  16896. (if (looking-at ".*?|")
  16897. (let ((pos (point))
  16898. (noalign (looking-at "[^|\n\r]* |"))
  16899. (c org-table-may-need-update))
  16900. (replace-match (concat
  16901. (substring (match-string 0) 1 -1)
  16902. " |"))
  16903. (goto-char pos)
  16904. ;; noalign: if there were two spaces at the end, this field
  16905. ;; does not determine the width of the column.
  16906. (if noalign (setq org-table-may-need-update c)))
  16907. (delete-char N))
  16908. (delete-char N)
  16909. (org-fix-tags-on-the-fly))))
  16910. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16911. (put 'org-self-insert-command 'delete-selection t)
  16912. (put 'orgtbl-self-insert-command 'delete-selection t)
  16913. (put 'org-delete-char 'delete-selection 'supersede)
  16914. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16915. (put 'org-yank 'delete-selection 'yank)
  16916. ;; Make `flyspell-mode' delay after some commands
  16917. (put 'org-self-insert-command 'flyspell-delayed t)
  16918. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16919. (put 'org-delete-char 'flyspell-delayed t)
  16920. (put 'org-delete-backward-char 'flyspell-delayed t)
  16921. ;; Make pabbrev-mode expand after org-mode commands
  16922. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16923. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16924. ;; How to do this: Measure non-white length of current string
  16925. ;; If equal to column width, we should realign.
  16926. (defun org-remap (map &rest commands)
  16927. "In MAP, remap the functions given in COMMANDS.
  16928. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16929. (let (new old)
  16930. (while commands
  16931. (setq old (pop commands) new (pop commands))
  16932. (if (fboundp 'command-remapping)
  16933. (org-defkey map (vector 'remap old) new)
  16934. (substitute-key-definition old new map global-map)))))
  16935. (when (eq org-enable-table-editor 'optimized)
  16936. ;; If the user wants maximum table support, we need to hijack
  16937. ;; some standard editing functions
  16938. (org-remap org-mode-map
  16939. 'self-insert-command 'org-self-insert-command
  16940. 'delete-char 'org-delete-char
  16941. 'delete-backward-char 'org-delete-backward-char)
  16942. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16943. (defvar org-ctrl-c-ctrl-c-hook nil
  16944. "Hook for functions attaching themselves to `C-c C-c'.
  16945. This can be used to add additional functionality to the C-c C-c
  16946. key which executes context-dependent commands. This hook is run
  16947. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16948. run after the last test.
  16949. Each function will be called with no arguments. The function
  16950. must check if the context is appropriate for it to act. If yes,
  16951. it should do its thing and then return a non-nil value. If the
  16952. context is wrong, just do nothing and return nil.")
  16953. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16954. "Hook for functions attaching themselves to `C-c C-c'.
  16955. This can be used to add additional functionality to the C-c C-c
  16956. key which executes context-dependent commands. This hook is run
  16957. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16958. before the first test.
  16959. Each function will be called with no arguments. The function
  16960. must check if the context is appropriate for it to act. If yes,
  16961. it should do its thing and then return a non-nil value. If the
  16962. context is wrong, just do nothing and return nil.")
  16963. (defvar org-tab-first-hook nil
  16964. "Hook for functions to attach themselves to TAB.
  16965. See `org-ctrl-c-ctrl-c-hook' for more information.
  16966. This hook runs as the first action when TAB is pressed, even before
  16967. `org-cycle' messes around with the `outline-regexp' to cater for
  16968. inline tasks and plain list item folding.
  16969. If any function in this hook returns t, any other actions that
  16970. would have been caused by TAB (such as table field motion or visibility
  16971. cycling) will not occur.")
  16972. (defvar org-tab-after-check-for-table-hook nil
  16973. "Hook for functions to attach themselves to TAB.
  16974. See `org-ctrl-c-ctrl-c-hook' for more information.
  16975. This hook runs after it has been established that the cursor is not in a
  16976. table, but before checking if the cursor is in a headline or if global cycling
  16977. should be done.
  16978. If any function in this hook returns t, not other actions like visibility
  16979. cycling will be done.")
  16980. (defvar org-tab-after-check-for-cycling-hook nil
  16981. "Hook for functions to attach themselves to TAB.
  16982. See `org-ctrl-c-ctrl-c-hook' for more information.
  16983. This hook runs after it has been established that not table field motion and
  16984. not visibility should be done because of current context. This is probably
  16985. the place where a package like yasnippets can hook in.")
  16986. (defvar org-tab-before-tab-emulation-hook nil
  16987. "Hook for functions to attach themselves to TAB.
  16988. See `org-ctrl-c-ctrl-c-hook' for more information.
  16989. This hook runs after every other options for TAB have been exhausted, but
  16990. before indentation and \t insertion takes place.")
  16991. (defvar org-metaleft-hook nil
  16992. "Hook for functions attaching themselves to `M-left'.
  16993. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16994. (defvar org-metaright-hook nil
  16995. "Hook for functions attaching themselves to `M-right'.
  16996. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16997. (defvar org-metaup-hook nil
  16998. "Hook for functions attaching themselves to `M-up'.
  16999. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17000. (defvar org-metadown-hook nil
  17001. "Hook for functions attaching themselves to `M-down'.
  17002. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17003. (defvar org-shiftmetaleft-hook nil
  17004. "Hook for functions attaching themselves to `M-S-left'.
  17005. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17006. (defvar org-shiftmetaright-hook nil
  17007. "Hook for functions attaching themselves to `M-S-right'.
  17008. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17009. (defvar org-shiftmetaup-hook nil
  17010. "Hook for functions attaching themselves to `M-S-up'.
  17011. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17012. (defvar org-shiftmetadown-hook nil
  17013. "Hook for functions attaching themselves to `M-S-down'.
  17014. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17015. (defvar org-metareturn-hook nil
  17016. "Hook for functions attaching themselves to `M-RET'.
  17017. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17018. (defvar org-shiftup-hook nil
  17019. "Hook for functions attaching themselves to `S-up'.
  17020. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17021. (defvar org-shiftup-final-hook nil
  17022. "Hook for functions attaching themselves to `S-up'.
  17023. This one runs after all other options except shift-select have been excluded.
  17024. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17025. (defvar org-shiftdown-hook nil
  17026. "Hook for functions attaching themselves to `S-down'.
  17027. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17028. (defvar org-shiftdown-final-hook nil
  17029. "Hook for functions attaching themselves to `S-down'.
  17030. This one runs after all other options except shift-select have been excluded.
  17031. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17032. (defvar org-shiftleft-hook nil
  17033. "Hook for functions attaching themselves to `S-left'.
  17034. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17035. (defvar org-shiftleft-final-hook nil
  17036. "Hook for functions attaching themselves to `S-left'.
  17037. This one runs after all other options except shift-select have been excluded.
  17038. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17039. (defvar org-shiftright-hook nil
  17040. "Hook for functions attaching themselves to `S-right'.
  17041. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17042. (defvar org-shiftright-final-hook nil
  17043. "Hook for functions attaching themselves to `S-right'.
  17044. This one runs after all other options except shift-select have been excluded.
  17045. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17046. (defun org-modifier-cursor-error ()
  17047. "Throw an error, a modified cursor command was applied in wrong context."
  17048. (error "This command is active in special context like tables, headlines or items"))
  17049. (defun org-shiftselect-error ()
  17050. "Throw an error because Shift-Cursor command was applied in wrong context."
  17051. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17052. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17053. (error "This command works only in special context like headlines or timestamps")))
  17054. (defun org-call-for-shift-select (cmd)
  17055. (let ((this-command-keys-shift-translated t))
  17056. (call-interactively cmd)))
  17057. (defun org-shifttab (&optional arg)
  17058. "Global visibility cycling or move to previous table field.
  17059. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17060. on context.
  17061. See the individual commands for more information."
  17062. (interactive "P")
  17063. (cond
  17064. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17065. ((integerp arg)
  17066. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17067. (message "Content view to level: %d" arg)
  17068. (org-content (prefix-numeric-value arg2))
  17069. (setq org-cycle-global-status 'overview)))
  17070. (t (call-interactively 'org-global-cycle))))
  17071. (defun org-shiftmetaleft ()
  17072. "Promote subtree or delete table column.
  17073. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17074. `org-table-delete-column', depending on context. See the
  17075. individual commands for more information."
  17076. (interactive)
  17077. (cond
  17078. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17079. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17080. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17081. ((if (not (org-region-active-p)) (org-at-item-p)
  17082. (save-excursion (goto-char (region-beginning))
  17083. (org-at-item-p)))
  17084. (call-interactively 'org-outdent-item-tree))
  17085. (t (org-modifier-cursor-error))))
  17086. (defun org-shiftmetaright ()
  17087. "Demote subtree or insert table column.
  17088. Calls `org-demote-subtree', `org-indent-item-tree', or
  17089. `org-table-insert-column', depending on context. See the
  17090. individual commands for more information."
  17091. (interactive)
  17092. (cond
  17093. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17094. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17095. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17096. ((if (not (org-region-active-p)) (org-at-item-p)
  17097. (save-excursion (goto-char (region-beginning))
  17098. (org-at-item-p)))
  17099. (call-interactively 'org-indent-item-tree))
  17100. (t (org-modifier-cursor-error))))
  17101. (defun org-shiftmetaup (&optional arg)
  17102. "Move subtree up or kill table row.
  17103. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17104. `org-move-item-up' or `org-timestamp-up', depending on context.
  17105. See the individual commands for more information."
  17106. (interactive "P")
  17107. (cond
  17108. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17109. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17110. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17111. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17112. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17113. (call-interactively 'org-timestamp-up)))
  17114. (t (org-modifier-cursor-error))))
  17115. (defun org-shiftmetadown (&optional arg)
  17116. "Move subtree down or insert table row.
  17117. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17118. `org-move-item-down' or `org-timestamp-up', depending on context.
  17119. See the individual commands for more information."
  17120. (interactive "P")
  17121. (cond
  17122. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17123. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17124. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17125. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17126. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17127. (call-interactively 'org-timestamp-down)))
  17128. (t (org-modifier-cursor-error))))
  17129. (defsubst org-hidden-tree-error ()
  17130. (error
  17131. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17132. (defun org-metaleft (&optional arg)
  17133. "Promote heading or move table column to left.
  17134. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17135. With no specific context, calls the Emacs default `backward-word'.
  17136. See the individual commands for more information."
  17137. (interactive "P")
  17138. (cond
  17139. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17140. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17141. ((org-with-limited-levels
  17142. (or (org-at-heading-p)
  17143. (and (org-region-active-p)
  17144. (save-excursion
  17145. (goto-char (region-beginning))
  17146. (org-at-heading-p)))))
  17147. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17148. (call-interactively 'org-do-promote))
  17149. ;; At an inline task.
  17150. ((org-at-heading-p)
  17151. (call-interactively 'org-inlinetask-promote))
  17152. ((or (org-at-item-p)
  17153. (and (org-region-active-p)
  17154. (save-excursion
  17155. (goto-char (region-beginning))
  17156. (org-at-item-p))))
  17157. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17158. (call-interactively 'org-outdent-item))
  17159. (t (call-interactively 'backward-word))))
  17160. (defun org-metaright (&optional arg)
  17161. "Demote a subtree, a list item or move table column to right.
  17162. In front of a drawer or a block keyword, indent it correctly.
  17163. With no specific context, calls the Emacs default `forward-word'.
  17164. See the individual commands for more information."
  17165. (interactive "P")
  17166. (cond
  17167. ((run-hook-with-args-until-success 'org-metaright-hook))
  17168. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17169. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17170. ((org-at-block-p) (call-interactively 'org-indent-block))
  17171. ((org-with-limited-levels
  17172. (or (org-at-heading-p)
  17173. (and (org-region-active-p)
  17174. (save-excursion
  17175. (goto-char (region-beginning))
  17176. (org-at-heading-p)))))
  17177. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17178. (call-interactively 'org-do-demote))
  17179. ;; At an inline task.
  17180. ((org-at-heading-p)
  17181. (call-interactively 'org-inlinetask-demote))
  17182. ((or (org-at-item-p)
  17183. (and (org-region-active-p)
  17184. (save-excursion
  17185. (goto-char (region-beginning))
  17186. (org-at-item-p))))
  17187. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17188. (call-interactively 'org-indent-item))
  17189. (t (call-interactively 'forward-word))))
  17190. (defun org-check-for-hidden (what)
  17191. "Check if there are hidden headlines/items in the current visual line.
  17192. WHAT can be either `headlines' or `items'. If the current line is
  17193. an outline or item heading and it has a folded subtree below it,
  17194. this function returns t, nil otherwise."
  17195. (let ((re (cond
  17196. ((eq what 'headlines) org-outline-regexp-bol)
  17197. ((eq what 'items) (org-item-beginning-re))
  17198. (t (error "This should not happen"))))
  17199. beg end)
  17200. (save-excursion
  17201. (catch 'exit
  17202. (unless (org-region-active-p)
  17203. (setq beg (point-at-bol))
  17204. (beginning-of-line 2)
  17205. (while (and (not (eobp)) ;; this is like `next-line'
  17206. (get-char-property (1- (point)) 'invisible))
  17207. (beginning-of-line 2))
  17208. (setq end (point))
  17209. (goto-char beg)
  17210. (goto-char (point-at-eol))
  17211. (setq end (max end (point)))
  17212. (while (re-search-forward re end t)
  17213. (if (get-char-property (match-beginning 0) 'invisible)
  17214. (throw 'exit t))))
  17215. nil))))
  17216. (autoload 'org-element-at-point "org-element")
  17217. (autoload 'org-element-type "org-element")
  17218. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  17219. (declare-function org-element-type "org-element" (element))
  17220. (declare-function org-element-contents "org-element" (element))
  17221. (declare-function org-element-property "org-element" (property element))
  17222. (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
  17223. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  17224. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  17225. (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
  17226. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  17227. (defun org-metaup (&optional arg)
  17228. "Move subtree up or move table row up.
  17229. Calls `org-move-subtree-up' or `org-table-move-row' or
  17230. `org-move-item-up', depending on context. See the individual commands
  17231. for more information."
  17232. (interactive "P")
  17233. (cond
  17234. ((run-hook-with-args-until-success 'org-metaup-hook))
  17235. ((org-region-active-p)
  17236. (let* ((a (min (region-beginning) (region-end)))
  17237. (b (1- (max (region-beginning) (region-end))))
  17238. (c (save-excursion (goto-char a)
  17239. (move-beginning-of-line 0)))
  17240. (d (save-excursion (goto-char a)
  17241. (move-end-of-line 0) (point))))
  17242. (transpose-regions a b c d)
  17243. (goto-char c)))
  17244. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17245. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17246. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17247. (t (org-drag-element-backward))))
  17248. (defun org-metadown (&optional arg)
  17249. "Move subtree down or move table row down.
  17250. Calls `org-move-subtree-down' or `org-table-move-row' or
  17251. `org-move-item-down', depending on context. See the individual
  17252. commands for more information."
  17253. (interactive "P")
  17254. (cond
  17255. ((run-hook-with-args-until-success 'org-metadown-hook))
  17256. ((org-region-active-p)
  17257. (let* ((a (min (region-beginning) (region-end)))
  17258. (b (max (region-beginning) (region-end)))
  17259. (c (save-excursion (goto-char b)
  17260. (move-beginning-of-line 1)))
  17261. (d (save-excursion (goto-char b)
  17262. (move-end-of-line 1) (1+ (point)))))
  17263. (transpose-regions a b c d)
  17264. (goto-char d)))
  17265. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17266. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17267. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17268. (t (org-drag-element-forward))))
  17269. (defun org-shiftup (&optional arg)
  17270. "Increase item in timestamp or increase priority of current headline.
  17271. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17272. depending on context. See the individual commands for more information."
  17273. (interactive "P")
  17274. (cond
  17275. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17276. ((and org-support-shift-select (org-region-active-p))
  17277. (org-call-for-shift-select 'previous-line))
  17278. ((org-at-timestamp-p t)
  17279. (call-interactively (if org-edit-timestamp-down-means-later
  17280. 'org-timestamp-down 'org-timestamp-up)))
  17281. ((and (not (eq org-support-shift-select 'always))
  17282. org-enable-priority-commands
  17283. (org-at-heading-p))
  17284. (call-interactively 'org-priority-up))
  17285. ((and (not org-support-shift-select) (org-at-item-p))
  17286. (call-interactively 'org-previous-item))
  17287. ((org-clocktable-try-shift 'up arg))
  17288. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17289. (org-support-shift-select
  17290. (org-call-for-shift-select 'previous-line))
  17291. (t (org-shiftselect-error))))
  17292. (defun org-shiftdown (&optional arg)
  17293. "Decrease item in timestamp or decrease priority of current headline.
  17294. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17295. depending on context. See the individual commands for more information."
  17296. (interactive "P")
  17297. (cond
  17298. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17299. ((and org-support-shift-select (org-region-active-p))
  17300. (org-call-for-shift-select 'next-line))
  17301. ((org-at-timestamp-p t)
  17302. (call-interactively (if org-edit-timestamp-down-means-later
  17303. 'org-timestamp-up 'org-timestamp-down)))
  17304. ((and (not (eq org-support-shift-select 'always))
  17305. org-enable-priority-commands
  17306. (org-at-heading-p))
  17307. (call-interactively 'org-priority-down))
  17308. ((and (not org-support-shift-select) (org-at-item-p))
  17309. (call-interactively 'org-next-item))
  17310. ((org-clocktable-try-shift 'down arg))
  17311. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17312. (org-support-shift-select
  17313. (org-call-for-shift-select 'next-line))
  17314. (t (org-shiftselect-error))))
  17315. (defun org-shiftright (&optional arg)
  17316. "Cycle the thing at point or in the current line, depending on context.
  17317. Depending on context, this does one of the following:
  17318. - switch a timestamp at point one day into the future
  17319. - on a headline, switch to the next TODO keyword.
  17320. - on an item, switch entire list to the next bullet type
  17321. - on a property line, switch to the next allowed value
  17322. - on a clocktable definition line, move time block into the future"
  17323. (interactive "P")
  17324. (cond
  17325. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17326. ((and org-support-shift-select (org-region-active-p))
  17327. (org-call-for-shift-select 'forward-char))
  17328. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17329. ((and (not (eq org-support-shift-select 'always))
  17330. (org-at-heading-p))
  17331. (let ((org-inhibit-logging
  17332. (not org-treat-S-cursor-todo-selection-as-state-change))
  17333. (org-inhibit-blocking
  17334. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17335. (org-call-with-arg 'org-todo 'right)))
  17336. ((or (and org-support-shift-select
  17337. (not (eq org-support-shift-select 'always))
  17338. (org-at-item-bullet-p))
  17339. (and (not org-support-shift-select) (org-at-item-p)))
  17340. (org-call-with-arg 'org-cycle-list-bullet nil))
  17341. ((and (not (eq org-support-shift-select 'always))
  17342. (org-at-property-p))
  17343. (call-interactively 'org-property-next-allowed-value))
  17344. ((org-clocktable-try-shift 'right arg))
  17345. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17346. (org-support-shift-select
  17347. (org-call-for-shift-select 'forward-char))
  17348. (t (org-shiftselect-error))))
  17349. (defun org-shiftleft (&optional arg)
  17350. "Cycle the thing at point or in the current line, depending on context.
  17351. Depending on context, this does one of the following:
  17352. - switch a timestamp at point one day into the past
  17353. - on a headline, switch to the previous TODO keyword.
  17354. - on an item, switch entire list to the previous bullet type
  17355. - on a property line, switch to the previous allowed value
  17356. - on a clocktable definition line, move time block into the past"
  17357. (interactive "P")
  17358. (cond
  17359. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17360. ((and org-support-shift-select (org-region-active-p))
  17361. (org-call-for-shift-select 'backward-char))
  17362. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17363. ((and (not (eq org-support-shift-select 'always))
  17364. (org-at-heading-p))
  17365. (let ((org-inhibit-logging
  17366. (not org-treat-S-cursor-todo-selection-as-state-change))
  17367. (org-inhibit-blocking
  17368. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17369. (org-call-with-arg 'org-todo 'left)))
  17370. ((or (and org-support-shift-select
  17371. (not (eq org-support-shift-select 'always))
  17372. (org-at-item-bullet-p))
  17373. (and (not org-support-shift-select) (org-at-item-p)))
  17374. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17375. ((and (not (eq org-support-shift-select 'always))
  17376. (org-at-property-p))
  17377. (call-interactively 'org-property-previous-allowed-value))
  17378. ((org-clocktable-try-shift 'left arg))
  17379. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17380. (org-support-shift-select
  17381. (org-call-for-shift-select 'backward-char))
  17382. (t (org-shiftselect-error))))
  17383. (defun org-shiftcontrolright ()
  17384. "Switch to next TODO set."
  17385. (interactive)
  17386. (cond
  17387. ((and org-support-shift-select (org-region-active-p))
  17388. (org-call-for-shift-select 'forward-word))
  17389. ((and (not (eq org-support-shift-select 'always))
  17390. (org-at-heading-p))
  17391. (org-call-with-arg 'org-todo 'nextset))
  17392. (org-support-shift-select
  17393. (org-call-for-shift-select 'forward-word))
  17394. (t (org-shiftselect-error))))
  17395. (defun org-shiftcontrolleft ()
  17396. "Switch to previous TODO set."
  17397. (interactive)
  17398. (cond
  17399. ((and org-support-shift-select (org-region-active-p))
  17400. (org-call-for-shift-select 'backward-word))
  17401. ((and (not (eq org-support-shift-select 'always))
  17402. (org-at-heading-p))
  17403. (org-call-with-arg 'org-todo 'previousset))
  17404. (org-support-shift-select
  17405. (org-call-for-shift-select 'backward-word))
  17406. (t (org-shiftselect-error))))
  17407. (defun org-shiftcontrolup ()
  17408. "Change timestamps synchronously up in CLOCK log lines."
  17409. (interactive)
  17410. (cond ((and (not org-support-shift-select)
  17411. (org-at-clock-log-p)
  17412. (org-at-timestamp-p t))
  17413. (org-clock-timestamps-up))
  17414. (t (org-shiftselect-error))))
  17415. (defun org-shiftcontroldown ()
  17416. "Change timestamps synchronously down in CLOCK log lines."
  17417. (interactive)
  17418. (cond ((and (not org-support-shift-select)
  17419. (org-at-clock-log-p)
  17420. (org-at-timestamp-p t))
  17421. (org-clock-timestamps-down))
  17422. (t (org-shiftselect-error))))
  17423. (defun org-ctrl-c-ret ()
  17424. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17425. (interactive)
  17426. (cond
  17427. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17428. (t (call-interactively 'org-insert-heading))))
  17429. (defun org-find-visible ()
  17430. (let ((s (point)))
  17431. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17432. (get-char-property s 'invisible)))
  17433. s))
  17434. (defun org-find-invisible ()
  17435. (let ((s (point)))
  17436. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17437. (not (get-char-property s 'invisible))))
  17438. s))
  17439. (defun org-copy-visible (beg end)
  17440. "Copy the visible parts of the region."
  17441. (interactive "r")
  17442. (let (snippets s)
  17443. (save-excursion
  17444. (save-restriction
  17445. (narrow-to-region beg end)
  17446. (setq s (goto-char (point-min)))
  17447. (while (not (= (point) (point-max)))
  17448. (goto-char (org-find-invisible))
  17449. (push (buffer-substring s (point)) snippets)
  17450. (setq s (goto-char (org-find-visible))))))
  17451. (kill-new (apply 'concat (nreverse snippets)))))
  17452. (defun org-copy-special ()
  17453. "Copy region in table or copy current subtree.
  17454. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17455. See the individual commands for more information."
  17456. (interactive)
  17457. (call-interactively
  17458. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17459. (defun org-cut-special ()
  17460. "Cut region in table or cut current subtree.
  17461. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17462. See the individual commands for more information."
  17463. (interactive)
  17464. (call-interactively
  17465. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17466. (defun org-paste-special (arg)
  17467. "Paste rectangular region into table, or past subtree relative to level.
  17468. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17469. See the individual commands for more information."
  17470. (interactive "P")
  17471. (if (org-at-table-p)
  17472. (org-table-paste-rectangle)
  17473. (org-paste-subtree arg)))
  17474. (defsubst org-in-fixed-width-region-p ()
  17475. "Is point in a fixed-width region?"
  17476. (save-match-data
  17477. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  17478. (defun org-edit-special (&optional arg)
  17479. "Call a special editor for the stuff at point.
  17480. When at a table, call the formula editor with `org-table-edit-formulas'.
  17481. When in a source code block, call `org-edit-src-code'.
  17482. When in a fixed-width region, call `org-edit-fixed-width-region'.
  17483. When in an #+include line, visit the included file.
  17484. On a link, call `ffap' to visit the link at point.
  17485. Otherwise, return a user error."
  17486. (interactive)
  17487. ;; possibly prep session before editing source
  17488. (when (and (org-in-src-block-p) arg)
  17489. (let* ((info (org-babel-get-src-block-info))
  17490. (lang (nth 0 info))
  17491. (params (nth 2 info))
  17492. (session (cdr (assoc :session params))))
  17493. (when (and info session) ;; we are in a source-code block with a session
  17494. (funcall
  17495. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17496. (cond ;; proceed with `org-edit-special'
  17497. ((save-excursion
  17498. (beginning-of-line 1)
  17499. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17500. (find-file (org-trim (match-string 1))))
  17501. ((org-at-table.el-p) (org-edit-src-code))
  17502. ((or (org-at-table-p)
  17503. (save-excursion
  17504. (beginning-of-line 1)
  17505. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17506. (call-interactively 'org-table-edit-formulas))
  17507. ((org-in-block-p '("src" "example" "latex" "html")) (org-edit-src-code))
  17508. ((org-in-fixed-width-region-p) (org-edit-fixed-width-region))
  17509. ((org-at-regexp-p org-any-link-re) (call-interactively 'ffap))
  17510. (t (user-error "No special environment to edit here"))))
  17511. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17512. (defun org-ctrl-c-ctrl-c (&optional arg)
  17513. "Set tags in headline, or update according to changed information at point.
  17514. This command does many different things, depending on context:
  17515. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17516. this is what we do.
  17517. - If the cursor is on a statistics cookie, update it.
  17518. - If the cursor is in a headline, prompt for tags and insert them
  17519. into the current line, aligned to `org-tags-column'. When called
  17520. with prefix arg, realign all tags in the current buffer.
  17521. - If the cursor is in one of the special #+KEYWORD lines, this
  17522. triggers scanning the buffer for these lines and updating the
  17523. information.
  17524. - If the cursor is inside a table, realign the table. This command
  17525. works even if the automatic table editor has been turned off.
  17526. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17527. the entire table.
  17528. - If the cursor is at a footnote reference or definition, jump to
  17529. the corresponding definition or references, respectively.
  17530. - If the cursor is a the beginning of a dynamic block, update it.
  17531. - If the current buffer is a capture buffer, close note and file it.
  17532. - If the cursor is on a <<<target>>>, update radio targets and
  17533. corresponding links in this buffer.
  17534. - If the cursor is on a numbered item in a plain list, renumber the
  17535. ordered list.
  17536. - If the cursor is on a checkbox, toggle it.
  17537. - If the cursor is on a code block, evaluate it. The variable
  17538. `org-confirm-babel-evaluate' can be used to control prompting
  17539. before code block evaluation, by default every code block
  17540. evaluation requires confirmation. Code block evaluation can be
  17541. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17542. (interactive "P")
  17543. (let ((org-enable-table-editor t))
  17544. (cond
  17545. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17546. org-occur-highlights
  17547. org-latex-fragment-image-overlays)
  17548. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17549. (org-remove-occur-highlights)
  17550. (org-remove-latex-fragment-image-overlays)
  17551. (message "Temporary highlights/overlays removed from current buffer"))
  17552. ((and (local-variable-p 'org-finish-function (current-buffer))
  17553. (fboundp org-finish-function))
  17554. (funcall org-finish-function))
  17555. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17556. ((org-in-regexp org-ts-regexp-both)
  17557. (org-timestamp-change 0 'day))
  17558. ((or (looking-at org-property-start-re)
  17559. (org-at-property-p))
  17560. (call-interactively 'org-property-action))
  17561. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17562. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17563. (or (org-at-heading-p) (org-at-item-p)))
  17564. (call-interactively 'org-update-statistics-cookies))
  17565. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17566. ((org-at-table.el-p)
  17567. (message "Use C-c ' to edit table.el tables"))
  17568. ((org-at-table-p)
  17569. (org-table-maybe-eval-formula)
  17570. (if arg
  17571. (call-interactively 'org-table-recalculate)
  17572. (org-table-maybe-recalculate-line))
  17573. (call-interactively 'org-table-align)
  17574. (orgtbl-send-table 'maybe))
  17575. ((or (org-footnote-at-reference-p)
  17576. (org-footnote-at-definition-p))
  17577. (call-interactively 'org-footnote-action))
  17578. ((org-at-item-checkbox-p)
  17579. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17580. ;; list only if at top item.
  17581. (let* ((cbox (match-string 1))
  17582. (struct (org-list-struct))
  17583. (old-struct (copy-tree struct))
  17584. (parents (org-list-parents-alist struct))
  17585. (orderedp (org-entry-get nil "ORDERED"))
  17586. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17587. block-item)
  17588. ;; Use a light version of `org-toggle-checkbox' to avoid
  17589. ;; computing list structure twice.
  17590. (let ((new-box (cond
  17591. ((equal arg '(16)) "[-]")
  17592. ((equal arg '(4)) nil)
  17593. ((equal "[X]" cbox) "[ ]")
  17594. (t "[X]"))))
  17595. (if (and firstp arg)
  17596. ;; If at first item of sub-list, remove check-box from
  17597. ;; every item at the same level.
  17598. (mapc
  17599. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17600. (org-list-get-all-items
  17601. (point-at-bol) struct (org-list-prevs-alist struct)))
  17602. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17603. ;; Replicate `org-list-write-struct', while grabbing a return
  17604. ;; value from `org-list-struct-fix-box'.
  17605. (org-list-struct-fix-ind struct parents 2)
  17606. (org-list-struct-fix-item-end struct)
  17607. (let ((prevs (org-list-prevs-alist struct)))
  17608. (org-list-struct-fix-bul struct prevs)
  17609. (org-list-struct-fix-ind struct parents)
  17610. (setq block-item
  17611. (org-list-struct-fix-box struct parents prevs orderedp)))
  17612. (if (equal struct old-struct)
  17613. (user-error "Cannot toggle this checkbox (unchecked subitems?)")
  17614. (org-list-struct-apply-struct struct old-struct)
  17615. (org-update-checkbox-count-maybe))
  17616. (when block-item
  17617. (message
  17618. "Checkboxes were removed due to unchecked box at line %d"
  17619. (org-current-line block-item)))
  17620. (when firstp (org-list-send-list 'maybe))))
  17621. ((org-at-item-p)
  17622. ;; Cursor at an item: repair list. Do checkbox related actions
  17623. ;; only if function was called with an argument. Send list only
  17624. ;; if at top item.
  17625. (let* ((struct (org-list-struct))
  17626. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17627. old-struct)
  17628. (when arg
  17629. (setq old-struct (copy-tree struct))
  17630. (if firstp
  17631. ;; If at first item of sub-list, add check-box to every
  17632. ;; item at the same level.
  17633. (mapc
  17634. (lambda (pos)
  17635. (unless (org-list-get-checkbox pos struct)
  17636. (org-list-set-checkbox pos struct "[ ]")))
  17637. (org-list-get-all-items
  17638. (point-at-bol) struct (org-list-prevs-alist struct)))
  17639. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17640. (org-list-write-struct
  17641. struct (org-list-parents-alist struct) old-struct)
  17642. (when arg (org-update-checkbox-count-maybe))
  17643. (when firstp (org-list-send-list 'maybe))))
  17644. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17645. ;; Dynamic block
  17646. (beginning-of-line 1)
  17647. (save-excursion (org-update-dblock)))
  17648. ((save-excursion
  17649. (let ((case-fold-search t))
  17650. (beginning-of-line 1)
  17651. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  17652. (cond
  17653. ((or (equal (match-string 1) "TBLFM")
  17654. (equal (match-string 1) "tblfm"))
  17655. ;; Recalculate the table before this line
  17656. (save-excursion
  17657. (beginning-of-line 1)
  17658. (skip-chars-backward " \r\n\t")
  17659. (if (org-at-table-p)
  17660. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17661. (t
  17662. (let ((org-inhibit-startup-visibility-stuff t)
  17663. (org-startup-align-all-tables nil))
  17664. (when (boundp 'org-table-coordinate-overlays)
  17665. (mapc 'delete-overlay org-table-coordinate-overlays)
  17666. (setq org-table-coordinate-overlays nil))
  17667. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17668. (message "Local setup has been refreshed"))))
  17669. ((org-clock-update-time-maybe))
  17670. (t
  17671. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17672. (error "C-c C-c can do nothing useful at this location"))))))
  17673. (defun org-mode-restart ()
  17674. "Restart Org-mode, to scan again for special lines.
  17675. Also updates the keyword regular expressions."
  17676. (interactive)
  17677. (org-mode)
  17678. (message "Org-mode restarted"))
  17679. (defun org-kill-note-or-show-branches ()
  17680. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17681. (interactive)
  17682. (if (not org-finish-function)
  17683. (progn
  17684. (hide-subtree)
  17685. (call-interactively 'show-branches))
  17686. (let ((org-note-abort t))
  17687. (funcall org-finish-function))))
  17688. (defun org-return (&optional indent)
  17689. "Goto next table row or insert a newline.
  17690. Calls `org-table-next-row' or `newline', depending on context.
  17691. See the individual commands for more information."
  17692. (interactive)
  17693. (let (org-ts-what)
  17694. (cond
  17695. ((or (bobp) (org-in-src-block-p))
  17696. (if indent (newline-and-indent) (newline)))
  17697. ((org-at-table-p)
  17698. (org-table-justify-field-maybe)
  17699. (call-interactively 'org-table-next-row))
  17700. ;; when `newline-and-indent' is called within a list, make sure
  17701. ;; text moved stays inside the item.
  17702. ((and (org-in-item-p) indent)
  17703. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17704. (progn
  17705. (save-match-data (newline))
  17706. (org-indent-line-to (length (match-string 0))))
  17707. (let ((ind (org-get-indentation)))
  17708. (newline)
  17709. (if (org-looking-back org-list-end-re)
  17710. (org-indent-line)
  17711. (org-indent-line-to ind)))))
  17712. ((and org-return-follows-link
  17713. (org-at-timestamp-p t)
  17714. (not (eq org-ts-what 'after)))
  17715. (org-follow-timestamp-link))
  17716. ((and org-return-follows-link
  17717. (let ((tprop (get-text-property (point) 'face)))
  17718. (or (eq tprop 'org-link)
  17719. (and (listp tprop) (memq 'org-link tprop)))))
  17720. (call-interactively 'org-open-at-point))
  17721. ((and (org-at-heading-p)
  17722. (looking-at
  17723. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17724. (org-show-entry)
  17725. (end-of-line 1)
  17726. (newline))
  17727. (t (if indent (newline-and-indent) (newline))))))
  17728. (defun org-return-indent ()
  17729. "Goto next table row or insert a newline and indent.
  17730. Calls `org-table-next-row' or `newline-and-indent', depending on
  17731. context. See the individual commands for more information."
  17732. (interactive)
  17733. (org-return t))
  17734. (defun org-ctrl-c-star ()
  17735. "Compute table, or change heading status of lines.
  17736. Calls `org-table-recalculate' or `org-toggle-heading',
  17737. depending on context."
  17738. (interactive)
  17739. (cond
  17740. ((org-at-table-p)
  17741. (call-interactively 'org-table-recalculate))
  17742. (t
  17743. ;; Convert all lines in region to list items
  17744. (call-interactively 'org-toggle-heading))))
  17745. (defun org-ctrl-c-minus ()
  17746. "Insert separator line in table or modify bullet status of line.
  17747. Also turns a plain line or a region of lines into list items.
  17748. Calls `org-table-insert-hline', `org-toggle-item', or
  17749. `org-cycle-list-bullet', depending on context."
  17750. (interactive)
  17751. (cond
  17752. ((org-at-table-p)
  17753. (call-interactively 'org-table-insert-hline))
  17754. ((org-region-active-p)
  17755. (call-interactively 'org-toggle-item))
  17756. ((org-in-item-p)
  17757. (call-interactively 'org-cycle-list-bullet))
  17758. (t
  17759. (call-interactively 'org-toggle-item))))
  17760. (defun org-toggle-item (arg)
  17761. "Convert headings or normal lines to items, items to normal lines.
  17762. If there is no active region, only the current line is considered.
  17763. If the first non blank line in the region is an headline, convert
  17764. all headlines to items, shifting text accordingly.
  17765. If it is an item, convert all items to normal lines.
  17766. If it is normal text, change region into an item. With a prefix
  17767. argument ARG, change each line in region into an item."
  17768. (interactive "P")
  17769. (let ((shift-text
  17770. (function
  17771. ;; Shift text in current section to IND, from point to END.
  17772. ;; The function leaves point to END line.
  17773. (lambda (ind end)
  17774. (let ((min-i 1000) (end (copy-marker end)))
  17775. ;; First determine the minimum indentation (MIN-I) of
  17776. ;; the text.
  17777. (save-excursion
  17778. (catch 'exit
  17779. (while (< (point) end)
  17780. (let ((i (org-get-indentation)))
  17781. (cond
  17782. ;; Skip blank lines and inline tasks.
  17783. ((looking-at "^[ \t]*$"))
  17784. ((looking-at org-outline-regexp-bol))
  17785. ;; We can't find less than 0 indentation.
  17786. ((zerop i) (throw 'exit (setq min-i 0)))
  17787. ((< i min-i) (setq min-i i))))
  17788. (forward-line))))
  17789. ;; Then indent each line so that a line indented to
  17790. ;; MIN-I becomes indented to IND. Ignore blank lines
  17791. ;; and inline tasks in the process.
  17792. (let ((delta (- ind min-i)))
  17793. (while (< (point) end)
  17794. (unless (or (looking-at "^[ \t]*$")
  17795. (looking-at org-outline-regexp-bol))
  17796. (org-indent-line-to (+ (org-get-indentation) delta)))
  17797. (forward-line)))))))
  17798. (skip-blanks
  17799. (function
  17800. ;; Return beginning of first non-blank line, starting from
  17801. ;; line at POS.
  17802. (lambda (pos)
  17803. (save-excursion
  17804. (goto-char pos)
  17805. (skip-chars-forward " \r\t\n")
  17806. (point-at-bol)))))
  17807. beg end)
  17808. ;; Determine boundaries of changes.
  17809. (if (org-region-active-p)
  17810. (setq beg (funcall skip-blanks (region-beginning))
  17811. end (copy-marker (region-end)))
  17812. (setq beg (funcall skip-blanks (point-at-bol))
  17813. end (copy-marker (point-at-eol))))
  17814. ;; Depending on the starting line, choose an action on the text
  17815. ;; between BEG and END.
  17816. (org-with-limited-levels
  17817. (save-excursion
  17818. (goto-char beg)
  17819. (cond
  17820. ;; Case 1. Start at an item: de-itemize. Note that it only
  17821. ;; happens when a region is active: `org-ctrl-c-minus'
  17822. ;; would call `org-cycle-list-bullet' otherwise.
  17823. ((org-at-item-p)
  17824. (while (< (point) end)
  17825. (when (org-at-item-p)
  17826. (skip-chars-forward " \t")
  17827. (delete-region (point) (match-end 0)))
  17828. (forward-line)))
  17829. ;; Case 2. Start at an heading: convert to items.
  17830. ((org-at-heading-p)
  17831. (let* ((bul (org-list-bullet-string "-"))
  17832. (bul-len (length bul))
  17833. ;; Indentation of the first heading. It should be
  17834. ;; relative to the indentation of its parent, if any.
  17835. (start-ind (save-excursion
  17836. (cond
  17837. ((not org-adapt-indentation) 0)
  17838. ((not (outline-previous-heading)) 0)
  17839. (t (length (match-string 0))))))
  17840. ;; Level of first heading. Further headings will be
  17841. ;; compared to it to determine hierarchy in the list.
  17842. (ref-level (org-reduced-level (org-outline-level))))
  17843. (while (< (point) end)
  17844. (let* ((level (org-reduced-level (org-outline-level)))
  17845. (delta (max 0 (- level ref-level))))
  17846. ;; If current headline is less indented than the first
  17847. ;; one, set it as reference, in order to preserve
  17848. ;; subtrees.
  17849. (when (< level ref-level) (setq ref-level level))
  17850. (replace-match bul t t)
  17851. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17852. ;; Ensure all text down to END (or SECTION-END) belongs
  17853. ;; to the newly created item.
  17854. (let ((section-end (save-excursion
  17855. (or (outline-next-heading) (point)))))
  17856. (forward-line)
  17857. (funcall shift-text
  17858. (+ start-ind (* (1+ delta) bul-len))
  17859. (min end section-end)))))))
  17860. ;; Case 3. Normal line with ARG: turn each non-item line into
  17861. ;; an item.
  17862. (arg
  17863. (while (< (point) end)
  17864. (unless (or (org-at-heading-p) (org-at-item-p))
  17865. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17866. (replace-match
  17867. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17868. (forward-line)))
  17869. ;; Case 4. Normal line without ARG: make the first line of
  17870. ;; region an item, and shift indentation of others
  17871. ;; lines to set them as item's body.
  17872. (t (let* ((bul (org-list-bullet-string "-"))
  17873. (bul-len (length bul))
  17874. (ref-ind (org-get-indentation)))
  17875. (skip-chars-forward " \t")
  17876. (insert bul)
  17877. (forward-line)
  17878. (while (< (point) end)
  17879. ;; Ensure that lines less indented than first one
  17880. ;; still get included in item body.
  17881. (funcall shift-text
  17882. (+ ref-ind bul-len)
  17883. (min end (save-excursion (or (outline-next-heading)
  17884. (point)))))
  17885. (forward-line)))))))))
  17886. (defun org-toggle-heading (&optional nstars)
  17887. "Convert headings to normal text, or items or text to headings.
  17888. If there is no active region, only the current line is considered.
  17889. With a \\[universal-argument] prefix, convert the whole list at
  17890. point into heading.
  17891. In a region:
  17892. - If the first non blank line is an headline, remove the stars
  17893. from all headlines in the region.
  17894. - If it is a normal line turn each and every normal line (i.e. not an
  17895. heading or an item) in the region into a heading.
  17896. - If it is a plain list item, turn all plain list items into headings.
  17897. When converting a line into a heading, the number of stars is chosen
  17898. such that the lines become children of the current entry. However,
  17899. when a prefix argument is given, its value determines the number of
  17900. stars to add."
  17901. (interactive "P")
  17902. (let ((skip-blanks
  17903. (function
  17904. ;; Return beginning of first non-blank line, starting from
  17905. ;; line at POS.
  17906. (lambda (pos)
  17907. (save-excursion
  17908. (goto-char pos)
  17909. (while (org-at-comment-p) (forward-line))
  17910. (skip-chars-forward " \r\t\n")
  17911. (point-at-bol)))))
  17912. beg end toggled)
  17913. ;; Determine boundaries of changes. If a universal prefix has
  17914. ;; been given, put the list in a region. If region ends at a bol,
  17915. ;; do not consider the last line to be in the region.
  17916. (when (and current-prefix-arg (org-at-item-p))
  17917. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  17918. (org-mark-element))
  17919. (if (org-region-active-p)
  17920. (setq beg (funcall skip-blanks (region-beginning))
  17921. end (copy-marker (save-excursion
  17922. (goto-char (region-end))
  17923. (if (bolp) (point) (point-at-eol)))))
  17924. (setq beg (funcall skip-blanks (point-at-bol))
  17925. end (copy-marker (point-at-eol))))
  17926. ;; Ensure inline tasks don't count as headings.
  17927. (org-with-limited-levels
  17928. (save-excursion
  17929. (goto-char beg)
  17930. (cond
  17931. ;; Case 1. Started at an heading: de-star headings.
  17932. ((org-at-heading-p)
  17933. (while (< (point) end)
  17934. (when (org-at-heading-p t)
  17935. (looking-at org-outline-regexp) (replace-match "")
  17936. (setq toggled t))
  17937. (forward-line)))
  17938. ;; Case 2. Started at an item: change items into headlines.
  17939. ;; One star will be added by `org-list-to-subtree'.
  17940. ((org-at-item-p)
  17941. (let* ((stars (make-string
  17942. (if nstars
  17943. ;; subtract the star that will be added again by
  17944. ;; `org-list-to-subtree'
  17945. (1- (prefix-numeric-value current-prefix-arg))
  17946. (or (org-current-level) 0))
  17947. ?*))
  17948. (add-stars
  17949. (cond (nstars "") ; stars from prefix only
  17950. ((equal stars "") "") ; before first heading
  17951. (org-odd-levels-only "*") ; inside heading, odd
  17952. (t "")))) ; inside heading, oddeven
  17953. (while (< (point) end)
  17954. (when (org-at-item-p)
  17955. ;; Pay attention to cases when region ends before list.
  17956. (let* ((struct (org-list-struct))
  17957. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17958. (save-restriction
  17959. (narrow-to-region (point) list-end)
  17960. (insert
  17961. (org-list-to-subtree
  17962. (org-list-parse-list t)
  17963. '(:istart (concat stars add-stars (funcall get-stars depth))
  17964. :icount (concat stars add-stars (funcall get-stars depth)))))))
  17965. (setq toggled t))
  17966. (forward-line))))
  17967. ;; Case 3. Started at normal text: make every line an heading,
  17968. ;; skipping headlines and items.
  17969. (t (let* ((stars (make-string
  17970. (if nstars
  17971. (prefix-numeric-value current-prefix-arg)
  17972. (or (org-current-level) 0))
  17973. ?*))
  17974. (add-stars
  17975. (cond (nstars "") ; stars from prefix only
  17976. ((equal stars "") "*") ; before first heading
  17977. (org-odd-levels-only "**") ; inside heading, odd
  17978. (t "*"))) ; inside heading, oddeven
  17979. (rpl (concat stars add-stars " ")))
  17980. (while (< (point) end)
  17981. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  17982. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17983. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  17984. (forward-line)))))))
  17985. (unless toggled (message "Cannot toggle heading from here"))))
  17986. (defun org-meta-return (&optional arg)
  17987. "Insert a new heading or wrap a region in a table.
  17988. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17989. See the individual commands for more information."
  17990. (interactive "P")
  17991. (cond
  17992. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17993. ((or (org-at-drawer-p) (org-at-property-p))
  17994. (newline-and-indent))
  17995. ((org-at-table-p)
  17996. (call-interactively 'org-table-wrap-region))
  17997. (t (call-interactively 'org-insert-heading))))
  17998. ;;; Menu entries
  17999. (defsubst org-in-subtree-not-table-p ()
  18000. "Are we in a subtree and not in a table?"
  18001. (and (not (org-before-first-heading-p))
  18002. (not (org-at-table-p))))
  18003. ;; Define the Org-mode menus
  18004. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18005. '("Tbl"
  18006. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18007. ["Next Field" org-cycle (org-at-table-p)]
  18008. ["Previous Field" org-shifttab (org-at-table-p)]
  18009. ["Next Row" org-return (org-at-table-p)]
  18010. "--"
  18011. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18012. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18013. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18014. "--"
  18015. ("Column"
  18016. ["Move Column Left" org-metaleft (org-at-table-p)]
  18017. ["Move Column Right" org-metaright (org-at-table-p)]
  18018. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18019. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18020. ("Row"
  18021. ["Move Row Up" org-metaup (org-at-table-p)]
  18022. ["Move Row Down" org-metadown (org-at-table-p)]
  18023. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18024. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18025. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18026. "--"
  18027. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18028. ("Rectangle"
  18029. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18030. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18031. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18032. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18033. "--"
  18034. ("Calculate"
  18035. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18036. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18037. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18038. "--"
  18039. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18040. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18041. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18042. "--"
  18043. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18044. "--"
  18045. ["Sum Column/Rectangle" org-table-sum
  18046. (or (org-at-table-p) (org-region-active-p))]
  18047. ["Which Column?" org-table-current-column (org-at-table-p)])
  18048. ["Debug Formulas"
  18049. org-table-toggle-formula-debugger
  18050. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18051. ["Show Col/Row Numbers"
  18052. org-table-toggle-coordinate-overlays
  18053. :style toggle
  18054. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18055. "--"
  18056. ["Create" org-table-create (and (not (org-at-table-p))
  18057. org-enable-table-editor)]
  18058. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18059. ["Import from File" org-table-import (not (org-at-table-p))]
  18060. ["Export to File" org-table-export (org-at-table-p)]
  18061. "--"
  18062. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18063. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18064. '("Org"
  18065. ("Show/Hide"
  18066. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18067. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18068. ["Sparse Tree..." org-sparse-tree t]
  18069. ["Reveal Context" org-reveal t]
  18070. ["Show All" show-all t]
  18071. "--"
  18072. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18073. "--"
  18074. ["New Heading" org-insert-heading t]
  18075. ("Navigate Headings"
  18076. ["Up" outline-up-heading t]
  18077. ["Next" outline-next-visible-heading t]
  18078. ["Previous" outline-previous-visible-heading t]
  18079. ["Next Same Level" outline-forward-same-level t]
  18080. ["Previous Same Level" outline-backward-same-level t]
  18081. "--"
  18082. ["Jump" org-goto t])
  18083. ("Edit Structure"
  18084. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18085. "--"
  18086. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18087. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18088. "--"
  18089. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18090. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18091. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18092. "--"
  18093. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18094. "--"
  18095. ["Copy visible text" org-copy-visible t]
  18096. "--"
  18097. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18098. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18099. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18100. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18101. "--"
  18102. ["Sort Region/Children" org-sort t]
  18103. "--"
  18104. ["Convert to odd levels" org-convert-to-odd-levels t]
  18105. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18106. ("Editing"
  18107. ["Emphasis..." org-emphasize t]
  18108. ["Edit Source Example" org-edit-special t]
  18109. "--"
  18110. ["Footnote new/jump" org-footnote-action t]
  18111. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18112. ("Archive"
  18113. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18114. "--"
  18115. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18116. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18117. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18118. )
  18119. "--"
  18120. ("Hyperlinks"
  18121. ["Store Link (Global)" org-store-link t]
  18122. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18123. ["Insert Link" org-insert-link t]
  18124. ["Follow Link" org-open-at-point t]
  18125. "--"
  18126. ["Next link" org-next-link t]
  18127. ["Previous link" org-previous-link t]
  18128. "--"
  18129. ["Descriptive Links"
  18130. org-toggle-link-display
  18131. :style radio
  18132. :selected org-descriptive-links
  18133. ]
  18134. ["Literal Links"
  18135. org-toggle-link-display
  18136. :style radio
  18137. :selected (not org-descriptive-links)])
  18138. "--"
  18139. ("TODO Lists"
  18140. ["TODO/DONE/-" org-todo t]
  18141. ("Select keyword"
  18142. ["Next keyword" org-shiftright (org-at-heading-p)]
  18143. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18144. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18145. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18146. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18147. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18148. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18149. "--"
  18150. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18151. :selected org-enforce-todo-dependencies :style toggle :active t]
  18152. "Settings for tree at point"
  18153. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18154. :selected (org-entry-get nil "ORDERED")
  18155. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18156. ["Do Children parallel" org-toggle-ordered-property :style radio
  18157. :selected (not (org-entry-get nil "ORDERED"))
  18158. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18159. "--"
  18160. ["Set Priority" org-priority t]
  18161. ["Priority Up" org-shiftup t]
  18162. ["Priority Down" org-shiftdown t]
  18163. "--"
  18164. ["Get news from all feeds" org-feed-update-all t]
  18165. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18166. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18167. ("TAGS and Properties"
  18168. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18169. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18170. "--"
  18171. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18172. ["Column view of properties" org-columns t]
  18173. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18174. ("Dates and Scheduling"
  18175. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18176. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18177. ("Change Date"
  18178. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18179. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18180. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18181. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18182. ["Compute Time Range" org-evaluate-time-range t]
  18183. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18184. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18185. "--"
  18186. ["Custom time format" org-toggle-time-stamp-overlays
  18187. :style radio :selected org-display-custom-times]
  18188. "--"
  18189. ["Goto Calendar" org-goto-calendar t]
  18190. ["Date from Calendar" org-date-from-calendar t]
  18191. "--"
  18192. ["Start/Restart Timer" org-timer-start t]
  18193. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18194. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18195. ["Insert Timer String" org-timer t]
  18196. ["Insert Timer Item" org-timer-item t])
  18197. ("Logging work"
  18198. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18199. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18200. ["Clock out" org-clock-out t]
  18201. ["Clock cancel" org-clock-cancel t]
  18202. "--"
  18203. ["Mark as default task" org-clock-mark-default-task t]
  18204. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18205. ["Goto running clock" org-clock-goto t]
  18206. "--"
  18207. ["Display times" org-clock-display t]
  18208. ["Create clock table" org-clock-report t]
  18209. "--"
  18210. ["Record DONE time"
  18211. (progn (setq org-log-done (not org-log-done))
  18212. (message "Switching to %s will %s record a timestamp"
  18213. (car org-done-keywords)
  18214. (if org-log-done "automatically" "not")))
  18215. :style toggle :selected org-log-done])
  18216. "--"
  18217. ["Agenda Command..." org-agenda t]
  18218. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18219. ("File List for Agenda")
  18220. ("Special views current file"
  18221. ["TODO Tree" org-show-todo-tree t]
  18222. ["Check Deadlines" org-check-deadlines t]
  18223. ["Timeline" org-timeline t]
  18224. ["Tags/Property tree" org-match-sparse-tree t])
  18225. "--"
  18226. ["Export/Publish..." org-export t]
  18227. ("LaTeX"
  18228. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18229. :selected org-cdlatex-mode]
  18230. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18231. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18232. ["Modify math symbol" org-cdlatex-math-modify
  18233. (org-inside-LaTeX-fragment-p)]
  18234. ["Insert citation" org-reftex-citation t]
  18235. "--"
  18236. ["Template for BEAMER" (progn (require 'org-beamer)
  18237. (org-insert-beamer-options-template)) t])
  18238. "--"
  18239. ("MobileOrg"
  18240. ["Push Files and Views" org-mobile-push t]
  18241. ["Get Captured and Flagged" org-mobile-pull t]
  18242. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18243. "--"
  18244. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18245. "--"
  18246. ("Documentation"
  18247. ["Show Version" org-version t]
  18248. ["Info Documentation" org-info t])
  18249. ("Customize"
  18250. ["Browse Org Group" org-customize t]
  18251. "--"
  18252. ["Expand This Menu" org-create-customize-menu
  18253. (fboundp 'customize-menu-create)])
  18254. ["Send bug report" org-submit-bug-report t]
  18255. "--"
  18256. ("Refresh/Reload"
  18257. ["Refresh setup current buffer" org-mode-restart t]
  18258. ["Reload Org (after update)" org-reload t]
  18259. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  18260. ))
  18261. (defun org-info (&optional node)
  18262. "Read documentation for Org-mode in the info system.
  18263. With optional NODE, go directly to that node."
  18264. (interactive)
  18265. (info (format "(org)%s" (or node ""))))
  18266. ;;;###autoload
  18267. (defun org-submit-bug-report ()
  18268. "Submit a bug report on Org-mode via mail.
  18269. Don't hesitate to report any problems or inaccurate documentation.
  18270. If you don't have setup sending mail from (X)Emacs, please copy the
  18271. output buffer into your mail program, as it gives us important
  18272. information about your Org-mode version and configuration."
  18273. (interactive)
  18274. (require 'reporter)
  18275. (org-load-modules-maybe)
  18276. (org-require-autoloaded-modules)
  18277. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18278. (reporter-submit-bug-report
  18279. "emacs-orgmode@gnu.org"
  18280. (org-version nil 'full)
  18281. (let (list)
  18282. (save-window-excursion
  18283. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18284. (delete-other-windows)
  18285. (erase-buffer)
  18286. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18287. We would like to add your full Org-mode and Outline configuration to the
  18288. bug report. This greatly simplifies the work of the maintainer and
  18289. other experts on the mailing list.
  18290. HOWEVER, some variables you have customized may contain private
  18291. information. The names of customers, colleagues, or friends, might
  18292. appear in the form of file names, tags, todo states, or search strings.
  18293. If you answer yes to the prompt, you might want to check and remove
  18294. such private information before sending the email.")
  18295. (add-text-properties (point-min) (point-max) '(face org-warning))
  18296. (when (yes-or-no-p "Include your Org-mode configuration ")
  18297. (mapatoms
  18298. (lambda (v)
  18299. (and (boundp v)
  18300. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18301. (or (and (symbol-value v)
  18302. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18303. (and
  18304. (get v 'custom-type) (get v 'standard-value)
  18305. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18306. (push v list)))))
  18307. (kill-buffer (get-buffer "*Warn about privacy*"))
  18308. list))
  18309. nil nil
  18310. "Remember to cover the basics, that is, what you expected to happen and
  18311. what in fact did happen. You don't know how to make a good report? See
  18312. http://orgmode.org/manual/Feedback.html#Feedback
  18313. Your bug report will be posted to the Org-mode mailing list.
  18314. ------------------------------------------------------------------------")
  18315. (save-excursion
  18316. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18317. (replace-match "\\1Bug: \\3 [\\2]")))))
  18318. (defun org-install-agenda-files-menu ()
  18319. (let ((bl (buffer-list)))
  18320. (save-excursion
  18321. (while bl
  18322. (set-buffer (pop bl))
  18323. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18324. (when (derived-mode-p 'org-mode)
  18325. (easy-menu-change
  18326. '("Org") "File List for Agenda"
  18327. (append
  18328. (list
  18329. ["Edit File List" (org-edit-agenda-file-list) t]
  18330. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18331. ["Remove Current File from List" org-remove-file t]
  18332. ["Cycle through agenda files" org-cycle-agenda-files t]
  18333. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18334. "--")
  18335. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18336. ;;;; Documentation
  18337. (defun org-require-autoloaded-modules ()
  18338. (interactive)
  18339. (mapc 'require
  18340. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18341. org-docbook org-exp org-html org-icalendar
  18342. org-id org-latex
  18343. org-publish org-remember org-table
  18344. org-timer org-xoxo)))
  18345. ;;;###autoload
  18346. (defun org-reload (&optional uncompiled)
  18347. "Reload all org lisp files.
  18348. With prefix arg UNCOMPILED, load the uncompiled versions."
  18349. (interactive "P")
  18350. (require 'find-func)
  18351. (let* ((file-re "^org\\(-.*\\)?\\.el")
  18352. (dir-org (file-name-directory (org-find-library-dir "org")))
  18353. (dir-org-contrib (ignore-errors
  18354. (file-name-directory
  18355. (org-find-library-dir "org-contribdir"))))
  18356. (babel-files
  18357. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  18358. (append (list nil "comint" "eval" "exp" "keys"
  18359. "lob" "ref" "table" "tangle")
  18360. (delq nil
  18361. (mapcar
  18362. (lambda (lang)
  18363. (when (cdr lang) (symbol-name (car lang))))
  18364. org-babel-load-languages)))))
  18365. (files
  18366. (append babel-files
  18367. (and dir-org-contrib
  18368. (directory-files dir-org-contrib t file-re))
  18369. (directory-files dir-org t file-re)))
  18370. (remove-re (concat (if (featurep 'xemacs)
  18371. "org-colview" "org-colview-xemacs")
  18372. "\\'")))
  18373. (setq files (mapcar 'file-name-sans-extension files))
  18374. (setq files (mapcar
  18375. (lambda (x) (if (string-match remove-re x) nil x))
  18376. files))
  18377. (setq files (delq nil files))
  18378. (mapc
  18379. (lambda (f)
  18380. (when (featurep (intern (file-name-nondirectory f)))
  18381. (if (and (not uncompiled)
  18382. (file-exists-p (concat f ".elc")))
  18383. (load (concat f ".elc") nil nil 'nosuffix)
  18384. (load (concat f ".el") nil nil 'nosuffix))))
  18385. files)
  18386. (load (concat dir-org "org-version.el") 'noerror nil 'nosuffix))
  18387. (org-version nil 'full 'message))
  18388. ;;;###autoload
  18389. (defun org-customize ()
  18390. "Call the customize function with org as argument."
  18391. (interactive)
  18392. (org-load-modules-maybe)
  18393. (org-require-autoloaded-modules)
  18394. (customize-browse 'org))
  18395. (defun org-create-customize-menu ()
  18396. "Create a full customization menu for Org-mode, insert it into the menu."
  18397. (interactive)
  18398. (org-load-modules-maybe)
  18399. (org-require-autoloaded-modules)
  18400. (if (fboundp 'customize-menu-create)
  18401. (progn
  18402. (easy-menu-change
  18403. '("Org") "Customize"
  18404. `(["Browse Org group" org-customize t]
  18405. "--"
  18406. ,(customize-menu-create 'org)
  18407. ["Set" Custom-set t]
  18408. ["Save" Custom-save t]
  18409. ["Reset to Current" Custom-reset-current t]
  18410. ["Reset to Saved" Custom-reset-saved t]
  18411. ["Reset to Standard Settings" Custom-reset-standard t]))
  18412. (message "\"Org\"-menu now contains full customization menu"))
  18413. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18414. ;;;; Miscellaneous stuff
  18415. ;;; Generally useful functions
  18416. (defun org-get-at-bol (property)
  18417. "Get text property PROPERTY at beginning of line."
  18418. (get-text-property (point-at-bol) property))
  18419. (defun org-find-text-property-in-string (prop s)
  18420. "Return the first non-nil value of property PROP in string S."
  18421. (or (get-text-property 0 prop s)
  18422. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18423. prop s)))
  18424. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18425. "Display the given MESSAGE as a warning."
  18426. (if (fboundp 'display-warning)
  18427. (display-warning 'org message
  18428. (if (featurep 'xemacs) 'warning :warning))
  18429. (let ((buf (get-buffer-create "*Org warnings*")))
  18430. (with-current-buffer buf
  18431. (goto-char (point-max))
  18432. (insert "Warning (Org): " message)
  18433. (unless (bolp)
  18434. (newline)))
  18435. (display-buffer buf)
  18436. (sit-for 0))))
  18437. (defun org-eval (form)
  18438. "Eval FORM and return result."
  18439. (condition-case error
  18440. (eval form)
  18441. (error (format "%%![Error: %s]" error))))
  18442. (defun org-in-clocktable-p ()
  18443. "Check if the cursor is in a clocktable."
  18444. (let ((pos (point)) start)
  18445. (save-excursion
  18446. (end-of-line 1)
  18447. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18448. (setq start (match-beginning 0))
  18449. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18450. (>= (match-end 0) pos)
  18451. start))))
  18452. (defun org-in-commented-line ()
  18453. "Is point in a line starting with `#'?"
  18454. (equal (char-after (point-at-bol)) ?#))
  18455. (defun org-in-indented-comment-line ()
  18456. "Is point in a line starting with `#' after some white space?"
  18457. (save-excursion
  18458. (save-match-data
  18459. (goto-char (point-at-bol))
  18460. (looking-at "[ \t]*#"))))
  18461. (defun org-in-verbatim-emphasis ()
  18462. (save-match-data
  18463. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18464. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18465. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18466. (if (and marker (marker-buffer marker)
  18467. (buffer-live-p (marker-buffer marker)))
  18468. (progn
  18469. (org-pop-to-buffer-same-window (marker-buffer marker))
  18470. (if (or (> marker (point-max)) (< marker (point-min)))
  18471. (widen))
  18472. (goto-char marker)
  18473. (org-show-context 'org-goto))
  18474. (if bookmark
  18475. (bookmark-jump bookmark)
  18476. (error "Cannot find location"))))
  18477. (defun org-quote-csv-field (s)
  18478. "Quote field for inclusion in CSV material."
  18479. (if (string-match "[\",]" s)
  18480. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18481. s))
  18482. (defun org-force-self-insert (N)
  18483. "Needed to enforce self-insert under remapping."
  18484. (interactive "p")
  18485. (self-insert-command N))
  18486. (defun org-string-width (s)
  18487. "Compute width of string, ignoring invisible characters.
  18488. This ignores character with invisibility property `org-link', and also
  18489. characters with property `org-cwidth', because these will become invisible
  18490. upon the next fontification round."
  18491. (let (b l)
  18492. (when (or (eq t buffer-invisibility-spec)
  18493. (assq 'org-link buffer-invisibility-spec))
  18494. (while (setq b (text-property-any 0 (length s)
  18495. 'invisible 'org-link s))
  18496. (setq s (concat (substring s 0 b)
  18497. (substring s (or (next-single-property-change
  18498. b 'invisible s) (length s)))))))
  18499. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18500. (setq s (concat (substring s 0 b)
  18501. (substring s (or (next-single-property-change
  18502. b 'org-cwidth s) (length s))))))
  18503. (setq l (string-width s) b -1)
  18504. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18505. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18506. l))
  18507. (defun org-shorten-string (s maxlength)
  18508. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18509. If the string is shorter or has length MAXLENGTH, just return the
  18510. original string. If it is longer, the functions finds a space in the
  18511. string, breaks this string off at that locations and adds three dots
  18512. as ellipsis. Including the ellipsis, the string will not be longer
  18513. than MAXLENGTH. If finding a good breaking point in the string does
  18514. not work, the string is just chopped off in the middle of a word
  18515. if necessary."
  18516. (if (<= (length s) maxlength)
  18517. s
  18518. (let* ((n (max (- maxlength 4) 1))
  18519. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18520. (if (string-match re s)
  18521. (concat (match-string 1 s) "...")
  18522. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18523. (defun org-get-indentation (&optional line)
  18524. "Get the indentation of the current line, interpreting tabs.
  18525. When LINE is given, assume it represents a line and compute its indentation."
  18526. (if line
  18527. (if (string-match "^ *" (org-remove-tabs line))
  18528. (match-end 0))
  18529. (save-excursion
  18530. (beginning-of-line 1)
  18531. (skip-chars-forward " \t")
  18532. (current-column))))
  18533. (defun org-get-string-indentation (s)
  18534. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18535. (let ((n -1) (i 0) (w tab-width) c)
  18536. (catch 'exit
  18537. (while (< (setq n (1+ n)) (length s))
  18538. (setq c (aref s n))
  18539. (cond ((= c ?\ ) (setq i (1+ i)))
  18540. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18541. (t (throw 'exit t)))))
  18542. i))
  18543. (defun org-remove-tabs (s &optional width)
  18544. "Replace tabulators in S with spaces.
  18545. Assumes that s is a single line, starting in column 0."
  18546. (setq width (or width tab-width))
  18547. (while (string-match "\t" s)
  18548. (setq s (replace-match
  18549. (make-string
  18550. (- (* width (/ (+ (match-beginning 0) width) width))
  18551. (match-beginning 0)) ?\ )
  18552. t t s)))
  18553. s)
  18554. (defun org-fix-indentation (line ind)
  18555. "Fix indentation in LINE.
  18556. IND is a cons cell with target and minimum indentation.
  18557. If the current indentation in LINE is smaller than the minimum,
  18558. leave it alone. If it is larger than ind, set it to the target."
  18559. (let* ((l (org-remove-tabs line))
  18560. (i (org-get-indentation l))
  18561. (i1 (car ind)) (i2 (cdr ind)))
  18562. (if (>= i i2) (setq l (substring line i2)))
  18563. (if (> i1 0)
  18564. (concat (make-string i1 ?\ ) l)
  18565. l)))
  18566. (defun org-remove-indentation (code &optional n)
  18567. "Remove the maximum common indentation from the lines in CODE.
  18568. N may optionally be the number of spaces to remove."
  18569. (with-temp-buffer
  18570. (insert code)
  18571. (org-do-remove-indentation n)
  18572. (buffer-string)))
  18573. (defun org-do-remove-indentation (&optional n)
  18574. "Remove the maximum common indentation from the buffer."
  18575. (untabify (point-min) (point-max))
  18576. (let ((min 10000) re)
  18577. (if n
  18578. (setq min n)
  18579. (goto-char (point-min))
  18580. (while (re-search-forward "^ *[^ \n]" nil t)
  18581. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18582. (unless (or (= min 0) (= min 10000))
  18583. (setq re (format "^ \\{%d\\}" min))
  18584. (goto-char (point-min))
  18585. (while (re-search-forward re nil t)
  18586. (replace-match "")
  18587. (end-of-line 1))
  18588. min)))
  18589. (defun org-fill-template (template alist)
  18590. "Find each %key of ALIST in TEMPLATE and replace it."
  18591. (let ((case-fold-search nil)
  18592. entry key value)
  18593. (setq alist (sort (copy-sequence alist)
  18594. (lambda (a b) (< (length (car a)) (length (car b))))))
  18595. (while (setq entry (pop alist))
  18596. (setq template
  18597. (replace-regexp-in-string
  18598. (concat "%" (regexp-quote (car entry)))
  18599. (or (cdr entry) "") template t t)))
  18600. template))
  18601. (defun org-base-buffer (buffer)
  18602. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18603. (if (not buffer)
  18604. buffer
  18605. (or (buffer-base-buffer buffer)
  18606. buffer)))
  18607. (defun org-trim (s)
  18608. "Remove whitespace at beginning and end of string."
  18609. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18610. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18611. s)
  18612. (defun org-wrap (string &optional width lines)
  18613. "Wrap string to either a number of lines, or a width in characters.
  18614. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18615. that costs. If there is a word longer than WIDTH, the text is actually
  18616. wrapped to the length of that word.
  18617. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18618. many lines, whatever width that takes.
  18619. The return value is a list of lines, without newlines at the end."
  18620. (let* ((words (org-split-string string "[ \t\n]+"))
  18621. (maxword (apply 'max (mapcar 'org-string-width words)))
  18622. w ll)
  18623. (cond (width
  18624. (org-do-wrap words (max maxword width)))
  18625. (lines
  18626. (setq w maxword)
  18627. (setq ll (org-do-wrap words maxword))
  18628. (if (<= (length ll) lines)
  18629. ll
  18630. (setq ll words)
  18631. (while (> (length ll) lines)
  18632. (setq w (1+ w))
  18633. (setq ll (org-do-wrap words w)))
  18634. ll))
  18635. (t (error "Cannot wrap this")))))
  18636. (defun org-do-wrap (words width)
  18637. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18638. (let (lines line)
  18639. (while words
  18640. (setq line (pop words))
  18641. (while (and words (< (+ (length line) (length (car words))) width))
  18642. (setq line (concat line " " (pop words))))
  18643. (setq lines (push line lines)))
  18644. (nreverse lines)))
  18645. (defun org-split-string (string &optional separators)
  18646. "Splits STRING into substrings at SEPARATORS.
  18647. No empty strings are returned if there are matches at the beginning
  18648. and end of string."
  18649. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18650. (start 0)
  18651. notfirst
  18652. (list nil))
  18653. (while (and (string-match rexp string
  18654. (if (and notfirst
  18655. (= start (match-beginning 0))
  18656. (< start (length string)))
  18657. (1+ start) start))
  18658. (< (match-beginning 0) (length string)))
  18659. (setq notfirst t)
  18660. (or (eq (match-beginning 0) 0)
  18661. (and (eq (match-beginning 0) (match-end 0))
  18662. (eq (match-beginning 0) start))
  18663. (setq list
  18664. (cons (substring string start (match-beginning 0))
  18665. list)))
  18666. (setq start (match-end 0)))
  18667. (or (eq start (length string))
  18668. (setq list
  18669. (cons (substring string start)
  18670. list)))
  18671. (nreverse list)))
  18672. (defun org-quote-vert (s)
  18673. "Replace \"|\" with \"\\vert\"."
  18674. (while (string-match "|" s)
  18675. (setq s (replace-match "\\vert" t t s)))
  18676. s)
  18677. (defun org-uuidgen-p (s)
  18678. "Is S an ID created by UUIDGEN?"
  18679. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18680. (defun org-in-src-block-p (&optional inside)
  18681. "Whether point is in a code source block.
  18682. When INSIDE is non-nil, don't consider we are within a src block
  18683. when point is at #+BEGIN_SRC or #+END_SRC."
  18684. (let ((case-fold-search t) ov)
  18685. (or (and (setq ov (overlays-at (point)))
  18686. (memq 'org-block-background
  18687. (overlay-properties (car ov))))
  18688. (and (not inside)
  18689. (save-match-data
  18690. (save-excursion
  18691. (beginning-of-line)
  18692. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  18693. (defun org-context ()
  18694. "Return a list of contexts of the current cursor position.
  18695. If several contexts apply, all are returned.
  18696. Each context entry is a list with a symbol naming the context, and
  18697. two positions indicating start and end of the context. Possible
  18698. contexts are:
  18699. :headline anywhere in a headline
  18700. :headline-stars on the leading stars in a headline
  18701. :todo-keyword on a TODO keyword (including DONE) in a headline
  18702. :tags on the TAGS in a headline
  18703. :priority on the priority cookie in a headline
  18704. :item on the first line of a plain list item
  18705. :item-bullet on the bullet/number of a plain list item
  18706. :checkbox on the checkbox in a plain list item
  18707. :table in an org-mode table
  18708. :table-special on a special filed in a table
  18709. :table-table in a table.el table
  18710. :clocktable in a clocktable
  18711. :src-block in a source block
  18712. :link on a hyperlink
  18713. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18714. :target on a <<target>>
  18715. :radio-target on a <<<radio-target>>>
  18716. :latex-fragment on a LaTeX fragment
  18717. :latex-preview on a LaTeX fragment with overlaid preview image
  18718. This function expects the position to be visible because it uses font-lock
  18719. faces as a help to recognize the following contexts: :table-special, :link,
  18720. and :keyword."
  18721. (let* ((f (get-text-property (point) 'face))
  18722. (faces (if (listp f) f (list f)))
  18723. (case-fold-search t)
  18724. (p (point)) clist o)
  18725. ;; First the large context
  18726. (cond
  18727. ((org-at-heading-p t)
  18728. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18729. (when (progn
  18730. (beginning-of-line 1)
  18731. (looking-at org-todo-line-tags-regexp))
  18732. (push (org-point-in-group p 1 :headline-stars) clist)
  18733. (push (org-point-in-group p 2 :todo-keyword) clist)
  18734. (push (org-point-in-group p 4 :tags) clist))
  18735. (goto-char p)
  18736. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18737. (if (looking-at "\\[#[A-Z0-9]\\]")
  18738. (push (org-point-in-group p 0 :priority) clist)))
  18739. ((org-at-item-p)
  18740. (push (org-point-in-group p 2 :item-bullet) clist)
  18741. (push (list :item (point-at-bol)
  18742. (save-excursion (org-end-of-item) (point)))
  18743. clist)
  18744. (and (org-at-item-checkbox-p)
  18745. (push (org-point-in-group p 0 :checkbox) clist)))
  18746. ((org-at-table-p)
  18747. (push (list :table (org-table-begin) (org-table-end)) clist)
  18748. (if (memq 'org-formula faces)
  18749. (push (list :table-special
  18750. (previous-single-property-change p 'face)
  18751. (next-single-property-change p 'face)) clist)))
  18752. ((org-at-table-p 'any)
  18753. (push (list :table-table) clist)))
  18754. (goto-char p)
  18755. (let ((case-fold-search t))
  18756. ;; New the "medium" contexts: clocktables, source blocks
  18757. (cond ((org-in-clocktable-p)
  18758. (push (list :clocktable
  18759. (and (or (looking-at "#\\+BEGIN: clocktable")
  18760. (search-backward "#+BEGIN: clocktable" nil t))
  18761. (match-beginning 0))
  18762. (and (re-search-forward "#\\+END:?" nil t)
  18763. (match-end 0))) clist))
  18764. ((org-in-src-block-p)
  18765. (push (list :src-block
  18766. (and (or (looking-at "#\\+BEGIN_SRC")
  18767. (search-backward "#+BEGIN_SRC" nil t))
  18768. (match-beginning 0))
  18769. (and (search-forward "#+END_SRC" nil t)
  18770. (match-beginning 0))) clist))))
  18771. (goto-char p)
  18772. ;; Now the small context
  18773. (cond
  18774. ((org-at-timestamp-p)
  18775. (push (org-point-in-group p 0 :timestamp) clist))
  18776. ((memq 'org-link faces)
  18777. (push (list :link
  18778. (previous-single-property-change p 'face)
  18779. (next-single-property-change p 'face)) clist))
  18780. ((memq 'org-special-keyword faces)
  18781. (push (list :keyword
  18782. (previous-single-property-change p 'face)
  18783. (next-single-property-change p 'face)) clist))
  18784. ((org-at-target-p)
  18785. (push (org-point-in-group p 0 :target) clist)
  18786. (goto-char (1- (match-beginning 0)))
  18787. (if (looking-at org-radio-target-regexp)
  18788. (push (org-point-in-group p 0 :radio-target) clist))
  18789. (goto-char p))
  18790. ((setq o (car (delq nil
  18791. (mapcar
  18792. (lambda (x)
  18793. (if (memq x org-latex-fragment-image-overlays) x))
  18794. (overlays-at (point))))))
  18795. (push (list :latex-fragment
  18796. (overlay-start o) (overlay-end o)) clist)
  18797. (push (list :latex-preview
  18798. (overlay-start o) (overlay-end o)) clist))
  18799. ((org-inside-LaTeX-fragment-p)
  18800. ;; FIXME: positions wrong.
  18801. (push (list :latex-fragment (point) (point)) clist)))
  18802. (setq clist (nreverse (delq nil clist)))
  18803. clist))
  18804. ;; FIXME: Compare with at-regexp-p Do we need both?
  18805. (defun org-in-regexp (re &optional nlines visually)
  18806. "Check if point is inside a match of regexp.
  18807. Normally only the current line is checked, but you can include NLINES extra
  18808. lines both before and after point into the search.
  18809. If VISUALLY is set, require that the cursor is not after the match but
  18810. really on, so that the block visually is on the match."
  18811. (catch 'exit
  18812. (let ((pos (point))
  18813. (eol (point-at-eol (+ 1 (or nlines 0))))
  18814. (inc (if visually 1 0)))
  18815. (save-excursion
  18816. (beginning-of-line (- 1 (or nlines 0)))
  18817. (while (re-search-forward re eol t)
  18818. (if (and (<= (match-beginning 0) pos)
  18819. (>= (+ inc (match-end 0)) pos))
  18820. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18821. (defun org-at-regexp-p (regexp)
  18822. "Is point inside a match of REGEXP in the current line?"
  18823. (catch 'exit
  18824. (save-excursion
  18825. (let ((pos (point)) (end (point-at-eol)))
  18826. (beginning-of-line 1)
  18827. (while (re-search-forward regexp end t)
  18828. (if (and (<= (match-beginning 0) pos)
  18829. (>= (match-end 0) pos))
  18830. (throw 'exit t)))
  18831. nil))))
  18832. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18833. "Non-nil when point is between matches of START-RE and END-RE.
  18834. Also return a non-nil value when point is on one of the matches.
  18835. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18836. buffer positions. Default values are the positions of headlines
  18837. surrounding the point.
  18838. The functions returns a cons cell whose car (resp. cdr) is the
  18839. position before START-RE (resp. after END-RE)."
  18840. (save-match-data
  18841. (let ((pos (point))
  18842. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18843. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18844. beg end)
  18845. (save-excursion
  18846. ;; Point is on a block when on START-RE or if START-RE can be
  18847. ;; found before it...
  18848. (and (or (org-at-regexp-p start-re)
  18849. (re-search-backward start-re limit-up t))
  18850. (setq beg (match-beginning 0))
  18851. ;; ... and END-RE after it...
  18852. (goto-char (match-end 0))
  18853. (re-search-forward end-re limit-down t)
  18854. (> (setq end (match-end 0)) pos)
  18855. ;; ... without another START-RE in-between.
  18856. (goto-char (match-beginning 0))
  18857. (not (re-search-backward start-re (1+ beg) t))
  18858. ;; Return value.
  18859. (cons beg end))))))
  18860. (defun org-in-block-p (names)
  18861. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18862. NAMES is a list of strings containing names of blocks.
  18863. Return first block name matched, or nil. Beware that in case of
  18864. nested blocks, the returned name may not belong to the closest
  18865. block from point."
  18866. (save-match-data
  18867. (catch 'exit
  18868. (let ((case-fold-search t)
  18869. (lim-up (save-excursion (outline-previous-heading)))
  18870. (lim-down (save-excursion (outline-next-heading))))
  18871. (mapc (lambda (name)
  18872. (let ((n (regexp-quote name)))
  18873. (when (org-between-regexps-p
  18874. (concat "^[ \t]*#\\+begin_" n)
  18875. (concat "^[ \t]*#\\+end_" n)
  18876. lim-up lim-down)
  18877. (throw 'exit n))))
  18878. names))
  18879. nil)))
  18880. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18881. "Call `multi-occur' with buffers for all agenda files."
  18882. (interactive "sOrg-files matching: \np")
  18883. (let* ((files (org-agenda-files))
  18884. (tnames (mapcar 'file-truename files))
  18885. (extra org-agenda-text-search-extra-files)
  18886. f)
  18887. (when (eq (car extra) 'agenda-archives)
  18888. (setq extra (cdr extra))
  18889. (setq files (org-add-archive-files files)))
  18890. (while (setq f (pop extra))
  18891. (unless (member (file-truename f) tnames)
  18892. (add-to-list 'files f 'append)
  18893. (add-to-list 'tnames (file-truename f) 'append)))
  18894. (multi-occur
  18895. (mapcar (lambda (x)
  18896. (with-current-buffer
  18897. (or (get-file-buffer x) (find-file-noselect x))
  18898. (widen)
  18899. (current-buffer)))
  18900. files)
  18901. regexp)))
  18902. (if (boundp 'occur-mode-find-occurrence-hook)
  18903. ;; Emacs 23
  18904. (add-hook 'occur-mode-find-occurrence-hook
  18905. (lambda ()
  18906. (when (derived-mode-p 'org-mode)
  18907. (org-reveal))))
  18908. ;; Emacs 22
  18909. (defadvice occur-mode-goto-occurrence
  18910. (after org-occur-reveal activate)
  18911. (and (derived-mode-p 'org-mode) (org-reveal)))
  18912. (defadvice occur-mode-goto-occurrence-other-window
  18913. (after org-occur-reveal activate)
  18914. (and (derived-mode-p 'org-mode) (org-reveal)))
  18915. (defadvice occur-mode-display-occurrence
  18916. (after org-occur-reveal activate)
  18917. (when (derived-mode-p 'org-mode)
  18918. (let ((pos (occur-mode-find-occurrence)))
  18919. (with-current-buffer (marker-buffer pos)
  18920. (save-excursion
  18921. (goto-char pos)
  18922. (org-reveal)))))))
  18923. (defun org-occur-link-in-agenda-files ()
  18924. "Create a link and search for it in the agendas.
  18925. The link is not stored in `org-stored-links', it is just created
  18926. for the search purpose."
  18927. (interactive)
  18928. (let ((link (condition-case nil
  18929. (org-store-link nil)
  18930. (error "Unable to create a link to here"))))
  18931. (org-occur-in-agenda-files (regexp-quote link))))
  18932. (defun org-uniquify (list)
  18933. "Remove duplicate elements from LIST."
  18934. (let (res)
  18935. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18936. res))
  18937. (defun org-delete-all (elts list)
  18938. "Remove all elements in ELTS from LIST."
  18939. (while elts
  18940. (setq list (delete (pop elts) list)))
  18941. list)
  18942. (defun org-count (cl-item cl-seq)
  18943. "Count the number of occurrences of ITEM in SEQ.
  18944. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18945. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18946. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18947. (while (< cl-start cl-end)
  18948. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18949. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18950. (setq cl-start (1+ cl-start)))
  18951. cl-count))
  18952. (defun org-remove-if (predicate seq)
  18953. "Remove everything from SEQ that fulfills PREDICATE."
  18954. (let (res e)
  18955. (while seq
  18956. (setq e (pop seq))
  18957. (if (not (funcall predicate e)) (push e res)))
  18958. (nreverse res)))
  18959. (defun org-remove-if-not (predicate seq)
  18960. "Remove everything from SEQ that does not fulfill PREDICATE."
  18961. (let (res e)
  18962. (while seq
  18963. (setq e (pop seq))
  18964. (if (funcall predicate e) (push e res)))
  18965. (nreverse res)))
  18966. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18967. "Reduce two-argument FUNCTION across SEQ.
  18968. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18969. \":initial-value\" removed."
  18970. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18971. (cadr (memq :initial-value cl-keys)))
  18972. (cl-seq (pop cl-seq))
  18973. (t (funcall cl-func)))))
  18974. (while cl-seq
  18975. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18976. cl-accum))
  18977. (defun org-back-over-empty-lines ()
  18978. "Move backwards over whitespace, to the beginning of the first empty line.
  18979. Returns the number of empty lines passed."
  18980. (let ((pos (point)))
  18981. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18982. (skip-chars-backward " \t\n\r")
  18983. (unless (eobp)
  18984. (forward-line -1)))
  18985. (beginning-of-line 2)
  18986. (goto-char (min (point) pos))
  18987. (count-lines (point) pos)))
  18988. (defun org-skip-whitespace ()
  18989. (skip-chars-forward " \t\n\r"))
  18990. (defun org-point-in-group (point group &optional context)
  18991. "Check if POINT is in match-group GROUP.
  18992. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18993. match. If the match group does not exist or point is not inside it,
  18994. return nil."
  18995. (and (match-beginning group)
  18996. (>= point (match-beginning group))
  18997. (<= point (match-end group))
  18998. (if context
  18999. (list context (match-beginning group) (match-end group))
  19000. t)))
  19001. (defun org-switch-to-buffer-other-window (&rest args)
  19002. "Switch to buffer in a second window on the current frame.
  19003. In particular, do not allow pop-up frames.
  19004. Returns the newly created buffer."
  19005. (org-no-popups
  19006. (apply 'switch-to-buffer-other-window args)))
  19007. (defun org-combine-plists (&rest plists)
  19008. "Create a single property list from all plists in PLISTS.
  19009. The process starts by copying the first list, and then setting properties
  19010. from the other lists. Settings in the last list are the most significant
  19011. ones and overrule settings in the other lists."
  19012. (let ((rtn (copy-sequence (pop plists)))
  19013. p v ls)
  19014. (while plists
  19015. (setq ls (pop plists))
  19016. (while ls
  19017. (setq p (pop ls) v (pop ls))
  19018. (setq rtn (plist-put rtn p v))))
  19019. rtn))
  19020. (defun org-replace-escapes (string table)
  19021. "Replace %-escapes in STRING with values in TABLE.
  19022. TABLE is an association list with keys like \"%a\" and string values.
  19023. The sequences in STRING may contain normal field width and padding information,
  19024. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19025. so values can contain further %-escapes if they are define later in TABLE."
  19026. (let ((tbl (copy-alist table))
  19027. (case-fold-search nil)
  19028. (pchg 0)
  19029. e re rpl)
  19030. (while (setq e (pop tbl))
  19031. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19032. (when (and (cdr e) (string-match re (cdr e)))
  19033. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19034. (safe "SREF"))
  19035. (add-text-properties 0 3 (list 'sref sref) safe)
  19036. (setcdr e (replace-match safe t t (cdr e)))))
  19037. (while (string-match re string)
  19038. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19039. (cdr e)))
  19040. (setq string (replace-match rpl t t string))))
  19041. (while (setq pchg (next-property-change pchg string))
  19042. (let ((sref (get-text-property pchg 'sref string)))
  19043. (when (and sref (string-match "SREF" string pchg))
  19044. (setq string (replace-match sref t t string)))))
  19045. string))
  19046. (defun org-sublist (list start end)
  19047. "Return a section of LIST, from START to END.
  19048. Counting starts at 1."
  19049. (let (rtn (c start))
  19050. (setq list (nthcdr (1- start) list))
  19051. (while (and list (<= c end))
  19052. (push (pop list) rtn)
  19053. (setq c (1+ c)))
  19054. (nreverse rtn)))
  19055. (defun org-find-base-buffer-visiting (file)
  19056. "Like `find-buffer-visiting' but always return the base buffer and
  19057. not an indirect buffer."
  19058. (let ((buf (or (get-file-buffer file)
  19059. (find-buffer-visiting file))))
  19060. (if buf
  19061. (or (buffer-base-buffer buf) buf)
  19062. nil)))
  19063. (defun org-image-file-name-regexp (&optional extensions)
  19064. "Return regexp matching the file names of images.
  19065. If EXTENSIONS is given, only match these."
  19066. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19067. (image-file-name-regexp)
  19068. (let ((image-file-name-extensions
  19069. (or extensions
  19070. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19071. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19072. (concat "\\."
  19073. (regexp-opt (nconc (mapcar 'upcase
  19074. image-file-name-extensions)
  19075. image-file-name-extensions)
  19076. t)
  19077. "\\'"))))
  19078. (defun org-file-image-p (file &optional extensions)
  19079. "Return non-nil if FILE is an image."
  19080. (save-match-data
  19081. (string-match (org-image-file-name-regexp extensions) file)))
  19082. (defun org-get-cursor-date ()
  19083. "Return the date at cursor in as a time.
  19084. This works in the calendar and in the agenda, anywhere else it just
  19085. returns the current time."
  19086. (let (date day defd)
  19087. (cond
  19088. ((eq major-mode 'calendar-mode)
  19089. (setq date (calendar-cursor-to-date)
  19090. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19091. ((eq major-mode 'org-agenda-mode)
  19092. (setq day (get-text-property (point) 'day))
  19093. (if day
  19094. (setq date (calendar-gregorian-from-absolute day)
  19095. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  19096. (nth 2 date))))))
  19097. (or defd (current-time))))
  19098. (defun org-mark-subtree (&optional up)
  19099. "Mark the current subtree.
  19100. This puts point at the start of the current subtree, and mark at
  19101. the end. If a numeric prefix UP is given, move up into the
  19102. hierarchy of headlines by UP levels before marking the subtree."
  19103. (interactive "P")
  19104. (org-with-limited-levels
  19105. (cond ((org-at-heading-p) (beginning-of-line))
  19106. ((org-before-first-heading-p) (error "Not in a subtree"))
  19107. (t (outline-previous-visible-heading 1))))
  19108. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19109. (if (org-called-interactively-p 'any)
  19110. (call-interactively 'org-mark-element)
  19111. (org-mark-element)))
  19112. ;;; Indentation
  19113. (defun org-indent-line ()
  19114. "Indent line depending on context."
  19115. (interactive)
  19116. (let* ((pos (point))
  19117. (itemp (org-at-item-p))
  19118. (case-fold-search t)
  19119. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19120. (inline-task-p (and (featurep 'org-inlinetask)
  19121. (org-inlinetask-in-task-p)))
  19122. (inline-re (and inline-task-p
  19123. (org-inlinetask-outline-regexp)))
  19124. column)
  19125. (if (and orgstruct-is-++ (eq pos (point)))
  19126. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19127. (indent-according-to-mode))
  19128. (beginning-of-line 1)
  19129. (cond
  19130. ;; Headings
  19131. ((looking-at org-outline-regexp) (setq column 0))
  19132. ;; Included files
  19133. ((looking-at "#\\+include:") (setq column 0))
  19134. ;; Footnote definition
  19135. ((looking-at org-footnote-definition-re) (setq column 0))
  19136. ;; Literal examples
  19137. ((looking-at "[ \t]*:\\( \\|$\\)")
  19138. (setq column (org-get-indentation))) ; do nothing
  19139. ;; Lists
  19140. ((ignore-errors (goto-char (org-in-item-p)))
  19141. (setq column (if itemp
  19142. (org-get-indentation)
  19143. (org-list-item-body-column (point))))
  19144. (goto-char pos))
  19145. ;; Drawers
  19146. ((and (looking-at "[ \t]*:END:")
  19147. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19148. (save-excursion
  19149. (goto-char (1- (match-beginning 1)))
  19150. (setq column (current-column))))
  19151. ;; Special blocks
  19152. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19153. (save-excursion
  19154. (re-search-backward
  19155. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19156. (setq column (org-get-indentation (match-string 0))))
  19157. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19158. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19159. (save-excursion
  19160. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19161. (setq column
  19162. (cond ((equal (downcase (match-string 1)) "src")
  19163. ;; src blocks: let `org-edit-src-exit' handle them
  19164. (org-get-indentation))
  19165. ((equal (downcase (match-string 1)) "example")
  19166. (max (org-get-indentation)
  19167. (org-get-indentation (match-string 0))))
  19168. (t
  19169. (org-get-indentation (match-string 0))))))
  19170. ;; This line has nothing special, look at the previous relevant
  19171. ;; line to compute indentation
  19172. (t
  19173. (beginning-of-line 0)
  19174. (while (and (not (bobp))
  19175. (not (looking-at org-table-line-regexp))
  19176. (not (looking-at org-drawer-regexp))
  19177. ;; When point started in an inline task, do not move
  19178. ;; above task starting line.
  19179. (not (and inline-task-p (looking-at inline-re)))
  19180. ;; Skip drawers, blocks, empty lines, verbatim,
  19181. ;; comments, tables, footnotes definitions, lists,
  19182. ;; inline tasks.
  19183. (or (and (looking-at "[ \t]*:END:")
  19184. (re-search-backward org-drawer-regexp nil t))
  19185. (and (looking-at "[ \t]*#\\+end_")
  19186. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19187. (looking-at "[ \t]*[\n:#|]")
  19188. (looking-at org-footnote-definition-re)
  19189. (and (ignore-errors (goto-char (org-in-item-p)))
  19190. (goto-char
  19191. (org-list-get-top-point (org-list-struct))))
  19192. (and (not inline-task-p)
  19193. (featurep 'org-inlinetask)
  19194. (org-inlinetask-in-task-p)
  19195. (or (org-inlinetask-goto-beginning) t))))
  19196. (beginning-of-line 0))
  19197. (cond
  19198. ;; There was an heading above.
  19199. ((looking-at "\\*+[ \t]+")
  19200. (if (not org-adapt-indentation)
  19201. (setq column 0)
  19202. (goto-char (match-end 0))
  19203. (setq column (current-column))))
  19204. ;; A drawer had started and is unfinished
  19205. ((looking-at org-drawer-regexp)
  19206. (goto-char (1- (match-beginning 1)))
  19207. (setq column (current-column)))
  19208. ;; Else, nothing noticeable found: get indentation and go on.
  19209. (t (setq column (org-get-indentation))))))
  19210. ;; Now apply indentation and move cursor accordingly
  19211. (goto-char pos)
  19212. (if (<= (current-column) (current-indentation))
  19213. (org-indent-line-to column)
  19214. (save-excursion (org-indent-line-to column)))
  19215. ;; Special polishing for properties, see `org-property-format'
  19216. (setq column (current-column))
  19217. (beginning-of-line 1)
  19218. (if (looking-at
  19219. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19220. (replace-match (concat (match-string 1)
  19221. (format org-property-format
  19222. (match-string 2) (match-string 3)))
  19223. t t))
  19224. (org-move-to-column column))))
  19225. (defun org-indent-drawer ()
  19226. "Indent the drawer at point."
  19227. (interactive)
  19228. (let ((p (point))
  19229. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19230. (match-end 0)))
  19231. (folded
  19232. (save-excursion
  19233. (end-of-line)
  19234. (when (overlays-at (point))
  19235. (member 'invisible (overlay-properties
  19236. (car (overlays-at (point)))))))))
  19237. (when folded (org-cycle))
  19238. (indent-for-tab-command)
  19239. (while (and (move-beginning-of-line 2) (< (point) e))
  19240. (indent-for-tab-command))
  19241. (goto-char p)
  19242. (when folded (org-cycle)))
  19243. (message "Drawer at point indented"))
  19244. (defun org-indent-block ()
  19245. "Indent the block at point."
  19246. (interactive)
  19247. (let ((p (point))
  19248. (case-fold-search t)
  19249. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19250. (match-end 0)))
  19251. (folded
  19252. (save-excursion
  19253. (end-of-line)
  19254. (when (overlays-at (point))
  19255. (member 'invisible (overlay-properties
  19256. (car (overlays-at (point)))))))))
  19257. (when folded (org-cycle))
  19258. (indent-for-tab-command)
  19259. (while (and (move-beginning-of-line 2) (< (point) e))
  19260. (indent-for-tab-command))
  19261. (goto-char p)
  19262. (when folded (org-cycle)))
  19263. (message "Block at point indented"))
  19264. (defun org-indent-region (start end)
  19265. "Indent region."
  19266. (interactive "r")
  19267. (save-excursion
  19268. (let ((line-end (org-current-line end)))
  19269. (goto-char start)
  19270. (while (< (org-current-line) line-end)
  19271. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19272. (t (call-interactively 'org-indent-line)))
  19273. (move-beginning-of-line 2)))))
  19274. ;;; Filling
  19275. ;; We use our own fill-paragraph and auto-fill functions.
  19276. ;; `org-fill-paragraph' relies on adaptive filling and context
  19277. ;; checking. Appropriate `fill-prefix' is computed with
  19278. ;; `org-adaptive-fill-function'.
  19279. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19280. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19281. ;; `org-adaptive-fill-function' value. Internally,
  19282. ;; `org-comment-line-break-function' breaks the line.
  19283. ;; `org-setup-filling' installs filling and auto-filling related
  19284. ;; variables during `org-mode' initialization.
  19285. (defun org-setup-filling ()
  19286. (interactive)
  19287. ;; Prevent auto-fill from inserting unwanted new items.
  19288. (when (boundp 'fill-nobreak-predicate)
  19289. (org-set-local
  19290. 'fill-nobreak-predicate
  19291. (org-uniquify
  19292. (append fill-nobreak-predicate
  19293. '(org-fill-paragraph-separate-nobreak-p
  19294. org-fill-line-break-nobreak-p
  19295. org-fill-paragraph-with-timestamp-nobreak-p)))))
  19296. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19297. (org-set-local 'auto-fill-inhibit-regexp nil)
  19298. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19299. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19300. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19301. (defvar org-element-paragraph-separate) ; org-element.el
  19302. (defun org-fill-paragraph-separate-nobreak-p ()
  19303. "Non-nil when a line break at point would insert a new item."
  19304. (looking-at (substring org-element-paragraph-separate 1)))
  19305. (defun org-fill-line-break-nobreak-p ()
  19306. "Non-nil when a line break at point would create an Org line break."
  19307. (save-excursion
  19308. (skip-chars-backward "[ \t]")
  19309. (skip-chars-backward "\\\\")
  19310. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19311. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  19312. "Non-nil when a line break at point would insert a new item."
  19313. (and (org-at-timestamp-p t)
  19314. (not (looking-at org-ts-regexp-both))))
  19315. (declare-function message-in-body-p "message" ())
  19316. (defvar org-element--affiliated-re) ; From org-element.el
  19317. (defvar orgtbl-line-start-regexp) ; From org-table.el
  19318. (defun org-adaptive-fill-function ()
  19319. "Compute a fill prefix for the current line.
  19320. Return fill prefix, as a string, or nil if current line isn't
  19321. meant to be filled."
  19322. (let (prefix)
  19323. (catch 'exit
  19324. (when (derived-mode-p 'message-mode)
  19325. (save-excursion
  19326. (beginning-of-line)
  19327. (cond ((or (not (message-in-body-p))
  19328. (looking-at orgtbl-line-start-regexp))
  19329. (throw 'exit nil))
  19330. ((looking-at message-cite-prefix-regexp)
  19331. (throw 'exit (match-string-no-properties 0)))
  19332. ((looking-at org-outline-regexp)
  19333. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  19334. (org-with-wide-buffer
  19335. (let* ((p (line-beginning-position))
  19336. (element (save-excursion (beginning-of-line) (org-element-at-point)))
  19337. (type (org-element-type element))
  19338. (post-affiliated
  19339. (save-excursion
  19340. (goto-char (org-element-property :begin element))
  19341. (while (looking-at org-element--affiliated-re) (forward-line))
  19342. (point))))
  19343. (unless (< p post-affiliated)
  19344. (case type
  19345. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19346. (footnote-definition "")
  19347. ((item plain-list)
  19348. (make-string (org-list-item-body-column post-affiliated) ? ))
  19349. (paragraph
  19350. ;; Fill prefix is usually the same as the current line,
  19351. ;; except if the paragraph is at the beginning of an item.
  19352. (let ((parent (org-element-property :parent element)))
  19353. (cond ((eq (org-element-type parent) 'item)
  19354. (make-string (org-list-item-body-column
  19355. (org-element-property :begin parent))
  19356. ? ))
  19357. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19358. (match-string 0))
  19359. (t ""))))
  19360. (comment-block
  19361. ;; Only fill contents if P is within block boundaries.
  19362. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19363. (forward-line)
  19364. (point)))
  19365. (cend (save-excursion
  19366. (goto-char (org-element-property :end element))
  19367. (skip-chars-backward " \r\t\n")
  19368. (line-beginning-position))))
  19369. (when (and (>= p cbeg) (< p cend))
  19370. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19371. (match-string 0)
  19372. "")))))))))))
  19373. (declare-function message-goto-body "message" ())
  19374. (defvar message-cite-prefix-regexp) ; From message.el
  19375. (defvar org-element-all-objects) ; From org-element.el
  19376. (defun org-fill-paragraph (&optional justify)
  19377. "Fill element at point, when applicable.
  19378. This function only applies to comment blocks, comments, example
  19379. blocks and paragraphs. Also, as a special case, re-align table
  19380. when point is at one.
  19381. If JUSTIFY is non-nil (interactively, with prefix argument),
  19382. justify as well. If `sentence-end-double-space' is non-nil, then
  19383. period followed by one space does not end a sentence, so don't
  19384. break a line there. The variable `fill-column' controls the
  19385. width for filling.
  19386. For convenience, when point is at a plain list, an item or
  19387. a footnote definition, try to fill the first paragraph within."
  19388. (interactive)
  19389. (if (and (derived-mode-p 'message-mode)
  19390. (or (not (message-in-body-p))
  19391. (save-excursion (move-beginning-of-line 1)
  19392. (looking-at message-cite-prefix-regexp))))
  19393. ;; First ensure filling is correct in message-mode.
  19394. (let ((fill-paragraph-function
  19395. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19396. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19397. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19398. (paragraph-separate
  19399. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19400. (fill-paragraph nil))
  19401. (save-excursion
  19402. ;; Move to end of line in order to get the first paragraph
  19403. ;; within a plain list or a footnote definition.
  19404. (end-of-line)
  19405. (let ((element (org-element-at-point)))
  19406. ;; First check if point is in a blank line at the beginning of
  19407. ;; the buffer. In that case, ignore filling.
  19408. (if (< (point) (org-element-property :begin element)) t
  19409. (case (org-element-type element)
  19410. ;; Use major mode filling function is src blocks.
  19411. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  19412. ;; Align Org tables, leave table.el tables as-is.
  19413. (table-row (org-table-align) t)
  19414. (table
  19415. (when (eq (org-element-property :type element) 'org)
  19416. (org-table-align))
  19417. t)
  19418. (paragraph
  19419. ;; Paragraphs may contain `line-break' type objects.
  19420. (let ((beg (max (point-min)
  19421. (org-element-property :contents-begin element)))
  19422. (end (min (point-max)
  19423. (org-element-property :contents-end element))))
  19424. ;; Do nothing if point is at an affiliated keyword.
  19425. (if (< (point) beg) t
  19426. (when (derived-mode-p 'message-mode)
  19427. ;; In `message-mode', do not fill following
  19428. ;; citation in current paragraph nor text before
  19429. ;; message body.
  19430. (let ((body-start (save-excursion (message-goto-body))))
  19431. (when body-start (setq beg (max body-start beg))))
  19432. (when (save-excursion
  19433. (re-search-forward
  19434. (concat "^" message-cite-prefix-regexp) end t))
  19435. (setq end (match-beginning 0))))
  19436. ;; Fill paragraph, taking line breaks into
  19437. ;; consideration. For that, slice the paragraph
  19438. ;; using line breaks as separators, and fill the
  19439. ;; parts in reverse order to avoid messing with
  19440. ;; markers.
  19441. (save-excursion
  19442. (goto-char end)
  19443. (mapc
  19444. (lambda (pos)
  19445. (fill-region-as-paragraph pos (point) justify)
  19446. (goto-char pos))
  19447. ;; Find the list of ending positions for line
  19448. ;; breaks in the current paragraph. Add paragraph
  19449. ;; beginning to include first slice.
  19450. (nreverse
  19451. (cons
  19452. beg
  19453. (org-element-map
  19454. (org-element--parse-objects
  19455. beg end nil org-element-all-objects)
  19456. 'line-break
  19457. (lambda (lb) (org-element-property :end lb)))))))
  19458. t)))
  19459. ;; Contents of `comment-block' type elements should be
  19460. ;; filled as plain text, but only if point is within block
  19461. ;; markers.
  19462. (comment-block
  19463. (let* ((case-fold-search t)
  19464. (beg (save-excursion
  19465. (goto-char (org-element-property :begin element))
  19466. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  19467. (forward-line)
  19468. (point)))
  19469. (end (save-excursion
  19470. (goto-char (org-element-property :end element))
  19471. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  19472. (line-beginning-position))))
  19473. (when (and (>= (point) beg) (< (point) end))
  19474. (fill-region-as-paragraph
  19475. (save-excursion
  19476. (end-of-line)
  19477. (re-search-backward "^[ \t]*$" beg 'move)
  19478. (line-beginning-position))
  19479. (save-excursion
  19480. (beginning-of-line)
  19481. (re-search-forward "^[ \t]*$" end 'move)
  19482. (line-beginning-position))
  19483. justify)))
  19484. t)
  19485. ;; Fill comments.
  19486. (comment (fill-comment-paragraph justify))
  19487. ;; Ignore every other element.
  19488. (otherwise t)))))))
  19489. (defun org-auto-fill-function ()
  19490. "Auto-fill function."
  19491. ;; Check if auto-filling is meaningful.
  19492. (let ((fc (current-fill-column)))
  19493. (when (and fc (> (current-column) fc))
  19494. (let* ((fill-prefix (org-adaptive-fill-function))
  19495. ;; Enforce empty fill prefix, if required. Otherwise, it
  19496. ;; will be computed again.
  19497. (adaptive-fill-mode (not (equal fill-prefix ""))))
  19498. (when fill-prefix (do-auto-fill))))))
  19499. (defun org-comment-line-break-function (&optional soft)
  19500. "Break line at point and indent, continuing comment if within one.
  19501. The inserted newline is marked hard if variable
  19502. `use-hard-newlines' is true, unless optional argument SOFT is
  19503. non-nil."
  19504. (if soft (insert-and-inherit ?\n) (newline 1))
  19505. (save-excursion (forward-char -1) (delete-horizontal-space))
  19506. (delete-horizontal-space)
  19507. (indent-to-left-margin)
  19508. (insert-before-markers-and-inherit fill-prefix))
  19509. ;;; Comments
  19510. ;; Org comments syntax is quite complex. It requires the entire line
  19511. ;; to be just a comment. Also, even with the right syntax at the
  19512. ;; beginning of line, some some elements (i.e. verse-block or
  19513. ;; example-block) don't accept comments. Usual Emacs comment commands
  19514. ;; cannot cope with those requirements. Therefore, Org replaces them.
  19515. ;; Org still relies on `comment-dwim', but cannot trust
  19516. ;; `comment-only-p'. So, `comment-region-function' and
  19517. ;; `uncomment-region-function' both point
  19518. ;; to`org-comment-or-uncomment-region'. Eventually,
  19519. ;; `org-insert-comment' takes care of insertion of comments at the
  19520. ;; beginning of line.
  19521. ;; `org-setup-comments-handling' install comments related variables
  19522. ;; during `org-mode' initialization.
  19523. (defun org-setup-comments-handling ()
  19524. (interactive)
  19525. (org-set-local 'comment-use-syntax nil)
  19526. (org-set-local 'comment-start "# ")
  19527. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  19528. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  19529. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  19530. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  19531. (defun org-insert-comment ()
  19532. "Insert an empty comment above current line.
  19533. If the line is empty, insert comment at its beginning."
  19534. (beginning-of-line)
  19535. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  19536. (org-indent-line)
  19537. (insert "# "))
  19538. (defvar comment-empty-lines) ; From newcomment.el.
  19539. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  19540. "Comment or uncomment each non-blank line in the region.
  19541. Uncomment each non-blank line between BEG and END if it only
  19542. contains commented lines. Otherwise, comment them."
  19543. (save-restriction
  19544. ;; Restrict region
  19545. (narrow-to-region (save-excursion (goto-char beg)
  19546. (skip-chars-forward " \r\t\n" end)
  19547. (line-beginning-position))
  19548. (save-excursion (goto-char end)
  19549. (skip-chars-backward " \r\t\n" beg)
  19550. (line-end-position)))
  19551. (let ((uncommentp
  19552. ;; UNCOMMENTP is non-nil when every non blank line between
  19553. ;; BEG and END is a comment.
  19554. (save-excursion
  19555. (goto-char (point-min))
  19556. (while (and (not (eobp))
  19557. (let ((element (org-element-at-point)))
  19558. (and (eq (org-element-type element) 'comment)
  19559. (goto-char (min (point-max)
  19560. (org-element-property
  19561. :end element)))))))
  19562. (eobp))))
  19563. (if uncommentp
  19564. ;; Only blank lines and comments in region: uncomment it.
  19565. (save-excursion
  19566. (goto-char (point-min))
  19567. (while (not (eobp))
  19568. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  19569. (replace-match "" nil nil nil 1))
  19570. (forward-line)))
  19571. ;; Comment each line in region.
  19572. (let ((min-indent (point-max)))
  19573. ;; First find the minimum indentation across all lines.
  19574. (save-excursion
  19575. (goto-char (point-min))
  19576. (while (and (not (eobp)) (not (zerop min-indent)))
  19577. (unless (looking-at "[ \t]*$")
  19578. (setq min-indent (min min-indent (current-indentation))))
  19579. (forward-line)))
  19580. ;; Then loop over all lines.
  19581. (save-excursion
  19582. (goto-char (point-min))
  19583. (while (not (eobp))
  19584. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  19585. (org-move-to-column min-indent t)
  19586. (insert comment-start))
  19587. (forward-line))))))))
  19588. ;;; Other stuff.
  19589. (defun org-toggle-fixed-width-section (arg)
  19590. "Toggle the fixed-width export.
  19591. If there is no active region, the QUOTE keyword at the current headline is
  19592. inserted or removed. When present, it causes the text between this headline
  19593. and the next to be exported as fixed-width text, and unmodified.
  19594. If there is an active region, this command adds or removes a colon as the
  19595. first character of this line. If the first character of a line is a colon,
  19596. this line is also exported in fixed-width font."
  19597. (interactive "P")
  19598. (let* ((cc 0)
  19599. (regionp (org-region-active-p))
  19600. (beg (if regionp (region-beginning) (point)))
  19601. (end (if regionp (region-end)))
  19602. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19603. (case-fold-search nil)
  19604. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  19605. off)
  19606. (if regionp
  19607. (save-excursion
  19608. (goto-char beg)
  19609. (setq cc (current-column))
  19610. (beginning-of-line 1)
  19611. (setq off (looking-at re))
  19612. (while (> nlines 0)
  19613. (setq nlines (1- nlines))
  19614. (beginning-of-line 1)
  19615. (cond
  19616. (arg
  19617. (org-move-to-column cc t)
  19618. (insert ": \n")
  19619. (forward-line -1))
  19620. ((and off (looking-at re))
  19621. (replace-match "" t t nil 1))
  19622. ((not off) (org-move-to-column cc t) (insert ": ")))
  19623. (forward-line 1)))
  19624. (save-excursion
  19625. (org-back-to-heading)
  19626. (cond
  19627. ((looking-at (format org-heading-keyword-regexp-format
  19628. org-quote-string))
  19629. (goto-char (match-end 1))
  19630. (looking-at (concat " +" org-quote-string))
  19631. (replace-match "" t t)
  19632. (when (eolp) (insert " ")))
  19633. ((looking-at org-outline-regexp)
  19634. (goto-char (match-end 0))
  19635. (insert org-quote-string " ")))))))
  19636. (defun org-reftex-citation ()
  19637. "Use reftex-citation to insert a citation into the buffer.
  19638. This looks for a line like
  19639. #+BIBLIOGRAPHY: foo plain option:-d
  19640. and derives from it that foo.bib is the bibliography file relevant
  19641. for this document. It then installs the necessary environment for RefTeX
  19642. to work in this buffer and calls `reftex-citation' to insert a citation
  19643. into the buffer.
  19644. Export of such citations to both LaTeX and HTML is handled by the contributed
  19645. package org-exp-bibtex by Taru Karttunen."
  19646. (interactive)
  19647. (let ((reftex-docstruct-symbol 'rds)
  19648. (reftex-cite-format "\\cite{%l}")
  19649. rds bib)
  19650. (save-excursion
  19651. (save-restriction
  19652. (widen)
  19653. (let ((case-fold-search t)
  19654. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  19655. (if (not (save-excursion
  19656. (or (re-search-forward re nil t)
  19657. (re-search-backward re nil t))))
  19658. (error "No bibliography defined in file")
  19659. (setq bib (concat (match-string 1) ".bib")
  19660. rds (list (list 'bib bib)))))))
  19661. (call-interactively 'reftex-citation)))
  19662. ;;;; Functions extending outline functionality
  19663. (defun org-beginning-of-line (&optional arg)
  19664. "Go to the beginning of the current line. If that is invisible, continue
  19665. to a visible line beginning. This makes the function of C-a more intuitive.
  19666. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19667. first attempt, and only move to after the tags when the cursor is already
  19668. beyond the end of the headline."
  19669. (interactive "P")
  19670. (let ((pos (point))
  19671. (special (if (consp org-special-ctrl-a/e)
  19672. (car org-special-ctrl-a/e)
  19673. org-special-ctrl-a/e))
  19674. refpos)
  19675. (if (org-bound-and-true-p visual-line-mode)
  19676. (beginning-of-visual-line 1)
  19677. (beginning-of-line 1))
  19678. (if (and arg (fboundp 'move-beginning-of-line))
  19679. (call-interactively 'move-beginning-of-line)
  19680. (if (bobp)
  19681. nil
  19682. (backward-char 1)
  19683. (if (org-truely-invisible-p)
  19684. (while (and (not (bobp)) (org-truely-invisible-p))
  19685. (backward-char 1)
  19686. (beginning-of-line 1))
  19687. (forward-char 1))))
  19688. (when special
  19689. (cond
  19690. ((and (looking-at org-complex-heading-regexp)
  19691. (= (char-after (match-end 1)) ?\ ))
  19692. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19693. (point-at-eol)))
  19694. (goto-char
  19695. (if (eq special t)
  19696. (cond ((> pos refpos) refpos)
  19697. ((= pos (point)) refpos)
  19698. (t (point)))
  19699. (cond ((> pos (point)) (point))
  19700. ((not (eq last-command this-command)) (point))
  19701. (t refpos)))))
  19702. ((org-at-item-p)
  19703. ;; Being at an item and not looking at an the item means point
  19704. ;; was previously moved to beginning of a visual line, which
  19705. ;; doesn't contain the item. Therefore, do nothing special,
  19706. ;; just stay here.
  19707. (when (looking-at org-list-full-item-re)
  19708. ;; Set special position at first white space character after
  19709. ;; bullet, and check-box, if any.
  19710. (let ((after-bullet
  19711. (let ((box (match-end 3)))
  19712. (if (not box) (match-end 1)
  19713. (let ((after (char-after box)))
  19714. (if (and after (= after ? )) (1+ box) box))))))
  19715. ;; Special case: Move point to special position when
  19716. ;; currently after it or at beginning of line.
  19717. (if (eq special t)
  19718. (when (or (> pos after-bullet) (= (point) pos))
  19719. (goto-char after-bullet))
  19720. ;; Reversed case: Move point to special position when
  19721. ;; point was already at beginning of line and command is
  19722. ;; repeated.
  19723. (when (and (= (point) pos) (eq last-command this-command))
  19724. (goto-char after-bullet))))))))
  19725. (org-no-warnings
  19726. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19727. (defun org-end-of-line (&optional arg)
  19728. "Go to the end of the line.
  19729. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  19730. tags on the first attempt, and only move to after the tags when
  19731. the cursor is already beyond the end of the headline."
  19732. (interactive "P")
  19733. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  19734. org-special-ctrl-a/e))
  19735. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  19736. 'end-of-visual-line)
  19737. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19738. (t 'end-of-line))))
  19739. (if (or (not special) arg) (call-interactively move-fun)
  19740. (let* ((element (save-excursion (beginning-of-line)
  19741. (org-element-at-point)))
  19742. (type (org-element-type element)))
  19743. (cond
  19744. ((memq type '(headline inlinetask))
  19745. (let ((pos (point)))
  19746. (beginning-of-line 1)
  19747. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19748. (if (eq special t)
  19749. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  19750. (goto-char (match-beginning 1))
  19751. (goto-char (match-end 0)))
  19752. (if (or (< pos (match-end 0))
  19753. (not (eq this-command last-command)))
  19754. (goto-char (match-end 0))
  19755. (goto-char (match-beginning 1))))
  19756. (call-interactively move-fun))))
  19757. ((org-element-property :hiddenp element)
  19758. ;; If element is hidden, `move-end-of-line' would put point
  19759. ;; after it. Use `end-of-line' to stay on current line.
  19760. (call-interactively 'end-of-line))
  19761. (t (call-interactively move-fun)))))
  19762. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19763. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19764. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19765. (defun org-backward-sentence (&optional arg)
  19766. "Go to beginning of sentence, or beginning of table field.
  19767. This will call `backward-sentence' or `org-table-beginning-of-field',
  19768. depending on context."
  19769. (interactive "P")
  19770. (cond
  19771. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19772. (t (call-interactively 'backward-sentence))))
  19773. (defun org-forward-sentence (&optional arg)
  19774. "Go to end of sentence, or end of table field.
  19775. This will call `forward-sentence' or `org-table-end-of-field',
  19776. depending on context."
  19777. (interactive "P")
  19778. (cond
  19779. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19780. (t (call-interactively 'forward-sentence))))
  19781. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19782. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19783. (defun org-kill-line (&optional arg)
  19784. "Kill line, to tags or end of line."
  19785. (interactive "P")
  19786. (cond
  19787. ((or (not org-special-ctrl-k)
  19788. (bolp)
  19789. (not (org-at-heading-p)))
  19790. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19791. org-ctrl-k-protect-subtree)
  19792. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19793. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19794. (error "C-k aborted - would kill hidden subtree")))
  19795. (call-interactively
  19796. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  19797. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19798. (kill-region (point) (match-beginning 1))
  19799. (org-set-tags nil t))
  19800. (t (kill-region (point) (point-at-eol)))))
  19801. (define-key org-mode-map "\C-k" 'org-kill-line)
  19802. (defun org-yank (&optional arg)
  19803. "Yank. If the kill is a subtree, treat it specially.
  19804. This command will look at the current kill and check if is a single
  19805. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19806. cursor is at the beginning of a line or after the stars of a currently
  19807. empty headline, then the yank is handled specially. How exactly depends
  19808. on the value of the following variables, both set by default.
  19809. org-yank-folded-subtrees
  19810. When set, the subtree(s) will be folded after insertion, but only
  19811. if doing so would now swallow text after the yanked text.
  19812. org-yank-adjusted-subtrees
  19813. When set, the subtree will be promoted or demoted in order to
  19814. fit into the local outline tree structure, which means that the level
  19815. will be adjusted so that it becomes the smaller one of the two
  19816. *visible* surrounding headings.
  19817. Any prefix to this command will cause `yank' to be called directly with
  19818. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19819. will just
  19820. plainly yank the text as it is.
  19821. \[1] The test checks if the first non-white line is a heading
  19822. and if there are no other headings with fewer stars."
  19823. (interactive "P")
  19824. (org-yank-generic 'yank arg))
  19825. (defun org-yank-generic (command arg)
  19826. "Perform some yank-like command.
  19827. This function implements the behavior described in the `org-yank'
  19828. documentation. However, it has been generalized to work for any
  19829. interactive command with similar behavior."
  19830. ;; pretend to be command COMMAND
  19831. (setq this-command command)
  19832. (if arg
  19833. (call-interactively command)
  19834. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19835. (and (org-kill-is-subtree-p)
  19836. (or (bolp)
  19837. (and (looking-at "[ \t]*$")
  19838. (string-match
  19839. "\\`\\*+\\'"
  19840. (buffer-substring (point-at-bol) (point)))))))
  19841. swallowp)
  19842. (cond
  19843. ((and subtreep org-yank-folded-subtrees)
  19844. (let ((beg (point))
  19845. end)
  19846. (if (and subtreep org-yank-adjusted-subtrees)
  19847. (org-paste-subtree nil nil 'for-yank)
  19848. (call-interactively command))
  19849. (setq end (point))
  19850. (goto-char beg)
  19851. (when (and (bolp) subtreep
  19852. (not (setq swallowp
  19853. (org-yank-folding-would-swallow-text beg end))))
  19854. (org-with-limited-levels
  19855. (or (looking-at org-outline-regexp)
  19856. (re-search-forward org-outline-regexp-bol end t))
  19857. (while (and (< (point) end) (looking-at org-outline-regexp))
  19858. (hide-subtree)
  19859. (org-cycle-show-empty-lines 'folded)
  19860. (condition-case nil
  19861. (outline-forward-same-level 1)
  19862. (error (goto-char end))))))
  19863. (when swallowp
  19864. (message
  19865. "Inserted text not folded because that would swallow text"))
  19866. (goto-char end)
  19867. (skip-chars-forward " \t\n\r")
  19868. (beginning-of-line 1)
  19869. (push-mark beg 'nomsg)))
  19870. ((and subtreep org-yank-adjusted-subtrees)
  19871. (let ((beg (point-at-bol)))
  19872. (org-paste-subtree nil nil 'for-yank)
  19873. (push-mark beg 'nomsg)))
  19874. (t
  19875. (call-interactively command))))))
  19876. (defun org-yank-folding-would-swallow-text (beg end)
  19877. "Would hide-subtree at BEG swallow any text after END?"
  19878. (let (level)
  19879. (org-with-limited-levels
  19880. (save-excursion
  19881. (goto-char beg)
  19882. (when (or (looking-at org-outline-regexp)
  19883. (re-search-forward org-outline-regexp-bol end t))
  19884. (setq level (org-outline-level)))
  19885. (goto-char end)
  19886. (skip-chars-forward " \t\r\n\v\f")
  19887. (if (or (eobp)
  19888. (and (bolp) (looking-at org-outline-regexp)
  19889. (<= (org-outline-level) level)))
  19890. nil ; Nothing would be swallowed
  19891. t))))) ; something would swallow
  19892. (define-key org-mode-map "\C-y" 'org-yank)
  19893. (defun org-truely-invisible-p ()
  19894. "Check if point is at a character currently not visible.
  19895. This version does not only check the character property, but also
  19896. `visible-mode'."
  19897. ;; Early versions of noutline don't have `outline-invisible-p'.
  19898. (if (org-bound-and-true-p visible-mode)
  19899. nil
  19900. (outline-invisible-p)))
  19901. (defun org-invisible-p2 ()
  19902. "Check if point is at a character currently not visible."
  19903. (save-excursion
  19904. (if (and (eolp) (not (bobp))) (backward-char 1))
  19905. ;; Early versions of noutline don't have `outline-invisible-p'.
  19906. (outline-invisible-p)))
  19907. (defun org-back-to-heading (&optional invisible-ok)
  19908. "Call `outline-back-to-heading', but provide a better error message."
  19909. (condition-case nil
  19910. (outline-back-to-heading invisible-ok)
  19911. (error (error "Before first headline at position %d in buffer %s"
  19912. (point) (current-buffer)))))
  19913. (defun org-before-first-heading-p ()
  19914. "Before first heading?"
  19915. (save-excursion
  19916. (end-of-line)
  19917. (null (re-search-backward org-outline-regexp-bol nil t))))
  19918. (defun org-at-heading-p (&optional ignored)
  19919. (outline-on-heading-p t))
  19920. ;; Compatibility alias with Org versions < 7.8.03
  19921. (defalias 'org-on-heading-p 'org-at-heading-p)
  19922. (defun org-at-comment-p nil
  19923. "Is cursor in a line starting with a # character?"
  19924. (save-excursion
  19925. (beginning-of-line)
  19926. (looking-at "^#")))
  19927. (defun org-at-drawer-p nil
  19928. "Is cursor at a drawer keyword?"
  19929. (save-excursion
  19930. (move-beginning-of-line 1)
  19931. (looking-at org-drawer-regexp)))
  19932. (defun org-at-block-p nil
  19933. "Is cursor at a block keyword?"
  19934. (save-excursion
  19935. (move-beginning-of-line 1)
  19936. (looking-at org-block-regexp)))
  19937. (defun org-point-at-end-of-empty-headline ()
  19938. "If point is at the end of an empty headline, return t, else nil.
  19939. If the heading only contains a TODO keyword, it is still still considered
  19940. empty."
  19941. (and (looking-at "[ \t]*$")
  19942. (when org-todo-line-regexp
  19943. (save-excursion
  19944. (beginning-of-line 1)
  19945. (let ((case-fold-search nil))
  19946. (looking-at org-todo-line-regexp)
  19947. (string= (match-string 3) ""))))))
  19948. (defun org-at-heading-or-item-p ()
  19949. (or (org-at-heading-p) (org-at-item-p)))
  19950. (defun org-at-target-p ()
  19951. (or (org-in-regexp org-radio-target-regexp)
  19952. (org-in-regexp org-target-regexp)))
  19953. ;; Compatibility alias with Org versions < 7.8.03
  19954. (defalias 'org-on-target-p 'org-at-target-p)
  19955. (defun org-up-heading-all (arg)
  19956. "Move to the heading line of which the present line is a subheading.
  19957. This function considers both visible and invisible heading lines.
  19958. With argument, move up ARG levels."
  19959. (if (fboundp 'outline-up-heading-all)
  19960. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19961. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19962. (defun org-up-heading-safe ()
  19963. "Move to the heading line of which the present line is a subheading.
  19964. This version will not throw an error. It will return the level of the
  19965. headline found, or nil if no higher level is found.
  19966. Also, this function will be a lot faster than `outline-up-heading',
  19967. because it relies on stars being the outline starters. This can really
  19968. make a significant difference in outlines with very many siblings."
  19969. (let (start-level re)
  19970. (org-back-to-heading t)
  19971. (setq start-level (funcall outline-level))
  19972. (if (equal start-level 1)
  19973. nil
  19974. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19975. (if (re-search-backward re nil t)
  19976. (funcall outline-level)))))
  19977. (defun org-first-sibling-p ()
  19978. "Is this heading the first child of its parents?"
  19979. (interactive)
  19980. (let ((re org-outline-regexp-bol)
  19981. level l)
  19982. (unless (org-at-heading-p t)
  19983. (error "Not at a heading"))
  19984. (setq level (funcall outline-level))
  19985. (save-excursion
  19986. (if (not (re-search-backward re nil t))
  19987. t
  19988. (setq l (funcall outline-level))
  19989. (< l level)))))
  19990. (defun org-goto-sibling (&optional previous)
  19991. "Goto the next sibling, even if it is invisible.
  19992. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19993. when a sibling was found. When none is found, return nil and don't
  19994. move point."
  19995. (let ((fun (if previous 're-search-backward 're-search-forward))
  19996. (pos (point))
  19997. (re org-outline-regexp-bol)
  19998. level l)
  19999. (when (condition-case nil (org-back-to-heading t) (error nil))
  20000. (setq level (funcall outline-level))
  20001. (catch 'exit
  20002. (or previous (forward-char 1))
  20003. (while (funcall fun re nil t)
  20004. (setq l (funcall outline-level))
  20005. (when (< l level) (goto-char pos) (throw 'exit nil))
  20006. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20007. (goto-char pos)
  20008. nil))))
  20009. (defun org-show-siblings ()
  20010. "Show all siblings of the current headline."
  20011. (save-excursion
  20012. (while (org-goto-sibling) (org-flag-heading nil)))
  20013. (save-excursion
  20014. (while (org-goto-sibling 'previous)
  20015. (org-flag-heading nil))))
  20016. (defun org-goto-first-child ()
  20017. "Goto the first child, even if it is invisible.
  20018. Return t when a child was found. Otherwise don't move point and
  20019. return nil."
  20020. (let (level (pos (point)) (re org-outline-regexp-bol))
  20021. (when (condition-case nil (org-back-to-heading t) (error nil))
  20022. (setq level (outline-level))
  20023. (forward-char 1)
  20024. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20025. (progn (goto-char (match-beginning 0)) t)
  20026. (goto-char pos) nil))))
  20027. (defun org-show-hidden-entry ()
  20028. "Show an entry where even the heading is hidden."
  20029. (save-excursion
  20030. (org-show-entry)))
  20031. (defun org-flag-heading (flag &optional entry)
  20032. "Flag the current heading. FLAG non-nil means make invisible.
  20033. When ENTRY is non-nil, show the entire entry."
  20034. (save-excursion
  20035. (org-back-to-heading t)
  20036. ;; Check if we should show the entire entry
  20037. (if entry
  20038. (progn
  20039. (org-show-entry)
  20040. (save-excursion
  20041. (and (outline-next-heading)
  20042. (org-flag-heading nil))))
  20043. (outline-flag-region (max (point-min) (1- (point)))
  20044. (save-excursion (outline-end-of-heading) (point))
  20045. flag))))
  20046. (defun org-get-next-sibling ()
  20047. "Move to next heading of the same level, and return point.
  20048. If there is no such heading, return nil.
  20049. This is like outline-next-sibling, but invisible headings are ok."
  20050. (let ((level (funcall outline-level)))
  20051. (outline-next-heading)
  20052. (while (and (not (eobp)) (> (funcall outline-level) level))
  20053. (outline-next-heading))
  20054. (if (or (eobp) (< (funcall outline-level) level))
  20055. nil
  20056. (point))))
  20057. (defun org-get-last-sibling ()
  20058. "Move to previous heading of the same level, and return point.
  20059. If there is no such heading, return nil."
  20060. (let ((opoint (point))
  20061. (level (funcall outline-level)))
  20062. (outline-previous-heading)
  20063. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20064. (while (and (> (funcall outline-level) level)
  20065. (not (bobp)))
  20066. (outline-previous-heading))
  20067. (if (< (funcall outline-level) level)
  20068. nil
  20069. (point)))))
  20070. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20071. "Goto to the end of a subtree."
  20072. ;; This contains an exact copy of the original function, but it uses
  20073. ;; `org-back-to-heading', to make it work also in invisible
  20074. ;; trees. And is uses an invisible-ok argument.
  20075. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20076. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20077. ;; with many children and grandchildren etc, this can be much faster
  20078. ;; than the outline version.
  20079. (org-back-to-heading invisible-ok)
  20080. (let ((first t)
  20081. (level (funcall outline-level)))
  20082. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20083. ;; A true heading (not a plain list item), in Org-mode
  20084. ;; This means we can easily find the end by looking
  20085. ;; only for the right number of stars. Using a regexp to do
  20086. ;; this is so much faster than using a Lisp loop.
  20087. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20088. (forward-char 1)
  20089. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20090. ;; something else, do it the slow way
  20091. (while (and (not (eobp))
  20092. (or first (> (funcall outline-level) level)))
  20093. (setq first nil)
  20094. (outline-next-heading)))
  20095. (unless to-heading
  20096. (if (memq (preceding-char) '(?\n ?\^M))
  20097. (progn
  20098. ;; Go to end of line before heading
  20099. (forward-char -1)
  20100. (if (memq (preceding-char) '(?\n ?\^M))
  20101. ;; leave blank line before heading
  20102. (forward-char -1))))))
  20103. (point))
  20104. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20105. "Use Org version in org-mode, for dramatic speed-up."
  20106. (if (derived-mode-p 'org-mode)
  20107. (progn
  20108. (org-end-of-subtree nil t)
  20109. (unless (eobp) (backward-char 1)))
  20110. ad-do-it))
  20111. (defun org-end-of-meta-data-and-drawers ()
  20112. "Jump to the first text after meta data and drawers in the current entry.
  20113. This will move over empty lines, lines with planning time stamps,
  20114. clocking lines, and drawers."
  20115. (org-back-to-heading t)
  20116. (let ((end (save-excursion (outline-next-heading) (point)))
  20117. (re (concat "\\(" org-drawer-regexp "\\)"
  20118. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20119. (forward-line 1)
  20120. (while (re-search-forward re end t)
  20121. (if (not (match-end 1))
  20122. ;; empty or planning line
  20123. (forward-line 1)
  20124. ;; a drawer, find the end
  20125. (re-search-forward "^[ \t]*:END:" end 'move)
  20126. (forward-line 1)))
  20127. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20128. (point)))
  20129. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20130. "Move forward to the arg'th subheading at same level as this one.
  20131. Stop at the first and last subheadings of a superior heading.
  20132. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20133. non-nil it will also look at invisible ones."
  20134. (interactive "p")
  20135. (org-back-to-heading invisible-ok)
  20136. (org-at-heading-p)
  20137. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20138. (re (format "^\\*\\{1,%d\\} " level))
  20139. l)
  20140. (forward-char 1)
  20141. (while (> arg 0)
  20142. (while (and (re-search-forward re nil 'move)
  20143. (setq l (- (match-end 0) (match-beginning 0) 1))
  20144. (= l level)
  20145. (not invisible-ok)
  20146. (progn (backward-char 1) (outline-invisible-p)))
  20147. (if (< l level) (setq arg 1)))
  20148. (setq arg (1- arg)))
  20149. (beginning-of-line 1)))
  20150. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20151. "Move backward to the arg'th subheading at same level as this one.
  20152. Stop at the first and last subheadings of a superior heading."
  20153. (interactive "p")
  20154. (org-back-to-heading)
  20155. (org-at-heading-p)
  20156. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20157. (re (format "^\\*\\{1,%d\\} " level))
  20158. l)
  20159. (while (> arg 0)
  20160. (while (and (re-search-backward re nil 'move)
  20161. (setq l (- (match-end 0) (match-beginning 0) 1))
  20162. (= l level)
  20163. (not invisible-ok)
  20164. (outline-invisible-p))
  20165. (if (< l level) (setq arg 1)))
  20166. (setq arg (1- arg)))))
  20167. (defun org-forward-element ()
  20168. "Move forward by one element.
  20169. Move to the next element at the same level, when possible."
  20170. (interactive)
  20171. (cond ((eobp) (error "Cannot move further down"))
  20172. ((org-with-limited-levels (org-at-heading-p))
  20173. (let ((origin (point)))
  20174. (org-forward-heading-same-level 1)
  20175. (unless (org-with-limited-levels (org-at-heading-p))
  20176. (goto-char origin)
  20177. (error "Cannot move further down"))))
  20178. (t
  20179. (let* ((elem (org-element-at-point))
  20180. (end (org-element-property :end elem))
  20181. (parent (org-element-property :parent elem)))
  20182. (if (and parent (= (org-element-property :contents-end parent) end))
  20183. (goto-char (org-element-property :end parent))
  20184. (goto-char end))))))
  20185. (defun org-backward-element ()
  20186. "Move backward by one element.
  20187. Move to the previous element at the same level, when possible."
  20188. (interactive)
  20189. (cond ((bobp) (error "Cannot move further up"))
  20190. ((org-with-limited-levels (org-at-heading-p))
  20191. ;; At an headline, move to the previous one, if any, or stay
  20192. ;; here.
  20193. (let ((origin (point)))
  20194. (org-backward-heading-same-level 1)
  20195. (unless (org-with-limited-levels (org-at-heading-p))
  20196. (goto-char origin)
  20197. (error "Cannot move further up"))))
  20198. (t
  20199. (let* ((trail (org-element-at-point 'keep-trail))
  20200. (elem (car trail))
  20201. (prev-elem (nth 1 trail))
  20202. (beg (org-element-property :begin elem)))
  20203. (cond
  20204. ;; Move to beginning of current element if point isn't
  20205. ;; there already.
  20206. ((/= (point) beg) (goto-char beg))
  20207. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20208. ((org-before-first-heading-p) (goto-char (point-min)))
  20209. (t (org-back-to-heading)))))))
  20210. (defun org-up-element ()
  20211. "Move to upper element."
  20212. (interactive)
  20213. (if (org-with-limited-levels (org-at-heading-p))
  20214. (unless (org-up-heading-safe) (error "No surrounding element"))
  20215. (let* ((elem (org-element-at-point))
  20216. (parent (org-element-property :parent elem)))
  20217. (if parent (goto-char (org-element-property :begin parent))
  20218. (if (org-with-limited-levels (org-before-first-heading-p))
  20219. (error "No surrounding element")
  20220. (org-with-limited-levels (org-back-to-heading)))))))
  20221. (defvar org-element-greater-elements)
  20222. (defun org-down-element ()
  20223. "Move to inner element."
  20224. (interactive)
  20225. (let ((element (org-element-at-point)))
  20226. (cond
  20227. ((memq (org-element-type element) '(plain-list table))
  20228. (goto-char (org-element-property :contents-begin element))
  20229. (forward-char))
  20230. ((memq (org-element-type element) org-element-greater-elements)
  20231. ;; If contents are hidden, first disclose them.
  20232. (when (org-element-property :hiddenp element) (org-cycle))
  20233. (goto-char (or (org-element-property :contents-begin element)
  20234. (error "No content for this element"))))
  20235. (t (error "No inner element")))))
  20236. (defun org-drag-element-backward ()
  20237. "Move backward element at point."
  20238. (interactive)
  20239. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20240. (let* ((trail (org-element-at-point 'keep-trail))
  20241. (elem (car trail))
  20242. (prev-elem (nth 1 trail)))
  20243. ;; Error out if no previous element or previous element is
  20244. ;; a parent of the current one.
  20245. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20246. (error "Cannot drag element backward")
  20247. (let ((pos (point)))
  20248. (org-element-swap-A-B prev-elem elem)
  20249. (goto-char (+ (org-element-property :begin prev-elem)
  20250. (- pos (org-element-property :begin elem)))))))))
  20251. (defun org-drag-element-forward ()
  20252. "Move forward element at point."
  20253. (interactive)
  20254. (let* ((pos (point))
  20255. (elem (org-element-at-point)))
  20256. (when (= (point-max) (org-element-property :end elem))
  20257. (error "Cannot drag element forward"))
  20258. (goto-char (org-element-property :end elem))
  20259. (let ((next-elem (org-element-at-point)))
  20260. (when (or (org-element-nested-p elem next-elem)
  20261. (and (eq (org-element-type next-elem) 'headline)
  20262. (not (eq (org-element-type elem) 'headline))))
  20263. (goto-char pos)
  20264. (error "Cannot drag element forward"))
  20265. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20266. ;; body's length (without final blanks) and by the length of
  20267. ;; blanks between ELEM and NEXT-ELEM.
  20268. (let ((size-next (- (save-excursion
  20269. (goto-char (org-element-property :end next-elem))
  20270. (skip-chars-backward " \r\t\n")
  20271. (forward-line)
  20272. ;; Small correction if buffer doesn't end
  20273. ;; with a newline character.
  20274. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20275. (org-element-property :begin next-elem)))
  20276. (size-blank (- (org-element-property :end elem)
  20277. (save-excursion
  20278. (goto-char (org-element-property :end elem))
  20279. (skip-chars-backward " \r\t\n")
  20280. (forward-line)
  20281. (point)))))
  20282. (org-element-swap-A-B elem next-elem)
  20283. (goto-char (+ pos size-next size-blank))))))
  20284. (defun org-mark-element ()
  20285. "Put point at beginning of this element, mark at end.
  20286. Interactively, if this command is repeated or (in Transient Mark
  20287. mode) if the mark is active, it marks the next element after the
  20288. ones already marked."
  20289. (interactive)
  20290. (let (deactivate-mark)
  20291. (if (and (org-called-interactively-p 'any)
  20292. (or (and (eq last-command this-command) (mark t))
  20293. (and transient-mark-mode mark-active)))
  20294. (set-mark
  20295. (save-excursion
  20296. (goto-char (mark))
  20297. (goto-char (org-element-property :end (org-element-at-point)))))
  20298. (let ((element (org-element-at-point)))
  20299. (end-of-line)
  20300. (push-mark (org-element-property :end element) t t)
  20301. (goto-char (org-element-property :begin element))))))
  20302. (defun org-narrow-to-element ()
  20303. "Narrow buffer to current element."
  20304. (interactive)
  20305. (let ((elem (org-element-at-point)))
  20306. (cond
  20307. ((eq (car elem) 'headline)
  20308. (narrow-to-region
  20309. (org-element-property :begin elem)
  20310. (org-element-property :end elem)))
  20311. ((memq (car elem) org-element-greater-elements)
  20312. (narrow-to-region
  20313. (org-element-property :contents-begin elem)
  20314. (org-element-property :contents-end elem)))
  20315. (t
  20316. (narrow-to-region
  20317. (org-element-property :begin elem)
  20318. (org-element-property :end elem))))))
  20319. (defun org-transpose-element ()
  20320. "Transpose current and previous elements, keeping blank lines between.
  20321. Point is moved after both elements."
  20322. (interactive)
  20323. (org-skip-whitespace)
  20324. (let ((end (org-element-property :end (org-element-at-point))))
  20325. (org-drag-element-backward)
  20326. (goto-char end)))
  20327. (defun org-unindent-buffer ()
  20328. "Un-indent the visible part of the buffer.
  20329. Relative indentation (between items, inside blocks, etc.) isn't
  20330. modified."
  20331. (interactive)
  20332. (unless (eq major-mode 'org-mode)
  20333. (error "Cannot un-indent a buffer not in Org mode"))
  20334. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20335. unindent-tree ; For byte-compiler.
  20336. (unindent-tree
  20337. (function
  20338. (lambda (contents)
  20339. (mapc
  20340. (lambda (element)
  20341. (if (memq (org-element-type element) '(headline section))
  20342. (funcall unindent-tree (org-element-contents element))
  20343. (save-excursion
  20344. (save-restriction
  20345. (narrow-to-region
  20346. (org-element-property :begin element)
  20347. (org-element-property :end element))
  20348. (org-do-remove-indentation)))))
  20349. (reverse contents))))))
  20350. (funcall unindent-tree (org-element-contents parse-tree))))
  20351. (defun org-show-subtree ()
  20352. "Show everything after this heading at deeper levels."
  20353. (interactive)
  20354. (outline-flag-region
  20355. (point)
  20356. (save-excursion
  20357. (org-end-of-subtree t t))
  20358. nil))
  20359. (defun org-show-entry ()
  20360. "Show the body directly following this heading.
  20361. Show the heading too, if it is currently invisible."
  20362. (interactive)
  20363. (save-excursion
  20364. (condition-case nil
  20365. (progn
  20366. (org-back-to-heading t)
  20367. (outline-flag-region
  20368. (max (point-min) (1- (point)))
  20369. (save-excursion
  20370. (if (re-search-forward
  20371. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  20372. (match-beginning 1)
  20373. (point-max)))
  20374. nil)
  20375. (org-cycle-hide-drawers 'children))
  20376. (error nil))))
  20377. (defun org-make-options-regexp (kwds &optional extra)
  20378. "Make a regular expression for keyword lines."
  20379. (concat
  20380. "^#\\+\\("
  20381. (mapconcat 'regexp-quote kwds "\\|")
  20382. (if extra (concat "\\|" extra))
  20383. "\\):[ \t]*\\(.*\\)"))
  20384. ;; Make isearch reveal the necessary context
  20385. (defun org-isearch-end ()
  20386. "Reveal context after isearch exits."
  20387. (when isearch-success ; only if search was successful
  20388. (if (featurep 'xemacs)
  20389. ;; Under XEmacs, the hook is run in the correct place,
  20390. ;; we directly show the context.
  20391. (org-show-context 'isearch)
  20392. ;; In Emacs the hook runs *before* restoring the overlays.
  20393. ;; So we have to use a one-time post-command-hook to do this.
  20394. ;; (Emacs 22 has a special variable, see function `org-mode')
  20395. (unless (and (boundp 'isearch-mode-end-hook-quit)
  20396. isearch-mode-end-hook-quit)
  20397. ;; Only when the isearch was not quitted.
  20398. (org-add-hook 'post-command-hook 'org-isearch-post-command
  20399. 'append 'local)))))
  20400. (defun org-isearch-post-command ()
  20401. "Remove self from hook, and show context."
  20402. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  20403. (org-show-context 'isearch))
  20404. ;;;; Integration with and fixes for other packages
  20405. ;;; Imenu support
  20406. (defvar org-imenu-markers nil
  20407. "All markers currently used by Imenu.")
  20408. (make-variable-buffer-local 'org-imenu-markers)
  20409. (defun org-imenu-new-marker (&optional pos)
  20410. "Return a new marker for use by Imenu, and remember the marker."
  20411. (let ((m (make-marker)))
  20412. (move-marker m (or pos (point)))
  20413. (push m org-imenu-markers)
  20414. m))
  20415. (defun org-imenu-get-tree ()
  20416. "Produce the index for Imenu."
  20417. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  20418. (setq org-imenu-markers nil)
  20419. (let* ((n org-imenu-depth)
  20420. (re (concat "^" (org-get-limited-outline-regexp)))
  20421. (subs (make-vector (1+ n) nil))
  20422. (last-level 0)
  20423. m level head)
  20424. (save-excursion
  20425. (save-restriction
  20426. (widen)
  20427. (goto-char (point-max))
  20428. (while (re-search-backward re nil t)
  20429. (setq level (org-reduced-level (funcall outline-level)))
  20430. (when (and (<= level n)
  20431. (looking-at org-complex-heading-regexp))
  20432. (setq head (org-link-display-format
  20433. (org-match-string-no-properties 4))
  20434. m (org-imenu-new-marker))
  20435. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  20436. (if (>= level last-level)
  20437. (push (cons head m) (aref subs level))
  20438. (push (cons head (aref subs (1+ level))) (aref subs level))
  20439. (loop for i from (1+ level) to n do (aset subs i nil)))
  20440. (setq last-level level)))))
  20441. (aref subs 1)))
  20442. (eval-after-load "imenu"
  20443. '(progn
  20444. (add-hook 'imenu-after-jump-hook
  20445. (lambda ()
  20446. (if (derived-mode-p 'org-mode)
  20447. (org-show-context 'org-goto))))))
  20448. (defun org-link-display-format (link)
  20449. "Replace a link with either the description, or the link target
  20450. if no description is present"
  20451. (save-match-data
  20452. (if (string-match org-bracket-link-analytic-regexp link)
  20453. (replace-match (if (match-end 5)
  20454. (match-string 5 link)
  20455. (concat (match-string 1 link)
  20456. (match-string 3 link)))
  20457. nil t link)
  20458. link)))
  20459. (defun org-toggle-link-display ()
  20460. "Toggle the literal or descriptive display of links."
  20461. (interactive)
  20462. (if org-descriptive-links
  20463. (progn (org-remove-from-invisibility-spec '(org-link))
  20464. (org-restart-font-lock)
  20465. (setq org-descriptive-links nil))
  20466. (progn (add-to-invisibility-spec '(org-link))
  20467. (org-restart-font-lock)
  20468. (setq org-descriptive-links t))))
  20469. ;; Speedbar support
  20470. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  20471. "Overlay marking the agenda restriction line in speedbar.")
  20472. (overlay-put org-speedbar-restriction-lock-overlay
  20473. 'face 'org-agenda-restriction-lock)
  20474. (overlay-put org-speedbar-restriction-lock-overlay
  20475. 'help-echo "Agendas are currently limited to this item.")
  20476. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20477. (defun org-speedbar-set-agenda-restriction ()
  20478. "Restrict future agenda commands to the location at point in speedbar.
  20479. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  20480. (interactive)
  20481. (require 'org-agenda)
  20482. (let (p m tp np dir txt)
  20483. (cond
  20484. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20485. 'org-imenu t))
  20486. (setq m (get-text-property p 'org-imenu-marker))
  20487. (with-current-buffer (marker-buffer m)
  20488. (goto-char m)
  20489. (org-agenda-set-restriction-lock 'subtree)))
  20490. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20491. 'speedbar-function 'speedbar-find-file))
  20492. (setq tp (previous-single-property-change
  20493. (1+ p) 'speedbar-function)
  20494. np (next-single-property-change
  20495. tp 'speedbar-function)
  20496. dir (speedbar-line-directory)
  20497. txt (buffer-substring-no-properties (or tp (point-min))
  20498. (or np (point-max))))
  20499. (with-current-buffer (find-file-noselect
  20500. (let ((default-directory dir))
  20501. (expand-file-name txt)))
  20502. (unless (derived-mode-p 'org-mode)
  20503. (error "Cannot restrict to non-Org-mode file"))
  20504. (org-agenda-set-restriction-lock 'file)))
  20505. (t (error "Don't know how to restrict Org-mode's agenda")))
  20506. (move-overlay org-speedbar-restriction-lock-overlay
  20507. (point-at-bol) (point-at-eol))
  20508. (setq current-prefix-arg nil)
  20509. (org-agenda-maybe-redo)))
  20510. (eval-after-load "speedbar"
  20511. '(progn
  20512. (speedbar-add-supported-extension ".org")
  20513. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  20514. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  20515. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  20516. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  20517. (add-hook 'speedbar-visiting-tag-hook
  20518. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  20519. ;;; Fixes and Hacks for problems with other packages
  20520. ;; Make flyspell not check words in links, to not mess up our keymap
  20521. (defun org-mode-flyspell-verify ()
  20522. "Don't let flyspell put overlays at active buttons, or on
  20523. {todo,all-time,additional-option-like}-keywords."
  20524. (let ((pos (max (1- (point)) (point-min)))
  20525. (word (thing-at-point 'word)))
  20526. (and (not (get-text-property pos 'keymap))
  20527. (not (get-text-property pos 'org-no-flyspell))
  20528. (not (member word org-todo-keywords-1))
  20529. (not (member word org-all-time-keywords))
  20530. (not (member word org-options-keywords))
  20531. (not (member word (mapcar 'car org-startup-options)))
  20532. (not (member word org-additional-option-like-keywords-for-flyspell)))))
  20533. (defun org-remove-flyspell-overlays-in (beg end)
  20534. "Remove flyspell overlays in region."
  20535. (and (org-bound-and-true-p flyspell-mode)
  20536. (fboundp 'flyspell-delete-region-overlays)
  20537. (flyspell-delete-region-overlays beg end))
  20538. (add-text-properties beg end '(org-no-flyspell t)))
  20539. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  20540. (eval-after-load "bookmark"
  20541. '(if (boundp 'bookmark-after-jump-hook)
  20542. ;; We can use the hook
  20543. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  20544. ;; Hook not available, use advice
  20545. (defadvice bookmark-jump (after org-make-visible activate)
  20546. "Make the position visible."
  20547. (org-bookmark-jump-unhide))))
  20548. ;; Make sure saveplace shows the location if it was hidden
  20549. (eval-after-load "saveplace"
  20550. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  20551. "Make the position visible."
  20552. (org-bookmark-jump-unhide)))
  20553. ;; Make sure ecb shows the location if it was hidden
  20554. (eval-after-load "ecb"
  20555. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  20556. "Make hierarchy visible when jumping into location from ECB tree buffer."
  20557. (if (derived-mode-p 'org-mode)
  20558. (org-show-context))))
  20559. (defun org-bookmark-jump-unhide ()
  20560. "Unhide the current position, to show the bookmark location."
  20561. (and (derived-mode-p 'org-mode)
  20562. (or (outline-invisible-p)
  20563. (save-excursion (goto-char (max (point-min) (1- (point))))
  20564. (outline-invisible-p)))
  20565. (org-show-context 'bookmark-jump)))
  20566. ;; Make session.el ignore our circular variable
  20567. (eval-after-load "session"
  20568. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  20569. ;;;; Experimental code
  20570. (defun org-closed-in-range ()
  20571. "Sparse tree of items closed in a certain time range.
  20572. Still experimental, may disappear in the future."
  20573. (interactive)
  20574. ;; Get the time interval from the user.
  20575. (let* ((time1 (org-float-time
  20576. (org-read-date nil 'to-time nil "Starting date: ")))
  20577. (time2 (org-float-time
  20578. (org-read-date nil 'to-time nil "End date:")))
  20579. ;; callback function
  20580. (callback (lambda ()
  20581. (let ((time
  20582. (org-float-time
  20583. (apply 'encode-time
  20584. (org-parse-time-string
  20585. (match-string 1))))))
  20586. ;; check if time in interval
  20587. (and (>= time time1) (<= time time2))))))
  20588. ;; make tree, check each match with the callback
  20589. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  20590. ;;;; Finish up
  20591. (provide 'org)
  20592. (run-hooks 'org-load-hook)
  20593. ;;; org.el ends here