org.el 845 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410
  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. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4394. ;; Setup the pcomplete hooks
  4395. (set (make-local-variable 'pcomplete-command-completion-function)
  4396. 'org-pcomplete-initial)
  4397. (set (make-local-variable 'pcomplete-command-name-function)
  4398. 'org-command-at-point)
  4399. (set (make-local-variable 'pcomplete-default-completion-function)
  4400. 'ignore)
  4401. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4402. 'org-parse-arguments)
  4403. (set (make-local-variable 'pcomplete-termination-string) "")
  4404. (when (>= emacs-major-version 23)
  4405. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4406. ;; If empty file that did not turn on org-mode automatically, make it to.
  4407. (if (and org-insert-mode-line-in-empty-file
  4408. (org-called-interactively-p 'any)
  4409. (= (point-min) (point-max)))
  4410. (insert "# -*- mode: org -*-\n\n"))
  4411. (unless org-inhibit-startup
  4412. (and org-startup-with-beamer-mode (org-beamer-mode))
  4413. (when org-startup-align-all-tables
  4414. (let ((bmp (buffer-modified-p)))
  4415. (org-table-map-tables 'org-table-align 'quietly)
  4416. (set-buffer-modified-p bmp)))
  4417. (when org-startup-with-inline-images
  4418. (org-display-inline-images))
  4419. (unless org-inhibit-startup-visibility-stuff
  4420. (org-set-startup-visibility)))
  4421. ;; Try to set org-hide correctly
  4422. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4423. (when (fboundp 'abbrev-table-put)
  4424. (abbrev-table-put org-mode-abbrev-table
  4425. :parents (list text-mode-abbrev-table)))
  4426. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4427. (defsubst org-fix-ellipsis-at-bol ()
  4428. (save-excursion (goto-char (window-start)) (recenter 0)))
  4429. (defun org-find-invisible-foreground ()
  4430. (let ((candidates (remove
  4431. "unspecified-bg"
  4432. (nconc
  4433. (list (face-background 'default)
  4434. (face-background 'org-default))
  4435. (mapcar
  4436. (lambda (alist)
  4437. (when (boundp alist)
  4438. (cdr (assoc 'background-color (symbol-value alist)))))
  4439. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4440. (list (face-foreground 'org-hide))))))
  4441. (car (remove nil candidates))))
  4442. (defun org-current-time ()
  4443. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4444. (if (> (car org-time-stamp-rounding-minutes) 1)
  4445. (let ((r (car org-time-stamp-rounding-minutes))
  4446. (time (decode-time)))
  4447. (apply 'encode-time
  4448. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4449. (nthcdr 2 time))))
  4450. (current-time)))
  4451. (defun org-today ()
  4452. "Return today date, considering `org-extend-today-until'."
  4453. (time-to-days
  4454. (time-subtract (current-time)
  4455. (list 0 (* 3600 org-extend-today-until) 0))))
  4456. ;;;; Font-Lock stuff, including the activators
  4457. (defvar org-mouse-map (make-sparse-keymap))
  4458. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4459. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4460. (when org-mouse-1-follows-link
  4461. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4462. (when org-tab-follows-link
  4463. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4464. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4465. (require 'font-lock)
  4466. (defconst org-non-link-chars "]\t\n\r<>")
  4467. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4468. "shell" "elisp" "doi" "message"))
  4469. (defvar org-link-types-re nil
  4470. "Matches a link that has a url-like prefix like \"http:\"")
  4471. (defvar org-link-re-with-space nil
  4472. "Matches a link with spaces, optional angular brackets around it.")
  4473. (defvar org-link-re-with-space2 nil
  4474. "Matches a link with spaces, optional angular brackets around it.")
  4475. (defvar org-link-re-with-space3 nil
  4476. "Matches a link with spaces, only for internal part in bracket links.")
  4477. (defvar org-angle-link-re nil
  4478. "Matches link with angular brackets, spaces are allowed.")
  4479. (defvar org-plain-link-re nil
  4480. "Matches plain link, without spaces.")
  4481. (defvar org-bracket-link-regexp nil
  4482. "Matches a link in double brackets.")
  4483. (defvar org-bracket-link-analytic-regexp nil
  4484. "Regular expression used to analyze links.
  4485. Here is what the match groups contain after a match:
  4486. 1: http:
  4487. 2: http
  4488. 3: path
  4489. 4: [desc]
  4490. 5: desc")
  4491. (defvar org-bracket-link-analytic-regexp++ nil
  4492. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4493. (defvar org-any-link-re nil
  4494. "Regular expression matching any link.")
  4495. (defcustom org-match-sexp-depth 3
  4496. "Number of stacked braces for sub/superscript matching.
  4497. This has to be set before loading org.el to be effective."
  4498. :group 'org-export-translation ; ??????????????????????????/
  4499. :type 'integer)
  4500. (defun org-create-multibrace-regexp (left right n)
  4501. "Create a regular expression which will match a balanced sexp.
  4502. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4503. as single character strings.
  4504. The regexp returned will match the entire expression including the
  4505. delimiters. It will also define a single group which contains the
  4506. match except for the outermost delimiters. The maximum depth of
  4507. stacked delimiters is N. Escaping delimiters is not possible."
  4508. (let* ((nothing (concat "[^" left right "]*?"))
  4509. (or "\\|")
  4510. (re nothing)
  4511. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4512. (while (> n 1)
  4513. (setq n (1- n)
  4514. re (concat re or next)
  4515. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4516. (concat left "\\(" re "\\)" right)))
  4517. (defvar org-match-substring-regexp
  4518. (concat
  4519. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4520. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4521. "\\|"
  4522. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4523. "\\|"
  4524. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4525. "The regular expression matching a sub- or superscript.")
  4526. (defvar org-match-substring-with-braces-regexp
  4527. (concat
  4528. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4529. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4530. "\\)")
  4531. "The regular expression matching a sub- or superscript, forcing braces.")
  4532. (defun org-make-link-regexps ()
  4533. "Update the link regular expressions.
  4534. This should be called after the variable `org-link-types' has changed."
  4535. (setq org-link-types-re
  4536. (concat
  4537. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4538. org-link-re-with-space
  4539. (concat
  4540. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4541. "\\([^" org-non-link-chars " ]"
  4542. "[^" org-non-link-chars "]*"
  4543. "[^" org-non-link-chars " ]\\)>?")
  4544. org-link-re-with-space2
  4545. (concat
  4546. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4547. "\\([^" org-non-link-chars " ]"
  4548. "[^\t\n\r]*"
  4549. "[^" org-non-link-chars " ]\\)>?")
  4550. org-link-re-with-space3
  4551. (concat
  4552. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4553. "\\([^" org-non-link-chars " ]"
  4554. "[^\t\n\r]*\\)")
  4555. org-angle-link-re
  4556. (concat
  4557. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4558. "\\([^" org-non-link-chars " ]"
  4559. "[^" org-non-link-chars "]*"
  4560. "\\)>")
  4561. org-plain-link-re
  4562. (concat
  4563. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4564. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4565. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4566. org-bracket-link-regexp
  4567. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4568. org-bracket-link-analytic-regexp
  4569. (concat
  4570. "\\[\\["
  4571. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4572. "\\([^]]+\\)"
  4573. "\\]"
  4574. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4575. "\\]")
  4576. org-bracket-link-analytic-regexp++
  4577. (concat
  4578. "\\[\\["
  4579. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4580. "\\([^]]+\\)"
  4581. "\\]"
  4582. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4583. "\\]")
  4584. org-any-link-re
  4585. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4586. org-angle-link-re "\\)\\|\\("
  4587. org-plain-link-re "\\)")))
  4588. (org-make-link-regexps)
  4589. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4590. "Regular expression for fast time stamp matching.")
  4591. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4592. "Regular expression for fast time stamp matching.")
  4593. (defconst org-ts-regexp0
  4594. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4595. "Regular expression matching time strings for analysis.
  4596. This one does not require the space after the date, so it can be used
  4597. on a string that terminates immediately after the date.")
  4598. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4599. "Regular expression matching time strings for analysis.")
  4600. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4601. "Regular expression matching time stamps, with groups.")
  4602. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4603. "Regular expression matching time stamps (also [..]), with groups.")
  4604. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4605. "Regular expression matching a time stamp range.")
  4606. (defconst org-tr-regexp-both
  4607. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4608. "Regular expression matching a time stamp range.")
  4609. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4610. org-ts-regexp "\\)?")
  4611. "Regular expression matching a time stamp or time stamp range.")
  4612. (defconst org-tsr-regexp-both
  4613. (concat org-ts-regexp-both "\\(--?-?"
  4614. org-ts-regexp-both "\\)?")
  4615. "Regular expression matching a time stamp or time stamp range.
  4616. The time stamps may be either active or inactive.")
  4617. (defvar org-emph-face nil)
  4618. (defun org-do-emphasis-faces (limit)
  4619. "Run through the buffer and add overlays to emphasized strings."
  4620. (let (rtn a)
  4621. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4622. (if (not (= (char-after (match-beginning 3))
  4623. (char-after (match-beginning 4))))
  4624. (progn
  4625. (setq rtn t)
  4626. (setq a (assoc (match-string 3) org-emphasis-alist))
  4627. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4628. 'face
  4629. (nth 1 a))
  4630. (and (nth 4 a)
  4631. (org-remove-flyspell-overlays-in
  4632. (match-beginning 0) (match-end 0)))
  4633. (add-text-properties (match-beginning 2) (match-end 2)
  4634. '(font-lock-multiline t org-emphasis t))
  4635. (when org-hide-emphasis-markers
  4636. (add-text-properties (match-end 4) (match-beginning 5)
  4637. '(invisible org-link))
  4638. (add-text-properties (match-beginning 3) (match-end 3)
  4639. '(invisible org-link)))))
  4640. (backward-char 1))
  4641. rtn))
  4642. (defun org-emphasize (&optional char)
  4643. "Insert or change an emphasis, i.e. a font like bold or italic.
  4644. If there is an active region, change that region to a new emphasis.
  4645. If there is no region, just insert the marker characters and position
  4646. the cursor between them.
  4647. CHAR should be either the marker character, or the first character of the
  4648. HTML tag associated with that emphasis. If CHAR is a space, the means
  4649. to remove the emphasis of the selected region.
  4650. If char is not given (for example in an interactive call) it
  4651. will be prompted for."
  4652. (interactive)
  4653. (let ((eal org-emphasis-alist) e det
  4654. (erc org-emphasis-regexp-components)
  4655. (prompt "")
  4656. (string "") beg end move tag c s)
  4657. (if (org-region-active-p)
  4658. (setq beg (region-beginning) end (region-end)
  4659. string (buffer-substring beg end))
  4660. (setq move t))
  4661. (while (setq e (pop eal))
  4662. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4663. c (aref tag 0))
  4664. (push (cons c (string-to-char (car e))) det)
  4665. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4666. (substring tag 1)))))
  4667. (setq det (nreverse det))
  4668. (unless char
  4669. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4670. (setq char (read-char-exclusive)))
  4671. (setq char (or (cdr (assoc char det)) char))
  4672. (if (equal char ?\ )
  4673. (setq s "" move nil)
  4674. (unless (assoc (char-to-string char) org-emphasis-alist)
  4675. (error "No such emphasis marker: \"%c\"" char))
  4676. (setq s (char-to-string char)))
  4677. (while (and (> (length string) 1)
  4678. (equal (substring string 0 1) (substring string -1))
  4679. (assoc (substring string 0 1) org-emphasis-alist))
  4680. (setq string (substring string 1 -1)))
  4681. (setq string (concat s string s))
  4682. (if beg (delete-region beg end))
  4683. (unless (or (bolp)
  4684. (string-match (concat "[" (nth 0 erc) "\n]")
  4685. (char-to-string (char-before (point)))))
  4686. (insert " "))
  4687. (unless (or (eobp)
  4688. (string-match (concat "[" (nth 1 erc) "\n]")
  4689. (char-to-string (char-after (point)))))
  4690. (insert " ") (backward-char 1))
  4691. (insert string)
  4692. (and move (backward-char 1))))
  4693. (defconst org-nonsticky-props
  4694. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4695. (defsubst org-rear-nonsticky-at (pos)
  4696. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4697. (defun org-activate-plain-links (limit)
  4698. "Run through the buffer and add overlays to links."
  4699. (let (f)
  4700. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4701. (not (org-in-src-block-p)))
  4702. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4703. (setq f (get-text-property (match-beginning 0) 'face))
  4704. (unless (or (org-in-src-block-p)
  4705. (eq f 'org-tag)
  4706. (and (listp f) (memq 'org-tag f)))
  4707. (add-text-properties (match-beginning 0) (match-end 0)
  4708. (list 'mouse-face 'highlight
  4709. 'face 'org-link
  4710. 'keymap org-mouse-map))
  4711. (org-rear-nonsticky-at (match-end 0)))
  4712. t)))
  4713. (defun org-activate-code (limit)
  4714. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4715. (progn
  4716. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4717. (remove-text-properties (match-beginning 0) (match-end 0)
  4718. '(display t invisible t intangible t))
  4719. t)))
  4720. (defcustom org-src-fontify-natively nil
  4721. "When non-nil, fontify code in code blocks."
  4722. :type 'boolean
  4723. :version "24.1"
  4724. :group 'org-appearance
  4725. :group 'org-babel)
  4726. (defcustom org-allow-promoting-top-level-subtree nil
  4727. "When non-nil, allow promoting a top level subtree.
  4728. The leading star of the top level headline will be replaced
  4729. by a #."
  4730. :type 'boolean
  4731. :version "24.1"
  4732. :group 'org-appearance)
  4733. (defun org-fontify-meta-lines-and-blocks (limit)
  4734. (condition-case nil
  4735. (org-fontify-meta-lines-and-blocks-1 limit)
  4736. (error (message "org-mode fontification error"))))
  4737. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4738. "Fontify #+ lines and blocks, in the correct ways."
  4739. (let ((case-fold-search t))
  4740. (if (re-search-forward
  4741. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4742. limit t)
  4743. (let ((beg (match-beginning 0))
  4744. (block-start (match-end 0))
  4745. (block-end nil)
  4746. (lang (match-string 7))
  4747. (beg1 (line-beginning-position 2))
  4748. (dc1 (downcase (match-string 2)))
  4749. (dc3 (downcase (match-string 3)))
  4750. end end1 quoting block-type ovl)
  4751. (cond
  4752. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4753. ;; a single line of backend-specific content
  4754. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4755. (remove-text-properties (match-beginning 0) (match-end 0)
  4756. '(display t invisible t intangible t))
  4757. (add-text-properties (match-beginning 1) (match-end 3)
  4758. '(font-lock-fontified t face org-meta-line))
  4759. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4760. '(font-lock-fontified t face org-block))
  4761. ; for backend-specific code
  4762. t)
  4763. ((and (match-end 4) (equal dc3 "+begin"))
  4764. ;; Truly a block
  4765. (setq block-type (downcase (match-string 5))
  4766. quoting (member block-type org-protecting-blocks))
  4767. (when (re-search-forward
  4768. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4769. nil t) ;; on purpose, we look further than LIMIT
  4770. (setq end (min (point-max) (match-end 0))
  4771. end1 (min (point-max) (1- (match-beginning 0))))
  4772. (setq block-end (match-beginning 0))
  4773. (when quoting
  4774. (remove-text-properties beg end
  4775. '(display t invisible t intangible t)))
  4776. (add-text-properties
  4777. beg end
  4778. '(font-lock-fontified t font-lock-multiline t))
  4779. (add-text-properties beg beg1 '(face org-meta-line))
  4780. (add-text-properties end1 (min (point-max) (1+ end))
  4781. '(face org-meta-line)) ; for end_src
  4782. (cond
  4783. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4784. (org-src-font-lock-fontify-block lang block-start block-end)
  4785. ;; remove old background overlays
  4786. (mapc (lambda (ov)
  4787. (if (eq (overlay-get ov 'face) 'org-block-background)
  4788. (delete-overlay ov)))
  4789. (overlays-at (/ (+ beg1 block-end) 2)))
  4790. ;; add a background overlay
  4791. (setq ovl (make-overlay beg1 block-end))
  4792. (overlay-put ovl 'face 'org-block-background)
  4793. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4794. (quoting
  4795. (add-text-properties beg1 (min (point-max) (1+ end1))
  4796. '(face org-block))) ; end of source block
  4797. ((not org-fontify-quote-and-verse-blocks))
  4798. ((string= block-type "quote")
  4799. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4800. ((string= block-type "verse")
  4801. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4802. (add-text-properties beg beg1 '(face org-block-begin-line))
  4803. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4804. '(face org-block-end-line))
  4805. t))
  4806. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  4807. (add-text-properties
  4808. beg (match-end 3)
  4809. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4810. '(font-lock-fontified t invisible t)
  4811. '(font-lock-fontified t face org-document-info-keyword)))
  4812. (add-text-properties
  4813. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  4814. (if (string-equal dc1 "+title:")
  4815. '(font-lock-fontified t face org-document-title)
  4816. '(font-lock-fontified t face org-document-info))))
  4817. ((or (equal dc1 "+results")
  4818. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  4819. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  4820. "+call:" "+header:" "+headers:" "+name:"))
  4821. (and (match-end 4) (equal dc3 "+attr")))
  4822. (add-text-properties
  4823. beg (match-end 0)
  4824. '(font-lock-fontified t face org-meta-line))
  4825. t)
  4826. ((member dc3 '(" " ""))
  4827. (add-text-properties
  4828. beg (match-end 0)
  4829. '(font-lock-fontified t face font-lock-comment-face)))
  4830. ((not (member (char-after beg) '(?\ ?\t)))
  4831. ;; just any other in-buffer setting, but not indented
  4832. (add-text-properties
  4833. beg (match-end 0)
  4834. '(font-lock-fontified t face org-meta-line))
  4835. t)
  4836. (t nil))))))
  4837. (defun org-activate-angle-links (limit)
  4838. "Run through the buffer and add overlays to links."
  4839. (if (and (re-search-forward org-angle-link-re limit t)
  4840. (not (org-in-src-block-p)))
  4841. (progn
  4842. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4843. (add-text-properties (match-beginning 0) (match-end 0)
  4844. (list 'mouse-face 'highlight
  4845. 'keymap org-mouse-map))
  4846. (org-rear-nonsticky-at (match-end 0))
  4847. t)))
  4848. (defun org-activate-footnote-links (limit)
  4849. "Run through the buffer and add overlays to footnotes."
  4850. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4851. (when fn
  4852. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4853. (org-remove-flyspell-overlays-in beg end)
  4854. (add-text-properties beg end
  4855. (list 'mouse-face 'highlight
  4856. 'keymap org-mouse-map
  4857. 'help-echo
  4858. (if (= (point-at-bol) beg)
  4859. "Footnote definition"
  4860. "Footnote reference")
  4861. 'font-lock-fontified t
  4862. 'font-lock-multiline t
  4863. 'face 'org-footnote))))))
  4864. (defun org-activate-bracket-links (limit)
  4865. "Run through the buffer and add overlays to bracketed links."
  4866. (if (and (re-search-forward org-bracket-link-regexp limit t)
  4867. (not (org-in-src-block-p)))
  4868. (let* ((help (concat "LINK: "
  4869. (org-match-string-no-properties 1)))
  4870. ;; FIXME: above we should remove the escapes.
  4871. ;; but that requires another match, protecting match data,
  4872. ;; a lot of overhead for font-lock.
  4873. (ip (org-maybe-intangible
  4874. (list 'invisible 'org-link
  4875. 'keymap org-mouse-map 'mouse-face 'highlight
  4876. 'font-lock-multiline t 'help-echo help)))
  4877. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4878. 'font-lock-multiline t 'help-echo help)))
  4879. ;; We need to remove the invisible property here. Table narrowing
  4880. ;; may have made some of this invisible.
  4881. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4882. (remove-text-properties (match-beginning 0) (match-end 0)
  4883. '(invisible nil))
  4884. (if (match-end 3)
  4885. (progn
  4886. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4887. (org-rear-nonsticky-at (match-beginning 3))
  4888. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4889. (org-rear-nonsticky-at (match-end 3))
  4890. (add-text-properties (match-end 3) (match-end 0) ip)
  4891. (org-rear-nonsticky-at (match-end 0)))
  4892. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4893. (org-rear-nonsticky-at (match-beginning 1))
  4894. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4895. (org-rear-nonsticky-at (match-end 1))
  4896. (add-text-properties (match-end 1) (match-end 0) ip)
  4897. (org-rear-nonsticky-at (match-end 0)))
  4898. t)))
  4899. (defun org-activate-dates (limit)
  4900. "Run through the buffer and add overlays to dates."
  4901. (if (and (re-search-forward org-tsr-regexp-both limit t)
  4902. (not (equal (char-before (match-beginning 0)) 91)))
  4903. (progn
  4904. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4905. (add-text-properties (match-beginning 0) (match-end 0)
  4906. (list 'mouse-face 'highlight
  4907. 'keymap org-mouse-map))
  4908. (org-rear-nonsticky-at (match-end 0))
  4909. (when org-display-custom-times
  4910. (if (match-end 3)
  4911. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4912. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4913. t)))
  4914. (defvar org-target-link-regexp nil
  4915. "Regular expression matching radio targets in plain text.")
  4916. (make-variable-buffer-local 'org-target-link-regexp)
  4917. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4918. "Regular expression matching a link target.")
  4919. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4920. "Regular expression matching a radio target.")
  4921. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4922. "Regular expression matching any target.")
  4923. (defun org-activate-target-links (limit)
  4924. "Run through the buffer and add overlays to target matches."
  4925. (when org-target-link-regexp
  4926. (let ((case-fold-search t))
  4927. (if (re-search-forward org-target-link-regexp limit t)
  4928. (progn
  4929. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4930. (add-text-properties (match-beginning 0) (match-end 0)
  4931. (list 'mouse-face 'highlight
  4932. 'keymap org-mouse-map
  4933. 'help-echo "Radio target link"
  4934. 'org-linked-text t))
  4935. (org-rear-nonsticky-at (match-end 0))
  4936. t)))))
  4937. (defun org-update-radio-target-regexp ()
  4938. "Find all radio targets in this file and update the regular expression."
  4939. (interactive)
  4940. (when (memq 'radio org-activate-links)
  4941. (setq org-target-link-regexp
  4942. (org-make-target-link-regexp (org-all-targets 'radio)))
  4943. (org-restart-font-lock)))
  4944. (defun org-hide-wide-columns (limit)
  4945. (let (s e)
  4946. (setq s (text-property-any (point) (or limit (point-max))
  4947. 'org-cwidth t))
  4948. (when s
  4949. (setq e (next-single-property-change s 'org-cwidth))
  4950. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4951. (goto-char e)
  4952. t)))
  4953. (defvar org-latex-and-specials-regexp nil
  4954. "Regular expression for highlighting export special stuff.")
  4955. (defvar org-match-substring-regexp)
  4956. (defvar org-match-substring-with-braces-regexp)
  4957. ;; This should be with the exporter code, but we also use if for font-locking
  4958. (defconst org-export-html-special-string-regexps
  4959. '(("\\\\-" . "&shy;")
  4960. ("---\\([^-]\\)" . "&mdash;\\1")
  4961. ("--\\([^-]\\)" . "&ndash;\\1")
  4962. ("\\.\\.\\." . "&hellip;"))
  4963. "Regular expressions for special string conversion.")
  4964. (defun org-compute-latex-and-specials-regexp ()
  4965. "Compute regular expression for stuff treated specially by exporters."
  4966. (if (not org-highlight-latex-fragments-and-specials)
  4967. (org-set-local 'org-latex-and-specials-regexp nil)
  4968. (require 'org-exp)
  4969. (let*
  4970. ((matchers (plist-get org-format-latex-options :matchers))
  4971. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4972. org-latex-regexps)))
  4973. (org-export-allow-BIND nil)
  4974. (options (org-combine-plists (org-default-export-plist)
  4975. (org-infile-export-plist)))
  4976. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4977. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4978. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4979. (org-export-html-expand (plist-get options :expand-quoted-html))
  4980. (org-export-with-special-strings (plist-get options :special-strings))
  4981. (re-sub
  4982. (cond
  4983. ((equal org-export-with-sub-superscripts '{})
  4984. (list org-match-substring-with-braces-regexp))
  4985. (org-export-with-sub-superscripts
  4986. (list org-match-substring-regexp))))
  4987. (re-latex
  4988. (if org-export-with-LaTeX-fragments
  4989. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4990. (re-macros
  4991. (if org-export-with-TeX-macros
  4992. (list (concat "\\\\"
  4993. (regexp-opt
  4994. (append
  4995. (delq nil
  4996. (mapcar 'car-safe
  4997. (append org-entities-user
  4998. org-entities)))
  4999. (if (boundp 'org-latex-entities)
  5000. (mapcar (lambda (x)
  5001. (or (car-safe x) x))
  5002. org-latex-entities)
  5003. nil))
  5004. 'words))) ; FIXME
  5005. ))
  5006. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5007. (re-special (if org-export-with-special-strings
  5008. (mapcar (lambda (x) (car x))
  5009. org-export-html-special-string-regexps)))
  5010. (re-rest
  5011. (delq nil
  5012. (list
  5013. (if org-export-html-expand "@<[^>\n]+>")
  5014. ))))
  5015. (org-set-local
  5016. 'org-latex-and-specials-regexp
  5017. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5018. re-rest) "\\|")))))
  5019. (defun org-do-latex-and-special-faces (limit)
  5020. "Run through the buffer and add overlays to links."
  5021. (when org-latex-and-specials-regexp
  5022. (let (rtn d)
  5023. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5024. limit t))
  5025. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5026. 'face))
  5027. '(org-code org-verbatim underline)))
  5028. (progn
  5029. (setq rtn t
  5030. d (cond ((member (char-after (1+ (match-beginning 0)))
  5031. '(?_ ?^)) 1)
  5032. (t 0)))
  5033. (font-lock-prepend-text-property
  5034. (+ d (match-beginning 0)) (match-end 0)
  5035. 'face 'org-latex-and-export-specials)
  5036. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5037. '(font-lock-multiline t)))))
  5038. rtn)))
  5039. (defun org-restart-font-lock ()
  5040. "Restart `font-lock-mode', to force refontification."
  5041. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5042. (font-lock-mode -1)
  5043. (font-lock-mode 1)))
  5044. (defun org-all-targets (&optional radio)
  5045. "Return a list of all targets in this file.
  5046. With optional argument RADIO, only find radio targets."
  5047. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5048. rtn)
  5049. (save-excursion
  5050. (goto-char (point-min))
  5051. (while (re-search-forward re nil t)
  5052. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5053. rtn)))
  5054. (defun org-make-target-link-regexp (targets)
  5055. "Make regular expression matching all strings in TARGETS.
  5056. The regular expression finds the targets also if there is a line break
  5057. between words."
  5058. (and targets
  5059. (concat
  5060. "\\<\\("
  5061. (mapconcat
  5062. (lambda (x)
  5063. (setq x (regexp-quote x))
  5064. (while (string-match " +" x)
  5065. (setq x (replace-match "\\s-+" t t x)))
  5066. x)
  5067. targets
  5068. "\\|")
  5069. "\\)\\>")))
  5070. (defun org-activate-tags (limit)
  5071. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5072. (progn
  5073. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5074. (add-text-properties (match-beginning 1) (match-end 1)
  5075. (list 'mouse-face 'highlight
  5076. 'keymap org-mouse-map))
  5077. (org-rear-nonsticky-at (match-end 1))
  5078. t)))
  5079. (defun org-outline-level ()
  5080. "Compute the outline level of the heading at point.
  5081. This function assumes that the cursor is at the beginning of a line matched
  5082. by `outline-regexp'. Otherwise it returns garbage.
  5083. If this is called at a normal headline, the level is the number of stars.
  5084. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5085. (save-excursion
  5086. (looking-at org-outline-regexp)
  5087. (1- (- (match-end 0) (match-beginning 0)))))
  5088. (defvar org-font-lock-keywords nil)
  5089. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5090. "Regular expression matching a property line.")
  5091. (defvar org-font-lock-hook nil
  5092. "Functions to be called for special font lock stuff.")
  5093. (defvar org-font-lock-set-keywords-hook nil
  5094. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5095. This is called after `org-font-lock-extra-keywords' is defined, but before
  5096. it is installed to be used by font lock. This can be useful if something
  5097. needs to be inserted at a specific position in the font-lock sequence.")
  5098. (defun org-font-lock-hook (limit)
  5099. "Run `org-font-lock-hook' within LIMIT."
  5100. (run-hook-with-args 'org-font-lock-hook limit))
  5101. (defun org-set-font-lock-defaults ()
  5102. "Set font lock defaults for the current buffer."
  5103. (let* ((em org-fontify-emphasized-text)
  5104. (lk org-activate-links)
  5105. (org-font-lock-extra-keywords
  5106. (list
  5107. ;; Call the hook
  5108. '(org-font-lock-hook)
  5109. ;; Headlines
  5110. `(,(if org-fontify-whole-heading-line
  5111. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5112. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5113. (1 (org-get-level-face 1))
  5114. (2 (org-get-level-face 2))
  5115. (3 (org-get-level-face 3)))
  5116. ;; Table lines
  5117. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5118. (1 'org-table t))
  5119. ;; Table internals
  5120. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5121. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5122. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5123. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5124. ;; Drawers
  5125. (list org-drawer-regexp '(0 'org-special-keyword t))
  5126. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5127. ;; Properties
  5128. (list org-property-re
  5129. '(1 'org-special-keyword t)
  5130. '(3 'org-property-value t))
  5131. ;; Links
  5132. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5133. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5134. (if (memq 'plain lk) '(org-activate-plain-links))
  5135. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5136. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5137. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5138. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5139. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5140. '(org-hide-wide-columns (0 nil append))
  5141. ;; TODO keyword
  5142. (list (format org-heading-keyword-regexp-format
  5143. org-todo-regexp)
  5144. '(2 (org-get-todo-face 2) t))
  5145. ;; DONE
  5146. (if org-fontify-done-headline
  5147. (list (format org-heading-keyword-regexp-format
  5148. (concat
  5149. "\\(?:"
  5150. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5151. "\\)"))
  5152. '(2 'org-headline-done t))
  5153. nil)
  5154. ;; Priorities
  5155. '(org-font-lock-add-priority-faces)
  5156. ;; Tags
  5157. '(org-font-lock-add-tag-faces)
  5158. ;; Special keywords
  5159. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5160. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5161. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5162. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5163. ;; Emphasis
  5164. (if em
  5165. (if (featurep 'xemacs)
  5166. '(org-do-emphasis-faces (0 nil append))
  5167. '(org-do-emphasis-faces)))
  5168. ;; Checkboxes
  5169. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5170. 1 'org-checkbox prepend)
  5171. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5172. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5173. (0 (org-get-checkbox-statistics-face) t)))
  5174. ;; Description list items
  5175. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5176. 1 'org-list-dt prepend)
  5177. ;; ARCHIVEd headings
  5178. (list (concat
  5179. org-outline-regexp-bol
  5180. "\\(.*:" org-archive-tag ":.*\\)")
  5181. '(1 'org-archived prepend))
  5182. ;; Specials
  5183. '(org-do-latex-and-special-faces)
  5184. '(org-fontify-entities)
  5185. '(org-raise-scripts)
  5186. ;; Code
  5187. '(org-activate-code (1 'org-code t))
  5188. ;; COMMENT
  5189. (list (format org-heading-keyword-regexp-format
  5190. (concat "\\("
  5191. org-comment-string "\\|" org-quote-string
  5192. "\\)"))
  5193. '(2 'org-special-keyword t))
  5194. ;; Blocks and meta lines
  5195. '(org-fontify-meta-lines-and-blocks)
  5196. )))
  5197. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5198. (run-hooks 'org-font-lock-set-keywords-hook)
  5199. ;; Now set the full font-lock-keywords
  5200. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5201. (org-set-local 'font-lock-defaults
  5202. '(org-font-lock-keywords t nil nil backward-paragraph))
  5203. (kill-local-variable 'font-lock-keywords) nil))
  5204. (defun org-toggle-pretty-entities ()
  5205. "Toggle the composition display of entities as UTF8 characters."
  5206. (interactive)
  5207. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5208. (org-restart-font-lock)
  5209. (if org-pretty-entities
  5210. (message "Entities are displayed as UTF8 characters")
  5211. (save-restriction
  5212. (widen)
  5213. (org-decompose-region (point-min) (point-max))
  5214. (message "Entities are displayed plain"))))
  5215. (defvar org-custom-properties-overlays nil
  5216. "List of overlays used for custom properties.")
  5217. (make-variable-buffer-local 'org-custom-properties-overlays)
  5218. (defun org-toggle-custom-properties-visibility ()
  5219. "Display or hide properties in `org-custom-properties'."
  5220. (interactive)
  5221. (if org-custom-properties-overlays
  5222. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5223. (setq org-custom-properties-overlays nil))
  5224. (unless (not org-custom-properties)
  5225. (save-excursion
  5226. (save-restriction
  5227. (widen)
  5228. (goto-char (point-min))
  5229. (while (re-search-forward org-property-re nil t)
  5230. (mapc (lambda(p)
  5231. (when (equal p (substring (match-string 1) 1 -1))
  5232. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5233. (overlay-put o 'invisible t)
  5234. (overlay-put o 'org-custom-property t)
  5235. (push o org-custom-properties-overlays))))
  5236. org-custom-properties)))))))
  5237. (defun org-fontify-entities (limit)
  5238. "Find an entity to fontify."
  5239. (let (ee)
  5240. (when org-pretty-entities
  5241. (catch 'match
  5242. (while (re-search-forward
  5243. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5244. limit t)
  5245. (if (and (not (org-in-indented-comment-line))
  5246. (setq ee (org-entity-get (match-string 1)))
  5247. (= (length (nth 6 ee)) 1))
  5248. (let*
  5249. ((end (if (equal (match-string 2) "{}")
  5250. (match-end 2)
  5251. (match-end 1))))
  5252. (add-text-properties
  5253. (match-beginning 0) end
  5254. (list 'font-lock-fontified t))
  5255. (compose-region (match-beginning 0) end
  5256. (nth 6 ee) nil)
  5257. (backward-char 1)
  5258. (throw 'match t))))
  5259. nil))))
  5260. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5261. "Fontify string S like in Org-mode."
  5262. (with-temp-buffer
  5263. (insert s)
  5264. (let ((org-odd-levels-only odd-levels))
  5265. (org-mode)
  5266. (font-lock-fontify-buffer)
  5267. (buffer-string))))
  5268. (defvar org-m nil)
  5269. (defvar org-l nil)
  5270. (defvar org-f nil)
  5271. (defun org-get-level-face (n)
  5272. "Get the right face for match N in font-lock matching of headlines."
  5273. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5274. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5275. (if org-cycle-level-faces
  5276. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5277. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5278. (cond
  5279. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5280. ((eq n 2) org-f)
  5281. (t (if org-level-color-stars-only nil org-f))))
  5282. (defun org-get-todo-face (kwd)
  5283. "Get the right face for a TODO keyword KWD.
  5284. If KWD is a number, get the corresponding match group."
  5285. (if (numberp kwd) (setq kwd (match-string kwd)))
  5286. (or (org-face-from-face-or-color
  5287. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5288. (and (member kwd org-done-keywords) 'org-done)
  5289. 'org-todo))
  5290. (defun org-face-from-face-or-color (context inherit face-or-color)
  5291. "Create a face list that inherits INHERIT, but sets the foreground color.
  5292. When FACE-OR-COLOR is not a string, just return it."
  5293. (if (stringp face-or-color)
  5294. (list :inherit inherit
  5295. (cdr (assoc context org-faces-easy-properties))
  5296. face-or-color)
  5297. face-or-color))
  5298. (defun org-font-lock-add-tag-faces (limit)
  5299. "Add the special tag faces."
  5300. (when (and org-tag-faces org-tags-special-faces-re)
  5301. (while (re-search-forward org-tags-special-faces-re limit t)
  5302. (add-text-properties (match-beginning 1) (match-end 1)
  5303. (list 'face (org-get-tag-face 1)
  5304. 'font-lock-fontified t))
  5305. (backward-char 1))))
  5306. (defun org-font-lock-add-priority-faces (limit)
  5307. "Add the special priority faces."
  5308. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5309. (when (save-match-data (org-at-heading-p))
  5310. (add-text-properties
  5311. (match-beginning 0) (match-end 0)
  5312. (list 'face (or (org-face-from-face-or-color
  5313. 'priority 'org-special-keyword
  5314. (cdr (assoc (char-after (match-beginning 1))
  5315. org-priority-faces)))
  5316. 'org-special-keyword)
  5317. 'font-lock-fontified t)))))
  5318. (defun org-get-tag-face (kwd)
  5319. "Get the right face for a TODO keyword KWD.
  5320. If KWD is a number, get the corresponding match group."
  5321. (if (numberp kwd) (setq kwd (match-string kwd)))
  5322. (or (org-face-from-face-or-color
  5323. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5324. 'org-tag))
  5325. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5326. "Remove fontification and activation overlays from links."
  5327. (font-lock-default-unfontify-region beg end)
  5328. (let* ((buffer-undo-list t)
  5329. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5330. (inhibit-modification-hooks t)
  5331. deactivate-mark buffer-file-name buffer-file-truename)
  5332. (org-decompose-region beg end)
  5333. (remove-text-properties beg end
  5334. '(mouse-face t keymap t org-linked-text t
  5335. invisible t intangible t
  5336. org-no-flyspell t org-emphasis t))
  5337. (org-remove-font-lock-display-properties beg end)))
  5338. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5339. ((raise 0.3) (height 0.7))
  5340. ((raise -0.5))
  5341. ((raise 0.5)))
  5342. "Display properties for showing superscripts and subscripts.")
  5343. (defun org-remove-font-lock-display-properties (beg end)
  5344. "Remove specific display properties that have been added by font lock.
  5345. The will remove the raise properties that are used to show superscripts
  5346. and subscripts."
  5347. (let (next prop)
  5348. (while (< beg end)
  5349. (setq next (next-single-property-change beg 'display nil end)
  5350. prop (get-text-property beg 'display))
  5351. (if (member prop org-script-display)
  5352. (put-text-property beg next 'display nil))
  5353. (setq beg next))))
  5354. (defun org-raise-scripts (limit)
  5355. "Add raise properties to sub/superscripts."
  5356. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5357. (if (re-search-forward
  5358. (if (eq org-use-sub-superscripts t)
  5359. org-match-substring-regexp
  5360. org-match-substring-with-braces-regexp)
  5361. limit t)
  5362. (let* ((pos (point)) table-p comment-p
  5363. (mpos (match-beginning 3))
  5364. (emph-p (get-text-property mpos 'org-emphasis))
  5365. (link-p (get-text-property mpos 'mouse-face))
  5366. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5367. (goto-char (point-at-bol))
  5368. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5369. comment-p (org-looking-at-p "[ \t]*#"))
  5370. (goto-char pos)
  5371. ;; FIXME: Should we go back one character here, for a_b^c
  5372. ;; (goto-char (1- pos)) ;????????????????????
  5373. (if (or comment-p emph-p link-p keyw-p)
  5374. t
  5375. (put-text-property (match-beginning 3) (match-end 0)
  5376. 'display
  5377. (if (equal (char-after (match-beginning 2)) ?^)
  5378. (nth (if table-p 3 1) org-script-display)
  5379. (nth (if table-p 2 0) org-script-display)))
  5380. (add-text-properties (match-beginning 2) (match-end 2)
  5381. (list 'invisible t
  5382. 'org-dwidth t 'org-dwidth-n 1))
  5383. (if (and (eq (char-after (match-beginning 3)) ?{)
  5384. (eq (char-before (match-end 3)) ?}))
  5385. (progn
  5386. (add-text-properties
  5387. (match-beginning 3) (1+ (match-beginning 3))
  5388. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5389. (add-text-properties
  5390. (1- (match-end 3)) (match-end 3)
  5391. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5392. t)))))
  5393. ;;;; Visibility cycling, including org-goto and indirect buffer
  5394. ;;; Cycling
  5395. (defvar org-cycle-global-status nil)
  5396. (make-variable-buffer-local 'org-cycle-global-status)
  5397. (defvar org-cycle-subtree-status nil)
  5398. (make-variable-buffer-local 'org-cycle-subtree-status)
  5399. (defvar org-inlinetask-min-level)
  5400. ;;;###autoload
  5401. (defun org-cycle (&optional arg)
  5402. "TAB-action and visibility cycling for Org-mode.
  5403. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5404. is outline visibility cycling, but it also invokes other actions
  5405. in special contexts.
  5406. - When this function is called with a prefix argument, rotate the entire
  5407. buffer through 3 states (global cycling)
  5408. 1. OVERVIEW: Show only top-level headlines.
  5409. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5410. 3. SHOW ALL: Show everything.
  5411. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5412. determined by the variable `org-startup-folded', and by any VISIBILITY
  5413. properties in the buffer.
  5414. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5415. including any drawers.
  5416. - When inside a table, re-align the table and move to the next field.
  5417. - When point is at the beginning of a headline, rotate the subtree started
  5418. by this line through 3 different states (local cycling)
  5419. 1. FOLDED: Only the main headline is shown.
  5420. 2. CHILDREN: The main headline and the direct children are shown.
  5421. From this state, you can move to one of the children
  5422. and zoom in further.
  5423. 3. SUBTREE: Show the entire subtree, including body text.
  5424. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5425. - When point is at the beginning of an empty headline and the variable
  5426. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5427. of the headline by demoting and promoting it to likely levels. This
  5428. speeds up creation document structure by pressing TAB once or several
  5429. times right after creating a new headline.
  5430. - When there is a numeric prefix, go up to a heading with level ARG, do
  5431. a `show-subtree' and return to the previous cursor position. If ARG
  5432. is negative, go up that many levels.
  5433. - When point is not at the beginning of a headline, execute the global
  5434. binding for TAB, which is re-indenting the line. See the option
  5435. `org-cycle-emulate-tab' for details.
  5436. - Special case: if point is at the beginning of the buffer and there is
  5437. no headline in line 1, this function will act as if called with prefix arg
  5438. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5439. But only if also the variable `org-cycle-global-at-bob' is t."
  5440. (interactive "P")
  5441. (org-load-modules-maybe)
  5442. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5443. (and org-cycle-level-after-item/entry-creation
  5444. (or (org-cycle-level)
  5445. (org-cycle-item-indentation))))
  5446. (let* ((limit-level
  5447. (or org-cycle-max-level
  5448. (and (boundp 'org-inlinetask-min-level)
  5449. org-inlinetask-min-level
  5450. (1- org-inlinetask-min-level))))
  5451. (nstars (and limit-level
  5452. (if org-odd-levels-only
  5453. (and limit-level (1- (* limit-level 2)))
  5454. limit-level)))
  5455. (org-outline-regexp
  5456. (if (not (derived-mode-p 'org-mode))
  5457. outline-regexp
  5458. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5459. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5460. (not (looking-at org-outline-regexp))))
  5461. (org-cycle-hook
  5462. (if bob-special
  5463. (delq 'org-optimize-window-after-visibility-change
  5464. (copy-sequence org-cycle-hook))
  5465. org-cycle-hook))
  5466. (pos (point)))
  5467. (if (or bob-special (equal arg '(4)))
  5468. ;; special case: use global cycling
  5469. (setq arg t))
  5470. (cond
  5471. ((equal arg '(16))
  5472. (setq last-command 'dummy)
  5473. (org-set-startup-visibility)
  5474. (message "Startup visibility, plus VISIBILITY properties"))
  5475. ((equal arg '(64))
  5476. (show-all)
  5477. (message "Entire buffer visible, including drawers"))
  5478. ;; Table: enter it or move to the next field.
  5479. ((org-at-table-p 'any)
  5480. (if (org-at-table.el-p)
  5481. (message "Use C-c ' to edit table.el tables")
  5482. (if arg (org-table-edit-field t)
  5483. (org-table-justify-field-maybe)
  5484. (call-interactively 'org-table-next-field))))
  5485. ((run-hook-with-args-until-success
  5486. 'org-tab-after-check-for-table-hook))
  5487. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5488. ((eq arg t) (org-cycle-internal-global))
  5489. ;; Drawers: delegate to `org-flag-drawer'.
  5490. ((and org-drawers org-drawer-regexp
  5491. (save-excursion
  5492. (beginning-of-line 1)
  5493. (looking-at org-drawer-regexp)))
  5494. (org-flag-drawer ; toggle block visibility
  5495. (not (get-char-property (match-end 0) 'invisible))))
  5496. ;; Show-subtree, ARG levels up from here.
  5497. ((integerp arg)
  5498. (save-excursion
  5499. (org-back-to-heading)
  5500. (outline-up-heading (if (< arg 0) (- arg)
  5501. (- (funcall outline-level) arg)))
  5502. (org-show-subtree)))
  5503. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5504. ((and (featurep 'org-inlinetask)
  5505. (org-inlinetask-at-task-p)
  5506. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5507. (org-inlinetask-toggle-visibility))
  5508. ((org-try-cdlatex-tab))
  5509. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5510. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5511. (save-excursion (beginning-of-line 1)
  5512. (looking-at org-outline-regexp)))
  5513. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5514. (org-cycle-internal-local))
  5515. ;; From there: TAB emulation and template completion.
  5516. (buffer-read-only (org-back-to-heading))
  5517. ((run-hook-with-args-until-success
  5518. 'org-tab-after-check-for-cycling-hook))
  5519. ((org-try-structure-completion))
  5520. ((run-hook-with-args-until-success
  5521. 'org-tab-before-tab-emulation-hook))
  5522. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5523. (or (not (bolp))
  5524. (not (looking-at org-outline-regexp))))
  5525. (call-interactively (global-key-binding "\t")))
  5526. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5527. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5528. (or (and (eq org-cycle-emulate-tab 'white)
  5529. (= (match-end 0) (point-at-eol)))
  5530. (and (eq org-cycle-emulate-tab 'whitestart)
  5531. (>= (match-end 0) pos))))
  5532. t
  5533. (eq org-cycle-emulate-tab t))
  5534. (call-interactively (global-key-binding "\t")))
  5535. (t (save-excursion
  5536. (org-back-to-heading)
  5537. (org-cycle)))))))
  5538. (defun org-cycle-internal-global ()
  5539. "Do the global cycling action."
  5540. ;; Hack to avoid display of messages for .org attachments in Gnus
  5541. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5542. (cond
  5543. ((and (eq last-command this-command)
  5544. (eq org-cycle-global-status 'overview))
  5545. ;; We just created the overview - now do table of contents
  5546. ;; This can be slow in very large buffers, so indicate action
  5547. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5548. (unless ga (message "CONTENTS..."))
  5549. (org-content)
  5550. (unless ga (message "CONTENTS...done"))
  5551. (setq org-cycle-global-status 'contents)
  5552. (run-hook-with-args 'org-cycle-hook 'contents))
  5553. ((and (eq last-command this-command)
  5554. (eq org-cycle-global-status 'contents))
  5555. ;; We just showed the table of contents - now show everything
  5556. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5557. (show-all)
  5558. (unless ga (message "SHOW ALL"))
  5559. (setq org-cycle-global-status 'all)
  5560. (run-hook-with-args 'org-cycle-hook 'all))
  5561. (t
  5562. ;; Default action: go to overview
  5563. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5564. (org-overview)
  5565. (unless ga (message "OVERVIEW"))
  5566. (setq org-cycle-global-status 'overview)
  5567. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5568. (defun org-cycle-internal-local ()
  5569. "Do the local cycling action."
  5570. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5571. ;; First, determine end of headline (EOH), end of subtree or item
  5572. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5573. (save-excursion
  5574. (if (org-at-item-p)
  5575. (progn
  5576. (beginning-of-line)
  5577. (setq struct (org-list-struct))
  5578. (setq eoh (point-at-eol))
  5579. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5580. (setq has-children (org-list-has-child-p (point) struct)))
  5581. (org-back-to-heading)
  5582. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5583. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5584. (setq has-children
  5585. (or (save-excursion
  5586. (let ((level (funcall outline-level)))
  5587. (outline-next-heading)
  5588. (and (org-at-heading-p t)
  5589. (> (funcall outline-level) level))))
  5590. (save-excursion
  5591. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5592. ;; Determine end invisible part of buffer (EOL)
  5593. (beginning-of-line 2)
  5594. ;; XEmacs doesn't have `next-single-char-property-change'
  5595. (if (featurep 'xemacs)
  5596. (while (and (not (eobp)) ;; this is like `next-line'
  5597. (get-char-property (1- (point)) 'invisible))
  5598. (beginning-of-line 2))
  5599. (while (and (not (eobp)) ;; this is like `next-line'
  5600. (get-char-property (1- (point)) 'invisible))
  5601. (goto-char (next-single-char-property-change (point) 'invisible))
  5602. (and (eolp) (beginning-of-line 2))))
  5603. (setq eol (point)))
  5604. ;; Find out what to do next and set `this-command'
  5605. (cond
  5606. ((= eos eoh)
  5607. ;; Nothing is hidden behind this heading
  5608. (unless (org-before-first-heading-p)
  5609. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5610. (message "EMPTY ENTRY")
  5611. (setq org-cycle-subtree-status nil)
  5612. (save-excursion
  5613. (goto-char eos)
  5614. (outline-next-heading)
  5615. (if (outline-invisible-p) (org-flag-heading nil))))
  5616. ((and (or (>= eol eos)
  5617. (not (string-match "\\S-" (buffer-substring eol eos))))
  5618. (or has-children
  5619. (not (setq children-skipped
  5620. org-cycle-skip-children-state-if-no-children))))
  5621. ;; Entire subtree is hidden in one line: children view
  5622. (unless (org-before-first-heading-p)
  5623. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5624. (if (org-at-item-p)
  5625. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5626. (org-show-entry)
  5627. (org-with-limited-levels (show-children))
  5628. ;; FIXME: This slows down the func way too much.
  5629. ;; How keep drawers hidden in subtree anyway?
  5630. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5631. ;; (org-cycle-hide-drawers 'subtree))
  5632. ;; Fold every list in subtree to top-level items.
  5633. (when (eq org-cycle-include-plain-lists 'integrate)
  5634. (save-excursion
  5635. (org-back-to-heading)
  5636. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5637. (beginning-of-line 1)
  5638. (let* ((struct (org-list-struct))
  5639. (prevs (org-list-prevs-alist struct))
  5640. (end (org-list-get-bottom-point struct)))
  5641. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5642. (org-list-get-all-items (point) struct prevs))
  5643. (goto-char end))))))
  5644. (message "CHILDREN")
  5645. (save-excursion
  5646. (goto-char eos)
  5647. (outline-next-heading)
  5648. (if (outline-invisible-p) (org-flag-heading nil)))
  5649. (setq org-cycle-subtree-status 'children)
  5650. (unless (org-before-first-heading-p)
  5651. (run-hook-with-args 'org-cycle-hook 'children)))
  5652. ((or children-skipped
  5653. (and (eq last-command this-command)
  5654. (eq org-cycle-subtree-status 'children)))
  5655. ;; We just showed the children, or no children are there,
  5656. ;; now show everything.
  5657. (unless (org-before-first-heading-p)
  5658. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5659. (outline-flag-region eoh eos nil)
  5660. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5661. (setq org-cycle-subtree-status 'subtree)
  5662. (unless (org-before-first-heading-p)
  5663. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5664. (t
  5665. ;; Default action: hide the subtree.
  5666. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5667. (outline-flag-region eoh eos t)
  5668. (message "FOLDED")
  5669. (setq org-cycle-subtree-status 'folded)
  5670. (unless (org-before-first-heading-p)
  5671. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5672. ;;;###autoload
  5673. (defun org-global-cycle (&optional arg)
  5674. "Cycle the global visibility. For details see `org-cycle'.
  5675. With \\[universal-argument] prefix arg, switch to startup visibility.
  5676. With a numeric prefix, show all headlines up to that level."
  5677. (interactive "P")
  5678. (let ((org-cycle-include-plain-lists
  5679. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5680. (cond
  5681. ((integerp arg)
  5682. (show-all)
  5683. (hide-sublevels arg)
  5684. (setq org-cycle-global-status 'contents))
  5685. ((equal arg '(4))
  5686. (org-set-startup-visibility)
  5687. (message "Startup visibility, plus VISIBILITY properties."))
  5688. (t
  5689. (org-cycle '(4))))))
  5690. (defun org-set-startup-visibility ()
  5691. "Set the visibility required by startup options and properties."
  5692. (cond
  5693. ((eq org-startup-folded t)
  5694. (org-cycle '(4)))
  5695. ((eq org-startup-folded 'content)
  5696. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5697. (org-cycle '(4)) (org-cycle '(4)))))
  5698. (unless (eq org-startup-folded 'showeverything)
  5699. (if org-hide-block-startup (org-hide-block-all))
  5700. (org-set-visibility-according-to-property 'no-cleanup)
  5701. (org-cycle-hide-archived-subtrees 'all)
  5702. (org-cycle-hide-drawers 'all)
  5703. (org-cycle-show-empty-lines t)))
  5704. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5705. "Switch subtree visibilities according to :VISIBILITY: property."
  5706. (interactive)
  5707. (let (org-show-entry-below state)
  5708. (save-excursion
  5709. (goto-char (point-min))
  5710. (while (re-search-forward
  5711. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5712. nil t)
  5713. (setq state (match-string 1))
  5714. (save-excursion
  5715. (org-back-to-heading t)
  5716. (hide-subtree)
  5717. (org-reveal)
  5718. (cond
  5719. ((equal state '("fold" "folded"))
  5720. (hide-subtree))
  5721. ((equal state "children")
  5722. (org-show-hidden-entry)
  5723. (show-children))
  5724. ((equal state "content")
  5725. (save-excursion
  5726. (save-restriction
  5727. (org-narrow-to-subtree)
  5728. (org-content))))
  5729. ((member state '("all" "showall"))
  5730. (show-subtree)))))
  5731. (unless no-cleanup
  5732. (org-cycle-hide-archived-subtrees 'all)
  5733. (org-cycle-hide-drawers 'all)
  5734. (org-cycle-show-empty-lines 'all)))))
  5735. ;; This function uses outline-regexp instead of the more fundamental
  5736. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5737. ;; buffers, where outline-regexp is needed.
  5738. (defun org-overview ()
  5739. "Switch to overview mode, showing only top-level headlines.
  5740. Really, this shows all headlines with level equal or greater than the level
  5741. of the first headline in the buffer. This is important, because if the
  5742. first headline is not level one, then (hide-sublevels 1) gives confusing
  5743. results."
  5744. (interactive)
  5745. (let ((l (org-current-line))
  5746. (level (save-excursion
  5747. (goto-char (point-min))
  5748. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5749. (progn
  5750. (goto-char (match-beginning 0))
  5751. (funcall outline-level))))))
  5752. (and level (hide-sublevels level))
  5753. (recenter '(4))
  5754. (org-goto-line l)))
  5755. (defun org-content (&optional arg)
  5756. "Show all headlines in the buffer, like a table of contents.
  5757. With numerical argument N, show content up to level N."
  5758. (interactive "P")
  5759. (save-excursion
  5760. ;; Visit all headings and show their offspring
  5761. (and (integerp arg) (org-overview))
  5762. (goto-char (point-max))
  5763. (catch 'exit
  5764. (while (and (progn (condition-case nil
  5765. (outline-previous-visible-heading 1)
  5766. (error (goto-char (point-min))))
  5767. t)
  5768. (looking-at org-outline-regexp))
  5769. (if (integerp arg)
  5770. (show-children (1- arg))
  5771. (show-branches))
  5772. (if (bobp) (throw 'exit nil))))))
  5773. (defun org-optimize-window-after-visibility-change (state)
  5774. "Adjust the window after a change in outline visibility.
  5775. This function is the default value of the hook `org-cycle-hook'."
  5776. (when (get-buffer-window (current-buffer))
  5777. (cond
  5778. ((eq state 'content) nil)
  5779. ((eq state 'all) nil)
  5780. ((eq state 'folded) nil)
  5781. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5782. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5783. (defun org-remove-empty-overlays-at (pos)
  5784. "Remove outline overlays that do not contain non-white stuff."
  5785. (mapc
  5786. (lambda (o)
  5787. (and (eq 'outline (overlay-get o 'invisible))
  5788. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5789. (overlay-end o))))
  5790. (delete-overlay o)))
  5791. (overlays-at pos)))
  5792. (defun org-clean-visibility-after-subtree-move ()
  5793. "Fix visibility issues after moving a subtree."
  5794. ;; First, find a reasonable region to look at:
  5795. ;; Start two siblings above, end three below
  5796. (let* ((beg (save-excursion
  5797. (and (org-get-last-sibling)
  5798. (org-get-last-sibling))
  5799. (point)))
  5800. (end (save-excursion
  5801. (and (org-get-next-sibling)
  5802. (org-get-next-sibling)
  5803. (org-get-next-sibling))
  5804. (if (org-at-heading-p)
  5805. (point-at-eol)
  5806. (point))))
  5807. (level (looking-at "\\*+"))
  5808. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5809. (save-excursion
  5810. (save-restriction
  5811. (narrow-to-region beg end)
  5812. (when re
  5813. ;; Properly fold already folded siblings
  5814. (goto-char (point-min))
  5815. (while (re-search-forward re nil t)
  5816. (if (and (not (outline-invisible-p))
  5817. (save-excursion
  5818. (goto-char (point-at-eol)) (outline-invisible-p)))
  5819. (hide-entry))))
  5820. (org-cycle-show-empty-lines 'overview)
  5821. (org-cycle-hide-drawers 'overview)))))
  5822. (defun org-cycle-show-empty-lines (state)
  5823. "Show empty lines above all visible headlines.
  5824. The region to be covered depends on STATE when called through
  5825. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5826. entire buffer covered. Note that an empty line is only shown if there
  5827. are at least `org-cycle-separator-lines' empty lines before the headline."
  5828. (when (not (= org-cycle-separator-lines 0))
  5829. (save-excursion
  5830. (let* ((n (abs org-cycle-separator-lines))
  5831. (re (cond
  5832. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5833. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5834. (t (let ((ns (number-to-string (- n 2))))
  5835. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5836. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5837. beg end b e)
  5838. (cond
  5839. ((memq state '(overview contents t))
  5840. (setq beg (point-min) end (point-max)))
  5841. ((memq state '(children folded))
  5842. (setq beg (point) end (progn (org-end-of-subtree t t)
  5843. (beginning-of-line 2)
  5844. (point)))))
  5845. (when beg
  5846. (goto-char beg)
  5847. (while (re-search-forward re end t)
  5848. (unless (get-char-property (match-end 1) 'invisible)
  5849. (setq e (match-end 1))
  5850. (if (< org-cycle-separator-lines 0)
  5851. (setq b (save-excursion
  5852. (goto-char (match-beginning 0))
  5853. (org-back-over-empty-lines)
  5854. (if (save-excursion
  5855. (goto-char (max (point-min) (1- (point))))
  5856. (org-at-heading-p))
  5857. (1- (point))
  5858. (point))))
  5859. (setq b (match-beginning 1)))
  5860. (outline-flag-region b e nil)))))))
  5861. ;; Never hide empty lines at the end of the file.
  5862. (save-excursion
  5863. (goto-char (point-max))
  5864. (outline-previous-heading)
  5865. (outline-end-of-heading)
  5866. (if (and (looking-at "[ \t\n]+")
  5867. (= (match-end 0) (point-max)))
  5868. (outline-flag-region (point) (match-end 0) nil))))
  5869. (defun org-show-empty-lines-in-parent ()
  5870. "Move to the parent and re-show empty lines before visible headlines."
  5871. (save-excursion
  5872. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5873. (org-cycle-show-empty-lines context))))
  5874. (defun org-files-list ()
  5875. "Return `org-agenda-files' list, plus all open org-mode files.
  5876. This is useful for operations that need to scan all of a user's
  5877. open and agenda-wise Org files."
  5878. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5879. (dolist (buf (buffer-list))
  5880. (with-current-buffer buf
  5881. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  5882. (let ((file (expand-file-name (buffer-file-name))))
  5883. (unless (member file files)
  5884. (push file files))))))
  5885. files))
  5886. (defsubst org-entry-beginning-position ()
  5887. "Return the beginning position of the current entry."
  5888. (save-excursion (outline-back-to-heading t) (point)))
  5889. (defsubst org-entry-end-position ()
  5890. "Return the end position of the current entry."
  5891. (save-excursion (outline-next-heading) (point)))
  5892. (defun org-cycle-hide-drawers (state)
  5893. "Re-hide all drawers after a visibility state change."
  5894. (when (and (derived-mode-p 'org-mode)
  5895. (not (memq state '(overview folded contents))))
  5896. (save-excursion
  5897. (let* ((globalp (memq state '(contents all)))
  5898. (beg (if globalp (point-min) (point)))
  5899. (end (if globalp (point-max)
  5900. (if (eq state 'children)
  5901. (save-excursion (outline-next-heading) (point))
  5902. (org-end-of-subtree t)))))
  5903. (goto-char beg)
  5904. (while (re-search-forward org-drawer-regexp end t)
  5905. (org-flag-drawer t))))))
  5906. (defun org-flag-drawer (flag)
  5907. "When FLAG is non-nil, hide the drawer we are within.
  5908. Otherwise make it visible."
  5909. (save-excursion
  5910. (beginning-of-line 1)
  5911. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5912. (let ((b (match-end 0)))
  5913. (if (re-search-forward
  5914. "^[ \t]*:END:"
  5915. (save-excursion (outline-next-heading) (point)) t)
  5916. (outline-flag-region b (point-at-eol) flag)
  5917. (error ":END: line missing at position %s" b))))))
  5918. (defun org-subtree-end-visible-p ()
  5919. "Is the end of the current subtree visible?"
  5920. (pos-visible-in-window-p
  5921. (save-excursion (org-end-of-subtree t) (point))))
  5922. (defun org-first-headline-recenter (&optional N)
  5923. "Move cursor to the first headline and recenter the headline.
  5924. Optional argument N means put the headline into the Nth line of the window."
  5925. (goto-char (point-min))
  5926. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  5927. (beginning-of-line)
  5928. (recenter (prefix-numeric-value N))))
  5929. ;;; Saving and restoring visibility
  5930. (defun org-outline-overlay-data (&optional use-markers)
  5931. "Return a list of the locations of all outline overlays.
  5932. These are overlays with the `invisible' property value `outline'.
  5933. The return value is a list of cons cells, with start and stop
  5934. positions for each overlay.
  5935. If USE-MARKERS is set, return the positions as markers."
  5936. (let (beg end)
  5937. (save-excursion
  5938. (save-restriction
  5939. (widen)
  5940. (delq nil
  5941. (mapcar (lambda (o)
  5942. (when (eq (overlay-get o 'invisible) 'outline)
  5943. (setq beg (overlay-start o)
  5944. end (overlay-end o))
  5945. (and beg end (> end beg)
  5946. (if use-markers
  5947. (cons (move-marker (make-marker) beg)
  5948. (move-marker (make-marker) end))
  5949. (cons beg end)))))
  5950. (overlays-in (point-min) (point-max))))))))
  5951. (defun org-set-outline-overlay-data (data)
  5952. "Create visibility overlays for all positions in DATA.
  5953. DATA should have been made by `org-outline-overlay-data'."
  5954. (let (o)
  5955. (save-excursion
  5956. (save-restriction
  5957. (widen)
  5958. (show-all)
  5959. (mapc (lambda (c)
  5960. (outline-flag-region (car c) (cdr c) t))
  5961. data)))))
  5962. ;;; Folding of blocks
  5963. (defvar org-hide-block-overlays nil
  5964. "Overlays hiding blocks.")
  5965. (make-variable-buffer-local 'org-hide-block-overlays)
  5966. (defun org-block-map (function &optional start end)
  5967. "Call FUNCTION at the head of all source blocks in the current buffer.
  5968. Optional arguments START and END can be used to limit the range."
  5969. (let ((start (or start (point-min)))
  5970. (end (or end (point-max))))
  5971. (save-excursion
  5972. (goto-char start)
  5973. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5974. (save-excursion
  5975. (save-match-data
  5976. (goto-char (match-beginning 0))
  5977. (funcall function)))))))
  5978. (defun org-hide-block-toggle-all ()
  5979. "Toggle the visibility of all blocks in the current buffer."
  5980. (org-block-map #'org-hide-block-toggle))
  5981. (defun org-hide-block-all ()
  5982. "Fold all blocks in the current buffer."
  5983. (interactive)
  5984. (org-show-block-all)
  5985. (org-block-map #'org-hide-block-toggle-maybe))
  5986. (defun org-show-block-all ()
  5987. "Unfold all blocks in the current buffer."
  5988. (interactive)
  5989. (mapc 'delete-overlay org-hide-block-overlays)
  5990. (setq org-hide-block-overlays nil))
  5991. (defun org-hide-block-toggle-maybe ()
  5992. "Toggle visibility of block at point."
  5993. (interactive)
  5994. (let ((case-fold-search t))
  5995. (if (save-excursion
  5996. (beginning-of-line 1)
  5997. (looking-at org-block-regexp))
  5998. (progn (org-hide-block-toggle)
  5999. t) ;; to signal that we took action
  6000. nil))) ;; to signal that we did not
  6001. (defun org-hide-block-toggle (&optional force)
  6002. "Toggle the visibility of the current block."
  6003. (interactive)
  6004. (save-excursion
  6005. (beginning-of-line)
  6006. (if (re-search-forward org-block-regexp nil t)
  6007. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6008. (end (match-end 0)) ;; end of entire body
  6009. ov)
  6010. (if (memq t (mapcar (lambda (overlay)
  6011. (eq (overlay-get overlay 'invisible)
  6012. 'org-hide-block))
  6013. (overlays-at start)))
  6014. (if (or (not force) (eq force 'off))
  6015. (mapc (lambda (ov)
  6016. (when (member ov org-hide-block-overlays)
  6017. (setq org-hide-block-overlays
  6018. (delq ov org-hide-block-overlays)))
  6019. (when (eq (overlay-get ov 'invisible)
  6020. 'org-hide-block)
  6021. (delete-overlay ov)))
  6022. (overlays-at start)))
  6023. (setq ov (make-overlay start end))
  6024. (overlay-put ov 'invisible 'org-hide-block)
  6025. ;; make the block accessible to isearch
  6026. (overlay-put
  6027. ov 'isearch-open-invisible
  6028. (lambda (ov)
  6029. (when (member ov org-hide-block-overlays)
  6030. (setq org-hide-block-overlays
  6031. (delq ov org-hide-block-overlays)))
  6032. (when (eq (overlay-get ov 'invisible)
  6033. 'org-hide-block)
  6034. (delete-overlay ov))))
  6035. (push ov org-hide-block-overlays)))
  6036. (error "Not looking at a source block"))))
  6037. ;; org-tab-after-check-for-cycling-hook
  6038. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6039. ;; Remove overlays when changing major mode
  6040. (add-hook 'org-mode-hook
  6041. (lambda () (org-add-hook 'change-major-mode-hook
  6042. 'org-show-block-all 'append 'local)))
  6043. ;;; Org-goto
  6044. (defvar org-goto-window-configuration nil)
  6045. (defvar org-goto-marker nil)
  6046. (defvar org-goto-map)
  6047. (defun org-goto-map ()
  6048. "Set the keymap `org-goto'."
  6049. (setq org-goto-map
  6050. (let ((map (make-sparse-keymap)))
  6051. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6052. mouse-drag-region universal-argument org-occur))
  6053. cmd)
  6054. (while (setq cmd (pop cmds))
  6055. (substitute-key-definition cmd cmd map global-map)))
  6056. (suppress-keymap map)
  6057. (org-defkey map "\C-m" 'org-goto-ret)
  6058. (org-defkey map [(return)] 'org-goto-ret)
  6059. (org-defkey map [(left)] 'org-goto-left)
  6060. (org-defkey map [(right)] 'org-goto-right)
  6061. (org-defkey map [(control ?g)] 'org-goto-quit)
  6062. (org-defkey map "\C-i" 'org-cycle)
  6063. (org-defkey map [(tab)] 'org-cycle)
  6064. (org-defkey map [(down)] 'outline-next-visible-heading)
  6065. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6066. (if org-goto-auto-isearch
  6067. (if (fboundp 'define-key-after)
  6068. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6069. nil)
  6070. (org-defkey map "q" 'org-goto-quit)
  6071. (org-defkey map "n" 'outline-next-visible-heading)
  6072. (org-defkey map "p" 'outline-previous-visible-heading)
  6073. (org-defkey map "f" 'outline-forward-same-level)
  6074. (org-defkey map "b" 'outline-backward-same-level)
  6075. (org-defkey map "u" 'outline-up-heading))
  6076. (org-defkey map "/" 'org-occur)
  6077. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6078. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6079. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6080. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6081. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6082. map)))
  6083. (defconst org-goto-help
  6084. "Browse buffer copy, to find location or copy text.%s
  6085. RET=jump to location C-g=quit and return to previous location
  6086. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6087. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6088. ;; FIXME: Docstring does not mention both interfaces
  6089. (defun org-goto (&optional alternative-interface)
  6090. "Look up a different location in the current file, keeping current visibility.
  6091. When you want look-up or go to a different location in a
  6092. document, the fastest way is often to fold the entire buffer and
  6093. then dive into the tree. This method has the disadvantage, that
  6094. the previous location will be folded, which may not be what you
  6095. want.
  6096. This command works around this by showing a copy of the current
  6097. buffer in an indirect buffer, in overview mode. You can dive
  6098. into the tree in that copy, use org-occur and incremental search
  6099. to find a location. When pressing RET or `Q', the command
  6100. returns to the original buffer in which the visibility is still
  6101. unchanged. After RET it will also jump to the location selected
  6102. in the indirect buffer and expose the headline hierarchy above.
  6103. With a prefix argument, use the alternative interface: e.g. if
  6104. `org-goto-interface' is 'outline use 'outline-path-completion."
  6105. (interactive "P")
  6106. (org-goto-map)
  6107. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6108. (org-refile-use-outline-path t)
  6109. (org-refile-target-verify-function nil)
  6110. (interface
  6111. (if (not alternative-interface)
  6112. org-goto-interface
  6113. (if (eq org-goto-interface 'outline)
  6114. 'outline-path-completion
  6115. 'outline)))
  6116. (org-goto-start-pos (point))
  6117. (selected-point
  6118. (if (eq interface 'outline)
  6119. (car (org-get-location (current-buffer) org-goto-help))
  6120. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6121. (org-refile-check-position pa)
  6122. (nth 3 pa)))))
  6123. (if selected-point
  6124. (progn
  6125. (org-mark-ring-push org-goto-start-pos)
  6126. (goto-char selected-point)
  6127. (if (or (outline-invisible-p) (org-invisible-p2))
  6128. (org-show-context 'org-goto)))
  6129. (message "Quit"))))
  6130. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6131. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6132. (defvar org-goto-local-auto-isearch-map) ; defined below
  6133. (defun org-get-location (buf help)
  6134. "Let the user select a location in the Org-mode buffer BUF.
  6135. This function uses a recursive edit. It returns the selected position
  6136. or nil."
  6137. (org-no-popups
  6138. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6139. (isearch-hide-immediately nil)
  6140. (isearch-search-fun-function
  6141. (lambda () 'org-goto-local-search-headings))
  6142. (org-goto-selected-point org-goto-exit-command))
  6143. (save-excursion
  6144. (save-window-excursion
  6145. (delete-other-windows)
  6146. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6147. (org-pop-to-buffer-same-window
  6148. (condition-case nil
  6149. (make-indirect-buffer (current-buffer) "*org-goto*")
  6150. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6151. (with-output-to-temp-buffer "*Org Help*"
  6152. (princ (format help (if org-goto-auto-isearch
  6153. " Just type for auto-isearch."
  6154. " n/p/f/b/u to navigate, q to quit."))))
  6155. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6156. (setq buffer-read-only nil)
  6157. (let ((org-startup-truncated t)
  6158. (org-startup-folded nil)
  6159. (org-startup-align-all-tables nil))
  6160. (org-mode)
  6161. (org-overview))
  6162. (setq buffer-read-only t)
  6163. (if (and (boundp 'org-goto-start-pos)
  6164. (integer-or-marker-p org-goto-start-pos))
  6165. (let ((org-show-hierarchy-above t)
  6166. (org-show-siblings t)
  6167. (org-show-following-heading t))
  6168. (goto-char org-goto-start-pos)
  6169. (and (outline-invisible-p) (org-show-context)))
  6170. (goto-char (point-min)))
  6171. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6172. (message "Select location and press RET")
  6173. (use-local-map org-goto-map)
  6174. (recursive-edit)))
  6175. (kill-buffer "*org-goto*")
  6176. (cons org-goto-selected-point org-goto-exit-command))))
  6177. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6178. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6179. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6180. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6181. (defun org-goto-local-search-headings (string bound noerror)
  6182. "Search and make sure that any matches are in headlines."
  6183. (catch 'return
  6184. (while (if isearch-forward
  6185. (search-forward string bound noerror)
  6186. (search-backward string bound noerror))
  6187. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6188. (and (member :headline context)
  6189. (not (member :tags context))))
  6190. (throw 'return (point))))))
  6191. (defun org-goto-local-auto-isearch ()
  6192. "Start isearch."
  6193. (interactive)
  6194. (goto-char (point-min))
  6195. (let ((keys (this-command-keys)))
  6196. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6197. (isearch-mode t)
  6198. (isearch-process-search-char (string-to-char keys)))))
  6199. (defun org-goto-ret (&optional arg)
  6200. "Finish `org-goto' by going to the new location."
  6201. (interactive "P")
  6202. (setq org-goto-selected-point (point)
  6203. org-goto-exit-command 'return)
  6204. (throw 'exit nil))
  6205. (defun org-goto-left ()
  6206. "Finish `org-goto' by going to the new location."
  6207. (interactive)
  6208. (if (org-at-heading-p)
  6209. (progn
  6210. (beginning-of-line 1)
  6211. (setq org-goto-selected-point (point)
  6212. org-goto-exit-command 'left)
  6213. (throw 'exit nil))
  6214. (error "Not on a heading")))
  6215. (defun org-goto-right ()
  6216. "Finish `org-goto' by going to the new location."
  6217. (interactive)
  6218. (if (org-at-heading-p)
  6219. (progn
  6220. (setq org-goto-selected-point (point)
  6221. org-goto-exit-command 'right)
  6222. (throw 'exit nil))
  6223. (error "Not on a heading")))
  6224. (defun org-goto-quit ()
  6225. "Finish `org-goto' without cursor motion."
  6226. (interactive)
  6227. (setq org-goto-selected-point nil)
  6228. (setq org-goto-exit-command 'quit)
  6229. (throw 'exit nil))
  6230. ;;; Indirect buffer display of subtrees
  6231. (defvar org-indirect-dedicated-frame nil
  6232. "This is the frame being used for indirect tree display.")
  6233. (defvar org-last-indirect-buffer nil)
  6234. (defun org-tree-to-indirect-buffer (&optional arg)
  6235. "Create indirect buffer and narrow it to current subtree.
  6236. With a numerical prefix ARG, go up to this level and then take that tree.
  6237. If ARG is negative, go up that many levels.
  6238. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6239. indirect buffer previously made with this command, to avoid proliferation of
  6240. indirect buffers. However, when you call the command with a \
  6241. \\[universal-argument] prefix, or
  6242. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6243. is kept so that you can work with several indirect buffers at the same time.
  6244. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6245. \\[universal-argument] prefix also
  6246. requests that a new frame be made for the new buffer, so that the dedicated
  6247. frame is not changed."
  6248. (interactive "P")
  6249. (let ((cbuf (current-buffer))
  6250. (cwin (selected-window))
  6251. (pos (point))
  6252. beg end level heading ibuf)
  6253. (save-excursion
  6254. (org-back-to-heading t)
  6255. (when (numberp arg)
  6256. (setq level (org-outline-level))
  6257. (if (< arg 0) (setq arg (+ level arg)))
  6258. (while (> (setq level (org-outline-level)) arg)
  6259. (org-up-heading-safe)))
  6260. (setq beg (point)
  6261. heading (org-get-heading))
  6262. (org-end-of-subtree t t)
  6263. (if (org-at-heading-p) (backward-char 1))
  6264. (setq end (point)))
  6265. (if (and (buffer-live-p org-last-indirect-buffer)
  6266. (not (eq org-indirect-buffer-display 'new-frame))
  6267. (not arg))
  6268. (kill-buffer org-last-indirect-buffer))
  6269. (setq ibuf (org-get-indirect-buffer cbuf)
  6270. org-last-indirect-buffer ibuf)
  6271. (cond
  6272. ((or (eq org-indirect-buffer-display 'new-frame)
  6273. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6274. (select-frame (make-frame))
  6275. (delete-other-windows)
  6276. (org-pop-to-buffer-same-window ibuf)
  6277. (org-set-frame-title heading))
  6278. ((eq org-indirect-buffer-display 'dedicated-frame)
  6279. (raise-frame
  6280. (select-frame (or (and org-indirect-dedicated-frame
  6281. (frame-live-p org-indirect-dedicated-frame)
  6282. org-indirect-dedicated-frame)
  6283. (setq org-indirect-dedicated-frame (make-frame)))))
  6284. (delete-other-windows)
  6285. (org-pop-to-buffer-same-window ibuf)
  6286. (org-set-frame-title (concat "Indirect: " heading)))
  6287. ((eq org-indirect-buffer-display 'current-window)
  6288. (org-pop-to-buffer-same-window ibuf))
  6289. ((eq org-indirect-buffer-display 'other-window)
  6290. (pop-to-buffer ibuf))
  6291. (t (error "Invalid value")))
  6292. (if (featurep 'xemacs)
  6293. (save-excursion (org-mode) (turn-on-font-lock)))
  6294. (narrow-to-region beg end)
  6295. (show-all)
  6296. (goto-char pos)
  6297. (run-hook-with-args 'org-cycle-hook 'all)
  6298. (and (window-live-p cwin) (select-window cwin))))
  6299. (defun org-get-indirect-buffer (&optional buffer)
  6300. (setq buffer (or buffer (current-buffer)))
  6301. (let ((n 1) (base (buffer-name buffer)) bname)
  6302. (while (buffer-live-p
  6303. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6304. (setq n (1+ n)))
  6305. (condition-case nil
  6306. (make-indirect-buffer buffer bname 'clone)
  6307. (error (make-indirect-buffer buffer bname)))))
  6308. (defun org-set-frame-title (title)
  6309. "Set the title of the current frame to the string TITLE."
  6310. ;; FIXME: how to name a single frame in XEmacs???
  6311. (unless (featurep 'xemacs)
  6312. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6313. ;;;; Structure editing
  6314. ;;; Inserting headlines
  6315. (defun org-previous-line-empty-p ()
  6316. (save-excursion
  6317. (and (not (bobp))
  6318. (or (beginning-of-line 0) t)
  6319. (save-match-data
  6320. (looking-at "[ \t]*$")))))
  6321. (defun org-insert-heading (&optional force-heading invisible-ok)
  6322. "Insert a new heading or item with same depth at point.
  6323. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6324. If point is at the beginning of a headline, insert a sibling before the
  6325. current headline. If point is not at the beginning, split the line,
  6326. create the new headline with the text in the current line after point
  6327. \(but see also the variable `org-M-RET-may-split-line').
  6328. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6329. This is important for non-interactive uses of the command."
  6330. (interactive "P")
  6331. (if (or (= (buffer-size) 0)
  6332. (and (not (save-excursion
  6333. (and (ignore-errors (org-back-to-heading invisible-ok))
  6334. (org-at-heading-p))))
  6335. (or force-heading (not (org-in-item-p)))))
  6336. (progn
  6337. (insert "\n* ")
  6338. (run-hooks 'org-insert-heading-hook))
  6339. (when (or force-heading (not (org-insert-item)))
  6340. (let* ((empty-line-p nil)
  6341. (level nil)
  6342. (on-heading (org-at-heading-p))
  6343. (head (save-excursion
  6344. (condition-case nil
  6345. (progn
  6346. (org-back-to-heading invisible-ok)
  6347. (when (and (not on-heading)
  6348. (featurep 'org-inlinetask)
  6349. (integerp org-inlinetask-min-level)
  6350. (>= (length (match-string 0))
  6351. org-inlinetask-min-level))
  6352. ;; Find a heading level before the inline task
  6353. (while (and (setq level (org-up-heading-safe))
  6354. (>= level org-inlinetask-min-level)))
  6355. (if (org-at-heading-p)
  6356. (org-back-to-heading invisible-ok)
  6357. (error "This should not happen")))
  6358. (setq empty-line-p (org-previous-line-empty-p))
  6359. (match-string 0))
  6360. (error "*"))))
  6361. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6362. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6363. pos hide-previous previous-pos)
  6364. (cond
  6365. ((and (org-at-heading-p) (bolp)
  6366. (or (bobp)
  6367. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6368. ;; insert before the current line
  6369. (open-line (if blank 2 1)))
  6370. ((and (bolp)
  6371. (not org-insert-heading-respect-content)
  6372. (or (bobp)
  6373. (save-excursion
  6374. (backward-char 1) (not (outline-invisible-p)))))
  6375. ;; insert right here
  6376. nil)
  6377. (t
  6378. ;; somewhere in the line
  6379. (save-excursion
  6380. (setq previous-pos (point-at-bol))
  6381. (end-of-line)
  6382. (setq hide-previous (outline-invisible-p)))
  6383. (and org-insert-heading-respect-content (org-show-subtree))
  6384. (let ((split
  6385. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6386. (save-excursion
  6387. (let ((p (point)))
  6388. (goto-char (point-at-bol))
  6389. (and (looking-at org-complex-heading-regexp)
  6390. (match-beginning 4)
  6391. (> p (match-beginning 4)))))))
  6392. tags pos)
  6393. (cond
  6394. (org-insert-heading-respect-content
  6395. (org-end-of-subtree nil t)
  6396. (when (featurep 'org-inlinetask)
  6397. (while (and (not (eobp))
  6398. (looking-at "\\(\\*+\\)[ \t]+")
  6399. (>= (length (match-string 1))
  6400. org-inlinetask-min-level))
  6401. (org-end-of-subtree nil t)))
  6402. (or (bolp) (newline))
  6403. (or (org-previous-line-empty-p)
  6404. (and blank (newline)))
  6405. (open-line 1))
  6406. ((org-at-heading-p)
  6407. (when hide-previous
  6408. (show-children)
  6409. (org-show-entry))
  6410. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6411. (setq tags (and (match-end 2) (match-string 2)))
  6412. (and (match-end 1)
  6413. (delete-region (match-beginning 1) (match-end 1)))
  6414. (setq pos (point-at-bol))
  6415. (or split (end-of-line 1))
  6416. (delete-horizontal-space)
  6417. (if (string-match "\\`\\*+\\'"
  6418. (buffer-substring (point-at-bol) (point)))
  6419. (insert " "))
  6420. (newline (if blank 2 1))
  6421. (when tags
  6422. (save-excursion
  6423. (goto-char pos)
  6424. (end-of-line 1)
  6425. (insert " " tags)
  6426. (org-set-tags nil 'align))))
  6427. (t
  6428. (or split (end-of-line 1))
  6429. (newline (if blank 2 1)))))))
  6430. (insert head) (just-one-space)
  6431. (setq pos (point))
  6432. (end-of-line 1)
  6433. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6434. (when (and org-insert-heading-respect-content hide-previous)
  6435. (save-excursion
  6436. (goto-char previous-pos)
  6437. (hide-subtree)))
  6438. (run-hooks 'org-insert-heading-hook)))))
  6439. (defun org-get-heading (&optional no-tags no-todo)
  6440. "Return the heading of the current entry, without the stars.
  6441. When NO-TAGS is non-nil, don't include tags.
  6442. When NO-TODO is non-nil, don't include TODO keywords."
  6443. (save-excursion
  6444. (org-back-to-heading t)
  6445. (cond
  6446. ((and no-tags no-todo)
  6447. (looking-at org-complex-heading-regexp)
  6448. (match-string 4))
  6449. (no-tags
  6450. (looking-at (concat org-outline-regexp
  6451. "\\(.*?\\)"
  6452. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6453. (match-string 1))
  6454. (no-todo
  6455. (looking-at org-todo-line-regexp)
  6456. (match-string 3))
  6457. (t (looking-at org-heading-regexp)
  6458. (match-string 2)))))
  6459. (defun org-heading-components ()
  6460. "Return the components of the current heading.
  6461. This is a list with the following elements:
  6462. - the level as an integer
  6463. - the reduced level, different if `org-odd-levels-only' is set.
  6464. - the TODO keyword, or nil
  6465. - the priority character, like ?A, or nil if no priority is given
  6466. - the headline text itself, or the tags string if no headline text
  6467. - the tags string, or nil."
  6468. (save-excursion
  6469. (org-back-to-heading t)
  6470. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6471. (list (length (match-string 1))
  6472. (org-reduced-level (length (match-string 1)))
  6473. (org-match-string-no-properties 2)
  6474. (and (match-end 3) (aref (match-string 3) 2))
  6475. (org-match-string-no-properties 4)
  6476. (org-match-string-no-properties 5)))))
  6477. (defun org-get-entry ()
  6478. "Get the entry text, after heading, entire subtree."
  6479. (save-excursion
  6480. (org-back-to-heading t)
  6481. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6482. (defun org-insert-heading-after-current ()
  6483. "Insert a new heading with same level as current, after current subtree."
  6484. (interactive)
  6485. (org-back-to-heading)
  6486. (org-insert-heading)
  6487. (org-move-subtree-down)
  6488. (end-of-line 1))
  6489. (defun org-insert-heading-respect-content (invisible-ok)
  6490. "Insert heading with `org-insert-heading-respect-content' set to t."
  6491. (interactive "P")
  6492. (let ((org-insert-heading-respect-content t))
  6493. (org-insert-heading t invisible-ok)))
  6494. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6495. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6496. (interactive "P")
  6497. (let ((org-insert-heading-respect-content t))
  6498. (org-insert-todo-heading force-state t)))
  6499. (defun org-insert-todo-heading (arg &optional force-heading)
  6500. "Insert a new heading with the same level and TODO state as current heading.
  6501. If the heading has no TODO state, or if the state is DONE, use the first
  6502. state (TODO by default). Also with prefix arg, force first state."
  6503. (interactive "P")
  6504. (when (or force-heading (not (org-insert-item 'checkbox)))
  6505. (org-insert-heading force-heading)
  6506. (save-excursion
  6507. (org-back-to-heading)
  6508. (outline-previous-heading)
  6509. (looking-at org-todo-line-regexp))
  6510. (let*
  6511. ((new-mark-x
  6512. (if (or arg
  6513. (not (match-beginning 2))
  6514. (member (match-string 2) org-done-keywords))
  6515. (car org-todo-keywords-1)
  6516. (match-string 2)))
  6517. (new-mark
  6518. (or
  6519. (run-hook-with-args-until-success
  6520. 'org-todo-get-default-hook new-mark-x nil)
  6521. new-mark-x)))
  6522. (beginning-of-line 1)
  6523. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6524. (if org-treat-insert-todo-heading-as-state-change
  6525. (org-todo new-mark)
  6526. (insert new-mark " "))))
  6527. (when org-provide-todo-statistics
  6528. (org-update-parent-todo-statistics))))
  6529. (defun org-insert-subheading (arg)
  6530. "Insert a new subheading and demote it.
  6531. Works for outline headings and for plain lists alike."
  6532. (interactive "P")
  6533. (org-insert-heading arg)
  6534. (cond
  6535. ((org-at-heading-p) (org-do-demote))
  6536. ((org-at-item-p) (org-indent-item))))
  6537. (defun org-insert-todo-subheading (arg)
  6538. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6539. Works for outline headings and for plain lists alike."
  6540. (interactive "P")
  6541. (org-insert-todo-heading arg)
  6542. (cond
  6543. ((org-at-heading-p) (org-do-demote))
  6544. ((org-at-item-p) (org-indent-item))))
  6545. ;;; Promotion and Demotion
  6546. (defvar org-after-demote-entry-hook nil
  6547. "Hook run after an entry has been demoted.
  6548. The cursor will be at the beginning of the entry.
  6549. When a subtree is being demoted, the hook will be called for each node.")
  6550. (defvar org-after-promote-entry-hook nil
  6551. "Hook run after an entry has been promoted.
  6552. The cursor will be at the beginning of the entry.
  6553. When a subtree is being promoted, the hook will be called for each node.")
  6554. (defun org-promote-subtree ()
  6555. "Promote the entire subtree.
  6556. See also `org-promote'."
  6557. (interactive)
  6558. (save-excursion
  6559. (org-with-limited-levels (org-map-tree 'org-promote)))
  6560. (org-fix-position-after-promote))
  6561. (defun org-demote-subtree ()
  6562. "Demote the entire subtree. See `org-demote'.
  6563. See also `org-promote'."
  6564. (interactive)
  6565. (save-excursion
  6566. (org-with-limited-levels (org-map-tree 'org-demote)))
  6567. (org-fix-position-after-promote))
  6568. (defun org-do-promote ()
  6569. "Promote the current heading higher up the tree.
  6570. If the region is active in `transient-mark-mode', promote all headings
  6571. in the region."
  6572. (interactive)
  6573. (save-excursion
  6574. (if (org-region-active-p)
  6575. (org-map-region 'org-promote (region-beginning) (region-end))
  6576. (org-promote)))
  6577. (org-fix-position-after-promote))
  6578. (defun org-do-demote ()
  6579. "Demote the current heading lower down the tree.
  6580. If the region is active in `transient-mark-mode', demote all headings
  6581. in the region."
  6582. (interactive)
  6583. (save-excursion
  6584. (if (org-region-active-p)
  6585. (org-map-region 'org-demote (region-beginning) (region-end))
  6586. (org-demote)))
  6587. (org-fix-position-after-promote))
  6588. (defun org-fix-position-after-promote ()
  6589. "Make sure that after pro/demotion cursor position is right."
  6590. (let ((pos (point)))
  6591. (when (save-excursion
  6592. (beginning-of-line 1)
  6593. (looking-at org-todo-line-regexp)
  6594. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6595. (cond ((eobp) (insert " "))
  6596. ((eolp) (insert " "))
  6597. ((equal (char-after) ?\ ) (forward-char 1))))))
  6598. (defun org-current-level ()
  6599. "Return the level of the current entry, or nil if before the first headline.
  6600. The level is the number of stars at the beginning of the headline."
  6601. (save-excursion
  6602. (org-with-limited-levels
  6603. (if (ignore-errors (org-back-to-heading t))
  6604. (funcall outline-level)))))
  6605. (defun org-get-previous-line-level ()
  6606. "Return the outline depth of the last headline before the current line.
  6607. Returns 0 for the first headline in the buffer, and nil if before the
  6608. first headline."
  6609. (let ((current-level (org-current-level))
  6610. (prev-level (when (> (line-number-at-pos) 1)
  6611. (save-excursion
  6612. (beginning-of-line 0)
  6613. (org-current-level)))))
  6614. (cond ((null current-level) nil) ; Before first headline
  6615. ((null prev-level) 0) ; At first headline
  6616. (prev-level))))
  6617. (defun org-reduced-level (l)
  6618. "Compute the effective level of a heading.
  6619. This takes into account the setting of `org-odd-levels-only'."
  6620. (cond
  6621. ((zerop l) 0)
  6622. (org-odd-levels-only (1+ (floor (/ l 2))))
  6623. (t l)))
  6624. (defun org-level-increment ()
  6625. "Return the number of stars that will be added or removed at a
  6626. time to headlines when structure editing, based on the value of
  6627. `org-odd-levels-only'."
  6628. (if org-odd-levels-only 2 1))
  6629. (defun org-get-valid-level (level &optional change)
  6630. "Rectify a level change under the influence of `org-odd-levels-only'
  6631. LEVEL is a current level, CHANGE is by how much the level should be
  6632. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6633. even level numbers will become the next higher odd number."
  6634. (if org-odd-levels-only
  6635. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6636. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6637. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6638. (max 1 (+ level (or change 0)))))
  6639. (if (boundp 'define-obsolete-function-alias)
  6640. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6641. (define-obsolete-function-alias 'org-get-legal-level
  6642. 'org-get-valid-level)
  6643. (define-obsolete-function-alias 'org-get-legal-level
  6644. 'org-get-valid-level "23.1")))
  6645. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6646. ;; ̀org-with-limited-levels'
  6647. (defun org-promote ()
  6648. "Promote the current heading higher up the tree.
  6649. If the region is active in `transient-mark-mode', promote all headings
  6650. in the region."
  6651. (org-back-to-heading t)
  6652. (let* ((level (save-match-data (funcall outline-level)))
  6653. (after-change-functions (remove 'flyspell-after-change-function
  6654. after-change-functions))
  6655. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6656. (diff (abs (- level (length up-head) -1))))
  6657. (cond ((and (= level 1) org-called-with-limited-levels
  6658. org-allow-promoting-top-level-subtree)
  6659. (replace-match "# " nil t))
  6660. ((= level 1)
  6661. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6662. (t (replace-match up-head nil t)))
  6663. ;; Fixup tag positioning
  6664. (unless (= level 1)
  6665. (and org-auto-align-tags (org-set-tags nil t))
  6666. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6667. (run-hooks 'org-after-promote-entry-hook)))
  6668. (defun org-demote ()
  6669. "Demote the current heading lower down the tree.
  6670. If the region is active in `transient-mark-mode', demote all headings
  6671. in the region."
  6672. (org-back-to-heading t)
  6673. (let* ((level (save-match-data (funcall outline-level)))
  6674. (after-change-functions (remove 'flyspell-after-change-function
  6675. after-change-functions))
  6676. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6677. (diff (abs (- level (length down-head) -1))))
  6678. (replace-match down-head nil t)
  6679. ;; Fixup tag positioning
  6680. (and org-auto-align-tags (org-set-tags nil t))
  6681. (if org-adapt-indentation (org-fixup-indentation diff))
  6682. (run-hooks 'org-after-demote-entry-hook)))
  6683. (defun org-cycle-level ()
  6684. "Cycle the level of an empty headline through possible states.
  6685. This goes first to child, then to parent, level, then up the hierarchy.
  6686. After top level, it switches back to sibling level."
  6687. (interactive)
  6688. (let ((org-adapt-indentation nil))
  6689. (when (org-point-at-end-of-empty-headline)
  6690. (setq this-command 'org-cycle-level) ; Only needed for caching
  6691. (let ((cur-level (org-current-level))
  6692. (prev-level (org-get-previous-line-level)))
  6693. (cond
  6694. ;; If first headline in file, promote to top-level.
  6695. ((= prev-level 0)
  6696. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6697. do (org-do-promote)))
  6698. ;; If same level as prev, demote one.
  6699. ((= prev-level cur-level)
  6700. (org-do-demote))
  6701. ;; If parent is top-level, promote to top level if not already.
  6702. ((= prev-level 1)
  6703. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6704. do (org-do-promote)))
  6705. ;; If top-level, return to prev-level.
  6706. ((= cur-level 1)
  6707. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6708. do (org-do-demote)))
  6709. ;; If less than prev-level, promote one.
  6710. ((< cur-level prev-level)
  6711. (org-do-promote))
  6712. ;; If deeper than prev-level, promote until higher than
  6713. ;; prev-level.
  6714. ((> cur-level prev-level)
  6715. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6716. do (org-do-promote))))
  6717. t))))
  6718. (defun org-map-tree (fun)
  6719. "Call FUN for every heading underneath the current one."
  6720. (org-back-to-heading)
  6721. (let ((level (funcall outline-level)))
  6722. (save-excursion
  6723. (funcall fun)
  6724. (while (and (progn
  6725. (outline-next-heading)
  6726. (> (funcall outline-level) level))
  6727. (not (eobp)))
  6728. (funcall fun)))))
  6729. (defun org-map-region (fun beg end)
  6730. "Call FUN for every heading between BEG and END."
  6731. (let ((org-ignore-region t))
  6732. (save-excursion
  6733. (setq end (copy-marker end))
  6734. (goto-char beg)
  6735. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6736. (< (point) end))
  6737. (funcall fun))
  6738. (while (and (progn
  6739. (outline-next-heading)
  6740. (< (point) end))
  6741. (not (eobp)))
  6742. (funcall fun)))))
  6743. (defvar org-property-end-re) ; silence byte-compiler
  6744. (defun org-fixup-indentation (diff)
  6745. "Change the indentation in the current entry by DIFF.
  6746. However, if any line in the current entry has no indentation, or if it
  6747. would end up with no indentation after the change, nothing at all is done."
  6748. (save-excursion
  6749. (let ((end (save-excursion (outline-next-heading)
  6750. (point-marker)))
  6751. (prohibit (if (> diff 0)
  6752. "^\\S-"
  6753. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6754. col)
  6755. (unless (save-excursion (end-of-line 1)
  6756. (re-search-forward prohibit end t))
  6757. (while (and (< (point) end)
  6758. (re-search-forward "^[ \t]+" end t))
  6759. (goto-char (match-end 0))
  6760. (setq col (current-column))
  6761. (if (< diff 0) (replace-match ""))
  6762. (org-indent-to-column (+ diff col))))
  6763. (move-marker end nil))))
  6764. (defun org-convert-to-odd-levels ()
  6765. "Convert an org-mode file with all levels allowed to one with odd levels.
  6766. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6767. level 5 etc."
  6768. (interactive)
  6769. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6770. (let ((outline-level 'org-outline-level)
  6771. (org-odd-levels-only nil) n)
  6772. (save-excursion
  6773. (goto-char (point-min))
  6774. (while (re-search-forward "^\\*\\*+ " nil t)
  6775. (setq n (- (length (match-string 0)) 2))
  6776. (while (>= (setq n (1- n)) 0)
  6777. (org-demote))
  6778. (end-of-line 1))))))
  6779. (defun org-convert-to-oddeven-levels ()
  6780. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6781. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6782. file contains a section with an even level, conversion would
  6783. destroy the structure of the file. An error is signaled in this
  6784. case."
  6785. (interactive)
  6786. (goto-char (point-min))
  6787. ;; First check if there are no even levels
  6788. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6789. (org-show-context t)
  6790. (error "Not all levels are odd in this file. Conversion not possible"))
  6791. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6792. (let ((outline-regexp org-outline-regexp)
  6793. (outline-level 'org-outline-level)
  6794. (org-odd-levels-only nil) n)
  6795. (save-excursion
  6796. (goto-char (point-min))
  6797. (while (re-search-forward "^\\*\\*+ " nil t)
  6798. (setq n (/ (1- (length (match-string 0))) 2))
  6799. (while (>= (setq n (1- n)) 0)
  6800. (org-promote))
  6801. (end-of-line 1))))))
  6802. (defun org-tr-level (n)
  6803. "Make N odd if required."
  6804. (if org-odd-levels-only (1+ (/ n 2)) n))
  6805. ;;; Vertical tree motion, cutting and pasting of subtrees
  6806. (defun org-move-subtree-up (&optional arg)
  6807. "Move the current subtree up past ARG headlines of the same level."
  6808. (interactive "p")
  6809. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6810. (defun org-move-subtree-down (&optional arg)
  6811. "Move the current subtree down past ARG headlines of the same level."
  6812. (interactive "p")
  6813. (setq arg (prefix-numeric-value arg))
  6814. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6815. 'org-get-last-sibling))
  6816. (ins-point (make-marker))
  6817. (cnt (abs arg))
  6818. (col (current-column))
  6819. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6820. ;; Select the tree
  6821. (org-back-to-heading)
  6822. (setq beg0 (point))
  6823. (save-excursion
  6824. (setq ne-beg (org-back-over-empty-lines))
  6825. (setq beg (point)))
  6826. (save-match-data
  6827. (save-excursion (outline-end-of-heading)
  6828. (setq folded (outline-invisible-p)))
  6829. (outline-end-of-subtree))
  6830. (outline-next-heading)
  6831. (setq ne-end (org-back-over-empty-lines))
  6832. (setq end (point))
  6833. (goto-char beg0)
  6834. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6835. ;; include less whitespace
  6836. (save-excursion
  6837. (goto-char beg)
  6838. (forward-line (- ne-beg ne-end))
  6839. (setq beg (point))))
  6840. ;; Find insertion point, with error handling
  6841. (while (> cnt 0)
  6842. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6843. (progn (goto-char beg0)
  6844. (error "Cannot move past superior level or buffer limit")))
  6845. (setq cnt (1- cnt)))
  6846. (if (> arg 0)
  6847. ;; Moving forward - still need to move over subtree
  6848. (progn (org-end-of-subtree t t)
  6849. (save-excursion
  6850. (org-back-over-empty-lines)
  6851. (or (bolp) (newline)))))
  6852. (setq ne-ins (org-back-over-empty-lines))
  6853. (move-marker ins-point (point))
  6854. (setq txt (buffer-substring beg end))
  6855. (org-save-markers-in-region beg end)
  6856. (delete-region beg end)
  6857. (org-remove-empty-overlays-at beg)
  6858. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6859. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6860. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6861. (let ((bbb (point)))
  6862. (insert-before-markers txt)
  6863. (org-reinstall-markers-in-region bbb)
  6864. (move-marker ins-point bbb))
  6865. (or (bolp) (insert "\n"))
  6866. (setq ins-end (point))
  6867. (goto-char ins-point)
  6868. (org-skip-whitespace)
  6869. (when (and (< arg 0)
  6870. (org-first-sibling-p)
  6871. (> ne-ins ne-beg))
  6872. ;; Move whitespace back to beginning
  6873. (save-excursion
  6874. (goto-char ins-end)
  6875. (let ((kill-whole-line t))
  6876. (kill-line (- ne-ins ne-beg)) (point)))
  6877. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6878. (move-marker ins-point nil)
  6879. (if folded
  6880. (hide-subtree)
  6881. (org-show-entry)
  6882. (show-children)
  6883. (org-cycle-hide-drawers 'children))
  6884. (org-clean-visibility-after-subtree-move)
  6885. ;; move back to the initial column we were at
  6886. (move-to-column col)))
  6887. (defvar org-subtree-clip ""
  6888. "Clipboard for cut and paste of subtrees.
  6889. This is actually only a copy of the kill, because we use the normal kill
  6890. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6891. (defvar org-subtree-clip-folded nil
  6892. "Was the last copied subtree folded?
  6893. This is used to fold the tree back after pasting.")
  6894. (defun org-cut-subtree (&optional n)
  6895. "Cut the current subtree into the clipboard.
  6896. With prefix arg N, cut this many sequential subtrees.
  6897. This is a short-hand for marking the subtree and then cutting it."
  6898. (interactive "p")
  6899. (org-copy-subtree n 'cut))
  6900. (defun org-copy-subtree (&optional n cut force-store-markers)
  6901. "Cut the current subtree into the clipboard.
  6902. With prefix arg N, cut this many sequential subtrees.
  6903. This is a short-hand for marking the subtree and then copying it.
  6904. If CUT is non-nil, actually cut the subtree.
  6905. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6906. of some markers in the region, even if CUT is non-nil. This is
  6907. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6908. (interactive "p")
  6909. (let (beg end folded (beg0 (point)))
  6910. (if (org-called-interactively-p 'any)
  6911. (org-back-to-heading nil) ; take what looks like a subtree
  6912. (org-back-to-heading t)) ; take what is really there
  6913. (setq beg (point))
  6914. (skip-chars-forward " \t\r\n")
  6915. (save-match-data
  6916. (save-excursion (outline-end-of-heading)
  6917. (setq folded (outline-invisible-p)))
  6918. (condition-case nil
  6919. (org-forward-heading-same-level (1- n) t)
  6920. (error nil))
  6921. (org-end-of-subtree t t))
  6922. (setq end (point))
  6923. (goto-char beg0)
  6924. (when (> end beg)
  6925. (setq org-subtree-clip-folded folded)
  6926. (when (or cut force-store-markers)
  6927. (org-save-markers-in-region beg end))
  6928. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6929. (setq org-subtree-clip (current-kill 0))
  6930. (message "%s: Subtree(s) with %d characters"
  6931. (if cut "Cut" "Copied")
  6932. (length org-subtree-clip)))))
  6933. (defun org-paste-subtree (&optional level tree for-yank)
  6934. "Paste the clipboard as a subtree, with modification of headline level.
  6935. The entire subtree is promoted or demoted in order to match a new headline
  6936. level.
  6937. If the cursor is at the beginning of a headline, the same level as
  6938. that headline is used to paste the tree
  6939. If not, the new level is derived from the *visible* headings
  6940. before and after the insertion point, and taken to be the inferior headline
  6941. level of the two. So if the previous visible heading is level 3 and the
  6942. next is level 4 (or vice versa), level 4 will be used for insertion.
  6943. This makes sure that the subtree remains an independent subtree and does
  6944. not swallow low level entries.
  6945. You can also force a different level, either by using a numeric prefix
  6946. argument, or by inserting the heading marker by hand. For example, if the
  6947. cursor is after \"*****\", then the tree will be shifted to level 5.
  6948. If optional TREE is given, use this text instead of the kill ring.
  6949. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6950. move back over whitespace before inserting, and move point to the end of
  6951. the inserted text when done."
  6952. (interactive "P")
  6953. (setq tree (or tree (and kill-ring (current-kill 0))))
  6954. (unless (org-kill-is-subtree-p tree)
  6955. (error "%s"
  6956. (substitute-command-keys
  6957. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6958. (org-with-limited-levels
  6959. (let* ((visp (not (outline-invisible-p)))
  6960. (txt tree)
  6961. (^re_ "\\(\\*+\\)[ \t]*")
  6962. (old-level (if (string-match org-outline-regexp-bol txt)
  6963. (- (match-end 0) (match-beginning 0) 1)
  6964. -1))
  6965. (force-level (cond (level (prefix-numeric-value level))
  6966. ((and (looking-at "[ \t]*$")
  6967. (string-match
  6968. "^\\*+$" (buffer-substring
  6969. (point-at-bol) (point))))
  6970. (- (match-end 1) (match-beginning 1)))
  6971. ((and (bolp)
  6972. (looking-at org-outline-regexp))
  6973. (- (match-end 0) (point) 1))))
  6974. (previous-level (save-excursion
  6975. (condition-case nil
  6976. (progn
  6977. (outline-previous-visible-heading 1)
  6978. (if (looking-at ^re_)
  6979. (- (match-end 0) (match-beginning 0) 1)
  6980. 1))
  6981. (error 1))))
  6982. (next-level (save-excursion
  6983. (condition-case nil
  6984. (progn
  6985. (or (looking-at org-outline-regexp)
  6986. (outline-next-visible-heading 1))
  6987. (if (looking-at ^re_)
  6988. (- (match-end 0) (match-beginning 0) 1)
  6989. 1))
  6990. (error 1))))
  6991. (new-level (or force-level (max previous-level next-level)))
  6992. (shift (if (or (= old-level -1)
  6993. (= new-level -1)
  6994. (= old-level new-level))
  6995. 0
  6996. (- new-level old-level)))
  6997. (delta (if (> shift 0) -1 1))
  6998. (func (if (> shift 0) 'org-demote 'org-promote))
  6999. (org-odd-levels-only nil)
  7000. beg end newend)
  7001. ;; Remove the forced level indicator
  7002. (if force-level
  7003. (delete-region (point-at-bol) (point)))
  7004. ;; Paste
  7005. (beginning-of-line (if (bolp) 1 2))
  7006. (setq beg (point))
  7007. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7008. (insert-before-markers txt)
  7009. (unless (string-match "\n\\'" txt) (insert "\n"))
  7010. (setq newend (point))
  7011. (org-reinstall-markers-in-region beg)
  7012. (setq end (point))
  7013. (goto-char beg)
  7014. (skip-chars-forward " \t\n\r")
  7015. (setq beg (point))
  7016. (if (and (outline-invisible-p) visp)
  7017. (save-excursion (outline-show-heading)))
  7018. ;; Shift if necessary
  7019. (unless (= shift 0)
  7020. (save-restriction
  7021. (narrow-to-region beg end)
  7022. (while (not (= shift 0))
  7023. (org-map-region func (point-min) (point-max))
  7024. (setq shift (+ delta shift)))
  7025. (goto-char (point-min))
  7026. (setq newend (point-max))))
  7027. (when (or (org-called-interactively-p 'interactive) for-yank)
  7028. (message "Clipboard pasted as level %d subtree" new-level))
  7029. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7030. kill-ring
  7031. (eq org-subtree-clip (current-kill 0))
  7032. org-subtree-clip-folded)
  7033. ;; The tree was folded before it was killed/copied
  7034. (hide-subtree))
  7035. (and for-yank (goto-char newend)))))
  7036. (defun org-kill-is-subtree-p (&optional txt)
  7037. "Check if the current kill is an outline subtree, or a set of trees.
  7038. Returns nil if kill does not start with a headline, or if the first
  7039. headline level is not the largest headline level in the tree.
  7040. So this will actually accept several entries of equal levels as well,
  7041. which is OK for `org-paste-subtree'.
  7042. If optional TXT is given, check this string instead of the current kill."
  7043. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7044. (re (org-get-limited-outline-regexp))
  7045. (^re (concat "^" re))
  7046. (start-level (and kill
  7047. (string-match
  7048. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7049. kill)
  7050. (- (match-end 2) (match-beginning 2) 1)))
  7051. (start (1+ (or (match-beginning 2) -1))))
  7052. (if (not start-level)
  7053. (progn
  7054. nil) ;; does not even start with a heading
  7055. (catch 'exit
  7056. (while (setq start (string-match ^re kill (1+ start)))
  7057. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7058. (throw 'exit nil)))
  7059. t))))
  7060. (defvar org-markers-to-move nil
  7061. "Markers that should be moved with a cut-and-paste operation.
  7062. Those markers are stored together with their positions relative to
  7063. the start of the region.")
  7064. (defun org-save-markers-in-region (beg end)
  7065. "Check markers in region.
  7066. If these markers are between BEG and END, record their position relative
  7067. to BEG, so that after moving the block of text, we can put the markers back
  7068. into place.
  7069. This function gets called just before an entry or tree gets cut from the
  7070. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7071. called immediately, to move the markers with the entries."
  7072. (setq org-markers-to-move nil)
  7073. (when (featurep 'org-clock)
  7074. (org-clock-save-markers-for-cut-and-paste beg end))
  7075. (when (featurep 'org-agenda)
  7076. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7077. (defun org-check-and-save-marker (marker beg end)
  7078. "Check if MARKER is between BEG and END.
  7079. If yes, remember the marker and the distance to BEG."
  7080. (when (and (marker-buffer marker)
  7081. (equal (marker-buffer marker) (current-buffer)))
  7082. (if (and (>= marker beg) (< marker end))
  7083. (push (cons marker (- marker beg)) org-markers-to-move))))
  7084. (defun org-reinstall-markers-in-region (beg)
  7085. "Move all remembered markers to their position relative to BEG."
  7086. (mapc (lambda (x)
  7087. (move-marker (car x) (+ beg (cdr x))))
  7088. org-markers-to-move)
  7089. (setq org-markers-to-move nil))
  7090. (defun org-narrow-to-subtree ()
  7091. "Narrow buffer to the current subtree."
  7092. (interactive)
  7093. (save-excursion
  7094. (save-match-data
  7095. (org-with-limited-levels
  7096. (narrow-to-region
  7097. (progn (org-back-to-heading t) (point))
  7098. (progn (org-end-of-subtree t t)
  7099. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7100. (point)))))))
  7101. (defun org-narrow-to-block ()
  7102. "Narrow buffer to the current block."
  7103. (interactive)
  7104. (let* ((case-fold-search t)
  7105. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7106. "^[ \t]*#\\+end_.*")))
  7107. (if blockp
  7108. (narrow-to-region (car blockp) (cdr blockp))
  7109. (error "Not in a block"))))
  7110. (eval-when-compile
  7111. (defvar org-property-drawer-re))
  7112. (defvar org-property-start-re) ;; defined below
  7113. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7114. "Clone the task (subtree) at point N times.
  7115. The clones will be inserted as siblings.
  7116. In interactive use, the user will be prompted for the number of
  7117. clones to be produced, and for a time SHIFT, which may be a
  7118. repeater as used in time stamps, for example `+3d'.
  7119. When a valid repeater is given and the entry contains any time
  7120. stamps, the clones will become a sequence in time, with time
  7121. stamps in the subtree shifted for each clone produced. If SHIFT
  7122. is nil or the empty string, time stamps will be left alone. The
  7123. ID property of the original subtree is removed.
  7124. If the original subtree did contain time stamps with a repeater,
  7125. the following will happen:
  7126. - the repeater will be removed in each clone
  7127. - an additional clone will be produced, with the current, unshifted
  7128. date(s) in the entry.
  7129. - the original entry will be placed *after* all the clones, with
  7130. repeater intact.
  7131. - the start days in the repeater in the original entry will be shifted
  7132. to past the last clone.
  7133. In this way you can spell out a number of instances of a repeating task,
  7134. and still retain the repeater to cover future instances of the task."
  7135. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7136. (let (beg end template task idprop
  7137. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7138. (drawer-re org-drawer-regexp))
  7139. (if (not (and (integerp n) (> n 0)))
  7140. (error "Invalid number of replications %s" n))
  7141. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7142. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7143. shift)))
  7144. (error "Invalid shift specification %s" shift))
  7145. (when doshift
  7146. (setq shift-n (string-to-number (match-string 1 shift))
  7147. shift-what (cdr (assoc (match-string 2 shift)
  7148. '(("d" . day) ("w" . week)
  7149. ("m" . month) ("y" . year))))))
  7150. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7151. (setq nmin 1 nmax n)
  7152. (org-back-to-heading t)
  7153. (setq beg (point))
  7154. (setq idprop (org-entry-get nil "ID"))
  7155. (org-end-of-subtree t t)
  7156. (or (bolp) (insert "\n"))
  7157. (setq end (point))
  7158. (setq template (buffer-substring beg end))
  7159. (when (and doshift
  7160. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7161. (delete-region beg end)
  7162. (setq end beg)
  7163. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7164. (goto-char end)
  7165. (loop for n from nmin to nmax do
  7166. ;; prepare clone
  7167. (with-temp-buffer
  7168. (insert template)
  7169. (org-mode)
  7170. (goto-char (point-min))
  7171. (org-show-subtree)
  7172. (and idprop (if org-clone-delete-id
  7173. (org-entry-delete nil "ID")
  7174. (org-id-get-create t)))
  7175. (unless (= n 0)
  7176. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7177. (kill-whole-line))
  7178. (goto-char (point-min))
  7179. (while (re-search-forward drawer-re nil t)
  7180. (mapc (lambda (d)
  7181. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7182. (goto-char (point-min))
  7183. (when doshift
  7184. (while (re-search-forward org-ts-regexp-both nil t)
  7185. (org-timestamp-change (* n shift-n) shift-what))
  7186. (unless (= n n-no-remove)
  7187. (goto-char (point-min))
  7188. (while (re-search-forward org-ts-regexp nil t)
  7189. (save-excursion
  7190. (goto-char (match-beginning 0))
  7191. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7192. (delete-region (match-beginning 1) (match-end 1)))))))
  7193. (setq task (buffer-string)))
  7194. (insert task))
  7195. (goto-char beg)))
  7196. ;;; Outline Sorting
  7197. (defun org-sort (with-case)
  7198. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7199. Optional argument WITH-CASE means sort case-sensitively."
  7200. (interactive "P")
  7201. (cond
  7202. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7203. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7204. (t
  7205. (org-call-with-arg 'org-sort-entries with-case))))
  7206. (defun org-sort-remove-invisible (s)
  7207. (remove-text-properties 0 (length s) org-rm-props s)
  7208. (while (string-match org-bracket-link-regexp s)
  7209. (setq s (replace-match (if (match-end 2)
  7210. (match-string 3 s)
  7211. (match-string 1 s)) t t s)))
  7212. s)
  7213. (defvar org-priority-regexp) ; defined later in the file
  7214. (defvar org-after-sorting-entries-or-items-hook nil
  7215. "Hook that is run after a bunch of entries or items have been sorted.
  7216. When children are sorted, the cursor is in the parent line when this
  7217. hook gets called. When a region or a plain list is sorted, the cursor
  7218. will be in the first entry of the sorted region/list.")
  7219. (defun org-sort-entries
  7220. (&optional with-case sorting-type getkey-func compare-func property)
  7221. "Sort entries on a certain level of an outline tree.
  7222. If there is an active region, the entries in the region are sorted.
  7223. Else, if the cursor is before the first entry, sort the top-level items.
  7224. Else, the children of the entry at point are sorted.
  7225. Sorting can be alphabetically, numerically, by date/time as given by
  7226. a time stamp, by a property or by priority.
  7227. The command prompts for the sorting type unless it has been given to the
  7228. function through the SORTING-TYPE argument, which needs to be a character,
  7229. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7230. precise meaning of each character:
  7231. n Numerically, by converting the beginning of the entry/item to a number.
  7232. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7233. o By order of TODO keywords.
  7234. t By date/time, either the first active time stamp in the entry, or, if
  7235. none exist, by the first inactive one.
  7236. s By the scheduled date/time.
  7237. d By deadline date/time.
  7238. c By creation time, which is assumed to be the first inactive time stamp
  7239. at the beginning of a line.
  7240. p By priority according to the cookie.
  7241. r By the value of a property.
  7242. Capital letters will reverse the sort order.
  7243. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7244. called with point at the beginning of the record. It must return either
  7245. a string or a number that should serve as the sorting key for that record.
  7246. Comparing entries ignores case by default. However, with an optional argument
  7247. WITH-CASE, the sorting considers case as well."
  7248. (interactive "P")
  7249. (let ((case-func (if with-case 'identity 'downcase))
  7250. (cmstr
  7251. ;; The clock marker is lost when using `sort-subr', let's
  7252. ;; store the clocking string.
  7253. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7254. (save-excursion
  7255. (goto-char org-clock-marker)
  7256. (looking-back "^.*") (match-string-no-properties 0))))
  7257. start beg end stars re re2
  7258. txt what tmp)
  7259. ;; Find beginning and end of region to sort
  7260. (cond
  7261. ((org-region-active-p)
  7262. ;; we will sort the region
  7263. (setq end (region-end)
  7264. what "region")
  7265. (goto-char (region-beginning))
  7266. (if (not (org-at-heading-p)) (outline-next-heading))
  7267. (setq start (point)))
  7268. ((or (org-at-heading-p)
  7269. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7270. ;; we will sort the children of the current headline
  7271. (org-back-to-heading)
  7272. (setq start (point)
  7273. end (progn (org-end-of-subtree t t)
  7274. (or (bolp) (insert "\n"))
  7275. (org-back-over-empty-lines)
  7276. (point))
  7277. what "children")
  7278. (goto-char start)
  7279. (show-subtree)
  7280. (outline-next-heading))
  7281. (t
  7282. ;; we will sort the top-level entries in this file
  7283. (goto-char (point-min))
  7284. (or (org-at-heading-p) (outline-next-heading))
  7285. (setq start (point))
  7286. (goto-char (point-max))
  7287. (beginning-of-line 1)
  7288. (when (looking-at ".*?\\S-")
  7289. ;; File ends in a non-white line
  7290. (end-of-line 1)
  7291. (insert "\n"))
  7292. (setq end (point-max))
  7293. (setq what "top-level")
  7294. (goto-char start)
  7295. (show-all)))
  7296. (setq beg (point))
  7297. (if (>= beg end) (error "Nothing to sort"))
  7298. (looking-at "\\(\\*+\\)")
  7299. (setq stars (match-string 1)
  7300. re (concat "^" (regexp-quote stars) " +")
  7301. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7302. txt (buffer-substring beg end))
  7303. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7304. (if (and (not (equal stars "*")) (string-match re2 txt))
  7305. (error "Region to sort contains a level above the first entry"))
  7306. (unless sorting-type
  7307. (message
  7308. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7309. [t]ime [s]cheduled [d]eadline [c]reated
  7310. A/N/P/R/O/F/T/S/D/C means reversed:"
  7311. what)
  7312. (setq sorting-type (read-char-exclusive))
  7313. (and (= (downcase sorting-type) ?f)
  7314. (setq getkey-func
  7315. (org-icompleting-read "Sort using function: "
  7316. obarray 'fboundp t nil nil))
  7317. (setq getkey-func (intern getkey-func)))
  7318. (and (= (downcase sorting-type) ?r)
  7319. (setq property
  7320. (org-icompleting-read "Property: "
  7321. (mapcar 'list (org-buffer-property-keys t))
  7322. nil t))))
  7323. (message "Sorting entries...")
  7324. (save-restriction
  7325. (narrow-to-region start end)
  7326. (let ((dcst (downcase sorting-type))
  7327. (case-fold-search nil)
  7328. (now (current-time)))
  7329. (sort-subr
  7330. (/= dcst sorting-type)
  7331. ;; This function moves to the beginning character of the "record" to
  7332. ;; be sorted.
  7333. (lambda nil
  7334. (if (re-search-forward re nil t)
  7335. (goto-char (match-beginning 0))
  7336. (goto-char (point-max))))
  7337. ;; This function moves to the last character of the "record" being
  7338. ;; sorted.
  7339. (lambda nil
  7340. (save-match-data
  7341. (condition-case nil
  7342. (outline-forward-same-level 1)
  7343. (error
  7344. (goto-char (point-max))))))
  7345. ;; This function returns the value that gets sorted against.
  7346. (lambda nil
  7347. (cond
  7348. ((= dcst ?n)
  7349. (if (looking-at org-complex-heading-regexp)
  7350. (string-to-number (match-string 4))
  7351. nil))
  7352. ((= dcst ?a)
  7353. (if (looking-at org-complex-heading-regexp)
  7354. (funcall case-func (match-string 4))
  7355. nil))
  7356. ((= dcst ?t)
  7357. (let ((end (save-excursion (outline-next-heading) (point))))
  7358. (if (or (re-search-forward org-ts-regexp end t)
  7359. (re-search-forward org-ts-regexp-both end t))
  7360. (org-time-string-to-seconds (match-string 0))
  7361. (org-float-time now))))
  7362. ((= dcst ?c)
  7363. (let ((end (save-excursion (outline-next-heading) (point))))
  7364. (if (re-search-forward
  7365. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7366. end t)
  7367. (org-time-string-to-seconds (match-string 0))
  7368. (org-float-time now))))
  7369. ((= dcst ?s)
  7370. (let ((end (save-excursion (outline-next-heading) (point))))
  7371. (if (re-search-forward org-scheduled-time-regexp end t)
  7372. (org-time-string-to-seconds (match-string 1))
  7373. (org-float-time now))))
  7374. ((= dcst ?d)
  7375. (let ((end (save-excursion (outline-next-heading) (point))))
  7376. (if (re-search-forward org-deadline-time-regexp end t)
  7377. (org-time-string-to-seconds (match-string 1))
  7378. (org-float-time now))))
  7379. ((= dcst ?p)
  7380. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7381. (string-to-char (match-string 2))
  7382. org-default-priority))
  7383. ((= dcst ?r)
  7384. (or (org-entry-get nil property) ""))
  7385. ((= dcst ?o)
  7386. (if (looking-at org-complex-heading-regexp)
  7387. (- 9999 (length (member (match-string 2)
  7388. org-todo-keywords-1)))))
  7389. ((= dcst ?f)
  7390. (if getkey-func
  7391. (progn
  7392. (setq tmp (funcall getkey-func))
  7393. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7394. tmp)
  7395. (error "Invalid key function `%s'" getkey-func)))
  7396. (t (error "Invalid sorting type `%c'" sorting-type))))
  7397. nil
  7398. (cond
  7399. ((= dcst ?a) 'string<)
  7400. ((= dcst ?f) compare-func)
  7401. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7402. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7403. ;; Reset the clock marker if needed
  7404. (when cmstr
  7405. (save-excursion
  7406. (goto-char start)
  7407. (search-forward cmstr nil t)
  7408. (move-marker org-clock-marker (point))))
  7409. (message "Sorting entries...done")))
  7410. (defun org-do-sort (table what &optional with-case sorting-type)
  7411. "Sort TABLE of WHAT according to SORTING-TYPE.
  7412. The user will be prompted for the SORTING-TYPE if the call to this
  7413. function does not specify it. WHAT is only for the prompt, to indicate
  7414. what is being sorted. The sorting key will be extracted from
  7415. the car of the elements of the table.
  7416. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7417. (unless sorting-type
  7418. (message
  7419. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7420. what)
  7421. (setq sorting-type (read-char-exclusive)))
  7422. (let ((dcst (downcase sorting-type))
  7423. extractfun comparefun)
  7424. ;; Define the appropriate functions
  7425. (cond
  7426. ((= dcst ?n)
  7427. (setq extractfun 'string-to-number
  7428. comparefun (if (= dcst sorting-type) '< '>)))
  7429. ((= dcst ?a)
  7430. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7431. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7432. comparefun (if (= dcst sorting-type)
  7433. 'string<
  7434. (lambda (a b) (and (not (string< a b))
  7435. (not (string= a b)))))))
  7436. ((= dcst ?t)
  7437. (setq extractfun
  7438. (lambda (x)
  7439. (if (or (string-match org-ts-regexp x)
  7440. (string-match org-ts-regexp-both x))
  7441. (org-float-time
  7442. (org-time-string-to-time (match-string 0 x)))
  7443. 0))
  7444. comparefun (if (= dcst sorting-type) '< '>)))
  7445. (t (error "Invalid sorting type `%c'" sorting-type)))
  7446. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7447. table)
  7448. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7449. ;;; The orgstruct minor mode
  7450. ;; Define a minor mode which can be used in other modes in order to
  7451. ;; integrate the org-mode structure editing commands.
  7452. ;; This is really a hack, because the org-mode structure commands use
  7453. ;; keys which normally belong to the major mode. Here is how it
  7454. ;; works: The minor mode defines all the keys necessary to operate the
  7455. ;; structure commands, but wraps the commands into a function which
  7456. ;; tests if the cursor is currently at a headline or a plain list
  7457. ;; item. If that is the case, the structure command is used,
  7458. ;; temporarily setting many Org-mode variables like regular
  7459. ;; expressions for filling etc. However, when any of those keys is
  7460. ;; used at a different location, function uses `key-binding' to look
  7461. ;; up if the key has an associated command in another currently active
  7462. ;; keymap (minor modes, major mode, global), and executes that
  7463. ;; command. There might be problems if any of the keys is otherwise
  7464. ;; used as a prefix key.
  7465. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7466. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7467. ;; addresses this by checking explicitly for both bindings.
  7468. (defvar orgstruct-mode-map (make-sparse-keymap)
  7469. "Keymap for the minor `orgstruct-mode'.")
  7470. (defvar org-local-vars nil
  7471. "List of local variables, for use by `orgstruct-mode'.")
  7472. ;;;###autoload
  7473. (define-minor-mode orgstruct-mode
  7474. "Toggle the minor mode `orgstruct-mode'.
  7475. This mode is for using Org-mode structure commands in other
  7476. modes. The following keys behave as if Org-mode were active, if
  7477. the cursor is on a headline, or on a plain list item (both as
  7478. defined by Org-mode).
  7479. M-up Move entry/item up
  7480. M-down Move entry/item down
  7481. M-left Promote
  7482. M-right Demote
  7483. M-S-up Move entry/item up
  7484. M-S-down Move entry/item down
  7485. M-S-left Promote subtree
  7486. M-S-right Demote subtree
  7487. M-q Fill paragraph and items like in Org-mode
  7488. C-c ^ Sort entries
  7489. C-c - Cycle list bullet
  7490. TAB Cycle item visibility
  7491. M-RET Insert new heading/item
  7492. S-M-RET Insert new TODO heading / Checkbox item
  7493. C-c C-c Set tags / toggle checkbox"
  7494. nil " OrgStruct" nil
  7495. (org-load-modules-maybe)
  7496. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7497. ;;;###autoload
  7498. (defun turn-on-orgstruct ()
  7499. "Unconditionally turn on `orgstruct-mode'."
  7500. (orgstruct-mode 1))
  7501. (defvar org-fb-vars nil)
  7502. (make-variable-buffer-local 'org-fb-vars)
  7503. (defun orgstruct++-mode (&optional arg)
  7504. "Toggle `orgstruct-mode', the enhanced version of it.
  7505. In addition to setting orgstruct-mode, this also exports all
  7506. indentation and autofilling variables from org-mode into the
  7507. buffer. It will also recognize item context in multiline items."
  7508. (interactive "P")
  7509. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7510. (if (< arg 1)
  7511. (progn (orgstruct-mode -1)
  7512. (mapc (lambda(v)
  7513. (org-set-local (car v)
  7514. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7515. org-fb-vars))
  7516. (orgstruct-mode 1)
  7517. (setq org-fb-vars nil)
  7518. (let (var val)
  7519. (mapc
  7520. (lambda (x)
  7521. (when (string-match
  7522. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7523. (symbol-name (car x)))
  7524. (setq var (car x) val (nth 1 x))
  7525. (push (list var `(quote ,(eval var))) org-fb-vars)
  7526. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7527. org-local-vars)
  7528. (org-set-local 'orgstruct-is-++ t))))
  7529. (defvar orgstruct-is-++ nil
  7530. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7531. (make-variable-buffer-local 'orgstruct-is-++)
  7532. ;;;###autoload
  7533. (defun turn-on-orgstruct++ ()
  7534. "Unconditionally turn on `orgstruct++-mode'."
  7535. (orgstruct++-mode 1))
  7536. (defun orgstruct-error ()
  7537. "Error when there is no default binding for a structure key."
  7538. (interactive)
  7539. (error "This key has no function outside structure elements"))
  7540. (defun orgstruct-setup ()
  7541. "Setup orgstruct keymaps."
  7542. (let ((nfunc 0)
  7543. (bindings
  7544. (list
  7545. '([(meta up)] org-metaup)
  7546. '([(meta down)] org-metadown)
  7547. '([(meta left)] org-metaleft)
  7548. '([(meta right)] org-metaright)
  7549. '([(meta shift up)] org-shiftmetaup)
  7550. '([(meta shift down)] org-shiftmetadown)
  7551. '([(meta shift left)] org-shiftmetaleft)
  7552. '([(meta shift right)] org-shiftmetaright)
  7553. '([?\e (up)] org-metaup)
  7554. '([?\e (down)] org-metadown)
  7555. '([?\e (left)] org-metaleft)
  7556. '([?\e (right)] org-metaright)
  7557. '([?\e (shift up)] org-shiftmetaup)
  7558. '([?\e (shift down)] org-shiftmetadown)
  7559. '([?\e (shift left)] org-shiftmetaleft)
  7560. '([?\e (shift right)] org-shiftmetaright)
  7561. '([(shift up)] org-shiftup)
  7562. '([(shift down)] org-shiftdown)
  7563. '([(shift left)] org-shiftleft)
  7564. '([(shift right)] org-shiftright)
  7565. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7566. '("\M-q" fill-paragraph)
  7567. '("\C-c^" org-sort)
  7568. '("\C-c-" org-cycle-list-bullet)))
  7569. elt key fun cmd)
  7570. (while (setq elt (pop bindings))
  7571. (setq nfunc (1+ nfunc))
  7572. (setq key (org-key (car elt))
  7573. fun (nth 1 elt)
  7574. cmd (orgstruct-make-binding fun nfunc key))
  7575. (org-defkey orgstruct-mode-map key cmd))
  7576. ;; Prevent an error for users who forgot to make autoloads
  7577. (require 'org-element)
  7578. ;; Special treatment needed for TAB and RET
  7579. (org-defkey orgstruct-mode-map [(tab)]
  7580. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7581. (org-defkey orgstruct-mode-map "\C-i"
  7582. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7583. (org-defkey orgstruct-mode-map "\M-\C-m"
  7584. (orgstruct-make-binding 'org-insert-heading 105
  7585. "\M-\C-m" [(meta return)]))
  7586. (org-defkey orgstruct-mode-map [(meta return)]
  7587. (orgstruct-make-binding 'org-insert-heading 106
  7588. [(meta return)] "\M-\C-m"))
  7589. (org-defkey orgstruct-mode-map [(shift meta return)]
  7590. (orgstruct-make-binding 'org-insert-todo-heading 107
  7591. [(meta return)] "\M-\C-m"))
  7592. (org-defkey orgstruct-mode-map "\e\C-m"
  7593. (orgstruct-make-binding 'org-insert-heading 108
  7594. "\e\C-m" [?\e (return)]))
  7595. (org-defkey orgstruct-mode-map [?\e (return)]
  7596. (orgstruct-make-binding 'org-insert-heading 109
  7597. [?\e (return)] "\e\C-m"))
  7598. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7599. (orgstruct-make-binding 'org-insert-todo-heading 110
  7600. [?\e (return)] "\e\C-m"))
  7601. (unless org-local-vars
  7602. (setq org-local-vars (org-get-local-variables)))
  7603. t))
  7604. (defun orgstruct-make-binding (fun n &rest keys)
  7605. "Create a function for binding in the structure minor mode.
  7606. FUN is the command to call inside a table. N is used to create a unique
  7607. command name. KEYS are keys that should be checked in for a command
  7608. to execute outside of tables."
  7609. (eval
  7610. (list 'defun
  7611. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7612. '(arg)
  7613. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7614. "Outside of structure, run the binding of `"
  7615. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7616. "'.")
  7617. '(interactive "p")
  7618. (list 'if
  7619. `(org-context-p 'headline 'item
  7620. (and orgstruct-is-++
  7621. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7622. 'item-body))
  7623. (list 'org-run-like-in-org-mode (list 'quote fun))
  7624. (list 'let '(orgstruct-mode)
  7625. (list 'call-interactively
  7626. (append '(or)
  7627. (mapcar (lambda (k)
  7628. (list 'key-binding k))
  7629. keys)
  7630. '('orgstruct-error))))))))
  7631. (defun org-contextualize-keys (alist contexts)
  7632. "Return valid elements in ALIST depending on CONTEXTS.
  7633. `org-agenda-custom-commands' or `org-capture-templates' are the
  7634. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7635. or `org-capture-templates-contexts' are the associated contexts
  7636. definitions."
  7637. (let ((contexts
  7638. ;; normalize contexts
  7639. (mapcar
  7640. (lambda(c) (cond ((listp (cadr c))
  7641. (list (car c) (car c) (cadr c)))
  7642. ((string= "" (cadr c))
  7643. (list (car c) (car c) (caddr c)))
  7644. (t c))) contexts))
  7645. (a alist) c r s)
  7646. ;; loop over all commands or templates
  7647. (while (setq c (pop a))
  7648. (let (vrules repl)
  7649. (cond
  7650. ((not (assoc (car c) contexts))
  7651. (push c r))
  7652. ((and (assoc (car c) contexts)
  7653. (setq vrules (org-contextualize-validate-key
  7654. (car c) contexts)))
  7655. (mapc (lambda (vr)
  7656. (when (not (equal (car vr) (cadr vr)))
  7657. (setq repl vr))) vrules)
  7658. (if (not repl) (push c r)
  7659. (push (cadr repl) s)
  7660. (push
  7661. (cons (car c)
  7662. (cdr (or (assoc (cadr repl) alist)
  7663. (error "Undefined key `%s' as contextual replacement for `%s'"
  7664. (cadr repl) (car c)))))
  7665. r))))))
  7666. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7667. (delq
  7668. nil
  7669. (delete-dups
  7670. (mapcar (lambda (x)
  7671. (let ((tpl (car x)))
  7672. (when (not (delq
  7673. nil
  7674. (mapcar (lambda(y)
  7675. (equal y tpl)) s))) x)))
  7676. (reverse r))))))
  7677. (defun org-contextualize-validate-key (key contexts)
  7678. "Check CONTEXTS for agenda or capture KEY."
  7679. (let (r rr res)
  7680. (while (setq r (pop contexts))
  7681. (mapc
  7682. (lambda (rr)
  7683. (when
  7684. (and (equal key (car r))
  7685. (if (functionp rr) (funcall rr)
  7686. (or (and (eq (car rr) 'in-file)
  7687. (buffer-file-name)
  7688. (string-match (cdr rr) (buffer-file-name)))
  7689. (and (eq (car rr) 'in-mode)
  7690. (string-match (cdr rr) (symbol-name major-mode)))
  7691. (when (and (eq (car rr) 'not-in-file)
  7692. (buffer-file-name))
  7693. (not (string-match (cdr rr) (buffer-file-name))))
  7694. (when (eq (car rr) 'not-in-mode)
  7695. (not (string-match (cdr rr) (symbol-name major-mode)))))))
  7696. (push r res)))
  7697. (car (last r))))
  7698. (delete-dups (delq nil res))))
  7699. (defun org-context-p (&rest contexts)
  7700. "Check if local context is any of CONTEXTS.
  7701. Possible values in the list of contexts are `table', `headline', and `item'."
  7702. (let ((pos (point)))
  7703. (goto-char (point-at-bol))
  7704. (prog1 (or (and (memq 'table contexts)
  7705. (looking-at "[ \t]*|"))
  7706. (and (memq 'headline contexts)
  7707. (looking-at org-outline-regexp))
  7708. (and (memq 'item contexts)
  7709. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7710. (and (memq 'item-body contexts)
  7711. (org-in-item-p)))
  7712. (goto-char pos))))
  7713. (defun org-get-local-variables ()
  7714. "Return a list of all local variables in an Org mode buffer."
  7715. (let (varlist)
  7716. (with-current-buffer (get-buffer-create "*Org tmp*")
  7717. (erase-buffer)
  7718. (org-mode)
  7719. (setq varlist (buffer-local-variables)))
  7720. (kill-buffer "*Org tmp*")
  7721. (delq nil
  7722. (mapcar
  7723. (lambda (x)
  7724. (setq x
  7725. (if (symbolp x)
  7726. (list x)
  7727. (list (car x) (list 'quote (cdr x)))))
  7728. (if (string-match
  7729. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7730. (symbol-name (car x)))
  7731. x nil))
  7732. varlist))))
  7733. (defun org-clone-local-variables (from-buffer &optional regexp)
  7734. "Clone local variables from FROM-BUFFER.
  7735. Optional argument REGEXP selects variables to clone."
  7736. (mapc
  7737. (lambda (pair)
  7738. (and (symbolp (car pair))
  7739. (or (null regexp)
  7740. (string-match regexp (symbol-name (car pair))))
  7741. (set (make-local-variable (car pair))
  7742. (cdr pair))))
  7743. (buffer-local-variables from-buffer)))
  7744. ;;;###autoload
  7745. (defun org-run-like-in-org-mode (cmd)
  7746. "Run a command, pretending that the current buffer is in Org-mode.
  7747. This will temporarily bind local variables that are typically bound in
  7748. Org-mode to the values they have in Org-mode, and then interactively
  7749. call CMD."
  7750. (org-load-modules-maybe)
  7751. (unless org-local-vars
  7752. (setq org-local-vars (org-get-local-variables)))
  7753. (eval (list 'let org-local-vars
  7754. (list 'call-interactively (list 'quote cmd)))))
  7755. ;;;; Archiving
  7756. (defun org-get-category (&optional pos force-refresh)
  7757. "Get the category applying to position POS."
  7758. (save-match-data
  7759. (if force-refresh (org-refresh-category-properties))
  7760. (let ((pos (or pos (point))))
  7761. (or (get-text-property pos 'org-category)
  7762. (progn (org-refresh-category-properties)
  7763. (get-text-property pos 'org-category))))))
  7764. (defun org-refresh-category-properties ()
  7765. "Refresh category text properties in the buffer."
  7766. (let ((case-fold-search t)
  7767. (inhibit-read-only t)
  7768. (def-cat (cond
  7769. ((null org-category)
  7770. (if buffer-file-name
  7771. (file-name-sans-extension
  7772. (file-name-nondirectory buffer-file-name))
  7773. "???"))
  7774. ((symbolp org-category) (symbol-name org-category))
  7775. (t org-category)))
  7776. beg end cat pos optionp)
  7777. (org-unmodified
  7778. (save-excursion
  7779. (save-restriction
  7780. (widen)
  7781. (goto-char (point-min))
  7782. (put-text-property (point) (point-max) 'org-category def-cat)
  7783. (while (re-search-forward
  7784. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7785. (setq pos (match-end 0)
  7786. optionp (equal (char-after (match-beginning 0)) ?#)
  7787. cat (org-trim (match-string 2)))
  7788. (if optionp
  7789. (setq beg (point-at-bol) end (point-max))
  7790. (org-back-to-heading t)
  7791. (setq beg (point) end (org-end-of-subtree t t)))
  7792. (put-text-property beg end 'org-category cat)
  7793. (put-text-property beg end 'org-category-position beg)
  7794. (goto-char pos)))))))
  7795. (defun org-refresh-properties (dprop tprop)
  7796. "Refresh buffer text properties.
  7797. DPROP is the drawer property and TPROP is the corresponding text
  7798. property to set."
  7799. (let ((case-fold-search t)
  7800. (inhibit-read-only t) p)
  7801. (org-unmodified
  7802. (save-excursion
  7803. (save-restriction
  7804. (widen)
  7805. (goto-char (point-min))
  7806. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  7807. (setq p (org-match-string-no-properties 1))
  7808. (save-excursion
  7809. (org-back-to-heading t)
  7810. (put-text-property
  7811. (point-at-bol) (point-at-eol) tprop p))))))))
  7812. ;;;; Link Stuff
  7813. ;;; Link abbreviations
  7814. (defun org-link-expand-abbrev (link)
  7815. "Apply replacements as defined in `org-link-abbrev-alist'."
  7816. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7817. (let* ((key (match-string 1 link))
  7818. (as (or (assoc key org-link-abbrev-alist-local)
  7819. (assoc key org-link-abbrev-alist)))
  7820. (tag (and (match-end 2) (match-string 3 link)))
  7821. rpl)
  7822. (if (not as)
  7823. link
  7824. (setq rpl (cdr as))
  7825. (cond
  7826. ((symbolp rpl) (funcall rpl tag))
  7827. ((string-match "%(\\([^)]+\\))" rpl)
  7828. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7829. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7830. ((string-match "%h" rpl)
  7831. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7832. (t (concat rpl tag)))))
  7833. link))
  7834. ;;; Storing and inserting links
  7835. (defvar org-insert-link-history nil
  7836. "Minibuffer history for links inserted with `org-insert-link'.")
  7837. (defvar org-stored-links nil
  7838. "Contains the links stored with `org-store-link'.")
  7839. (defvar org-store-link-plist nil
  7840. "Plist with info about the most recently link created with `org-store-link'.")
  7841. (defvar org-link-protocols nil
  7842. "Link protocols added to Org-mode using `org-add-link-type'.")
  7843. (defvar org-store-link-functions nil
  7844. "List of functions that are called to create and store a link.
  7845. Each function will be called in turn until one returns a non-nil
  7846. value. Each function should check if it is responsible for creating
  7847. this link (for example by looking at the major mode).
  7848. If not, it must exit and return nil.
  7849. If yes, it should return a non-nil value after a calling
  7850. `org-store-link-props' with a list of properties and values.
  7851. Special properties are:
  7852. :type The link prefix, like \"http\". This must be given.
  7853. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7854. This is obligatory as well.
  7855. :description Optional default description for the second pair
  7856. of brackets in an Org-mode link. The user can still change
  7857. this when inserting this link into an Org-mode buffer.
  7858. In addition to these, any additional properties can be specified
  7859. and then used in capture templates.")
  7860. (defun org-add-link-type (type &optional follow export)
  7861. "Add TYPE to the list of `org-link-types'.
  7862. Re-compute all regular expressions depending on `org-link-types'
  7863. FOLLOW and EXPORT are two functions.
  7864. FOLLOW should take the link path as the single argument and do whatever
  7865. is necessary to follow the link, for example find a file or display
  7866. a mail message.
  7867. EXPORT should format the link path for export to one of the export formats.
  7868. It should be a function accepting three arguments:
  7869. path the path of the link, the text after the prefix (like \"http:\")
  7870. desc the description of the link, if any, or a description added by
  7871. org-export-normalize-links if there is none
  7872. format the export format, a symbol like `html' or `latex' or `ascii'..
  7873. The function may use the FORMAT information to return different values
  7874. depending on the format. The return value will be put literally into
  7875. the exported file. If the return value is nil, this means Org should
  7876. do what it normally does with links which do not have EXPORT defined.
  7877. Org-mode has a built-in default for exporting links. If you are happy with
  7878. this default, there is no need to define an export function for the link
  7879. type. For a simple example of an export function, see `org-bbdb.el'."
  7880. (add-to-list 'org-link-types type t)
  7881. (org-make-link-regexps)
  7882. (if (assoc type org-link-protocols)
  7883. (setcdr (assoc type org-link-protocols) (list follow export))
  7884. (push (list type follow export) org-link-protocols)))
  7885. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  7886. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  7887. ;;;###autoload
  7888. (defun org-store-link (arg)
  7889. "\\<org-mode-map>Store an org-link to the current location.
  7890. This link is added to `org-stored-links' and can later be inserted
  7891. into an org-buffer with \\[org-insert-link].
  7892. For some link types, a prefix arg is interpreted:
  7893. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7894. For file links, arg negates `org-context-in-file-links'."
  7895. (interactive "P")
  7896. (org-load-modules-maybe)
  7897. (setq org-store-link-plist nil) ; reset
  7898. (org-with-limited-levels
  7899. (let (link cpltxt desc description search txt custom-id agenda-link)
  7900. (cond
  7901. ((run-hook-with-args-until-success 'org-store-link-functions)
  7902. (setq link (plist-get org-store-link-plist :link)
  7903. desc (or (plist-get org-store-link-plist :description) link)))
  7904. ((org-src-edit-buffer-p)
  7905. (let (label gc)
  7906. (while (or (not label)
  7907. (save-excursion
  7908. (save-restriction
  7909. (widen)
  7910. (goto-char (point-min))
  7911. (re-search-forward
  7912. (regexp-quote (format org-coderef-label-format label))
  7913. nil t))))
  7914. (when label (message "Label exists already") (sit-for 2))
  7915. (setq label (read-string "Code line label: " label)))
  7916. (end-of-line 1)
  7917. (setq link (format org-coderef-label-format label))
  7918. (setq gc (- 79 (length link)))
  7919. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7920. (insert link)
  7921. (setq link (concat "(" label ")") desc nil)))
  7922. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7923. ;; We are in the agenda, link to referenced location
  7924. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7925. (get-text-property (point) 'org-marker))))
  7926. (when m
  7927. (org-with-point-at m
  7928. (setq agenda-link
  7929. (if (org-called-interactively-p 'any)
  7930. (call-interactively 'org-store-link)
  7931. (org-store-link nil)))))))
  7932. ((eq major-mode 'calendar-mode)
  7933. (let ((cd (calendar-cursor-to-date)))
  7934. (setq link
  7935. (format-time-string
  7936. (car org-time-stamp-formats)
  7937. (apply 'encode-time
  7938. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7939. nil nil nil))))
  7940. (org-store-link-props :type "calendar" :date cd)))
  7941. ((eq major-mode 'help-mode)
  7942. (setq link (concat "help:" (save-excursion
  7943. (goto-char (point-min))
  7944. (looking-at "^[^ ]+")
  7945. (match-string 0))))
  7946. (org-store-link-props :type "help"))
  7947. ((eq major-mode 'w3-mode)
  7948. (setq cpltxt (if (and (buffer-name)
  7949. (not (string-match "Untitled" (buffer-name))))
  7950. (buffer-name)
  7951. (url-view-url t))
  7952. link (url-view-url t))
  7953. (org-store-link-props :type "w3" :url (url-view-url t)))
  7954. ((eq major-mode 'w3m-mode)
  7955. (setq cpltxt (or w3m-current-title w3m-current-url)
  7956. link w3m-current-url)
  7957. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7958. ((setq search (run-hook-with-args-until-success
  7959. 'org-create-file-search-functions))
  7960. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7961. "::" search))
  7962. (setq cpltxt (or description link)))
  7963. ((eq major-mode 'image-mode)
  7964. (setq cpltxt (concat "file:"
  7965. (abbreviate-file-name buffer-file-name))
  7966. link cpltxt)
  7967. (org-store-link-props :type "image" :file buffer-file-name))
  7968. ((eq major-mode 'dired-mode)
  7969. ;; link to the file in the current line
  7970. (let ((file (dired-get-filename nil t)))
  7971. (setq file (if file
  7972. (abbreviate-file-name
  7973. (expand-file-name (dired-get-filename nil t)))
  7974. ;; otherwise, no file so use current directory.
  7975. default-directory))
  7976. (setq cpltxt (concat "file:" file)
  7977. link cpltxt)))
  7978. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  7979. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7980. (cond
  7981. ((org-in-regexp "<<\\(.*?\\)>>")
  7982. (setq cpltxt
  7983. (concat "file:"
  7984. (abbreviate-file-name
  7985. (buffer-file-name (buffer-base-buffer)))
  7986. "::" (match-string 1))
  7987. link cpltxt))
  7988. ((and (featurep 'org-id)
  7989. (or (eq org-id-link-to-org-use-id t)
  7990. (and (org-called-interactively-p 'any)
  7991. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  7992. (and (eq org-id-link-to-org-use-id
  7993. 'create-if-interactive-and-no-custom-id)
  7994. (not custom-id))))
  7995. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  7996. ;; We can make a link using the ID.
  7997. (setq link (condition-case nil
  7998. (prog1 (org-id-store-link)
  7999. (setq desc (plist-get org-store-link-plist :description)))
  8000. (error
  8001. ;; probably before first headline, link to file only
  8002. (concat "file:"
  8003. (abbreviate-file-name
  8004. (buffer-file-name (buffer-base-buffer))))))))
  8005. (t
  8006. ;; Just link to current headline
  8007. (setq cpltxt (concat "file:"
  8008. (abbreviate-file-name
  8009. (buffer-file-name (buffer-base-buffer)))))
  8010. ;; Add a context search string
  8011. (when (org-xor org-context-in-file-links arg)
  8012. (setq txt (cond
  8013. ((org-at-heading-p) nil)
  8014. ((org-region-active-p)
  8015. (buffer-substring (region-beginning) (region-end)))))
  8016. (when (or (null txt) (string-match "\\S-" txt))
  8017. (setq cpltxt
  8018. (concat cpltxt "::"
  8019. (condition-case nil
  8020. (org-make-org-heading-search-string txt)
  8021. (error "")))
  8022. desc (or (nth 4 (ignore-errors
  8023. (org-heading-components))) "NONE"))))
  8024. (if (string-match "::\\'" cpltxt)
  8025. (setq cpltxt (substring cpltxt 0 -2)))
  8026. (setq link cpltxt))))
  8027. ((buffer-file-name (buffer-base-buffer))
  8028. ;; Just link to this file here.
  8029. (setq cpltxt (concat "file:"
  8030. (abbreviate-file-name
  8031. (buffer-file-name (buffer-base-buffer)))))
  8032. ;; Add a context string
  8033. (when (org-xor org-context-in-file-links arg)
  8034. (setq txt (if (org-region-active-p)
  8035. (buffer-substring (region-beginning) (region-end))
  8036. (buffer-substring (point-at-bol) (point-at-eol))))
  8037. ;; Only use search option if there is some text.
  8038. (when (string-match "\\S-" txt)
  8039. (setq cpltxt
  8040. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8041. desc "NONE")))
  8042. (setq link cpltxt))
  8043. ((org-called-interactively-p 'interactive)
  8044. (error "Cannot link to a buffer which is not visiting a file"))
  8045. (t (setq link nil)))
  8046. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8047. (setq link (or link cpltxt)
  8048. desc (or desc cpltxt))
  8049. (if (equal desc "NONE") (setq desc nil))
  8050. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8051. (progn
  8052. (setq org-stored-links
  8053. (cons (list link desc) org-stored-links))
  8054. (message "Stored: %s" (or desc link))
  8055. (when custom-id
  8056. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8057. "::#" custom-id))
  8058. (setq org-stored-links
  8059. (cons (list link desc) org-stored-links))))
  8060. (or agenda-link (and link (org-make-link-string link desc)))))))
  8061. (defun org-store-link-props (&rest plist)
  8062. "Store link properties, extract names and addresses."
  8063. (let (x adr)
  8064. (when (setq x (plist-get plist :from))
  8065. (setq adr (mail-extract-address-components x))
  8066. (setq plist (plist-put plist :fromname (car adr)))
  8067. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8068. (when (setq x (plist-get plist :to))
  8069. (setq adr (mail-extract-address-components x))
  8070. (setq plist (plist-put plist :toname (car adr)))
  8071. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8072. (let ((from (plist-get plist :from))
  8073. (to (plist-get plist :to)))
  8074. (when (and from to org-from-is-user-regexp)
  8075. (setq plist
  8076. (plist-put plist :fromto
  8077. (if (string-match org-from-is-user-regexp from)
  8078. (concat "to %t")
  8079. (concat "from %f"))))))
  8080. (setq org-store-link-plist plist))
  8081. (defun org-add-link-props (&rest plist)
  8082. "Add these properties to the link property list."
  8083. (let (key value)
  8084. (while plist
  8085. (setq key (pop plist) value (pop plist))
  8086. (setq org-store-link-plist
  8087. (plist-put org-store-link-plist key value)))))
  8088. (defun org-email-link-description (&optional fmt)
  8089. "Return the description part of an email link.
  8090. This takes information from `org-store-link-plist' and formats it
  8091. according to FMT (default from `org-email-link-description-format')."
  8092. (setq fmt (or fmt org-email-link-description-format))
  8093. (let* ((p org-store-link-plist)
  8094. (to (plist-get p :toaddress))
  8095. (from (plist-get p :fromaddress))
  8096. (table
  8097. (list
  8098. (cons "%c" (plist-get p :fromto))
  8099. (cons "%F" (plist-get p :from))
  8100. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8101. (cons "%T" (plist-get p :to))
  8102. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8103. (cons "%s" (plist-get p :subject))
  8104. (cons "%d" (plist-get p :date))
  8105. (cons "%m" (plist-get p :message-id)))))
  8106. (when (string-match "%c" fmt)
  8107. ;; Check if the user wrote this message
  8108. (if (and org-from-is-user-regexp from to
  8109. (save-match-data (string-match org-from-is-user-regexp from)))
  8110. (setq fmt (replace-match "to %t" t t fmt))
  8111. (setq fmt (replace-match "from %f" t t fmt))))
  8112. (org-replace-escapes fmt table)))
  8113. (defun org-make-org-heading-search-string (&optional string heading)
  8114. "Make search string for STRING or current headline."
  8115. (interactive)
  8116. (let ((s (or string (org-get-heading)))
  8117. (lines org-context-in-file-links))
  8118. (unless (and string (not heading))
  8119. ;; We are using a headline, clean up garbage in there.
  8120. (if (string-match org-todo-regexp s)
  8121. (setq s (replace-match "" t t s)))
  8122. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8123. (setq s (replace-match "" t t s)))
  8124. (setq s (org-trim s))
  8125. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8126. org-comment-string "\\)") s)
  8127. (setq s (replace-match "" t t s)))
  8128. (while (string-match org-ts-regexp s)
  8129. (setq s (replace-match "" t t s))))
  8130. (or string (setq s (concat "*" s))) ; Add * for headlines
  8131. (when (and string (integerp lines) (> lines 0))
  8132. (let ((slines (org-split-string s "\n")))
  8133. (when (< lines (length slines))
  8134. (setq s (mapconcat
  8135. 'identity
  8136. (reverse (nthcdr (- (length slines) lines)
  8137. (reverse slines))) "\n")))))
  8138. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8139. (defun org-make-link-string (link &optional description)
  8140. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8141. (unless (string-match "\\S-" link)
  8142. (error "Empty link"))
  8143. (when (and description
  8144. (stringp description)
  8145. (not (string-match "\\S-" description)))
  8146. (setq description nil))
  8147. (when (stringp description)
  8148. ;; Remove brackets from the description, they are fatal.
  8149. (while (string-match "\\[" description)
  8150. (setq description (replace-match "{" t t description)))
  8151. (while (string-match "\\]" description)
  8152. (setq description (replace-match "}" t t description))))
  8153. (when (equal link description)
  8154. ;; No description needed, it is identical
  8155. (setq description nil))
  8156. (when (and (not description)
  8157. (not (string-match (org-image-file-name-regexp) link))
  8158. (not (equal link (org-link-escape link))))
  8159. (setq description (org-extract-attributes link)))
  8160. (setq link
  8161. (cond ((string-match (org-image-file-name-regexp) link) link)
  8162. ((string-match org-link-types-re link)
  8163. (concat (match-string 1 link)
  8164. (org-link-escape (substring link (match-end 1)))))
  8165. (t (org-link-escape link))))
  8166. (concat "[[" link "]"
  8167. (if description (concat "[" description "]") "")
  8168. "]"))
  8169. (defconst org-link-escape-chars
  8170. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8171. "List of characters that should be escaped in link.
  8172. This is the list that is used for internal purposes.")
  8173. (defconst org-link-escape-chars-browser
  8174. '(?\ )
  8175. "List of escapes for characters that are problematic in links.
  8176. This is the list that is used before handing over to the browser.")
  8177. (defun org-link-escape (text &optional table merge)
  8178. "Return percent escaped representation of TEXT.
  8179. TEXT is a string with the text to escape.
  8180. Optional argument TABLE is a list with characters that should be
  8181. escaped. When nil, `org-link-escape-chars' is used.
  8182. If optional argument MERGE is set, merge TABLE into
  8183. `org-link-escape-chars'."
  8184. (cond
  8185. ((and table merge)
  8186. (mapc (lambda (defchr)
  8187. (unless (member defchr table)
  8188. (setq table (cons defchr table)))) org-link-escape-chars))
  8189. ((null table)
  8190. (setq table org-link-escape-chars)))
  8191. (mapconcat
  8192. (lambda (char)
  8193. (if (or (member char table)
  8194. (and (or (< char 32) (= char 37) (> char 126))
  8195. org-url-hexify-p))
  8196. (mapconcat (lambda (sequence-element)
  8197. (format "%%%.2X" sequence-element))
  8198. (or (encode-coding-char char 'utf-8)
  8199. (error "Unable to percent escape character: %s"
  8200. (char-to-string char))) "")
  8201. (char-to-string char))) text ""))
  8202. (defun org-link-unescape (str)
  8203. "Unhex hexified Unicode strings as returned from the JavaScript function
  8204. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8205. (unless (and (null str) (string= "" str))
  8206. (let ((pos 0) (case-fold-search t) unhexed)
  8207. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8208. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8209. (setq str (replace-match unhexed t t str))
  8210. (setq pos (+ pos (length unhexed))))))
  8211. str)
  8212. (defun org-link-unescape-compound (hex)
  8213. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8214. Note: this function also decodes single byte encodings like
  8215. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8216. (save-match-data
  8217. (let* ((bytes (cdr (split-string hex "%")))
  8218. (ret "")
  8219. (eat 0)
  8220. (sum 0))
  8221. (while bytes
  8222. (let* ((val (string-to-number (pop bytes) 16))
  8223. (shift-xor
  8224. (if (= 0 eat)
  8225. (cond
  8226. ((>= val 252) (cons 6 252))
  8227. ((>= val 248) (cons 5 248))
  8228. ((>= val 240) (cons 4 240))
  8229. ((>= val 224) (cons 3 224))
  8230. ((>= val 192) (cons 2 192))
  8231. (t (cons 0 0)))
  8232. (cons 6 128))))
  8233. (if (>= val 192) (setq eat (car shift-xor)))
  8234. (setq val (logxor val (cdr shift-xor)))
  8235. (setq sum (+ (lsh sum (car shift-xor)) val))
  8236. (if (> eat 0) (setq eat (- eat 1)))
  8237. (cond
  8238. ((= 0 eat) ;multi byte
  8239. (setq ret (concat ret (org-char-to-string sum)))
  8240. (setq sum 0))
  8241. ((not bytes) ; single byte(s)
  8242. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8243. )) ;; end (while bytes
  8244. ret )))
  8245. (defun org-link-unescape-single-byte-sequence (hex)
  8246. "Unhexify hex-encoded single byte character sequences."
  8247. (mapconcat (lambda (byte)
  8248. (char-to-string (string-to-number byte 16)))
  8249. (cdr (split-string hex "%")) ""))
  8250. (defun org-xor (a b)
  8251. "Exclusive or."
  8252. (if a (not b) b))
  8253. (defun org-fixup-message-id-for-http (s)
  8254. "Replace special characters in a message id, so it can be used in an http query."
  8255. (when (string-match "%" s)
  8256. (setq s (mapconcat (lambda (c)
  8257. (if (eq c ?%)
  8258. "%25"
  8259. (char-to-string c)))
  8260. s "")))
  8261. (while (string-match "<" s)
  8262. (setq s (replace-match "%3C" t t s)))
  8263. (while (string-match ">" s)
  8264. (setq s (replace-match "%3E" t t s)))
  8265. (while (string-match "@" s)
  8266. (setq s (replace-match "%40" t t s)))
  8267. s)
  8268. (defun org-link-prettify (link)
  8269. "Return a human-readable representation of LINK.
  8270. The car of LINK must be a raw link the cdr of LINK must be either
  8271. a link description or nil."
  8272. (let ((desc (or (cadr link) "<no description>")))
  8273. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8274. "<" (car link) ">")))
  8275. ;;;###autoload
  8276. (defun org-insert-link-global ()
  8277. "Insert a link like Org-mode does.
  8278. This command can be called in any mode to insert a link in Org-mode syntax."
  8279. (interactive)
  8280. (org-load-modules-maybe)
  8281. (org-run-like-in-org-mode 'org-insert-link))
  8282. (defun org-insert-all-links (&optional keep)
  8283. "Insert all links in `org-stored-links'."
  8284. (interactive "P")
  8285. (let ((links (copy-sequence org-stored-links)) l)
  8286. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8287. (insert "- ")
  8288. (org-insert-link nil (car l) (cadr l))
  8289. (insert "\n"))))
  8290. (defun org-link-fontify-links-to-this-file ()
  8291. "Fontify links to the current file in `org-stored-links'."
  8292. (let ((f (buffer-file-name)) a b)
  8293. (setq a (mapcar (lambda(l)
  8294. (let ((ll (car l)))
  8295. (when (and (string-match "^file:\\(.+\\)::" ll)
  8296. (equal f (expand-file-name (match-string 1 ll))))
  8297. ll)))
  8298. org-stored-links))
  8299. (when (featurep 'org-id)
  8300. (setq b (mapcar (lambda(l)
  8301. (let ((ll (car l)))
  8302. (when (and (string-match "^id:\\(.+\\)$" ll)
  8303. (equal f (expand-file-name
  8304. (or (org-id-find-id-file
  8305. (match-string 1 ll)) ""))))
  8306. ll)))
  8307. org-stored-links)))
  8308. (mapcar (lambda(l)
  8309. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8310. (delq nil (append a b)))))
  8311. (defvar org-link-links-in-this-file nil)
  8312. (defun org-insert-link (&optional complete-file link-location default-description)
  8313. "Insert a link. At the prompt, enter the link.
  8314. Completion can be used to insert any of the link protocol prefixes like
  8315. http or ftp in use.
  8316. The history can be used to select a link previously stored with
  8317. `org-store-link'. When the empty string is entered (i.e. if you just
  8318. press RET at the prompt), the link defaults to the most recently
  8319. stored link. As SPC triggers completion in the minibuffer, you need to
  8320. use M-SPC or C-q SPC to force the insertion of a space character.
  8321. You will also be prompted for a description, and if one is given, it will
  8322. be displayed in the buffer instead of the link.
  8323. If there is already a link at point, this command will allow you to edit link
  8324. and description parts.
  8325. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8326. be selected using completion. The path to the file will be relative to the
  8327. current directory if the file is in the current directory or a subdirectory.
  8328. Otherwise, the link will be the absolute path as completed in the minibuffer
  8329. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8330. option `org-link-file-path-type'.
  8331. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8332. the current directory or below.
  8333. With three \\[universal-argument] prefixes, negate the meaning of
  8334. `org-keep-stored-link-after-insertion'.
  8335. If `org-make-link-description-function' is non-nil, this function will be
  8336. called with the link target, and the result will be the default
  8337. link description.
  8338. If the LINK-LOCATION parameter is non-nil, this value will be
  8339. used as the link location instead of reading one interactively.
  8340. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8341. be used as the default description."
  8342. (interactive "P")
  8343. (let* ((wcf (current-window-configuration))
  8344. (region (if (org-region-active-p)
  8345. (buffer-substring (region-beginning) (region-end))))
  8346. (remove (and region (list (region-beginning) (region-end))))
  8347. (desc region)
  8348. tmphist ; byte-compile incorrectly complains about this
  8349. (link link-location)
  8350. (abbrevs org-link-abbrev-alist-local)
  8351. entry file all-prefixes auto-desc)
  8352. (cond
  8353. (link-location) ; specified by arg, just use it.
  8354. ((org-in-regexp org-bracket-link-regexp 1)
  8355. ;; We do have a link at point, and we are going to edit it.
  8356. (setq remove (list (match-beginning 0) (match-end 0)))
  8357. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8358. (setq link (read-string "Link: "
  8359. (org-link-unescape
  8360. (org-match-string-no-properties 1)))))
  8361. ((or (org-in-regexp org-angle-link-re)
  8362. (org-in-regexp org-plain-link-re))
  8363. ;; Convert to bracket link
  8364. (setq remove (list (match-beginning 0) (match-end 0))
  8365. link (read-string "Link: "
  8366. (org-remove-angle-brackets (match-string 0)))))
  8367. ((member complete-file '((4) (16)))
  8368. ;; Completing read for file names.
  8369. (setq link (org-file-complete-link complete-file)))
  8370. (t
  8371. ;; Read link, with completion for stored links.
  8372. (org-link-fontify-links-to-this-file)
  8373. (org-switch-to-buffer-other-window "*Org Links*")
  8374. (with-current-buffer "*Org Links*"
  8375. (erase-buffer)
  8376. (insert "Insert a link.
  8377. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8378. (when org-stored-links
  8379. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8380. (insert (mapconcat 'org-link-prettify
  8381. (reverse org-stored-links) "\n")))
  8382. (goto-char (point-min)))
  8383. (let ((cw (selected-window)))
  8384. (select-window (get-buffer-window "*Org Links*" 'visible))
  8385. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8386. (unless (pos-visible-in-window-p (point-max))
  8387. (org-fit-window-to-buffer))
  8388. (and (window-live-p cw) (select-window cw)))
  8389. ;; Fake a link history, containing the stored links.
  8390. (setq tmphist (append (mapcar 'car org-stored-links)
  8391. org-insert-link-history))
  8392. (setq all-prefixes (append (mapcar 'car abbrevs)
  8393. (mapcar 'car org-link-abbrev-alist)
  8394. org-link-types))
  8395. (unwind-protect
  8396. (progn
  8397. (setq link
  8398. (let ((org-completion-use-ido nil)
  8399. (org-completion-use-iswitchb nil))
  8400. (org-completing-read
  8401. "Link: "
  8402. (append
  8403. (mapcar (lambda (x) (list (concat x ":")))
  8404. all-prefixes)
  8405. (mapcar 'car org-stored-links)
  8406. (mapcar 'cadr org-stored-links))
  8407. nil nil nil
  8408. 'tmphist
  8409. (caar org-stored-links))))
  8410. (if (not (string-match "\\S-" link))
  8411. (error "No link selected"))
  8412. (mapc (lambda(l)
  8413. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8414. org-stored-links)
  8415. (if (or (member link all-prefixes)
  8416. (and (equal ":" (substring link -1))
  8417. (member (substring link 0 -1) all-prefixes)
  8418. (setq link (substring link 0 -1))))
  8419. (setq link (org-link-try-special-completion link))))
  8420. (set-window-configuration wcf)
  8421. (kill-buffer "*Org Links*"))
  8422. (setq entry (assoc link org-stored-links))
  8423. (or entry (push link org-insert-link-history))
  8424. (setq desc (or desc (nth 1 entry)))))
  8425. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8426. (not org-keep-stored-link-after-insertion))
  8427. (setq org-stored-links (delq (assoc link org-stored-links)
  8428. org-stored-links)))
  8429. (if (string-match org-plain-link-re link)
  8430. ;; URL-like link, normalize the use of angular brackets.
  8431. (setq link (org-remove-angle-brackets link)))
  8432. ;; Check if we are linking to the current file with a search
  8433. ;; option If yes, simplify the link by using only the search
  8434. ;; option.
  8435. (when (and buffer-file-name
  8436. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8437. (let* ((path (match-string 1 link))
  8438. (case-fold-search nil)
  8439. (search (match-string 2 link)))
  8440. (save-match-data
  8441. (if (equal (file-truename buffer-file-name) (file-truename path))
  8442. ;; We are linking to this same file, with a search option
  8443. (setq link search)))))
  8444. ;; Check if we can/should use a relative path. If yes, simplify the link
  8445. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8446. (let* ((type (match-string 1 link))
  8447. (path (match-string 2 link))
  8448. (origpath path)
  8449. (case-fold-search nil))
  8450. (cond
  8451. ((or (eq org-link-file-path-type 'absolute)
  8452. (equal complete-file '(16)))
  8453. (setq path (abbreviate-file-name (expand-file-name path))))
  8454. ((eq org-link-file-path-type 'noabbrev)
  8455. (setq path (expand-file-name path)))
  8456. ((eq org-link-file-path-type 'relative)
  8457. (setq path (file-relative-name path)))
  8458. (t
  8459. (save-match-data
  8460. (if (string-match (concat "^" (regexp-quote
  8461. (expand-file-name
  8462. (file-name-as-directory
  8463. default-directory))))
  8464. (expand-file-name path))
  8465. ;; We are linking a file with relative path name.
  8466. (setq path (substring (expand-file-name path)
  8467. (match-end 0)))
  8468. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8469. (setq link (concat type path))
  8470. (if (equal desc origpath)
  8471. (setq desc path))))
  8472. (if org-make-link-description-function
  8473. (setq desc
  8474. (or (condition-case nil
  8475. (funcall org-make-link-description-function link desc)
  8476. (error (progn (message "Can't get link description from `%s'"
  8477. (symbol-name org-make-link-description-function))
  8478. (sit-for 2) nil)))
  8479. (read-string "Description: " default-description)))
  8480. (if default-description (setq desc default-description)
  8481. (setq desc (or (and auto-desc desc)
  8482. (read-string "Description: " desc)))))
  8483. (unless (string-match "\\S-" desc) (setq desc nil))
  8484. (if remove (apply 'delete-region remove))
  8485. (insert (org-make-link-string link desc))))
  8486. (defun org-link-try-special-completion (type)
  8487. "If there is completion support for link type TYPE, offer it."
  8488. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8489. (if (functionp fun)
  8490. (funcall fun)
  8491. (read-string "Link (no completion support): " (concat type ":")))))
  8492. (defun org-file-complete-link (&optional arg)
  8493. "Create a file link using completion."
  8494. (let (file link)
  8495. (setq file (read-file-name "File: "))
  8496. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8497. (pwd1 (file-name-as-directory (abbreviate-file-name
  8498. (expand-file-name ".")))))
  8499. (cond
  8500. ((equal arg '(16))
  8501. (setq link (concat
  8502. "file:"
  8503. (abbreviate-file-name (expand-file-name file)))))
  8504. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8505. (setq link (concat "file:" (match-string 1 file))))
  8506. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8507. (expand-file-name file))
  8508. (setq link (concat
  8509. "file:" (match-string 1 (expand-file-name file)))))
  8510. (t (setq link (concat "file:" file)))))
  8511. link))
  8512. (defun org-completing-read (&rest args)
  8513. "Completing-read with SPACE being a normal character."
  8514. (let ((enable-recursive-minibuffers t)
  8515. (minibuffer-local-completion-map
  8516. (copy-keymap minibuffer-local-completion-map)))
  8517. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8518. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8519. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8520. (apply 'org-icompleting-read args)))
  8521. (defun org-completing-read-no-i (&rest args)
  8522. (let (org-completion-use-ido org-completion-use-iswitchb)
  8523. (apply 'org-completing-read args)))
  8524. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8525. "Use iswitch as a completing-read replacement to choose from choices.
  8526. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8527. from."
  8528. (let* ((iswitchb-use-virtual-buffers nil)
  8529. (iswitchb-make-buflist-hook
  8530. (lambda ()
  8531. (setq iswitchb-temp-buflist choices))))
  8532. (iswitchb-read-buffer prompt)))
  8533. (defun org-icompleting-read (&rest args)
  8534. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8535. (org-without-partial-completion
  8536. (if (and org-completion-use-ido
  8537. (fboundp 'ido-completing-read)
  8538. (boundp 'ido-mode) ido-mode
  8539. (listp (second args)))
  8540. (let ((ido-enter-matching-directory nil))
  8541. (apply 'ido-completing-read (concat (car args))
  8542. (if (consp (car (nth 1 args)))
  8543. (mapcar 'car (nth 1 args))
  8544. (nth 1 args))
  8545. (cddr args)))
  8546. (if (and org-completion-use-iswitchb
  8547. (boundp 'iswitchb-mode) iswitchb-mode
  8548. (listp (second args)))
  8549. (apply 'org-iswitchb-completing-read (concat (car args))
  8550. (if (consp (car (nth 1 args)))
  8551. (mapcar 'car (nth 1 args))
  8552. (nth 1 args))
  8553. (cddr args))
  8554. (apply 'completing-read args)))))
  8555. (defun org-extract-attributes (s)
  8556. "Extract the attributes cookie from a string and set as text property."
  8557. (let (a attr (start 0) key value)
  8558. (save-match-data
  8559. (when (string-match "{{\\([^}]+\\)}}$" s)
  8560. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8561. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8562. (setq key (match-string 1 a) value (match-string 2 a)
  8563. start (match-end 0)
  8564. attr (plist-put attr (intern key) value))))
  8565. (org-add-props s nil 'org-attr attr))
  8566. s))
  8567. (defun org-extract-attributes-from-string (tag)
  8568. (let (key value attr)
  8569. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8570. (setq key (match-string 1 tag) value (match-string 2 tag)
  8571. tag (replace-match "" t t tag)
  8572. attr (plist-put attr (intern key) value)))
  8573. (cons tag attr)))
  8574. (defun org-attributes-to-string (plist)
  8575. "Format a property list into an HTML attribute list."
  8576. (let ((s "") key value)
  8577. (while plist
  8578. (setq key (pop plist) value (pop plist))
  8579. (and value
  8580. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8581. s))
  8582. ;;; Opening/following a link
  8583. (defvar org-link-search-failed nil)
  8584. (defvar org-open-link-functions nil
  8585. "Hook for functions finding a plain text link.
  8586. These functions must take a single argument, the link content.
  8587. They will be called for links that look like [[link text][description]]
  8588. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8589. like a filename (e.g. \"./blue.png\").
  8590. These functions will be called *before* Org attempts to resolve the
  8591. link by doing text searches in the current buffer - so if you want a
  8592. link \"[[target]]\" to still find \"<<target>>\", your function should
  8593. handle this as a special case.
  8594. When the function does handle the link, it must return a non-nil value.
  8595. If it decides that it is not responsible for this link, it must return
  8596. nil to indicate that that Org-mode can continue with other options
  8597. like exact and fuzzy text search.")
  8598. (defun org-next-link ()
  8599. "Move forward to the next link.
  8600. If the link is in hidden text, expose it."
  8601. (interactive)
  8602. (when (and org-link-search-failed (eq this-command last-command))
  8603. (goto-char (point-min))
  8604. (message "Link search wrapped back to beginning of buffer"))
  8605. (setq org-link-search-failed nil)
  8606. (let* ((pos (point))
  8607. (ct (org-context))
  8608. (a (assoc :link ct)))
  8609. (if a (goto-char (nth 2 a)))
  8610. (if (re-search-forward org-any-link-re nil t)
  8611. (progn
  8612. (goto-char (match-beginning 0))
  8613. (if (outline-invisible-p) (org-show-context)))
  8614. (goto-char pos)
  8615. (setq org-link-search-failed t)
  8616. (error "No further link found"))))
  8617. (defun org-previous-link ()
  8618. "Move backward to the previous link.
  8619. If the link is in hidden text, expose it."
  8620. (interactive)
  8621. (when (and org-link-search-failed (eq this-command last-command))
  8622. (goto-char (point-max))
  8623. (message "Link search wrapped back to end of buffer"))
  8624. (setq org-link-search-failed nil)
  8625. (let* ((pos (point))
  8626. (ct (org-context))
  8627. (a (assoc :link ct)))
  8628. (if a (goto-char (nth 1 a)))
  8629. (if (re-search-backward org-any-link-re nil t)
  8630. (progn
  8631. (goto-char (match-beginning 0))
  8632. (if (outline-invisible-p) (org-show-context)))
  8633. (goto-char pos)
  8634. (setq org-link-search-failed t)
  8635. (error "No further link found"))))
  8636. (defun org-translate-link (s)
  8637. "Translate a link string if a translation function has been defined."
  8638. (if (and org-link-translation-function
  8639. (fboundp org-link-translation-function)
  8640. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8641. (progn
  8642. (setq s (funcall org-link-translation-function
  8643. (match-string 1 s) (match-string 2 s)))
  8644. (concat (car s) ":" (cdr s)))
  8645. s))
  8646. (defun org-translate-link-from-planner (type path)
  8647. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8648. This is still an experimental function, your mileage may vary."
  8649. (cond
  8650. ((member type '("http" "https" "news" "ftp"))
  8651. ;; standard Internet links are the same.
  8652. nil)
  8653. ((and (equal type "irc") (string-match "^//" path))
  8654. ;; Planner has two / at the beginning of an irc link, we have 1.
  8655. ;; We should have zero, actually....
  8656. (setq path (substring path 1)))
  8657. ((and (equal type "lisp") (string-match "^/" path))
  8658. ;; Planner has a slash, we do not.
  8659. (setq type "elisp" path (substring path 1)))
  8660. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8661. ;; A typical message link. Planner has the id after the final slash,
  8662. ;; we separate it with a hash mark
  8663. (setq path (concat (match-string 1 path) "#"
  8664. (org-remove-angle-brackets (match-string 2 path)))))
  8665. )
  8666. (cons type path))
  8667. (defun org-find-file-at-mouse (ev)
  8668. "Open file link or URL at mouse."
  8669. (interactive "e")
  8670. (mouse-set-point ev)
  8671. (org-open-at-point 'in-emacs))
  8672. (defun org-open-at-mouse (ev)
  8673. "Open file link or URL at mouse.
  8674. See the docstring of `org-open-file' for details."
  8675. (interactive "e")
  8676. (mouse-set-point ev)
  8677. (if (eq major-mode 'org-agenda-mode)
  8678. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8679. (org-open-at-point))
  8680. (defvar org-window-config-before-follow-link nil
  8681. "The window configuration before following a link.
  8682. This is saved in case the need arises to restore it.")
  8683. (defvar org-open-link-marker (make-marker)
  8684. "Marker pointing to the location where `org-open-at-point; was called.")
  8685. ;;;###autoload
  8686. (defun org-open-at-point-global ()
  8687. "Follow a link like Org-mode does.
  8688. This command can be called in any mode to follow a link that has
  8689. Org-mode syntax."
  8690. (interactive)
  8691. (org-run-like-in-org-mode 'org-open-at-point))
  8692. ;;;###autoload
  8693. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8694. "Open a link in the string S, as if it was in Org-mode."
  8695. (interactive "sLink: \nP")
  8696. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8697. (with-temp-buffer
  8698. (let ((org-inhibit-startup (not reference-buffer)))
  8699. (org-mode)
  8700. (insert s)
  8701. (goto-char (point-min))
  8702. (when reference-buffer
  8703. (setq org-link-abbrev-alist-local
  8704. (with-current-buffer reference-buffer
  8705. org-link-abbrev-alist-local)))
  8706. (org-open-at-point arg reference-buffer)))))
  8707. (defvar org-open-at-point-functions nil
  8708. "Hook that is run when following a link at point.
  8709. Functions in this hook must return t if they identify and follow
  8710. a link at point. If they don't find anything interesting at point,
  8711. they must return nil.")
  8712. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8713. (defun org-open-at-point (&optional arg reference-buffer)
  8714. "Open link at or after point.
  8715. If there is no link at point, this function will search forward up to
  8716. the end of the current line.
  8717. Normally, files will be opened by an appropriate application. If the
  8718. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8719. With a double prefix argument, try to open outside of Emacs, in the
  8720. application the system uses for this file type."
  8721. (interactive "P")
  8722. ;; if in a code block, then open the block's results
  8723. (unless (call-interactively #'org-babel-open-src-block-result)
  8724. (org-load-modules-maybe)
  8725. (move-marker org-open-link-marker (point))
  8726. (setq org-window-config-before-follow-link (current-window-configuration))
  8727. (org-remove-occur-highlights nil nil t)
  8728. (cond
  8729. ((and (org-at-heading-p)
  8730. (not (org-at-timestamp-p t))
  8731. (not (org-in-regexp
  8732. (concat org-plain-link-re "\\|"
  8733. org-bracket-link-regexp "\\|"
  8734. org-angle-link-re "\\|"
  8735. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8736. (not (get-text-property (point) 'org-linked-text)))
  8737. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  8738. (lk0 (car lkall))
  8739. (lk (if (stringp lk0) (list lk0) lk0))
  8740. (lkend (cdr lkall)))
  8741. (mapcar (lambda(l)
  8742. (search-forward l nil lkend)
  8743. (goto-char (match-beginning 0))
  8744. (org-open-at-point))
  8745. lk))
  8746. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8747. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8748. ((and (org-at-timestamp-p t)
  8749. (not (org-in-regexp org-bracket-link-regexp)))
  8750. (org-follow-timestamp-link))
  8751. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8752. (not (org-in-regexp org-any-link-re)))
  8753. (org-footnote-action))
  8754. (t
  8755. (let (type path link line search (pos (point)))
  8756. (catch 'match
  8757. (save-excursion
  8758. (skip-chars-forward "^]\n\r")
  8759. (when (org-in-regexp org-bracket-link-regexp 1)
  8760. (setq link (org-extract-attributes
  8761. (org-link-unescape (org-match-string-no-properties 1))))
  8762. (while (string-match " *\n *" link)
  8763. (setq link (replace-match " " t t link)))
  8764. (setq link (org-link-expand-abbrev link))
  8765. (cond
  8766. ((or (file-name-absolute-p link)
  8767. (string-match "^\\.\\.?/" link))
  8768. (setq type "file" path link))
  8769. ((string-match org-link-re-with-space3 link)
  8770. (setq type (match-string 1 link) path (match-string 2 link)))
  8771. ((string-match "^help:+\\(.+\\)" link)
  8772. (setq type "help" path (match-string 1 link)))
  8773. (t (setq type "thisfile" path link)))
  8774. (throw 'match t)))
  8775. (when (get-text-property (point) 'org-linked-text)
  8776. (setq type "thisfile"
  8777. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8778. (1+ (point)) (point))
  8779. path (buffer-substring
  8780. (or (previous-single-property-change pos 'org-linked-text)
  8781. (point-min))
  8782. (or (next-single-property-change pos 'org-linked-text)
  8783. (point-max))))
  8784. (throw 'match t))
  8785. (save-excursion
  8786. (let ((plinkpos (org-in-regexp org-plain-link-re)))
  8787. (when (or (org-in-regexp org-angle-link-re)
  8788. (and plinkpos (goto-char (car plinkpos))
  8789. (save-match-data (not (looking-back "\\[\\[")))))
  8790. (setq type (match-string 1)
  8791. path (org-link-unescape (match-string 2)))
  8792. (throw 'match t))))
  8793. (save-excursion
  8794. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8795. (setq type "tags"
  8796. path (match-string 1))
  8797. (while (string-match ":" path)
  8798. (setq path (replace-match "+" t t path)))
  8799. (throw 'match t)))
  8800. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8801. (setq type "tree-match"
  8802. path (match-string 1))
  8803. (throw 'match t)))
  8804. (unless path
  8805. (user-error "No link found"))
  8806. ;; switch back to reference buffer
  8807. ;; needed when if called in a temporary buffer through
  8808. ;; org-open-link-from-string
  8809. (with-current-buffer (or reference-buffer (current-buffer))
  8810. ;; Remove any trailing spaces in path
  8811. (if (string-match " +\\'" path)
  8812. (setq path (replace-match "" t t path)))
  8813. (if (and org-link-translation-function
  8814. (fboundp org-link-translation-function))
  8815. ;; Check if we need to translate the link
  8816. (let ((tmp (funcall org-link-translation-function type path)))
  8817. (setq type (car tmp) path (cdr tmp))))
  8818. (cond
  8819. ((assoc type org-link-protocols)
  8820. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8821. ((equal type "help")
  8822. (let ((f-or-v (intern path)))
  8823. (cond ((fboundp f-or-v)
  8824. (describe-function f-or-v))
  8825. ((boundp f-or-v)
  8826. (describe-variable f-or-v))
  8827. (t (error "Not a known function or variable")))))
  8828. ((equal type "mailto")
  8829. (let ((cmd (car org-link-mailto-program))
  8830. (args (cdr org-link-mailto-program)) args1
  8831. (address path) (subject "") a)
  8832. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8833. (setq address (match-string 1 path)
  8834. subject (org-link-escape (match-string 2 path))))
  8835. (while args
  8836. (cond
  8837. ((not (stringp (car args))) (push (pop args) args1))
  8838. (t (setq a (pop args))
  8839. (if (string-match "%a" a)
  8840. (setq a (replace-match address t t a)))
  8841. (if (string-match "%s" a)
  8842. (setq a (replace-match subject t t a)))
  8843. (push a args1))))
  8844. (apply cmd (nreverse args1))))
  8845. ((member type '("http" "https" "ftp" "news"))
  8846. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8847. (org-link-escape
  8848. path org-link-escape-chars-browser)
  8849. path))))
  8850. ((string= type "doi")
  8851. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  8852. (org-link-escape
  8853. path org-link-escape-chars-browser)
  8854. path))))
  8855. ((member type '("message"))
  8856. (browse-url (concat type ":" path)))
  8857. ((string= type "tags")
  8858. (org-tags-view arg path))
  8859. ((string= type "tree-match")
  8860. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8861. ((string= type "file")
  8862. (if (string-match "::\\([0-9]+\\)\\'" path)
  8863. (setq line (string-to-number (match-string 1 path))
  8864. path (substring path 0 (match-beginning 0)))
  8865. (if (string-match "::\\(.+\\)\\'" path)
  8866. (setq search (match-string 1 path)
  8867. path (substring path 0 (match-beginning 0)))))
  8868. (if (string-match "[*?{]" (file-name-nondirectory path))
  8869. (dired path)
  8870. (org-open-file path arg line search)))
  8871. ((string= type "shell")
  8872. (let ((buf (generate-new-buffer "*Org Shell Output"))
  8873. (cmd path))
  8874. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8875. (string-match org-confirm-shell-link-not-regexp cmd))
  8876. (not org-confirm-shell-link-function)
  8877. (funcall org-confirm-shell-link-function
  8878. (format "Execute \"%s\" in shell? "
  8879. (org-add-props cmd nil
  8880. 'face 'org-warning))))
  8881. (progn
  8882. (message "Executing %s" cmd)
  8883. (shell-command cmd buf)
  8884. (if (featurep 'midnight)
  8885. (setq clean-buffer-list-kill-buffer-names
  8886. (cons buf clean-buffer-list-kill-buffer-names))))
  8887. (error "Abort"))))
  8888. ((string= type "elisp")
  8889. (let ((cmd path))
  8890. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8891. (string-match org-confirm-elisp-link-not-regexp cmd))
  8892. (not org-confirm-elisp-link-function)
  8893. (funcall org-confirm-elisp-link-function
  8894. (format "Execute \"%s\" as elisp? "
  8895. (org-add-props cmd nil
  8896. 'face 'org-warning))))
  8897. (message "%s => %s" cmd
  8898. (if (equal (string-to-char cmd) ?\()
  8899. (eval (read cmd))
  8900. (call-interactively (read cmd))))
  8901. (error "Abort"))))
  8902. ((and (string= type "thisfile")
  8903. (run-hook-with-args-until-success
  8904. 'org-open-link-functions path)))
  8905. ((string= type "thisfile")
  8906. (if arg
  8907. (switch-to-buffer-other-window
  8908. (org-get-buffer-for-internal-link (current-buffer)))
  8909. (org-mark-ring-push))
  8910. (let ((cmd `(org-link-search
  8911. ,path
  8912. ,(cond ((equal arg '(4)) ''occur)
  8913. ((equal arg '(16)) ''org-occur))
  8914. ,pos)))
  8915. (condition-case nil (let ((org-link-search-inhibit-query t))
  8916. (eval cmd))
  8917. (error (progn (widen) (eval cmd))))))
  8918. (t (browse-url-at-point)))))))
  8919. (move-marker org-open-link-marker nil)
  8920. (run-hook-with-args 'org-follow-link-hook)))
  8921. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  8922. "Offer links in the current entry and return the selected link.
  8923. If there is only one link, return it.
  8924. If NTH is an integer, return the NTH link found.
  8925. If ZERO is a string, check also this string for a link, and if
  8926. there is one, return it."
  8927. (with-current-buffer buffer
  8928. (save-excursion
  8929. (save-restriction
  8930. (widen)
  8931. (goto-char marker)
  8932. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8933. "\\(" org-angle-link-re "\\)\\|"
  8934. "\\(" org-plain-link-re "\\)"))
  8935. (cnt ?0)
  8936. (in-emacs (if (integerp nth) nil nth))
  8937. have-zero end links link c)
  8938. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8939. (push (match-string 0 zero) links)
  8940. (setq cnt (1- cnt) have-zero t))
  8941. (save-excursion
  8942. (org-back-to-heading t)
  8943. (setq end (save-excursion (outline-next-heading) (point)))
  8944. (while (re-search-forward re end t)
  8945. (push (match-string 0) links))
  8946. (setq links (org-uniquify (reverse links))))
  8947. (cond
  8948. ((null links)
  8949. (message "No links"))
  8950. ((equal (length links) 1)
  8951. (setq link (car links)))
  8952. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8953. (setq link (nth (if have-zero nth (1- nth)) links)))
  8954. (t ; we have to select a link
  8955. (save-excursion
  8956. (save-window-excursion
  8957. (delete-other-windows)
  8958. (with-output-to-temp-buffer "*Select Link*"
  8959. (mapc (lambda (l)
  8960. (if (not (string-match org-bracket-link-regexp l))
  8961. (princ (format "[%c] %s\n" (incf cnt)
  8962. (org-remove-angle-brackets l)))
  8963. (if (match-end 3)
  8964. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8965. (match-string 3 l) (match-string 1 l)))
  8966. (princ (format "[%c] %s\n" (incf cnt)
  8967. (match-string 1 l))))))
  8968. links))
  8969. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8970. (message "Select link to open, RET to open all:")
  8971. (setq c (read-char-exclusive))
  8972. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8973. (when (equal c ?q) (error "Abort"))
  8974. (if (equal c ?\C-m)
  8975. (setq link links)
  8976. (setq nth (- c ?0))
  8977. (if have-zero (setq nth (1+ nth)))
  8978. (unless (and (integerp nth) (>= (length links) nth))
  8979. (error "Invalid link selection"))
  8980. (setq link (nth (1- nth) links)))))
  8981. (cons link end))))))
  8982. ;; Add special file links that specify the way of opening
  8983. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8984. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8985. (defun org-open-file-with-system (path)
  8986. "Open file at PATH using the system way of opening it."
  8987. (org-open-file path 'system))
  8988. (defun org-open-file-with-emacs (path)
  8989. "Open file at PATH in Emacs."
  8990. (org-open-file path 'emacs))
  8991. (defun org-remove-file-link-modifiers ()
  8992. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8993. (goto-char (point-min))
  8994. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8995. (org-if-unprotected
  8996. (replace-match "file:" t t))))
  8997. (eval-after-load "org-exp"
  8998. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8999. 'org-remove-file-link-modifiers))
  9000. ;;; File search
  9001. (defvar org-create-file-search-functions nil
  9002. "List of functions to construct the right search string for a file link.
  9003. These functions are called in turn with point at the location to
  9004. which the link should point.
  9005. A function in the hook should first test if it would like to
  9006. handle this file type, for example by checking the `major-mode'
  9007. or the file extension. If it decides not to handle this file, it
  9008. should just return nil to give other functions a chance. If it
  9009. does handle the file, it must return the search string to be used
  9010. when following the link. The search string will be part of the
  9011. file link, given after a double colon, and `org-open-at-point'
  9012. will automatically search for it. If special measures must be
  9013. taken to make the search successful, another function should be
  9014. added to the companion hook `org-execute-file-search-functions',
  9015. which see.
  9016. A function in this hook may also use `setq' to set the variable
  9017. `description' to provide a suggestion for the descriptive text to
  9018. be used for this link when it gets inserted into an Org-mode
  9019. buffer with \\[org-insert-link].")
  9020. (defvar org-execute-file-search-functions nil
  9021. "List of functions to execute a file search triggered by a link.
  9022. Functions added to this hook must accept a single argument, the
  9023. search string that was part of the file link, the part after the
  9024. double colon. The function must first check if it would like to
  9025. handle this search, for example by checking the `major-mode' or
  9026. the file extension. If it decides not to handle this search, it
  9027. should just return nil to give other functions a chance. If it
  9028. does handle the search, it must return a non-nil value to keep
  9029. other functions from trying.
  9030. Each function can access the current prefix argument through the
  9031. variable `current-prefix-argument'. Note that a single prefix is
  9032. used to force opening a link in Emacs, so it may be good to only
  9033. use a numeric or double prefix to guide the search function.
  9034. In case this is needed, a function in this hook can also restore
  9035. the window configuration before `org-open-at-point' was called using:
  9036. (set-window-configuration org-window-config-before-follow-link)")
  9037. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9038. (defun org-link-search (s &optional type avoid-pos stealth)
  9039. "Search for a link search option.
  9040. If S is surrounded by forward slashes, it is interpreted as a
  9041. regular expression. In org-mode files, this will create an `org-occur'
  9042. sparse tree. In ordinary files, `occur' will be used to list matches.
  9043. If the current buffer is in `dired-mode', grep will be used to search
  9044. in all files. If AVOID-POS is given, ignore matches near that position.
  9045. When optional argument STEALTH is non-nil, do not modify
  9046. visibility around point, thus ignoring
  9047. `org-show-hierarchy-above', `org-show-following-heading' and
  9048. `org-show-siblings' variables."
  9049. (let ((case-fold-search t)
  9050. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9051. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9052. (append '(("") (" ") ("\t") ("\n"))
  9053. org-emphasis-alist)
  9054. "\\|") "\\)"))
  9055. (pos (point))
  9056. (pre nil) (post nil)
  9057. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9058. (cond
  9059. ;; First check if there are any special search functions
  9060. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9061. ;; Now try the builtin stuff
  9062. ((and (equal (string-to-char s0) ?#)
  9063. (> (length s0) 1)
  9064. (save-excursion
  9065. (goto-char (point-min))
  9066. (and
  9067. (re-search-forward
  9068. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9069. (setq type 'dedicated
  9070. pos (match-beginning 0))))
  9071. ;; There is an exact target for this
  9072. (goto-char pos)
  9073. (org-back-to-heading t)))
  9074. ((save-excursion
  9075. (goto-char (point-min))
  9076. (and
  9077. (re-search-forward
  9078. (concat "<<" (regexp-quote s0) ">>") nil t)
  9079. (setq type 'dedicated
  9080. pos (match-beginning 0))))
  9081. ;; There is an exact target for this
  9082. (goto-char pos))
  9083. ((save-excursion
  9084. (goto-char (point-min))
  9085. (and
  9086. (re-search-forward
  9087. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9088. (setq type 'dedicated pos (match-beginning 0))))
  9089. ;; Found an invisible target.
  9090. (goto-char pos))
  9091. ((save-excursion
  9092. (goto-char (point-min))
  9093. (and
  9094. (re-search-forward
  9095. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9096. (setq type 'dedicated pos (match-beginning 0))))
  9097. ;; Found an element with a matching #+name affiliated keyword.
  9098. (goto-char pos))
  9099. ((and (string-match "^(\\(.*\\))$" s0)
  9100. (save-excursion
  9101. (goto-char (point-min))
  9102. (and
  9103. (re-search-forward
  9104. (concat "[^[]" (regexp-quote
  9105. (format org-coderef-label-format
  9106. (match-string 1 s0))))
  9107. nil t)
  9108. (setq type 'dedicated
  9109. pos (1+ (match-beginning 0))))))
  9110. ;; There is a coderef target for this
  9111. (goto-char pos))
  9112. ((string-match "^/\\(.*\\)/$" s)
  9113. ;; A regular expression
  9114. (cond
  9115. ((derived-mode-p 'org-mode)
  9116. (org-occur (match-string 1 s)))
  9117. ;;((eq major-mode 'dired-mode)
  9118. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9119. (t (org-do-occur (match-string 1 s)))))
  9120. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9121. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9122. (goto-char (point-min))
  9123. (cond
  9124. ((let (case-fold-search)
  9125. (re-search-forward (format org-complex-heading-regexp-format
  9126. (regexp-quote s))
  9127. nil t))
  9128. ;; OK, found a match
  9129. (setq type 'dedicated)
  9130. (goto-char (match-beginning 0)))
  9131. ((and (not org-link-search-inhibit-query)
  9132. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9133. (y-or-n-p "No match - create this as a new heading? "))
  9134. (goto-char (point-max))
  9135. (or (bolp) (newline))
  9136. (insert "* " s "\n")
  9137. (beginning-of-line 0))
  9138. (t
  9139. (goto-char pos)
  9140. (error "No match"))))
  9141. (t
  9142. ;; A normal search string
  9143. (when (equal (string-to-char s) ?*)
  9144. ;; Anchor on headlines, post may include tags.
  9145. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9146. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9147. s (substring s 1)))
  9148. (remove-text-properties
  9149. 0 (length s)
  9150. '(face nil mouse-face nil keymap nil fontified nil) s)
  9151. ;; Make a series of regular expressions to find a match
  9152. (setq words (org-split-string s "[ \n\r\t]+")
  9153. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9154. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9155. "\\)" markers)
  9156. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9157. re2a (concat "[ \t\r\n]" re2a_)
  9158. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9159. re4 (concat "[^a-zA-Z_]" re4_)
  9160. re1 (concat pre re2 post)
  9161. re3 (concat pre (if pre re4_ re4) post)
  9162. re5 (concat pre ".*" re4)
  9163. re2 (concat pre re2)
  9164. re2a (concat pre (if pre re2a_ re2a))
  9165. re4 (concat pre (if pre re4_ re4))
  9166. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9167. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9168. re5 "\\)"
  9169. ))
  9170. (cond
  9171. ((eq type 'org-occur) (org-occur reall))
  9172. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9173. (t (goto-char (point-min))
  9174. (setq type 'fuzzy)
  9175. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9176. (org-search-not-self 1 re1 nil t)
  9177. (org-search-not-self 1 re2 nil t)
  9178. (org-search-not-self 1 re2a nil t)
  9179. (org-search-not-self 1 re3 nil t)
  9180. (org-search-not-self 1 re4 nil t)
  9181. (org-search-not-self 1 re5 nil t)
  9182. )
  9183. (goto-char (match-beginning 1))
  9184. (goto-char pos)
  9185. (error "No match"))))))
  9186. (and (derived-mode-p 'org-mode)
  9187. (not stealth)
  9188. (org-show-context 'link-search))
  9189. type))
  9190. (defun org-search-not-self (group &rest args)
  9191. "Execute `re-search-forward', but only accept matches that do not
  9192. enclose the position of `org-open-link-marker'."
  9193. (let ((m org-open-link-marker))
  9194. (catch 'exit
  9195. (while (apply 're-search-forward args)
  9196. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9197. (goto-char (match-end group))
  9198. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9199. (> (match-beginning 0) (marker-position m))
  9200. (< (match-end 0) (marker-position m)))
  9201. (save-match-data
  9202. (or (not (org-in-regexp
  9203. org-bracket-link-analytic-regexp 1))
  9204. (not (match-end 4)) ; no description
  9205. (and (<= (match-beginning 4) (point))
  9206. (>= (match-end 4) (point))))))
  9207. (throw 'exit (point))))))))
  9208. (defun org-get-buffer-for-internal-link (buffer)
  9209. "Return a buffer to be used for displaying the link target of internal links."
  9210. (cond
  9211. ((not org-display-internal-link-with-indirect-buffer)
  9212. buffer)
  9213. ((string-match "(Clone)$" (buffer-name buffer))
  9214. (message "Buffer is already a clone, not making another one")
  9215. ;; we also do not modify visibility in this case
  9216. buffer)
  9217. (t ; make a new indirect buffer for displaying the link
  9218. (let* ((bn (buffer-name buffer))
  9219. (ibn (concat bn "(Clone)"))
  9220. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9221. (with-current-buffer ib (org-overview))
  9222. ib))))
  9223. (defun org-do-occur (regexp &optional cleanup)
  9224. "Call the Emacs command `occur'.
  9225. If CLEANUP is non-nil, remove the printout of the regular expression
  9226. in the *Occur* buffer. This is useful if the regex is long and not useful
  9227. to read."
  9228. (occur regexp)
  9229. (when cleanup
  9230. (let ((cwin (selected-window)) win beg end)
  9231. (when (setq win (get-buffer-window "*Occur*"))
  9232. (select-window win))
  9233. (goto-char (point-min))
  9234. (when (re-search-forward "match[a-z]+" nil t)
  9235. (setq beg (match-end 0))
  9236. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9237. (setq end (1- (match-beginning 0)))))
  9238. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9239. (goto-char (point-min))
  9240. (select-window cwin))))
  9241. ;;; The mark ring for links jumps
  9242. (defvar org-mark-ring nil
  9243. "Mark ring for positions before jumps in Org-mode.")
  9244. (defvar org-mark-ring-last-goto nil
  9245. "Last position in the mark ring used to go back.")
  9246. ;; Fill and close the ring
  9247. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9248. (loop for i from 1 to org-mark-ring-length do
  9249. (push (make-marker) org-mark-ring))
  9250. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9251. org-mark-ring)
  9252. (defun org-mark-ring-push (&optional pos buffer)
  9253. "Put the current position or POS into the mark ring and rotate it."
  9254. (interactive)
  9255. (setq pos (or pos (point)))
  9256. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9257. (move-marker (car org-mark-ring)
  9258. (or pos (point))
  9259. (or buffer (current-buffer)))
  9260. (message "%s"
  9261. (substitute-command-keys
  9262. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9263. (defun org-mark-ring-goto (&optional n)
  9264. "Jump to the previous position in the mark ring.
  9265. With prefix arg N, jump back that many stored positions. When
  9266. called several times in succession, walk through the entire ring.
  9267. Org-mode commands jumping to a different position in the current file,
  9268. or to another Org-mode file, automatically push the old position
  9269. onto the ring."
  9270. (interactive "p")
  9271. (let (p m)
  9272. (if (eq last-command this-command)
  9273. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9274. (setq p org-mark-ring))
  9275. (setq org-mark-ring-last-goto p)
  9276. (setq m (car p))
  9277. (org-pop-to-buffer-same-window (marker-buffer m))
  9278. (goto-char m)
  9279. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9280. (defun org-remove-angle-brackets (s)
  9281. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9282. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9283. s)
  9284. (defun org-add-angle-brackets (s)
  9285. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9286. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9287. s)
  9288. (defun org-remove-double-quotes (s)
  9289. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9290. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9291. s)
  9292. ;;; Following specific links
  9293. (defun org-follow-timestamp-link ()
  9294. "Open an agenda view for the time-stamp date/range at point."
  9295. (cond
  9296. ((org-at-date-range-p t)
  9297. (let ((org-agenda-start-on-weekday)
  9298. (t1 (match-string 1))
  9299. (t2 (match-string 2)) tt1 tt2)
  9300. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9301. tt2 (time-to-days (org-time-string-to-time t2)))
  9302. (let ((org-agenda-buffer-tmp-name
  9303. (format "*Org Agenda(a:%s)"
  9304. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9305. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9306. ((org-at-timestamp-p t)
  9307. (let ((org-agenda-buffer-tmp-name
  9308. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9309. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9310. (substring (match-string 1) 0 10)))
  9311. 1)))
  9312. (t (error "This should not happen"))))
  9313. ;;; Following file links
  9314. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9315. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9316. (declare-function mailcap-mime-info
  9317. "mailcap" (string &optional request no-decode))
  9318. (defvar org-wait nil)
  9319. (defun org-open-file (path &optional in-emacs line search)
  9320. "Open the file at PATH.
  9321. First, this expands any special file name abbreviations. Then the
  9322. configuration variable `org-file-apps' is checked if it contains an
  9323. entry for this file type, and if yes, the corresponding command is launched.
  9324. If no application is found, Emacs simply visits the file.
  9325. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9326. With a double \\[universal-argument] \\[universal-argument] \
  9327. prefix arg, Org tries to avoid opening in Emacs
  9328. and to use an external application to visit the file.
  9329. Optional LINE specifies a line to go to, optional SEARCH a string
  9330. to search for. If LINE or SEARCH is given, the file will be
  9331. opened in Emacs, unless an entry from org-file-apps that makes
  9332. use of groups in a regexp matches.
  9333. If you want to change the way frames are used when following a
  9334. link, please customize `org-link-frame-setup'.
  9335. If the file does not exist, an error is thrown."
  9336. (let* ((file (if (equal path "")
  9337. buffer-file-name
  9338. (substitute-in-file-name (expand-file-name path))))
  9339. (file-apps (append org-file-apps (org-default-apps)))
  9340. (apps (org-remove-if
  9341. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9342. (apps-dlink (org-remove-if-not
  9343. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9344. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9345. (dirp (if remp nil (file-directory-p file)))
  9346. (file (if (and dirp org-open-directory-means-index-dot-org)
  9347. (concat (file-name-as-directory file) "index.org")
  9348. file))
  9349. (a-m-a-p (assq 'auto-mode apps))
  9350. (dfile (downcase file))
  9351. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9352. (link (cond ((and (eq line nil)
  9353. (eq search nil))
  9354. file)
  9355. (line
  9356. (concat file "::" (number-to-string line)))
  9357. (search
  9358. (concat file "::" search))))
  9359. (dlink (downcase link))
  9360. (old-buffer (current-buffer))
  9361. (old-pos (point))
  9362. (old-mode major-mode)
  9363. ext cmd link-match-data)
  9364. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9365. (setq ext (match-string 1 dfile))
  9366. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9367. (setq ext (match-string 1 dfile))))
  9368. (cond
  9369. ((member in-emacs '((16) system))
  9370. (setq cmd (cdr (assoc 'system apps))))
  9371. (in-emacs (setq cmd 'emacs))
  9372. (t
  9373. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9374. (and dirp (cdr (assoc 'directory apps)))
  9375. ; first, try matching against apps-dlink
  9376. ; if we get a match here, store the match data for later
  9377. (let ((match (assoc-default dlink apps-dlink
  9378. 'string-match)))
  9379. (if match
  9380. (progn (setq link-match-data (match-data))
  9381. match)
  9382. (progn (setq in-emacs (or in-emacs line search))
  9383. nil))) ; if we have no match in apps-dlink,
  9384. ; always open the file in emacs if line or search
  9385. ; is given (for backwards compatibility)
  9386. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9387. 'string-match)
  9388. (cdr (assoc ext apps))
  9389. (cdr (assoc t apps))))))
  9390. (when (eq cmd 'system)
  9391. (setq cmd (cdr (assoc 'system apps))))
  9392. (when (eq cmd 'default)
  9393. (setq cmd (cdr (assoc t apps))))
  9394. (when (eq cmd 'mailcap)
  9395. (require 'mailcap)
  9396. (mailcap-parse-mailcaps)
  9397. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9398. (command (mailcap-mime-info mime-type)))
  9399. (if (stringp command)
  9400. (setq cmd command)
  9401. (setq cmd 'emacs))))
  9402. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9403. (not (file-exists-p file))
  9404. (not org-open-non-existing-files))
  9405. (error "No such file: %s" file))
  9406. (cond
  9407. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9408. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9409. (while (string-match "['\"]%s['\"]" cmd)
  9410. (setq cmd (replace-match "%s" t t cmd)))
  9411. (while (string-match "%s" cmd)
  9412. (setq cmd (replace-match
  9413. (save-match-data
  9414. (shell-quote-argument
  9415. (convert-standard-filename file)))
  9416. t t cmd)))
  9417. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9418. (save-match-data
  9419. (let ((match-index 1)
  9420. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9421. (set-match-data link-match-data)
  9422. (while (<= match-index number-of-groups)
  9423. (let ((regex (concat "%" (number-to-string match-index)))
  9424. (replace-with (match-string match-index dlink)))
  9425. (while (string-match regex cmd)
  9426. (setq cmd (replace-match replace-with t t cmd))))
  9427. (setq match-index (+ match-index 1)))))
  9428. (save-window-excursion
  9429. (start-process-shell-command cmd nil cmd)
  9430. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9431. ))
  9432. ((or (stringp cmd)
  9433. (eq cmd 'emacs))
  9434. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9435. (widen)
  9436. (if line (org-goto-line line)
  9437. (if search (org-link-search search))))
  9438. ((consp cmd)
  9439. (let ((file (convert-standard-filename file)))
  9440. (save-match-data
  9441. (set-match-data link-match-data)
  9442. (eval cmd))))
  9443. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9444. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9445. (or (not (equal old-buffer (current-buffer)))
  9446. (not (equal old-pos (point))))
  9447. (org-mark-ring-push old-pos old-buffer))))
  9448. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9449. "This function returns non-nil if `entry' uses a regular
  9450. expression which should be matched against the whole link by
  9451. org-open-file.
  9452. It assumes that is the case when the entry uses a regular
  9453. expression which has at least one grouping construct and the
  9454. action is either a lisp form or a command string containing
  9455. '%1', i.e. using at least one subexpression match as a
  9456. parameter."
  9457. (let ((selector (car entry))
  9458. (action (cdr entry)))
  9459. (if (stringp selector)
  9460. (and (> (regexp-opt-depth selector) 0)
  9461. (or (and (stringp action)
  9462. (string-match "%[0-9]" action))
  9463. (consp action)))
  9464. nil)))
  9465. (defun org-default-apps ()
  9466. "Return the default applications for this operating system."
  9467. (cond
  9468. ((eq system-type 'darwin)
  9469. org-file-apps-defaults-macosx)
  9470. ((eq system-type 'windows-nt)
  9471. org-file-apps-defaults-windowsnt)
  9472. (t org-file-apps-defaults-gnu)))
  9473. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9474. "Convert extensions to regular expressions in the cars of LIST.
  9475. Also, weed out any non-string entries, because the return value is used
  9476. only for regexp matching.
  9477. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9478. point to the symbol `emacs', indicating that the file should
  9479. be opened in Emacs."
  9480. (append
  9481. (delq nil
  9482. (mapcar (lambda (x)
  9483. (if (not (stringp (car x)))
  9484. nil
  9485. (if (string-match "\\W" (car x))
  9486. x
  9487. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9488. list))
  9489. (if add-auto-mode
  9490. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9491. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9492. (defun org-file-remote-p (file)
  9493. "Test whether FILE specifies a location on a remote system.
  9494. Return non-nil if the location is indeed remote.
  9495. For example, the filename \"/user@host:/foo\" specifies a location
  9496. on the system \"/user@host:\"."
  9497. (cond ((fboundp 'file-remote-p)
  9498. (file-remote-p file))
  9499. ((fboundp 'tramp-handle-file-remote-p)
  9500. (tramp-handle-file-remote-p file))
  9501. ((and (boundp 'ange-ftp-name-format)
  9502. (string-match (car ange-ftp-name-format) file))
  9503. t)))
  9504. ;;;; Refiling
  9505. (defun org-get-org-file ()
  9506. "Read a filename, with default directory `org-directory'."
  9507. (let ((default (or org-default-notes-file remember-data-file)))
  9508. (read-file-name (format "File name [%s]: " default)
  9509. (file-name-as-directory org-directory)
  9510. default)))
  9511. (defun org-notes-order-reversed-p ()
  9512. "Check if the current file should receive notes in reversed order."
  9513. (cond
  9514. ((not org-reverse-note-order) nil)
  9515. ((eq t org-reverse-note-order) t)
  9516. ((not (listp org-reverse-note-order)) nil)
  9517. (t (catch 'exit
  9518. (let ((all org-reverse-note-order)
  9519. entry)
  9520. (while (setq entry (pop all))
  9521. (if (string-match (car entry) buffer-file-name)
  9522. (throw 'exit (cdr entry))))
  9523. nil)))))
  9524. (defvar org-refile-target-table nil
  9525. "The list of refile targets, created by `org-refile'.")
  9526. (defvar org-agenda-new-buffers nil
  9527. "Buffers created to visit agenda files.")
  9528. (defvar org-refile-cache nil
  9529. "Cache for refile targets.")
  9530. (defvar org-refile-markers nil
  9531. "All the markers used for caching refile locations.")
  9532. (defun org-refile-marker (pos)
  9533. "Get a new refile marker, but only if caching is in use."
  9534. (if (not org-refile-use-cache)
  9535. pos
  9536. (let ((m (make-marker)))
  9537. (move-marker m pos)
  9538. (push m org-refile-markers)
  9539. m)))
  9540. (defun org-refile-cache-clear ()
  9541. "Clear the refile cache and disable all the markers."
  9542. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9543. (setq org-refile-markers nil)
  9544. (setq org-refile-cache nil)
  9545. (message "Refile cache has been cleared"))
  9546. (defun org-refile-cache-check-set (set)
  9547. "Check if all the markers in the cache still have live buffers."
  9548. (let (marker)
  9549. (catch 'exit
  9550. (while (and set (setq marker (nth 3 (pop set))))
  9551. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9552. (when (and marker (null (marker-buffer marker)))
  9553. (message "not found") (sit-for 3)
  9554. (throw 'exit nil)))
  9555. t)))
  9556. (defun org-refile-cache-put (set &rest identifiers)
  9557. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9558. (let* ((key (sha1 (prin1-to-string identifiers)))
  9559. (entry (assoc key org-refile-cache)))
  9560. (if entry
  9561. (setcdr entry set)
  9562. (push (cons key set) org-refile-cache))))
  9563. (defun org-refile-cache-get (&rest identifiers)
  9564. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9565. (cond
  9566. ((not org-refile-cache) nil)
  9567. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9568. (t
  9569. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9570. org-refile-cache))))
  9571. (and set (org-refile-cache-check-set set) set)))))
  9572. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9573. "Produce a table with refile targets."
  9574. (let ((case-fold-search nil)
  9575. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9576. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9577. targets tgs txt re files f desc descre fast-path-p level pos0)
  9578. (message "Getting targets...")
  9579. (with-current-buffer (or default-buffer (current-buffer))
  9580. (while (setq entry (pop entries))
  9581. (setq files (car entry) desc (cdr entry))
  9582. (setq fast-path-p nil)
  9583. (cond
  9584. ((null files) (setq files (list (current-buffer))))
  9585. ((eq files 'org-agenda-files)
  9586. (setq files (org-agenda-files 'unrestricted)))
  9587. ((and (symbolp files) (fboundp files))
  9588. (setq files (funcall files)))
  9589. ((and (symbolp files) (boundp files))
  9590. (setq files (symbol-value files))))
  9591. (if (stringp files) (setq files (list files)))
  9592. (cond
  9593. ((eq (car desc) :tag)
  9594. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9595. ((eq (car desc) :todo)
  9596. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9597. ((eq (car desc) :regexp)
  9598. (setq descre (cdr desc)))
  9599. ((eq (car desc) :level)
  9600. (setq descre (concat "^\\*\\{" (number-to-string
  9601. (if org-odd-levels-only
  9602. (1- (* 2 (cdr desc)))
  9603. (cdr desc)))
  9604. "\\}[ \t]")))
  9605. ((eq (car desc) :maxlevel)
  9606. (setq fast-path-p t)
  9607. (setq descre (concat "^\\*\\{1," (number-to-string
  9608. (if org-odd-levels-only
  9609. (1- (* 2 (cdr desc)))
  9610. (cdr desc)))
  9611. "\\}[ \t]")))
  9612. (t (error "Bad refiling target description %s" desc)))
  9613. (while (setq f (pop files))
  9614. (with-current-buffer
  9615. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9616. (or
  9617. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9618. (progn
  9619. (if (bufferp f) (setq f (buffer-file-name
  9620. (buffer-base-buffer f))))
  9621. (setq f (and f (expand-file-name f)))
  9622. (if (eq org-refile-use-outline-path 'file)
  9623. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9624. (save-excursion
  9625. (save-restriction
  9626. (widen)
  9627. (goto-char (point-min))
  9628. (while (re-search-forward descre nil t)
  9629. (goto-char (setq pos0 (point-at-bol)))
  9630. (catch 'next
  9631. (when org-refile-target-verify-function
  9632. (save-match-data
  9633. (or (funcall org-refile-target-verify-function)
  9634. (throw 'next t))))
  9635. (when (and (looking-at org-complex-heading-regexp)
  9636. (not (member (match-string 4) excluded-entries))
  9637. (match-string 4))
  9638. (setq level (org-reduced-level
  9639. (- (match-end 1) (match-beginning 1)))
  9640. txt (org-link-display-format (match-string 4))
  9641. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9642. re (format org-complex-heading-regexp-format
  9643. (regexp-quote (match-string 4))))
  9644. (when org-refile-use-outline-path
  9645. (setq txt (mapconcat
  9646. 'org-protect-slash
  9647. (append
  9648. (if (eq org-refile-use-outline-path
  9649. 'file)
  9650. (list (file-name-nondirectory
  9651. (buffer-file-name
  9652. (buffer-base-buffer))))
  9653. (if (eq org-refile-use-outline-path
  9654. 'full-file-path)
  9655. (list (buffer-file-name
  9656. (buffer-base-buffer)))))
  9657. (org-get-outline-path fast-path-p
  9658. level txt)
  9659. (list txt))
  9660. "/")))
  9661. (push (list txt f re (org-refile-marker (point)))
  9662. tgs)))
  9663. (when (= (point) pos0)
  9664. ;; verification function has not moved point
  9665. (goto-char (point-at-eol))))))))
  9666. (when org-refile-use-cache
  9667. (org-refile-cache-put tgs (buffer-file-name) descre))
  9668. (setq targets (append tgs targets))
  9669. ))))
  9670. (message "Getting targets...done")
  9671. (nreverse targets)))
  9672. (defun org-protect-slash (s)
  9673. (while (string-match "/" s)
  9674. (setq s (replace-match "\\" t t s)))
  9675. s)
  9676. (defvar org-olpa (make-vector 20 nil))
  9677. (defun org-get-outline-path (&optional fastp level heading)
  9678. "Return the outline path to the current entry, as a list.
  9679. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9680. routine which makes outline path derivations for an entire file,
  9681. avoiding backtracing. Refile target collection makes use of that."
  9682. (if fastp
  9683. (progn
  9684. (if (> level 19)
  9685. (error "Outline path failure, more than 19 levels"))
  9686. (loop for i from level upto 19 do
  9687. (aset org-olpa i nil))
  9688. (prog1
  9689. (delq nil (append org-olpa nil))
  9690. (aset org-olpa level heading)))
  9691. (let (rtn case-fold-search)
  9692. (save-excursion
  9693. (save-restriction
  9694. (widen)
  9695. (while (org-up-heading-safe)
  9696. (when (looking-at org-complex-heading-regexp)
  9697. (push (org-match-string-no-properties 4) rtn)))
  9698. rtn)))))
  9699. (defun org-format-outline-path (path &optional width prefix)
  9700. "Format the outline path PATH for display.
  9701. Width is the maximum number of characters that is available.
  9702. Prefix is a prefix to be included in the returned string,
  9703. such as the file name."
  9704. (setq width (or width 79))
  9705. (if prefix (setq width (- width (length prefix))))
  9706. (if (not path)
  9707. (or prefix "")
  9708. (let* ((nsteps (length path))
  9709. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9710. (maxwidth (if (<= total-width width)
  9711. 10000 ;; everything fits
  9712. ;; we need to shorten the level headings
  9713. (/ (- width nsteps) nsteps)))
  9714. (org-odd-levels-only nil)
  9715. (n 0)
  9716. (total (1+ (length prefix))))
  9717. (setq maxwidth (max maxwidth 10))
  9718. (concat prefix
  9719. (mapconcat
  9720. (lambda (h)
  9721. (setq n (1+ n))
  9722. (if (and (= n nsteps) (< maxwidth 10000))
  9723. (setq maxwidth (- total-width total)))
  9724. (if (< (length h) maxwidth)
  9725. (progn (setq total (+ total (length h) 1)) h)
  9726. (setq h (substring h 0 (- maxwidth 2))
  9727. total (+ total maxwidth 1))
  9728. (if (string-match "[ \t]+\\'" h)
  9729. (setq h (substring h 0 (match-beginning 0))))
  9730. (setq h (concat h "..")))
  9731. (org-add-props h nil 'face
  9732. (nth (% (1- n) org-n-level-faces)
  9733. org-level-faces))
  9734. h)
  9735. path "/")))))
  9736. (defun org-display-outline-path (&optional file current)
  9737. "Display the current outline path in the echo area."
  9738. (interactive "P")
  9739. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9740. (case-fold-search nil)
  9741. (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
  9742. (if current (setq path (append path
  9743. (save-excursion
  9744. (org-back-to-heading t)
  9745. (if (looking-at org-complex-heading-regexp)
  9746. (list (match-string 4)))))))
  9747. (message "%s"
  9748. (org-format-outline-path
  9749. path
  9750. (1- (frame-width))
  9751. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9752. (defvar org-refile-history nil
  9753. "History for refiling operations.")
  9754. (defvar org-after-refile-insert-hook nil
  9755. "Hook run after `org-refile' has inserted its stuff at the new location.
  9756. Note that this is still *before* the stuff will be removed from
  9757. the *old* location.")
  9758. (defvar org-capture-last-stored-marker)
  9759. (defun org-refile (&optional goto default-buffer rfloc)
  9760. "Move the entry or entries at point to another heading.
  9761. The list of target headings is compiled using the information in
  9762. `org-refile-targets', which see.
  9763. At the target location, the entry is filed as a subitem of the target
  9764. heading. Depending on `org-reverse-note-order', the new subitem will
  9765. either be the first or the last subitem.
  9766. If there is an active region, all entries in that region will be moved.
  9767. However, the region must fulfill the requirement that the first heading
  9768. is the first one sets the top-level of the moved text - at most siblings
  9769. below it are allowed.
  9770. With prefix arg GOTO, the command will only visit the target location
  9771. and not actually move anything.
  9772. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9773. go to the location where the last refiling operation has put the subtree.
  9774. With a prefix argument of `2', refile to the running clock.
  9775. RFLOC can be a refile location obtained in a different way.
  9776. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9777. If you are using target caching (see `org-refile-use-cache'),
  9778. you have to clear the target cache in order to find new targets.
  9779. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9780. prefix argument (`C-u C-u C-u C-c C-w')."
  9781. (interactive "P")
  9782. (if (member goto '(0 (64)))
  9783. (org-refile-cache-clear)
  9784. (let* ((cbuf (current-buffer))
  9785. (regionp (org-region-active-p))
  9786. (region-start (and regionp (region-beginning)))
  9787. (region-end (and regionp (region-end)))
  9788. (region-length (and regionp (- region-end region-start)))
  9789. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9790. pos it nbuf file re level reversed)
  9791. (setq last-command nil)
  9792. (when regionp
  9793. (goto-char region-start)
  9794. (or (bolp) (goto-char (point-at-bol)))
  9795. (setq region-start (point))
  9796. (unless (or (org-kill-is-subtree-p
  9797. (buffer-substring region-start region-end))
  9798. (prog1 org-refile-active-region-within-subtree
  9799. (org-toggle-heading)))
  9800. (error "The region is not a (sequence of) subtree(s)")))
  9801. (if (equal goto '(16))
  9802. (org-refile-goto-last-stored)
  9803. (when (or
  9804. (and (equal goto 2)
  9805. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9806. (prog1
  9807. (setq it (list (or org-clock-heading "running clock")
  9808. (buffer-file-name
  9809. (marker-buffer org-clock-hd-marker))
  9810. ""
  9811. (marker-position org-clock-hd-marker)))
  9812. (setq goto nil)))
  9813. (setq it (or rfloc
  9814. (let (heading-text)
  9815. (save-excursion
  9816. (unless goto
  9817. (org-back-to-heading t)
  9818. (setq heading-text
  9819. (nth 4 (org-heading-components))))
  9820. (org-refile-get-location
  9821. (cond (goto "Goto")
  9822. (regionp "Refile region to")
  9823. (t (concat "Refile subtree \""
  9824. heading-text "\" to")))
  9825. default-buffer
  9826. (and (not (equal '(4) goto))
  9827. org-refile-allow-creating-parent-nodes)
  9828. goto))))))
  9829. (setq file (nth 1 it)
  9830. re (nth 2 it)
  9831. pos (nth 3 it))
  9832. (if (and (not goto)
  9833. pos
  9834. (equal (buffer-file-name) file)
  9835. (if regionp
  9836. (and (>= pos region-start)
  9837. (<= pos region-end))
  9838. (and (>= pos (point))
  9839. (< pos (save-excursion
  9840. (org-end-of-subtree t t))))))
  9841. (error "Cannot refile to position inside the tree or region"))
  9842. (setq nbuf (or (find-buffer-visiting file)
  9843. (find-file-noselect file)))
  9844. (if goto
  9845. (progn
  9846. (org-pop-to-buffer-same-window nbuf)
  9847. (goto-char pos)
  9848. (org-show-context 'org-goto))
  9849. (if regionp
  9850. (progn
  9851. (org-kill-new (buffer-substring region-start region-end))
  9852. (org-save-markers-in-region region-start region-end))
  9853. (org-copy-subtree 1 nil t))
  9854. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9855. (find-file-noselect file)))
  9856. (setq reversed (org-notes-order-reversed-p))
  9857. (save-excursion
  9858. (save-restriction
  9859. (widen)
  9860. (if pos
  9861. (progn
  9862. (goto-char pos)
  9863. (looking-at org-outline-regexp)
  9864. (setq level (org-get-valid-level (funcall outline-level) 1))
  9865. (goto-char
  9866. (if reversed
  9867. (or (outline-next-heading) (point-max))
  9868. (or (save-excursion (org-get-next-sibling))
  9869. (org-end-of-subtree t t)
  9870. (point-max)))))
  9871. (setq level 1)
  9872. (if (not reversed)
  9873. (goto-char (point-max))
  9874. (goto-char (point-min))
  9875. (or (outline-next-heading) (goto-char (point-max)))))
  9876. (if (not (bolp)) (newline))
  9877. (org-paste-subtree level)
  9878. (when org-log-refile
  9879. (org-add-log-setup 'refile nil nil 'findpos
  9880. org-log-refile)
  9881. (unless (eq org-log-refile 'note)
  9882. (save-excursion (org-add-log-note))))
  9883. (and org-auto-align-tags
  9884. (let ((org-loop-over-headlines-in-active-region nil))
  9885. (org-set-tags nil t)))
  9886. (with-demoted-errors
  9887. (bookmark-set "org-refile-last-stored"))
  9888. ;; If we are refiling for capture, make sure that the
  9889. ;; last-capture pointers point here
  9890. (when (org-bound-and-true-p org-refile-for-capture)
  9891. (with-demoted-errors
  9892. (bookmark-set "org-capture-last-stored-marker"))
  9893. (move-marker org-capture-last-stored-marker (point)))
  9894. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9895. (run-hooks 'org-after-refile-insert-hook))))
  9896. (if regionp
  9897. (delete-region (point) (+ (point) region-length))
  9898. (org-cut-subtree))
  9899. (when (featurep 'org-inlinetask)
  9900. (org-inlinetask-remove-END-maybe))
  9901. (setq org-markers-to-move nil)
  9902. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9903. (defun org-refile-goto-last-stored ()
  9904. "Go to the location where the last refile was stored."
  9905. (interactive)
  9906. (bookmark-jump "org-refile-last-stored")
  9907. (message "This is the location of the last refile"))
  9908. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9909. no-exclude)
  9910. "Prompt the user for a refile location, using PROMPT.
  9911. PROMPT should not be suffixed with a colon and a space, because
  9912. this function appends the default value from
  9913. `org-refile-history' automatically, if that is not empty.
  9914. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9915. this is used for the GOTO interface."
  9916. (let ((org-refile-targets org-refile-targets)
  9917. (org-refile-use-outline-path org-refile-use-outline-path)
  9918. excluded-entries)
  9919. (when (and (derived-mode-p 'org-mode)
  9920. (not org-refile-use-cache)
  9921. (not no-exclude))
  9922. (org-map-tree
  9923. (lambda()
  9924. (setq excluded-entries
  9925. (append excluded-entries (list (org-get-heading t t)))))))
  9926. (setq org-refile-target-table
  9927. (org-refile-get-targets default-buffer excluded-entries)))
  9928. (unless org-refile-target-table
  9929. (error "No refile targets"))
  9930. (let* ((prompt (concat prompt
  9931. (and (car org-refile-history)
  9932. (concat " (default " (car org-refile-history) ")"))
  9933. ": "))
  9934. (cbuf (current-buffer))
  9935. (partial-completion-mode nil)
  9936. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9937. (cfunc (if (and org-refile-use-outline-path
  9938. org-outline-path-complete-in-steps)
  9939. 'org-olpath-completing-read
  9940. 'org-icompleting-read))
  9941. (extra (if org-refile-use-outline-path "/" ""))
  9942. (filename (and cfn (expand-file-name cfn)))
  9943. (tbl (mapcar
  9944. (lambda (x)
  9945. (if (and (not (member org-refile-use-outline-path
  9946. '(file full-file-path)))
  9947. (not (equal filename (nth 1 x))))
  9948. (cons (concat (car x) extra " ("
  9949. (file-name-nondirectory (nth 1 x)) ")")
  9950. (cdr x))
  9951. (cons (concat (car x) extra) (cdr x))))
  9952. org-refile-target-table))
  9953. (completion-ignore-case t)
  9954. pa answ parent-target child parent old-hist)
  9955. (setq old-hist org-refile-history)
  9956. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9957. nil 'org-refile-history (car org-refile-history)))
  9958. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9959. (org-refile-check-position pa)
  9960. (if pa
  9961. (progn
  9962. (when (or (not org-refile-history)
  9963. (not (eq old-hist org-refile-history))
  9964. (not (equal (car pa) (car org-refile-history))))
  9965. (setq org-refile-history
  9966. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9967. org-refile-history
  9968. (cdr org-refile-history))))
  9969. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9970. (pop org-refile-history)))
  9971. pa)
  9972. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9973. (progn
  9974. (setq parent (match-string 1 answ)
  9975. child (match-string 2 answ))
  9976. (setq parent-target (or (assoc parent tbl)
  9977. (assoc (concat parent "/") tbl)))
  9978. (when (and parent-target
  9979. (or (eq new-nodes t)
  9980. (and (eq new-nodes 'confirm)
  9981. (y-or-n-p (format "Create new node \"%s\"? "
  9982. child)))))
  9983. (org-refile-new-child parent-target child)))
  9984. (error "Invalid target location")))))
  9985. (declare-function org-string-nw-p "org-macs" (s))
  9986. (defun org-refile-check-position (refile-pointer)
  9987. "Check if the refile pointer matches the headline to which it points."
  9988. (let* ((file (nth 1 refile-pointer))
  9989. (re (nth 2 refile-pointer))
  9990. (pos (nth 3 refile-pointer))
  9991. buffer)
  9992. (if (and (not (markerp pos)) (not file))
  9993. (error "Please save the buffer to a file before refiling")
  9994. (when (org-string-nw-p re)
  9995. (setq buffer (if (markerp pos)
  9996. (marker-buffer pos)
  9997. (or (find-buffer-visiting file)
  9998. (find-file-noselect file))))
  9999. (with-current-buffer buffer
  10000. (save-excursion
  10001. (save-restriction
  10002. (widen)
  10003. (goto-char pos)
  10004. (beginning-of-line 1)
  10005. (unless (org-looking-at-p re)
  10006. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10007. (defun org-refile-new-child (parent-target child)
  10008. "Use refile target PARENT-TARGET to add new CHILD below it."
  10009. (unless parent-target
  10010. (error "Cannot find parent for new node"))
  10011. (let ((file (nth 1 parent-target))
  10012. (pos (nth 3 parent-target))
  10013. level)
  10014. (with-current-buffer (or (find-buffer-visiting file)
  10015. (find-file-noselect file))
  10016. (save-excursion
  10017. (save-restriction
  10018. (widen)
  10019. (if pos
  10020. (goto-char pos)
  10021. (goto-char (point-max))
  10022. (if (not (bolp)) (newline)))
  10023. (when (looking-at org-outline-regexp)
  10024. (setq level (funcall outline-level))
  10025. (org-end-of-subtree t t))
  10026. (org-back-over-empty-lines)
  10027. (insert "\n" (make-string
  10028. (if pos (org-get-valid-level level 1) 1) ?*)
  10029. " " child "\n")
  10030. (beginning-of-line 0)
  10031. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10032. (defun org-olpath-completing-read (prompt collection &rest args)
  10033. "Read an outline path like a file name."
  10034. (let ((thetable collection)
  10035. (org-completion-use-ido nil) ; does not work with ido.
  10036. (org-completion-use-iswitchb nil)) ; or iswitchb
  10037. (apply
  10038. 'org-icompleting-read prompt
  10039. (lambda (string predicate &optional flag)
  10040. (let (rtn r f (l (length string)))
  10041. (cond
  10042. ((eq flag nil)
  10043. ;; try completion
  10044. (try-completion string thetable))
  10045. ((eq flag t)
  10046. ;; all-completions
  10047. (setq rtn (all-completions string thetable predicate))
  10048. (mapcar
  10049. (lambda (x)
  10050. (setq r (substring x l))
  10051. (if (string-match " ([^)]*)$" x)
  10052. (setq f (match-string 0 x))
  10053. (setq f ""))
  10054. (if (string-match "/" r)
  10055. (concat string (substring r 0 (match-end 0)) f)
  10056. x))
  10057. rtn))
  10058. ((eq flag 'lambda)
  10059. ;; exact match?
  10060. (assoc string thetable)))))
  10061. args)))
  10062. ;;;; Dynamic blocks
  10063. (defun org-find-dblock (name)
  10064. "Find the first dynamic block with name NAME in the buffer.
  10065. If not found, stay at current position and return nil."
  10066. (let ((case-fold-search t) pos)
  10067. (save-excursion
  10068. (goto-char (point-min))
  10069. (setq pos (and (re-search-forward
  10070. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10071. (match-beginning 0))))
  10072. (if pos (goto-char pos))
  10073. pos))
  10074. (defconst org-dblock-start-re
  10075. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10076. "Matches the start line of a dynamic block, with parameters.")
  10077. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10078. "Matches the end of a dynamic block.")
  10079. (defun org-create-dblock (plist)
  10080. "Create a dynamic block section, with parameters taken from PLIST.
  10081. PLIST must contain a :name entry which is used as name of the block."
  10082. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10083. (end-of-line 1)
  10084. (newline))
  10085. (let ((col (current-column))
  10086. (name (plist-get plist :name)))
  10087. (insert "#+BEGIN: " name)
  10088. (while plist
  10089. (if (eq (car plist) :name)
  10090. (setq plist (cddr plist))
  10091. (insert " " (prin1-to-string (pop plist)))))
  10092. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10093. (beginning-of-line -2)))
  10094. (defun org-prepare-dblock ()
  10095. "Prepare dynamic block for refresh.
  10096. This empties the block, puts the cursor at the insert position and returns
  10097. the property list including an extra property :name with the block name."
  10098. (unless (looking-at org-dblock-start-re)
  10099. (error "Not at a dynamic block"))
  10100. (let* ((begdel (1+ (match-end 0)))
  10101. (name (org-no-properties (match-string 1)))
  10102. (params (append (list :name name)
  10103. (read (concat "(" (match-string 3) ")")))))
  10104. (save-excursion
  10105. (beginning-of-line 1)
  10106. (skip-chars-forward " \t")
  10107. (setq params (plist-put params :indentation-column (current-column))))
  10108. (unless (re-search-forward org-dblock-end-re nil t)
  10109. (error "Dynamic block not terminated"))
  10110. (setq params
  10111. (append params
  10112. (list :content (buffer-substring
  10113. begdel (match-beginning 0)))))
  10114. (delete-region begdel (match-beginning 0))
  10115. (goto-char begdel)
  10116. (open-line 1)
  10117. params))
  10118. (defun org-map-dblocks (&optional command)
  10119. "Apply COMMAND to all dynamic blocks in the current buffer.
  10120. If COMMAND is not given, use `org-update-dblock'."
  10121. (let ((cmd (or command 'org-update-dblock)))
  10122. (save-excursion
  10123. (goto-char (point-min))
  10124. (while (re-search-forward org-dblock-start-re nil t)
  10125. (goto-char (match-beginning 0))
  10126. (save-excursion
  10127. (condition-case nil
  10128. (funcall cmd)
  10129. (error (message "Error during update of dynamic block"))))
  10130. (unless (re-search-forward org-dblock-end-re nil t)
  10131. (error "Dynamic block not terminated"))))))
  10132. (defun org-dblock-update (&optional arg)
  10133. "User command for updating dynamic blocks.
  10134. Update the dynamic block at point. With prefix ARG, update all dynamic
  10135. blocks in the buffer."
  10136. (interactive "P")
  10137. (if arg
  10138. (org-update-all-dblocks)
  10139. (or (looking-at org-dblock-start-re)
  10140. (org-beginning-of-dblock))
  10141. (org-update-dblock)))
  10142. (defun org-update-dblock ()
  10143. "Update the dynamic block at point.
  10144. This means to empty the block, parse for parameters and then call
  10145. the correct writing function."
  10146. (interactive)
  10147. (save-window-excursion
  10148. (let* ((pos (point))
  10149. (line (org-current-line))
  10150. (params (org-prepare-dblock))
  10151. (name (plist-get params :name))
  10152. (indent (plist-get params :indentation-column))
  10153. (cmd (intern (concat "org-dblock-write:" name))))
  10154. (message "Updating dynamic block `%s' at line %d..." name line)
  10155. (funcall cmd params)
  10156. (message "Updating dynamic block `%s' at line %d...done" name line)
  10157. (goto-char pos)
  10158. (when (and indent (> indent 0))
  10159. (setq indent (make-string indent ?\ ))
  10160. (save-excursion
  10161. (org-beginning-of-dblock)
  10162. (forward-line 1)
  10163. (while (not (looking-at org-dblock-end-re))
  10164. (insert indent)
  10165. (beginning-of-line 2))
  10166. (when (looking-at org-dblock-end-re)
  10167. (and (looking-at "[ \t]+")
  10168. (replace-match ""))
  10169. (insert indent)))))))
  10170. (defun org-beginning-of-dblock ()
  10171. "Find the beginning of the dynamic block at point.
  10172. Error if there is no such block at point."
  10173. (let ((pos (point))
  10174. beg)
  10175. (end-of-line 1)
  10176. (if (and (re-search-backward org-dblock-start-re nil t)
  10177. (setq beg (match-beginning 0))
  10178. (re-search-forward org-dblock-end-re nil t)
  10179. (> (match-end 0) pos))
  10180. (goto-char beg)
  10181. (goto-char pos)
  10182. (error "Not in a dynamic block"))))
  10183. (defun org-update-all-dblocks ()
  10184. "Update all dynamic blocks in the buffer.
  10185. This function can be used in a hook."
  10186. (interactive)
  10187. (when (derived-mode-p 'org-mode)
  10188. (org-map-dblocks 'org-update-dblock)))
  10189. ;;;; Completion
  10190. (defconst org-additional-option-like-keywords
  10191. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10192. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10193. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10194. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10195. "BEGIN:" "END:"
  10196. "ORGTBL" "TBLFM:" "TBLNAME:"
  10197. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10198. "BEGIN_VERBATIM" "END_VERBATIM"
  10199. "BEGIN_QUOTE" "END_QUOTE"
  10200. "BEGIN_VERSE" "END_VERSE"
  10201. "BEGIN_CENTER" "END_CENTER"
  10202. "BEGIN_SRC" "END_SRC"
  10203. "BEGIN_RESULT" "END_RESULT"
  10204. "BEGIN_lstlisting" "END_lstlisting"
  10205. "NAME:" "RESULTS:"
  10206. "HEADER:" "HEADERS:"
  10207. "COLUMNS:" "PROPERTY:"
  10208. "CAPTION:" "LABEL:"
  10209. "SETUPFILE:"
  10210. "INCLUDE:" "INDEX:"
  10211. "BIND:"
  10212. "MACRO:"))
  10213. (defconst org-options-keywords
  10214. '("TITLE:" "AUTHOR:" "EMAIL:" "DATE:"
  10215. "DESCRIPTION:" "KEYWORDS:" "LANGUAGE:" "OPTIONS:"
  10216. "EXPORT_SELECT_TAGS:" "EXPORT_EXCLUDE_TAGS:"
  10217. "LINK_UP:" "LINK_HOME:" "LINK:" "TODO:"
  10218. "XSLT:" "MATHJAX:" "CATEGORY:" "SEQ_TODO:" "TYP_TODO:"
  10219. "PRIORITIES:" "DRAWERS:" "STARTUP:" "TAGS:" "STYLE:"
  10220. "FILETAGS:" "ARCHIVE:" "INFOJS_OPT:"))
  10221. (defconst org-additional-option-like-keywords-for-flyspell
  10222. (delete-dups
  10223. (split-string
  10224. (mapconcat (lambda(k)
  10225. (replace-regexp-in-string
  10226. "_\\|:" " "
  10227. (concat k " " (downcase k) " " (upcase k))))
  10228. (append org-options-keywords org-additional-option-like-keywords)
  10229. " ")
  10230. " +" t)))
  10231. (defcustom org-structure-template-alist
  10232. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10233. "<src lang=\"?\">\n\n</src>")
  10234. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10235. "<example>\n?\n</example>")
  10236. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10237. "<quote>\n?\n</quote>")
  10238. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10239. "<verse>\n?\n</verse>")
  10240. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10241. "<verbatim>\n?\n</verbatim>")
  10242. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10243. "<center>\n?\n</center>")
  10244. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10245. "<literal style=\"latex\">\n?\n</literal>")
  10246. ("L" "#+LaTeX: "
  10247. "<literal style=\"latex\">?</literal>")
  10248. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10249. "<literal style=\"html\">\n?\n</literal>")
  10250. ("H" "#+HTML: "
  10251. "<literal style=\"html\">?</literal>")
  10252. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10253. ("A" "#+ASCII: ")
  10254. ("i" "#+INDEX: ?"
  10255. "#+INDEX: ?")
  10256. ("I" "#+INCLUDE: %file ?"
  10257. "<include file=%file markup=\"?\">"))
  10258. "Structure completion elements.
  10259. This is a list of abbreviation keys and values. The value gets inserted
  10260. if you type `<' followed by the key and then press the completion key,
  10261. usually `M-TAB'. %file will be replaced by a file name after prompting
  10262. for the file using completion. The cursor will be placed at the position
  10263. of the `?` in the template.
  10264. There are two templates for each key, the first uses the original Org syntax,
  10265. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10266. the default when the /org-mtags.el/ module has been loaded. See also the
  10267. variable `org-mtags-prefer-muse-templates'."
  10268. :group 'org-completion
  10269. :type '(repeat
  10270. (string :tag "Key")
  10271. (string :tag "Template")
  10272. (string :tag "Muse Template")))
  10273. (defun org-try-structure-completion ()
  10274. "Try to complete a structure template before point.
  10275. This looks for strings like \"<e\" on an otherwise empty line and
  10276. expands them."
  10277. (let ((l (buffer-substring (point-at-bol) (point)))
  10278. a)
  10279. (when (and (looking-at "[ \t]*$")
  10280. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10281. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10282. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10283. (match-beginning 1)) a)
  10284. t)))
  10285. (defun org-complete-expand-structure-template (start cell)
  10286. "Expand a structure template."
  10287. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10288. (rpl (nth (if musep 2 1) cell))
  10289. (ind ""))
  10290. (delete-region start (point))
  10291. (when (string-match "\\`#\\+" rpl)
  10292. (cond
  10293. ((bolp))
  10294. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10295. (setq ind (buffer-substring (point-at-bol) (point))))
  10296. (t (newline))))
  10297. (setq start (point))
  10298. (if (string-match "%file" rpl)
  10299. (setq rpl (replace-match
  10300. (concat
  10301. "\""
  10302. (save-match-data
  10303. (abbreviate-file-name (read-file-name "Include file: ")))
  10304. "\"")
  10305. t t rpl)))
  10306. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10307. (concat "\n" ind)))
  10308. (insert rpl)
  10309. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10310. ;;;; TODO, DEADLINE, Comments
  10311. (defun org-toggle-comment ()
  10312. "Change the COMMENT state of an entry."
  10313. (interactive)
  10314. (save-excursion
  10315. (org-back-to-heading)
  10316. (let (case-fold-search)
  10317. (cond
  10318. ((looking-at (format org-heading-keyword-regexp-format
  10319. org-comment-string))
  10320. (goto-char (match-end 1))
  10321. (looking-at (concat " +" org-comment-string))
  10322. (replace-match "" t t)
  10323. (when (eolp) (insert " ")))
  10324. ((looking-at org-outline-regexp)
  10325. (goto-char (match-end 0))
  10326. (insert org-comment-string " "))))))
  10327. (defvar org-last-todo-state-is-todo nil
  10328. "This is non-nil when the last TODO state change led to a TODO state.
  10329. If the last change removed the TODO tag or switched to DONE, then
  10330. this is nil.")
  10331. (defvar org-setting-tags nil) ; dynamically skipped
  10332. (defvar org-todo-setup-filter-hook nil
  10333. "Hook for functions that pre-filter todo specs.
  10334. Each function takes a todo spec and returns either nil or the spec
  10335. transformed into canonical form." )
  10336. (defvar org-todo-get-default-hook nil
  10337. "Hook for functions that get a default item for todo.
  10338. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10339. nil or a string to be used for the todo mark." )
  10340. (defvar org-agenda-headline-snapshot-before-repeat)
  10341. (defun org-current-effective-time ()
  10342. "Return current time adjusted for `org-extend-today-until' variable."
  10343. (let* ((ct (org-current-time))
  10344. (dct (decode-time ct))
  10345. (ct1
  10346. (if (and org-use-effective-time
  10347. (< (nth 2 dct) org-extend-today-until))
  10348. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10349. ct)))
  10350. ct1))
  10351. (defun org-todo-yesterday (&optional arg)
  10352. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10353. (interactive "P")
  10354. (if (eq major-mode 'org-agenda-mode)
  10355. (apply 'org-agenda-todo-yesterday arg)
  10356. (let* ((hour (third (decode-time
  10357. (org-current-time))))
  10358. (org-extend-today-until (1+ hour)))
  10359. (org-todo arg))))
  10360. (defun org-todo (&optional arg)
  10361. "Change the TODO state of an item.
  10362. The state of an item is given by a keyword at the start of the heading,
  10363. like
  10364. *** TODO Write paper
  10365. *** DONE Call mom
  10366. The different keywords are specified in the variable `org-todo-keywords'.
  10367. By default the available states are \"TODO\" and \"DONE\".
  10368. So for this example: when the item starts with TODO, it is changed to DONE.
  10369. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10370. DONE are present, add TODO at the beginning of the heading.
  10371. With \\[universal-argument] prefix arg, use completion to determine the new \
  10372. state.
  10373. With numeric prefix arg, switch to that state.
  10374. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10375. keywords (nextset).
  10376. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10377. With a numeric prefix arg of 0, inhibit note taking for the change.
  10378. For calling through lisp, arg is also interpreted in the following way:
  10379. 'none -> empty state
  10380. \"\"(empty string) -> switch to empty state
  10381. 'done -> switch to DONE
  10382. 'nextset -> switch to the next set of keywords
  10383. 'previousset -> switch to the previous set of keywords
  10384. \"WAITING\" -> switch to the specified keyword, but only if it
  10385. really is a member of `org-todo-keywords'."
  10386. (interactive "P")
  10387. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10388. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10389. 'region-start-level 'region))
  10390. org-loop-over-headlines-in-active-region)
  10391. (org-map-entries
  10392. `(org-todo ,arg)
  10393. org-loop-over-headlines-in-active-region
  10394. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10395. (if (equal arg '(16)) (setq arg 'nextset))
  10396. (let ((org-blocker-hook org-blocker-hook)
  10397. commentp
  10398. case-fold-search)
  10399. (when (equal arg '(64))
  10400. (setq arg nil org-blocker-hook nil))
  10401. (when (and org-blocker-hook
  10402. (or org-inhibit-blocking
  10403. (org-entry-get nil "NOBLOCKING")))
  10404. (setq org-blocker-hook nil))
  10405. (save-excursion
  10406. (catch 'exit
  10407. (org-back-to-heading t)
  10408. (when (looking-at (concat "^\\*+ " org-comment-string))
  10409. (org-toggle-comment)
  10410. (setq commentp t))
  10411. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10412. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10413. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10414. (let* ((match-data (match-data))
  10415. (startpos (point-at-bol))
  10416. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10417. (org-log-done org-log-done)
  10418. (org-log-repeat org-log-repeat)
  10419. (org-todo-log-states org-todo-log-states)
  10420. (org-inhibit-logging
  10421. (if (equal arg 0)
  10422. (progn (setq arg nil) 'note) org-inhibit-logging))
  10423. (this (match-string 1))
  10424. (hl-pos (match-beginning 0))
  10425. (head (org-get-todo-sequence-head this))
  10426. (ass (assoc head org-todo-kwd-alist))
  10427. (interpret (nth 1 ass))
  10428. (done-word (nth 3 ass))
  10429. (final-done-word (nth 4 ass))
  10430. (org-last-state (or this ""))
  10431. (completion-ignore-case t)
  10432. (member (member this org-todo-keywords-1))
  10433. (tail (cdr member))
  10434. (org-state (cond
  10435. ((and org-todo-key-trigger
  10436. (or (and (equal arg '(4))
  10437. (eq org-use-fast-todo-selection 'prefix))
  10438. (and (not arg) org-use-fast-todo-selection
  10439. (not (eq org-use-fast-todo-selection
  10440. 'prefix)))))
  10441. ;; Use fast selection
  10442. (org-fast-todo-selection))
  10443. ((and (equal arg '(4))
  10444. (or (not org-use-fast-todo-selection)
  10445. (not org-todo-key-trigger)))
  10446. ;; Read a state with completion
  10447. (org-icompleting-read
  10448. "State: " (mapcar (lambda(x) (list x))
  10449. org-todo-keywords-1)
  10450. nil t))
  10451. ((eq arg 'right)
  10452. (if this
  10453. (if tail (car tail) nil)
  10454. (car org-todo-keywords-1)))
  10455. ((eq arg 'left)
  10456. (if (equal member org-todo-keywords-1)
  10457. nil
  10458. (if this
  10459. (nth (- (length org-todo-keywords-1)
  10460. (length tail) 2)
  10461. org-todo-keywords-1)
  10462. (org-last org-todo-keywords-1))))
  10463. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10464. (setq arg nil))) ; hack to fall back to cycling
  10465. (arg
  10466. ;; user or caller requests a specific state
  10467. (cond
  10468. ((equal arg "") nil)
  10469. ((eq arg 'none) nil)
  10470. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10471. ((eq arg 'nextset)
  10472. (or (car (cdr (member head org-todo-heads)))
  10473. (car org-todo-heads)))
  10474. ((eq arg 'previousset)
  10475. (let ((org-todo-heads (reverse org-todo-heads)))
  10476. (or (car (cdr (member head org-todo-heads)))
  10477. (car org-todo-heads))))
  10478. ((car (member arg org-todo-keywords-1)))
  10479. ((stringp arg)
  10480. (error "State `%s' not valid in this file" arg))
  10481. ((nth (1- (prefix-numeric-value arg))
  10482. org-todo-keywords-1))))
  10483. ((null member) (or head (car org-todo-keywords-1)))
  10484. ((equal this final-done-word) nil) ;; -> make empty
  10485. ((null tail) nil) ;; -> first entry
  10486. ((memq interpret '(type priority))
  10487. (if (eq this-command last-command)
  10488. (car tail)
  10489. (if (> (length tail) 0)
  10490. (or done-word (car org-done-keywords))
  10491. nil)))
  10492. (t
  10493. (car tail))))
  10494. (org-state (or
  10495. (run-hook-with-args-until-success
  10496. 'org-todo-get-default-hook org-state org-last-state)
  10497. org-state))
  10498. (next (if org-state (concat " " org-state " ") " "))
  10499. (change-plist (list :type 'todo-state-change :from this :to org-state
  10500. :position startpos))
  10501. dolog now-done-p)
  10502. (when org-blocker-hook
  10503. (setq org-last-todo-state-is-todo
  10504. (not (member this org-done-keywords)))
  10505. (unless (save-excursion
  10506. (save-match-data
  10507. (org-with-wide-buffer
  10508. (run-hook-with-args-until-failure
  10509. 'org-blocker-hook change-plist))))
  10510. (if (org-called-interactively-p 'interactive)
  10511. (error "TODO state change from %s to %s blocked" this org-state)
  10512. ;; fail silently
  10513. (message "TODO state change from %s to %s blocked" this org-state)
  10514. (throw 'exit nil))))
  10515. (store-match-data match-data)
  10516. (replace-match next t t)
  10517. (unless (pos-visible-in-window-p hl-pos)
  10518. (message "TODO state changed to %s" (org-trim next)))
  10519. (unless head
  10520. (setq head (org-get-todo-sequence-head org-state)
  10521. ass (assoc head org-todo-kwd-alist)
  10522. interpret (nth 1 ass)
  10523. done-word (nth 3 ass)
  10524. final-done-word (nth 4 ass)))
  10525. (when (memq arg '(nextset previousset))
  10526. (message "Keyword-Set %d/%d: %s"
  10527. (- (length org-todo-sets) -1
  10528. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10529. (length org-todo-sets)
  10530. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10531. (setq org-last-todo-state-is-todo
  10532. (not (member org-state org-done-keywords)))
  10533. (setq now-done-p (and (member org-state org-done-keywords)
  10534. (not (member this org-done-keywords))))
  10535. (and logging (org-local-logging logging))
  10536. (when (and (or org-todo-log-states org-log-done)
  10537. (not (eq org-inhibit-logging t))
  10538. (not (memq arg '(nextset previousset))))
  10539. ;; we need to look at recording a time and note
  10540. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10541. (nth 2 (assoc this org-todo-log-states))))
  10542. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10543. (setq dolog 'time))
  10544. (when (and org-state
  10545. (member org-state org-not-done-keywords)
  10546. (not (member this org-not-done-keywords)))
  10547. ;; This is now a todo state and was not one before
  10548. ;; If there was a CLOSED time stamp, get rid of it.
  10549. (org-add-planning-info nil nil 'closed))
  10550. (when (and now-done-p org-log-done)
  10551. ;; It is now done, and it was not done before
  10552. (org-add-planning-info 'closed (org-current-effective-time))
  10553. (if (and (not dolog) (eq 'note org-log-done))
  10554. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10555. (when (and org-state dolog)
  10556. ;; This is a non-nil state, and we need to log it
  10557. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10558. ;; Fixup tag positioning
  10559. (org-todo-trigger-tag-changes org-state)
  10560. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10561. (when org-provide-todo-statistics
  10562. (org-update-parent-todo-statistics))
  10563. (run-hooks 'org-after-todo-state-change-hook)
  10564. (if (and arg (not (member org-state org-done-keywords)))
  10565. (setq head (org-get-todo-sequence-head org-state)))
  10566. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10567. ;; Do we need to trigger a repeat?
  10568. (when now-done-p
  10569. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10570. ;; This is for the agenda, take a snapshot of the headline.
  10571. (save-match-data
  10572. (setq org-agenda-headline-snapshot-before-repeat
  10573. (org-get-heading))))
  10574. (org-auto-repeat-maybe org-state))
  10575. ;; Fixup cursor location if close to the keyword
  10576. (if (and (outline-on-heading-p)
  10577. (not (bolp))
  10578. (save-excursion (beginning-of-line 1)
  10579. (looking-at org-todo-line-regexp))
  10580. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10581. (progn
  10582. (goto-char (or (match-end 2) (match-end 1)))
  10583. (and (looking-at " ") (just-one-space))))
  10584. (when org-trigger-hook
  10585. (save-excursion
  10586. (run-hook-with-args 'org-trigger-hook change-plist)))
  10587. (when commentp (org-toggle-comment))))))))
  10588. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10589. "Block turning an entry into a TODO, using the hierarchy.
  10590. This checks whether the current task should be blocked from state
  10591. changes. Such blocking occurs when:
  10592. 1. The task has children which are not all in a completed state.
  10593. 2. A task has a parent with the property :ORDERED:, and there
  10594. are siblings prior to the current task with incomplete
  10595. status.
  10596. 3. The parent of the task is blocked because it has siblings that should
  10597. be done first, or is child of a block grandparent TODO entry."
  10598. (if (not org-enforce-todo-dependencies)
  10599. t ; if locally turned off don't block
  10600. (catch 'dont-block
  10601. ;; If this is not a todo state change, or if this entry is already DONE,
  10602. ;; do not block
  10603. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10604. (member (plist-get change-plist :from)
  10605. (cons 'done org-done-keywords))
  10606. (member (plist-get change-plist :to)
  10607. (cons 'todo org-not-done-keywords))
  10608. (not (plist-get change-plist :to)))
  10609. (throw 'dont-block t))
  10610. ;; If this task has children, and any are undone, it's blocked
  10611. (save-excursion
  10612. (org-back-to-heading t)
  10613. (let ((this-level (funcall outline-level)))
  10614. (outline-next-heading)
  10615. (let ((child-level (funcall outline-level)))
  10616. (while (and (not (eobp))
  10617. (> child-level this-level))
  10618. ;; this todo has children, check whether they are all
  10619. ;; completed
  10620. (if (and (not (org-entry-is-done-p))
  10621. (org-entry-is-todo-p))
  10622. (throw 'dont-block nil))
  10623. (outline-next-heading)
  10624. (setq child-level (funcall outline-level))))))
  10625. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10626. ;; any previous siblings are undone, it's blocked
  10627. (save-excursion
  10628. (org-back-to-heading t)
  10629. (let* ((pos (point))
  10630. (parent-pos (and (org-up-heading-safe) (point))))
  10631. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10632. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10633. (forward-line 1)
  10634. (re-search-forward org-not-done-heading-regexp pos t))
  10635. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10636. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10637. (while t
  10638. (goto-char parent-pos)
  10639. (if (not (looking-at org-not-done-heading-regexp))
  10640. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10641. (setq pos (point))
  10642. (setq parent-pos (and (org-up-heading-safe) (point)))
  10643. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10644. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10645. (forward-line 1)
  10646. (re-search-forward org-not-done-heading-regexp pos t))
  10647. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10648. (defcustom org-track-ordered-property-with-tag nil
  10649. "Should the ORDERED property also be shown as a tag?
  10650. The ORDERED property decides if an entry should require subtasks to be
  10651. completed in sequence. Since a property is not very visible, setting
  10652. this option means that toggling the ORDERED property with the command
  10653. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10654. not relevant for the behavior, but it makes things more visible.
  10655. Note that toggling the tag with tags commands will not change the property
  10656. and therefore not influence behavior!
  10657. This can be t, meaning the tag ORDERED should be used, It can also be a
  10658. string to select a different tag for this task."
  10659. :group 'org-todo
  10660. :type '(choice
  10661. (const :tag "No tracking" nil)
  10662. (const :tag "Track with ORDERED tag" t)
  10663. (string :tag "Use other tag")))
  10664. (defun org-toggle-ordered-property ()
  10665. "Toggle the ORDERED property of the current entry.
  10666. For better visibility, you can track the value of this property with a tag.
  10667. See variable `org-track-ordered-property-with-tag'."
  10668. (interactive)
  10669. (let* ((t1 org-track-ordered-property-with-tag)
  10670. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10671. (save-excursion
  10672. (org-back-to-heading)
  10673. (if (org-entry-get nil "ORDERED")
  10674. (progn
  10675. (org-delete-property "ORDERED")
  10676. (and tag (org-toggle-tag tag 'off))
  10677. (message "Subtasks can be completed in arbitrary order"))
  10678. (org-entry-put nil "ORDERED" "t")
  10679. (and tag (org-toggle-tag tag 'on))
  10680. (message "Subtasks must be completed in sequence")))))
  10681. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10682. (defun org-block-todo-from-checkboxes (change-plist)
  10683. "Block turning an entry into a TODO, using checkboxes.
  10684. This checks whether the current task should be blocked from state
  10685. changes because there are unchecked boxes in this entry."
  10686. (if (not org-enforce-todo-checkbox-dependencies)
  10687. t ; if locally turned off don't block
  10688. (catch 'dont-block
  10689. ;; If this is not a todo state change, or if this entry is already DONE,
  10690. ;; do not block
  10691. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10692. (member (plist-get change-plist :from)
  10693. (cons 'done org-done-keywords))
  10694. (member (plist-get change-plist :to)
  10695. (cons 'todo org-not-done-keywords))
  10696. (not (plist-get change-plist :to)))
  10697. (throw 'dont-block t))
  10698. ;; If this task has checkboxes that are not checked, it's blocked
  10699. (save-excursion
  10700. (org-back-to-heading t)
  10701. (let ((beg (point)) end)
  10702. (outline-next-heading)
  10703. (setq end (point))
  10704. (goto-char beg)
  10705. (if (org-list-search-forward
  10706. (concat (org-item-beginning-re)
  10707. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10708. "\\[[- ]\\]")
  10709. end t)
  10710. (progn
  10711. (if (boundp 'org-blocked-by-checkboxes)
  10712. (setq org-blocked-by-checkboxes t))
  10713. (throw 'dont-block nil)))))
  10714. t))) ; do not block
  10715. (defun org-entry-blocked-p ()
  10716. "Is the current entry blocked?"
  10717. (org-with-buffer-modified-unmodified
  10718. (if (org-entry-get nil "NOBLOCKING")
  10719. nil ;; Never block this entry
  10720. (not
  10721. (run-hook-with-args-until-failure
  10722. 'org-blocker-hook
  10723. (list :type 'todo-state-change
  10724. :position (point)
  10725. :from 'todo
  10726. :to 'done))))))
  10727. (defun org-update-statistics-cookies (all)
  10728. "Update the statistics cookie, either from TODO or from checkboxes.
  10729. This should be called with the cursor in a line with a statistics cookie."
  10730. (interactive "P")
  10731. (if all
  10732. (progn
  10733. (org-update-checkbox-count 'all)
  10734. (org-map-entries 'org-update-parent-todo-statistics))
  10735. (if (not (org-at-heading-p))
  10736. (org-update-checkbox-count)
  10737. (let ((pos (point-marker))
  10738. end l1 l2)
  10739. (ignore-errors (org-back-to-heading t))
  10740. (if (not (org-at-heading-p))
  10741. (org-update-checkbox-count)
  10742. (setq l1 (org-outline-level))
  10743. (setq end (save-excursion
  10744. (outline-next-heading)
  10745. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10746. (point)))
  10747. (if (and (save-excursion
  10748. (re-search-forward
  10749. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10750. (not (save-excursion (re-search-forward
  10751. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10752. (org-update-checkbox-count)
  10753. (if (and l2 (> l2 l1))
  10754. (progn
  10755. (goto-char end)
  10756. (org-update-parent-todo-statistics))
  10757. (goto-char pos)
  10758. (beginning-of-line 1)
  10759. (while (re-search-forward
  10760. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10761. (point-at-eol) t)
  10762. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10763. (goto-char pos)
  10764. (move-marker pos nil)))))
  10765. (defvar org-entry-property-inherited-from) ;; defined below
  10766. (defun org-update-parent-todo-statistics ()
  10767. "Update any statistics cookie in the parent of the current headline.
  10768. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10769. the entire subtree and this will travel up the hierarchy and update
  10770. statistics everywhere."
  10771. (let* ((prop (save-excursion (org-up-heading-safe)
  10772. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10773. (recursive (or (not org-hierarchical-todo-statistics)
  10774. (and prop (string-match "\\<recursive\\>" prop))))
  10775. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10776. 0))
  10777. (first t)
  10778. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10779. level ltoggle l1 new ndel
  10780. (cnt-all 0) (cnt-done 0) is-percent kwd
  10781. checkbox-beg ov ovs ove cookie-present)
  10782. (catch 'exit
  10783. (save-excursion
  10784. (beginning-of-line 1)
  10785. (setq ltoggle (funcall outline-level))
  10786. ;; Three situations are to consider:
  10787. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10788. ;; to the top-level ancestor on the headline;
  10789. ;; 2. If parent has "recursive" property, repeat up to the
  10790. ;; headline setting that property, taking inheritance into
  10791. ;; account;
  10792. ;; 3. Else, move up to direct parent and proceed only once.
  10793. (while (and (setq level (org-up-heading-safe))
  10794. (or recursive first)
  10795. (>= (point) lim))
  10796. (setq first nil cookie-present nil)
  10797. (unless (and level
  10798. (not (string-match
  10799. "\\<checkbox\\>"
  10800. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10801. "")))))
  10802. (throw 'exit nil))
  10803. (while (re-search-forward box-re (point-at-eol) t)
  10804. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10805. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10806. (save-match-data
  10807. (unless (outline-next-heading) (throw 'exit nil))
  10808. (while (and (looking-at org-complex-heading-regexp)
  10809. (> (setq l1 (length (match-string 1))) level))
  10810. (setq kwd (and (or recursive (= l1 ltoggle))
  10811. (match-string 2)))
  10812. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10813. (and (listp org-provide-todo-statistics)
  10814. (or (member kwd org-provide-todo-statistics)
  10815. (member kwd org-done-keywords))))
  10816. (setq cnt-all (1+ cnt-all))
  10817. (if (eq org-provide-todo-statistics t)
  10818. (and kwd (setq cnt-all (1+ cnt-all)))))
  10819. (and (member kwd org-done-keywords)
  10820. (setq cnt-done (1+ cnt-done)))
  10821. (outline-next-heading)))
  10822. (setq new
  10823. (if is-percent
  10824. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10825. (format "[%d/%d]" cnt-done cnt-all))
  10826. ndel (- (match-end 0) checkbox-beg))
  10827. ;; handle overlays when updating cookie from column view
  10828. (when (setq ov (car (overlays-at checkbox-beg)))
  10829. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10830. (delete-overlay ov))
  10831. (goto-char checkbox-beg)
  10832. (insert new)
  10833. (delete-region (point) (+ (point) ndel))
  10834. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10835. (when ov (move-overlay ov ovs ove)))
  10836. (when cookie-present
  10837. (run-hook-with-args 'org-after-todo-statistics-hook
  10838. cnt-done (- cnt-all cnt-done))))))
  10839. (run-hooks 'org-todo-statistics-hook)))
  10840. (defvar org-after-todo-statistics-hook nil
  10841. "Hook that is called after a TODO statistics cookie has been updated.
  10842. Each function is called with two arguments: the number of not-done entries
  10843. and the number of done entries.
  10844. For example, the following function, when added to this hook, will switch
  10845. an entry to DONE when all children are done, and back to TODO when new
  10846. entries are set to a TODO status. Note that this hook is only called
  10847. when there is a statistics cookie in the headline!
  10848. (defun org-summary-todo (n-done n-not-done)
  10849. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10850. (let (org-log-done org-log-states) ; turn off logging
  10851. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10852. ")
  10853. (defvar org-todo-statistics-hook nil
  10854. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10855. This hook runs even if there is no statistics cookie present, in which case
  10856. `org-after-todo-statistics-hook' would not run.")
  10857. (defun org-todo-trigger-tag-changes (state)
  10858. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10859. (let ((l org-todo-state-tags-triggers)
  10860. changes)
  10861. (when (or (not state) (equal state ""))
  10862. (setq changes (append changes (cdr (assoc "" l)))))
  10863. (when (and (stringp state) (> (length state) 0))
  10864. (setq changes (append changes (cdr (assoc state l)))))
  10865. (when (member state org-not-done-keywords)
  10866. (setq changes (append changes (cdr (assoc 'todo l)))))
  10867. (when (member state org-done-keywords)
  10868. (setq changes (append changes (cdr (assoc 'done l)))))
  10869. (dolist (c changes)
  10870. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10871. (defun org-local-logging (value)
  10872. "Get logging settings from a property VALUE."
  10873. (let* (words w a)
  10874. ;; directly set the variables, they are already local.
  10875. (setq org-log-done nil
  10876. org-log-repeat nil
  10877. org-todo-log-states nil)
  10878. (setq words (org-split-string value))
  10879. (while (setq w (pop words))
  10880. (cond
  10881. ((setq a (assoc w org-startup-options))
  10882. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10883. (set (nth 1 a) (nth 2 a))))
  10884. ((setq a (org-extract-log-state-settings w))
  10885. (and (member (car a) org-todo-keywords-1)
  10886. (push a org-todo-log-states)))))))
  10887. (defun org-get-todo-sequence-head (kwd)
  10888. "Return the head of the TODO sequence to which KWD belongs.
  10889. If KWD is not set, check if there is a text property remembering the
  10890. right sequence."
  10891. (let (p)
  10892. (cond
  10893. ((not kwd)
  10894. (or (get-text-property (point-at-bol) 'org-todo-head)
  10895. (progn
  10896. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10897. nil (point-at-eol)))
  10898. (get-text-property p 'org-todo-head))))
  10899. ((not (member kwd org-todo-keywords-1))
  10900. (car org-todo-keywords-1))
  10901. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10902. (defun org-fast-todo-selection ()
  10903. "Fast TODO keyword selection with single keys.
  10904. Returns the new TODO keyword, or nil if no state change should occur."
  10905. (let* ((fulltable org-todo-key-alist)
  10906. (done-keywords org-done-keywords) ;; needed for the faces.
  10907. (maxlen (apply 'max (mapcar
  10908. (lambda (x)
  10909. (if (stringp (car x)) (string-width (car x)) 0))
  10910. fulltable)))
  10911. (expert nil)
  10912. (fwidth (+ maxlen 3 1 3))
  10913. (ncol (/ (- (window-width) 4) fwidth))
  10914. tg cnt e c tbl
  10915. groups ingroup)
  10916. (save-excursion
  10917. (save-window-excursion
  10918. (if expert
  10919. (set-buffer (get-buffer-create " *Org todo*"))
  10920. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10921. (erase-buffer)
  10922. (org-set-local 'org-done-keywords done-keywords)
  10923. (setq tbl fulltable cnt 0)
  10924. (while (setq e (pop tbl))
  10925. (cond
  10926. ((equal e '(:startgroup))
  10927. (push '() groups) (setq ingroup t)
  10928. (when (not (= cnt 0))
  10929. (setq cnt 0)
  10930. (insert "\n"))
  10931. (insert "{ "))
  10932. ((equal e '(:endgroup))
  10933. (setq ingroup nil cnt 0)
  10934. (insert "}\n"))
  10935. ((equal e '(:newline))
  10936. (when (not (= cnt 0))
  10937. (setq cnt 0)
  10938. (insert "\n")
  10939. (setq e (car tbl))
  10940. (while (equal (car tbl) '(:newline))
  10941. (insert "\n")
  10942. (setq tbl (cdr tbl)))))
  10943. (t
  10944. (setq tg (car e) c (cdr e))
  10945. (if ingroup (push tg (car groups)))
  10946. (setq tg (org-add-props tg nil 'face
  10947. (org-get-todo-face tg)))
  10948. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10949. (insert "[" c "] " tg (make-string
  10950. (- fwidth 4 (length tg)) ?\ ))
  10951. (when (= (setq cnt (1+ cnt)) ncol)
  10952. (insert "\n")
  10953. (if ingroup (insert " "))
  10954. (setq cnt 0)))))
  10955. (insert "\n")
  10956. (goto-char (point-min))
  10957. (if (not expert) (org-fit-window-to-buffer))
  10958. (message "[a-z..]:Set [SPC]:clear")
  10959. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10960. (cond
  10961. ((or (= c ?\C-g)
  10962. (and (= c ?q) (not (rassoc c fulltable))))
  10963. (setq quit-flag t))
  10964. ((= c ?\ ) nil)
  10965. ((setq e (rassoc c fulltable) tg (car e))
  10966. tg)
  10967. (t (setq quit-flag t)))))))
  10968. (defun org-entry-is-todo-p ()
  10969. (member (org-get-todo-state) org-not-done-keywords))
  10970. (defun org-entry-is-done-p ()
  10971. (member (org-get-todo-state) org-done-keywords))
  10972. (defun org-get-todo-state ()
  10973. (save-excursion
  10974. (org-back-to-heading t)
  10975. (and (looking-at org-todo-line-regexp)
  10976. (match-end 2)
  10977. (match-string 2))))
  10978. (defun org-at-date-range-p (&optional inactive-ok)
  10979. "Is the cursor inside a date range?"
  10980. (interactive)
  10981. (save-excursion
  10982. (catch 'exit
  10983. (let ((pos (point)))
  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. (skip-chars-backward "^<[\r\n")
  10990. (skip-chars-backward "<[")
  10991. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10992. (>= (match-end 0) pos)
  10993. (throw 'exit t)))
  10994. nil)))
  10995. (defun org-get-repeat (&optional tagline)
  10996. "Check if there is a deadline/schedule with repeater in this entry."
  10997. (save-match-data
  10998. (save-excursion
  10999. (org-back-to-heading t)
  11000. (and (re-search-forward (if tagline
  11001. (concat tagline "\\s-*" org-repeat-re)
  11002. org-repeat-re)
  11003. (org-entry-end-position) t)
  11004. (match-string-no-properties 1)))))
  11005. (defvar org-last-changed-timestamp)
  11006. (defvar org-last-inserted-timestamp)
  11007. (defvar org-log-post-message)
  11008. (defvar org-log-note-purpose)
  11009. (defvar org-log-note-how)
  11010. (defvar org-log-note-extra)
  11011. (defun org-auto-repeat-maybe (done-word)
  11012. "Check if the current headline contains a repeated deadline/schedule.
  11013. If yes, set TODO state back to what it was and change the base date
  11014. of repeating deadline/scheduled time stamps to new date.
  11015. This function is run automatically after each state change to a DONE state."
  11016. ;; last-state is dynamically scoped into this function
  11017. (let* ((repeat (org-get-repeat))
  11018. (aa (assoc org-last-state org-todo-kwd-alist))
  11019. (interpret (nth 1 aa))
  11020. (head (nth 2 aa))
  11021. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11022. (msg "Entry repeats: ")
  11023. (org-log-done nil)
  11024. (org-todo-log-states nil)
  11025. re type n what ts time to-state)
  11026. (when repeat
  11027. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11028. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11029. org-todo-repeat-to-state))
  11030. (unless (and to-state (member to-state org-todo-keywords-1))
  11031. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11032. (org-todo to-state)
  11033. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11034. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11035. (org-time-stamp-format t t))))
  11036. (when org-log-repeat
  11037. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11038. (memq 'org-add-log-note post-command-hook))
  11039. ;; OK, we are already setup for some record
  11040. (if (eq org-log-repeat 'note)
  11041. ;; make sure we take a note, not only a time stamp
  11042. (setq org-log-note-how 'note))
  11043. ;; Set up for taking a record
  11044. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11045. org-last-state
  11046. 'findpos org-log-repeat)))
  11047. (org-back-to-heading t)
  11048. (org-add-planning-info nil nil 'closed)
  11049. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11050. org-deadline-time-regexp "\\)\\|\\("
  11051. org-ts-regexp "\\)"))
  11052. (while (re-search-forward
  11053. re (save-excursion (outline-next-heading) (point)) t)
  11054. (setq type (if (match-end 1) org-scheduled-string
  11055. (if (match-end 3) org-deadline-string "Plain:"))
  11056. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11057. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11058. (setq n (string-to-number (match-string 2 ts))
  11059. what (match-string 3 ts))
  11060. (if (equal what "w") (setq n (* n 7) what "d"))
  11061. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11062. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11063. ;; Preparation, see if we need to modify the start date for the change
  11064. (when (match-end 1)
  11065. (setq time (save-match-data (org-time-string-to-time ts)))
  11066. (cond
  11067. ((equal (match-string 1 ts) ".")
  11068. ;; Shift starting date to today
  11069. (org-timestamp-change
  11070. (- (org-today) (time-to-days time))
  11071. 'day))
  11072. ((equal (match-string 1 ts) "+")
  11073. (let ((nshiftmax 10) (nshift 0))
  11074. (while (or (= nshift 0)
  11075. (<= (time-to-days time)
  11076. (time-to-days (current-time))))
  11077. (when (= (incf nshift) nshiftmax)
  11078. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11079. (error "Abort")))
  11080. (org-timestamp-change n (cdr (assoc what whata)))
  11081. (org-at-timestamp-p t)
  11082. (setq ts (match-string 1))
  11083. (setq time (save-match-data (org-time-string-to-time ts)))))
  11084. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11085. ;; rematch, so that we have everything in place for the real shift
  11086. (org-at-timestamp-p t)
  11087. (setq ts (match-string 1))
  11088. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11089. (org-timestamp-change n (cdr (assoc what whata)))
  11090. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11091. (setq org-log-post-message msg)
  11092. (message "%s" msg))))
  11093. (defun org-show-todo-tree (arg)
  11094. "Make a compact tree which shows all headlines marked with TODO.
  11095. The tree will show the lines where the regexp matches, and all higher
  11096. headlines above the match.
  11097. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11098. With a numeric prefix N, construct a sparse tree for the Nth element
  11099. of `org-todo-keywords-1'."
  11100. (interactive "P")
  11101. (let ((case-fold-search nil)
  11102. (kwd-re
  11103. (cond ((null arg) org-not-done-regexp)
  11104. ((equal arg '(4))
  11105. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11106. (mapcar 'list org-todo-keywords-1))))
  11107. (concat "\\("
  11108. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11109. "\\)\\>")))
  11110. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11111. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11112. org-todo-keywords-1)))
  11113. (t (error "Invalid prefix argument: %s" arg)))))
  11114. (message "%d TODO entries found"
  11115. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11116. (defun org-deadline (&optional remove time)
  11117. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11118. With argument REMOVE, remove any deadline from the item.
  11119. With argument TIME, set the deadline at the corresponding date. TIME
  11120. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11121. (interactive "P")
  11122. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11123. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11124. 'region-start-level 'region))
  11125. org-loop-over-headlines-in-active-region)
  11126. (org-map-entries
  11127. `(org-deadline ',remove ,time)
  11128. org-loop-over-headlines-in-active-region
  11129. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11130. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11131. (repeater (and old-date
  11132. (string-match
  11133. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11134. old-date)
  11135. (match-string 1 old-date))))
  11136. (if remove
  11137. (progn
  11138. (when (and old-date org-log-redeadline)
  11139. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11140. org-log-redeadline))
  11141. (org-remove-timestamp-with-keyword org-deadline-string)
  11142. (message "Item no longer has a deadline."))
  11143. (org-add-planning-info 'deadline time 'closed)
  11144. (when (and old-date org-log-redeadline
  11145. (not (equal old-date
  11146. (substring org-last-inserted-timestamp 1 -1))))
  11147. (org-add-log-setup 'redeadline nil old-date 'findpos
  11148. org-log-redeadline))
  11149. (when repeater
  11150. (save-excursion
  11151. (org-back-to-heading t)
  11152. (when (re-search-forward (concat org-deadline-string " "
  11153. org-last-inserted-timestamp)
  11154. (save-excursion
  11155. (outline-next-heading) (point)) t)
  11156. (goto-char (1- (match-end 0)))
  11157. (insert " " repeater)
  11158. (setq org-last-inserted-timestamp
  11159. (concat (substring org-last-inserted-timestamp 0 -1)
  11160. " " repeater
  11161. (substring org-last-inserted-timestamp -1))))))
  11162. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11163. (defun org-schedule (&optional remove time)
  11164. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11165. With argument REMOVE, remove any scheduling date from the item.
  11166. With argument TIME, scheduled at the corresponding date. TIME can
  11167. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11168. (interactive "P")
  11169. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11170. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11171. 'region-start-level 'region))
  11172. org-loop-over-headlines-in-active-region)
  11173. (org-map-entries
  11174. `(org-schedule ',remove ,time)
  11175. org-loop-over-headlines-in-active-region
  11176. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11177. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11178. (repeater (and old-date
  11179. (string-match
  11180. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11181. old-date)
  11182. (match-string 1 old-date))))
  11183. (if remove
  11184. (progn
  11185. (when (and old-date org-log-reschedule)
  11186. (org-add-log-setup 'delschedule nil old-date 'findpos
  11187. org-log-reschedule))
  11188. (org-remove-timestamp-with-keyword org-scheduled-string)
  11189. (message "Item is no longer scheduled."))
  11190. (org-add-planning-info 'scheduled time 'closed)
  11191. (when (and old-date org-log-reschedule
  11192. (not (equal old-date
  11193. (substring org-last-inserted-timestamp 1 -1))))
  11194. (org-add-log-setup 'reschedule nil old-date 'findpos
  11195. org-log-reschedule))
  11196. (when repeater
  11197. (save-excursion
  11198. (org-back-to-heading t)
  11199. (when (re-search-forward (concat org-scheduled-string " "
  11200. org-last-inserted-timestamp)
  11201. (save-excursion
  11202. (outline-next-heading) (point)) t)
  11203. (goto-char (1- (match-end 0)))
  11204. (insert " " repeater)
  11205. (setq org-last-inserted-timestamp
  11206. (concat (substring org-last-inserted-timestamp 0 -1)
  11207. " " repeater
  11208. (substring org-last-inserted-timestamp -1))))))
  11209. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11210. (defun org-get-scheduled-time (pom &optional inherit)
  11211. "Get the scheduled time as a time tuple, of a format suitable
  11212. for calling org-schedule with, or if there is no scheduling,
  11213. returns nil."
  11214. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11215. (when time
  11216. (apply 'encode-time (org-parse-time-string time)))))
  11217. (defun org-get-deadline-time (pom &optional inherit)
  11218. "Get the deadline as a time tuple, of a format suitable for
  11219. calling org-deadline with, or if there is no scheduling, returns
  11220. nil."
  11221. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11222. (when time
  11223. (apply 'encode-time (org-parse-time-string time)))))
  11224. (defun org-remove-timestamp-with-keyword (keyword)
  11225. "Remove all time stamps with KEYWORD in the current entry."
  11226. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11227. beg)
  11228. (save-excursion
  11229. (org-back-to-heading t)
  11230. (setq beg (point))
  11231. (outline-next-heading)
  11232. (while (re-search-backward re beg t)
  11233. (replace-match "")
  11234. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11235. (equal (char-before) ?\ ))
  11236. (backward-delete-char 1)
  11237. (if (string-match "^[ \t]*$" (buffer-substring
  11238. (point-at-bol) (point-at-eol)))
  11239. (delete-region (point-at-bol)
  11240. (min (point-max) (1+ (point-at-eol))))))))))
  11241. (defun org-add-planning-info (what &optional time &rest remove)
  11242. "Insert new timestamp with keyword in the line directly after the headline.
  11243. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11244. If non is given, the user is prompted for a date.
  11245. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11246. be removed."
  11247. (interactive)
  11248. (let (org-time-was-given org-end-time-was-given ts
  11249. end default-time default-input)
  11250. (catch 'exit
  11251. (when (and (memq what '(scheduled deadline))
  11252. (or (not time)
  11253. (and (stringp time)
  11254. (string-match "^[-+]+[0-9]" time))))
  11255. ;; Try to get a default date/time from existing timestamp
  11256. (save-excursion
  11257. (org-back-to-heading t)
  11258. (setq end (save-excursion (outline-next-heading) (point)))
  11259. (when (re-search-forward (if (eq what 'scheduled)
  11260. org-scheduled-time-regexp
  11261. org-deadline-time-regexp)
  11262. end t)
  11263. (setq ts (match-string 1)
  11264. default-time
  11265. (apply 'encode-time (org-parse-time-string ts))
  11266. default-input (and ts (org-get-compact-tod ts))))))
  11267. (when what
  11268. (setq time
  11269. (if (stringp time)
  11270. ;; This is a string (relative or absolute), set proper date
  11271. (apply 'encode-time
  11272. (org-read-date-analyze
  11273. time default-time (decode-time default-time)))
  11274. ;; If necessary, get the time from the user
  11275. (or time (org-read-date nil 'to-time nil nil
  11276. default-time default-input)))))
  11277. (when (and org-insert-labeled-timestamps-at-point
  11278. (member what '(scheduled deadline)))
  11279. (insert
  11280. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11281. (org-insert-time-stamp time org-time-was-given
  11282. nil nil nil (list org-end-time-was-given))
  11283. (setq what nil))
  11284. (save-excursion
  11285. (save-restriction
  11286. (let (col list elt ts buffer-invisibility-spec)
  11287. (org-back-to-heading t)
  11288. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11289. (goto-char (match-end 1))
  11290. (setq col (current-column))
  11291. (goto-char (match-end 0))
  11292. (if (eobp) (insert "\n") (forward-char 1))
  11293. (when (and (not what)
  11294. (not (looking-at
  11295. (concat "[ \t]*"
  11296. org-keyword-time-not-clock-regexp))))
  11297. ;; Nothing to add, nothing to remove...... :-)
  11298. (throw 'exit nil))
  11299. (if (and (not (looking-at org-outline-regexp))
  11300. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11301. "[^\r\n]*"))
  11302. (not (equal (match-string 1) org-clock-string)))
  11303. (narrow-to-region (match-beginning 0) (match-end 0))
  11304. (insert-before-markers "\n")
  11305. (backward-char 1)
  11306. (narrow-to-region (point) (point))
  11307. (and org-adapt-indentation (org-indent-to-column col)))
  11308. ;; Check if we have to remove something.
  11309. (setq list (cons what remove))
  11310. (while list
  11311. (setq elt (pop list))
  11312. (when (or (and (eq elt 'scheduled)
  11313. (re-search-forward org-scheduled-time-regexp nil t))
  11314. (and (eq elt 'deadline)
  11315. (re-search-forward org-deadline-time-regexp nil t))
  11316. (and (eq elt 'closed)
  11317. (re-search-forward org-closed-time-regexp nil t)))
  11318. (replace-match "")
  11319. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11320. (and (looking-at "[ \t]+") (replace-match ""))
  11321. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11322. (when what
  11323. (insert
  11324. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11325. (cond ((eq what 'scheduled) org-scheduled-string)
  11326. ((eq what 'deadline) org-deadline-string)
  11327. ((eq what 'closed) org-closed-string))
  11328. " ")
  11329. (setq ts (org-insert-time-stamp
  11330. time
  11331. (or org-time-was-given
  11332. (and (eq what 'closed) org-log-done-with-time))
  11333. (eq what 'closed)
  11334. nil nil (list org-end-time-was-given)))
  11335. (insert
  11336. (if (not (or (bolp) (eq (char-before) ?\ )
  11337. (memq (char-after) '(32 10))
  11338. (eobp))) " " ""))
  11339. (end-of-line 1))
  11340. (goto-char (point-min))
  11341. (widen)
  11342. (if (and (looking-at "[ \t]*\n")
  11343. (equal (char-before) ?\n))
  11344. (delete-region (1- (point)) (point-at-eol)))
  11345. ts))))))
  11346. (defvar org-log-note-marker (make-marker))
  11347. (defvar org-log-note-purpose nil)
  11348. (defvar org-log-note-state nil)
  11349. (defvar org-log-note-previous-state nil)
  11350. (defvar org-log-note-how nil)
  11351. (defvar org-log-note-extra nil)
  11352. (defvar org-log-note-window-configuration nil)
  11353. (defvar org-log-note-return-to (make-marker))
  11354. (defvar org-log-note-effective-time nil
  11355. "Remembered current time so that dynamically scoped
  11356. `org-extend-today-until' affects tha timestamps in state change
  11357. log")
  11358. (defvar org-log-post-message nil
  11359. "Message to be displayed after a log note has been stored.
  11360. The auto-repeater uses this.")
  11361. (defun org-add-note ()
  11362. "Add a note to the current entry.
  11363. This is done in the same way as adding a state change note."
  11364. (interactive)
  11365. (org-add-log-setup 'note nil nil 'findpos nil))
  11366. (defvar org-property-end-re)
  11367. (defun org-add-log-setup (&optional purpose state prev-state
  11368. findpos how extra)
  11369. "Set up the post command hook to take a note.
  11370. If this is about to TODO state change, the new state is expected in STATE.
  11371. When FINDPOS is non-nil, find the correct position for the note in
  11372. the current entry. If not, assume that it can be inserted at point.
  11373. HOW is an indicator what kind of note should be created.
  11374. EXTRA is additional text that will be inserted into the notes buffer."
  11375. (let* ((org-log-into-drawer (org-log-into-drawer))
  11376. (drawer (cond ((stringp org-log-into-drawer)
  11377. org-log-into-drawer)
  11378. (org-log-into-drawer "LOGBOOK"))))
  11379. (save-restriction
  11380. (save-excursion
  11381. (when findpos
  11382. (org-back-to-heading t)
  11383. (narrow-to-region (point) (save-excursion
  11384. (outline-next-heading) (point)))
  11385. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11386. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11387. "[^\r\n]*\\)?"))
  11388. (goto-char (match-end 0))
  11389. (cond
  11390. (drawer
  11391. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11392. nil t)
  11393. (progn
  11394. (goto-char (match-end 0))
  11395. (or org-log-states-order-reversed
  11396. (and (re-search-forward org-property-end-re nil t)
  11397. (goto-char (1- (match-beginning 0))))))
  11398. (insert "\n:" drawer ":\n:END:")
  11399. (beginning-of-line 0)
  11400. (org-indent-line)
  11401. (beginning-of-line 2)
  11402. (org-indent-line)
  11403. (end-of-line 0)))
  11404. ((and org-log-state-notes-insert-after-drawers
  11405. (save-excursion
  11406. (forward-line) (looking-at org-drawer-regexp)))
  11407. (forward-line)
  11408. (while (looking-at org-drawer-regexp)
  11409. (goto-char (match-end 0))
  11410. (re-search-forward org-property-end-re (point-max) t)
  11411. (forward-line))
  11412. (forward-line -1)))
  11413. (unless org-log-states-order-reversed
  11414. (and (= (char-after) ?\n) (forward-char 1))
  11415. (org-skip-over-state-notes)
  11416. (skip-chars-backward " \t\n\r")))
  11417. (move-marker org-log-note-marker (point))
  11418. (setq org-log-note-purpose purpose
  11419. org-log-note-state state
  11420. org-log-note-previous-state prev-state
  11421. org-log-note-how how
  11422. org-log-note-extra extra
  11423. org-log-note-effective-time (org-current-effective-time))
  11424. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11425. (defun org-skip-over-state-notes ()
  11426. "Skip past the list of State notes in an entry."
  11427. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11428. (when (ignore-errors (goto-char (org-in-item-p)))
  11429. (let* ((struct (org-list-struct))
  11430. (prevs (org-list-prevs-alist struct)))
  11431. (while (looking-at "[ \t]*- State")
  11432. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11433. (org-list-get-item-end (point) struct)))))))
  11434. (defun org-add-log-note (&optional purpose)
  11435. "Pop up a window for taking a note, and add this note later at point."
  11436. (remove-hook 'post-command-hook 'org-add-log-note)
  11437. (setq org-log-note-window-configuration (current-window-configuration))
  11438. (delete-other-windows)
  11439. (move-marker org-log-note-return-to (point))
  11440. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11441. (goto-char org-log-note-marker)
  11442. (org-switch-to-buffer-other-window "*Org Note*")
  11443. (erase-buffer)
  11444. (if (memq org-log-note-how '(time state))
  11445. (let (current-prefix-arg) (org-store-log-note))
  11446. (let ((org-inhibit-startup t)) (org-mode))
  11447. (insert (format "# Insert note for %s.
  11448. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11449. (cond
  11450. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11451. ((eq org-log-note-purpose 'done) "closed todo item")
  11452. ((eq org-log-note-purpose 'state)
  11453. (format "state change from \"%s\" to \"%s\""
  11454. (or org-log-note-previous-state "")
  11455. (or org-log-note-state "")))
  11456. ((eq org-log-note-purpose 'reschedule)
  11457. "rescheduling")
  11458. ((eq org-log-note-purpose 'delschedule)
  11459. "no longer scheduled")
  11460. ((eq org-log-note-purpose 'redeadline)
  11461. "changing deadline")
  11462. ((eq org-log-note-purpose 'deldeadline)
  11463. "removing deadline")
  11464. ((eq org-log-note-purpose 'refile)
  11465. "refiling")
  11466. ((eq org-log-note-purpose 'note)
  11467. "this entry")
  11468. (t (error "This should not happen")))))
  11469. (if org-log-note-extra (insert org-log-note-extra))
  11470. (org-set-local 'org-finish-function 'org-store-log-note)
  11471. (run-hooks 'org-log-buffer-setup-hook)))
  11472. (defvar org-note-abort nil) ; dynamically scoped
  11473. (defun org-store-log-note ()
  11474. "Finish taking a log note, and insert it to where it belongs."
  11475. (let ((txt (buffer-string))
  11476. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11477. lines ind bul)
  11478. (kill-buffer (current-buffer))
  11479. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11480. (setq txt (replace-match "" t t txt)))
  11481. (if (string-match "\\s-+\\'" txt)
  11482. (setq txt (replace-match "" t t txt)))
  11483. (setq lines (org-split-string txt "\n"))
  11484. (when (and note (string-match "\\S-" note))
  11485. (setq note
  11486. (org-replace-escapes
  11487. note
  11488. (list (cons "%u" (user-login-name))
  11489. (cons "%U" user-full-name)
  11490. (cons "%t" (format-time-string
  11491. (org-time-stamp-format 'long 'inactive)
  11492. org-log-note-effective-time))
  11493. (cons "%T" (format-time-string
  11494. (org-time-stamp-format 'long nil)
  11495. org-log-note-effective-time))
  11496. (cons "%d" (format-time-string
  11497. (org-time-stamp-format nil 'inactive)
  11498. org-log-note-effective-time))
  11499. (cons "%D" (format-time-string
  11500. (org-time-stamp-format nil nil)
  11501. org-log-note-effective-time))
  11502. (cons "%s" (if org-log-note-state
  11503. (concat "\"" org-log-note-state "\"")
  11504. ""))
  11505. (cons "%S" (if org-log-note-previous-state
  11506. (concat "\"" org-log-note-previous-state "\"")
  11507. "\"\"")))))
  11508. (if lines (setq note (concat note " \\\\")))
  11509. (push note lines))
  11510. (when (or current-prefix-arg org-note-abort)
  11511. (when org-log-into-drawer
  11512. (org-remove-empty-drawer-at
  11513. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11514. org-log-note-marker))
  11515. (setq lines nil))
  11516. (when lines
  11517. (with-current-buffer (marker-buffer org-log-note-marker)
  11518. (save-excursion
  11519. (goto-char org-log-note-marker)
  11520. (move-marker org-log-note-marker nil)
  11521. (end-of-line 1)
  11522. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11523. (setq ind (save-excursion
  11524. (if (ignore-errors (goto-char (org-in-item-p)))
  11525. (let ((struct (org-list-struct)))
  11526. (org-list-get-ind
  11527. (org-list-get-top-point struct) struct))
  11528. (skip-chars-backward " \r\t\n")
  11529. (cond
  11530. ((and (org-at-heading-p)
  11531. org-adapt-indentation)
  11532. (1+ (org-current-level)))
  11533. ((org-at-heading-p) 0)
  11534. (t (org-get-indentation))))))
  11535. (setq bul (org-list-bullet-string "-"))
  11536. (org-indent-line-to ind)
  11537. (insert bul (pop lines))
  11538. (let ((ind-body (+ (length bul) ind)))
  11539. (while lines
  11540. (insert "\n")
  11541. (org-indent-line-to ind-body)
  11542. (insert (pop lines))))
  11543. (message "Note stored")
  11544. (org-back-to-heading t)
  11545. (org-cycle-hide-drawers 'children)))))
  11546. (set-window-configuration org-log-note-window-configuration)
  11547. (with-current-buffer (marker-buffer org-log-note-return-to)
  11548. (goto-char org-log-note-return-to))
  11549. (move-marker org-log-note-return-to nil)
  11550. (and org-log-post-message (message "%s" org-log-post-message)))
  11551. (defun org-remove-empty-drawer-at (drawer pos)
  11552. "Remove an empty drawer DRAWER at position POS.
  11553. POS may also be a marker."
  11554. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11555. (save-excursion
  11556. (save-restriction
  11557. (widen)
  11558. (goto-char pos)
  11559. (if (org-in-regexp
  11560. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11561. (replace-match ""))))))
  11562. (defvar org-ts-type nil)
  11563. (defun org-sparse-tree (&optional arg type)
  11564. "Create a sparse tree, prompt for the details.
  11565. This command can create sparse trees. You first need to select the type
  11566. of match used to create the tree:
  11567. t Show all TODO entries.
  11568. T Show entries with a specific TODO keyword.
  11569. m Show entries selected by a tags/property match.
  11570. p Enter a property name and its value (both with completion on existing
  11571. names/values) and show entries with that property.
  11572. r Show entries matching a regular expression (`/' can be used as well).
  11573. b Show deadlines and scheduled items before a date.
  11574. a Show deadlines and scheduled items after a date.
  11575. d Show deadlines due within `org-deadline-warning-days'.
  11576. D Show deadlines and scheduled items between a date range."
  11577. (interactive "P")
  11578. (let (ans kwd value ts-type)
  11579. (setq type (or type org-sparse-tree-default-date-type))
  11580. (setq org-ts-type type)
  11581. (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"
  11582. (cond ((eq type 'all) "all timestamps")
  11583. ((eq type 'scheduled) "only scheduled")
  11584. ((eq type 'deadline) "only deadline")
  11585. ((eq type 'active) "only active timestamps")
  11586. ((eq type 'inactive) "only inactive timestamps")
  11587. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11588. (t "scheduled/deadline")))
  11589. (setq ans (read-char-exclusive))
  11590. (cond
  11591. ((equal ans ?c)
  11592. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11593. ((equal ans ?d)
  11594. (call-interactively 'org-check-deadlines))
  11595. ((equal ans ?b)
  11596. (call-interactively 'org-check-before-date))
  11597. ((equal ans ?a)
  11598. (call-interactively 'org-check-after-date))
  11599. ((equal ans ?D)
  11600. (call-interactively 'org-check-dates-range))
  11601. ((equal ans ?t)
  11602. (call-interactively 'org-show-todo-tree))
  11603. ((equal ans ?T)
  11604. (org-show-todo-tree '(4)))
  11605. ((member ans '(?T ?m))
  11606. (call-interactively 'org-match-sparse-tree))
  11607. ((member ans '(?p ?P))
  11608. (setq kwd (org-icompleting-read "Property: "
  11609. (mapcar 'list (org-buffer-property-keys))))
  11610. (setq value (org-icompleting-read "Value: "
  11611. (mapcar 'list (org-property-values kwd))))
  11612. (unless (string-match "\\`{.*}\\'" value)
  11613. (setq value (concat "\"" value "\"")))
  11614. (org-match-sparse-tree arg (concat kwd "=" value)))
  11615. ((member ans '(?r ?R ?/))
  11616. (call-interactively 'org-occur))
  11617. (t (error "No such sparse tree command \"%c\"" ans)))))
  11618. (defvar org-occur-highlights nil
  11619. "List of overlays used for occur matches.")
  11620. (make-variable-buffer-local 'org-occur-highlights)
  11621. (defvar org-occur-parameters nil
  11622. "Parameters of the active org-occur calls.
  11623. This is a list, each call to org-occur pushes as cons cell,
  11624. containing the regular expression and the callback, onto the list.
  11625. The list can contain several entries if `org-occur' has been called
  11626. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11627. will only contain one set of parameters. When the highlights are
  11628. removed (for example with `C-c C-c', or with the next edit (depending
  11629. on `org-remove-highlights-with-change'), this variable is emptied
  11630. as well.")
  11631. (make-variable-buffer-local 'org-occur-parameters)
  11632. (defun org-occur (regexp &optional keep-previous callback)
  11633. "Make a compact tree which shows all matches of REGEXP.
  11634. The tree will show the lines where the regexp matches, and all higher
  11635. headlines above the match. It will also show the heading after the match,
  11636. to make sure editing the matching entry is easy.
  11637. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11638. call to `org-occur' will be kept, to allow stacking of calls to this
  11639. command.
  11640. If CALLBACK is non-nil, it is a function which is called to confirm
  11641. that the match should indeed be shown."
  11642. (interactive "sRegexp: \nP")
  11643. (when (equal regexp "")
  11644. (error "Regexp cannot be empty"))
  11645. (unless keep-previous
  11646. (org-remove-occur-highlights nil nil t))
  11647. (push (cons regexp callback) org-occur-parameters)
  11648. (let ((cnt 0))
  11649. (save-excursion
  11650. (goto-char (point-min))
  11651. (if (or (not keep-previous) ; do not want to keep
  11652. (not org-occur-highlights)) ; no previous matches
  11653. ;; hide everything
  11654. (org-overview))
  11655. (while (re-search-forward regexp nil t)
  11656. (when (or (not callback)
  11657. (save-match-data (funcall callback)))
  11658. (setq cnt (1+ cnt))
  11659. (when org-highlight-sparse-tree-matches
  11660. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11661. (org-show-context 'occur-tree))))
  11662. (when org-remove-highlights-with-change
  11663. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11664. nil 'local))
  11665. (unless org-sparse-tree-open-archived-trees
  11666. (org-hide-archived-subtrees (point-min) (point-max)))
  11667. (run-hooks 'org-occur-hook)
  11668. (if (org-called-interactively-p 'interactive)
  11669. (message "%d match(es) for regexp %s" cnt regexp))
  11670. cnt))
  11671. (defun org-occur-next-match (&optional n reset)
  11672. "Function for `next-error-function' to find sparse tree matches.
  11673. N is the number of matches to move, when negative move backwards.
  11674. RESET is entirely ignored - this function always goes back to the
  11675. starting point when no match is found."
  11676. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11677. (search-func (if (< n 0)
  11678. 'previous-single-char-property-change
  11679. 'next-single-char-property-change))
  11680. (n (abs n))
  11681. (pos (point))
  11682. p1)
  11683. (catch 'exit
  11684. (while (setq p1 (funcall search-func (point) 'org-type))
  11685. (when (equal p1 limit)
  11686. (goto-char pos)
  11687. (error "No more matches"))
  11688. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11689. (setq n (1- n))
  11690. (when (= n 0)
  11691. (goto-char p1)
  11692. (throw 'exit (point))))
  11693. (goto-char p1))
  11694. (goto-char p1)
  11695. (error "No more matches"))))
  11696. (defun org-show-context (&optional key)
  11697. "Make sure point and context are visible.
  11698. How much context is shown depends upon the variables
  11699. `org-show-hierarchy-above', `org-show-following-heading',
  11700. `org-show-entry-below' and `org-show-siblings'."
  11701. (let ((heading-p (org-at-heading-p t))
  11702. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11703. (following-p (org-get-alist-option org-show-following-heading key))
  11704. (entry-p (org-get-alist-option org-show-entry-below key))
  11705. (siblings-p (org-get-alist-option org-show-siblings key)))
  11706. ;; Show heading or entry text
  11707. (if (and heading-p (not entry-p))
  11708. (org-flag-heading nil) ; only show the heading
  11709. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11710. (org-show-hidden-entry))) ; show entire entry
  11711. (when following-p
  11712. ;; Show next sibling, or heading below text
  11713. (save-excursion
  11714. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11715. (org-flag-heading nil))))
  11716. (when siblings-p (org-show-siblings))
  11717. (when hierarchy-p
  11718. ;; show all higher headings, possibly with siblings
  11719. (save-excursion
  11720. (while (and (condition-case nil
  11721. (progn (org-up-heading-all 1) t)
  11722. (error nil))
  11723. (not (bobp)))
  11724. (org-flag-heading nil)
  11725. (when siblings-p (org-show-siblings)))))
  11726. (org-fix-ellipsis-at-bol)))
  11727. (defvar org-reveal-start-hook nil
  11728. "Hook run before revealing a location.")
  11729. (defun org-reveal (&optional siblings)
  11730. "Show current entry, hierarchy above it, and the following headline.
  11731. This can be used to show a consistent set of context around locations
  11732. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11733. not t for the search context.
  11734. With optional argument SIBLINGS, on each level of the hierarchy all
  11735. siblings are shown. This repairs the tree structure to what it would
  11736. look like when opened with hierarchical calls to `org-cycle'.
  11737. With double optional argument \\[universal-argument] \\[universal-argument], \
  11738. go to the parent and show the
  11739. entire tree."
  11740. (interactive "P")
  11741. (run-hooks 'org-reveal-start-hook)
  11742. (let ((org-show-hierarchy-above t)
  11743. (org-show-following-heading t)
  11744. (org-show-siblings (if siblings t org-show-siblings)))
  11745. (org-show-context nil))
  11746. (when (equal siblings '(16))
  11747. (save-excursion
  11748. (when (org-up-heading-safe)
  11749. (org-show-subtree)
  11750. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11751. (defun org-highlight-new-match (beg end)
  11752. "Highlight from BEG to END and mark the highlight is an occur headline."
  11753. (let ((ov (make-overlay beg end)))
  11754. (overlay-put ov 'face 'secondary-selection)
  11755. (overlay-put ov 'org-type 'org-occur)
  11756. (push ov org-occur-highlights)))
  11757. (defun org-remove-occur-highlights (&optional beg end noremove)
  11758. "Remove the occur highlights from the buffer.
  11759. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11760. from the `before-change-functions' in the current buffer."
  11761. (interactive)
  11762. (unless org-inhibit-highlight-removal
  11763. (mapc 'delete-overlay org-occur-highlights)
  11764. (setq org-occur-highlights nil)
  11765. (setq org-occur-parameters nil)
  11766. (unless noremove
  11767. (remove-hook 'before-change-functions
  11768. 'org-remove-occur-highlights 'local))))
  11769. ;;;; Priorities
  11770. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11771. "Regular expression matching the priority indicator.")
  11772. (defvar org-remove-priority-next-time nil)
  11773. (defun org-priority-up ()
  11774. "Increase the priority of the current item."
  11775. (interactive)
  11776. (org-priority 'up))
  11777. (defun org-priority-down ()
  11778. "Decrease the priority of the current item."
  11779. (interactive)
  11780. (org-priority 'down))
  11781. (defun org-priority (&optional action show)
  11782. "Change the priority of an item.
  11783. ACTION can be `set', `up', `down', or a character."
  11784. (interactive "P")
  11785. (if (equal action '(4))
  11786. (org-show-priority)
  11787. (unless org-enable-priority-commands
  11788. (error "Priority commands are disabled"))
  11789. (setq action (or action 'set))
  11790. (let (current new news have remove)
  11791. (save-excursion
  11792. (org-back-to-heading t)
  11793. (if (looking-at org-priority-regexp)
  11794. (setq current (string-to-char (match-string 2))
  11795. have t))
  11796. (cond
  11797. ((eq action 'remove)
  11798. (setq remove t new ?\ ))
  11799. ((or (eq action 'set)
  11800. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11801. (if (not (eq action 'set))
  11802. (setq new action)
  11803. (message "Priority %c-%c, SPC to remove: "
  11804. org-highest-priority org-lowest-priority)
  11805. (save-match-data
  11806. (setq new (read-char-exclusive))))
  11807. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11808. (= (upcase org-lowest-priority) org-lowest-priority))
  11809. (setq new (upcase new)))
  11810. (cond ((equal new ?\ ) (setq remove t))
  11811. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11812. (error "Priority must be between `%c' and `%c'"
  11813. org-highest-priority org-lowest-priority))))
  11814. ((eq action 'up)
  11815. (setq new (if have
  11816. (1- current) ; normal cycling
  11817. ;; last priority was empty
  11818. (if (eq last-command this-command)
  11819. org-lowest-priority ; wrap around empty to lowest
  11820. ;; default
  11821. (if org-priority-start-cycle-with-default
  11822. org-default-priority
  11823. (1- org-default-priority))))))
  11824. ((eq action 'down)
  11825. (setq new (if have
  11826. (1+ current) ; normal cycling
  11827. ;; last priority was empty
  11828. (if (eq last-command this-command)
  11829. org-highest-priority ; wrap around empty to highest
  11830. ;; default
  11831. (if org-priority-start-cycle-with-default
  11832. org-default-priority
  11833. (1+ org-default-priority))))))
  11834. (t (error "Invalid action")))
  11835. (if (or (< (upcase new) org-highest-priority)
  11836. (> (upcase new) org-lowest-priority))
  11837. (if (and (memq action '(up down))
  11838. (not have) (not (eq last-command this-command)))
  11839. ;; `new' is from default priority
  11840. (error
  11841. "The default can not be set, see `org-default-priority' why")
  11842. ;; normal cycling: `new' is beyond highest/lowest priority
  11843. ;; and is wrapped around to the empty priority
  11844. (setq remove t)))
  11845. (setq news (format "%c" new))
  11846. (if have
  11847. (if remove
  11848. (replace-match "" t t nil 1)
  11849. (replace-match news t t nil 2))
  11850. (if remove
  11851. (error "No priority cookie found in line")
  11852. (let ((case-fold-search nil))
  11853. (looking-at org-todo-line-regexp))
  11854. (if (match-end 2)
  11855. (progn
  11856. (goto-char (match-end 2))
  11857. (insert " [#" news "]"))
  11858. (goto-char (match-beginning 3))
  11859. (insert "[#" news "] "))))
  11860. (org-preserve-lc (org-set-tags nil 'align)))
  11861. (if remove
  11862. (message "Priority removed")
  11863. (message "Priority of current item set to %s" news)))))
  11864. (defun org-show-priority ()
  11865. "Show the priority of the current item.
  11866. This priority is composed of the main priority given with the [#A] cookies,
  11867. and by additional input from the age of a schedules or deadline entry."
  11868. (interactive)
  11869. (let ((pri (if (eq major-mode 'org-agenda-mode)
  11870. (org-get-at-bol 'priority)
  11871. (save-excursion
  11872. (save-match-data
  11873. (beginning-of-line)
  11874. (and (looking-at org-heading-regexp)
  11875. (org-get-priority (match-string 0))))))))
  11876. (message "Priority is %d" (if pri pri -1000))))
  11877. (defun org-get-priority (s)
  11878. "Find priority cookie and return priority."
  11879. (save-match-data
  11880. (if (functionp org-get-priority-function)
  11881. (funcall org-get-priority-function)
  11882. (if (not (string-match org-priority-regexp s))
  11883. (* 1000 (- org-lowest-priority org-default-priority))
  11884. (* 1000 (- org-lowest-priority
  11885. (string-to-char (match-string 2 s))))))))
  11886. ;;;; Tags
  11887. (defvar org-agenda-archives-mode)
  11888. (defvar org-map-continue-from nil
  11889. "Position from where mapping should continue.
  11890. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11891. (defvar org-scanner-tags nil
  11892. "The current tag list while the tags scanner is running.")
  11893. (defvar org-trust-scanner-tags nil
  11894. "Should `org-get-tags-at' use the tags for the scanner.
  11895. This is for internal dynamical scoping only.
  11896. When this is non-nil, the function `org-get-tags-at' will return the value
  11897. of `org-scanner-tags' instead of building the list by itself. This
  11898. can lead to large speed-ups when the tags scanner is used in a file with
  11899. many entries, and when the list of tags is retrieved, for example to
  11900. obtain a list of properties. Building the tags list for each entry in such
  11901. a file becomes an N^2 operation - but with this variable set, it scales
  11902. as N.")
  11903. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11904. "Scan headline tags with inheritance and produce output ACTION.
  11905. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11906. or `agenda' to produce an entry list for an agenda view. It can also be
  11907. a Lisp form or a function that should be called at each matched headline, in
  11908. this case the return value is a list of all return values from these calls.
  11909. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11910. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11911. only lines with a not-done TODO keyword are included in the output.
  11912. This should be the same variable that was scoped into
  11913. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11914. START-LEVEL can be a string with asterisks, reducing the scope to
  11915. headlines matching this string."
  11916. (require 'org-agenda)
  11917. (let* ((re (concat "^"
  11918. (if start-level
  11919. ;; Get the correct level to match
  11920. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11921. org-outline-regexp)
  11922. " *\\(\\<\\("
  11923. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11924. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11925. (props (list 'face 'default
  11926. 'done-face 'org-agenda-done
  11927. 'undone-face 'default
  11928. 'mouse-face 'highlight
  11929. 'org-not-done-regexp org-not-done-regexp
  11930. 'org-todo-regexp org-todo-regexp
  11931. 'org-complex-heading-regexp org-complex-heading-regexp
  11932. 'help-echo
  11933. (format "mouse-2 or RET jump to org file %s"
  11934. (abbreviate-file-name
  11935. (or (buffer-file-name (buffer-base-buffer))
  11936. (buffer-name (buffer-base-buffer)))))))
  11937. (case-fold-search nil)
  11938. (org-map-continue-from nil)
  11939. lspos tags tags-list
  11940. (tags-alist (list (cons 0 org-file-tags)))
  11941. (llast 0) rtn rtn1 level category i txt
  11942. todo marker entry priority)
  11943. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11944. (setq action (list 'lambda nil action)))
  11945. (save-excursion
  11946. (goto-char (point-min))
  11947. (when (eq action 'sparse-tree)
  11948. (org-overview)
  11949. (org-remove-occur-highlights))
  11950. (while (re-search-forward re nil t)
  11951. (setq org-map-continue-from nil)
  11952. (catch :skip
  11953. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11954. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11955. (goto-char (setq lspos (match-beginning 0)))
  11956. (setq level (org-reduced-level (funcall outline-level))
  11957. category (org-get-category))
  11958. (setq i llast llast level)
  11959. ;; remove tag lists from same and sublevels
  11960. (while (>= i level)
  11961. (when (setq entry (assoc i tags-alist))
  11962. (setq tags-alist (delete entry tags-alist)))
  11963. (setq i (1- i)))
  11964. ;; add the next tags
  11965. (when tags
  11966. (setq tags (org-split-string tags ":")
  11967. tags-alist
  11968. (cons (cons level tags) tags-alist)))
  11969. ;; compile tags for current headline
  11970. (setq tags-list
  11971. (if org-use-tag-inheritance
  11972. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11973. tags)
  11974. org-scanner-tags tags-list)
  11975. (when org-use-tag-inheritance
  11976. (setcdr (car tags-alist)
  11977. (mapcar (lambda (x)
  11978. (setq x (copy-sequence x))
  11979. (org-add-prop-inherited x))
  11980. (cdar tags-alist))))
  11981. (when (and tags org-use-tag-inheritance
  11982. (or (not (eq t org-use-tag-inheritance))
  11983. org-tags-exclude-from-inheritance))
  11984. ;; selective inheritance, remove uninherited ones
  11985. (setcdr (car tags-alist)
  11986. (org-remove-uninherited-tags (cdar tags-alist))))
  11987. (when (and
  11988. ;; eval matcher only when the todo condition is OK
  11989. (and (or (not todo-only) (member todo org-not-done-keywords))
  11990. (let ((case-fold-search t) (org-trust-scanner-tags t))
  11991. (eval matcher)))
  11992. ;; Call the skipper, but return t if it does not skip,
  11993. ;; so that the `and' form continues evaluating
  11994. (progn
  11995. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11996. t)
  11997. ;; Check if timestamps are deselecting this entry
  11998. (or (not todo-only)
  11999. (and (member todo org-not-done-keywords)
  12000. (or (not org-agenda-tags-todo-honor-ignore-options)
  12001. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12002. ;; select this headline
  12003. (cond
  12004. ((eq action 'sparse-tree)
  12005. (and org-highlight-sparse-tree-matches
  12006. (org-get-heading) (match-end 0)
  12007. (org-highlight-new-match
  12008. (match-beginning 1) (match-end 1)))
  12009. (org-show-context 'tags-tree))
  12010. ((eq action 'agenda)
  12011. (setq txt (org-agenda-format-item
  12012. ""
  12013. (concat
  12014. (if (eq org-tags-match-list-sublevels 'indented)
  12015. (make-string (1- level) ?.) "")
  12016. (org-get-heading))
  12017. category
  12018. tags-list)
  12019. priority (org-get-priority txt))
  12020. (goto-char lspos)
  12021. (setq marker (org-agenda-new-marker))
  12022. (org-add-props txt props
  12023. 'org-marker marker 'org-hd-marker marker 'org-category category
  12024. 'todo-state todo
  12025. 'priority priority 'type "tagsmatch")
  12026. (push txt rtn))
  12027. ((functionp action)
  12028. (setq org-map-continue-from nil)
  12029. (save-excursion
  12030. (setq rtn1 (funcall action))
  12031. (push rtn1 rtn)))
  12032. (t (error "Invalid action")))
  12033. ;; if we are to skip sublevels, jump to end of subtree
  12034. (unless org-tags-match-list-sublevels
  12035. (org-end-of-subtree t)
  12036. (backward-char 1))))
  12037. ;; Get the correct position from where to continue
  12038. (if org-map-continue-from
  12039. (goto-char org-map-continue-from)
  12040. (and (= (point) lspos) (end-of-line 1)))))
  12041. (when (and (eq action 'sparse-tree)
  12042. (not org-sparse-tree-open-archived-trees))
  12043. (org-hide-archived-subtrees (point-min) (point-max)))
  12044. (nreverse rtn)))
  12045. (defun org-remove-uninherited-tags (tags)
  12046. "Remove all tags that are not inherited from the list TAGS."
  12047. (cond
  12048. ((eq org-use-tag-inheritance t)
  12049. (if org-tags-exclude-from-inheritance
  12050. (org-delete-all org-tags-exclude-from-inheritance tags)
  12051. tags))
  12052. ((not org-use-tag-inheritance) nil)
  12053. ((stringp org-use-tag-inheritance)
  12054. (delq nil (mapcar
  12055. (lambda (x)
  12056. (if (and (string-match org-use-tag-inheritance x)
  12057. (not (member x org-tags-exclude-from-inheritance)))
  12058. x nil))
  12059. tags)))
  12060. ((listp org-use-tag-inheritance)
  12061. (delq nil (mapcar
  12062. (lambda (x)
  12063. (if (member x org-use-tag-inheritance) x nil))
  12064. tags)))))
  12065. (defun org-match-sparse-tree (&optional todo-only match)
  12066. "Create a sparse tree according to tags string MATCH.
  12067. MATCH can contain positive and negative selection of tags, like
  12068. \"+WORK+URGENT-WITHBOSS\".
  12069. If optional argument TODO-ONLY is non-nil, only select lines that are
  12070. also TODO lines."
  12071. (interactive "P")
  12072. (org-agenda-prepare-buffers (list (current-buffer)))
  12073. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12074. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12075. (defvar org-cached-props nil)
  12076. (defun org-cached-entry-get (pom property)
  12077. (if (or (eq t org-use-property-inheritance)
  12078. (and (stringp org-use-property-inheritance)
  12079. (string-match org-use-property-inheritance property))
  12080. (and (listp org-use-property-inheritance)
  12081. (member property org-use-property-inheritance)))
  12082. ;; Caching is not possible, check it directly
  12083. (org-entry-get pom property 'inherit)
  12084. ;; Get all properties, so that we can do complicated checks easily
  12085. (cdr (assoc property (or org-cached-props
  12086. (setq org-cached-props
  12087. (org-entry-properties pom)))))))
  12088. (defun org-global-tags-completion-table (&optional files)
  12089. "Return the list of all tags in all agenda buffer/files.
  12090. Optional FILES argument is a list of files which can be used
  12091. instead of the agenda files."
  12092. (save-excursion
  12093. (org-uniquify
  12094. (delq nil
  12095. (apply 'append
  12096. (mapcar
  12097. (lambda (file)
  12098. (set-buffer (find-file-noselect file))
  12099. (append (org-get-buffer-tags)
  12100. (mapcar (lambda (x) (if (stringp (car-safe x))
  12101. (list (car-safe x)) nil))
  12102. org-tag-alist)))
  12103. (if (and files (car files))
  12104. files
  12105. (org-agenda-files))))))))
  12106. (defun org-make-tags-matcher (match)
  12107. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12108. The variable `todo-only' is scoped dynamically into this function.
  12109. It will be set to t if the matcher restricts matching to TODO entries,
  12110. otherwise will not be touched.
  12111. Returns a cons of the selection string MATCH and the constructed
  12112. lisp form implementing the matcher. The matcher is to be evaluated
  12113. at an Org entry, with point on the headline, and returns t if the
  12114. entry matches the selection string MATCH. The returned lisp form
  12115. references two variables with information about the entry, which
  12116. must be bound around the form's evaluation: todo, the TODO keyword
  12117. at the entry (or nil of none); and tags-list, the list of all tags
  12118. at the entry including inherited ones. Additionally, the category
  12119. of the entry (if any) must be specified as the text property
  12120. 'org-category on the headline.
  12121. See also `org-scan-tags'.
  12122. "
  12123. (declare (special todo-only))
  12124. (unless (boundp 'todo-only)
  12125. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12126. (unless match
  12127. ;; Get a new match request, with completion
  12128. (let ((org-last-tags-completion-table
  12129. (org-global-tags-completion-table)))
  12130. (setq match (org-completing-read-no-i
  12131. "Match: " 'org-tags-completion-function nil nil nil
  12132. 'org-tags-history))))
  12133. ;; Parse the string and create a lisp form
  12134. (let ((match0 match)
  12135. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12136. minus tag mm
  12137. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12138. orterms term orlist re-p str-p level-p level-op time-p
  12139. prop-p pn pv po gv rest)
  12140. (if (string-match "/+" match)
  12141. ;; match contains also a todo-matching request
  12142. (progn
  12143. (setq tagsmatch (substring match 0 (match-beginning 0))
  12144. todomatch (substring match (match-end 0)))
  12145. (if (string-match "^!" todomatch)
  12146. (setq todo-only t todomatch (substring todomatch 1)))
  12147. (if (string-match "^\\s-*$" todomatch)
  12148. (setq todomatch nil)))
  12149. ;; only matching tags
  12150. (setq tagsmatch match todomatch nil))
  12151. ;; Make the tags matcher
  12152. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12153. (setq tagsmatcher t)
  12154. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12155. (while (setq term (pop orterms))
  12156. (while (and (equal (substring term -1) "\\") orterms)
  12157. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12158. (while (string-match re term)
  12159. (setq rest (substring term (match-end 0))
  12160. minus (and (match-end 1)
  12161. (equal (match-string 1 term) "-"))
  12162. tag (save-match-data (replace-regexp-in-string
  12163. "\\\\-" "-"
  12164. (match-string 2 term)))
  12165. re-p (equal (string-to-char tag) ?{)
  12166. level-p (match-end 4)
  12167. prop-p (match-end 5)
  12168. mm (cond
  12169. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12170. (level-p
  12171. (setq level-op (org-op-to-function (match-string 3 term)))
  12172. `(,level-op level ,(string-to-number
  12173. (match-string 4 term))))
  12174. (prop-p
  12175. (setq pn (match-string 5 term)
  12176. po (match-string 6 term)
  12177. pv (match-string 7 term)
  12178. re-p (equal (string-to-char pv) ?{)
  12179. str-p (equal (string-to-char pv) ?\")
  12180. time-p (save-match-data
  12181. (string-match "^\"[[<].*[]>]\"$" pv))
  12182. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12183. (if time-p (setq pv (org-matcher-time pv)))
  12184. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12185. (cond
  12186. ((equal pn "CATEGORY")
  12187. (setq gv '(get-text-property (point) 'org-category)))
  12188. ((equal pn "TODO")
  12189. (setq gv 'todo))
  12190. (t
  12191. (setq gv `(org-cached-entry-get nil ,pn))))
  12192. (if re-p
  12193. (if (eq po 'org<>)
  12194. `(not (string-match ,pv (or ,gv "")))
  12195. `(string-match ,pv (or ,gv "")))
  12196. (if str-p
  12197. `(,po (or ,gv "") ,pv)
  12198. `(,po (string-to-number (or ,gv ""))
  12199. ,(string-to-number pv) ))))
  12200. (t `(member ,tag tags-list)))
  12201. mm (if minus (list 'not mm) mm)
  12202. term rest)
  12203. (push mm tagsmatcher))
  12204. (push (if (> (length tagsmatcher) 1)
  12205. (cons 'and tagsmatcher)
  12206. (car tagsmatcher))
  12207. orlist)
  12208. (setq tagsmatcher nil))
  12209. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12210. (setq tagsmatcher
  12211. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12212. ;; Make the todo matcher
  12213. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12214. (setq todomatcher t)
  12215. (setq orterms (org-split-string todomatch "|") orlist nil)
  12216. (while (setq term (pop orterms))
  12217. (while (string-match re term)
  12218. (setq minus (and (match-end 1)
  12219. (equal (match-string 1 term) "-"))
  12220. kwd (match-string 2 term)
  12221. re-p (equal (string-to-char kwd) ?{)
  12222. term (substring term (match-end 0))
  12223. mm (if re-p
  12224. `(string-match ,(substring kwd 1 -1) todo)
  12225. (list 'equal 'todo kwd))
  12226. mm (if minus (list 'not mm) mm))
  12227. (push mm todomatcher))
  12228. (push (if (> (length todomatcher) 1)
  12229. (cons 'and todomatcher)
  12230. (car todomatcher))
  12231. orlist)
  12232. (setq todomatcher nil))
  12233. (setq todomatcher (if (> (length orlist) 1)
  12234. (cons 'or orlist) (car orlist))))
  12235. ;; Return the string and lisp forms of the matcher
  12236. (setq matcher (if todomatcher
  12237. (list 'and tagsmatcher todomatcher)
  12238. tagsmatcher))
  12239. (when todo-only
  12240. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12241. matcher)))
  12242. (cons match0 matcher)))
  12243. (defun org-op-to-function (op &optional stringp)
  12244. "Turn an operator into the appropriate function."
  12245. (setq op
  12246. (cond
  12247. ((equal op "<" ) '(< string< org-time<))
  12248. ((equal op ">" ) '(> org-string> org-time>))
  12249. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12250. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12251. ((member op '("=" "==")) '(= string= org-time=))
  12252. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12253. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12254. (defun org<> (a b) (not (= a b)))
  12255. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12256. (defun org-string>= (a b) (not (string< a b)))
  12257. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12258. (defun org-string<> (a b) (not (string= a b)))
  12259. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12260. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12261. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12262. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12263. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12264. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12265. (defun org-2ft (s)
  12266. "Convert S to a floating point time.
  12267. If S is already a number, just return it. If it is a string, parse
  12268. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12269. (cond
  12270. ((numberp s) s)
  12271. ((stringp s)
  12272. (condition-case nil
  12273. (float-time (apply 'encode-time (org-parse-time-string s)))
  12274. (error 0.)))
  12275. (t 0.)))
  12276. (defun org-time-today ()
  12277. "Time in seconds today at 0:00.
  12278. Returns the float number of seconds since the beginning of the
  12279. epoch to the beginning of today (00:00)."
  12280. (float-time (apply 'encode-time
  12281. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12282. (defun org-matcher-time (s)
  12283. "Interpret a time comparison value."
  12284. (save-match-data
  12285. (cond
  12286. ((string= s "<now>") (float-time))
  12287. ((string= s "<today>") (org-time-today))
  12288. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12289. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12290. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12291. (+ (org-time-today)
  12292. (* (string-to-number (match-string 1 s))
  12293. (cdr (assoc (match-string 2 s)
  12294. '(("d" . 86400.0) ("w" . 604800.0)
  12295. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12296. (t (org-2ft s)))))
  12297. (defun org-match-any-p (re list)
  12298. "Does re match any element of list?"
  12299. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12300. (delq nil list))
  12301. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12302. (defvar org-tags-overlay (make-overlay 1 1))
  12303. (org-detach-overlay org-tags-overlay)
  12304. (defun org-get-local-tags-at (&optional pos)
  12305. "Get a list of tags defined in the current headline."
  12306. (org-get-tags-at pos 'local))
  12307. (defun org-get-local-tags ()
  12308. "Get a list of tags defined in the current headline."
  12309. (org-get-tags-at nil 'local))
  12310. (defun org-get-tags-at (&optional pos local)
  12311. "Get a list of all headline tags applicable at POS.
  12312. POS defaults to point. If tags are inherited, the list contains
  12313. the targets in the same sequence as the headlines appear, i.e.
  12314. the tags of the current headline come last.
  12315. When LOCAL is non-nil, only return tags from the current headline,
  12316. ignore inherited ones."
  12317. (interactive)
  12318. (if (and org-trust-scanner-tags
  12319. (or (not pos) (equal pos (point)))
  12320. (not local))
  12321. org-scanner-tags
  12322. (let (tags ltags lastpos parent)
  12323. (save-excursion
  12324. (save-restriction
  12325. (widen)
  12326. (goto-char (or pos (point)))
  12327. (save-match-data
  12328. (catch 'done
  12329. (condition-case nil
  12330. (progn
  12331. (org-back-to-heading t)
  12332. (while (not (equal lastpos (point)))
  12333. (setq lastpos (point))
  12334. (when (looking-at
  12335. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12336. (setq ltags (org-split-string
  12337. (org-match-string-no-properties 1) ":"))
  12338. (when parent
  12339. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12340. (setq tags (append
  12341. (if parent
  12342. (org-remove-uninherited-tags ltags)
  12343. ltags)
  12344. tags)))
  12345. (or org-use-tag-inheritance (throw 'done t))
  12346. (if local (throw 'done t))
  12347. (or (org-up-heading-safe) (error nil))
  12348. (setq parent t)))
  12349. (error nil)))))
  12350. (if local
  12351. tags
  12352. (reverse (delete-dups
  12353. (reverse (append
  12354. (org-remove-uninherited-tags
  12355. org-file-tags) tags)))))))))
  12356. (defun org-add-prop-inherited (s)
  12357. (add-text-properties 0 (length s) '(inherited t) s)
  12358. s)
  12359. (defun org-toggle-tag (tag &optional onoff)
  12360. "Toggle the tag TAG for the current line.
  12361. If ONOFF is `on' or `off', don't toggle but set to this state."
  12362. (let (res current)
  12363. (save-excursion
  12364. (org-back-to-heading t)
  12365. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12366. (point-at-eol) t)
  12367. (progn
  12368. (setq current (match-string 1))
  12369. (replace-match ""))
  12370. (setq current ""))
  12371. (setq current (nreverse (org-split-string current ":")))
  12372. (cond
  12373. ((eq onoff 'on)
  12374. (setq res t)
  12375. (or (member tag current) (push tag current)))
  12376. ((eq onoff 'off)
  12377. (or (not (member tag current)) (setq current (delete tag current))))
  12378. (t (if (member tag current)
  12379. (setq current (delete tag current))
  12380. (setq res t)
  12381. (push tag current))))
  12382. (end-of-line 1)
  12383. (if current
  12384. (progn
  12385. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12386. (org-set-tags nil t))
  12387. (delete-horizontal-space))
  12388. (run-hooks 'org-after-tags-change-hook))
  12389. res))
  12390. (defun org-align-tags-here (to-col)
  12391. ;; Assumes that this is a headline
  12392. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12393. (beginning-of-line 1)
  12394. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12395. (< pos (match-beginning 2)))
  12396. (progn
  12397. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12398. (goto-char (match-beginning 1))
  12399. (insert " ")
  12400. (delete-region (point) (1+ (match-beginning 2)))
  12401. (setq ncol (max (current-column)
  12402. (1+ col)
  12403. (if (> to-col 0)
  12404. to-col
  12405. (- (abs to-col) tags-l))))
  12406. (setq p (point))
  12407. (insert (make-string (- ncol (current-column)) ?\ ))
  12408. (setq ncol (current-column))
  12409. (when indent-tabs-mode (tabify p (point-at-eol)))
  12410. (org-move-to-column (min ncol col) t))
  12411. (goto-char pos))))
  12412. (defun org-set-tags-command (&optional arg just-align)
  12413. "Call the set-tags command for the current entry."
  12414. (interactive "P")
  12415. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12416. (org-set-tags arg just-align)
  12417. (save-excursion
  12418. (unless (and (org-region-active-p)
  12419. org-loop-over-headlines-in-active-region)
  12420. (org-back-to-heading t))
  12421. (org-set-tags arg just-align))))
  12422. (defun org-set-tags-to (data)
  12423. "Set the tags of the current entry to DATA, replacing the current tags.
  12424. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12425. If DATA is nil or the empty string, any tags will be removed."
  12426. (interactive "sTags: ")
  12427. (setq data
  12428. (cond
  12429. ((eq data nil) "")
  12430. ((equal data "") "")
  12431. ((stringp data)
  12432. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12433. ":"))
  12434. ((listp data)
  12435. (concat ":" (mapconcat 'identity data ":") ":"))))
  12436. (when data
  12437. (save-excursion
  12438. (org-back-to-heading t)
  12439. (when (looking-at org-complex-heading-regexp)
  12440. (if (match-end 5)
  12441. (progn
  12442. (goto-char (match-beginning 5))
  12443. (insert data)
  12444. (delete-region (point) (point-at-eol))
  12445. (org-set-tags nil 'align))
  12446. (goto-char (point-at-eol))
  12447. (insert " " data)
  12448. (org-set-tags nil 'align)))
  12449. (beginning-of-line 1)
  12450. (if (looking-at ".*?\\([ \t]+\\)$")
  12451. (delete-region (match-beginning 1) (match-end 1))))))
  12452. (defun org-align-all-tags ()
  12453. "Align the tags i all headings."
  12454. (interactive)
  12455. (save-excursion
  12456. (or (ignore-errors (org-back-to-heading t))
  12457. (outline-next-heading))
  12458. (if (org-at-heading-p)
  12459. (org-set-tags t)
  12460. (message "No headings"))))
  12461. (defvar org-indent-indentation-per-level)
  12462. (defun org-set-tags (&optional arg just-align)
  12463. "Set the tags for the current headline.
  12464. With prefix ARG, realign all tags in headings in the current buffer."
  12465. (interactive "P")
  12466. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12467. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12468. 'region-start-level 'region))
  12469. org-loop-over-headlines-in-active-region)
  12470. (org-map-entries
  12471. ;; We don't use ARG and JUST-ALIGN here these args are not
  12472. ;; useful when looping over headlines
  12473. `(org-set-tags)
  12474. org-loop-over-headlines-in-active-region
  12475. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12476. (let* ((re org-outline-regexp-bol)
  12477. (current (unless arg (org-get-tags-string)))
  12478. (col (current-column))
  12479. (org-setting-tags t)
  12480. table current-tags inherited-tags ; computed below when needed
  12481. tags p0 c0 c1 rpl di tc level)
  12482. (if arg
  12483. (save-excursion
  12484. (goto-char (point-min))
  12485. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12486. (while (re-search-forward re nil t)
  12487. (org-set-tags nil t)
  12488. (end-of-line 1)))
  12489. (message "All tags realigned to column %d" org-tags-column))
  12490. (if just-align
  12491. (setq tags current)
  12492. ;; Get a new set of tags from the user
  12493. (save-excursion
  12494. (setq table (append org-tag-persistent-alist
  12495. (or org-tag-alist (org-get-buffer-tags))
  12496. (and
  12497. org-complete-tags-always-offer-all-agenda-tags
  12498. (org-global-tags-completion-table
  12499. (org-agenda-files))))
  12500. org-last-tags-completion-table table
  12501. current-tags (org-split-string current ":")
  12502. inherited-tags (nreverse
  12503. (nthcdr (length current-tags)
  12504. (nreverse (org-get-tags-at))))
  12505. tags
  12506. (if (or (eq t org-use-fast-tag-selection)
  12507. (and org-use-fast-tag-selection
  12508. (delq nil (mapcar 'cdr table))))
  12509. (org-fast-tag-selection
  12510. current-tags inherited-tags table
  12511. (if org-fast-tag-selection-include-todo
  12512. org-todo-key-alist))
  12513. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12514. (org-trim
  12515. (org-icompleting-read "Tags: "
  12516. 'org-tags-completion-function
  12517. nil nil current 'org-tags-history))))))
  12518. (while (string-match "[-+&]+" tags)
  12519. ;; No boolean logic, just a list
  12520. (setq tags (replace-match ":" t t tags))))
  12521. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12522. (if org-tags-sort-function
  12523. (setq tags (mapconcat 'identity
  12524. (sort (org-split-string
  12525. tags (org-re "[^[:alnum:]_@#%]+"))
  12526. org-tags-sort-function) ":")))
  12527. (if (string-match "\\`[\t ]*\\'" tags)
  12528. (setq tags "")
  12529. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12530. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12531. ;; Insert new tags at the correct column
  12532. (beginning-of-line 1)
  12533. (setq level (or (and (looking-at org-outline-regexp)
  12534. (- (match-end 0) (point) 1))
  12535. 1))
  12536. (cond
  12537. ((and (equal current "") (equal tags "")))
  12538. ((re-search-forward
  12539. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12540. (point-at-eol) t)
  12541. (if (equal tags "")
  12542. (setq rpl "")
  12543. (goto-char (match-beginning 0))
  12544. (setq c0 (current-column)
  12545. ;; compute offset for the case of org-indent-mode active
  12546. di (if org-indent-mode
  12547. (* (1- org-indent-indentation-per-level) (1- level))
  12548. 0)
  12549. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12550. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12551. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12552. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12553. (replace-match rpl t t)
  12554. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12555. tags)
  12556. (t (error "Tags alignment failed")))
  12557. (org-move-to-column col)
  12558. (unless just-align
  12559. (run-hooks 'org-after-tags-change-hook))))))
  12560. (defun org-change-tag-in-region (beg end tag off)
  12561. "Add or remove TAG for each entry in the region.
  12562. This works in the agenda, and also in an org-mode buffer."
  12563. (interactive
  12564. (list (region-beginning) (region-end)
  12565. (let ((org-last-tags-completion-table
  12566. (if (derived-mode-p 'org-mode)
  12567. (org-get-buffer-tags)
  12568. (org-global-tags-completion-table))))
  12569. (org-icompleting-read
  12570. "Tag: " 'org-tags-completion-function nil nil nil
  12571. 'org-tags-history))
  12572. (progn
  12573. (message "[s]et or [r]emove? ")
  12574. (equal (read-char-exclusive) ?r))))
  12575. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12576. (let ((agendap (equal major-mode 'org-agenda-mode))
  12577. l1 l2 m buf pos newhead (cnt 0))
  12578. (goto-char end)
  12579. (setq l2 (1- (org-current-line)))
  12580. (goto-char beg)
  12581. (setq l1 (org-current-line))
  12582. (loop for l from l1 to l2 do
  12583. (org-goto-line l)
  12584. (setq m (get-text-property (point) 'org-hd-marker))
  12585. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12586. (and agendap m))
  12587. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12588. pos (if agendap m (point)))
  12589. (with-current-buffer buf
  12590. (save-excursion
  12591. (save-restriction
  12592. (goto-char pos)
  12593. (setq cnt (1+ cnt))
  12594. (org-toggle-tag tag (if off 'off 'on))
  12595. (setq newhead (org-get-heading)))))
  12596. (and agendap (org-agenda-change-all-lines newhead m))))
  12597. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12598. (defun org-tags-completion-function (string predicate &optional flag)
  12599. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12600. (confirm (lambda (x) (stringp (car x)))))
  12601. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12602. (setq s1 (match-string 1 string)
  12603. s2 (match-string 2 string))
  12604. (setq s1 "" s2 string))
  12605. (cond
  12606. ((eq flag nil)
  12607. ;; try completion
  12608. (setq rtn (try-completion s2 ctable confirm))
  12609. (if (stringp rtn)
  12610. (setq rtn
  12611. (concat s1 s2 (substring rtn (length s2))
  12612. (if (and org-add-colon-after-tag-completion
  12613. (assoc rtn ctable))
  12614. ":" ""))))
  12615. rtn)
  12616. ((eq flag t)
  12617. ;; all-completions
  12618. (all-completions s2 ctable confirm)
  12619. )
  12620. ((eq flag 'lambda)
  12621. ;; exact match?
  12622. (assoc s2 ctable)))
  12623. ))
  12624. (defun org-fast-tag-insert (kwd tags face &optional end)
  12625. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12626. (insert (format "%-12s" (concat kwd ":"))
  12627. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12628. (or end "")))
  12629. (defun org-fast-tag-show-exit (flag)
  12630. (save-excursion
  12631. (org-goto-line 3)
  12632. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12633. (replace-match ""))
  12634. (when flag
  12635. (end-of-line 1)
  12636. (org-move-to-column (- (window-width) 19) t)
  12637. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12638. (defun org-set-current-tags-overlay (current prefix)
  12639. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12640. (if (featurep 'xemacs)
  12641. (org-overlay-display org-tags-overlay (concat prefix s)
  12642. 'secondary-selection)
  12643. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12644. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12645. (defvar org-last-tag-selection-key nil)
  12646. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12647. "Fast tag selection with single keys.
  12648. CURRENT is the current list of tags in the headline, INHERITED is the
  12649. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12650. possibly with grouping information. TODO-TABLE is a similar table with
  12651. TODO keywords, should these have keys assigned to them.
  12652. If the keys are nil, a-z are automatically assigned.
  12653. Returns the new tags string, or nil to not change the current settings."
  12654. (let* ((fulltable (append table todo-table))
  12655. (maxlen (apply 'max (mapcar
  12656. (lambda (x)
  12657. (if (stringp (car x)) (string-width (car x)) 0))
  12658. fulltable)))
  12659. (buf (current-buffer))
  12660. (expert (eq org-fast-tag-selection-single-key 'expert))
  12661. (buffer-tags nil)
  12662. (fwidth (+ maxlen 3 1 3))
  12663. (ncol (/ (- (window-width) 4) fwidth))
  12664. (i-face 'org-done)
  12665. (c-face 'org-todo)
  12666. tg cnt e c char c1 c2 ntable tbl rtn
  12667. ov-start ov-end ov-prefix
  12668. (exit-after-next org-fast-tag-selection-single-key)
  12669. (done-keywords org-done-keywords)
  12670. groups ingroup)
  12671. (save-excursion
  12672. (beginning-of-line 1)
  12673. (if (looking-at
  12674. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12675. (setq ov-start (match-beginning 1)
  12676. ov-end (match-end 1)
  12677. ov-prefix "")
  12678. (setq ov-start (1- (point-at-eol))
  12679. ov-end (1+ ov-start))
  12680. (skip-chars-forward "^\n\r")
  12681. (setq ov-prefix
  12682. (concat
  12683. (buffer-substring (1- (point)) (point))
  12684. (if (> (current-column) org-tags-column)
  12685. " "
  12686. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12687. (move-overlay org-tags-overlay ov-start ov-end)
  12688. (save-window-excursion
  12689. (if expert
  12690. (set-buffer (get-buffer-create " *Org tags*"))
  12691. (delete-other-windows)
  12692. (split-window-vertically)
  12693. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12694. (erase-buffer)
  12695. (org-set-local 'org-done-keywords done-keywords)
  12696. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12697. (org-fast-tag-insert "Current" current c-face "\n\n")
  12698. (org-fast-tag-show-exit exit-after-next)
  12699. (org-set-current-tags-overlay current ov-prefix)
  12700. (setq tbl fulltable char ?a cnt 0)
  12701. (while (setq e (pop tbl))
  12702. (cond
  12703. ((equal (car e) :startgroup)
  12704. (push '() groups) (setq ingroup t)
  12705. (when (not (= cnt 0))
  12706. (setq cnt 0)
  12707. (insert "\n"))
  12708. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12709. ((equal (car e) :endgroup)
  12710. (setq ingroup nil cnt 0)
  12711. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12712. ((equal e '(:newline))
  12713. (when (not (= cnt 0))
  12714. (setq cnt 0)
  12715. (insert "\n")
  12716. (setq e (car tbl))
  12717. (while (equal (car tbl) '(:newline))
  12718. (insert "\n")
  12719. (setq tbl (cdr tbl)))))
  12720. (t
  12721. (setq tg (copy-sequence (car e)) c2 nil)
  12722. (if (cdr e)
  12723. (setq c (cdr e))
  12724. ;; automatically assign a character.
  12725. (setq c1 (string-to-char
  12726. (downcase (substring
  12727. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12728. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12729. (while (or (rassoc char ntable) (rassoc char table))
  12730. (setq char (1+ char)))
  12731. (setq c2 c1))
  12732. (setq c (or c2 char)))
  12733. (if ingroup (push tg (car groups)))
  12734. (setq tg (org-add-props tg nil 'face
  12735. (cond
  12736. ((not (assoc tg table))
  12737. (org-get-todo-face tg))
  12738. ((member tg current) c-face)
  12739. ((member tg inherited) i-face))))
  12740. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12741. (insert "[" c "] " tg (make-string
  12742. (- fwidth 4 (length tg)) ?\ ))
  12743. (push (cons tg c) ntable)
  12744. (when (= (setq cnt (1+ cnt)) ncol)
  12745. (insert "\n")
  12746. (if ingroup (insert " "))
  12747. (setq cnt 0)))))
  12748. (setq ntable (nreverse ntable))
  12749. (insert "\n")
  12750. (goto-char (point-min))
  12751. (if (not expert) (org-fit-window-to-buffer))
  12752. (setq rtn
  12753. (catch 'exit
  12754. (while t
  12755. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12756. (if (not groups) "no " "")
  12757. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12758. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12759. (setq org-last-tag-selection-key c)
  12760. (cond
  12761. ((= c ?\r) (throw 'exit t))
  12762. ((= c ?!)
  12763. (setq groups (not groups))
  12764. (goto-char (point-min))
  12765. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12766. ((= c ?\C-c)
  12767. (if (not expert)
  12768. (org-fast-tag-show-exit
  12769. (setq exit-after-next (not exit-after-next)))
  12770. (setq expert nil)
  12771. (delete-other-windows)
  12772. (set-window-buffer (split-window-vertically) " *Org tags*")
  12773. (org-switch-to-buffer-other-window " *Org tags*")
  12774. (org-fit-window-to-buffer)))
  12775. ((or (= c ?\C-g)
  12776. (and (= c ?q) (not (rassoc c ntable))))
  12777. (org-detach-overlay org-tags-overlay)
  12778. (setq quit-flag t))
  12779. ((= c ?\ )
  12780. (setq current nil)
  12781. (if exit-after-next (setq exit-after-next 'now)))
  12782. ((= c ?\t)
  12783. (condition-case nil
  12784. (setq tg (org-icompleting-read
  12785. "Tag: "
  12786. (or buffer-tags
  12787. (with-current-buffer buf
  12788. (org-get-buffer-tags)))))
  12789. (quit (setq tg "")))
  12790. (when (string-match "\\S-" tg)
  12791. (add-to-list 'buffer-tags (list tg))
  12792. (if (member tg current)
  12793. (setq current (delete tg current))
  12794. (push tg current)))
  12795. (if exit-after-next (setq exit-after-next 'now)))
  12796. ((setq e (rassoc c todo-table) tg (car e))
  12797. (with-current-buffer buf
  12798. (save-excursion (org-todo tg)))
  12799. (if exit-after-next (setq exit-after-next 'now)))
  12800. ((setq e (rassoc c ntable) tg (car e))
  12801. (if (member tg current)
  12802. (setq current (delete tg current))
  12803. (loop for g in groups do
  12804. (if (member tg g)
  12805. (mapc (lambda (x)
  12806. (setq current (delete x current)))
  12807. g)))
  12808. (push tg current))
  12809. (if exit-after-next (setq exit-after-next 'now))))
  12810. ;; Create a sorted list
  12811. (setq current
  12812. (sort current
  12813. (lambda (a b)
  12814. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12815. (if (eq exit-after-next 'now) (throw 'exit t))
  12816. (goto-char (point-min))
  12817. (beginning-of-line 2)
  12818. (delete-region (point) (point-at-eol))
  12819. (org-fast-tag-insert "Current" current c-face)
  12820. (org-set-current-tags-overlay current ov-prefix)
  12821. (while (re-search-forward
  12822. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12823. (setq tg (match-string 1))
  12824. (add-text-properties
  12825. (match-beginning 1) (match-end 1)
  12826. (list 'face
  12827. (cond
  12828. ((member tg current) c-face)
  12829. ((member tg inherited) i-face)
  12830. (t (get-text-property (match-beginning 1) 'face))))))
  12831. (goto-char (point-min)))))
  12832. (org-detach-overlay org-tags-overlay)
  12833. (if rtn
  12834. (mapconcat 'identity current ":")
  12835. nil))))
  12836. (defun org-get-tags-string ()
  12837. "Get the TAGS string in the current headline."
  12838. (unless (org-at-heading-p t)
  12839. (error "Not on a heading"))
  12840. (save-excursion
  12841. (beginning-of-line 1)
  12842. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12843. (org-match-string-no-properties 1)
  12844. "")))
  12845. (defun org-get-tags ()
  12846. "Get the list of tags specified in the current headline."
  12847. (org-split-string (org-get-tags-string) ":"))
  12848. (defun org-get-buffer-tags ()
  12849. "Get a table of all tags used in the buffer, for completion."
  12850. (let (tags)
  12851. (save-excursion
  12852. (goto-char (point-min))
  12853. (while (re-search-forward
  12854. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12855. (when (equal (char-after (point-at-bol 0)) ?*)
  12856. (mapc (lambda (x) (add-to-list 'tags x))
  12857. (org-split-string (org-match-string-no-properties 1) ":")))))
  12858. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12859. (mapcar 'list tags)))
  12860. ;;;; The mapping API
  12861. (defun org-map-entries (func &optional match scope &rest skip)
  12862. "Call FUNC at each headline selected by MATCH in SCOPE.
  12863. FUNC is a function or a lisp form. The function will be called without
  12864. arguments, with the cursor positioned at the beginning of the headline.
  12865. The return values of all calls to the function will be collected and
  12866. returned as a list.
  12867. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12868. does not need to preserve point. After evaluation, the cursor will be
  12869. moved to the end of the line (presumably of the headline of the
  12870. processed entry) and search continues from there. Under some
  12871. circumstances, this may not produce the wanted results. For example,
  12872. if you have removed (e.g. archived) the current (sub)tree it could
  12873. mean that the next entry will be skipped entirely. In such cases, you
  12874. can specify the position from where search should continue by making
  12875. FUNC set the variable `org-map-continue-from' to the desired buffer
  12876. position.
  12877. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12878. Only headlines that are matched by this query will be considered during
  12879. the iteration. When MATCH is nil or t, all headlines will be
  12880. visited by the iteration.
  12881. SCOPE determines the scope of this command. It can be any of:
  12882. nil The current buffer, respecting the restriction if any
  12883. tree The subtree started with the entry at point
  12884. region The entries within the active region, if any
  12885. region-start-level
  12886. The entries within the active region, but only those at
  12887. the same level than the first one.
  12888. file The current buffer, without restriction
  12889. file-with-archives
  12890. The current buffer, and any archives associated with it
  12891. agenda All agenda files
  12892. agenda-with-archives
  12893. All agenda files with any archive files associated with them
  12894. \(file1 file2 ...)
  12895. If this is a list, all files in the list will be scanned
  12896. The remaining args are treated as settings for the skipping facilities of
  12897. the scanner. The following items can be given here:
  12898. archive skip trees with the archive tag
  12899. comment skip trees with the COMMENT keyword
  12900. function or Emacs Lisp form:
  12901. will be used as value for `org-agenda-skip-function', so
  12902. whenever the function returns a position, FUNC will not be
  12903. called for that entry and search will continue from the
  12904. position returned
  12905. If your function needs to retrieve the tags including inherited tags
  12906. at the *current* entry, you can use the value of the variable
  12907. `org-scanner-tags' which will be much faster than getting the value
  12908. with `org-get-tags-at'. If your function gets properties with
  12909. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12910. to t around the call to `org-entry-properties' to get the same speedup.
  12911. Note that if your function moves around to retrieve tags and properties at
  12912. a *different* entry, you cannot use these techniques."
  12913. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12914. (not (org-region-active-p)))
  12915. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12916. (org-agenda-skip-archived-trees (memq 'archive skip))
  12917. (org-agenda-skip-comment-trees (memq 'comment skip))
  12918. (org-agenda-skip-function
  12919. (car (org-delete-all '(comment archive) skip)))
  12920. (org-tags-match-list-sublevels t)
  12921. (start-level (eq scope 'region-start-level))
  12922. matcher file res
  12923. org-todo-keywords-for-agenda
  12924. org-done-keywords-for-agenda
  12925. org-todo-keyword-alist-for-agenda
  12926. org-drawers-for-agenda
  12927. org-tag-alist-for-agenda
  12928. todo-only)
  12929. (cond
  12930. ((eq match t) (setq matcher t))
  12931. ((eq match nil) (setq matcher t))
  12932. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12933. (save-excursion
  12934. (save-restriction
  12935. (cond ((eq scope 'tree)
  12936. (org-back-to-heading t)
  12937. (org-narrow-to-subtree)
  12938. (setq scope nil))
  12939. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12940. (org-region-active-p))
  12941. ;; If needed, set start-level to a string like "2"
  12942. (when start-level
  12943. (save-excursion
  12944. (goto-char (region-beginning))
  12945. (unless (org-at-heading-p) (outline-next-heading))
  12946. (setq start-level (org-current-level))))
  12947. (narrow-to-region (region-beginning)
  12948. (save-excursion
  12949. (goto-char (region-end))
  12950. (unless (and (bolp) (org-at-heading-p))
  12951. (outline-next-heading))
  12952. (point)))
  12953. (setq scope nil)))
  12954. (if (not scope)
  12955. (progn
  12956. (org-agenda-prepare-buffers
  12957. (list (buffer-file-name (current-buffer))))
  12958. (setq res (org-scan-tags func matcher todo-only start-level)))
  12959. ;; Get the right scope
  12960. (cond
  12961. ((and scope (listp scope) (symbolp (car scope)))
  12962. (setq scope (eval scope)))
  12963. ((eq scope 'agenda)
  12964. (setq scope (org-agenda-files t)))
  12965. ((eq scope 'agenda-with-archives)
  12966. (setq scope (org-agenda-files t))
  12967. (setq scope (org-add-archive-files scope)))
  12968. ((eq scope 'file)
  12969. (setq scope (list (buffer-file-name))))
  12970. ((eq scope 'file-with-archives)
  12971. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12972. (org-agenda-prepare-buffers scope)
  12973. (while (setq file (pop scope))
  12974. (with-current-buffer (org-find-base-buffer-visiting file)
  12975. (save-excursion
  12976. (save-restriction
  12977. (widen)
  12978. (goto-char (point-min))
  12979. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  12980. res)))
  12981. ;;;; Properties
  12982. ;;; Setting and retrieving properties
  12983. (defconst org-special-properties
  12984. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12985. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  12986. "The special properties valid in Org-mode.
  12987. These are properties that are not defined in the property drawer,
  12988. but in some other way.")
  12989. (defconst org-default-properties
  12990. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12991. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12992. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12993. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12994. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12995. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12996. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12997. "Some properties that are used by Org-mode for various purposes.
  12998. Being in this list makes sure that they are offered for completion.")
  12999. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13000. "Regular expression matching the first line of a property drawer.")
  13001. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13002. "Regular expression matching the last line of a property drawer.")
  13003. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13004. "Regular expression matching the first line of a property drawer.")
  13005. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13006. "Regular expression matching the first line of a property drawer.")
  13007. (defconst org-property-drawer-re
  13008. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13009. org-property-end-re "\\)\n?")
  13010. "Matches an entire property drawer.")
  13011. (defconst org-clock-drawer-re
  13012. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13013. org-property-end-re "\\)\n?")
  13014. "Matches an entire clock drawer.")
  13015. (defsubst org-re-property (property)
  13016. "Return a regexp matching a PROPERTY line.
  13017. Match group 1 will be set to the value."
  13018. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13019. (defsubst org-re-property-keyword (property)
  13020. "Return a regexp matching a PROPERTY line, possibly with no
  13021. value for the property."
  13022. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13023. (defun org-property-action ()
  13024. "Do an action on properties."
  13025. (interactive)
  13026. (let (c)
  13027. (org-at-property-p)
  13028. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13029. (setq c (read-char-exclusive))
  13030. (cond
  13031. ((equal c ?s)
  13032. (call-interactively 'org-set-property))
  13033. ((equal c ?d)
  13034. (call-interactively 'org-delete-property))
  13035. ((equal c ?D)
  13036. (call-interactively 'org-delete-property-globally))
  13037. ((equal c ?c)
  13038. (call-interactively 'org-compute-property-at-point))
  13039. (t (error "No such property action %c" c)))))
  13040. (defun org-inc-effort ()
  13041. "Increment the value of the effort property in the current entry."
  13042. (interactive)
  13043. (org-set-effort nil t))
  13044. (defun org-set-effort (&optional value increment)
  13045. "Set the effort property of the current entry.
  13046. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13047. 10th allowed value.
  13048. When INCREMENT is non-nil, set the property to the next allowed value."
  13049. (interactive "P")
  13050. (if (equal value 0) (setq value 10))
  13051. (let* ((completion-ignore-case t)
  13052. (prop org-effort-property)
  13053. (cur (org-entry-get nil prop))
  13054. (allowed (org-property-get-allowed-values nil prop 'table))
  13055. (existing (mapcar 'list (org-property-values prop)))
  13056. rpl
  13057. (val (cond
  13058. ((stringp value) value)
  13059. ((and allowed (integerp value))
  13060. (or (car (nth (1- value) allowed))
  13061. (car (org-last allowed))))
  13062. ((and allowed increment)
  13063. (or (caadr (member (list cur) allowed))
  13064. (error "Allowed effort values are not set")))
  13065. (allowed
  13066. (message "Select 1-9,0, [RET%s]: %s"
  13067. (if cur (concat "=" cur) "")
  13068. (mapconcat 'car allowed " "))
  13069. (setq rpl (read-char-exclusive))
  13070. (if (equal rpl ?\r)
  13071. cur
  13072. (setq rpl (- rpl ?0))
  13073. (if (equal rpl 0) (setq rpl 10))
  13074. (if (and (> rpl 0) (<= rpl (length allowed)))
  13075. (car (nth (1- rpl) allowed))
  13076. (org-completing-read "Effort: " allowed nil))))
  13077. (t
  13078. (let (org-completion-use-ido org-completion-use-iswitchb)
  13079. (org-completing-read
  13080. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13081. (concat "[" cur "]") "")
  13082. ": ")
  13083. existing nil nil "" nil cur))))))
  13084. (unless (equal (org-entry-get nil prop) val)
  13085. (org-entry-put nil prop val))
  13086. (save-excursion
  13087. (org-back-to-heading t)
  13088. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13089. (message "%s is now %s" prop val)))
  13090. (defun org-at-property-p ()
  13091. "Is cursor inside a property drawer?"
  13092. (save-excursion
  13093. (beginning-of-line 1)
  13094. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13095. (save-match-data ;; Used by calling procedures
  13096. (let ((p (point))
  13097. (range (unless (org-before-first-heading-p)
  13098. (org-get-property-block))))
  13099. (and range (<= (car range) p) (< p (cdr range))))))))
  13100. (defun org-get-property-block (&optional beg end force)
  13101. "Return the (beg . end) range of the body of the property drawer.
  13102. BEG and END are the beginning and end of the current subtree, or of
  13103. the part before the first headline. If they are not given, they will
  13104. be found. If the drawer does not exist and FORCE is non-nil, create
  13105. the drawer."
  13106. (catch 'exit
  13107. (save-excursion
  13108. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13109. (progn (org-back-to-heading t) (point))))
  13110. (end (or end (and (not (outline-next-heading)) (point-max))
  13111. (point))))
  13112. (goto-char beg)
  13113. (if (re-search-forward org-property-start-re end t)
  13114. (setq beg (1+ (match-end 0)))
  13115. (if force
  13116. (save-excursion
  13117. (org-insert-property-drawer)
  13118. (setq end (progn (outline-next-heading) (point))))
  13119. (throw 'exit nil))
  13120. (goto-char beg)
  13121. (if (re-search-forward org-property-start-re end t)
  13122. (setq beg (1+ (match-end 0)))))
  13123. (if (re-search-forward org-property-end-re end t)
  13124. (setq end (match-beginning 0))
  13125. (or force (throw 'exit nil))
  13126. (goto-char beg)
  13127. (setq end beg)
  13128. (org-indent-line)
  13129. (insert ":END:\n"))
  13130. (cons beg end)))))
  13131. (defun org-entry-properties (&optional pom which specific)
  13132. "Get all properties of the entry at point-or-marker POM.
  13133. This includes the TODO keyword, the tags, time strings for deadline,
  13134. scheduled, and clocking, and any additional properties defined in the
  13135. entry. The return value is an alist, keys may occur multiple times
  13136. if the property key was used several times.
  13137. POM may also be nil, in which case the current entry is used.
  13138. If WHICH is nil or `all', get all properties. If WHICH is
  13139. `special' or `standard', only get that subclass. If WHICH
  13140. is a string only get exactly this property. SPECIFIC can be a string, the
  13141. specific property we are interested in. Specifying it can speed
  13142. things up because then unnecessary parsing is avoided."
  13143. (setq which (or which 'all))
  13144. (org-with-point-at pom
  13145. (let ((clockstr (substring org-clock-string 0 -1))
  13146. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13147. (case-fold-search nil)
  13148. beg end range props sum-props key key1 value string clocksum clocksumt)
  13149. (save-excursion
  13150. (when (condition-case nil
  13151. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13152. (error nil))
  13153. (setq beg (point))
  13154. (setq sum-props (get-text-property (point) 'org-summaries))
  13155. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13156. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13157. (outline-next-heading)
  13158. (setq end (point))
  13159. (when (memq which '(all special))
  13160. ;; Get the special properties, like TODO and tags
  13161. (goto-char beg)
  13162. (when (and (or (not specific) (string= specific "TODO"))
  13163. (looking-at org-todo-line-regexp) (match-end 2))
  13164. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13165. (when (and (or (not specific) (string= specific "PRIORITY"))
  13166. (looking-at org-priority-regexp))
  13167. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13168. (when (or (not specific) (string= specific "FILE"))
  13169. (push (cons "FILE" buffer-file-name) props))
  13170. (when (and (or (not specific) (string= specific "TAGS"))
  13171. (setq value (org-get-tags-string))
  13172. (string-match "\\S-" value))
  13173. (push (cons "TAGS" value) props))
  13174. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13175. (setq value (org-get-tags-at)))
  13176. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13177. ":"))
  13178. props))
  13179. (when (or (not specific) (string= specific "BLOCKED"))
  13180. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13181. (when (or (not specific)
  13182. (member specific
  13183. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13184. "TIMESTAMP" "TIMESTAMP_IA")))
  13185. (catch 'match
  13186. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13187. (setq key (if (match-end 1)
  13188. (substring (org-match-string-no-properties 1)
  13189. 0 -1))
  13190. string (if (equal key clockstr)
  13191. (org-trim
  13192. (buffer-substring-no-properties
  13193. (match-beginning 3) (goto-char
  13194. (point-at-eol))))
  13195. (substring (org-match-string-no-properties 3)
  13196. 1 -1)))
  13197. ;; Get the correct property name from the key. This is
  13198. ;; necessary if the user has configured time keywords.
  13199. (setq key1 (concat key ":"))
  13200. (cond
  13201. ((not key)
  13202. (setq key
  13203. (if (= (char-after (match-beginning 3)) ?\[)
  13204. "TIMESTAMP_IA" "TIMESTAMP")))
  13205. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13206. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13207. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13208. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13209. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13210. (progn
  13211. (push (cons key string) props)
  13212. ;; no need to search further if match is found
  13213. (throw 'match t))
  13214. (when (or (equal key "CLOCK") (not (assoc key props)))
  13215. (push (cons key string) props)))))))
  13216. (when (memq which '(all standard))
  13217. ;; Get the standard properties, like :PROP: ...
  13218. (setq range (org-get-property-block beg end))
  13219. (when range
  13220. (goto-char (car range))
  13221. (while (re-search-forward
  13222. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13223. (cdr range) t)
  13224. (setq key (org-match-string-no-properties 1)
  13225. value (org-trim (or (org-match-string-no-properties 2) "")))
  13226. (unless (member key excluded)
  13227. (push (cons key (or value "")) props)))))
  13228. (if clocksum
  13229. (push (cons "CLOCKSUM"
  13230. (org-columns-number-to-string (/ (float clocksum) 60.)
  13231. 'add_times))
  13232. props))
  13233. (if clocksumt
  13234. (push (cons "CLOCKSUM_T"
  13235. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13236. 'add_times))
  13237. props))
  13238. (unless (assoc "CATEGORY" props)
  13239. (push (cons "CATEGORY" (org-get-category)) props))
  13240. (append sum-props (nreverse props)))))))
  13241. (defun org-entry-get (pom property &optional inherit literal-nil)
  13242. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13243. If INHERIT is non-nil and the entry does not have the property,
  13244. then also check higher levels of the hierarchy.
  13245. If INHERIT is the symbol `selective', use inheritance only if the setting
  13246. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13247. If the property is present but empty, the return value is the empty string.
  13248. If the property is not present at all, nil is returned.
  13249. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13250. do not interpret it as the list atom nil. This is used for inheritance
  13251. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13252. (org-with-point-at pom
  13253. (if (and inherit (if (eq inherit 'selective)
  13254. (org-property-inherit-p property)
  13255. t))
  13256. (org-entry-get-with-inheritance property literal-nil)
  13257. (if (member property org-special-properties)
  13258. ;; We need a special property. Use `org-entry-properties' to
  13259. ;; retrieve it, but specify the wanted property
  13260. (cdr (assoc property (org-entry-properties nil 'special property)))
  13261. (let ((range (org-get-property-block)))
  13262. (when (and range (not (eq (car range) (cdr range))))
  13263. (let* ((props (list (or (assoc property org-file-properties)
  13264. (assoc property org-global-properties)
  13265. (assoc property org-global-properties-fixed))))
  13266. (ap (lambda (key)
  13267. (when (re-search-forward
  13268. (org-re-property key) (cdr range) t)
  13269. (setq props
  13270. (org-update-property-plist
  13271. key
  13272. (if (match-end 1)
  13273. (org-match-string-no-properties 1) "")
  13274. props)))))
  13275. val)
  13276. (goto-char (car range))
  13277. (funcall ap property)
  13278. (goto-char (car range))
  13279. (while (funcall ap (concat property "+")))
  13280. (setq val (cdr (assoc property props)))
  13281. (when val (if literal-nil val (org-not-nil val))))))))))
  13282. (defun org-property-or-variable-value (var &optional inherit)
  13283. "Check if there is a property fixing the value of VAR.
  13284. If yes, return this value. If not, return the current value of the variable."
  13285. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13286. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13287. (read prop)
  13288. (symbol-value var))))
  13289. (defun org-entry-delete (pom property)
  13290. "Delete the property PROPERTY from entry at point-or-marker POM."
  13291. (org-with-point-at pom
  13292. (if (member property org-special-properties)
  13293. nil ; cannot delete these properties.
  13294. (let ((range (org-get-property-block)))
  13295. (if (and range
  13296. (goto-char (car range))
  13297. (re-search-forward
  13298. (org-re-property property)
  13299. (cdr range) t))
  13300. (progn
  13301. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13302. t)
  13303. nil)))))
  13304. ;; Multi-values properties are properties that contain multiple values
  13305. ;; These values are assumed to be single words, separated by whitespace.
  13306. (defun org-entry-add-to-multivalued-property (pom property value)
  13307. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13308. (let* ((old (org-entry-get pom property))
  13309. (values (and old (org-split-string old "[ \t]"))))
  13310. (setq value (org-entry-protect-space value))
  13311. (unless (member value values)
  13312. (setq values (cons value values))
  13313. (org-entry-put pom property
  13314. (mapconcat 'identity values " ")))))
  13315. (defun org-entry-remove-from-multivalued-property (pom property value)
  13316. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13317. (let* ((old (org-entry-get pom property))
  13318. (values (and old (org-split-string old "[ \t]"))))
  13319. (setq value (org-entry-protect-space value))
  13320. (when (member value values)
  13321. (setq values (delete value values))
  13322. (org-entry-put pom property
  13323. (mapconcat 'identity values " ")))))
  13324. (defun org-entry-member-in-multivalued-property (pom property value)
  13325. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13326. (let* ((old (org-entry-get pom property))
  13327. (values (and old (org-split-string old "[ \t]"))))
  13328. (setq value (org-entry-protect-space value))
  13329. (member value values)))
  13330. (defun org-entry-get-multivalued-property (pom property)
  13331. "Return a list of values in a multivalued property."
  13332. (let* ((value (org-entry-get pom property))
  13333. (values (and value (org-split-string value "[ \t]"))))
  13334. (mapcar 'org-entry-restore-space values)))
  13335. (defun org-entry-put-multivalued-property (pom property &rest values)
  13336. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13337. VALUES should be a list of strings. Spaces will be protected."
  13338. (org-entry-put pom property
  13339. (mapconcat 'org-entry-protect-space values " "))
  13340. (let* ((value (org-entry-get pom property))
  13341. (values (and value (org-split-string value "[ \t]"))))
  13342. (mapcar 'org-entry-restore-space values)))
  13343. (defun org-entry-protect-space (s)
  13344. "Protect spaces and newline in string S."
  13345. (while (string-match " " s)
  13346. (setq s (replace-match "%20" t t s)))
  13347. (while (string-match "\n" s)
  13348. (setq s (replace-match "%0A" t t s)))
  13349. s)
  13350. (defun org-entry-restore-space (s)
  13351. "Restore spaces and newline in string S."
  13352. (while (string-match "%20" s)
  13353. (setq s (replace-match " " t t s)))
  13354. (while (string-match "%0A" s)
  13355. (setq s (replace-match "\n" t t s)))
  13356. s)
  13357. (defvar org-entry-property-inherited-from (make-marker)
  13358. "Marker pointing to the entry from where a property was inherited.
  13359. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13360. location of the entry where the inheritance search matched. If there was
  13361. no match, the marker will point nowhere.
  13362. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13363. is set.")
  13364. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13365. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13366. The search will stop at the first ancestor which has the property defined.
  13367. If the value found is \"nil\", return nil to show that the property
  13368. should be considered as undefined (this is the meaning of nil here).
  13369. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13370. (move-marker org-entry-property-inherited-from nil)
  13371. (let (tmp)
  13372. (save-excursion
  13373. (save-restriction
  13374. (widen)
  13375. (catch 'ex
  13376. (while t
  13377. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13378. (or (ignore-errors (org-back-to-heading t))
  13379. (goto-char (point-min)))
  13380. (move-marker org-entry-property-inherited-from (point))
  13381. (throw 'ex tmp))
  13382. (or (ignore-errors (org-up-heading-safe))
  13383. (throw 'ex nil))))))
  13384. (setq tmp (or tmp
  13385. (cdr (assoc property org-file-properties))
  13386. (cdr (assoc property org-global-properties))
  13387. (cdr (assoc property org-global-properties-fixed))))
  13388. (if literal-nil tmp (org-not-nil tmp))))
  13389. (defvar org-property-changed-functions nil
  13390. "Hook called when the value of a property has changed.
  13391. Each hook function should accept two arguments, the name of the property
  13392. and the new value.")
  13393. (defun org-entry-put (pom property value)
  13394. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13395. (org-with-point-at pom
  13396. (org-back-to-heading t)
  13397. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13398. range)
  13399. (cond
  13400. ((equal property "TODO")
  13401. (when (and (stringp value) (string-match "\\S-" value)
  13402. (not (member value org-todo-keywords-1)))
  13403. (error "\"%s\" is not a valid TODO state" value))
  13404. (if (or (not value)
  13405. (not (string-match "\\S-" value)))
  13406. (setq value 'none))
  13407. (org-todo value)
  13408. (org-set-tags nil 'align))
  13409. ((equal property "PRIORITY")
  13410. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13411. (string-to-char value) ?\ ))
  13412. (org-set-tags nil 'align))
  13413. ((equal property "SCHEDULED")
  13414. (if (re-search-forward org-scheduled-time-regexp end t)
  13415. (cond
  13416. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13417. ((eq value 'later) (org-timestamp-change 1 'day))
  13418. (t (call-interactively 'org-schedule)))
  13419. (call-interactively 'org-schedule)))
  13420. ((equal property "DEADLINE")
  13421. (if (re-search-forward org-deadline-time-regexp end t)
  13422. (cond
  13423. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13424. ((eq value 'later) (org-timestamp-change 1 'day))
  13425. (t (call-interactively 'org-deadline)))
  13426. (call-interactively 'org-deadline)))
  13427. ((member property org-special-properties)
  13428. (error "The %s property can not yet be set with `org-entry-put'"
  13429. property))
  13430. (t ; a non-special property
  13431. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13432. (setq range (org-get-property-block beg end 'force))
  13433. (goto-char (car range))
  13434. (if (re-search-forward
  13435. (org-re-property-keyword property) (cdr range) t)
  13436. (progn
  13437. (delete-region (match-beginning 0) (match-end 0))
  13438. (goto-char (match-beginning 0)))
  13439. (goto-char (cdr range))
  13440. (insert "\n")
  13441. (backward-char 1)
  13442. (org-indent-line))
  13443. (insert ":" property ":")
  13444. (and value (insert " " value))
  13445. (org-indent-line)))))
  13446. (run-hook-with-args 'org-property-changed-functions property value)))
  13447. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13448. "Get all property keys in the current buffer.
  13449. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13450. like tags and TODO state.
  13451. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13452. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13453. and others.
  13454. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13455. formats in the current buffer."
  13456. (let (rtn range cfmt s p)
  13457. (save-excursion
  13458. (save-restriction
  13459. (widen)
  13460. (goto-char (point-min))
  13461. (while (re-search-forward org-property-start-re nil t)
  13462. (setq range (org-get-property-block))
  13463. (goto-char (car range))
  13464. (while (re-search-forward
  13465. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13466. (cdr range) t)
  13467. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13468. (outline-next-heading))))
  13469. (when include-specials
  13470. (setq rtn (append org-special-properties rtn)))
  13471. (when include-defaults
  13472. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13473. (add-to-list 'rtn org-effort-property))
  13474. (when include-columns
  13475. (save-excursion
  13476. (save-restriction
  13477. (widen)
  13478. (goto-char (point-min))
  13479. (while (re-search-forward
  13480. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13481. nil t)
  13482. (setq cfmt (match-string 2) s 0)
  13483. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13484. cfmt s)
  13485. (setq s (match-end 0)
  13486. p (match-string 1 cfmt))
  13487. (unless (or (equal p "ITEM")
  13488. (member p org-special-properties))
  13489. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13490. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13491. (defun org-property-values (key)
  13492. "Return a list of all values of property KEY in the current buffer."
  13493. (save-excursion
  13494. (save-restriction
  13495. (widen)
  13496. (goto-char (point-min))
  13497. (let ((re (org-re-property key))
  13498. values)
  13499. (while (re-search-forward re nil t)
  13500. (add-to-list 'values (org-trim (match-string 1))))
  13501. (delete "" values)))))
  13502. (defun org-insert-property-drawer ()
  13503. "Insert a property drawer into the current entry."
  13504. (org-back-to-heading t)
  13505. (looking-at org-outline-regexp)
  13506. (let ((indent (if org-adapt-indentation
  13507. (- (match-end 0) (match-beginning 0))
  13508. 0))
  13509. (beg (point))
  13510. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13511. end hiddenp)
  13512. (outline-next-heading)
  13513. (setq end (point))
  13514. (goto-char beg)
  13515. (while (re-search-forward re end t))
  13516. (setq hiddenp (outline-invisible-p))
  13517. (end-of-line 1)
  13518. (and (equal (char-after) ?\n) (forward-char 1))
  13519. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13520. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13521. ;; just skip this line
  13522. (beginning-of-line 2)
  13523. ;; Drawer start, find the end
  13524. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13525. (beginning-of-line 1)))
  13526. (org-skip-over-state-notes)
  13527. (skip-chars-backward " \t\n\r")
  13528. (if (eq (char-before) ?*) (forward-char 1))
  13529. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13530. (beginning-of-line 0)
  13531. (org-indent-to-column indent)
  13532. (beginning-of-line 2)
  13533. (org-indent-to-column indent)
  13534. (beginning-of-line 0)
  13535. (if hiddenp
  13536. (save-excursion
  13537. (org-back-to-heading t)
  13538. (hide-entry))
  13539. (org-flag-drawer t))))
  13540. (defun org-insert-drawer (&optional arg drawer)
  13541. "Insert a drawer at point.
  13542. Optional argument DRAWER, when non-nil, is a string representing
  13543. drawer's name. Otherwise, the user is prompted for a name.
  13544. If a region is active, insert the drawer around that region
  13545. instead.
  13546. Point is left between drawer's boundaries."
  13547. (interactive "P")
  13548. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13549. "LOGBOOK"))
  13550. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13551. ;; internally by Org. They are meant to be inserted
  13552. ;; automatically.
  13553. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13554. ;; Remove system drawers from list. Note: For some reason,
  13555. ;; `org-completing-read' ignores the predicate while
  13556. ;; `completing-read' handles it fine.
  13557. (drawer (if arg "PROPERTIES"
  13558. (or drawer
  13559. (completing-read
  13560. "Drawer: " org-drawers
  13561. (lambda (d) (not (member d system-drawers))))))))
  13562. (cond
  13563. ;; With C-u, fall back on `org-insert-property-drawer'
  13564. (arg (org-insert-property-drawer))
  13565. ;; With an active region, insert a drawer at point.
  13566. ((not (org-region-active-p))
  13567. (progn
  13568. (unless (bolp) (insert "\n"))
  13569. (insert (format ":%s:\n\n:END:\n" drawer))
  13570. (forward-line -2)))
  13571. ;; Otherwise, insert the drawer at point
  13572. (t
  13573. (let ((rbeg (region-beginning))
  13574. (rend (copy-marker (region-end))))
  13575. (unwind-protect
  13576. (progn
  13577. (goto-char rbeg)
  13578. (beginning-of-line)
  13579. (when (save-excursion
  13580. (re-search-forward org-outline-regexp-bol rend t))
  13581. (error "Drawers cannot contain headlines"))
  13582. ;; Position point at the beginning of the first
  13583. ;; non-blank line in region. Insert drawer's opening
  13584. ;; there, then indent it.
  13585. (org-skip-whitespace)
  13586. (beginning-of-line)
  13587. (insert ":" drawer ":\n")
  13588. (forward-line -1)
  13589. (indent-for-tab-command)
  13590. ;; Move point to the beginning of the first blank line
  13591. ;; after the last non-blank line in region. Insert
  13592. ;; drawer's closing, then indent it.
  13593. (goto-char rend)
  13594. (skip-chars-backward " \r\t\n")
  13595. (insert "\n:END:")
  13596. (deactivate-mark t)
  13597. (indent-for-tab-command)
  13598. (unless (eolp) (insert "\n")))
  13599. ;; Clear marker, whatever the outcome of insertion is.
  13600. (set-marker rend nil)))))))
  13601. (defvar org-property-set-functions-alist nil
  13602. "Property set function alist.
  13603. Each entry should have the following format:
  13604. (PROPERTY . READ-FUNCTION)
  13605. The read function will be called with the same argument as
  13606. `org-completing-read'.")
  13607. (defun org-set-property-function (property)
  13608. "Get the function that should be used to set PROPERTY.
  13609. This is computed according to `org-property-set-functions-alist'."
  13610. (or (cdr (assoc property org-property-set-functions-alist))
  13611. 'org-completing-read))
  13612. (defun org-read-property-value (property)
  13613. "Read PROPERTY value from user."
  13614. (let* ((completion-ignore-case t)
  13615. (allowed (org-property-get-allowed-values nil property 'table))
  13616. (cur (org-entry-get nil property))
  13617. (prompt (concat property " value"
  13618. (if (and cur (string-match "\\S-" cur))
  13619. (concat " [" cur "]") "") ": "))
  13620. (set-function (org-set-property-function property))
  13621. (val (if allowed
  13622. (funcall set-function prompt allowed nil
  13623. (not (get-text-property 0 'org-unrestricted
  13624. (caar allowed))))
  13625. (let (org-completion-use-ido org-completion-use-iswitchb)
  13626. (funcall set-function prompt
  13627. (mapcar 'list (org-property-values property))
  13628. nil nil "" nil cur)))))
  13629. (if (equal val "")
  13630. cur
  13631. val)))
  13632. (defvar org-last-set-property nil)
  13633. (defun org-read-property-name ()
  13634. "Read a property name."
  13635. (let* ((completion-ignore-case t)
  13636. (keys (org-buffer-property-keys nil t t))
  13637. (default-prop (or (save-excursion
  13638. (save-match-data
  13639. (beginning-of-line)
  13640. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13641. (null (string= (match-string 1) "END"))
  13642. (match-string 1))))
  13643. org-last-set-property))
  13644. (property (org-icompleting-read
  13645. (concat "Property"
  13646. (if default-prop (concat " [" default-prop "]") "")
  13647. ": ")
  13648. (mapcar 'list keys)
  13649. nil nil nil nil
  13650. default-prop
  13651. )))
  13652. (if (member property keys)
  13653. property
  13654. (or (cdr (assoc (downcase property)
  13655. (mapcar (lambda (x) (cons (downcase x) x))
  13656. keys)))
  13657. property))))
  13658. (defun org-set-property (property value)
  13659. "In the current entry, set PROPERTY to VALUE.
  13660. When called interactively, this will prompt for a property name, offering
  13661. completion on existing and default properties. And then it will prompt
  13662. for a value, offering completion either on allowed values (via an inherited
  13663. xxx_ALL property) or on existing values in other instances of this property
  13664. in the current file."
  13665. (interactive (list nil nil))
  13666. (let* ((property (or property (org-read-property-name)))
  13667. (value (or value (org-read-property-value property)))
  13668. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13669. (setq org-last-set-property property)
  13670. ;; Possibly postprocess the inserted value:
  13671. (when fn (setq value (funcall fn value)))
  13672. (unless (equal (org-entry-get nil property) value)
  13673. (org-entry-put nil property value))))
  13674. (defun org-delete-property (property)
  13675. "In the current entry, delete PROPERTY."
  13676. (interactive
  13677. (let* ((completion-ignore-case t)
  13678. (prop (org-icompleting-read "Property: "
  13679. (org-entry-properties nil 'standard))))
  13680. (list prop)))
  13681. (message "Property %s %s" property
  13682. (if (org-entry-delete nil property)
  13683. "deleted"
  13684. "was not present in the entry")))
  13685. (defun org-delete-property-globally (property)
  13686. "Remove PROPERTY globally, from all entries."
  13687. (interactive
  13688. (let* ((completion-ignore-case t)
  13689. (prop (org-icompleting-read
  13690. "Globally remove property: "
  13691. (mapcar 'list (org-buffer-property-keys)))))
  13692. (list prop)))
  13693. (save-excursion
  13694. (save-restriction
  13695. (widen)
  13696. (goto-char (point-min))
  13697. (let ((cnt 0))
  13698. (while (re-search-forward
  13699. (org-re-property property)
  13700. nil t)
  13701. (setq cnt (1+ cnt))
  13702. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13703. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13704. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13705. (defun org-compute-property-at-point ()
  13706. "Compute the property at point.
  13707. This looks for an enclosing column format, extracts the operator and
  13708. then applies it to the property in the column format's scope."
  13709. (interactive)
  13710. (unless (org-at-property-p)
  13711. (error "Not at a property"))
  13712. (let ((prop (org-match-string-no-properties 2)))
  13713. (org-columns-get-format-and-top-level)
  13714. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13715. (error "No operator defined for property %s" prop))
  13716. (org-columns-compute prop)))
  13717. (defvar org-property-allowed-value-functions nil
  13718. "Hook for functions supplying allowed values for a specific property.
  13719. The functions must take a single argument, the name of the property, and
  13720. return a flat list of allowed values. If \":ETC\" is one of
  13721. the values, this means that these values are intended as defaults for
  13722. completion, but that other values should be allowed too.
  13723. The functions must return nil if they are not responsible for this
  13724. property.")
  13725. (defun org-property-get-allowed-values (pom property &optional table)
  13726. "Get allowed values for the property PROPERTY.
  13727. When TABLE is non-nil, return an alist that can directly be used for
  13728. completion."
  13729. (let (vals)
  13730. (cond
  13731. ((equal property "TODO")
  13732. (setq vals (org-with-point-at pom
  13733. (append org-todo-keywords-1 '("")))))
  13734. ((equal property "PRIORITY")
  13735. (let ((n org-lowest-priority))
  13736. (while (>= n org-highest-priority)
  13737. (push (char-to-string n) vals)
  13738. (setq n (1- n)))))
  13739. ((member property org-special-properties))
  13740. ((setq vals (run-hook-with-args-until-success
  13741. 'org-property-allowed-value-functions property)))
  13742. (t
  13743. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13744. (when (and vals (string-match "\\S-" vals))
  13745. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13746. (setq vals (mapcar (lambda (x)
  13747. (cond ((stringp x) x)
  13748. ((numberp x) (number-to-string x))
  13749. ((symbolp x) (symbol-name x))
  13750. (t "???")))
  13751. vals)))))
  13752. (when (member ":ETC" vals)
  13753. (setq vals (remove ":ETC" vals))
  13754. (org-add-props (car vals) '(org-unrestricted t)))
  13755. (if table (mapcar 'list vals) vals)))
  13756. (defun org-property-previous-allowed-value (&optional previous)
  13757. "Switch to the next allowed value for this property."
  13758. (interactive)
  13759. (org-property-next-allowed-value t))
  13760. (defun org-property-next-allowed-value (&optional previous)
  13761. "Switch to the next allowed value for this property."
  13762. (interactive)
  13763. (unless (org-at-property-p)
  13764. (error "Not at a property"))
  13765. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  13766. (key (match-string 2))
  13767. (value (match-string 3))
  13768. (allowed (or (org-property-get-allowed-values (point) key)
  13769. (and (member value '("[ ]" "[-]" "[X]"))
  13770. '("[ ]" "[X]"))))
  13771. nval)
  13772. (unless allowed
  13773. (error "Allowed values for this property have not been defined"))
  13774. (if previous (setq allowed (reverse allowed)))
  13775. (if (member value allowed)
  13776. (setq nval (car (cdr (member value allowed)))))
  13777. (setq nval (or nval (car allowed)))
  13778. (if (equal nval value)
  13779. (error "Only one allowed value for this property"))
  13780. (org-at-property-p)
  13781. (replace-match (concat " :" key ": " nval) t t)
  13782. (org-indent-line)
  13783. (beginning-of-line 1)
  13784. (skip-chars-forward " \t")
  13785. (when (equal prop org-effort-property)
  13786. (save-excursion
  13787. (org-back-to-heading t)
  13788. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval)))
  13789. (run-hook-with-args 'org-property-changed-functions key nval)))
  13790. (defun org-find-olp (path &optional this-buffer)
  13791. "Return a marker pointing to the entry at outline path OLP.
  13792. If anything goes wrong, throw an error.
  13793. You can wrap this call to catch the error like this:
  13794. (condition-case msg
  13795. (org-mobile-locate-entry (match-string 4))
  13796. (error (nth 1 msg)))
  13797. The return value will then be either a string with the error message,
  13798. or a marker if everything is OK.
  13799. If THIS-BUFFER is set, the outline path does not contain a file,
  13800. only headings."
  13801. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13802. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13803. (level 1)
  13804. (lmin 1)
  13805. (lmax 1)
  13806. limit re end found pos heading cnt flevel)
  13807. (unless buffer (error "File not found :%s" file))
  13808. (with-current-buffer buffer
  13809. (save-excursion
  13810. (save-restriction
  13811. (widen)
  13812. (setq limit (point-max))
  13813. (goto-char (point-min))
  13814. (while (setq heading (pop path))
  13815. (setq re (format org-complex-heading-regexp-format
  13816. (regexp-quote heading)))
  13817. (setq cnt 0 pos (point))
  13818. (while (re-search-forward re end t)
  13819. (setq level (- (match-end 1) (match-beginning 1)))
  13820. (if (and (>= level lmin) (<= level lmax))
  13821. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13822. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13823. lmax heading))
  13824. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13825. lmax heading))
  13826. (goto-char found)
  13827. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13828. (setq end (save-excursion (org-end-of-subtree t t))))
  13829. (when (org-at-heading-p)
  13830. (point-marker)))))))
  13831. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13832. "Find node HEADING in BUFFER.
  13833. Return a marker to the heading if it was found, or nil if not.
  13834. If POS-ONLY is set, return just the position instead of a marker.
  13835. The heading text must match exact, but it may have a TODO keyword,
  13836. a priority cookie and tags in the standard locations."
  13837. (with-current-buffer (or buffer (current-buffer))
  13838. (save-excursion
  13839. (save-restriction
  13840. (widen)
  13841. (goto-char (point-min))
  13842. (let (case-fold-search)
  13843. (if (re-search-forward
  13844. (format org-complex-heading-regexp-format
  13845. (regexp-quote heading)) nil t)
  13846. (if pos-only
  13847. (match-beginning 0)
  13848. (move-marker (make-marker) (match-beginning 0)))))))))
  13849. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13850. "Find Org node headline HEADING in all .org files in directory DIR.
  13851. When the target headline is found, return a marker to this location."
  13852. (let ((files (directory-files (or dir default-directory)
  13853. nil "\\`[^.#].*\\.org\\'"))
  13854. file visiting m buffer)
  13855. (catch 'found
  13856. (while (setq file (pop files))
  13857. (message "trying %s" file)
  13858. (setq visiting (org-find-base-buffer-visiting file))
  13859. (setq buffer (or visiting (find-file-noselect file)))
  13860. (setq m (org-find-exact-headline-in-buffer
  13861. heading buffer))
  13862. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13863. (and m (throw 'found m))))))
  13864. (defun org-find-entry-with-id (ident)
  13865. "Locate the entry that contains the ID property with exact value IDENT.
  13866. IDENT can be a string, a symbol or a number, this function will search for
  13867. the string representation of it.
  13868. Return the position where this entry starts, or nil if there is no such entry."
  13869. (interactive "sID: ")
  13870. (let ((id (cond
  13871. ((stringp ident) ident)
  13872. ((symbol-name ident) (symbol-name ident))
  13873. ((numberp ident) (number-to-string ident))
  13874. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13875. (case-fold-search nil))
  13876. (save-excursion
  13877. (save-restriction
  13878. (widen)
  13879. (goto-char (point-min))
  13880. (when (re-search-forward
  13881. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13882. nil t)
  13883. (org-back-to-heading t)
  13884. (point))))))
  13885. ;;;; Timestamps
  13886. (defvar org-last-changed-timestamp nil)
  13887. (defvar org-last-inserted-timestamp nil
  13888. "The last time stamp inserted with `org-insert-time-stamp'.")
  13889. (defvar org-time-was-given) ; dynamically scoped parameter
  13890. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13891. (defvar org-ts-what) ; dynamically scoped parameter
  13892. (defun org-time-stamp (arg &optional inactive)
  13893. "Prompt for a date/time and insert a time stamp.
  13894. If the user specifies a time like HH:MM or if this command is
  13895. called with at least one prefix argument, the time stamp contains
  13896. the date and the time. Otherwise, only the date is be included.
  13897. All parts of a date not specified by the user is filled in from
  13898. the current date/time. So if you just press return without
  13899. typing anything, the time stamp will represent the current
  13900. date/time.
  13901. If there is already a timestamp at the cursor, it will be
  13902. modified.
  13903. With two universal prefix arguments, insert an active timestamp
  13904. with the current time without prompting the user."
  13905. (interactive "P")
  13906. (let* ((ts nil)
  13907. (default-time
  13908. ;; Default time is either today, or, when entering a range,
  13909. ;; the range start.
  13910. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13911. (save-excursion
  13912. (re-search-backward
  13913. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13914. (- (point) 20) t)))
  13915. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13916. (current-time)))
  13917. (default-input (and ts (org-get-compact-tod ts)))
  13918. (repeater (save-excursion
  13919. (save-match-data
  13920. (beginning-of-line)
  13921. (when (re-search-forward
  13922. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  13923. (save-excursion (progn (end-of-line) (point))) t)
  13924. (match-string 0)))))
  13925. org-time-was-given org-end-time-was-given time)
  13926. (cond
  13927. ((and (org-at-timestamp-p t)
  13928. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13929. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13930. (insert "--")
  13931. (setq time (let ((this-command this-command))
  13932. (org-read-date arg 'totime nil nil
  13933. default-time default-input inactive)))
  13934. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13935. ((org-at-timestamp-p t)
  13936. (setq time (let ((this-command this-command))
  13937. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13938. (when (org-at-timestamp-p t) ; just to get the match data
  13939. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13940. (replace-match "")
  13941. (setq org-last-changed-timestamp
  13942. (org-insert-time-stamp
  13943. time (or org-time-was-given arg)
  13944. inactive nil nil (list org-end-time-was-given)))
  13945. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13946. (setq org-last-changed-timestamp
  13947. (concat (substring org-last-inserted-timestamp 0 -1)
  13948. " " repeater ">"))))
  13949. (message "Timestamp updated"))
  13950. ((equal arg '(16))
  13951. (org-insert-time-stamp (current-time) t))
  13952. (t
  13953. (setq time (let ((this-command this-command))
  13954. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13955. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13956. nil nil (list org-end-time-was-given))))))
  13957. ;; FIXME: can we use this for something else, like computing time differences?
  13958. (defun org-get-compact-tod (s)
  13959. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13960. (let* ((t1 (match-string 1 s))
  13961. (h1 (string-to-number (match-string 2 s)))
  13962. (m1 (string-to-number (match-string 3 s)))
  13963. (t2 (and (match-end 4) (match-string 5 s)))
  13964. (h2 (and t2 (string-to-number (match-string 6 s))))
  13965. (m2 (and t2 (string-to-number (match-string 7 s))))
  13966. dh dm)
  13967. (if (not t2)
  13968. t1
  13969. (setq dh (- h2 h1) dm (- m2 m1))
  13970. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13971. (concat t1 "+" (number-to-string dh)
  13972. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13973. (defun org-time-stamp-inactive (&optional arg)
  13974. "Insert an inactive time stamp.
  13975. An inactive time stamp is enclosed in square brackets instead of angle
  13976. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13977. does not link to the calendar and cannot be changed with the S-cursor keys.
  13978. So these are more for recording a certain time/date."
  13979. (interactive "P")
  13980. (org-time-stamp arg 'inactive))
  13981. (defvar org-date-ovl (make-overlay 1 1))
  13982. (overlay-put org-date-ovl 'face 'org-date-selected)
  13983. (org-detach-overlay org-date-ovl)
  13984. (defvar org-ans1) ; dynamically scoped parameter
  13985. (defvar org-ans2) ; dynamically scoped parameter
  13986. (defvar org-plain-time-of-day-regexp) ; defined below
  13987. (defvar org-overriding-default-time nil) ; dynamically scoped
  13988. (defvar org-read-date-overlay nil)
  13989. (defvar org-dcst nil) ; dynamically scoped
  13990. (defvar org-read-date-history nil)
  13991. (defvar org-read-date-final-answer nil)
  13992. (defvar org-read-date-analyze-futurep nil)
  13993. (defvar org-read-date-analyze-forced-year nil)
  13994. (defvar org-read-date-inactive)
  13995. (defun org-read-date (&optional org-with-time to-time from-string prompt
  13996. default-time default-input inactive)
  13997. "Read a date, possibly a time, and make things smooth for the user.
  13998. The prompt will suggest to enter an ISO date, but you can also enter anything
  13999. which will at least partially be understood by `parse-time-string'.
  14000. Unrecognized parts of the date will default to the current day, month, year,
  14001. hour and minute. If this command is called to replace a timestamp at point,
  14002. or to enter the second timestamp of a range, the default time is taken
  14003. from the existing stamp. Furthermore, the command prefers the future,
  14004. so if you are giving a date where the year is not given, and the day-month
  14005. combination is already past in the current year, it will assume you
  14006. mean next year. For details, see the manual. A few examples:
  14007. 3-2-5 --> 2003-02-05
  14008. feb 15 --> currentyear-02-15
  14009. 2/15 --> currentyear-02-15
  14010. sep 12 9 --> 2009-09-12
  14011. 12:45 --> today 12:45
  14012. 22 sept 0:34 --> currentyear-09-22 0:34
  14013. 12 --> currentyear-currentmonth-12
  14014. Fri --> nearest Friday (today or later)
  14015. etc.
  14016. Furthermore you can specify a relative date by giving, as the *first* thing
  14017. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14018. change in days weeks, months, years.
  14019. With a single plus or minus, the date is relative to today. With a double
  14020. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14021. +4d --> four days from today
  14022. +4 --> same as above
  14023. +2w --> two weeks from today
  14024. ++5 --> five days from default date
  14025. The function understands only English month and weekday abbreviations.
  14026. While prompting, a calendar is popped up - you can also select the
  14027. date with the mouse (button 1). The calendar shows a period of three
  14028. months. To scroll it to other months, use the keys `>' and `<'.
  14029. If you don't like the calendar, turn it off with
  14030. \(setq org-read-date-popup-calendar nil)
  14031. With optional argument TO-TIME, the date will immediately be converted
  14032. to an internal time.
  14033. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14034. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14035. still enter a time, and this function will inform the calling routine
  14036. about this change. The calling routine may then choose to change the
  14037. format used to insert the time stamp into the buffer to include the time.
  14038. With optional argument FROM-STRING, read from this string instead from
  14039. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14040. the time/date that is used for everything that is not specified by the
  14041. user."
  14042. (require 'parse-time)
  14043. (let* ((org-time-stamp-rounding-minutes
  14044. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14045. (org-dcst org-display-custom-times)
  14046. (ct (org-current-time))
  14047. (org-def (or org-overriding-default-time default-time ct))
  14048. (org-defdecode (decode-time org-def))
  14049. (dummy (progn
  14050. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14051. (setcar (nthcdr 2 org-defdecode) -1)
  14052. (setcar (nthcdr 1 org-defdecode) 59)
  14053. (setq org-def (apply 'encode-time org-defdecode)
  14054. org-defdecode (decode-time org-def)))))
  14055. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14056. (calendar-frame-setup nil)
  14057. (calendar-setup nil)
  14058. (calendar-move-hook nil)
  14059. (calendar-view-diary-initially-flag nil)
  14060. (calendar-view-holidays-initially-flag nil)
  14061. (timestr (format-time-string
  14062. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14063. (prompt (concat (if prompt (concat prompt " ") "")
  14064. (format "Date+time [%s]: " timestr)))
  14065. ans (org-ans0 "") org-ans1 org-ans2 final)
  14066. (cond
  14067. (from-string (setq ans from-string))
  14068. (org-read-date-popup-calendar
  14069. (save-excursion
  14070. (save-window-excursion
  14071. (calendar)
  14072. (org-eval-in-calendar '(setq cursor-type nil) t)
  14073. (unwind-protect
  14074. (progn
  14075. (calendar-forward-day (- (time-to-days org-def)
  14076. (calendar-absolute-from-gregorian
  14077. (calendar-current-date))))
  14078. (org-eval-in-calendar nil t)
  14079. (let* ((old-map (current-local-map))
  14080. (map (copy-keymap calendar-mode-map))
  14081. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  14082. (org-defkey map (kbd "RET") 'org-calendar-select)
  14083. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14084. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14085. (org-defkey minibuffer-local-map [(meta shift left)]
  14086. (lambda () (interactive)
  14087. (org-eval-in-calendar '(calendar-backward-month 1))))
  14088. (org-defkey minibuffer-local-map [(meta shift right)]
  14089. (lambda () (interactive)
  14090. (org-eval-in-calendar '(calendar-forward-month 1))))
  14091. (org-defkey minibuffer-local-map [(meta shift up)]
  14092. (lambda () (interactive)
  14093. (org-eval-in-calendar '(calendar-backward-year 1))))
  14094. (org-defkey minibuffer-local-map [(meta shift down)]
  14095. (lambda () (interactive)
  14096. (org-eval-in-calendar '(calendar-forward-year 1))))
  14097. (org-defkey minibuffer-local-map [?\e (shift left)]
  14098. (lambda () (interactive)
  14099. (org-eval-in-calendar '(calendar-backward-month 1))))
  14100. (org-defkey minibuffer-local-map [?\e (shift right)]
  14101. (lambda () (interactive)
  14102. (org-eval-in-calendar '(calendar-forward-month 1))))
  14103. (org-defkey minibuffer-local-map [?\e (shift up)]
  14104. (lambda () (interactive)
  14105. (org-eval-in-calendar '(calendar-backward-year 1))))
  14106. (org-defkey minibuffer-local-map [?\e (shift down)]
  14107. (lambda () (interactive)
  14108. (org-eval-in-calendar '(calendar-forward-year 1))))
  14109. (org-defkey minibuffer-local-map [(shift up)]
  14110. (lambda () (interactive)
  14111. (org-eval-in-calendar '(calendar-backward-week 1))))
  14112. (org-defkey minibuffer-local-map [(shift down)]
  14113. (lambda () (interactive)
  14114. (org-eval-in-calendar '(calendar-forward-week 1))))
  14115. (org-defkey minibuffer-local-map [(shift left)]
  14116. (lambda () (interactive)
  14117. (org-eval-in-calendar '(calendar-backward-day 1))))
  14118. (org-defkey minibuffer-local-map [(shift right)]
  14119. (lambda () (interactive)
  14120. (org-eval-in-calendar '(calendar-forward-day 1))))
  14121. (org-defkey minibuffer-local-map ">"
  14122. (lambda () (interactive)
  14123. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14124. (org-defkey minibuffer-local-map "<"
  14125. (lambda () (interactive)
  14126. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14127. (org-defkey minibuffer-local-map "\C-v"
  14128. (lambda () (interactive)
  14129. (org-eval-in-calendar
  14130. '(calendar-scroll-left-three-months 1))))
  14131. (org-defkey minibuffer-local-map "\M-v"
  14132. (lambda () (interactive)
  14133. (org-eval-in-calendar
  14134. '(calendar-scroll-right-three-months 1))))
  14135. (run-hooks 'org-read-date-minibuffer-setup-hook)
  14136. (unwind-protect
  14137. (progn
  14138. (use-local-map map)
  14139. (setq org-read-date-inactive inactive)
  14140. (add-hook 'post-command-hook 'org-read-date-display)
  14141. (setq org-ans0 (read-string prompt default-input
  14142. 'org-read-date-history nil))
  14143. ;; org-ans0: from prompt
  14144. ;; org-ans1: from mouse click
  14145. ;; org-ans2: from calendar motion
  14146. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14147. (remove-hook 'post-command-hook 'org-read-date-display)
  14148. (use-local-map old-map)
  14149. (when org-read-date-overlay
  14150. (delete-overlay org-read-date-overlay)
  14151. (setq org-read-date-overlay nil)))))
  14152. (bury-buffer "*Calendar*")))))
  14153. (t ; Naked prompt only
  14154. (unwind-protect
  14155. (setq ans (read-string prompt default-input
  14156. 'org-read-date-history timestr))
  14157. (when org-read-date-overlay
  14158. (delete-overlay org-read-date-overlay)
  14159. (setq org-read-date-overlay nil)))))
  14160. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14161. (when org-read-date-analyze-forced-year
  14162. (message "Year was forced into %s"
  14163. (if org-read-date-force-compatible-dates
  14164. "compatible range (1970-2037)"
  14165. "range representable on this machine"))
  14166. (ding))
  14167. ;; One round trip to get rid of 34th of August and stuff like that....
  14168. (setq final (decode-time (apply 'encode-time final)))
  14169. (setq org-read-date-final-answer ans)
  14170. (if to-time
  14171. (apply 'encode-time final)
  14172. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14173. (format "%04d-%02d-%02d %02d:%02d"
  14174. (nth 5 final) (nth 4 final) (nth 3 final)
  14175. (nth 2 final) (nth 1 final))
  14176. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14177. (defvar org-def)
  14178. (defvar org-defdecode)
  14179. (defvar org-with-time)
  14180. (defun org-read-date-display ()
  14181. "Display the current date prompt interpretation in the minibuffer."
  14182. (when org-read-date-display-live
  14183. (when org-read-date-overlay
  14184. (delete-overlay org-read-date-overlay))
  14185. (when (minibufferp (current-buffer))
  14186. (save-excursion
  14187. (end-of-line 1)
  14188. (while (not (equal (buffer-substring
  14189. (max (point-min) (- (point) 4)) (point))
  14190. " "))
  14191. (insert " ")))
  14192. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14193. " " (or org-ans1 org-ans2)))
  14194. (org-end-time-was-given nil)
  14195. (f (org-read-date-analyze ans org-def org-defdecode))
  14196. (fmts (if org-dcst
  14197. org-time-stamp-custom-formats
  14198. org-time-stamp-formats))
  14199. (fmt (if (or org-with-time
  14200. (and (boundp 'org-time-was-given) org-time-was-given))
  14201. (cdr fmts)
  14202. (car fmts)))
  14203. (txt (format-time-string fmt (apply 'encode-time f)))
  14204. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14205. (txt (concat "=> " txt)))
  14206. (when (and org-end-time-was-given
  14207. (string-match org-plain-time-of-day-regexp txt))
  14208. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14209. org-end-time-was-given
  14210. (substring txt (match-end 0)))))
  14211. (when org-read-date-analyze-futurep
  14212. (setq txt (concat txt " (=>F)")))
  14213. (setq org-read-date-overlay
  14214. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14215. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14216. (defun org-read-date-analyze (ans org-def org-defdecode)
  14217. "Analyze the combined answer of the date prompt."
  14218. ;; FIXME: cleanup and comment
  14219. (let ((nowdecode (decode-time (current-time)))
  14220. delta deltan deltaw deltadef year month day
  14221. hour minute second wday pm h2 m2 tl wday1
  14222. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14223. (setq org-read-date-analyze-futurep nil
  14224. org-read-date-analyze-forced-year nil)
  14225. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14226. (setq ans "+0"))
  14227. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14228. (setq ans (replace-match "" t t ans)
  14229. deltan (car delta)
  14230. deltaw (nth 1 delta)
  14231. deltadef (nth 2 delta)))
  14232. ;; Check if there is an iso week date in there. If yes, store the
  14233. ;; info and postpone interpreting it until the rest of the parsing
  14234. ;; is done.
  14235. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14236. (setq iso-year (if (match-end 1)
  14237. (org-small-year-to-year
  14238. (string-to-number (match-string 1 ans))))
  14239. iso-weekday (if (match-end 3)
  14240. (string-to-number (match-string 3 ans)))
  14241. iso-week (string-to-number (match-string 2 ans)))
  14242. (setq ans (replace-match "" t t ans)))
  14243. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14244. (when (string-match
  14245. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14246. (setq year (if (match-end 2)
  14247. (string-to-number (match-string 2 ans))
  14248. (progn (setq kill-year t)
  14249. (string-to-number (format-time-string "%Y"))))
  14250. month (string-to-number (match-string 3 ans))
  14251. day (string-to-number (match-string 4 ans)))
  14252. (if (< year 100) (setq year (+ 2000 year)))
  14253. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14254. t nil ans)))
  14255. ;; Help matching dotted european dates
  14256. (when (string-match
  14257. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14258. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14259. (setq kill-year t)
  14260. (string-to-number (format-time-string "%Y")))
  14261. day (string-to-number (match-string 1 ans))
  14262. month (string-to-number (match-string 2 ans))
  14263. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14264. t nil ans)))
  14265. ;; Help matching american dates, like 5/30 or 5/30/7
  14266. (when (string-match
  14267. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14268. (setq year (if (match-end 4)
  14269. (string-to-number (match-string 4 ans))
  14270. (progn (setq kill-year t)
  14271. (string-to-number (format-time-string "%Y"))))
  14272. month (string-to-number (match-string 1 ans))
  14273. day (string-to-number (match-string 2 ans)))
  14274. (if (< year 100) (setq year (+ 2000 year)))
  14275. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14276. t nil ans)))
  14277. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14278. ;; If there is a time with am/pm, and *no* time without it, we convert
  14279. ;; so that matching will be successful.
  14280. (loop for i from 1 to 2 do ; twice, for end time as well
  14281. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14282. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14283. (setq hour (string-to-number (match-string 1 ans))
  14284. minute (if (match-end 3)
  14285. (string-to-number (match-string 3 ans))
  14286. 0)
  14287. pm (equal ?p
  14288. (string-to-char (downcase (match-string 4 ans)))))
  14289. (if (and (= hour 12) (not pm))
  14290. (setq hour 0)
  14291. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14292. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14293. t t ans))))
  14294. ;; Check if a time range is given as a duration
  14295. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14296. (setq hour (string-to-number (match-string 1 ans))
  14297. h2 (+ hour (string-to-number (match-string 3 ans)))
  14298. minute (string-to-number (match-string 2 ans))
  14299. m2 (+ minute (if (match-end 5) (string-to-number
  14300. (match-string 5 ans))0)))
  14301. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14302. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14303. t t ans)))
  14304. ;; Check if there is a time range
  14305. (when (boundp 'org-end-time-was-given)
  14306. (setq org-time-was-given nil)
  14307. (when (and (string-match org-plain-time-of-day-regexp ans)
  14308. (match-end 8))
  14309. (setq org-end-time-was-given (match-string 8 ans))
  14310. (setq ans (concat (substring ans 0 (match-beginning 7))
  14311. (substring ans (match-end 7))))))
  14312. (setq tl (parse-time-string ans)
  14313. day (or (nth 3 tl) (nth 3 org-defdecode))
  14314. month (or (nth 4 tl)
  14315. (if (and org-read-date-prefer-future
  14316. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14317. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14318. (nth 4 org-defdecode)))
  14319. year (or (and (not kill-year) (nth 5 tl))
  14320. (if (and org-read-date-prefer-future
  14321. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14322. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14323. (nth 5 org-defdecode)))
  14324. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14325. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14326. second (or (nth 0 tl) 0)
  14327. wday (nth 6 tl))
  14328. (when (and (eq org-read-date-prefer-future 'time)
  14329. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14330. (equal day (nth 3 nowdecode))
  14331. (equal month (nth 4 nowdecode))
  14332. (equal year (nth 5 nowdecode))
  14333. (nth 2 tl)
  14334. (or (< (nth 2 tl) (nth 2 nowdecode))
  14335. (and (= (nth 2 tl) (nth 2 nowdecode))
  14336. (nth 1 tl)
  14337. (< (nth 1 tl) (nth 1 nowdecode)))))
  14338. (setq day (1+ day)
  14339. futurep t))
  14340. ;; Special date definitions below
  14341. (cond
  14342. (iso-week
  14343. ;; There was an iso week
  14344. (require 'cal-iso)
  14345. (setq futurep nil)
  14346. (setq year (or iso-year year)
  14347. day (or iso-weekday wday 1)
  14348. wday nil ; to make sure that the trigger below does not match
  14349. iso-date (calendar-gregorian-from-absolute
  14350. (calendar-absolute-from-iso
  14351. (list iso-week day year))))
  14352. ; FIXME: Should we also push ISO weeks into the future?
  14353. ; (when (and org-read-date-prefer-future
  14354. ; (not iso-year)
  14355. ; (< (calendar-absolute-from-gregorian iso-date)
  14356. ; (time-to-days (current-time))))
  14357. ; (setq year (1+ year)
  14358. ; iso-date (calendar-gregorian-from-absolute
  14359. ; (calendar-absolute-from-iso
  14360. ; (list iso-week day year)))))
  14361. (setq month (car iso-date)
  14362. year (nth 2 iso-date)
  14363. day (nth 1 iso-date)))
  14364. (deltan
  14365. (setq futurep nil)
  14366. (unless deltadef
  14367. (let ((now (decode-time (current-time))))
  14368. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14369. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14370. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14371. ((equal deltaw "m") (setq month (+ month deltan)))
  14372. ((equal deltaw "y") (setq year (+ year deltan)))))
  14373. ((and wday (not (nth 3 tl)))
  14374. ;; Weekday was given, but no day, so pick that day in the week
  14375. ;; on or after the derived date.
  14376. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14377. (unless (equal wday wday1)
  14378. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14379. (if (and (boundp 'org-time-was-given)
  14380. (nth 2 tl))
  14381. (setq org-time-was-given t))
  14382. (if (< year 100) (setq year (+ 2000 year)))
  14383. ;; Check of the date is representable
  14384. (if org-read-date-force-compatible-dates
  14385. (progn
  14386. (if (< year 1970)
  14387. (setq year 1970 org-read-date-analyze-forced-year t))
  14388. (if (> year 2037)
  14389. (setq year 2037 org-read-date-analyze-forced-year t)))
  14390. (condition-case nil
  14391. (ignore (encode-time second minute hour day month year))
  14392. (error
  14393. (setq year (nth 5 org-defdecode))
  14394. (setq org-read-date-analyze-forced-year t))))
  14395. (setq org-read-date-analyze-futurep futurep)
  14396. (list second minute hour day month year)))
  14397. (defvar parse-time-weekdays)
  14398. (defun org-read-date-get-relative (s today default)
  14399. "Check string S for special relative date string.
  14400. TODAY and DEFAULT are internal times, for today and for a default.
  14401. Return shift list (N what def-flag)
  14402. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14403. N is the number of WHATs to shift.
  14404. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14405. the DEFAULT date rather than TODAY."
  14406. (require 'parse-time)
  14407. (when (and
  14408. (string-match
  14409. (concat
  14410. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14411. "\\([0-9]+\\)?"
  14412. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14413. "\\([ \t]\\|$\\)") s)
  14414. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14415. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14416. (string-to-char (substring (match-string 1 s) -1))
  14417. ?+))
  14418. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14419. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14420. (what (if (match-end 3) (match-string 3 s) "d"))
  14421. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14422. (date (if rel default today))
  14423. (wday (nth 6 (decode-time date)))
  14424. delta)
  14425. (if wday1
  14426. (progn
  14427. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14428. (if (= dir ?-) (setq delta (- delta 7)))
  14429. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14430. (list delta "d" rel))
  14431. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14432. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14433. "Turn a user-specified date into the internal representation.
  14434. The internal representation needed by the calendar is (month day year).
  14435. This is a wrapper to handle the brain-dead convention in calendar that
  14436. user function argument order change dependent on argument order."
  14437. (if (boundp 'calendar-date-style)
  14438. (cond
  14439. ((eq calendar-date-style 'american)
  14440. (list arg1 arg2 arg3))
  14441. ((eq calendar-date-style 'european)
  14442. (list arg2 arg1 arg3))
  14443. ((eq calendar-date-style 'iso)
  14444. (list arg2 arg3 arg1)))
  14445. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14446. (if (org-bound-and-true-p european-calendar-style)
  14447. (list arg2 arg1 arg3)
  14448. (list arg1 arg2 arg3)))))
  14449. (defun org-eval-in-calendar (form &optional keepdate)
  14450. "Eval FORM in the calendar window and return to current window.
  14451. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14452. otherwise stick to the current value of `org-ans2'."
  14453. (let ((sf (selected-frame))
  14454. (sw (selected-window)))
  14455. (select-window (get-buffer-window "*Calendar*" t))
  14456. (eval form)
  14457. (when (and (not keepdate) (calendar-cursor-to-date))
  14458. (let* ((date (calendar-cursor-to-date))
  14459. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14460. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14461. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14462. (select-window sw)
  14463. (org-select-frame-set-input-focus sf)))
  14464. (defun org-calendar-select ()
  14465. "Return to `org-read-date' with the date currently selected.
  14466. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14467. (interactive)
  14468. (when (calendar-cursor-to-date)
  14469. (let* ((date (calendar-cursor-to-date))
  14470. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14471. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14472. (if (active-minibuffer-window) (exit-minibuffer))))
  14473. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14474. "Insert a date stamp for the date given by the internal TIME.
  14475. WITH-HM means use the stamp format that includes the time of the day.
  14476. INACTIVE means use square brackets instead of angular ones, so that the
  14477. stamp will not contribute to the agenda.
  14478. PRE and POST are optional strings to be inserted before and after the
  14479. stamp.
  14480. The command returns the inserted time stamp."
  14481. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14482. stamp)
  14483. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14484. (insert-before-markers (or pre ""))
  14485. (when (listp extra)
  14486. (setq extra (car extra))
  14487. (if (and (stringp extra)
  14488. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14489. (setq extra (format "-%02d:%02d"
  14490. (string-to-number (match-string 1 extra))
  14491. (string-to-number (match-string 2 extra))))
  14492. (setq extra nil)))
  14493. (when extra
  14494. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14495. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14496. (insert-before-markers (or post ""))
  14497. (setq org-last-inserted-timestamp stamp)))
  14498. (defun org-toggle-time-stamp-overlays ()
  14499. "Toggle the use of custom time stamp formats."
  14500. (interactive)
  14501. (setq org-display-custom-times (not org-display-custom-times))
  14502. (unless org-display-custom-times
  14503. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14504. (while (setq p (next-single-property-change p 'display))
  14505. (if (and (get-text-property p 'display)
  14506. (eq (get-text-property p 'face) 'org-date))
  14507. (remove-text-properties
  14508. p (setq p (next-single-property-change p 'display))
  14509. '(display t))))
  14510. (set-buffer-modified-p bmp)))
  14511. (if (featurep 'xemacs)
  14512. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14513. (org-restart-font-lock)
  14514. (setq org-table-may-need-update t)
  14515. (if org-display-custom-times
  14516. (message "Time stamps are overlaid with custom format")
  14517. (message "Time stamp overlays removed")))
  14518. (defun org-display-custom-time (beg end)
  14519. "Overlay modified time stamp format over timestamp between BEG and END."
  14520. (let* ((ts (buffer-substring beg end))
  14521. t1 w1 with-hm tf time str w2 (off 0))
  14522. (save-match-data
  14523. (setq t1 (org-parse-time-string ts t))
  14524. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14525. (setq off (- (match-end 0) (match-beginning 0)))))
  14526. (setq end (- end off))
  14527. (setq w1 (- end beg)
  14528. with-hm (and (nth 1 t1) (nth 2 t1))
  14529. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14530. time (org-fix-decoded-time t1)
  14531. str (org-add-props
  14532. (format-time-string
  14533. (substring tf 1 -1) (apply 'encode-time time))
  14534. nil 'mouse-face 'highlight)
  14535. w2 (length str))
  14536. (if (not (= w2 w1))
  14537. (add-text-properties (1+ beg) (+ 2 beg)
  14538. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14539. (if (featurep 'xemacs)
  14540. (progn
  14541. (put-text-property beg end 'invisible t)
  14542. (put-text-property beg end 'end-glyph (make-glyph str)))
  14543. (put-text-property beg end 'display str))))
  14544. (defun org-translate-time (string)
  14545. "Translate all timestamps in STRING to custom format.
  14546. But do this only if the variable `org-display-custom-times' is set."
  14547. (when org-display-custom-times
  14548. (save-match-data
  14549. (let* ((start 0)
  14550. (re org-ts-regexp-both)
  14551. t1 with-hm inactive tf time str beg end)
  14552. (while (setq start (string-match re string start))
  14553. (setq beg (match-beginning 0)
  14554. end (match-end 0)
  14555. t1 (save-match-data
  14556. (org-parse-time-string (substring string beg end) t))
  14557. with-hm (and (nth 1 t1) (nth 2 t1))
  14558. inactive (equal (substring string beg (1+ beg)) "[")
  14559. tf (funcall (if with-hm 'cdr 'car)
  14560. org-time-stamp-custom-formats)
  14561. time (org-fix-decoded-time t1)
  14562. str (format-time-string
  14563. (concat
  14564. (if inactive "[" "<") (substring tf 1 -1)
  14565. (if inactive "]" ">"))
  14566. (apply 'encode-time time))
  14567. string (replace-match str t t string)
  14568. start (+ start (length str)))))))
  14569. string)
  14570. (defun org-fix-decoded-time (time)
  14571. "Set 0 instead of nil for the first 6 elements of time.
  14572. Don't touch the rest."
  14573. (let ((n 0))
  14574. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14575. (defun org-days-to-time (timestamp-string)
  14576. "Difference between TIMESTAMP-STRING and now in days."
  14577. (- (time-to-days (org-time-string-to-time timestamp-string))
  14578. (time-to-days (current-time))))
  14579. (defun org-deadline-close (timestamp-string &optional ndays)
  14580. "Is the time in TIMESTAMP-STRING close to the current date?"
  14581. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14582. (and (< (org-days-to-time timestamp-string) ndays)
  14583. (not (org-entry-is-done-p))))
  14584. (defun org-get-wdays (ts)
  14585. "Get the deadline lead time appropriate for timestring TS."
  14586. (cond
  14587. ((<= org-deadline-warning-days 0)
  14588. ;; 0 or negative, enforce this value no matter what
  14589. (- org-deadline-warning-days))
  14590. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14591. ;; lead time is specified.
  14592. (floor (* (string-to-number (match-string 1 ts))
  14593. (cdr (assoc (match-string 2 ts)
  14594. '(("d" . 1) ("w" . 7)
  14595. ("m" . 30.4) ("y" . 365.25)
  14596. ("h" . 0.041667)))))))
  14597. ;; go for the default.
  14598. (t org-deadline-warning-days)))
  14599. (defun org-calendar-select-mouse (ev)
  14600. "Return to `org-read-date' with the date currently selected.
  14601. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14602. (interactive "e")
  14603. (mouse-set-point ev)
  14604. (when (calendar-cursor-to-date)
  14605. (let* ((date (calendar-cursor-to-date))
  14606. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14607. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14608. (if (active-minibuffer-window) (exit-minibuffer))))
  14609. (defun org-check-deadlines (ndays)
  14610. "Check if there are any deadlines due or past due.
  14611. A deadline is considered due if it happens within `org-deadline-warning-days'
  14612. days from today's date. If the deadline appears in an entry marked DONE,
  14613. it is not shown. The prefix arg NDAYS can be used to test that many
  14614. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14615. (interactive "P")
  14616. (let* ((org-warn-days
  14617. (cond
  14618. ((equal ndays '(4)) 100000)
  14619. (ndays (prefix-numeric-value ndays))
  14620. (t (abs org-deadline-warning-days))))
  14621. (case-fold-search nil)
  14622. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14623. (callback
  14624. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14625. (message "%d deadlines past-due or due within %d days"
  14626. (org-occur regexp nil callback)
  14627. org-warn-days)))
  14628. (defsubst org-re-timestamp (type)
  14629. "Return a regexp for timestamp TYPE.
  14630. Allowed values for TYPE are:
  14631. all: all timestamps
  14632. active: only active timestamps (<...>)
  14633. inactive: only inactive timestamps ([...])
  14634. scheduled: only scheduled timestamps
  14635. deadline: only deadline timestamps
  14636. When TYPE is nil, fall back on returning a regexp that matches
  14637. both scheduled and deadline timestamps."
  14638. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14639. ((eq type 'active) org-ts-regexp)
  14640. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14641. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14642. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14643. ((eq type 'scheduled-or-deadline)
  14644. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14645. (defun org-check-before-date (date)
  14646. "Check if there are deadlines or scheduled entries before DATE."
  14647. (interactive (list (org-read-date)))
  14648. (let ((case-fold-search nil)
  14649. (regexp (org-re-timestamp org-ts-type))
  14650. (callback
  14651. (lambda () (time-less-p
  14652. (org-time-string-to-time (match-string 1))
  14653. (org-time-string-to-time date)))))
  14654. (message "%d entries before %s"
  14655. (org-occur regexp nil callback) date)))
  14656. (defun org-check-after-date (date)
  14657. "Check if there are deadlines or scheduled entries after DATE."
  14658. (interactive (list (org-read-date)))
  14659. (let ((case-fold-search nil)
  14660. (regexp (org-re-timestamp org-ts-type))
  14661. (callback
  14662. (lambda () (not
  14663. (time-less-p
  14664. (org-time-string-to-time (match-string 1))
  14665. (org-time-string-to-time date))))))
  14666. (message "%d entries after %s"
  14667. (org-occur regexp nil callback) date)))
  14668. (defun org-check-dates-range (start-date end-date)
  14669. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14670. (interactive (list (org-read-date nil nil nil "Range starts")
  14671. (org-read-date nil nil nil "Range end")))
  14672. (let ((case-fold-search nil)
  14673. (regexp (org-re-timestamp org-ts-type))
  14674. (callback
  14675. (lambda ()
  14676. (let ((match (match-string 1)))
  14677. (and
  14678. (not (time-less-p
  14679. (org-time-string-to-time match)
  14680. (org-time-string-to-time start-date)))
  14681. (time-less-p
  14682. (org-time-string-to-time match)
  14683. (org-time-string-to-time end-date)))))))
  14684. (message "%d entries between %s and %s"
  14685. (org-occur regexp nil callback) start-date end-date)))
  14686. (defun org-evaluate-time-range (&optional to-buffer)
  14687. "Evaluate a time range by computing the difference between start and end.
  14688. Normally the result is just printed in the echo area, but with prefix arg
  14689. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14690. If the time range is actually in a table, the result is inserted into the
  14691. next column.
  14692. For time difference computation, a year is assumed to be exactly 365
  14693. days in order to avoid rounding problems."
  14694. (interactive "P")
  14695. (or
  14696. (org-clock-update-time-maybe)
  14697. (save-excursion
  14698. (unless (org-at-date-range-p t)
  14699. (goto-char (point-at-bol))
  14700. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14701. (if (not (org-at-date-range-p t))
  14702. (error "Not at a time-stamp range, and none found in current line")))
  14703. (let* ((ts1 (match-string 1))
  14704. (ts2 (match-string 2))
  14705. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14706. (match-end (match-end 0))
  14707. (time1 (org-time-string-to-time ts1))
  14708. (time2 (org-time-string-to-time ts2))
  14709. (t1 (org-float-time time1))
  14710. (t2 (org-float-time time2))
  14711. (diff (abs (- t2 t1)))
  14712. (negative (< (- t2 t1) 0))
  14713. ;; (ys (floor (* 365 24 60 60)))
  14714. (ds (* 24 60 60))
  14715. (hs (* 60 60))
  14716. (fy "%dy %dd %02d:%02d")
  14717. (fy1 "%dy %dd")
  14718. (fd "%dd %02d:%02d")
  14719. (fd1 "%dd")
  14720. (fh "%02d:%02d")
  14721. y d h m align)
  14722. (if havetime
  14723. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14724. y 0
  14725. d (floor (/ diff ds)) diff (mod diff ds)
  14726. h (floor (/ diff hs)) diff (mod diff hs)
  14727. m (floor (/ diff 60)))
  14728. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14729. y 0
  14730. d (floor (+ (/ diff ds) 0.5))
  14731. h 0 m 0))
  14732. (if (not to-buffer)
  14733. (message "%s" (org-make-tdiff-string y d h m))
  14734. (if (org-at-table-p)
  14735. (progn
  14736. (goto-char match-end)
  14737. (setq align t)
  14738. (and (looking-at " *|") (goto-char (match-end 0))))
  14739. (goto-char match-end))
  14740. (if (looking-at
  14741. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14742. (replace-match ""))
  14743. (if negative (insert " -"))
  14744. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14745. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14746. (insert " " (format fh h m))))
  14747. (if align (org-table-align))
  14748. (message "Time difference inserted")))))
  14749. (defun org-make-tdiff-string (y d h m)
  14750. (let ((fmt "")
  14751. (l nil))
  14752. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14753. l (push y l)))
  14754. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14755. l (push d l)))
  14756. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14757. l (push h l)))
  14758. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14759. l (push m l)))
  14760. (apply 'format fmt (nreverse l))))
  14761. (defun org-time-string-to-time (s &optional buffer pos)
  14762. "Convert a timestamp string into internal time."
  14763. (condition-case errdata
  14764. (apply 'encode-time (org-parse-time-string s))
  14765. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14766. s (if (not (and buffer pos))
  14767. ""
  14768. (format " at %d in buffer `%s'" pos buffer))
  14769. (cdr errdata)))))
  14770. (defun org-time-string-to-seconds (s)
  14771. "Convert a timestamp string to a number of seconds."
  14772. (org-float-time (org-time-string-to-time s)))
  14773. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14774. "Convert a time stamp to an absolute day number.
  14775. If there is a specifier for a cyclic time stamp, get the closest date to
  14776. DAYNR.
  14777. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14778. The variable date is bound by the calendar when this is called."
  14779. (cond
  14780. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14781. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14782. daynr
  14783. (+ daynr 1000)))
  14784. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14785. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14786. (time-to-days (current-time))) (match-string 0 s)
  14787. prefer show-all))
  14788. (t (time-to-days
  14789. (condition-case errdata
  14790. (apply 'encode-time (org-parse-time-string s))
  14791. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14792. s (if (not (and buffer pos))
  14793. ""
  14794. (format " at %d in buffer `%s'" pos buffer))
  14795. (cdr errdata))))))))
  14796. (defun org-days-to-iso-week (days)
  14797. "Return the iso week number."
  14798. (require 'cal-iso)
  14799. (car (calendar-iso-from-absolute days)))
  14800. (defun org-small-year-to-year (year)
  14801. "Convert 2-digit years into 4-digit years.
  14802. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14803. The year 2000 cannot be abbreviated. Any year larger than 99
  14804. is returned unchanged."
  14805. (if (< year 38)
  14806. (setq year (+ 2000 year))
  14807. (if (< year 100)
  14808. (setq year (+ 1900 year))))
  14809. year)
  14810. (defun org-time-from-absolute (d)
  14811. "Return the time corresponding to date D.
  14812. D may be an absolute day number, or a calendar-type list (month day year)."
  14813. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14814. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14815. (defun org-calendar-holiday ()
  14816. "List of holidays, for Diary display in Org-mode."
  14817. (require 'holidays)
  14818. (let ((hl (funcall
  14819. (if (fboundp 'calendar-check-holidays)
  14820. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14821. (if hl (mapconcat 'identity hl "; "))))
  14822. (defun org-diary-sexp-entry (sexp entry date)
  14823. "Process a SEXP diary ENTRY for DATE."
  14824. (require 'diary-lib)
  14825. (let ((result (if calendar-debug-sexp
  14826. (let ((stack-trace-on-error t))
  14827. (eval (car (read-from-string sexp))))
  14828. (condition-case nil
  14829. (eval (car (read-from-string sexp)))
  14830. (error
  14831. (beep)
  14832. (message "Bad sexp at line %d in %s: %s"
  14833. (org-current-line)
  14834. (buffer-file-name) sexp)
  14835. (sleep-for 2))))))
  14836. (cond ((stringp result) (split-string result "; "))
  14837. ((and (consp result)
  14838. (not (consp (cdr result)))
  14839. (stringp (cdr result))) (cdr result))
  14840. ((and (consp result)
  14841. (stringp (car result))) result)
  14842. (result entry))))
  14843. (defun org-diary-to-ical-string (frombuf)
  14844. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14845. This uses the icalendar.el library."
  14846. (let* ((tmpdir (if (featurep 'xemacs)
  14847. (temp-directory)
  14848. temporary-file-directory))
  14849. (tmpfile (make-temp-name
  14850. (expand-file-name "orgics" tmpdir)))
  14851. buf rtn b e)
  14852. (with-current-buffer frombuf
  14853. (icalendar-export-region (point-min) (point-max) tmpfile)
  14854. (setq buf (find-buffer-visiting tmpfile))
  14855. (set-buffer buf)
  14856. (goto-char (point-min))
  14857. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14858. (setq b (match-beginning 0)))
  14859. (goto-char (point-max))
  14860. (if (re-search-backward "^END:VEVENT" nil t)
  14861. (setq e (match-end 0)))
  14862. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14863. (kill-buffer buf)
  14864. (delete-file tmpfile)
  14865. rtn))
  14866. (defun org-closest-date (start current change prefer show-all)
  14867. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14868. When PREFER is `past', return a date that is either CURRENT or past.
  14869. When PREFER is `future', return a date that is either CURRENT or future.
  14870. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14871. ;; Make the proper lists from the dates
  14872. (catch 'exit
  14873. (let ((a1 '(("h" . hour)
  14874. ("d" . day)
  14875. ("w" . week)
  14876. ("m" . month)
  14877. ("y" . year)))
  14878. (shour (nth 2 (org-parse-time-string start)))
  14879. dn dw sday cday n1 n2 n0
  14880. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14881. (setq start (org-date-to-gregorian start)
  14882. current (org-date-to-gregorian
  14883. (if show-all
  14884. current
  14885. (time-to-days (current-time))))
  14886. sday (calendar-absolute-from-gregorian start)
  14887. cday (calendar-absolute-from-gregorian current))
  14888. (if (<= cday sday) (throw 'exit sday))
  14889. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  14890. (setq dn (string-to-number (match-string 1 change))
  14891. dw (cdr (assoc (match-string 2 change) a1)))
  14892. (error "Invalid change specifier: %s" change))
  14893. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14894. (cond
  14895. ((eq dw 'hour)
  14896. (let ((missing-hours
  14897. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  14898. dn)))
  14899. (setq n1 (if (zerop missing-hours) cday
  14900. (- cday (1+ (floor (/ missing-hours 24)))))
  14901. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  14902. ((eq dw 'day)
  14903. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14904. n2 (+ n1 dn)))
  14905. ((eq dw 'year)
  14906. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14907. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14908. (setq date1 (list m d y1)
  14909. n1 (calendar-absolute-from-gregorian date1)
  14910. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14911. n2 (calendar-absolute-from-gregorian date2)))
  14912. ((eq dw 'month)
  14913. ;; approx number of month between the two dates
  14914. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14915. ;; How often does dn fit in there?
  14916. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14917. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14918. m (+ m nm)
  14919. ny (floor (/ m 12))
  14920. y (+ y ny)
  14921. m (- m (* ny 12)))
  14922. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14923. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  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. (while (<= n2 cday)
  14928. (setq n1 n2 m m2 y y2)
  14929. (setq m2 (+ m dn) y2 y)
  14930. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14931. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14932. ;; Make sure n1 is the earlier date
  14933. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14934. (if show-all
  14935. (cond
  14936. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14937. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14938. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14939. (cond
  14940. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14941. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14942. (t (if (= cday n1) n1 n2)))))))
  14943. (defun org-date-to-gregorian (date)
  14944. "Turn any specification of DATE into a Gregorian date for the calendar."
  14945. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14946. ((and (listp date) (= (length date) 3)) date)
  14947. ((stringp date)
  14948. (setq date (org-parse-time-string date))
  14949. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14950. ((listp date)
  14951. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14952. (defun org-parse-time-string (s &optional nodefault)
  14953. "Parse the standard Org-mode time string.
  14954. This should be a lot faster than the normal `parse-time-string'.
  14955. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14956. hour and minute fields will be nil if not given."
  14957. (if (string-match org-ts-regexp0 s)
  14958. (list 0
  14959. (if (or (match-beginning 8) (not nodefault))
  14960. (string-to-number (or (match-string 8 s) "0")))
  14961. (if (or (match-beginning 7) (not nodefault))
  14962. (string-to-number (or (match-string 7 s) "0")))
  14963. (string-to-number (match-string 4 s))
  14964. (string-to-number (match-string 3 s))
  14965. (string-to-number (match-string 2 s))
  14966. nil nil nil)
  14967. (error "Not a standard Org-mode time string: %s" s)))
  14968. (defun org-timestamp-up (&optional arg)
  14969. "Increase the date item at the cursor by one.
  14970. If the cursor is on the year, change the year. If it is on the month,
  14971. the day or the time, change that.
  14972. With prefix ARG, change by that many units."
  14973. (interactive "p")
  14974. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14975. (defun org-timestamp-down (&optional arg)
  14976. "Decrease the date item at the cursor by one.
  14977. If the cursor is on the year, change the year. If it is on the month,
  14978. the day or the time, change that.
  14979. With prefix ARG, change by that many units."
  14980. (interactive "p")
  14981. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14982. (defun org-timestamp-up-day (&optional arg)
  14983. "Increase the date in the time stamp by one day.
  14984. With prefix ARG, change that many days."
  14985. (interactive "p")
  14986. (if (and (not (org-at-timestamp-p t))
  14987. (org-at-heading-p))
  14988. (org-todo 'up)
  14989. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14990. (defun org-timestamp-down-day (&optional arg)
  14991. "Decrease the date in the time stamp by one day.
  14992. With prefix ARG, change that many days."
  14993. (interactive "p")
  14994. (if (and (not (org-at-timestamp-p t))
  14995. (org-at-heading-p))
  14996. (org-todo 'down)
  14997. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14998. (defun org-at-timestamp-p (&optional inactive-ok)
  14999. "Determine if the cursor is in or at a timestamp."
  15000. (interactive)
  15001. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15002. (pos (point))
  15003. (ans (or (looking-at tsr)
  15004. (save-excursion
  15005. (skip-chars-backward "^[<\n\r\t")
  15006. (if (> (point) (point-min)) (backward-char 1))
  15007. (and (looking-at tsr)
  15008. (> (- (match-end 0) pos) -1))))))
  15009. (and ans
  15010. (boundp 'org-ts-what)
  15011. (setq org-ts-what
  15012. (cond
  15013. ((= pos (match-beginning 0)) 'bracket)
  15014. ;; Point is considered to be "on the bracket" whether
  15015. ;; it's really on it or right after it.
  15016. ((= pos (1- (match-end 0))) 'bracket)
  15017. ((= pos (match-end 0)) 'after)
  15018. ((org-pos-in-match-range pos 2) 'year)
  15019. ((org-pos-in-match-range pos 3) 'month)
  15020. ((org-pos-in-match-range pos 7) 'hour)
  15021. ((org-pos-in-match-range pos 8) 'minute)
  15022. ((or (org-pos-in-match-range pos 4)
  15023. (org-pos-in-match-range pos 5)) 'day)
  15024. ((and (> pos (or (match-end 8) (match-end 5)))
  15025. (< pos (match-end 0)))
  15026. (- pos (or (match-end 8) (match-end 5))))
  15027. (t 'day))))
  15028. ans))
  15029. (defun org-toggle-timestamp-type ()
  15030. "Toggle the type (<active> or [inactive]) of a time stamp."
  15031. (interactive)
  15032. (when (org-at-timestamp-p t)
  15033. (let ((beg (match-beginning 0)) (end (match-end 0))
  15034. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15035. (save-excursion
  15036. (goto-char beg)
  15037. (while (re-search-forward "[][<>]" end t)
  15038. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15039. t t)))
  15040. (message "Timestamp is now %sactive"
  15041. (if (equal (char-after beg) ?<) "" "in")))))
  15042. (defun org-at-clock-log-p nil
  15043. "Is the cursor on the clock log line?"
  15044. (save-excursion
  15045. (move-beginning-of-line 1)
  15046. (looking-at "^[ \t]*CLOCK:")))
  15047. (defvar org-clock-history) ; defined in org-clock.el
  15048. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15049. (defun org-timestamp-change (n &optional what updown)
  15050. "Change the date in the time stamp at point.
  15051. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15052. `year', `minute', `second'. If WHAT is not given, the cursor position
  15053. in the timestamp determines what will be changed."
  15054. (let ((origin (point)) origin-cat
  15055. with-hm inactive
  15056. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15057. org-ts-what
  15058. extra rem
  15059. ts time time0 fixnext clrgx)
  15060. (if (not (org-at-timestamp-p t))
  15061. (error "Not at a timestamp"))
  15062. (if (and (not what) (eq org-ts-what 'bracket))
  15063. (org-toggle-timestamp-type)
  15064. ;; Point isn't on brackets. Remember the part of the time-stamp
  15065. ;; the point was in. Indeed, size of time-stamps may change,
  15066. ;; but point must be kept in the same category nonetheless.
  15067. (setq origin-cat org-ts-what)
  15068. (if (and (not what) (not (eq org-ts-what 'day))
  15069. org-display-custom-times
  15070. (get-text-property (point) 'display)
  15071. (not (get-text-property (1- (point)) 'display)))
  15072. (setq org-ts-what 'day))
  15073. (setq org-ts-what (or what org-ts-what)
  15074. inactive (= (char-after (match-beginning 0)) ?\[)
  15075. ts (match-string 0))
  15076. (replace-match "")
  15077. (if (string-match
  15078. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15079. ts)
  15080. (setq extra (match-string 1 ts)))
  15081. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15082. (setq with-hm t))
  15083. (setq time0 (org-parse-time-string ts))
  15084. (when (and updown
  15085. (eq org-ts-what 'minute)
  15086. (not current-prefix-arg))
  15087. ;; This looks like s-up and s-down. Change by one rounding step.
  15088. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15089. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15090. (setcar (cdr time0) (+ (nth 1 time0)
  15091. (if (> n 0) (- rem) (- dm rem))))))
  15092. (setq time
  15093. (encode-time (or (car time0) 0)
  15094. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15095. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15096. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15097. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15098. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15099. (nthcdr 6 time0)))
  15100. (when (and (member org-ts-what '(hour minute))
  15101. extra
  15102. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15103. (setq extra (org-modify-ts-extra
  15104. extra
  15105. (if (eq org-ts-what 'hour) 2 5)
  15106. n dm)))
  15107. (when (integerp org-ts-what)
  15108. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15109. (if (eq what 'calendar)
  15110. (let ((cal-date (org-get-date-from-calendar)))
  15111. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15112. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15113. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15114. (setcar time0 (or (car time0) 0))
  15115. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15116. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15117. (setq time (apply 'encode-time time0))))
  15118. ;; Insert the new time-stamp, and ensure point stays in the same
  15119. ;; category as before (i.e. not after the last position in that
  15120. ;; category).
  15121. (let ((pos (point)))
  15122. ;; Stay before inserted string. `save-excursion' is of no use.
  15123. (setq org-last-changed-timestamp
  15124. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15125. (goto-char pos))
  15126. (save-match-data
  15127. (looking-at org-ts-regexp3)
  15128. (goto-char (cond
  15129. ;; `day' category ends before `hour' if any, or at
  15130. ;; the end of the day name.
  15131. ((eq origin-cat 'day)
  15132. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15133. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15134. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15135. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15136. ;; `year' and `month' have both fixed size: point
  15137. ;; couldn't have moved into another part.
  15138. (t origin))))
  15139. ;; Update clock if on a CLOCK line.
  15140. (org-clock-update-time-maybe)
  15141. ;; Maybe adjust the closest clock in `org-clock-history'
  15142. (when org-clock-adjust-closest
  15143. (if (not (and (org-at-clock-log-p)
  15144. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15145. org-clock-history))))))
  15146. (message "No clock to adjust")
  15147. (cond ((save-excursion ; fix previous clock?
  15148. (re-search-backward org-ts-regexp0 nil t)
  15149. (org-looking-back (concat org-clock-string " \\[")))
  15150. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15151. ((save-excursion ; fix next clock?
  15152. (re-search-backward org-ts-regexp0 nil t)
  15153. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15154. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15155. (save-window-excursion
  15156. ;; Find closest clock to point, adjust the previous/next one in history
  15157. (let* ((p (save-excursion (org-back-to-heading t)))
  15158. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15159. (clfixnth
  15160. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15161. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15162. (if (not clfixpos)
  15163. (message "No clock to adjust")
  15164. (save-excursion
  15165. (org-goto-marker-or-bmk clfixpos)
  15166. (org-show-subtree)
  15167. (when (re-search-forward clrgx nil t)
  15168. (goto-char (match-beginning 1))
  15169. (let (org-clock-adjust-closest)
  15170. (org-timestamp-change n org-ts-what updown))
  15171. (message "Clock adjusted in %s for heading: %s"
  15172. (file-name-nondirectory (buffer-file-name))
  15173. (org-get-heading t t)))))))))
  15174. ;; Try to recenter the calendar window, if any.
  15175. (if (and org-calendar-follow-timestamp-change
  15176. (get-buffer-window "*Calendar*" t)
  15177. (memq org-ts-what '(day month year)))
  15178. (org-recenter-calendar (time-to-days time))))))
  15179. (defun org-modify-ts-extra (s pos n dm)
  15180. "Change the different parts of the lead-time and repeat fields in timestamp."
  15181. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15182. ng h m new rem)
  15183. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15184. (cond
  15185. ((or (org-pos-in-match-range pos 2)
  15186. (org-pos-in-match-range pos 3))
  15187. (setq m (string-to-number (match-string 3 s))
  15188. h (string-to-number (match-string 2 s)))
  15189. (if (org-pos-in-match-range pos 2)
  15190. (setq h (+ h n))
  15191. (setq n (* dm (org-no-warnings (signum n))))
  15192. (when (not (= 0 (setq rem (% m dm))))
  15193. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15194. (setq m (+ m n)))
  15195. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15196. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15197. (setq h (min 24 (max 0 h)))
  15198. (setq ng 1 new (format "-%02d:%02d" h m)))
  15199. ((org-pos-in-match-range pos 6)
  15200. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15201. ((org-pos-in-match-range pos 5)
  15202. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15203. ((org-pos-in-match-range pos 9)
  15204. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15205. ((org-pos-in-match-range pos 8)
  15206. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15207. (when ng
  15208. (setq s (concat
  15209. (substring s 0 (match-beginning ng))
  15210. new
  15211. (substring s (match-end ng))))))
  15212. s))
  15213. (defun org-recenter-calendar (date)
  15214. "If the calendar is visible, recenter it to DATE."
  15215. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15216. (when cwin
  15217. (let ((calendar-move-hook nil))
  15218. (with-selected-window cwin
  15219. (calendar-goto-date (if (listp date) date
  15220. (calendar-gregorian-from-absolute date))))))))
  15221. (defun org-goto-calendar (&optional arg)
  15222. "Go to the Emacs calendar at the current date.
  15223. If there is a time stamp in the current line, go to that date.
  15224. A prefix ARG can be used to force the current date."
  15225. (interactive "P")
  15226. (let ((tsr org-ts-regexp) diff
  15227. (calendar-move-hook nil)
  15228. (calendar-view-holidays-initially-flag nil)
  15229. (calendar-view-diary-initially-flag nil))
  15230. (if (or (org-at-timestamp-p)
  15231. (save-excursion
  15232. (beginning-of-line 1)
  15233. (looking-at (concat ".*" tsr))))
  15234. (let ((d1 (time-to-days (current-time)))
  15235. (d2 (time-to-days
  15236. (org-time-string-to-time (match-string 1)))))
  15237. (setq diff (- d2 d1))))
  15238. (calendar)
  15239. (calendar-goto-today)
  15240. (if (and diff (not arg)) (calendar-forward-day diff))))
  15241. (defun org-get-date-from-calendar ()
  15242. "Return a list (month day year) of date at point in calendar."
  15243. (with-current-buffer "*Calendar*"
  15244. (save-match-data
  15245. (calendar-cursor-to-date))))
  15246. (defun org-date-from-calendar ()
  15247. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15248. If there is already a time stamp at the cursor position, update it."
  15249. (interactive)
  15250. (if (org-at-timestamp-p t)
  15251. (org-timestamp-change 0 'calendar)
  15252. (let ((cal-date (org-get-date-from-calendar)))
  15253. (org-insert-time-stamp
  15254. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15255. (defun org-minutes-to-hh:mm-string (m)
  15256. "Compute H:MM from a number of minutes."
  15257. (let ((h (/ m 60)))
  15258. (setq m (- m (* 60 h)))
  15259. (format org-time-clocksum-format h m)))
  15260. (defun org-hh:mm-string-to-minutes (s)
  15261. "Convert a string H:MM to a number of minutes.
  15262. If the string is just a number, interpret it as minutes.
  15263. In fact, the first hh:mm or number in the string will be taken,
  15264. there can be extra stuff in the string.
  15265. If no number is found, the return value is 0."
  15266. (cond
  15267. ((integerp s) s)
  15268. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15269. (+ (* (string-to-number (match-string 1 s)) 60)
  15270. (string-to-number (match-string 2 s))))
  15271. ((string-match "\\([0-9]+\\)" s)
  15272. (string-to-number (match-string 1 s)))
  15273. (t 0)))
  15274. (defcustom org-effort-durations
  15275. `(("h" . 60)
  15276. ("d" . ,(* 60 8))
  15277. ("w" . ,(* 60 8 5))
  15278. ("m" . ,(* 60 8 5 4))
  15279. ("y" . ,(* 60 8 5 40)))
  15280. "Conversion factor to minutes for an effort modifier.
  15281. Each entry has the form (MODIFIER . MINUTES).
  15282. In an effort string, a number followed by MODIFIER is multiplied
  15283. by the specified number of MINUTES to obtain an effort in
  15284. minutes.
  15285. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15286. effort string \"2hours\" is equivalent to 120 minutes."
  15287. :group 'org-agenda
  15288. :version "24.1"
  15289. :type '(alist :key-type (string :tag "Modifier")
  15290. :value-type (number :tag "Minutes")))
  15291. (defcustom org-agenda-inhibit-startup t
  15292. "Inhibit startup when preparing agenda buffers.
  15293. When this variable is `t' (the default), the initialization of
  15294. the Org agenda buffers is inhibited: e.g. the visibility state
  15295. is not set, the tables are not re-aligned, etc."
  15296. :type 'boolean
  15297. :version "24.3"
  15298. :group 'org-agenda)
  15299. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15300. "Convert a duration string S to minutes.
  15301. A bare number is interpreted as minutes, modifiers can be set by
  15302. customizing `org-effort-durations' (which see).
  15303. Entries containing a colon are interpreted as H:MM by
  15304. `org-hh:mm-string-to-minutes'."
  15305. (let ((result 0)
  15306. (re (concat "\\([0-9.]+\\) *\\("
  15307. (regexp-opt (mapcar 'car org-effort-durations))
  15308. "\\)")))
  15309. (while (string-match re s)
  15310. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15311. (string-to-number (match-string 1 s))))
  15312. (setq s (replace-match "" nil t s)))
  15313. (setq result (floor result))
  15314. (incf result (org-hh:mm-string-to-minutes s))
  15315. (if output-to-string (number-to-string result) result)))
  15316. ;;;; Files
  15317. (defun org-save-all-org-buffers ()
  15318. "Save all Org-mode buffers without user confirmation."
  15319. (interactive)
  15320. (message "Saving all Org-mode buffers...")
  15321. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15322. (when (featurep 'org-id) (org-id-locations-save))
  15323. (message "Saving all Org-mode buffers... done"))
  15324. (defun org-revert-all-org-buffers ()
  15325. "Revert all Org-mode buffers.
  15326. Prompt for confirmation when there are unsaved changes.
  15327. Be sure you know what you are doing before letting this function
  15328. overwrite your changes.
  15329. This function is useful in a setup where one tracks org files
  15330. with a version control system, to revert on one machine after pulling
  15331. changes from another. I believe the procedure must be like this:
  15332. 1. M-x org-save-all-org-buffers
  15333. 2. Pull changes from the other machine, resolve conflicts
  15334. 3. M-x org-revert-all-org-buffers"
  15335. (interactive)
  15336. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15337. (error "Abort"))
  15338. (save-excursion
  15339. (save-window-excursion
  15340. (mapc
  15341. (lambda (b)
  15342. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15343. (with-current-buffer b buffer-file-name))
  15344. (org-pop-to-buffer-same-window b)
  15345. (revert-buffer t 'no-confirm)))
  15346. (buffer-list))
  15347. (when (and (featurep 'org-id) org-id-track-globally)
  15348. (org-id-locations-load)))))
  15349. ;;;; Agenda files
  15350. ;;;###autoload
  15351. (defun org-switchb (&optional arg)
  15352. "Switch between Org buffers.
  15353. With one prefix argument, restrict available buffers to files.
  15354. With two prefix arguments, restrict available buffers to agenda files.
  15355. Defaults to `iswitchb' for buffer name completion.
  15356. Set `org-completion-use-ido' to make it use ido instead."
  15357. (interactive "P")
  15358. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15359. ((equal arg '(16)) (org-buffer-list 'agenda))
  15360. (t (org-buffer-list))))
  15361. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15362. (org-completion-use-ido org-completion-use-ido))
  15363. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15364. (setq org-completion-use-iswitchb t))
  15365. (org-pop-to-buffer-same-window
  15366. (org-icompleting-read "Org buffer: "
  15367. (mapcar 'list (mapcar 'buffer-name blist))
  15368. nil t))))
  15369. ;;; Define some older names previously used for this functionality
  15370. ;;;###autoload
  15371. (defalias 'org-ido-switchb 'org-switchb)
  15372. ;;;###autoload
  15373. (defalias 'org-iswitchb 'org-switchb)
  15374. (defun org-buffer-list (&optional predicate exclude-tmp)
  15375. "Return a list of Org buffers.
  15376. PREDICATE can be `export', `files' or `agenda'.
  15377. export restrict the list to Export buffers.
  15378. files restrict the list to buffers visiting Org files.
  15379. agenda restrict the list to buffers visiting agenda files.
  15380. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15381. (let* ((bfn nil)
  15382. (agenda-files (and (eq predicate 'agenda)
  15383. (mapcar 'file-truename (org-agenda-files t))))
  15384. (filter
  15385. (cond
  15386. ((eq predicate 'files)
  15387. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15388. ((eq predicate 'export)
  15389. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15390. ((eq predicate 'agenda)
  15391. (lambda (b)
  15392. (with-current-buffer b
  15393. (and (derived-mode-p 'org-mode)
  15394. (setq bfn (buffer-file-name b))
  15395. (member (file-truename bfn) agenda-files)))))
  15396. (t (lambda (b) (with-current-buffer b
  15397. (or (derived-mode-p 'org-mode)
  15398. (string-match "\*Org .*Export"
  15399. (buffer-name b)))))))))
  15400. (delq nil
  15401. (mapcar
  15402. (lambda(b)
  15403. (if (and (funcall filter b)
  15404. (or (not exclude-tmp)
  15405. (not (string-match "tmp" (buffer-name b)))))
  15406. b
  15407. nil))
  15408. (buffer-list)))))
  15409. (defun org-agenda-files (&optional unrestricted archives)
  15410. "Get the list of agenda files.
  15411. Optional UNRESTRICTED means return the full list even if a restriction
  15412. is currently in place.
  15413. When ARCHIVES is t, include all archive files that are really being
  15414. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15415. `org-agenda-archives-mode' is t."
  15416. (let ((files
  15417. (cond
  15418. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15419. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15420. ((listp org-agenda-files) org-agenda-files)
  15421. (t (error "Invalid value of `org-agenda-files'")))))
  15422. (setq files (apply 'append
  15423. (mapcar (lambda (f)
  15424. (if (file-directory-p f)
  15425. (directory-files
  15426. f t org-agenda-file-regexp)
  15427. (list f)))
  15428. files)))
  15429. (when org-agenda-skip-unavailable-files
  15430. (setq files (delq nil
  15431. (mapcar (function
  15432. (lambda (file)
  15433. (and (file-readable-p file) file)))
  15434. files))))
  15435. (when (or (eq archives t)
  15436. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15437. (setq files (org-add-archive-files files)))
  15438. files))
  15439. (defun org-agenda-file-p (&optional file)
  15440. "Return non-nil, if FILE is an agenda file.
  15441. If FILE is omitted, use the file associated with the current
  15442. buffer."
  15443. (member (or file (buffer-file-name))
  15444. (org-agenda-files t)))
  15445. (defun org-edit-agenda-file-list ()
  15446. "Edit the list of agenda files.
  15447. Depending on setup, this either uses customize to edit the variable
  15448. `org-agenda-files', or it visits the file that is holding the list. In the
  15449. latter case, the buffer is set up in a way that saving it automatically kills
  15450. the buffer and restores the previous window configuration."
  15451. (interactive)
  15452. (if (stringp org-agenda-files)
  15453. (let ((cw (current-window-configuration)))
  15454. (find-file org-agenda-files)
  15455. (org-set-local 'org-window-configuration cw)
  15456. (org-add-hook 'after-save-hook
  15457. (lambda ()
  15458. (set-window-configuration
  15459. (prog1 org-window-configuration
  15460. (kill-buffer (current-buffer))))
  15461. (org-install-agenda-files-menu)
  15462. (message "New agenda file list installed"))
  15463. nil 'local)
  15464. (message "%s" (substitute-command-keys
  15465. "Edit list and finish with \\[save-buffer]")))
  15466. (customize-variable 'org-agenda-files)))
  15467. (defun org-store-new-agenda-file-list (list)
  15468. "Set new value for the agenda file list and save it correctly."
  15469. (if (stringp org-agenda-files)
  15470. (let ((fe (org-read-agenda-file-list t)) b u)
  15471. (while (setq b (find-buffer-visiting org-agenda-files))
  15472. (kill-buffer b))
  15473. (with-temp-file org-agenda-files
  15474. (insert
  15475. (mapconcat
  15476. (lambda (f) ;; Keep un-expanded entries.
  15477. (if (setq u (assoc f fe))
  15478. (cdr u)
  15479. f))
  15480. list "\n")
  15481. "\n")))
  15482. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15483. (org-insert-mode-line-in-empty-file nil))
  15484. (setq org-agenda-files list)
  15485. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15486. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15487. "Read the list of agenda files from a file.
  15488. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15489. filenames, used by `org-store-new-agenda-file-list' to write back
  15490. un-expanded file names."
  15491. (when (file-directory-p org-agenda-files)
  15492. (error "`org-agenda-files' cannot be a single directory"))
  15493. (when (stringp org-agenda-files)
  15494. (with-temp-buffer
  15495. (insert-file-contents org-agenda-files)
  15496. (mapcar
  15497. (lambda (f)
  15498. (let ((e (expand-file-name (substitute-in-file-name f)
  15499. org-directory)))
  15500. (if pair-with-expansion
  15501. (cons e f)
  15502. e)))
  15503. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15504. ;;;###autoload
  15505. (defun org-cycle-agenda-files ()
  15506. "Cycle through the files in `org-agenda-files'.
  15507. If the current buffer visits an agenda file, find the next one in the list.
  15508. If the current buffer does not, find the first agenda file."
  15509. (interactive)
  15510. (let* ((fs (org-agenda-files t))
  15511. (files (append fs (list (car fs))))
  15512. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15513. file)
  15514. (unless files (error "No agenda files"))
  15515. (catch 'exit
  15516. (while (setq file (pop files))
  15517. (if (equal (file-truename file) tcf)
  15518. (when (car files)
  15519. (find-file (car files))
  15520. (throw 'exit t))))
  15521. (find-file (car fs)))
  15522. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15523. (defun org-agenda-file-to-front (&optional to-end)
  15524. "Move/add the current file to the top of the agenda file list.
  15525. If the file is not present in the list, it is added to the front. If it is
  15526. present, it is moved there. With optional argument TO-END, add/move to the
  15527. end of the list."
  15528. (interactive "P")
  15529. (let ((org-agenda-skip-unavailable-files nil)
  15530. (file-alist (mapcar (lambda (x)
  15531. (cons (file-truename x) x))
  15532. (org-agenda-files t)))
  15533. (ctf (file-truename
  15534. (or buffer-file-name
  15535. (error "Please save the current buffer to a file"))))
  15536. x had)
  15537. (setq x (assoc ctf file-alist) had x)
  15538. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15539. (if to-end
  15540. (setq file-alist (append (delq x file-alist) (list x)))
  15541. (setq file-alist (cons x (delq x file-alist))))
  15542. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15543. (org-install-agenda-files-menu)
  15544. (message "File %s to %s of agenda file list"
  15545. (if had "moved" "added") (if to-end "end" "front"))))
  15546. (defun org-remove-file (&optional file)
  15547. "Remove current file from the list of files in variable `org-agenda-files'.
  15548. These are the files which are being checked for agenda entries.
  15549. Optional argument FILE means use this file instead of the current."
  15550. (interactive)
  15551. (let* ((org-agenda-skip-unavailable-files nil)
  15552. (file (or file buffer-file-name
  15553. (error "Current buffer does not visit a file")))
  15554. (true-file (file-truename file))
  15555. (afile (abbreviate-file-name file))
  15556. (files (delq nil (mapcar
  15557. (lambda (x)
  15558. (if (equal true-file
  15559. (file-truename x))
  15560. nil x))
  15561. (org-agenda-files t)))))
  15562. (if (not (= (length files) (length (org-agenda-files t))))
  15563. (progn
  15564. (org-store-new-agenda-file-list files)
  15565. (org-install-agenda-files-menu)
  15566. (message "Removed file: %s" afile))
  15567. (message "File was not in list: %s (not removed)" afile))))
  15568. (defun org-file-menu-entry (file)
  15569. (vector file (list 'find-file file) t))
  15570. (defun org-check-agenda-file (file)
  15571. "Make sure FILE exists. If not, ask user what to do."
  15572. (when (not (file-exists-p file))
  15573. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  15574. (abbreviate-file-name file))
  15575. (let ((r (downcase (read-char-exclusive))))
  15576. (cond
  15577. ((equal r ?r)
  15578. (org-remove-file file)
  15579. (throw 'nextfile t))
  15580. (t (error "Abort"))))))
  15581. (defun org-get-agenda-file-buffer (file)
  15582. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15583. it to the list of buffers which might be released later."
  15584. (let ((buf (org-find-base-buffer-visiting file)))
  15585. (if buf
  15586. buf ; just return it
  15587. ;; Make a new buffer and remember it
  15588. (setq buf (find-file-noselect file))
  15589. (if buf (push buf org-agenda-new-buffers))
  15590. buf)))
  15591. (defun org-release-buffers (blist)
  15592. "Release all buffers in list, asking the user for confirmation when needed.
  15593. When a buffer is unmodified, it is just killed. When modified, it is saved
  15594. \(if the user agrees) and then killed."
  15595. (let (buf file)
  15596. (while (setq buf (pop blist))
  15597. (setq file (buffer-file-name buf))
  15598. (when (and (buffer-modified-p buf)
  15599. file
  15600. (y-or-n-p (format "Save file %s? " file)))
  15601. (with-current-buffer buf (save-buffer)))
  15602. (kill-buffer buf))))
  15603. (defun org-agenda-prepare-buffers (files)
  15604. "Create buffers for all agenda files, protect archived trees and comments."
  15605. (interactive)
  15606. (let ((pa '(:org-archived t))
  15607. (pc '(:org-comment t))
  15608. (pall '(:org-archived t :org-comment t))
  15609. (inhibit-read-only t)
  15610. (org-inhibit-startup org-agenda-inhibit-startup)
  15611. (rea (concat ":" org-archive-tag ":"))
  15612. bmp file re)
  15613. (save-excursion
  15614. (save-restriction
  15615. (while (setq file (pop files))
  15616. (catch 'nextfile
  15617. (if (bufferp file)
  15618. (set-buffer file)
  15619. (org-check-agenda-file file)
  15620. (set-buffer (org-get-agenda-file-buffer file)))
  15621. (widen)
  15622. (setq bmp (buffer-modified-p))
  15623. (org-refresh-category-properties)
  15624. (org-refresh-properties org-effort-property 'org-effort)
  15625. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  15626. (setq org-todo-keywords-for-agenda
  15627. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15628. (setq org-done-keywords-for-agenda
  15629. (append org-done-keywords-for-agenda org-done-keywords))
  15630. (setq org-todo-keyword-alist-for-agenda
  15631. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15632. (setq org-drawers-for-agenda
  15633. (append org-drawers-for-agenda org-drawers))
  15634. (setq org-tag-alist-for-agenda
  15635. (append org-tag-alist-for-agenda org-tag-alist))
  15636. (save-excursion
  15637. (remove-text-properties (point-min) (point-max) pall)
  15638. (when org-agenda-skip-archived-trees
  15639. (goto-char (point-min))
  15640. (while (re-search-forward rea nil t)
  15641. (if (org-at-heading-p t)
  15642. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15643. (goto-char (point-min))
  15644. (setq re (format org-heading-keyword-regexp-format
  15645. org-comment-string))
  15646. (while (re-search-forward re nil t)
  15647. (add-text-properties
  15648. (match-beginning 0) (org-end-of-subtree t) pc)))
  15649. (set-buffer-modified-p bmp)))))
  15650. (setq org-todo-keywords-for-agenda
  15651. (org-uniquify org-todo-keywords-for-agenda))
  15652. (setq org-todo-keyword-alist-for-agenda
  15653. (org-uniquify org-todo-keyword-alist-for-agenda)
  15654. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15655. ;;;; Embedded LaTeX
  15656. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15657. "Keymap for the minor `org-cdlatex-mode'.")
  15658. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15659. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15660. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15661. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15662. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15663. (defvar org-cdlatex-texmathp-advice-is-done nil
  15664. "Flag remembering if we have applied the advice to texmathp already.")
  15665. (define-minor-mode org-cdlatex-mode
  15666. "Toggle the minor `org-cdlatex-mode'.
  15667. This mode supports entering LaTeX environment and math in LaTeX fragments
  15668. in Org-mode.
  15669. \\{org-cdlatex-mode-map}"
  15670. nil " OCDL" nil
  15671. (when org-cdlatex-mode
  15672. (require 'cdlatex)
  15673. (run-hooks 'cdlatex-mode-hook)
  15674. (cdlatex-compute-tables))
  15675. (unless org-cdlatex-texmathp-advice-is-done
  15676. (setq org-cdlatex-texmathp-advice-is-done t)
  15677. (defadvice texmathp (around org-math-always-on activate)
  15678. "Always return t in org-mode buffers.
  15679. This is because we want to insert math symbols without dollars even outside
  15680. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15681. an embedded LaTeX fragment, let texmathp do its job.
  15682. \\[org-cdlatex-mode-map]"
  15683. (interactive)
  15684. (let (p)
  15685. (cond
  15686. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15687. ((eq this-command 'cdlatex-math-symbol)
  15688. (setq ad-return-value t
  15689. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15690. (t
  15691. (let ((p (org-inside-LaTeX-fragment-p)))
  15692. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15693. (setq ad-return-value t
  15694. texmathp-why '("Org-mode embedded math" . 0))
  15695. (if p ad-do-it)))))))))
  15696. (defun turn-on-org-cdlatex ()
  15697. "Unconditionally turn on `org-cdlatex-mode'."
  15698. (org-cdlatex-mode 1))
  15699. (defun org-inside-LaTeX-fragment-p ()
  15700. "Test if point is inside a LaTeX fragment.
  15701. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15702. sequence appearing also before point.
  15703. Even though the matchers for math are configurable, this function assumes
  15704. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15705. delimiters are skipped when they have been removed by customization.
  15706. The return value is nil, or a cons cell with the delimiter and the
  15707. position of this delimiter.
  15708. This function does a reasonably good job, but can locally be fooled by
  15709. for example currency specifications. For example it will assume being in
  15710. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15711. fragments that are properly closed, but during editing, we have to live
  15712. with the uncertainty caused by missing closing delimiters. This function
  15713. looks only before point, not after."
  15714. (catch 'exit
  15715. (let ((pos (point))
  15716. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15717. (lim (progn
  15718. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15719. (point)))
  15720. dd-on str (start 0) m re)
  15721. (goto-char pos)
  15722. (when dodollar
  15723. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15724. re (nth 1 (assoc "$" org-latex-regexps)))
  15725. (while (string-match re str start)
  15726. (cond
  15727. ((= (match-end 0) (length str))
  15728. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15729. ((= (match-end 0) (- (length str) 5))
  15730. (throw 'exit nil))
  15731. (t (setq start (match-end 0))))))
  15732. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15733. (goto-char pos)
  15734. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15735. (and (match-beginning 2) (throw 'exit nil))
  15736. ;; count $$
  15737. (while (re-search-backward "\\$\\$" lim t)
  15738. (setq dd-on (not dd-on)))
  15739. (goto-char pos)
  15740. (if dd-on (cons "$$" m))))))
  15741. (defun org-inside-latex-macro-p ()
  15742. "Is point inside a LaTeX macro or its arguments?"
  15743. (save-match-data
  15744. (org-in-regexp
  15745. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15746. (defun org-try-cdlatex-tab ()
  15747. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15748. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15749. - inside a LaTeX fragment, or
  15750. - after the first word in a line, where an abbreviation expansion could
  15751. insert a LaTeX environment."
  15752. (when org-cdlatex-mode
  15753. (cond
  15754. ;; Before any word on the line: No expansion possible.
  15755. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15756. ;; Just after first word on the line: Expand it. Make sure it
  15757. ;; cannot happen on headlines, though.
  15758. ((save-excursion
  15759. (skip-chars-backward "a-zA-Z0-9*")
  15760. (skip-chars-backward " \t")
  15761. (and (bolp) (not (org-at-heading-p))))
  15762. (cdlatex-tab) t)
  15763. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15764. (defun org-cdlatex-underscore-caret (&optional arg)
  15765. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15766. Revert to the normal definition outside of these fragments."
  15767. (interactive "P")
  15768. (if (org-inside-LaTeX-fragment-p)
  15769. (call-interactively 'cdlatex-sub-superscript)
  15770. (let (org-cdlatex-mode)
  15771. (call-interactively (key-binding (vector last-input-event))))))
  15772. (defun org-cdlatex-math-modify (&optional arg)
  15773. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15774. Revert to the normal definition outside of these fragments."
  15775. (interactive "P")
  15776. (if (org-inside-LaTeX-fragment-p)
  15777. (call-interactively 'cdlatex-math-modify)
  15778. (let (org-cdlatex-mode)
  15779. (call-interactively (key-binding (vector last-input-event))))))
  15780. (defvar org-latex-fragment-image-overlays nil
  15781. "List of overlays carrying the images of latex fragments.")
  15782. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15783. (defun org-remove-latex-fragment-image-overlays ()
  15784. "Remove all overlays with LaTeX fragment images in current buffer."
  15785. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15786. (setq org-latex-fragment-image-overlays nil))
  15787. (defun org-preview-latex-fragment (&optional subtree)
  15788. "Preview the LaTeX fragment at point, or all locally or globally.
  15789. If the cursor is in a LaTeX fragment, create the image and overlay
  15790. it over the source code. If there is no fragment at point, display
  15791. all fragments in the current text, from one headline to the next. With
  15792. prefix SUBTREE, display all fragments in the current subtree. With a
  15793. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15794. the cursor is before the first headline,
  15795. display all fragments in the buffer.
  15796. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15797. (interactive "P")
  15798. (unless buffer-file-name
  15799. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15800. (org-remove-latex-fragment-image-overlays)
  15801. (save-excursion
  15802. (save-restriction
  15803. (let (beg end at msg)
  15804. (cond
  15805. ((or (equal subtree '(16))
  15806. (not (save-excursion
  15807. (re-search-backward org-outline-regexp-bol nil t))))
  15808. (setq beg (point-min) end (point-max)
  15809. msg "Creating images for buffer...%s"))
  15810. ((equal subtree '(4))
  15811. (org-back-to-heading)
  15812. (setq beg (point) end (org-end-of-subtree t)
  15813. msg "Creating images for subtree...%s"))
  15814. (t
  15815. (if (setq at (org-inside-LaTeX-fragment-p))
  15816. (goto-char (max (point-min) (- (cdr at) 2)))
  15817. (org-back-to-heading))
  15818. (setq beg (point) end (progn (outline-next-heading) (point))
  15819. msg (if at "Creating image...%s"
  15820. "Creating images for entry...%s"))))
  15821. (message msg "")
  15822. (narrow-to-region beg end)
  15823. (goto-char beg)
  15824. (org-format-latex
  15825. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  15826. (file-name-nondirectory
  15827. buffer-file-name)))
  15828. default-directory 'overlays msg at 'forbuffer
  15829. org-latex-create-formula-image-program)
  15830. (message msg "done. Use `C-c C-c' to remove images.")))))
  15831. (defvar org-latex-regexps
  15832. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15833. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15834. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15835. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15836. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15837. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15838. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15839. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15840. "Regular expressions for matching embedded LaTeX.")
  15841. (defvar org-export-have-math nil) ;; dynamic scoping
  15842. (defun org-format-latex (prefix &optional dir overlays msg at
  15843. forbuffer processing-type)
  15844. "Replace LaTeX fragments with links to an image, and produce images.
  15845. Some of the options can be changed using the variable
  15846. `org-format-latex-options'."
  15847. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15848. (let* ((prefixnodir (file-name-nondirectory prefix))
  15849. (absprefix (expand-file-name prefix dir))
  15850. (todir (file-name-directory absprefix))
  15851. (opt org-format-latex-options)
  15852. (matchers (plist-get opt :matchers))
  15853. (re-list org-latex-regexps)
  15854. (org-format-latex-header-extra
  15855. (plist-get (org-infile-export-plist) :latex-header-extra))
  15856. (cnt 0) txt hash link beg end re e checkdir
  15857. executables-checked string
  15858. m n block-type block linkfile movefile ov)
  15859. ;; Check the different regular expressions
  15860. (while (setq e (pop re-list))
  15861. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15862. block (if block-type "\n\n" ""))
  15863. (when (member m matchers)
  15864. (goto-char (point-min))
  15865. (while (re-search-forward re nil t)
  15866. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15867. (not (get-text-property (match-beginning n)
  15868. 'org-protected))
  15869. (or (not overlays)
  15870. (not (eq (get-char-property (match-beginning n)
  15871. 'org-overlay-type)
  15872. 'org-latex-overlay))))
  15873. (setq org-export-have-math t)
  15874. (cond
  15875. ((eq processing-type 'verbatim)
  15876. ;; Leave the text verbatim, just protect it
  15877. (add-text-properties (match-beginning n) (match-end n)
  15878. '(org-protected t)))
  15879. ((eq processing-type 'mathjax)
  15880. ;; Prepare for MathJax processing
  15881. (setq string (match-string n))
  15882. (if (member m '("$" "$1"))
  15883. (save-excursion
  15884. (delete-region (match-beginning n) (match-end n))
  15885. (goto-char (match-beginning n))
  15886. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15887. "\\)")
  15888. '(org-protected t))))
  15889. (add-text-properties (match-beginning n) (match-end n)
  15890. '(org-protected t))))
  15891. ((or (eq processing-type 'dvipng)
  15892. (eq processing-type 'imagemagick))
  15893. ;; Process to an image
  15894. (setq txt (match-string n)
  15895. beg (match-beginning n) end (match-end n)
  15896. cnt (1+ cnt))
  15897. (let (print-length print-level) ; make sure full list is printed
  15898. (setq hash (sha1 (prin1-to-string
  15899. (list org-format-latex-header
  15900. org-format-latex-header-extra
  15901. org-export-latex-default-packages-alist
  15902. org-export-latex-packages-alist
  15903. org-format-latex-options
  15904. forbuffer txt)))
  15905. linkfile (format "%s_%s.png" prefix hash)
  15906. movefile (format "%s_%s.png" absprefix hash)))
  15907. (setq link (concat block "[[file:" linkfile "]]" block))
  15908. (if msg (message msg cnt))
  15909. (goto-char beg)
  15910. (unless checkdir ; make sure the directory exists
  15911. (setq checkdir t)
  15912. (or (file-directory-p todir) (make-directory todir t)))
  15913. (cond
  15914. ((eq processing-type 'dvipng)
  15915. (unless executables-checked
  15916. (org-check-external-command
  15917. "latex" "needed to convert LaTeX fragments to images")
  15918. (org-check-external-command
  15919. "dvipng" "needed to convert LaTeX fragments to images")
  15920. (setq executables-checked t))
  15921. (unless (file-exists-p movefile)
  15922. (org-create-formula-image-with-dvipng
  15923. txt movefile opt forbuffer)))
  15924. ((eq processing-type 'imagemagick)
  15925. (unless executables-checked
  15926. (org-check-external-command
  15927. "convert" "you need to install imagemagick")
  15928. (setq executables-checked t))
  15929. (unless (file-exists-p movefile)
  15930. (org-create-formula-image-with-imagemagick
  15931. txt movefile opt forbuffer))))
  15932. (if overlays
  15933. (progn
  15934. (mapc (lambda (o)
  15935. (if (eq (overlay-get o 'org-overlay-type)
  15936. 'org-latex-overlay)
  15937. (delete-overlay o)))
  15938. (overlays-in beg end))
  15939. (setq ov (make-overlay beg end))
  15940. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15941. (if (featurep 'xemacs)
  15942. (progn
  15943. (overlay-put ov 'invisible t)
  15944. (overlay-put
  15945. ov 'end-glyph
  15946. (make-glyph (vector 'png :file movefile))))
  15947. (overlay-put
  15948. ov 'display
  15949. (list 'image :type 'png :file movefile :ascent 'center)))
  15950. (push ov org-latex-fragment-image-overlays)
  15951. (goto-char end))
  15952. (delete-region beg end)
  15953. (insert (org-add-props link
  15954. (list 'org-latex-src
  15955. (replace-regexp-in-string
  15956. "\"" "" txt)
  15957. 'org-latex-src-embed-type
  15958. (if block-type 'paragraph 'character))))))
  15959. ((eq processing-type 'mathml)
  15960. ;; Process to MathML
  15961. (unless executables-checked
  15962. (unless (save-match-data (org-format-latex-mathml-available-p))
  15963. (error "LaTeX to MathML converter not configured"))
  15964. (setq executables-checked t))
  15965. (setq txt (match-string n)
  15966. beg (match-beginning n) end (match-end n)
  15967. cnt (1+ cnt))
  15968. (if msg (message msg cnt))
  15969. (goto-char beg)
  15970. (delete-region beg end)
  15971. (insert (org-format-latex-as-mathml
  15972. txt block-type prefix dir)))
  15973. (t
  15974. (error "Unknown conversion type %s for latex fragments"
  15975. processing-type)))))))))
  15976. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15977. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15978. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15979. sucessful, return the portion between \"<math...> </math>\"
  15980. elements otherwise return nil. When MATHML-FILE is specified,
  15981. write the results in to that file. When invoked as an
  15982. interactive command, prompt for LATEX-FRAG, with initial value
  15983. set to the current active region and echo the results for user
  15984. inspection."
  15985. (interactive (list (let ((frag (when (org-region-active-p)
  15986. (buffer-substring-no-properties
  15987. (region-beginning) (region-end)))))
  15988. (read-string "LaTeX Fragment: " frag nil frag))))
  15989. (unless latex-frag (error "Invalid latex-frag"))
  15990. (let* ((tmp-in-file (file-relative-name
  15991. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15992. (ignore (write-region latex-frag nil tmp-in-file))
  15993. (tmp-out-file (file-relative-name
  15994. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15995. (cmd (format-spec
  15996. org-latex-to-mathml-convert-command
  15997. `((?j . ,(shell-quote-argument
  15998. (expand-file-name org-latex-to-mathml-jar-file)))
  15999. (?I . ,(shell-quote-argument tmp-in-file))
  16000. (?o . ,(shell-quote-argument tmp-out-file)))))
  16001. mathml shell-command-output)
  16002. (when (org-called-interactively-p 'any)
  16003. (unless (org-format-latex-mathml-available-p)
  16004. (error "LaTeX to MathML converter not configured")))
  16005. (message "Running %s" cmd)
  16006. (setq shell-command-output (shell-command-to-string cmd))
  16007. (setq mathml
  16008. (when (file-readable-p tmp-out-file)
  16009. (with-current-buffer (find-file-noselect tmp-out-file t)
  16010. (goto-char (point-min))
  16011. (when (re-search-forward
  16012. (concat
  16013. (regexp-quote
  16014. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16015. "\\(.\\|\n\\)*"
  16016. (regexp-quote "</math>")) nil t)
  16017. (prog1 (match-string 0) (kill-buffer))))))
  16018. (cond
  16019. (mathml
  16020. (setq mathml
  16021. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16022. (when mathml-file
  16023. (write-region mathml nil mathml-file))
  16024. (when (org-called-interactively-p 'any)
  16025. (message mathml)))
  16026. ((message "LaTeX to MathML conversion failed")
  16027. (message shell-command-output)))
  16028. (delete-file tmp-in-file)
  16029. (when (file-exists-p tmp-out-file)
  16030. (delete-file tmp-out-file))
  16031. mathml))
  16032. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16033. prefix &optional dir)
  16034. "Use `org-create-math-formula' but check local cache first."
  16035. (let* ((absprefix (expand-file-name prefix dir))
  16036. (print-length nil) (print-level nil)
  16037. (formula-id (concat
  16038. "formula-"
  16039. (sha1
  16040. (prin1-to-string
  16041. (list latex-frag
  16042. org-latex-to-mathml-convert-command)))))
  16043. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16044. (formula-cache-dir (file-name-directory formula-cache)))
  16045. (unless (file-directory-p formula-cache-dir)
  16046. (make-directory formula-cache-dir t))
  16047. (unless (file-exists-p formula-cache)
  16048. (org-create-math-formula latex-frag formula-cache))
  16049. (if (file-exists-p formula-cache)
  16050. ;; Successful conversion. Return the link to MathML file.
  16051. (org-add-props
  16052. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16053. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16054. 'org-latex-src-embed-type (if latex-frag-type
  16055. 'paragraph 'character)))
  16056. ;; Failed conversion. Return the LaTeX fragment verbatim
  16057. (add-text-properties
  16058. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  16059. latex-frag)))
  16060. ;; This function borrows from Ganesh Swami's latex2png.el
  16061. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16062. "This calls dvipng."
  16063. (require 'org-latex)
  16064. (let* ((tmpdir (if (featurep 'xemacs)
  16065. (temp-directory)
  16066. temporary-file-directory))
  16067. (texfilebase (make-temp-name
  16068. (expand-file-name "orgtex" tmpdir)))
  16069. (texfile (concat texfilebase ".tex"))
  16070. (dvifile (concat texfilebase ".dvi"))
  16071. (pngfile (concat texfilebase ".png"))
  16072. (fnh (if (featurep 'xemacs)
  16073. (font-height (face-font 'default))
  16074. (face-attribute 'default :height nil)))
  16075. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16076. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16077. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16078. "Black"))
  16079. (bg (or (plist-get options (if buffer :background :html-background))
  16080. "Transparent")))
  16081. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  16082. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  16083. (with-temp-file texfile
  16084. (insert (org-splice-latex-header
  16085. org-format-latex-header
  16086. org-export-latex-default-packages-alist
  16087. org-export-latex-packages-alist t
  16088. org-format-latex-header-extra))
  16089. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  16090. (require 'org-latex)
  16091. (org-export-latex-fix-inputenc))
  16092. (let ((dir default-directory))
  16093. (condition-case nil
  16094. (progn
  16095. (cd tmpdir)
  16096. (call-process "latex" nil nil nil texfile))
  16097. (error nil))
  16098. (cd dir))
  16099. (if (not (file-exists-p dvifile))
  16100. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16101. (condition-case nil
  16102. (if (featurep 'xemacs)
  16103. (call-process "dvipng" nil nil nil
  16104. "-fg" fg "-bg" bg
  16105. "-T" "tight"
  16106. "-o" pngfile
  16107. dvifile)
  16108. (call-process "dvipng" nil nil nil
  16109. "-fg" fg "-bg" bg
  16110. "-D" dpi
  16111. ;;"-x" scale "-y" scale
  16112. "-T" "tight"
  16113. "-o" pngfile
  16114. dvifile))
  16115. (error nil))
  16116. (if (not (file-exists-p pngfile))
  16117. (if org-format-latex-signal-error
  16118. (error "Failed to create png file from %s" texfile)
  16119. (message "Failed to create png file from %s" texfile)
  16120. nil)
  16121. ;; Use the requested file name and clean up
  16122. (copy-file pngfile tofile 'replace)
  16123. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16124. (if (file-exists-p (concat texfilebase e))
  16125. (delete-file (concat texfilebase e))))
  16126. pngfile))))
  16127. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  16128. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16129. "This calls convert, which is included into imagemagick."
  16130. (require 'org-latex)
  16131. (let* ((tmpdir (if (featurep 'xemacs)
  16132. (temp-directory)
  16133. temporary-file-directory))
  16134. (texfilebase (make-temp-name
  16135. (expand-file-name "orgtex" tmpdir)))
  16136. (texfile (concat texfilebase ".tex"))
  16137. (pdffile (concat texfilebase ".pdf"))
  16138. (pngfile (concat texfilebase ".png"))
  16139. (fnh (if (featurep 'xemacs)
  16140. (font-height (face-font 'default))
  16141. (face-attribute 'default :height nil)))
  16142. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16143. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16144. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16145. "black"))
  16146. (bg (or (plist-get options (if buffer :background :html-background))
  16147. "white")))
  16148. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16149. (setq fg (org-latex-color-format fg)))
  16150. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16151. (setq bg (org-latex-color-format
  16152. (if (string= bg "Transparent")(setq bg "white")))))
  16153. (with-temp-file texfile
  16154. (insert (org-splice-latex-header
  16155. org-format-latex-header
  16156. org-export-latex-default-packages-alist
  16157. org-export-latex-packages-alist t
  16158. org-format-latex-header-extra))
  16159. (insert "\n\\begin{document}\n"
  16160. "\\definecolor{fg}{rgb}{" fg "}\n"
  16161. "\\definecolor{bg}{rgb}{" bg "}\n"
  16162. "\n\\pagecolor{bg}\n"
  16163. "\n{\\color{fg}\n"
  16164. string
  16165. "\n}\n"
  16166. "\n\\end{document}\n" )
  16167. (require 'org-latex)
  16168. (org-export-latex-fix-inputenc))
  16169. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16170. (condition-case nil
  16171. (progn
  16172. (cd tmpdir)
  16173. (setq cmds org-latex-to-pdf-process)
  16174. (while cmds
  16175. (setq latex-frags-cmds (pop cmds))
  16176. (if (listp latex-frags-cmds)
  16177. (setq cmds nil)
  16178. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  16179. (while latex-frags-cmds
  16180. (setq cmd (pop latex-frags-cmds))
  16181. (while (string-match "%b" cmd)
  16182. (setq cmd (replace-match
  16183. (save-match-data
  16184. (shell-quote-argument texfile))
  16185. t t cmd)))
  16186. (while (string-match "%f" cmd)
  16187. (setq cmd (replace-match
  16188. (save-match-data
  16189. (shell-quote-argument (file-name-nondirectory texfile)))
  16190. t t cmd)))
  16191. (while (string-match "%o" cmd)
  16192. (setq cmd (replace-match
  16193. (save-match-data
  16194. (shell-quote-argument (file-name-directory texfile)))
  16195. t t cmd)))
  16196. (setq cmd (split-string cmd))
  16197. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16198. (error nil))
  16199. (cd dir))
  16200. (if (not (file-exists-p pdffile))
  16201. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16202. (condition-case nil
  16203. (if (featurep 'xemacs)
  16204. (call-process "convert" nil nil nil
  16205. "-density" "96"
  16206. "-trim"
  16207. "-antialias"
  16208. pdffile
  16209. "-quality" "100"
  16210. ;; "-sharpen" "0x1.0"
  16211. pngfile)
  16212. (call-process "convert" nil nil nil
  16213. "-density" dpi
  16214. "-trim"
  16215. "-antialias"
  16216. pdffile
  16217. "-quality" "100"
  16218. ; "-sharpen" "0x1.0"
  16219. pngfile))
  16220. (error nil))
  16221. (if (not (file-exists-p pngfile))
  16222. (if org-format-latex-signal-error
  16223. (error "Failed to create png file from %s" texfile)
  16224. (message "Failed to create png file from %s" texfile)
  16225. nil)
  16226. ;; Use the requested file name and clean up
  16227. (copy-file pngfile tofile 'replace)
  16228. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16229. (if (file-exists-p (concat texfilebase e))
  16230. (delete-file (concat texfilebase e))))
  16231. pngfile))))
  16232. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16233. "Fill a LaTeX header template TPL.
  16234. In the template, the following place holders will be recognized:
  16235. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16236. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16237. [PACKAGES] \\usepackage statements for PKG
  16238. [NO-PACKAGES] do not include PKG
  16239. [EXTRA] the string EXTRA
  16240. [NO-EXTRA] do not include EXTRA
  16241. For backward compatibility, if both the positive and the negative place
  16242. holder is missing, the positive one (without the \"NO-\") will be
  16243. assumed to be present at the end of the template.
  16244. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16245. EXTRA is a string.
  16246. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16247. (let (rpl (end ""))
  16248. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16249. (setq rpl (if (or (match-end 1) (not def-pkg))
  16250. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16251. tpl (replace-match rpl t t tpl))
  16252. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16253. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16254. (setq rpl (if (or (match-end 1) (not pkg))
  16255. "" (org-latex-packages-to-string pkg snippets-p t))
  16256. tpl (replace-match rpl t t tpl))
  16257. (if pkg (setq end
  16258. (concat end "\n"
  16259. (org-latex-packages-to-string pkg snippets-p)))))
  16260. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16261. (setq rpl (if (or (match-end 1) (not extra))
  16262. "" (concat extra "\n"))
  16263. tpl (replace-match rpl t t tpl))
  16264. (if (and extra (string-match "\\S-" extra))
  16265. (setq end (concat end "\n" extra))))
  16266. (if (string-match "\\S-" end)
  16267. (concat tpl "\n" end)
  16268. tpl)))
  16269. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16270. "Turn an alist of packages into a string with the \\usepackage macros."
  16271. (setq pkg (mapconcat (lambda(p)
  16272. (cond
  16273. ((stringp p) p)
  16274. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16275. (format "%% Package %s omitted" (cadr p)))
  16276. ((equal "" (car p))
  16277. (format "\\usepackage{%s}" (cadr p)))
  16278. (t
  16279. (format "\\usepackage[%s]{%s}"
  16280. (car p) (cadr p)))))
  16281. pkg
  16282. "\n"))
  16283. (if newline (concat pkg "\n") pkg))
  16284. (defun org-dvipng-color (attr)
  16285. "Return a RGB color specification for dvipng."
  16286. (apply 'format "rgb %s %s %s"
  16287. (mapcar 'org-normalize-color
  16288. (if (featurep 'xemacs)
  16289. (color-rgb-components
  16290. (face-property 'default
  16291. (cond ((eq attr :foreground) 'foreground)
  16292. ((eq attr :background) 'background))))
  16293. (color-values (face-attribute 'default attr nil))))))
  16294. (defun org-latex-color (attr)
  16295. "Return a RGB color for the LaTeX color package."
  16296. (apply 'format "%s,%s,%s"
  16297. (mapcar 'org-normalize-color
  16298. (if (featurep 'xemacs)
  16299. (color-rgb-components
  16300. (face-property 'default
  16301. (cond ((eq attr :foreground) 'foreground)
  16302. ((eq attr :background) 'background))))
  16303. (color-values (face-attribute 'default attr nil))))))
  16304. (defun org-latex-color-format (color-name)
  16305. "Convert COLOR-NAME to a RGB color value."
  16306. (apply 'format "%s,%s,%s"
  16307. (mapcar 'org-normalize-color
  16308. (color-values color-name))))
  16309. (defun org-normalize-color (value)
  16310. "Return string to be used as color value for an RGB component."
  16311. (format "%g" (/ value 65535.0)))
  16312. ;; Image display
  16313. (defvar org-inline-image-overlays nil)
  16314. (make-variable-buffer-local 'org-inline-image-overlays)
  16315. (defun org-toggle-inline-images (&optional include-linked)
  16316. "Toggle the display of inline images.
  16317. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16318. (interactive "P")
  16319. (if org-inline-image-overlays
  16320. (progn
  16321. (org-remove-inline-images)
  16322. (message "Inline image display turned off"))
  16323. (org-display-inline-images include-linked)
  16324. (if org-inline-image-overlays
  16325. (message "%d images displayed inline"
  16326. (length org-inline-image-overlays))
  16327. (message "No images to display inline"))))
  16328. (defun org-redisplay-inline-images ()
  16329. "Refresh the display of inline images."
  16330. (interactive)
  16331. (if (not org-inline-image-overlays)
  16332. (org-toggle-inline-images)
  16333. (org-toggle-inline-images)
  16334. (org-toggle-inline-images)))
  16335. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16336. "Display inline images.
  16337. Normally only links without a description part are inlined, because this
  16338. is how it will work for export. When INCLUDE-LINKED is set, also links
  16339. with a description part will be inlined. This can be nice for a quick
  16340. look at those images, but it does not reflect what exported files will look
  16341. like.
  16342. When REFRESH is set, refresh existing images between BEG and END.
  16343. This will create new image displays only if necessary.
  16344. BEG and END default to the buffer boundaries."
  16345. (interactive "P")
  16346. (unless refresh
  16347. (org-remove-inline-images)
  16348. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16349. (save-excursion
  16350. (save-restriction
  16351. (widen)
  16352. (setq beg (or beg (point-min)) end (or end (point-max)))
  16353. (goto-char beg)
  16354. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16355. (substring (org-image-file-name-regexp) 0 -2)
  16356. "\\)\\]" (if include-linked "" "\\]")))
  16357. old file ov img)
  16358. (while (re-search-forward re end t)
  16359. (setq old (get-char-property-and-overlay (match-beginning 1)
  16360. 'org-image-overlay))
  16361. (setq file (expand-file-name
  16362. (concat (or (match-string 3) "") (match-string 4))))
  16363. (when (file-exists-p file)
  16364. (if (and (car-safe old) refresh)
  16365. (image-refresh (overlay-get (cdr old) 'display))
  16366. (setq img (save-match-data (create-image file)))
  16367. (when img
  16368. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16369. (overlay-put ov 'display img)
  16370. (overlay-put ov 'face 'default)
  16371. (overlay-put ov 'org-image-overlay t)
  16372. (overlay-put ov 'modification-hooks
  16373. (list 'org-display-inline-remove-overlay))
  16374. (push ov org-inline-image-overlays)))))))))
  16375. (define-obsolete-function-alias
  16376. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16377. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16378. "Remove inline-display overlay if a corresponding region is modified."
  16379. (let ((inhibit-modification-hooks t))
  16380. (when (and ov after)
  16381. (delete ov org-inline-image-overlays)
  16382. (delete-overlay ov))))
  16383. (defun org-remove-inline-images ()
  16384. "Remove inline display of images."
  16385. (interactive)
  16386. (mapc 'delete-overlay org-inline-image-overlays)
  16387. (setq org-inline-image-overlays nil))
  16388. ;;;; Key bindings
  16389. ;; Outline functions from `outline-mode-prefix-map'
  16390. ;; that can be remapped in Org:
  16391. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16392. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16393. (define-key org-mode-map [remap outline-forward-same-level]
  16394. 'org-forward-heading-same-level)
  16395. (define-key org-mode-map [remap outline-backward-same-level]
  16396. 'org-backward-heading-same-level)
  16397. (define-key org-mode-map [remap show-branches]
  16398. 'org-kill-note-or-show-branches)
  16399. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16400. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16401. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16402. ;; Outline functions from `outline-mode-prefix-map' that can not
  16403. ;; be remapped in Org:
  16404. ;;
  16405. ;; - the column "key binding" shows whether the Outline function is still
  16406. ;; available in Org mode on the same key that it has been bound to in
  16407. ;; Outline mode:
  16408. ;; - "overridden": key used for a different functionality in Org mode
  16409. ;; - else: key still bound to the same Outline function in Org mode
  16410. ;;
  16411. ;; | Outline function | key binding | Org replacement |
  16412. ;; |------------------------------------+-------------+-----------------------|
  16413. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16414. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16415. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16416. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16417. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16418. ;; | `show-entry' | overridden | no replacement |
  16419. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16420. ;; | `show-branches' | `C-c C-k' | still same function |
  16421. ;; | `show-subtree' | overridden | visibility cycling |
  16422. ;; | `show-all' | overridden | no replacement |
  16423. ;; | `hide-subtree' | overridden | visibility cycling |
  16424. ;; | `hide-body' | overridden | no replacement |
  16425. ;; | `hide-entry' | overridden | visibility cycling |
  16426. ;; | `hide-leaves' | overridden | no replacement |
  16427. ;; | `hide-sublevels' | overridden | no replacement |
  16428. ;; | `hide-other' | overridden | no replacement |
  16429. ;; Make `C-c C-x' a prefix key
  16430. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16431. ;; TAB key with modifiers
  16432. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16433. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16434. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16435. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16436. ;; The following line is necessary under Suse GNU/Linux
  16437. (unless (featurep 'xemacs)
  16438. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16439. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16440. (define-key org-mode-map [backtab] 'org-shifttab)
  16441. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16442. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16443. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16444. ;; Cursor keys with modifiers
  16445. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16446. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16447. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16448. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16449. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16450. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16451. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16452. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16453. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16454. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16455. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16456. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16457. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16458. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16459. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16460. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16461. ;; Babel keys
  16462. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16463. (mapc (lambda (pair)
  16464. (define-key org-babel-map (car pair) (cdr pair)))
  16465. org-babel-key-bindings)
  16466. ;;; Extra keys for tty access.
  16467. ;; We only set them when really needed because otherwise the
  16468. ;; menus don't show the simple keys
  16469. (when (or org-use-extra-keys
  16470. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16471. (not window-system))
  16472. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16473. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16474. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16475. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16476. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16477. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16478. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16479. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16480. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16481. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16482. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16483. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16484. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16485. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16486. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16487. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16488. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16489. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16490. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16491. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16492. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16493. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16494. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16495. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16496. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16497. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16498. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16499. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16500. ;; All the other keys
  16501. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16502. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16503. (if (boundp 'narrow-map)
  16504. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16505. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16506. (if (boundp 'narrow-map)
  16507. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16508. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16509. (if (boundp 'narrow-map)
  16510. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16511. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16512. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16513. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16514. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16515. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16516. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16517. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16518. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16519. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16520. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16521. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16522. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16523. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16524. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16525. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16526. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16527. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16528. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16529. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16530. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16531. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16532. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16533. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16534. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16535. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16536. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16537. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16538. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16539. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16540. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16541. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16542. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16543. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16544. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16545. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16546. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16547. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16548. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16549. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16550. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16551. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16552. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16553. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16554. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16555. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16556. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16557. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16558. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16559. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16560. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16561. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16562. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16563. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16564. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16565. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16566. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16567. (org-defkey org-mode-map "\C-m" 'org-return)
  16568. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16569. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16570. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16571. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16572. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16573. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16574. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16575. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16576. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16577. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16578. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16579. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16580. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16581. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16582. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16583. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16584. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16585. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16586. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16587. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16588. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  16589. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16590. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16591. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16592. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16593. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16594. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16595. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16596. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16597. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16598. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16599. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16600. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  16601. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16602. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16603. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16604. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16605. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16606. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  16607. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16608. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16609. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16610. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16611. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  16612. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16613. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16614. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16615. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16616. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16617. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16618. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16619. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16620. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16621. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16622. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16623. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16624. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16625. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16626. (when (featurep 'xemacs)
  16627. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16628. (defconst org-speed-commands-default
  16629. '(
  16630. ("Outline Navigation")
  16631. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16632. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16633. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  16634. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  16635. ("u" . (org-speed-move-safe 'outline-up-heading))
  16636. ("j" . org-goto)
  16637. ("g" . (org-refile t))
  16638. ("Outline Visibility")
  16639. ("c" . org-cycle)
  16640. ("C" . org-shifttab)
  16641. (" " . org-display-outline-path)
  16642. ("=" . org-columns)
  16643. ("Outline Structure Editing")
  16644. ("U" . org-shiftmetaup)
  16645. ("D" . org-shiftmetadown)
  16646. ("r" . org-metaright)
  16647. ("l" . org-metaleft)
  16648. ("R" . org-shiftmetaright)
  16649. ("L" . org-shiftmetaleft)
  16650. ("i" . (progn (forward-char 1) (call-interactively
  16651. 'org-insert-heading-respect-content)))
  16652. ("^" . org-sort)
  16653. ("w" . org-refile)
  16654. ("a" . org-archive-subtree-default-with-confirmation)
  16655. ("." . org-mark-subtree)
  16656. ("#" . org-toggle-comment)
  16657. ("Clock Commands")
  16658. ("I" . org-clock-in)
  16659. ("O" . org-clock-out)
  16660. ("Meta Data Editing")
  16661. ("t" . org-todo)
  16662. ("," . (org-priority))
  16663. ("0" . (org-priority ?\ ))
  16664. ("1" . (org-priority ?A))
  16665. ("2" . (org-priority ?B))
  16666. ("3" . (org-priority ?C))
  16667. (":" . org-set-tags-command)
  16668. ("e" . org-set-effort)
  16669. ("E" . org-inc-effort)
  16670. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  16671. (org-entry-put (point) "APPT_WARNTIME" m)))
  16672. ("Agenda Views etc")
  16673. ("v" . org-agenda)
  16674. ("/" . org-sparse-tree)
  16675. ("Misc")
  16676. ("o" . org-open-at-point)
  16677. ("?" . org-speed-command-help)
  16678. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16679. (">" . (org-agenda-remove-restriction-lock))
  16680. )
  16681. "The default speed commands.")
  16682. (defun org-print-speed-command (e)
  16683. (if (> (length (car e)) 1)
  16684. (progn
  16685. (princ "\n")
  16686. (princ (car e))
  16687. (princ "\n")
  16688. (princ (make-string (length (car e)) ?-))
  16689. (princ "\n"))
  16690. (princ (car e))
  16691. (princ " ")
  16692. (if (symbolp (cdr e))
  16693. (princ (symbol-name (cdr e)))
  16694. (prin1 (cdr e)))
  16695. (princ "\n")))
  16696. (defun org-speed-command-help ()
  16697. "Show the available speed commands."
  16698. (interactive)
  16699. (if (not org-use-speed-commands)
  16700. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16701. (with-output-to-temp-buffer "*Help*"
  16702. (princ "User-defined Speed commands\n===========================\n")
  16703. (mapc 'org-print-speed-command org-speed-commands-user)
  16704. (princ "\n")
  16705. (princ "Built-in Speed commands\n=======================\n")
  16706. (mapc 'org-print-speed-command org-speed-commands-default))
  16707. (with-current-buffer "*Help*"
  16708. (setq truncate-lines t))))
  16709. (defun org-speed-move-safe (cmd)
  16710. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16711. If not, return to the original position and throw an error."
  16712. (interactive)
  16713. (let ((pos (point)))
  16714. (call-interactively cmd)
  16715. (unless (and (bolp) (org-at-heading-p))
  16716. (goto-char pos)
  16717. (error "Boundary reached while executing %s" cmd))))
  16718. (defvar org-self-insert-command-undo-counter 0)
  16719. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16720. (defvar org-speed-command nil)
  16721. (define-obsolete-function-alias
  16722. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  16723. (defun org-speed-command-activate (keys)
  16724. "Hook for activating single-letter speed commands.
  16725. `org-speed-commands-default' specifies a minimal command set.
  16726. Use `org-speed-commands-user' for further customization."
  16727. (when (or (and (bolp) (looking-at org-outline-regexp))
  16728. (and (functionp org-use-speed-commands)
  16729. (funcall org-use-speed-commands)))
  16730. (cdr (assoc keys (append org-speed-commands-user
  16731. org-speed-commands-default)))))
  16732. (define-obsolete-function-alias
  16733. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  16734. (defun org-babel-speed-command-activate (keys)
  16735. "Hook for activating single-letter code block commands."
  16736. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16737. (cdr (assoc keys org-babel-key-bindings))))
  16738. (defcustom org-speed-command-hook
  16739. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16740. "Hook for activating speed commands at strategic locations.
  16741. Hook functions are called in sequence until a valid handler is
  16742. found.
  16743. Each hook takes a single argument, a user-pressed command key
  16744. which is also a `self-insert-command' from the global map.
  16745. Within the hook, examine the cursor position and the command key
  16746. and return nil or a valid handler as appropriate. Handler could
  16747. be one of an interactive command, a function, or a form.
  16748. Set `org-use-speed-commands' to non-nil value to enable this
  16749. hook. The default setting is `org-speed-command-activate'."
  16750. :group 'org-structure
  16751. :version "24.1"
  16752. :type 'hook)
  16753. (defun org-self-insert-command (N)
  16754. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16755. If the cursor is in a table looking at whitespace, the whitespace is
  16756. overwritten, and the table is not marked as requiring realignment."
  16757. (interactive "p")
  16758. (org-check-before-invisible-edit 'insert)
  16759. (cond
  16760. ((and org-use-speed-commands
  16761. (setq org-speed-command
  16762. (run-hook-with-args-until-success
  16763. 'org-speed-command-hook (this-command-keys))))
  16764. (cond
  16765. ((commandp org-speed-command)
  16766. (setq this-command org-speed-command)
  16767. (call-interactively org-speed-command))
  16768. ((functionp org-speed-command)
  16769. (funcall org-speed-command))
  16770. ((and org-speed-command (listp org-speed-command))
  16771. (eval org-speed-command))
  16772. (t (let (org-use-speed-commands)
  16773. (call-interactively 'org-self-insert-command)))))
  16774. ((and
  16775. (org-table-p)
  16776. (progn
  16777. ;; check if we blank the field, and if that triggers align
  16778. (and (featurep 'org-table) org-table-auto-blank-field
  16779. (member last-command
  16780. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16781. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16782. ;; got extra space, this field does not determine column width
  16783. (let (org-table-may-need-update) (org-table-blank-field))
  16784. ;; no extra space, this field may determine column width
  16785. (org-table-blank-field)))
  16786. t)
  16787. (eq N 1)
  16788. (looking-at "[^|\n]* |"))
  16789. (let (org-table-may-need-update)
  16790. (goto-char (1- (match-end 0)))
  16791. (backward-delete-char 1)
  16792. (goto-char (match-beginning 0))
  16793. (self-insert-command N)))
  16794. (t
  16795. (setq org-table-may-need-update t)
  16796. (self-insert-command N)
  16797. (org-fix-tags-on-the-fly)
  16798. (if org-self-insert-cluster-for-undo
  16799. (if (not (eq last-command 'org-self-insert-command))
  16800. (setq org-self-insert-command-undo-counter 1)
  16801. (if (>= org-self-insert-command-undo-counter 20)
  16802. (setq org-self-insert-command-undo-counter 1)
  16803. (and (> org-self-insert-command-undo-counter 0)
  16804. buffer-undo-list (listp buffer-undo-list)
  16805. (not (cadr buffer-undo-list)) ; remove nil entry
  16806. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16807. (setq org-self-insert-command-undo-counter
  16808. (1+ org-self-insert-command-undo-counter))))))))
  16809. (defun org-check-before-invisible-edit (kind)
  16810. "Check is editing if kind KIND would be dangerous with invisible text around.
  16811. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16812. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16813. (if (and org-catch-invisible-edits
  16814. (or (not (boundp 'visible-mode)) (not visible-mode))
  16815. (or (get-char-property (point) 'invisible)
  16816. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16817. ;; OK, we need to take a closer look
  16818. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16819. (invisible-before-point (if (bobp) nil (get-char-property
  16820. (1- (point)) 'invisible)))
  16821. (border-and-ok-direction
  16822. (or
  16823. ;; Check if we are acting predictably before invisible text
  16824. (and invisible-at-point (not invisible-before-point)
  16825. (memq kind '(insert delete-backward)))
  16826. ;; Check if we are acting predictably after invisible text
  16827. ;; This works not well, and I have turned it off. It seems
  16828. ;; better to always show and stop after invisible text.
  16829. ;; (and (not invisible-at-point) invisible-before-point
  16830. ;; (memq kind '(insert delete)))
  16831. )))
  16832. (when (or (memq invisible-at-point '(outline org-hide-block t))
  16833. (memq invisible-before-point '(outline org-hide-block t)))
  16834. (if (eq org-catch-invisible-edits 'error)
  16835. (error "Editing in invisible areas is prohibited - make visible first"))
  16836. (if (and org-custom-properties-overlays
  16837. (y-or-n-p "Display invisible properties in this buffer? "))
  16838. (org-toggle-custom-properties-visibility)
  16839. ;; Make the area visible
  16840. (save-excursion
  16841. (if invisible-before-point
  16842. (goto-char (previous-single-char-property-change
  16843. (point) 'invisible)))
  16844. (org-cycle))
  16845. (cond
  16846. ((eq org-catch-invisible-edits 'show)
  16847. ;; That's it, we do the edit after showing
  16848. (message
  16849. "Unfolding invisible region around point before editing")
  16850. (sit-for 1))
  16851. ((and (eq org-catch-invisible-edits 'smart)
  16852. border-and-ok-direction)
  16853. (message "Unfolding invisible region around point before editing"))
  16854. (t
  16855. ;; Don't do the edit, make the user repeat it in full visibility
  16856. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  16857. (defun org-fix-tags-on-the-fly ()
  16858. (when (and (equal (char-after (point-at-bol)) ?*)
  16859. (org-at-heading-p))
  16860. (org-align-tags-here org-tags-column)))
  16861. (defun org-delete-backward-char (N)
  16862. "Like `delete-backward-char', insert whitespace at field end in tables.
  16863. When deleting backwards, in tables this function will insert whitespace in
  16864. front of the next \"|\" separator, to keep the table aligned. The table will
  16865. still be marked for re-alignment if the field did fill the entire column,
  16866. because, in this case the deletion might narrow the column."
  16867. (interactive "p")
  16868. (save-match-data
  16869. (org-check-before-invisible-edit 'delete-backward)
  16870. (if (and (org-table-p)
  16871. (eq N 1)
  16872. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16873. (looking-at ".*?|"))
  16874. (let ((pos (point))
  16875. (noalign (looking-at "[^|\n\r]* |"))
  16876. (c org-table-may-need-update))
  16877. (backward-delete-char N)
  16878. (if (not overwrite-mode)
  16879. (progn
  16880. (skip-chars-forward "^|")
  16881. (insert " ")
  16882. (goto-char (1- pos))))
  16883. ;; noalign: if there were two spaces at the end, this field
  16884. ;; does not determine the width of the column.
  16885. (if noalign (setq org-table-may-need-update c)))
  16886. (backward-delete-char N)
  16887. (org-fix-tags-on-the-fly))))
  16888. (defun org-delete-char (N)
  16889. "Like `delete-char', but insert whitespace at field end in tables.
  16890. When deleting characters, in tables this function will insert whitespace in
  16891. front of the next \"|\" separator, to keep the table aligned. The table will
  16892. still be marked for re-alignment if the field did fill the entire column,
  16893. because, in this case the deletion might narrow the column."
  16894. (interactive "p")
  16895. (save-match-data
  16896. (org-check-before-invisible-edit 'delete)
  16897. (if (and (org-table-p)
  16898. (not (bolp))
  16899. (not (= (char-after) ?|))
  16900. (eq N 1))
  16901. (if (looking-at ".*?|")
  16902. (let ((pos (point))
  16903. (noalign (looking-at "[^|\n\r]* |"))
  16904. (c org-table-may-need-update))
  16905. (replace-match (concat
  16906. (substring (match-string 0) 1 -1)
  16907. " |"))
  16908. (goto-char pos)
  16909. ;; noalign: if there were two spaces at the end, this field
  16910. ;; does not determine the width of the column.
  16911. (if noalign (setq org-table-may-need-update c)))
  16912. (delete-char N))
  16913. (delete-char N)
  16914. (org-fix-tags-on-the-fly))))
  16915. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16916. (put 'org-self-insert-command 'delete-selection t)
  16917. (put 'orgtbl-self-insert-command 'delete-selection t)
  16918. (put 'org-delete-char 'delete-selection 'supersede)
  16919. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16920. (put 'org-yank 'delete-selection 'yank)
  16921. ;; Make `flyspell-mode' delay after some commands
  16922. (put 'org-self-insert-command 'flyspell-delayed t)
  16923. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16924. (put 'org-delete-char 'flyspell-delayed t)
  16925. (put 'org-delete-backward-char 'flyspell-delayed t)
  16926. ;; Make pabbrev-mode expand after org-mode commands
  16927. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16928. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16929. ;; How to do this: Measure non-white length of current string
  16930. ;; If equal to column width, we should realign.
  16931. (defun org-remap (map &rest commands)
  16932. "In MAP, remap the functions given in COMMANDS.
  16933. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16934. (let (new old)
  16935. (while commands
  16936. (setq old (pop commands) new (pop commands))
  16937. (if (fboundp 'command-remapping)
  16938. (org-defkey map (vector 'remap old) new)
  16939. (substitute-key-definition old new map global-map)))))
  16940. (when (eq org-enable-table-editor 'optimized)
  16941. ;; If the user wants maximum table support, we need to hijack
  16942. ;; some standard editing functions
  16943. (org-remap org-mode-map
  16944. 'self-insert-command 'org-self-insert-command
  16945. 'delete-char 'org-delete-char
  16946. 'delete-backward-char 'org-delete-backward-char)
  16947. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16948. (defvar org-ctrl-c-ctrl-c-hook nil
  16949. "Hook for functions attaching themselves to `C-c C-c'.
  16950. This can be used to add additional functionality to the C-c C-c
  16951. key which executes context-dependent commands. This hook is run
  16952. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16953. run after the last test.
  16954. Each function will be called with no arguments. The function
  16955. must check if the context is appropriate for it to act. If yes,
  16956. it should do its thing and then return a non-nil value. If the
  16957. context is wrong, just do nothing and return nil.")
  16958. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16959. "Hook for functions attaching themselves to `C-c C-c'.
  16960. This can be used to add additional functionality to the C-c C-c
  16961. key which executes context-dependent commands. This hook is run
  16962. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16963. before the first test.
  16964. Each function will be called with no arguments. The function
  16965. must check if the context is appropriate for it to act. If yes,
  16966. it should do its thing and then return a non-nil value. If the
  16967. context is wrong, just do nothing and return nil.")
  16968. (defvar org-tab-first-hook nil
  16969. "Hook for functions to attach themselves to TAB.
  16970. See `org-ctrl-c-ctrl-c-hook' for more information.
  16971. This hook runs as the first action when TAB is pressed, even before
  16972. `org-cycle' messes around with the `outline-regexp' to cater for
  16973. inline tasks and plain list item folding.
  16974. If any function in this hook returns t, any other actions that
  16975. would have been caused by TAB (such as table field motion or visibility
  16976. cycling) will not occur.")
  16977. (defvar org-tab-after-check-for-table-hook nil
  16978. "Hook for functions to attach themselves to TAB.
  16979. See `org-ctrl-c-ctrl-c-hook' for more information.
  16980. This hook runs after it has been established that the cursor is not in a
  16981. table, but before checking if the cursor is in a headline or if global cycling
  16982. should be done.
  16983. If any function in this hook returns t, not other actions like visibility
  16984. cycling will be done.")
  16985. (defvar org-tab-after-check-for-cycling-hook nil
  16986. "Hook for functions to attach themselves to TAB.
  16987. See `org-ctrl-c-ctrl-c-hook' for more information.
  16988. This hook runs after it has been established that not table field motion and
  16989. not visibility should be done because of current context. This is probably
  16990. the place where a package like yasnippets can hook in.")
  16991. (defvar org-tab-before-tab-emulation-hook nil
  16992. "Hook for functions to attach themselves to TAB.
  16993. See `org-ctrl-c-ctrl-c-hook' for more information.
  16994. This hook runs after every other options for TAB have been exhausted, but
  16995. before indentation and \t insertion takes place.")
  16996. (defvar org-metaleft-hook nil
  16997. "Hook for functions attaching themselves to `M-left'.
  16998. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16999. (defvar org-metaright-hook nil
  17000. "Hook for functions attaching themselves to `M-right'.
  17001. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17002. (defvar org-metaup-hook nil
  17003. "Hook for functions attaching themselves to `M-up'.
  17004. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17005. (defvar org-metadown-hook nil
  17006. "Hook for functions attaching themselves to `M-down'.
  17007. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17008. (defvar org-shiftmetaleft-hook nil
  17009. "Hook for functions attaching themselves to `M-S-left'.
  17010. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17011. (defvar org-shiftmetaright-hook nil
  17012. "Hook for functions attaching themselves to `M-S-right'.
  17013. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17014. (defvar org-shiftmetaup-hook nil
  17015. "Hook for functions attaching themselves to `M-S-up'.
  17016. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17017. (defvar org-shiftmetadown-hook nil
  17018. "Hook for functions attaching themselves to `M-S-down'.
  17019. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17020. (defvar org-metareturn-hook nil
  17021. "Hook for functions attaching themselves to `M-RET'.
  17022. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17023. (defvar org-shiftup-hook nil
  17024. "Hook for functions attaching themselves to `S-up'.
  17025. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17026. (defvar org-shiftup-final-hook nil
  17027. "Hook for functions attaching themselves to `S-up'.
  17028. This one runs after all other options except shift-select have been excluded.
  17029. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17030. (defvar org-shiftdown-hook nil
  17031. "Hook for functions attaching themselves to `S-down'.
  17032. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17033. (defvar org-shiftdown-final-hook nil
  17034. "Hook for functions attaching themselves to `S-down'.
  17035. This one runs after all other options except shift-select have been excluded.
  17036. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17037. (defvar org-shiftleft-hook nil
  17038. "Hook for functions attaching themselves to `S-left'.
  17039. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17040. (defvar org-shiftleft-final-hook nil
  17041. "Hook for functions attaching themselves to `S-left'.
  17042. This one runs after all other options except shift-select have been excluded.
  17043. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17044. (defvar org-shiftright-hook nil
  17045. "Hook for functions attaching themselves to `S-right'.
  17046. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17047. (defvar org-shiftright-final-hook nil
  17048. "Hook for functions attaching themselves to `S-right'.
  17049. This one runs after all other options except shift-select have been excluded.
  17050. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17051. (defun org-modifier-cursor-error ()
  17052. "Throw an error, a modified cursor command was applied in wrong context."
  17053. (error "This command is active in special context like tables, headlines or items"))
  17054. (defun org-shiftselect-error ()
  17055. "Throw an error because Shift-Cursor command was applied in wrong context."
  17056. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17057. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17058. (error "This command works only in special context like headlines or timestamps")))
  17059. (defun org-call-for-shift-select (cmd)
  17060. (let ((this-command-keys-shift-translated t))
  17061. (call-interactively cmd)))
  17062. (defun org-shifttab (&optional arg)
  17063. "Global visibility cycling or move to previous table field.
  17064. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17065. on context.
  17066. See the individual commands for more information."
  17067. (interactive "P")
  17068. (cond
  17069. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17070. ((integerp arg)
  17071. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17072. (message "Content view to level: %d" arg)
  17073. (org-content (prefix-numeric-value arg2))
  17074. (setq org-cycle-global-status 'overview)))
  17075. (t (call-interactively 'org-global-cycle))))
  17076. (defun org-shiftmetaleft ()
  17077. "Promote subtree or delete table column.
  17078. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17079. `org-table-delete-column', depending on context. See the
  17080. individual commands for more information."
  17081. (interactive)
  17082. (cond
  17083. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17084. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17085. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17086. ((if (not (org-region-active-p)) (org-at-item-p)
  17087. (save-excursion (goto-char (region-beginning))
  17088. (org-at-item-p)))
  17089. (call-interactively 'org-outdent-item-tree))
  17090. (t (org-modifier-cursor-error))))
  17091. (defun org-shiftmetaright ()
  17092. "Demote subtree or insert table column.
  17093. Calls `org-demote-subtree', `org-indent-item-tree', or
  17094. `org-table-insert-column', depending on context. See the
  17095. individual commands for more information."
  17096. (interactive)
  17097. (cond
  17098. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17099. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17100. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17101. ((if (not (org-region-active-p)) (org-at-item-p)
  17102. (save-excursion (goto-char (region-beginning))
  17103. (org-at-item-p)))
  17104. (call-interactively 'org-indent-item-tree))
  17105. (t (org-modifier-cursor-error))))
  17106. (defun org-shiftmetaup (&optional arg)
  17107. "Move subtree up or kill table row.
  17108. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17109. `org-move-item-up' or `org-timestamp-up', depending on context.
  17110. See the individual commands for more information."
  17111. (interactive "P")
  17112. (cond
  17113. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17114. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17115. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17116. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17117. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17118. (call-interactively 'org-timestamp-up)))
  17119. (t (org-modifier-cursor-error))))
  17120. (defun org-shiftmetadown (&optional arg)
  17121. "Move subtree down or insert table row.
  17122. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17123. `org-move-item-down' or `org-timestamp-up', depending on context.
  17124. See the individual commands for more information."
  17125. (interactive "P")
  17126. (cond
  17127. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17128. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17129. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17130. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17131. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17132. (call-interactively 'org-timestamp-down)))
  17133. (t (org-modifier-cursor-error))))
  17134. (defsubst org-hidden-tree-error ()
  17135. (error
  17136. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17137. (defun org-metaleft (&optional arg)
  17138. "Promote heading or move table column to left.
  17139. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17140. With no specific context, calls the Emacs default `backward-word'.
  17141. See the individual commands for more information."
  17142. (interactive "P")
  17143. (cond
  17144. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17145. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17146. ((org-with-limited-levels
  17147. (or (org-at-heading-p)
  17148. (and (org-region-active-p)
  17149. (save-excursion
  17150. (goto-char (region-beginning))
  17151. (org-at-heading-p)))))
  17152. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17153. (call-interactively 'org-do-promote))
  17154. ;; At an inline task.
  17155. ((org-at-heading-p)
  17156. (call-interactively 'org-inlinetask-promote))
  17157. ((or (org-at-item-p)
  17158. (and (org-region-active-p)
  17159. (save-excursion
  17160. (goto-char (region-beginning))
  17161. (org-at-item-p))))
  17162. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17163. (call-interactively 'org-outdent-item))
  17164. (t (call-interactively 'backward-word))))
  17165. (defun org-metaright (&optional arg)
  17166. "Demote a subtree, a list item or move table column to right.
  17167. In front of a drawer or a block keyword, indent it correctly.
  17168. With no specific context, calls the Emacs default `forward-word'.
  17169. See the individual commands for more information."
  17170. (interactive "P")
  17171. (cond
  17172. ((run-hook-with-args-until-success 'org-metaright-hook))
  17173. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17174. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17175. ((org-at-block-p) (call-interactively 'org-indent-block))
  17176. ((org-with-limited-levels
  17177. (or (org-at-heading-p)
  17178. (and (org-region-active-p)
  17179. (save-excursion
  17180. (goto-char (region-beginning))
  17181. (org-at-heading-p)))))
  17182. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17183. (call-interactively 'org-do-demote))
  17184. ;; At an inline task.
  17185. ((org-at-heading-p)
  17186. (call-interactively 'org-inlinetask-demote))
  17187. ((or (org-at-item-p)
  17188. (and (org-region-active-p)
  17189. (save-excursion
  17190. (goto-char (region-beginning))
  17191. (org-at-item-p))))
  17192. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17193. (call-interactively 'org-indent-item))
  17194. (t (call-interactively 'forward-word))))
  17195. (defun org-check-for-hidden (what)
  17196. "Check if there are hidden headlines/items in the current visual line.
  17197. WHAT can be either `headlines' or `items'. If the current line is
  17198. an outline or item heading and it has a folded subtree below it,
  17199. this function returns t, nil otherwise."
  17200. (let ((re (cond
  17201. ((eq what 'headlines) org-outline-regexp-bol)
  17202. ((eq what 'items) (org-item-beginning-re))
  17203. (t (error "This should not happen"))))
  17204. beg end)
  17205. (save-excursion
  17206. (catch 'exit
  17207. (unless (org-region-active-p)
  17208. (setq beg (point-at-bol))
  17209. (beginning-of-line 2)
  17210. (while (and (not (eobp)) ;; this is like `next-line'
  17211. (get-char-property (1- (point)) 'invisible))
  17212. (beginning-of-line 2))
  17213. (setq end (point))
  17214. (goto-char beg)
  17215. (goto-char (point-at-eol))
  17216. (setq end (max end (point)))
  17217. (while (re-search-forward re end t)
  17218. (if (get-char-property (match-beginning 0) 'invisible)
  17219. (throw 'exit t))))
  17220. nil))))
  17221. (org-autoload "org-element" '(org-element-at-point org-element-type))
  17222. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  17223. (declare-function org-element-type "org-element" (element))
  17224. (declare-function org-element-contents "org-element" (element))
  17225. (declare-function org-element-property "org-element" (property element))
  17226. (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion))
  17227. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  17228. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  17229. (declare-function org-element--parse-objects "org-element" (beg end acc restriction))
  17230. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  17231. (defun org-metaup (&optional arg)
  17232. "Move subtree up or move table row up.
  17233. Calls `org-move-subtree-up' or `org-table-move-row' or
  17234. `org-move-item-up', depending on context. See the individual commands
  17235. for more information."
  17236. (interactive "P")
  17237. (cond
  17238. ((run-hook-with-args-until-success 'org-metaup-hook))
  17239. ((org-region-active-p)
  17240. (let* ((a (min (region-beginning) (region-end)))
  17241. (b (1- (max (region-beginning) (region-end))))
  17242. (c (save-excursion (goto-char a)
  17243. (move-beginning-of-line 0)))
  17244. (d (save-excursion (goto-char a)
  17245. (move-end-of-line 0) (point))))
  17246. (transpose-regions a b c d)
  17247. (goto-char c)))
  17248. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17249. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17250. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17251. (t (org-drag-element-backward))))
  17252. (defun org-metadown (&optional arg)
  17253. "Move subtree down or move table row down.
  17254. Calls `org-move-subtree-down' or `org-table-move-row' or
  17255. `org-move-item-down', depending on context. See the individual
  17256. commands for more information."
  17257. (interactive "P")
  17258. (cond
  17259. ((run-hook-with-args-until-success 'org-metadown-hook))
  17260. ((org-region-active-p)
  17261. (let* ((a (min (region-beginning) (region-end)))
  17262. (b (max (region-beginning) (region-end)))
  17263. (c (save-excursion (goto-char b)
  17264. (move-beginning-of-line 1)))
  17265. (d (save-excursion (goto-char b)
  17266. (move-end-of-line 1) (1+ (point)))))
  17267. (transpose-regions a b c d)
  17268. (goto-char d)))
  17269. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17270. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17271. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17272. (t (org-drag-element-forward))))
  17273. (defun org-shiftup (&optional arg)
  17274. "Increase item in timestamp or increase priority of current headline.
  17275. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17276. depending on context. See the individual commands for more information."
  17277. (interactive "P")
  17278. (cond
  17279. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17280. ((and org-support-shift-select (org-region-active-p))
  17281. (org-call-for-shift-select 'previous-line))
  17282. ((org-at-timestamp-p t)
  17283. (call-interactively (if org-edit-timestamp-down-means-later
  17284. 'org-timestamp-down 'org-timestamp-up)))
  17285. ((and (not (eq org-support-shift-select 'always))
  17286. org-enable-priority-commands
  17287. (org-at-heading-p))
  17288. (call-interactively 'org-priority-up))
  17289. ((and (not org-support-shift-select) (org-at-item-p))
  17290. (call-interactively 'org-previous-item))
  17291. ((org-clocktable-try-shift 'up arg))
  17292. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17293. (org-support-shift-select
  17294. (org-call-for-shift-select 'previous-line))
  17295. (t (org-shiftselect-error))))
  17296. (defun org-shiftdown (&optional arg)
  17297. "Decrease item in timestamp or decrease priority of current headline.
  17298. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17299. depending on context. See the individual commands for more information."
  17300. (interactive "P")
  17301. (cond
  17302. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17303. ((and org-support-shift-select (org-region-active-p))
  17304. (org-call-for-shift-select 'next-line))
  17305. ((org-at-timestamp-p t)
  17306. (call-interactively (if org-edit-timestamp-down-means-later
  17307. 'org-timestamp-up 'org-timestamp-down)))
  17308. ((and (not (eq org-support-shift-select 'always))
  17309. org-enable-priority-commands
  17310. (org-at-heading-p))
  17311. (call-interactively 'org-priority-down))
  17312. ((and (not org-support-shift-select) (org-at-item-p))
  17313. (call-interactively 'org-next-item))
  17314. ((org-clocktable-try-shift 'down arg))
  17315. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17316. (org-support-shift-select
  17317. (org-call-for-shift-select 'next-line))
  17318. (t (org-shiftselect-error))))
  17319. (defun org-shiftright (&optional arg)
  17320. "Cycle the thing at point or in the current line, depending on context.
  17321. Depending on context, this does one of the following:
  17322. - switch a timestamp at point one day into the future
  17323. - on a headline, switch to the next TODO keyword.
  17324. - on an item, switch entire list to the next bullet type
  17325. - on a property line, switch to the next allowed value
  17326. - on a clocktable definition line, move time block into the future"
  17327. (interactive "P")
  17328. (cond
  17329. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17330. ((and org-support-shift-select (org-region-active-p))
  17331. (org-call-for-shift-select 'forward-char))
  17332. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17333. ((and (not (eq org-support-shift-select 'always))
  17334. (org-at-heading-p))
  17335. (let ((org-inhibit-logging
  17336. (not org-treat-S-cursor-todo-selection-as-state-change))
  17337. (org-inhibit-blocking
  17338. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17339. (org-call-with-arg 'org-todo 'right)))
  17340. ((or (and org-support-shift-select
  17341. (not (eq org-support-shift-select 'always))
  17342. (org-at-item-bullet-p))
  17343. (and (not org-support-shift-select) (org-at-item-p)))
  17344. (org-call-with-arg 'org-cycle-list-bullet nil))
  17345. ((and (not (eq org-support-shift-select 'always))
  17346. (org-at-property-p))
  17347. (call-interactively 'org-property-next-allowed-value))
  17348. ((org-clocktable-try-shift 'right arg))
  17349. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17350. (org-support-shift-select
  17351. (org-call-for-shift-select 'forward-char))
  17352. (t (org-shiftselect-error))))
  17353. (defun org-shiftleft (&optional arg)
  17354. "Cycle the thing at point or in the current line, depending on context.
  17355. Depending on context, this does one of the following:
  17356. - switch a timestamp at point one day into the past
  17357. - on a headline, switch to the previous TODO keyword.
  17358. - on an item, switch entire list to the previous bullet type
  17359. - on a property line, switch to the previous allowed value
  17360. - on a clocktable definition line, move time block into the past"
  17361. (interactive "P")
  17362. (cond
  17363. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17364. ((and org-support-shift-select (org-region-active-p))
  17365. (org-call-for-shift-select 'backward-char))
  17366. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17367. ((and (not (eq org-support-shift-select 'always))
  17368. (org-at-heading-p))
  17369. (let ((org-inhibit-logging
  17370. (not org-treat-S-cursor-todo-selection-as-state-change))
  17371. (org-inhibit-blocking
  17372. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17373. (org-call-with-arg 'org-todo 'left)))
  17374. ((or (and org-support-shift-select
  17375. (not (eq org-support-shift-select 'always))
  17376. (org-at-item-bullet-p))
  17377. (and (not org-support-shift-select) (org-at-item-p)))
  17378. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17379. ((and (not (eq org-support-shift-select 'always))
  17380. (org-at-property-p))
  17381. (call-interactively 'org-property-previous-allowed-value))
  17382. ((org-clocktable-try-shift 'left arg))
  17383. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17384. (org-support-shift-select
  17385. (org-call-for-shift-select 'backward-char))
  17386. (t (org-shiftselect-error))))
  17387. (defun org-shiftcontrolright ()
  17388. "Switch to next TODO set."
  17389. (interactive)
  17390. (cond
  17391. ((and org-support-shift-select (org-region-active-p))
  17392. (org-call-for-shift-select 'forward-word))
  17393. ((and (not (eq org-support-shift-select 'always))
  17394. (org-at-heading-p))
  17395. (org-call-with-arg 'org-todo 'nextset))
  17396. (org-support-shift-select
  17397. (org-call-for-shift-select 'forward-word))
  17398. (t (org-shiftselect-error))))
  17399. (defun org-shiftcontrolleft ()
  17400. "Switch to previous TODO set."
  17401. (interactive)
  17402. (cond
  17403. ((and org-support-shift-select (org-region-active-p))
  17404. (org-call-for-shift-select 'backward-word))
  17405. ((and (not (eq org-support-shift-select 'always))
  17406. (org-at-heading-p))
  17407. (org-call-with-arg 'org-todo 'previousset))
  17408. (org-support-shift-select
  17409. (org-call-for-shift-select 'backward-word))
  17410. (t (org-shiftselect-error))))
  17411. (defun org-shiftcontrolup ()
  17412. "Change timestamps synchronously up in CLOCK log lines."
  17413. (interactive)
  17414. (cond ((and (not org-support-shift-select)
  17415. (org-at-clock-log-p)
  17416. (org-at-timestamp-p t))
  17417. (org-clock-timestamps-up))
  17418. (t (org-shiftselect-error))))
  17419. (defun org-shiftcontroldown ()
  17420. "Change timestamps synchronously down in CLOCK log lines."
  17421. (interactive)
  17422. (cond ((and (not org-support-shift-select)
  17423. (org-at-clock-log-p)
  17424. (org-at-timestamp-p t))
  17425. (org-clock-timestamps-down))
  17426. (t (org-shiftselect-error))))
  17427. (defun org-ctrl-c-ret ()
  17428. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17429. (interactive)
  17430. (cond
  17431. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17432. (t (call-interactively 'org-insert-heading))))
  17433. (defun org-find-visible ()
  17434. (let ((s (point)))
  17435. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17436. (get-char-property s 'invisible)))
  17437. s))
  17438. (defun org-find-invisible ()
  17439. (let ((s (point)))
  17440. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17441. (not (get-char-property s 'invisible))))
  17442. s))
  17443. (defun org-copy-visible (beg end)
  17444. "Copy the visible parts of the region."
  17445. (interactive "r")
  17446. (let (snippets s)
  17447. (save-excursion
  17448. (save-restriction
  17449. (narrow-to-region beg end)
  17450. (setq s (goto-char (point-min)))
  17451. (while (not (= (point) (point-max)))
  17452. (goto-char (org-find-invisible))
  17453. (push (buffer-substring s (point)) snippets)
  17454. (setq s (goto-char (org-find-visible))))))
  17455. (kill-new (apply 'concat (nreverse snippets)))))
  17456. (defun org-copy-special ()
  17457. "Copy region in table or copy current subtree.
  17458. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17459. See the individual commands for more information."
  17460. (interactive)
  17461. (call-interactively
  17462. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17463. (defun org-cut-special ()
  17464. "Cut region in table or cut current subtree.
  17465. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17466. See the individual commands for more information."
  17467. (interactive)
  17468. (call-interactively
  17469. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17470. (defun org-paste-special (arg)
  17471. "Paste rectangular region into table, or past subtree relative to level.
  17472. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17473. See the individual commands for more information."
  17474. (interactive "P")
  17475. (if (org-at-table-p)
  17476. (org-table-paste-rectangle)
  17477. (org-paste-subtree arg)))
  17478. (defsubst org-in-fixed-width-region-p ()
  17479. "Is point in a fixed-width region?"
  17480. (save-match-data
  17481. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  17482. (defun org-edit-special (&optional arg)
  17483. "Call a special editor for the stuff at point.
  17484. When at a table, call the formula editor with `org-table-edit-formulas'.
  17485. When in a source code block, call `org-edit-src-code'.
  17486. When in a fixed-width region, call `org-edit-fixed-width-region'.
  17487. When in an #+include line, visit the included file.
  17488. On a link, call `ffap' to visit the link at point.
  17489. Otherwise, return a user error."
  17490. (interactive)
  17491. ;; possibly prep session before editing source
  17492. (when (and (org-in-src-block-p) arg)
  17493. (let* ((info (org-babel-get-src-block-info))
  17494. (lang (nth 0 info))
  17495. (params (nth 2 info))
  17496. (session (cdr (assoc :session params))))
  17497. (when (and info session) ;; we are in a source-code block with a session
  17498. (funcall
  17499. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17500. (cond ;; proceed with `org-edit-special'
  17501. ((save-excursion
  17502. (beginning-of-line 1)
  17503. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17504. (find-file (org-trim (match-string 1))))
  17505. ((org-at-table.el-p) (org-edit-src-code))
  17506. ((or (org-at-table-p)
  17507. (save-excursion
  17508. (beginning-of-line 1)
  17509. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17510. (call-interactively 'org-table-edit-formulas))
  17511. ((org-in-block-p '("src" "example" "latex" "html")) (org-edit-src-code))
  17512. ((org-in-fixed-width-region-p) (org-edit-fixed-width-region))
  17513. ((org-at-regexp-p org-any-link-re) (call-interactively 'ffap))
  17514. (t (user-error "No special environment to edit here"))))
  17515. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17516. (defun org-ctrl-c-ctrl-c (&optional arg)
  17517. "Set tags in headline, or update according to changed information at point.
  17518. This command does many different things, depending on context:
  17519. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17520. this is what we do.
  17521. - If the cursor is on a statistics cookie, update it.
  17522. - If the cursor is in a headline, prompt for tags and insert them
  17523. into the current line, aligned to `org-tags-column'. When called
  17524. with prefix arg, realign all tags in the current buffer.
  17525. - If the cursor is in one of the special #+KEYWORD lines, this
  17526. triggers scanning the buffer for these lines and updating the
  17527. information.
  17528. - If the cursor is inside a table, realign the table. This command
  17529. works even if the automatic table editor has been turned off.
  17530. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17531. the entire table.
  17532. - If the cursor is at a footnote reference or definition, jump to
  17533. the corresponding definition or references, respectively.
  17534. - If the cursor is a the beginning of a dynamic block, update it.
  17535. - If the current buffer is a capture buffer, close note and file it.
  17536. - If the cursor is on a <<<target>>>, update radio targets and
  17537. corresponding links in this buffer.
  17538. - If the cursor is on a numbered item in a plain list, renumber the
  17539. ordered list.
  17540. - If the cursor is on a checkbox, toggle it.
  17541. - If the cursor is on a code block, evaluate it. The variable
  17542. `org-confirm-babel-evaluate' can be used to control prompting
  17543. before code block evaluation, by default every code block
  17544. evaluation requires confirmation. Code block evaluation can be
  17545. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17546. (interactive "P")
  17547. (let ((org-enable-table-editor t))
  17548. (cond
  17549. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17550. org-occur-highlights
  17551. org-latex-fragment-image-overlays)
  17552. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17553. (org-remove-occur-highlights)
  17554. (org-remove-latex-fragment-image-overlays)
  17555. (message "Temporary highlights/overlays removed from current buffer"))
  17556. ((and (local-variable-p 'org-finish-function (current-buffer))
  17557. (fboundp org-finish-function))
  17558. (funcall org-finish-function))
  17559. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17560. ((org-in-regexp org-ts-regexp-both)
  17561. (org-timestamp-change 0 'day))
  17562. ((or (looking-at org-property-start-re)
  17563. (org-at-property-p))
  17564. (call-interactively 'org-property-action))
  17565. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17566. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17567. (or (org-at-heading-p) (org-at-item-p)))
  17568. (call-interactively 'org-update-statistics-cookies))
  17569. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17570. ((org-at-table.el-p)
  17571. (message "Use C-c ' to edit table.el tables"))
  17572. ((org-at-table-p)
  17573. (org-table-maybe-eval-formula)
  17574. (if arg
  17575. (call-interactively 'org-table-recalculate)
  17576. (org-table-maybe-recalculate-line))
  17577. (call-interactively 'org-table-align)
  17578. (orgtbl-send-table 'maybe))
  17579. ((or (org-footnote-at-reference-p)
  17580. (org-footnote-at-definition-p))
  17581. (call-interactively 'org-footnote-action))
  17582. ((org-at-item-checkbox-p)
  17583. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17584. ;; list only if at top item.
  17585. (let* ((cbox (match-string 1))
  17586. (struct (org-list-struct))
  17587. (old-struct (copy-tree struct))
  17588. (parents (org-list-parents-alist struct))
  17589. (orderedp (org-entry-get nil "ORDERED"))
  17590. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17591. block-item)
  17592. ;; Use a light version of `org-toggle-checkbox' to avoid
  17593. ;; computing list structure twice.
  17594. (let ((new-box (cond
  17595. ((equal arg '(16)) "[-]")
  17596. ((equal arg '(4)) nil)
  17597. ((equal "[X]" cbox) "[ ]")
  17598. (t "[X]"))))
  17599. (if (and firstp arg)
  17600. ;; If at first item of sub-list, remove check-box from
  17601. ;; every item at the same level.
  17602. (mapc
  17603. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17604. (org-list-get-all-items
  17605. (point-at-bol) struct (org-list-prevs-alist struct)))
  17606. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17607. ;; Replicate `org-list-write-struct', while grabbing a return
  17608. ;; value from `org-list-struct-fix-box'.
  17609. (org-list-struct-fix-ind struct parents 2)
  17610. (org-list-struct-fix-item-end struct)
  17611. (let ((prevs (org-list-prevs-alist struct)))
  17612. (org-list-struct-fix-bul struct prevs)
  17613. (org-list-struct-fix-ind struct parents)
  17614. (setq block-item
  17615. (org-list-struct-fix-box struct parents prevs orderedp)))
  17616. (if (equal struct old-struct)
  17617. (user-error "Cannot toggle this checkbox (unchecked subitems?)")
  17618. (org-list-struct-apply-struct struct old-struct)
  17619. (org-update-checkbox-count-maybe))
  17620. (when block-item
  17621. (message
  17622. "Checkboxes were removed due to unchecked box at line %d"
  17623. (org-current-line block-item)))
  17624. (when firstp (org-list-send-list 'maybe))))
  17625. ((org-at-item-p)
  17626. ;; Cursor at an item: repair list. Do checkbox related actions
  17627. ;; only if function was called with an argument. Send list only
  17628. ;; if at top item.
  17629. (let* ((struct (org-list-struct))
  17630. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17631. old-struct)
  17632. (when arg
  17633. (setq old-struct (copy-tree struct))
  17634. (if firstp
  17635. ;; If at first item of sub-list, add check-box to every
  17636. ;; item at the same level.
  17637. (mapc
  17638. (lambda (pos)
  17639. (unless (org-list-get-checkbox pos struct)
  17640. (org-list-set-checkbox pos struct "[ ]")))
  17641. (org-list-get-all-items
  17642. (point-at-bol) struct (org-list-prevs-alist struct)))
  17643. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17644. (org-list-write-struct
  17645. struct (org-list-parents-alist struct) old-struct)
  17646. (when arg (org-update-checkbox-count-maybe))
  17647. (when firstp (org-list-send-list 'maybe))))
  17648. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17649. ;; Dynamic block
  17650. (beginning-of-line 1)
  17651. (save-excursion (org-update-dblock)))
  17652. ((save-excursion
  17653. (let ((case-fold-search t))
  17654. (beginning-of-line 1)
  17655. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  17656. (cond
  17657. ((or (equal (match-string 1) "TBLFM")
  17658. (equal (match-string 1) "tblfm"))
  17659. ;; Recalculate the table before this line
  17660. (save-excursion
  17661. (beginning-of-line 1)
  17662. (skip-chars-backward " \r\n\t")
  17663. (if (org-at-table-p)
  17664. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17665. (t
  17666. (let ((org-inhibit-startup-visibility-stuff t)
  17667. (org-startup-align-all-tables nil))
  17668. (when (boundp 'org-table-coordinate-overlays)
  17669. (mapc 'delete-overlay org-table-coordinate-overlays)
  17670. (setq org-table-coordinate-overlays nil))
  17671. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17672. (message "Local setup has been refreshed"))))
  17673. ((org-clock-update-time-maybe))
  17674. (t
  17675. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17676. (error "C-c C-c can do nothing useful at this location"))))))
  17677. (defun org-mode-restart ()
  17678. "Restart Org-mode, to scan again for special lines.
  17679. Also updates the keyword regular expressions."
  17680. (interactive)
  17681. (org-mode)
  17682. (message "Org-mode restarted"))
  17683. (defun org-kill-note-or-show-branches ()
  17684. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17685. (interactive)
  17686. (if (not org-finish-function)
  17687. (progn
  17688. (hide-subtree)
  17689. (call-interactively 'show-branches))
  17690. (let ((org-note-abort t))
  17691. (funcall org-finish-function))))
  17692. (defun org-return (&optional indent)
  17693. "Goto next table row or insert a newline.
  17694. Calls `org-table-next-row' or `newline', depending on context.
  17695. See the individual commands for more information."
  17696. (interactive)
  17697. (let (org-ts-what)
  17698. (cond
  17699. ((or (bobp) (org-in-src-block-p))
  17700. (if indent (newline-and-indent) (newline)))
  17701. ((org-at-table-p)
  17702. (org-table-justify-field-maybe)
  17703. (call-interactively 'org-table-next-row))
  17704. ;; when `newline-and-indent' is called within a list, make sure
  17705. ;; text moved stays inside the item.
  17706. ((and (org-in-item-p) indent)
  17707. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17708. (progn
  17709. (save-match-data (newline))
  17710. (org-indent-line-to (length (match-string 0))))
  17711. (let ((ind (org-get-indentation)))
  17712. (newline)
  17713. (if (org-looking-back org-list-end-re)
  17714. (org-indent-line)
  17715. (org-indent-line-to ind)))))
  17716. ((and org-return-follows-link
  17717. (org-at-timestamp-p t)
  17718. (not (eq org-ts-what 'after)))
  17719. (org-follow-timestamp-link))
  17720. ((and org-return-follows-link
  17721. (let ((tprop (get-text-property (point) 'face)))
  17722. (or (eq tprop 'org-link)
  17723. (and (listp tprop) (memq 'org-link tprop)))))
  17724. (call-interactively 'org-open-at-point))
  17725. ((and (org-at-heading-p)
  17726. (looking-at
  17727. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17728. (org-show-entry)
  17729. (end-of-line 1)
  17730. (newline))
  17731. (t (if indent (newline-and-indent) (newline))))))
  17732. (defun org-return-indent ()
  17733. "Goto next table row or insert a newline and indent.
  17734. Calls `org-table-next-row' or `newline-and-indent', depending on
  17735. context. See the individual commands for more information."
  17736. (interactive)
  17737. (org-return t))
  17738. (defun org-ctrl-c-star ()
  17739. "Compute table, or change heading status of lines.
  17740. Calls `org-table-recalculate' or `org-toggle-heading',
  17741. depending on context."
  17742. (interactive)
  17743. (cond
  17744. ((org-at-table-p)
  17745. (call-interactively 'org-table-recalculate))
  17746. (t
  17747. ;; Convert all lines in region to list items
  17748. (call-interactively 'org-toggle-heading))))
  17749. (defun org-ctrl-c-minus ()
  17750. "Insert separator line in table or modify bullet status of line.
  17751. Also turns a plain line or a region of lines into list items.
  17752. Calls `org-table-insert-hline', `org-toggle-item', or
  17753. `org-cycle-list-bullet', depending on context."
  17754. (interactive)
  17755. (cond
  17756. ((org-at-table-p)
  17757. (call-interactively 'org-table-insert-hline))
  17758. ((org-region-active-p)
  17759. (call-interactively 'org-toggle-item))
  17760. ((org-in-item-p)
  17761. (call-interactively 'org-cycle-list-bullet))
  17762. (t
  17763. (call-interactively 'org-toggle-item))))
  17764. (defun org-toggle-item (arg)
  17765. "Convert headings or normal lines to items, items to normal lines.
  17766. If there is no active region, only the current line is considered.
  17767. If the first non blank line in the region is an headline, convert
  17768. all headlines to items, shifting text accordingly.
  17769. If it is an item, convert all items to normal lines.
  17770. If it is normal text, change region into an item. With a prefix
  17771. argument ARG, change each line in region into an item."
  17772. (interactive "P")
  17773. (let ((shift-text
  17774. (function
  17775. ;; Shift text in current section to IND, from point to END.
  17776. ;; The function leaves point to END line.
  17777. (lambda (ind end)
  17778. (let ((min-i 1000) (end (copy-marker end)))
  17779. ;; First determine the minimum indentation (MIN-I) of
  17780. ;; the text.
  17781. (save-excursion
  17782. (catch 'exit
  17783. (while (< (point) end)
  17784. (let ((i (org-get-indentation)))
  17785. (cond
  17786. ;; Skip blank lines and inline tasks.
  17787. ((looking-at "^[ \t]*$"))
  17788. ((looking-at org-outline-regexp-bol))
  17789. ;; We can't find less than 0 indentation.
  17790. ((zerop i) (throw 'exit (setq min-i 0)))
  17791. ((< i min-i) (setq min-i i))))
  17792. (forward-line))))
  17793. ;; Then indent each line so that a line indented to
  17794. ;; MIN-I becomes indented to IND. Ignore blank lines
  17795. ;; and inline tasks in the process.
  17796. (let ((delta (- ind min-i)))
  17797. (while (< (point) end)
  17798. (unless (or (looking-at "^[ \t]*$")
  17799. (looking-at org-outline-regexp-bol))
  17800. (org-indent-line-to (+ (org-get-indentation) delta)))
  17801. (forward-line)))))))
  17802. (skip-blanks
  17803. (function
  17804. ;; Return beginning of first non-blank line, starting from
  17805. ;; line at POS.
  17806. (lambda (pos)
  17807. (save-excursion
  17808. (goto-char pos)
  17809. (skip-chars-forward " \r\t\n")
  17810. (point-at-bol)))))
  17811. beg end)
  17812. ;; Determine boundaries of changes.
  17813. (if (org-region-active-p)
  17814. (setq beg (funcall skip-blanks (region-beginning))
  17815. end (copy-marker (region-end)))
  17816. (setq beg (funcall skip-blanks (point-at-bol))
  17817. end (copy-marker (point-at-eol))))
  17818. ;; Depending on the starting line, choose an action on the text
  17819. ;; between BEG and END.
  17820. (org-with-limited-levels
  17821. (save-excursion
  17822. (goto-char beg)
  17823. (cond
  17824. ;; Case 1. Start at an item: de-itemize. Note that it only
  17825. ;; happens when a region is active: `org-ctrl-c-minus'
  17826. ;; would call `org-cycle-list-bullet' otherwise.
  17827. ((org-at-item-p)
  17828. (while (< (point) end)
  17829. (when (org-at-item-p)
  17830. (skip-chars-forward " \t")
  17831. (delete-region (point) (match-end 0)))
  17832. (forward-line)))
  17833. ;; Case 2. Start at an heading: convert to items.
  17834. ((org-at-heading-p)
  17835. (let* ((bul (org-list-bullet-string "-"))
  17836. (bul-len (length bul))
  17837. ;; Indentation of the first heading. It should be
  17838. ;; relative to the indentation of its parent, if any.
  17839. (start-ind (save-excursion
  17840. (cond
  17841. ((not org-adapt-indentation) 0)
  17842. ((not (outline-previous-heading)) 0)
  17843. (t (length (match-string 0))))))
  17844. ;; Level of first heading. Further headings will be
  17845. ;; compared to it to determine hierarchy in the list.
  17846. (ref-level (org-reduced-level (org-outline-level))))
  17847. (while (< (point) end)
  17848. (let* ((level (org-reduced-level (org-outline-level)))
  17849. (delta (max 0 (- level ref-level))))
  17850. ;; If current headline is less indented than the first
  17851. ;; one, set it as reference, in order to preserve
  17852. ;; subtrees.
  17853. (when (< level ref-level) (setq ref-level level))
  17854. (replace-match bul t t)
  17855. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17856. ;; Ensure all text down to END (or SECTION-END) belongs
  17857. ;; to the newly created item.
  17858. (let ((section-end (save-excursion
  17859. (or (outline-next-heading) (point)))))
  17860. (forward-line)
  17861. (funcall shift-text
  17862. (+ start-ind (* (1+ delta) bul-len))
  17863. (min end section-end)))))))
  17864. ;; Case 3. Normal line with ARG: turn each non-item line into
  17865. ;; an item.
  17866. (arg
  17867. (while (< (point) end)
  17868. (unless (or (org-at-heading-p) (org-at-item-p))
  17869. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17870. (replace-match
  17871. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17872. (forward-line)))
  17873. ;; Case 4. Normal line without ARG: make the first line of
  17874. ;; region an item, and shift indentation of others
  17875. ;; lines to set them as item's body.
  17876. (t (let* ((bul (org-list-bullet-string "-"))
  17877. (bul-len (length bul))
  17878. (ref-ind (org-get-indentation)))
  17879. (skip-chars-forward " \t")
  17880. (insert bul)
  17881. (forward-line)
  17882. (while (< (point) end)
  17883. ;; Ensure that lines less indented than first one
  17884. ;; still get included in item body.
  17885. (funcall shift-text
  17886. (+ ref-ind bul-len)
  17887. (min end (save-excursion (or (outline-next-heading)
  17888. (point)))))
  17889. (forward-line)))))))))
  17890. (defun org-toggle-heading (&optional nstars)
  17891. "Convert headings to normal text, or items or text to headings.
  17892. If there is no active region, only the current line is considered.
  17893. With a \\[universal-argument] prefix, convert the whole list at
  17894. point into heading.
  17895. In a region:
  17896. - If the first non blank line is an headline, remove the stars
  17897. from all headlines in the region.
  17898. - If it is a normal line turn each and every normal line (i.e. not an
  17899. heading or an item) in the region into a heading.
  17900. - If it is a plain list item, turn all plain list items into headings.
  17901. When converting a line into a heading, the number of stars is chosen
  17902. such that the lines become children of the current entry. However,
  17903. when a prefix argument is given, its value determines the number of
  17904. stars to add."
  17905. (interactive "P")
  17906. (let ((skip-blanks
  17907. (function
  17908. ;; Return beginning of first non-blank line, starting from
  17909. ;; line at POS.
  17910. (lambda (pos)
  17911. (save-excursion
  17912. (goto-char pos)
  17913. (while (org-at-comment-p) (forward-line))
  17914. (skip-chars-forward " \r\t\n")
  17915. (point-at-bol)))))
  17916. beg end toggled)
  17917. ;; Determine boundaries of changes. If a universal prefix has
  17918. ;; been given, put the list in a region. If region ends at a bol,
  17919. ;; do not consider the last line to be in the region.
  17920. (when (and current-prefix-arg (org-at-item-p))
  17921. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  17922. (org-mark-element))
  17923. (if (org-region-active-p)
  17924. (setq beg (funcall skip-blanks (region-beginning))
  17925. end (copy-marker (save-excursion
  17926. (goto-char (region-end))
  17927. (if (bolp) (point) (point-at-eol)))))
  17928. (setq beg (funcall skip-blanks (point-at-bol))
  17929. end (copy-marker (point-at-eol))))
  17930. ;; Ensure inline tasks don't count as headings.
  17931. (org-with-limited-levels
  17932. (save-excursion
  17933. (goto-char beg)
  17934. (cond
  17935. ;; Case 1. Started at an heading: de-star headings.
  17936. ((org-at-heading-p)
  17937. (while (< (point) end)
  17938. (when (org-at-heading-p t)
  17939. (looking-at org-outline-regexp) (replace-match "")
  17940. (setq toggled t))
  17941. (forward-line)))
  17942. ;; Case 2. Started at an item: change items into headlines.
  17943. ;; One star will be added by `org-list-to-subtree'.
  17944. ((org-at-item-p)
  17945. (let* ((stars (make-string
  17946. (if nstars
  17947. ;; subtract the star that will be added again by
  17948. ;; `org-list-to-subtree'
  17949. (1- (prefix-numeric-value current-prefix-arg))
  17950. (or (org-current-level) 0))
  17951. ?*))
  17952. (add-stars
  17953. (cond (nstars "") ; stars from prefix only
  17954. ((equal stars "") "") ; before first heading
  17955. (org-odd-levels-only "*") ; inside heading, odd
  17956. (t "")))) ; inside heading, oddeven
  17957. (while (< (point) end)
  17958. (when (org-at-item-p)
  17959. ;; Pay attention to cases when region ends before list.
  17960. (let* ((struct (org-list-struct))
  17961. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17962. (save-restriction
  17963. (narrow-to-region (point) list-end)
  17964. (insert
  17965. (org-list-to-subtree
  17966. (org-list-parse-list t)
  17967. '(:istart (concat stars add-stars (funcall get-stars depth))
  17968. :icount (concat stars add-stars (funcall get-stars depth)))))))
  17969. (setq toggled t))
  17970. (forward-line))))
  17971. ;; Case 3. Started at normal text: make every line an heading,
  17972. ;; skipping headlines and items.
  17973. (t (let* ((stars (make-string
  17974. (if nstars
  17975. (prefix-numeric-value current-prefix-arg)
  17976. (or (org-current-level) 0))
  17977. ?*))
  17978. (add-stars
  17979. (cond (nstars "") ; stars from prefix only
  17980. ((equal stars "") "*") ; before first heading
  17981. (org-odd-levels-only "**") ; inside heading, odd
  17982. (t "*"))) ; inside heading, oddeven
  17983. (rpl (concat stars add-stars " ")))
  17984. (while (< (point) end)
  17985. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  17986. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17987. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  17988. (forward-line)))))))
  17989. (unless toggled (message "Cannot toggle heading from here"))))
  17990. (defun org-meta-return (&optional arg)
  17991. "Insert a new heading or wrap a region in a table.
  17992. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17993. See the individual commands for more information."
  17994. (interactive "P")
  17995. (cond
  17996. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17997. ((or (org-at-drawer-p) (org-at-property-p))
  17998. (newline-and-indent))
  17999. ((org-at-table-p)
  18000. (call-interactively 'org-table-wrap-region))
  18001. (t (call-interactively 'org-insert-heading))))
  18002. ;;; Menu entries
  18003. (defsubst org-in-subtree-not-table-p ()
  18004. "Are we in a subtree and not in a table?"
  18005. (and (not (org-before-first-heading-p))
  18006. (not (org-at-table-p))))
  18007. ;; Define the Org-mode menus
  18008. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18009. '("Tbl"
  18010. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18011. ["Next Field" org-cycle (org-at-table-p)]
  18012. ["Previous Field" org-shifttab (org-at-table-p)]
  18013. ["Next Row" org-return (org-at-table-p)]
  18014. "--"
  18015. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18016. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18017. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18018. "--"
  18019. ("Column"
  18020. ["Move Column Left" org-metaleft (org-at-table-p)]
  18021. ["Move Column Right" org-metaright (org-at-table-p)]
  18022. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18023. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18024. ("Row"
  18025. ["Move Row Up" org-metaup (org-at-table-p)]
  18026. ["Move Row Down" org-metadown (org-at-table-p)]
  18027. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18028. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18029. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18030. "--"
  18031. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18032. ("Rectangle"
  18033. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18034. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18035. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18036. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18037. "--"
  18038. ("Calculate"
  18039. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18040. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18041. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18042. "--"
  18043. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18044. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18045. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18046. "--"
  18047. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18048. "--"
  18049. ["Sum Column/Rectangle" org-table-sum
  18050. (or (org-at-table-p) (org-region-active-p))]
  18051. ["Which Column?" org-table-current-column (org-at-table-p)])
  18052. ["Debug Formulas"
  18053. org-table-toggle-formula-debugger
  18054. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18055. ["Show Col/Row Numbers"
  18056. org-table-toggle-coordinate-overlays
  18057. :style toggle
  18058. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18059. "--"
  18060. ["Create" org-table-create (and (not (org-at-table-p))
  18061. org-enable-table-editor)]
  18062. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18063. ["Import from File" org-table-import (not (org-at-table-p))]
  18064. ["Export to File" org-table-export (org-at-table-p)]
  18065. "--"
  18066. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18067. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18068. '("Org"
  18069. ("Show/Hide"
  18070. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18071. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18072. ["Sparse Tree..." org-sparse-tree t]
  18073. ["Reveal Context" org-reveal t]
  18074. ["Show All" show-all t]
  18075. "--"
  18076. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18077. "--"
  18078. ["New Heading" org-insert-heading t]
  18079. ("Navigate Headings"
  18080. ["Up" outline-up-heading t]
  18081. ["Next" outline-next-visible-heading t]
  18082. ["Previous" outline-previous-visible-heading t]
  18083. ["Next Same Level" outline-forward-same-level t]
  18084. ["Previous Same Level" outline-backward-same-level t]
  18085. "--"
  18086. ["Jump" org-goto t])
  18087. ("Edit Structure"
  18088. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18089. "--"
  18090. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18091. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18092. "--"
  18093. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18094. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18095. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18096. "--"
  18097. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18098. "--"
  18099. ["Copy visible text" org-copy-visible t]
  18100. "--"
  18101. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18102. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18103. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18104. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18105. "--"
  18106. ["Sort Region/Children" org-sort t]
  18107. "--"
  18108. ["Convert to odd levels" org-convert-to-odd-levels t]
  18109. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18110. ("Editing"
  18111. ["Emphasis..." org-emphasize t]
  18112. ["Edit Source Example" org-edit-special t]
  18113. "--"
  18114. ["Footnote new/jump" org-footnote-action t]
  18115. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18116. ("Archive"
  18117. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18118. "--"
  18119. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18120. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18121. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18122. )
  18123. "--"
  18124. ("Hyperlinks"
  18125. ["Store Link (Global)" org-store-link t]
  18126. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18127. ["Insert Link" org-insert-link t]
  18128. ["Follow Link" org-open-at-point t]
  18129. "--"
  18130. ["Next link" org-next-link t]
  18131. ["Previous link" org-previous-link t]
  18132. "--"
  18133. ["Descriptive Links"
  18134. org-toggle-link-display
  18135. :style radio
  18136. :selected org-descriptive-links
  18137. ]
  18138. ["Literal Links"
  18139. org-toggle-link-display
  18140. :style radio
  18141. :selected (not org-descriptive-links)])
  18142. "--"
  18143. ("TODO Lists"
  18144. ["TODO/DONE/-" org-todo t]
  18145. ("Select keyword"
  18146. ["Next keyword" org-shiftright (org-at-heading-p)]
  18147. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18148. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18149. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18150. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18151. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18152. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18153. "--"
  18154. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18155. :selected org-enforce-todo-dependencies :style toggle :active t]
  18156. "Settings for tree at point"
  18157. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18158. :selected (org-entry-get nil "ORDERED")
  18159. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18160. ["Do Children parallel" org-toggle-ordered-property :style radio
  18161. :selected (not (org-entry-get nil "ORDERED"))
  18162. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18163. "--"
  18164. ["Set Priority" org-priority t]
  18165. ["Priority Up" org-shiftup t]
  18166. ["Priority Down" org-shiftdown t]
  18167. "--"
  18168. ["Get news from all feeds" org-feed-update-all t]
  18169. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18170. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18171. ("TAGS and Properties"
  18172. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18173. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18174. "--"
  18175. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18176. ["Column view of properties" org-columns t]
  18177. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18178. ("Dates and Scheduling"
  18179. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18180. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18181. ("Change Date"
  18182. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18183. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18184. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18185. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18186. ["Compute Time Range" org-evaluate-time-range t]
  18187. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18188. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18189. "--"
  18190. ["Custom time format" org-toggle-time-stamp-overlays
  18191. :style radio :selected org-display-custom-times]
  18192. "--"
  18193. ["Goto Calendar" org-goto-calendar t]
  18194. ["Date from Calendar" org-date-from-calendar t]
  18195. "--"
  18196. ["Start/Restart Timer" org-timer-start t]
  18197. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18198. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18199. ["Insert Timer String" org-timer t]
  18200. ["Insert Timer Item" org-timer-item t])
  18201. ("Logging work"
  18202. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18203. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18204. ["Clock out" org-clock-out t]
  18205. ["Clock cancel" org-clock-cancel t]
  18206. "--"
  18207. ["Mark as default task" org-clock-mark-default-task t]
  18208. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18209. ["Goto running clock" org-clock-goto t]
  18210. "--"
  18211. ["Display times" org-clock-display t]
  18212. ["Create clock table" org-clock-report t]
  18213. "--"
  18214. ["Record DONE time"
  18215. (progn (setq org-log-done (not org-log-done))
  18216. (message "Switching to %s will %s record a timestamp"
  18217. (car org-done-keywords)
  18218. (if org-log-done "automatically" "not")))
  18219. :style toggle :selected org-log-done])
  18220. "--"
  18221. ["Agenda Command..." org-agenda t]
  18222. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18223. ("File List for Agenda")
  18224. ("Special views current file"
  18225. ["TODO Tree" org-show-todo-tree t]
  18226. ["Check Deadlines" org-check-deadlines t]
  18227. ["Timeline" org-timeline t]
  18228. ["Tags/Property tree" org-match-sparse-tree t])
  18229. "--"
  18230. ["Export/Publish..." org-export t]
  18231. ("LaTeX"
  18232. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18233. :selected org-cdlatex-mode]
  18234. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18235. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18236. ["Modify math symbol" org-cdlatex-math-modify
  18237. (org-inside-LaTeX-fragment-p)]
  18238. ["Insert citation" org-reftex-citation t]
  18239. "--"
  18240. ["Template for BEAMER" (progn (require 'org-beamer)
  18241. (org-insert-beamer-options-template)) t])
  18242. "--"
  18243. ("MobileOrg"
  18244. ["Push Files and Views" org-mobile-push t]
  18245. ["Get Captured and Flagged" org-mobile-pull t]
  18246. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18247. "--"
  18248. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18249. "--"
  18250. ("Documentation"
  18251. ["Show Version" org-version t]
  18252. ["Info Documentation" org-info t])
  18253. ("Customize"
  18254. ["Browse Org Group" org-customize t]
  18255. "--"
  18256. ["Expand This Menu" org-create-customize-menu
  18257. (fboundp 'customize-menu-create)])
  18258. ["Send bug report" org-submit-bug-report t]
  18259. "--"
  18260. ("Refresh/Reload"
  18261. ["Refresh setup current buffer" org-mode-restart t]
  18262. ["Reload Org (after update)" org-reload t]
  18263. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  18264. ))
  18265. (defun org-info (&optional node)
  18266. "Read documentation for Org-mode in the info system.
  18267. With optional NODE, go directly to that node."
  18268. (interactive)
  18269. (info (format "(org)%s" (or node ""))))
  18270. ;;;###autoload
  18271. (defun org-submit-bug-report ()
  18272. "Submit a bug report on Org-mode via mail.
  18273. Don't hesitate to report any problems or inaccurate documentation.
  18274. If you don't have setup sending mail from (X)Emacs, please copy the
  18275. output buffer into your mail program, as it gives us important
  18276. information about your Org-mode version and configuration."
  18277. (interactive)
  18278. (require 'reporter)
  18279. (org-load-modules-maybe)
  18280. (org-require-autoloaded-modules)
  18281. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18282. (reporter-submit-bug-report
  18283. "emacs-orgmode@gnu.org"
  18284. (org-version nil 'full)
  18285. (let (list)
  18286. (save-window-excursion
  18287. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18288. (delete-other-windows)
  18289. (erase-buffer)
  18290. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18291. We would like to add your full Org-mode and Outline configuration to the
  18292. bug report. This greatly simplifies the work of the maintainer and
  18293. other experts on the mailing list.
  18294. HOWEVER, some variables you have customized may contain private
  18295. information. The names of customers, colleagues, or friends, might
  18296. appear in the form of file names, tags, todo states, or search strings.
  18297. If you answer yes to the prompt, you might want to check and remove
  18298. such private information before sending the email.")
  18299. (add-text-properties (point-min) (point-max) '(face org-warning))
  18300. (when (yes-or-no-p "Include your Org-mode configuration ")
  18301. (mapatoms
  18302. (lambda (v)
  18303. (and (boundp v)
  18304. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18305. (or (and (symbol-value v)
  18306. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18307. (and
  18308. (get v 'custom-type) (get v 'standard-value)
  18309. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18310. (push v list)))))
  18311. (kill-buffer (get-buffer "*Warn about privacy*"))
  18312. list))
  18313. nil nil
  18314. "Remember to cover the basics, that is, what you expected to happen and
  18315. what in fact did happen. You don't know how to make a good report? See
  18316. http://orgmode.org/manual/Feedback.html#Feedback
  18317. Your bug report will be posted to the Org-mode mailing list.
  18318. ------------------------------------------------------------------------")
  18319. (save-excursion
  18320. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18321. (replace-match "\\1Bug: \\3 [\\2]")))))
  18322. (defun org-install-agenda-files-menu ()
  18323. (let ((bl (buffer-list)))
  18324. (save-excursion
  18325. (while bl
  18326. (set-buffer (pop bl))
  18327. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18328. (when (derived-mode-p 'org-mode)
  18329. (easy-menu-change
  18330. '("Org") "File List for Agenda"
  18331. (append
  18332. (list
  18333. ["Edit File List" (org-edit-agenda-file-list) t]
  18334. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18335. ["Remove Current File from List" org-remove-file t]
  18336. ["Cycle through agenda files" org-cycle-agenda-files t]
  18337. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18338. "--")
  18339. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18340. ;;;; Documentation
  18341. (defun org-require-autoloaded-modules ()
  18342. (interactive)
  18343. (mapc 'require
  18344. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18345. org-docbook org-exp org-html org-icalendar
  18346. org-id org-latex
  18347. org-publish org-remember org-table
  18348. org-timer org-xoxo)))
  18349. ;;;###autoload
  18350. (defun org-reload (&optional uncompiled)
  18351. "Reload all org lisp files.
  18352. With prefix arg UNCOMPILED, load the uncompiled versions."
  18353. (interactive "P")
  18354. (require 'find-func)
  18355. (let* ((file-re "^org\\(-.*\\)?\\.el")
  18356. (dir-org (file-name-directory (org-find-library-dir "org")))
  18357. (dir-org-contrib (ignore-errors
  18358. (file-name-directory
  18359. (org-find-library-dir "org-contribdir"))))
  18360. (babel-files
  18361. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  18362. (append (list nil "comint" "eval" "exp" "keys"
  18363. "lob" "ref" "table" "tangle")
  18364. (delq nil
  18365. (mapcar
  18366. (lambda (lang)
  18367. (when (cdr lang) (symbol-name (car lang))))
  18368. org-babel-load-languages)))))
  18369. (files
  18370. (append babel-files
  18371. (and dir-org-contrib
  18372. (directory-files dir-org-contrib t file-re))
  18373. (directory-files dir-org t file-re)))
  18374. (remove-re (concat (if (featurep 'xemacs)
  18375. "org-colview" "org-colview-xemacs")
  18376. "\\'")))
  18377. (setq files (mapcar 'file-name-sans-extension files))
  18378. (setq files (mapcar
  18379. (lambda (x) (if (string-match remove-re x) nil x))
  18380. files))
  18381. (setq files (delq nil files))
  18382. (mapc
  18383. (lambda (f)
  18384. (when (featurep (intern (file-name-nondirectory f)))
  18385. (if (and (not uncompiled)
  18386. (file-exists-p (concat f ".elc")))
  18387. (load (concat f ".elc") nil nil 'nosuffix)
  18388. (load (concat f ".el") nil nil 'nosuffix))))
  18389. files)
  18390. (load (concat dir-org "org-version.el") 'noerror nil 'nosuffix))
  18391. (org-version nil 'full 'message))
  18392. ;;;###autoload
  18393. (defun org-customize ()
  18394. "Call the customize function with org as argument."
  18395. (interactive)
  18396. (org-load-modules-maybe)
  18397. (org-require-autoloaded-modules)
  18398. (customize-browse 'org))
  18399. (defun org-create-customize-menu ()
  18400. "Create a full customization menu for Org-mode, insert it into the menu."
  18401. (interactive)
  18402. (org-load-modules-maybe)
  18403. (org-require-autoloaded-modules)
  18404. (if (fboundp 'customize-menu-create)
  18405. (progn
  18406. (easy-menu-change
  18407. '("Org") "Customize"
  18408. `(["Browse Org group" org-customize t]
  18409. "--"
  18410. ,(customize-menu-create 'org)
  18411. ["Set" Custom-set t]
  18412. ["Save" Custom-save t]
  18413. ["Reset to Current" Custom-reset-current t]
  18414. ["Reset to Saved" Custom-reset-saved t]
  18415. ["Reset to Standard Settings" Custom-reset-standard t]))
  18416. (message "\"Org\"-menu now contains full customization menu"))
  18417. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18418. ;;;; Miscellaneous stuff
  18419. ;;; Generally useful functions
  18420. (defun org-get-at-bol (property)
  18421. "Get text property PROPERTY at beginning of line."
  18422. (get-text-property (point-at-bol) property))
  18423. (defun org-find-text-property-in-string (prop s)
  18424. "Return the first non-nil value of property PROP in string S."
  18425. (or (get-text-property 0 prop s)
  18426. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18427. prop s)))
  18428. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18429. "Display the given MESSAGE as a warning."
  18430. (if (fboundp 'display-warning)
  18431. (display-warning 'org message
  18432. (if (featurep 'xemacs) 'warning :warning))
  18433. (let ((buf (get-buffer-create "*Org warnings*")))
  18434. (with-current-buffer buf
  18435. (goto-char (point-max))
  18436. (insert "Warning (Org): " message)
  18437. (unless (bolp)
  18438. (newline)))
  18439. (display-buffer buf)
  18440. (sit-for 0))))
  18441. (defun org-eval (form)
  18442. "Eval FORM and return result."
  18443. (condition-case error
  18444. (eval form)
  18445. (error (format "%%![Error: %s]" error))))
  18446. (defun org-in-clocktable-p ()
  18447. "Check if the cursor is in a clocktable."
  18448. (let ((pos (point)) start)
  18449. (save-excursion
  18450. (end-of-line 1)
  18451. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18452. (setq start (match-beginning 0))
  18453. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18454. (>= (match-end 0) pos)
  18455. start))))
  18456. (defun org-in-commented-line ()
  18457. "Is point in a line starting with `#'?"
  18458. (equal (char-after (point-at-bol)) ?#))
  18459. (defun org-in-indented-comment-line ()
  18460. "Is point in a line starting with `#' after some white space?"
  18461. (save-excursion
  18462. (save-match-data
  18463. (goto-char (point-at-bol))
  18464. (looking-at "[ \t]*#"))))
  18465. (defun org-in-verbatim-emphasis ()
  18466. (save-match-data
  18467. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18468. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18469. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18470. (if (and marker (marker-buffer marker)
  18471. (buffer-live-p (marker-buffer marker)))
  18472. (progn
  18473. (org-pop-to-buffer-same-window (marker-buffer marker))
  18474. (if (or (> marker (point-max)) (< marker (point-min)))
  18475. (widen))
  18476. (goto-char marker)
  18477. (org-show-context 'org-goto))
  18478. (if bookmark
  18479. (bookmark-jump bookmark)
  18480. (error "Cannot find location"))))
  18481. (defun org-quote-csv-field (s)
  18482. "Quote field for inclusion in CSV material."
  18483. (if (string-match "[\",]" s)
  18484. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18485. s))
  18486. (defun org-force-self-insert (N)
  18487. "Needed to enforce self-insert under remapping."
  18488. (interactive "p")
  18489. (self-insert-command N))
  18490. (defun org-string-width (s)
  18491. "Compute width of string, ignoring invisible characters.
  18492. This ignores character with invisibility property `org-link', and also
  18493. characters with property `org-cwidth', because these will become invisible
  18494. upon the next fontification round."
  18495. (let (b l)
  18496. (when (or (eq t buffer-invisibility-spec)
  18497. (assq 'org-link buffer-invisibility-spec))
  18498. (while (setq b (text-property-any 0 (length s)
  18499. 'invisible 'org-link s))
  18500. (setq s (concat (substring s 0 b)
  18501. (substring s (or (next-single-property-change
  18502. b 'invisible s) (length s)))))))
  18503. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18504. (setq s (concat (substring s 0 b)
  18505. (substring s (or (next-single-property-change
  18506. b 'org-cwidth s) (length s))))))
  18507. (setq l (string-width s) b -1)
  18508. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18509. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18510. l))
  18511. (defun org-shorten-string (s maxlength)
  18512. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18513. If the string is shorter or has length MAXLENGTH, just return the
  18514. original string. If it is longer, the functions finds a space in the
  18515. string, breaks this string off at that locations and adds three dots
  18516. as ellipsis. Including the ellipsis, the string will not be longer
  18517. than MAXLENGTH. If finding a good breaking point in the string does
  18518. not work, the string is just chopped off in the middle of a word
  18519. if necessary."
  18520. (if (<= (length s) maxlength)
  18521. s
  18522. (let* ((n (max (- maxlength 4) 1))
  18523. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18524. (if (string-match re s)
  18525. (concat (match-string 1 s) "...")
  18526. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18527. (defun org-get-indentation (&optional line)
  18528. "Get the indentation of the current line, interpreting tabs.
  18529. When LINE is given, assume it represents a line and compute its indentation."
  18530. (if line
  18531. (if (string-match "^ *" (org-remove-tabs line))
  18532. (match-end 0))
  18533. (save-excursion
  18534. (beginning-of-line 1)
  18535. (skip-chars-forward " \t")
  18536. (current-column))))
  18537. (defun org-get-string-indentation (s)
  18538. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18539. (let ((n -1) (i 0) (w tab-width) c)
  18540. (catch 'exit
  18541. (while (< (setq n (1+ n)) (length s))
  18542. (setq c (aref s n))
  18543. (cond ((= c ?\ ) (setq i (1+ i)))
  18544. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18545. (t (throw 'exit t)))))
  18546. i))
  18547. (defun org-remove-tabs (s &optional width)
  18548. "Replace tabulators in S with spaces.
  18549. Assumes that s is a single line, starting in column 0."
  18550. (setq width (or width tab-width))
  18551. (while (string-match "\t" s)
  18552. (setq s (replace-match
  18553. (make-string
  18554. (- (* width (/ (+ (match-beginning 0) width) width))
  18555. (match-beginning 0)) ?\ )
  18556. t t s)))
  18557. s)
  18558. (defun org-fix-indentation (line ind)
  18559. "Fix indentation in LINE.
  18560. IND is a cons cell with target and minimum indentation.
  18561. If the current indentation in LINE is smaller than the minimum,
  18562. leave it alone. If it is larger than ind, set it to the target."
  18563. (let* ((l (org-remove-tabs line))
  18564. (i (org-get-indentation l))
  18565. (i1 (car ind)) (i2 (cdr ind)))
  18566. (if (>= i i2) (setq l (substring line i2)))
  18567. (if (> i1 0)
  18568. (concat (make-string i1 ?\ ) l)
  18569. l)))
  18570. (defun org-remove-indentation (code &optional n)
  18571. "Remove the maximum common indentation from the lines in CODE.
  18572. N may optionally be the number of spaces to remove."
  18573. (with-temp-buffer
  18574. (insert code)
  18575. (org-do-remove-indentation n)
  18576. (buffer-string)))
  18577. (defun org-do-remove-indentation (&optional n)
  18578. "Remove the maximum common indentation from the buffer."
  18579. (untabify (point-min) (point-max))
  18580. (let ((min 10000) re)
  18581. (if n
  18582. (setq min n)
  18583. (goto-char (point-min))
  18584. (while (re-search-forward "^ *[^ \n]" nil t)
  18585. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18586. (unless (or (= min 0) (= min 10000))
  18587. (setq re (format "^ \\{%d\\}" min))
  18588. (goto-char (point-min))
  18589. (while (re-search-forward re nil t)
  18590. (replace-match "")
  18591. (end-of-line 1))
  18592. min)))
  18593. (defun org-fill-template (template alist)
  18594. "Find each %key of ALIST in TEMPLATE and replace it."
  18595. (let ((case-fold-search nil)
  18596. entry key value)
  18597. (setq alist (sort (copy-sequence alist)
  18598. (lambda (a b) (< (length (car a)) (length (car b))))))
  18599. (while (setq entry (pop alist))
  18600. (setq template
  18601. (replace-regexp-in-string
  18602. (concat "%" (regexp-quote (car entry)))
  18603. (or (cdr entry) "") template t t)))
  18604. template))
  18605. (defun org-base-buffer (buffer)
  18606. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18607. (if (not buffer)
  18608. buffer
  18609. (or (buffer-base-buffer buffer)
  18610. buffer)))
  18611. (defun org-trim (s)
  18612. "Remove whitespace at beginning and end of string."
  18613. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18614. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18615. s)
  18616. (defun org-wrap (string &optional width lines)
  18617. "Wrap string to either a number of lines, or a width in characters.
  18618. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18619. that costs. If there is a word longer than WIDTH, the text is actually
  18620. wrapped to the length of that word.
  18621. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18622. many lines, whatever width that takes.
  18623. The return value is a list of lines, without newlines at the end."
  18624. (let* ((words (org-split-string string "[ \t\n]+"))
  18625. (maxword (apply 'max (mapcar 'org-string-width words)))
  18626. w ll)
  18627. (cond (width
  18628. (org-do-wrap words (max maxword width)))
  18629. (lines
  18630. (setq w maxword)
  18631. (setq ll (org-do-wrap words maxword))
  18632. (if (<= (length ll) lines)
  18633. ll
  18634. (setq ll words)
  18635. (while (> (length ll) lines)
  18636. (setq w (1+ w))
  18637. (setq ll (org-do-wrap words w)))
  18638. ll))
  18639. (t (error "Cannot wrap this")))))
  18640. (defun org-do-wrap (words width)
  18641. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18642. (let (lines line)
  18643. (while words
  18644. (setq line (pop words))
  18645. (while (and words (< (+ (length line) (length (car words))) width))
  18646. (setq line (concat line " " (pop words))))
  18647. (setq lines (push line lines)))
  18648. (nreverse lines)))
  18649. (defun org-split-string (string &optional separators)
  18650. "Splits STRING into substrings at SEPARATORS.
  18651. No empty strings are returned if there are matches at the beginning
  18652. and end of string."
  18653. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18654. (start 0)
  18655. notfirst
  18656. (list nil))
  18657. (while (and (string-match rexp string
  18658. (if (and notfirst
  18659. (= start (match-beginning 0))
  18660. (< start (length string)))
  18661. (1+ start) start))
  18662. (< (match-beginning 0) (length string)))
  18663. (setq notfirst t)
  18664. (or (eq (match-beginning 0) 0)
  18665. (and (eq (match-beginning 0) (match-end 0))
  18666. (eq (match-beginning 0) start))
  18667. (setq list
  18668. (cons (substring string start (match-beginning 0))
  18669. list)))
  18670. (setq start (match-end 0)))
  18671. (or (eq start (length string))
  18672. (setq list
  18673. (cons (substring string start)
  18674. list)))
  18675. (nreverse list)))
  18676. (defun org-quote-vert (s)
  18677. "Replace \"|\" with \"\\vert\"."
  18678. (while (string-match "|" s)
  18679. (setq s (replace-match "\\vert" t t s)))
  18680. s)
  18681. (defun org-uuidgen-p (s)
  18682. "Is S an ID created by UUIDGEN?"
  18683. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18684. (defun org-in-src-block-p (&optional inside)
  18685. "Whether point is in a code source block.
  18686. When INSIDE is non-nil, don't consider we are within a src block
  18687. when point is at #+BEGIN_SRC or #+END_SRC."
  18688. (let ((case-fold-search t) ov)
  18689. (or (and (setq ov (overlays-at (point)))
  18690. (memq 'org-block-background
  18691. (overlay-properties (car ov))))
  18692. (and (not inside)
  18693. (save-match-data
  18694. (save-excursion
  18695. (beginning-of-line)
  18696. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  18697. (defun org-context ()
  18698. "Return a list of contexts of the current cursor position.
  18699. If several contexts apply, all are returned.
  18700. Each context entry is a list with a symbol naming the context, and
  18701. two positions indicating start and end of the context. Possible
  18702. contexts are:
  18703. :headline anywhere in a headline
  18704. :headline-stars on the leading stars in a headline
  18705. :todo-keyword on a TODO keyword (including DONE) in a headline
  18706. :tags on the TAGS in a headline
  18707. :priority on the priority cookie in a headline
  18708. :item on the first line of a plain list item
  18709. :item-bullet on the bullet/number of a plain list item
  18710. :checkbox on the checkbox in a plain list item
  18711. :table in an org-mode table
  18712. :table-special on a special filed in a table
  18713. :table-table in a table.el table
  18714. :clocktable in a clocktable
  18715. :src-block in a source block
  18716. :link on a hyperlink
  18717. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18718. :target on a <<target>>
  18719. :radio-target on a <<<radio-target>>>
  18720. :latex-fragment on a LaTeX fragment
  18721. :latex-preview on a LaTeX fragment with overlaid preview image
  18722. This function expects the position to be visible because it uses font-lock
  18723. faces as a help to recognize the following contexts: :table-special, :link,
  18724. and :keyword."
  18725. (let* ((f (get-text-property (point) 'face))
  18726. (faces (if (listp f) f (list f)))
  18727. (case-fold-search t)
  18728. (p (point)) clist o)
  18729. ;; First the large context
  18730. (cond
  18731. ((org-at-heading-p t)
  18732. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18733. (when (progn
  18734. (beginning-of-line 1)
  18735. (looking-at org-todo-line-tags-regexp))
  18736. (push (org-point-in-group p 1 :headline-stars) clist)
  18737. (push (org-point-in-group p 2 :todo-keyword) clist)
  18738. (push (org-point-in-group p 4 :tags) clist))
  18739. (goto-char p)
  18740. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18741. (if (looking-at "\\[#[A-Z0-9]\\]")
  18742. (push (org-point-in-group p 0 :priority) clist)))
  18743. ((org-at-item-p)
  18744. (push (org-point-in-group p 2 :item-bullet) clist)
  18745. (push (list :item (point-at-bol)
  18746. (save-excursion (org-end-of-item) (point)))
  18747. clist)
  18748. (and (org-at-item-checkbox-p)
  18749. (push (org-point-in-group p 0 :checkbox) clist)))
  18750. ((org-at-table-p)
  18751. (push (list :table (org-table-begin) (org-table-end)) clist)
  18752. (if (memq 'org-formula faces)
  18753. (push (list :table-special
  18754. (previous-single-property-change p 'face)
  18755. (next-single-property-change p 'face)) clist)))
  18756. ((org-at-table-p 'any)
  18757. (push (list :table-table) clist)))
  18758. (goto-char p)
  18759. (let ((case-fold-search t))
  18760. ;; New the "medium" contexts: clocktables, source blocks
  18761. (cond ((org-in-clocktable-p)
  18762. (push (list :clocktable
  18763. (and (or (looking-at "#\\+BEGIN: clocktable")
  18764. (search-backward "#+BEGIN: clocktable" nil t))
  18765. (match-beginning 0))
  18766. (and (re-search-forward "#\\+END:?" nil t)
  18767. (match-end 0))) clist))
  18768. ((org-in-src-block-p)
  18769. (push (list :src-block
  18770. (and (or (looking-at "#\\+BEGIN_SRC")
  18771. (search-backward "#+BEGIN_SRC" nil t))
  18772. (match-beginning 0))
  18773. (and (search-forward "#+END_SRC" nil t)
  18774. (match-beginning 0))) clist))))
  18775. (goto-char p)
  18776. ;; Now the small context
  18777. (cond
  18778. ((org-at-timestamp-p)
  18779. (push (org-point-in-group p 0 :timestamp) clist))
  18780. ((memq 'org-link faces)
  18781. (push (list :link
  18782. (previous-single-property-change p 'face)
  18783. (next-single-property-change p 'face)) clist))
  18784. ((memq 'org-special-keyword faces)
  18785. (push (list :keyword
  18786. (previous-single-property-change p 'face)
  18787. (next-single-property-change p 'face)) clist))
  18788. ((org-at-target-p)
  18789. (push (org-point-in-group p 0 :target) clist)
  18790. (goto-char (1- (match-beginning 0)))
  18791. (if (looking-at org-radio-target-regexp)
  18792. (push (org-point-in-group p 0 :radio-target) clist))
  18793. (goto-char p))
  18794. ((setq o (car (delq nil
  18795. (mapcar
  18796. (lambda (x)
  18797. (if (memq x org-latex-fragment-image-overlays) x))
  18798. (overlays-at (point))))))
  18799. (push (list :latex-fragment
  18800. (overlay-start o) (overlay-end o)) clist)
  18801. (push (list :latex-preview
  18802. (overlay-start o) (overlay-end o)) clist))
  18803. ((org-inside-LaTeX-fragment-p)
  18804. ;; FIXME: positions wrong.
  18805. (push (list :latex-fragment (point) (point)) clist)))
  18806. (setq clist (nreverse (delq nil clist)))
  18807. clist))
  18808. ;; FIXME: Compare with at-regexp-p Do we need both?
  18809. (defun org-in-regexp (re &optional nlines visually)
  18810. "Check if point is inside a match of regexp.
  18811. Normally only the current line is checked, but you can include NLINES extra
  18812. lines both before and after point into the search.
  18813. If VISUALLY is set, require that the cursor is not after the match but
  18814. really on, so that the block visually is on the match."
  18815. (catch 'exit
  18816. (let ((pos (point))
  18817. (eol (point-at-eol (+ 1 (or nlines 0))))
  18818. (inc (if visually 1 0)))
  18819. (save-excursion
  18820. (beginning-of-line (- 1 (or nlines 0)))
  18821. (while (re-search-forward re eol t)
  18822. (if (and (<= (match-beginning 0) pos)
  18823. (>= (+ inc (match-end 0)) pos))
  18824. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18825. (defun org-at-regexp-p (regexp)
  18826. "Is point inside a match of REGEXP in the current line?"
  18827. (catch 'exit
  18828. (save-excursion
  18829. (let ((pos (point)) (end (point-at-eol)))
  18830. (beginning-of-line 1)
  18831. (while (re-search-forward regexp end t)
  18832. (if (and (<= (match-beginning 0) pos)
  18833. (>= (match-end 0) pos))
  18834. (throw 'exit t)))
  18835. nil))))
  18836. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18837. "Non-nil when point is between matches of START-RE and END-RE.
  18838. Also return a non-nil value when point is on one of the matches.
  18839. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18840. buffer positions. Default values are the positions of headlines
  18841. surrounding the point.
  18842. The functions returns a cons cell whose car (resp. cdr) is the
  18843. position before START-RE (resp. after END-RE)."
  18844. (save-match-data
  18845. (let ((pos (point))
  18846. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18847. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18848. beg end)
  18849. (save-excursion
  18850. ;; Point is on a block when on START-RE or if START-RE can be
  18851. ;; found before it...
  18852. (and (or (org-at-regexp-p start-re)
  18853. (re-search-backward start-re limit-up t))
  18854. (setq beg (match-beginning 0))
  18855. ;; ... and END-RE after it...
  18856. (goto-char (match-end 0))
  18857. (re-search-forward end-re limit-down t)
  18858. (> (setq end (match-end 0)) pos)
  18859. ;; ... without another START-RE in-between.
  18860. (goto-char (match-beginning 0))
  18861. (not (re-search-backward start-re (1+ beg) t))
  18862. ;; Return value.
  18863. (cons beg end))))))
  18864. (defun org-in-block-p (names)
  18865. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18866. NAMES is a list of strings containing names of blocks.
  18867. Return first block name matched, or nil. Beware that in case of
  18868. nested blocks, the returned name may not belong to the closest
  18869. block from point."
  18870. (save-match-data
  18871. (catch 'exit
  18872. (let ((case-fold-search t)
  18873. (lim-up (save-excursion (outline-previous-heading)))
  18874. (lim-down (save-excursion (outline-next-heading))))
  18875. (mapc (lambda (name)
  18876. (let ((n (regexp-quote name)))
  18877. (when (org-between-regexps-p
  18878. (concat "^[ \t]*#\\+begin_" n)
  18879. (concat "^[ \t]*#\\+end_" n)
  18880. lim-up lim-down)
  18881. (throw 'exit n))))
  18882. names))
  18883. nil)))
  18884. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18885. "Call `multi-occur' with buffers for all agenda files."
  18886. (interactive "sOrg-files matching: \np")
  18887. (let* ((files (org-agenda-files))
  18888. (tnames (mapcar 'file-truename files))
  18889. (extra org-agenda-text-search-extra-files)
  18890. f)
  18891. (when (eq (car extra) 'agenda-archives)
  18892. (setq extra (cdr extra))
  18893. (setq files (org-add-archive-files files)))
  18894. (while (setq f (pop extra))
  18895. (unless (member (file-truename f) tnames)
  18896. (add-to-list 'files f 'append)
  18897. (add-to-list 'tnames (file-truename f) 'append)))
  18898. (multi-occur
  18899. (mapcar (lambda (x)
  18900. (with-current-buffer
  18901. (or (get-file-buffer x) (find-file-noselect x))
  18902. (widen)
  18903. (current-buffer)))
  18904. files)
  18905. regexp)))
  18906. (if (boundp 'occur-mode-find-occurrence-hook)
  18907. ;; Emacs 23
  18908. (add-hook 'occur-mode-find-occurrence-hook
  18909. (lambda ()
  18910. (when (derived-mode-p 'org-mode)
  18911. (org-reveal))))
  18912. ;; Emacs 22
  18913. (defadvice occur-mode-goto-occurrence
  18914. (after org-occur-reveal activate)
  18915. (and (derived-mode-p 'org-mode) (org-reveal)))
  18916. (defadvice occur-mode-goto-occurrence-other-window
  18917. (after org-occur-reveal activate)
  18918. (and (derived-mode-p 'org-mode) (org-reveal)))
  18919. (defadvice occur-mode-display-occurrence
  18920. (after org-occur-reveal activate)
  18921. (when (derived-mode-p 'org-mode)
  18922. (let ((pos (occur-mode-find-occurrence)))
  18923. (with-current-buffer (marker-buffer pos)
  18924. (save-excursion
  18925. (goto-char pos)
  18926. (org-reveal)))))))
  18927. (defun org-occur-link-in-agenda-files ()
  18928. "Create a link and search for it in the agendas.
  18929. The link is not stored in `org-stored-links', it is just created
  18930. for the search purpose."
  18931. (interactive)
  18932. (let ((link (condition-case nil
  18933. (org-store-link nil)
  18934. (error "Unable to create a link to here"))))
  18935. (org-occur-in-agenda-files (regexp-quote link))))
  18936. (defun org-uniquify (list)
  18937. "Remove duplicate elements from LIST."
  18938. (let (res)
  18939. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18940. res))
  18941. (defun org-delete-all (elts list)
  18942. "Remove all elements in ELTS from LIST."
  18943. (while elts
  18944. (setq list (delete (pop elts) list)))
  18945. list)
  18946. (defun org-count (cl-item cl-seq)
  18947. "Count the number of occurrences of ITEM in SEQ.
  18948. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18949. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18950. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18951. (while (< cl-start cl-end)
  18952. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18953. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18954. (setq cl-start (1+ cl-start)))
  18955. cl-count))
  18956. (defun org-remove-if (predicate seq)
  18957. "Remove everything from SEQ that fulfills PREDICATE."
  18958. (let (res e)
  18959. (while seq
  18960. (setq e (pop seq))
  18961. (if (not (funcall predicate e)) (push e res)))
  18962. (nreverse res)))
  18963. (defun org-remove-if-not (predicate seq)
  18964. "Remove everything from SEQ that does not fulfill PREDICATE."
  18965. (let (res e)
  18966. (while seq
  18967. (setq e (pop seq))
  18968. (if (funcall predicate e) (push e res)))
  18969. (nreverse res)))
  18970. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18971. "Reduce two-argument FUNCTION across SEQ.
  18972. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18973. \":initial-value\" removed."
  18974. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18975. (cadr (memq :initial-value cl-keys)))
  18976. (cl-seq (pop cl-seq))
  18977. (t (funcall cl-func)))))
  18978. (while cl-seq
  18979. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18980. cl-accum))
  18981. (defun org-back-over-empty-lines ()
  18982. "Move backwards over whitespace, to the beginning of the first empty line.
  18983. Returns the number of empty lines passed."
  18984. (let ((pos (point)))
  18985. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18986. (skip-chars-backward " \t\n\r")
  18987. (unless (eobp)
  18988. (forward-line -1)))
  18989. (beginning-of-line 2)
  18990. (goto-char (min (point) pos))
  18991. (count-lines (point) pos)))
  18992. (defun org-skip-whitespace ()
  18993. (skip-chars-forward " \t\n\r"))
  18994. (defun org-point-in-group (point group &optional context)
  18995. "Check if POINT is in match-group GROUP.
  18996. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18997. match. If the match group does not exist or point is not inside it,
  18998. return nil."
  18999. (and (match-beginning group)
  19000. (>= point (match-beginning group))
  19001. (<= point (match-end group))
  19002. (if context
  19003. (list context (match-beginning group) (match-end group))
  19004. t)))
  19005. (defun org-switch-to-buffer-other-window (&rest args)
  19006. "Switch to buffer in a second window on the current frame.
  19007. In particular, do not allow pop-up frames.
  19008. Returns the newly created buffer."
  19009. (org-no-popups
  19010. (apply 'switch-to-buffer-other-window args)))
  19011. (defun org-combine-plists (&rest plists)
  19012. "Create a single property list from all plists in PLISTS.
  19013. The process starts by copying the first list, and then setting properties
  19014. from the other lists. Settings in the last list are the most significant
  19015. ones and overrule settings in the other lists."
  19016. (let ((rtn (copy-sequence (pop plists)))
  19017. p v ls)
  19018. (while plists
  19019. (setq ls (pop plists))
  19020. (while ls
  19021. (setq p (pop ls) v (pop ls))
  19022. (setq rtn (plist-put rtn p v))))
  19023. rtn))
  19024. (defun org-replace-escapes (string table)
  19025. "Replace %-escapes in STRING with values in TABLE.
  19026. TABLE is an association list with keys like \"%a\" and string values.
  19027. The sequences in STRING may contain normal field width and padding information,
  19028. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19029. so values can contain further %-escapes if they are define later in TABLE."
  19030. (let ((tbl (copy-alist table))
  19031. (case-fold-search nil)
  19032. (pchg 0)
  19033. e re rpl)
  19034. (while (setq e (pop tbl))
  19035. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19036. (when (and (cdr e) (string-match re (cdr e)))
  19037. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19038. (safe "SREF"))
  19039. (add-text-properties 0 3 (list 'sref sref) safe)
  19040. (setcdr e (replace-match safe t t (cdr e)))))
  19041. (while (string-match re string)
  19042. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19043. (cdr e)))
  19044. (setq string (replace-match rpl t t string))))
  19045. (while (setq pchg (next-property-change pchg string))
  19046. (let ((sref (get-text-property pchg 'sref string)))
  19047. (when (and sref (string-match "SREF" string pchg))
  19048. (setq string (replace-match sref t t string)))))
  19049. string))
  19050. (defun org-sublist (list start end)
  19051. "Return a section of LIST, from START to END.
  19052. Counting starts at 1."
  19053. (let (rtn (c start))
  19054. (setq list (nthcdr (1- start) list))
  19055. (while (and list (<= c end))
  19056. (push (pop list) rtn)
  19057. (setq c (1+ c)))
  19058. (nreverse rtn)))
  19059. (defun org-find-base-buffer-visiting (file)
  19060. "Like `find-buffer-visiting' but always return the base buffer and
  19061. not an indirect buffer."
  19062. (let ((buf (or (get-file-buffer file)
  19063. (find-buffer-visiting file))))
  19064. (if buf
  19065. (or (buffer-base-buffer buf) buf)
  19066. nil)))
  19067. (defun org-image-file-name-regexp (&optional extensions)
  19068. "Return regexp matching the file names of images.
  19069. If EXTENSIONS is given, only match these."
  19070. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19071. (image-file-name-regexp)
  19072. (let ((image-file-name-extensions
  19073. (or extensions
  19074. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19075. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19076. (concat "\\."
  19077. (regexp-opt (nconc (mapcar 'upcase
  19078. image-file-name-extensions)
  19079. image-file-name-extensions)
  19080. t)
  19081. "\\'"))))
  19082. (defun org-file-image-p (file &optional extensions)
  19083. "Return non-nil if FILE is an image."
  19084. (save-match-data
  19085. (string-match (org-image-file-name-regexp extensions) file)))
  19086. (defun org-get-cursor-date ()
  19087. "Return the date at cursor in as a time.
  19088. This works in the calendar and in the agenda, anywhere else it just
  19089. returns the current time."
  19090. (let (date day defd)
  19091. (cond
  19092. ((eq major-mode 'calendar-mode)
  19093. (setq date (calendar-cursor-to-date)
  19094. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  19095. ((eq major-mode 'org-agenda-mode)
  19096. (setq day (get-text-property (point) 'day))
  19097. (if day
  19098. (setq date (calendar-gregorian-from-absolute day)
  19099. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  19100. (nth 2 date))))))
  19101. (or defd (current-time))))
  19102. (defun org-mark-subtree (&optional up)
  19103. "Mark the current subtree.
  19104. This puts point at the start of the current subtree, and mark at
  19105. the end. If a numeric prefix UP is given, move up into the
  19106. hierarchy of headlines by UP levels before marking the subtree."
  19107. (interactive "P")
  19108. (org-with-limited-levels
  19109. (cond ((org-at-heading-p) (beginning-of-line))
  19110. ((org-before-first-heading-p) (error "Not in a subtree"))
  19111. (t (outline-previous-visible-heading 1))))
  19112. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19113. (if (org-called-interactively-p 'any)
  19114. (call-interactively 'org-mark-element)
  19115. (org-mark-element)))
  19116. ;;; Indentation
  19117. (defun org-indent-line ()
  19118. "Indent line depending on context."
  19119. (interactive)
  19120. (let* ((pos (point))
  19121. (itemp (org-at-item-p))
  19122. (case-fold-search t)
  19123. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19124. (inline-task-p (and (featurep 'org-inlinetask)
  19125. (org-inlinetask-in-task-p)))
  19126. (inline-re (and inline-task-p
  19127. (org-inlinetask-outline-regexp)))
  19128. column)
  19129. (if (and orgstruct-is-++ (eq pos (point)))
  19130. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19131. (indent-according-to-mode))
  19132. (beginning-of-line 1)
  19133. (cond
  19134. ;; Headings
  19135. ((looking-at org-outline-regexp) (setq column 0))
  19136. ;; Included files
  19137. ((looking-at "#\\+include:") (setq column 0))
  19138. ;; Footnote definition
  19139. ((looking-at org-footnote-definition-re) (setq column 0))
  19140. ;; Literal examples
  19141. ((looking-at "[ \t]*:\\( \\|$\\)")
  19142. (setq column (org-get-indentation))) ; do nothing
  19143. ;; Lists
  19144. ((ignore-errors (goto-char (org-in-item-p)))
  19145. (setq column (if itemp
  19146. (org-get-indentation)
  19147. (org-list-item-body-column (point))))
  19148. (goto-char pos))
  19149. ;; Drawers
  19150. ((and (looking-at "[ \t]*:END:")
  19151. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19152. (save-excursion
  19153. (goto-char (1- (match-beginning 1)))
  19154. (setq column (current-column))))
  19155. ;; Special blocks
  19156. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19157. (save-excursion
  19158. (re-search-backward
  19159. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19160. (setq column (org-get-indentation (match-string 0))))
  19161. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19162. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19163. (save-excursion
  19164. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19165. (setq column
  19166. (cond ((equal (downcase (match-string 1)) "src")
  19167. ;; src blocks: let `org-edit-src-exit' handle them
  19168. (org-get-indentation))
  19169. ((equal (downcase (match-string 1)) "example")
  19170. (max (org-get-indentation)
  19171. (org-get-indentation (match-string 0))))
  19172. (t
  19173. (org-get-indentation (match-string 0))))))
  19174. ;; This line has nothing special, look at the previous relevant
  19175. ;; line to compute indentation
  19176. (t
  19177. (beginning-of-line 0)
  19178. (while (and (not (bobp))
  19179. (not (looking-at org-table-line-regexp))
  19180. (not (looking-at org-drawer-regexp))
  19181. ;; When point started in an inline task, do not move
  19182. ;; above task starting line.
  19183. (not (and inline-task-p (looking-at inline-re)))
  19184. ;; Skip drawers, blocks, empty lines, verbatim,
  19185. ;; comments, tables, footnotes definitions, lists,
  19186. ;; inline tasks.
  19187. (or (and (looking-at "[ \t]*:END:")
  19188. (re-search-backward org-drawer-regexp nil t))
  19189. (and (looking-at "[ \t]*#\\+end_")
  19190. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19191. (looking-at "[ \t]*[\n:#|]")
  19192. (looking-at org-footnote-definition-re)
  19193. (and (ignore-errors (goto-char (org-in-item-p)))
  19194. (goto-char
  19195. (org-list-get-top-point (org-list-struct))))
  19196. (and (not inline-task-p)
  19197. (featurep 'org-inlinetask)
  19198. (org-inlinetask-in-task-p)
  19199. (or (org-inlinetask-goto-beginning) t))))
  19200. (beginning-of-line 0))
  19201. (cond
  19202. ;; There was an heading above.
  19203. ((looking-at "\\*+[ \t]+")
  19204. (if (not org-adapt-indentation)
  19205. (setq column 0)
  19206. (goto-char (match-end 0))
  19207. (setq column (current-column))))
  19208. ;; A drawer had started and is unfinished
  19209. ((looking-at org-drawer-regexp)
  19210. (goto-char (1- (match-beginning 1)))
  19211. (setq column (current-column)))
  19212. ;; Else, nothing noticeable found: get indentation and go on.
  19213. (t (setq column (org-get-indentation))))))
  19214. ;; Now apply indentation and move cursor accordingly
  19215. (goto-char pos)
  19216. (if (<= (current-column) (current-indentation))
  19217. (org-indent-line-to column)
  19218. (save-excursion (org-indent-line-to column)))
  19219. ;; Special polishing for properties, see `org-property-format'
  19220. (setq column (current-column))
  19221. (beginning-of-line 1)
  19222. (if (looking-at
  19223. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19224. (replace-match (concat (match-string 1)
  19225. (format org-property-format
  19226. (match-string 2) (match-string 3)))
  19227. t t))
  19228. (org-move-to-column column))))
  19229. (defun org-indent-drawer ()
  19230. "Indent the drawer at point."
  19231. (interactive)
  19232. (let ((p (point))
  19233. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19234. (match-end 0)))
  19235. (folded
  19236. (save-excursion
  19237. (end-of-line)
  19238. (when (overlays-at (point))
  19239. (member 'invisible (overlay-properties
  19240. (car (overlays-at (point)))))))))
  19241. (when folded (org-cycle))
  19242. (indent-for-tab-command)
  19243. (while (and (move-beginning-of-line 2) (< (point) e))
  19244. (indent-for-tab-command))
  19245. (goto-char p)
  19246. (when folded (org-cycle)))
  19247. (message "Drawer at point indented"))
  19248. (defun org-indent-block ()
  19249. "Indent the block at point."
  19250. (interactive)
  19251. (let ((p (point))
  19252. (case-fold-search t)
  19253. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19254. (match-end 0)))
  19255. (folded
  19256. (save-excursion
  19257. (end-of-line)
  19258. (when (overlays-at (point))
  19259. (member 'invisible (overlay-properties
  19260. (car (overlays-at (point)))))))))
  19261. (when folded (org-cycle))
  19262. (indent-for-tab-command)
  19263. (while (and (move-beginning-of-line 2) (< (point) e))
  19264. (indent-for-tab-command))
  19265. (goto-char p)
  19266. (when folded (org-cycle)))
  19267. (message "Block at point indented"))
  19268. (defun org-indent-region (start end)
  19269. "Indent region."
  19270. (interactive "r")
  19271. (save-excursion
  19272. (let ((line-end (org-current-line end)))
  19273. (goto-char start)
  19274. (while (< (org-current-line) line-end)
  19275. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19276. (t (call-interactively 'org-indent-line)))
  19277. (move-beginning-of-line 2)))))
  19278. ;;; Filling
  19279. ;; We use our own fill-paragraph and auto-fill functions.
  19280. ;; `org-fill-paragraph' relies on adaptive filling and context
  19281. ;; checking. Appropriate `fill-prefix' is computed with
  19282. ;; `org-adaptive-fill-function'.
  19283. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19284. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19285. ;; `org-adaptive-fill-function' value. Internally,
  19286. ;; `org-comment-line-break-function' breaks the line.
  19287. ;; `org-setup-filling' installs filling and auto-filling related
  19288. ;; variables during `org-mode' initialization.
  19289. (defun org-setup-filling ()
  19290. (interactive)
  19291. ;; Prevent auto-fill from inserting unwanted new items.
  19292. (when (boundp 'fill-nobreak-predicate)
  19293. (org-set-local
  19294. 'fill-nobreak-predicate
  19295. (org-uniquify
  19296. (append fill-nobreak-predicate
  19297. '(org-fill-paragraph-separate-nobreak-p
  19298. org-fill-line-break-nobreak-p
  19299. org-fill-paragraph-with-timestamp-nobreak-p)))))
  19300. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19301. (org-set-local 'auto-fill-inhibit-regexp nil)
  19302. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19303. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19304. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19305. (defvar org-element-paragraph-separate) ; org-element.el
  19306. (defun org-fill-paragraph-separate-nobreak-p ()
  19307. "Non-nil when a line break at point would insert a new item."
  19308. (looking-at (substring org-element-paragraph-separate 1)))
  19309. (defun org-fill-line-break-nobreak-p ()
  19310. "Non-nil when a line break at point would create an Org line break."
  19311. (save-excursion
  19312. (skip-chars-backward "[ \t]")
  19313. (skip-chars-backward "\\\\")
  19314. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19315. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  19316. "Non-nil when a line break at point would insert a new item."
  19317. (and (org-at-timestamp-p t)
  19318. (not (looking-at org-ts-regexp-both))))
  19319. (declare-function message-in-body-p "message" ())
  19320. (defvar org-element--affiliated-re) ; From org-element.el
  19321. (defvar orgtbl-line-start-regexp) ; From org-table.el
  19322. (defun org-adaptive-fill-function ()
  19323. "Compute a fill prefix for the current line.
  19324. Return fill prefix, as a string, or nil if current line isn't
  19325. meant to be filled."
  19326. (let (prefix)
  19327. (catch 'exit
  19328. (when (derived-mode-p 'message-mode)
  19329. (save-excursion
  19330. (beginning-of-line)
  19331. (cond ((or (not (message-in-body-p))
  19332. (looking-at orgtbl-line-start-regexp))
  19333. (throw 'exit nil))
  19334. ((looking-at message-cite-prefix-regexp)
  19335. (throw 'exit (match-string-no-properties 0)))
  19336. ((looking-at org-outline-regexp)
  19337. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  19338. (org-with-wide-buffer
  19339. (let* ((p (line-beginning-position))
  19340. (element (save-excursion (beginning-of-line) (org-element-at-point)))
  19341. (type (org-element-type element))
  19342. (post-affiliated
  19343. (save-excursion
  19344. (goto-char (org-element-property :begin element))
  19345. (while (looking-at org-element--affiliated-re) (forward-line))
  19346. (point))))
  19347. (unless (< p post-affiliated)
  19348. (case type
  19349. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19350. (footnote-definition "")
  19351. ((item plain-list)
  19352. (make-string (org-list-item-body-column post-affiliated) ? ))
  19353. (paragraph
  19354. ;; Fill prefix is usually the same as the current line,
  19355. ;; except if the paragraph is at the beginning of an item.
  19356. (let ((parent (org-element-property :parent element)))
  19357. (cond ((eq (org-element-type parent) 'item)
  19358. (make-string (org-list-item-body-column
  19359. (org-element-property :begin parent))
  19360. ? ))
  19361. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19362. (match-string 0))
  19363. (t ""))))
  19364. (comment-block
  19365. ;; Only fill contents if P is within block boundaries.
  19366. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19367. (forward-line)
  19368. (point)))
  19369. (cend (save-excursion
  19370. (goto-char (org-element-property :end element))
  19371. (skip-chars-backward " \r\t\n")
  19372. (line-beginning-position))))
  19373. (when (and (>= p cbeg) (< p cend))
  19374. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19375. (match-string 0)
  19376. "")))))))))))
  19377. (declare-function message-goto-body "message" ())
  19378. (defvar message-cite-prefix-regexp) ; From message.el
  19379. (defvar org-element-all-objects) ; From org-element.el
  19380. (defun org-fill-paragraph (&optional justify)
  19381. "Fill element at point, when applicable.
  19382. This function only applies to comment blocks, comments, example
  19383. blocks and paragraphs. Also, as a special case, re-align table
  19384. when point is at one.
  19385. If JUSTIFY is non-nil (interactively, with prefix argument),
  19386. justify as well. If `sentence-end-double-space' is non-nil, then
  19387. period followed by one space does not end a sentence, so don't
  19388. break a line there. The variable `fill-column' controls the
  19389. width for filling.
  19390. For convenience, when point is at a plain list, an item or
  19391. a footnote definition, try to fill the first paragraph within."
  19392. (interactive)
  19393. (if (and (derived-mode-p 'message-mode)
  19394. (or (not (message-in-body-p))
  19395. (save-excursion (move-beginning-of-line 1)
  19396. (looking-at message-cite-prefix-regexp))))
  19397. ;; First ensure filling is correct in message-mode.
  19398. (let ((fill-paragraph-function
  19399. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19400. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19401. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19402. (paragraph-separate
  19403. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19404. (fill-paragraph nil))
  19405. (save-excursion
  19406. ;; Move to end of line in order to get the first paragraph
  19407. ;; within a plain list or a footnote definition.
  19408. (end-of-line)
  19409. (let ((element (org-element-at-point)))
  19410. ;; First check if point is in a blank line at the beginning of
  19411. ;; the buffer. In that case, ignore filling.
  19412. (if (< (point) (org-element-property :begin element)) t
  19413. (case (org-element-type element)
  19414. ;; Use major mode filling function is src blocks.
  19415. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  19416. ;; Align Org tables, leave table.el tables as-is.
  19417. (table-row (org-table-align) t)
  19418. (table
  19419. (when (eq (org-element-property :type element) 'org)
  19420. (org-table-align))
  19421. t)
  19422. (paragraph
  19423. ;; Paragraphs may contain `line-break' type objects.
  19424. (let ((beg (max (point-min)
  19425. (org-element-property :contents-begin element)))
  19426. (end (min (point-max)
  19427. (org-element-property :contents-end element))))
  19428. ;; Do nothing if point is at an affiliated keyword.
  19429. (if (< (point) beg) t
  19430. (when (derived-mode-p 'message-mode)
  19431. ;; In `message-mode', do not fill following
  19432. ;; citation in current paragraph nor text before
  19433. ;; message body.
  19434. (let ((body-start (save-excursion (message-goto-body))))
  19435. (when body-start (setq beg (max body-start beg))))
  19436. (when (save-excursion
  19437. (re-search-forward
  19438. (concat "^" message-cite-prefix-regexp) end t))
  19439. (setq end (match-beginning 0))))
  19440. ;; Fill paragraph, taking line breaks into
  19441. ;; consideration. For that, slice the paragraph
  19442. ;; using line breaks as separators, and fill the
  19443. ;; parts in reverse order to avoid messing with
  19444. ;; markers.
  19445. (save-excursion
  19446. (goto-char end)
  19447. (mapc
  19448. (lambda (pos)
  19449. (fill-region-as-paragraph pos (point) justify)
  19450. (goto-char pos))
  19451. ;; Find the list of ending positions for line
  19452. ;; breaks in the current paragraph. Add paragraph
  19453. ;; beginning to include first slice.
  19454. (nreverse
  19455. (cons
  19456. beg
  19457. (org-element-map
  19458. (org-element--parse-objects
  19459. beg end nil org-element-all-objects)
  19460. 'line-break
  19461. (lambda (lb) (org-element-property :end lb)))))))
  19462. t)))
  19463. ;; Contents of `comment-block' type elements should be
  19464. ;; filled as plain text, but only if point is within block
  19465. ;; markers.
  19466. (comment-block
  19467. (let* ((case-fold-search t)
  19468. (beg (save-excursion
  19469. (goto-char (org-element-property :begin element))
  19470. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  19471. (forward-line)
  19472. (point)))
  19473. (end (save-excursion
  19474. (goto-char (org-element-property :end element))
  19475. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  19476. (line-beginning-position))))
  19477. (when (and (>= (point) beg) (< (point) end))
  19478. (fill-region-as-paragraph
  19479. (save-excursion
  19480. (end-of-line)
  19481. (re-search-backward "^[ \t]*$" beg 'move)
  19482. (line-beginning-position))
  19483. (save-excursion
  19484. (beginning-of-line)
  19485. (re-search-forward "^[ \t]*$" end 'move)
  19486. (line-beginning-position))
  19487. justify)))
  19488. t)
  19489. ;; Fill comments.
  19490. (comment (fill-comment-paragraph justify))
  19491. ;; Ignore every other element.
  19492. (otherwise t)))))))
  19493. (defun org-auto-fill-function ()
  19494. "Auto-fill function."
  19495. ;; Check if auto-filling is meaningful.
  19496. (let ((fc (current-fill-column)))
  19497. (when (and fc (> (current-column) fc))
  19498. (let* ((fill-prefix (org-adaptive-fill-function))
  19499. ;; Enforce empty fill prefix, if required. Otherwise, it
  19500. ;; will be computed again.
  19501. (adaptive-fill-mode (not (equal fill-prefix ""))))
  19502. (when fill-prefix (do-auto-fill))))))
  19503. (defun org-comment-line-break-function (&optional soft)
  19504. "Break line at point and indent, continuing comment if within one.
  19505. The inserted newline is marked hard if variable
  19506. `use-hard-newlines' is true, unless optional argument SOFT is
  19507. non-nil."
  19508. (if soft (insert-and-inherit ?\n) (newline 1))
  19509. (save-excursion (forward-char -1) (delete-horizontal-space))
  19510. (delete-horizontal-space)
  19511. (indent-to-left-margin)
  19512. (insert-before-markers-and-inherit fill-prefix))
  19513. ;;; Comments
  19514. ;; Org comments syntax is quite complex. It requires the entire line
  19515. ;; to be just a comment. Also, even with the right syntax at the
  19516. ;; beginning of line, some some elements (i.e. verse-block or
  19517. ;; example-block) don't accept comments. Usual Emacs comment commands
  19518. ;; cannot cope with those requirements. Therefore, Org replaces them.
  19519. ;; Org still relies on `comment-dwim', but cannot trust
  19520. ;; `comment-only-p'. So, `comment-region-function' and
  19521. ;; `uncomment-region-function' both point
  19522. ;; to`org-comment-or-uncomment-region'. Eventually,
  19523. ;; `org-insert-comment' takes care of insertion of comments at the
  19524. ;; beginning of line.
  19525. ;; `org-setup-comments-handling' install comments related variables
  19526. ;; during `org-mode' initialization.
  19527. (defun org-setup-comments-handling ()
  19528. (interactive)
  19529. (org-set-local 'comment-use-syntax nil)
  19530. (org-set-local 'comment-start "# ")
  19531. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  19532. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  19533. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  19534. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  19535. (defun org-insert-comment ()
  19536. "Insert an empty comment above current line.
  19537. If the line is empty, insert comment at its beginning."
  19538. (beginning-of-line)
  19539. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  19540. (org-indent-line)
  19541. (insert "# "))
  19542. (defvar comment-empty-lines) ; From newcomment.el.
  19543. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  19544. "Comment or uncomment each non-blank line in the region.
  19545. Uncomment each non-blank line between BEG and END if it only
  19546. contains commented lines. Otherwise, comment them."
  19547. (save-restriction
  19548. ;; Restrict region
  19549. (narrow-to-region (save-excursion (goto-char beg)
  19550. (skip-chars-forward " \r\t\n" end)
  19551. (line-beginning-position))
  19552. (save-excursion (goto-char end)
  19553. (skip-chars-backward " \r\t\n" beg)
  19554. (line-end-position)))
  19555. (let ((uncommentp
  19556. ;; UNCOMMENTP is non-nil when every non blank line between
  19557. ;; BEG and END is a comment.
  19558. (save-excursion
  19559. (goto-char (point-min))
  19560. (while (and (not (eobp))
  19561. (let ((element (org-element-at-point)))
  19562. (and (eq (org-element-type element) 'comment)
  19563. (goto-char (min (point-max)
  19564. (org-element-property
  19565. :end element)))))))
  19566. (eobp))))
  19567. (if uncommentp
  19568. ;; Only blank lines and comments in region: uncomment it.
  19569. (save-excursion
  19570. (goto-char (point-min))
  19571. (while (not (eobp))
  19572. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  19573. (replace-match "" nil nil nil 1))
  19574. (forward-line)))
  19575. ;; Comment each line in region.
  19576. (let ((min-indent (point-max)))
  19577. ;; First find the minimum indentation across all lines.
  19578. (save-excursion
  19579. (goto-char (point-min))
  19580. (while (and (not (eobp)) (not (zerop min-indent)))
  19581. (unless (looking-at "[ \t]*$")
  19582. (setq min-indent (min min-indent (current-indentation))))
  19583. (forward-line)))
  19584. ;; Then loop over all lines.
  19585. (save-excursion
  19586. (goto-char (point-min))
  19587. (while (not (eobp))
  19588. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  19589. (org-move-to-column min-indent t)
  19590. (insert comment-start))
  19591. (forward-line))))))))
  19592. ;;; Other stuff.
  19593. (defun org-toggle-fixed-width-section (arg)
  19594. "Toggle the fixed-width export.
  19595. If there is no active region, the QUOTE keyword at the current headline is
  19596. inserted or removed. When present, it causes the text between this headline
  19597. and the next to be exported as fixed-width text, and unmodified.
  19598. If there is an active region, this command adds or removes a colon as the
  19599. first character of this line. If the first character of a line is a colon,
  19600. this line is also exported in fixed-width font."
  19601. (interactive "P")
  19602. (let* ((cc 0)
  19603. (regionp (org-region-active-p))
  19604. (beg (if regionp (region-beginning) (point)))
  19605. (end (if regionp (region-end)))
  19606. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19607. (case-fold-search nil)
  19608. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  19609. off)
  19610. (if regionp
  19611. (save-excursion
  19612. (goto-char beg)
  19613. (setq cc (current-column))
  19614. (beginning-of-line 1)
  19615. (setq off (looking-at re))
  19616. (while (> nlines 0)
  19617. (setq nlines (1- nlines))
  19618. (beginning-of-line 1)
  19619. (cond
  19620. (arg
  19621. (org-move-to-column cc t)
  19622. (insert ": \n")
  19623. (forward-line -1))
  19624. ((and off (looking-at re))
  19625. (replace-match "" t t nil 1))
  19626. ((not off) (org-move-to-column cc t) (insert ": ")))
  19627. (forward-line 1)))
  19628. (save-excursion
  19629. (org-back-to-heading)
  19630. (cond
  19631. ((looking-at (format org-heading-keyword-regexp-format
  19632. org-quote-string))
  19633. (goto-char (match-end 1))
  19634. (looking-at (concat " +" org-quote-string))
  19635. (replace-match "" t t)
  19636. (when (eolp) (insert " ")))
  19637. ((looking-at org-outline-regexp)
  19638. (goto-char (match-end 0))
  19639. (insert org-quote-string " ")))))))
  19640. (defun org-reftex-citation ()
  19641. "Use reftex-citation to insert a citation into the buffer.
  19642. This looks for a line like
  19643. #+BIBLIOGRAPHY: foo plain option:-d
  19644. and derives from it that foo.bib is the bibliography file relevant
  19645. for this document. It then installs the necessary environment for RefTeX
  19646. to work in this buffer and calls `reftex-citation' to insert a citation
  19647. into the buffer.
  19648. Export of such citations to both LaTeX and HTML is handled by the contributed
  19649. package org-exp-bibtex by Taru Karttunen."
  19650. (interactive)
  19651. (let ((reftex-docstruct-symbol 'rds)
  19652. (reftex-cite-format "\\cite{%l}")
  19653. rds bib)
  19654. (save-excursion
  19655. (save-restriction
  19656. (widen)
  19657. (let ((case-fold-search t)
  19658. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  19659. (if (not (save-excursion
  19660. (or (re-search-forward re nil t)
  19661. (re-search-backward re nil t))))
  19662. (error "No bibliography defined in file")
  19663. (setq bib (concat (match-string 1) ".bib")
  19664. rds (list (list 'bib bib)))))))
  19665. (call-interactively 'reftex-citation)))
  19666. ;;;; Functions extending outline functionality
  19667. (defun org-beginning-of-line (&optional arg)
  19668. "Go to the beginning of the current line. If that is invisible, continue
  19669. to a visible line beginning. This makes the function of C-a more intuitive.
  19670. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19671. first attempt, and only move to after the tags when the cursor is already
  19672. beyond the end of the headline."
  19673. (interactive "P")
  19674. (let ((pos (point))
  19675. (special (if (consp org-special-ctrl-a/e)
  19676. (car org-special-ctrl-a/e)
  19677. org-special-ctrl-a/e))
  19678. refpos)
  19679. (if (org-bound-and-true-p visual-line-mode)
  19680. (beginning-of-visual-line 1)
  19681. (beginning-of-line 1))
  19682. (if (and arg (fboundp 'move-beginning-of-line))
  19683. (call-interactively 'move-beginning-of-line)
  19684. (if (bobp)
  19685. nil
  19686. (backward-char 1)
  19687. (if (org-truely-invisible-p)
  19688. (while (and (not (bobp)) (org-truely-invisible-p))
  19689. (backward-char 1)
  19690. (beginning-of-line 1))
  19691. (forward-char 1))))
  19692. (when special
  19693. (cond
  19694. ((and (looking-at org-complex-heading-regexp)
  19695. (= (char-after (match-end 1)) ?\ ))
  19696. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19697. (point-at-eol)))
  19698. (goto-char
  19699. (if (eq special t)
  19700. (cond ((> pos refpos) refpos)
  19701. ((= pos (point)) refpos)
  19702. (t (point)))
  19703. (cond ((> pos (point)) (point))
  19704. ((not (eq last-command this-command)) (point))
  19705. (t refpos)))))
  19706. ((org-at-item-p)
  19707. ;; Being at an item and not looking at an the item means point
  19708. ;; was previously moved to beginning of a visual line, which
  19709. ;; doesn't contain the item. Therefore, do nothing special,
  19710. ;; just stay here.
  19711. (when (looking-at org-list-full-item-re)
  19712. ;; Set special position at first white space character after
  19713. ;; bullet, and check-box, if any.
  19714. (let ((after-bullet
  19715. (let ((box (match-end 3)))
  19716. (if (not box) (match-end 1)
  19717. (let ((after (char-after box)))
  19718. (if (and after (= after ? )) (1+ box) box))))))
  19719. ;; Special case: Move point to special position when
  19720. ;; currently after it or at beginning of line.
  19721. (if (eq special t)
  19722. (when (or (> pos after-bullet) (= (point) pos))
  19723. (goto-char after-bullet))
  19724. ;; Reversed case: Move point to special position when
  19725. ;; point was already at beginning of line and command is
  19726. ;; repeated.
  19727. (when (and (= (point) pos) (eq last-command this-command))
  19728. (goto-char after-bullet))))))))
  19729. (org-no-warnings
  19730. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19731. (defun org-end-of-line (&optional arg)
  19732. "Go to the end of the line.
  19733. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  19734. tags on the first attempt, and only move to after the tags when
  19735. the cursor is already beyond the end of the headline."
  19736. (interactive "P")
  19737. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  19738. org-special-ctrl-a/e))
  19739. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  19740. 'end-of-visual-line)
  19741. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19742. (t 'end-of-line))))
  19743. (if (or (not special) arg) (call-interactively move-fun)
  19744. (let* ((element (save-excursion (beginning-of-line)
  19745. (org-element-at-point)))
  19746. (type (org-element-type element)))
  19747. (cond
  19748. ((memq type '(headline inlinetask))
  19749. (let ((pos (point)))
  19750. (beginning-of-line 1)
  19751. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19752. (if (eq special t)
  19753. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  19754. (goto-char (match-beginning 1))
  19755. (goto-char (match-end 0)))
  19756. (if (or (< pos (match-end 0))
  19757. (not (eq this-command last-command)))
  19758. (goto-char (match-end 0))
  19759. (goto-char (match-beginning 1))))
  19760. (call-interactively move-fun))))
  19761. ((org-element-property :hiddenp element)
  19762. ;; If element is hidden, `move-end-of-line' would put point
  19763. ;; after it. Use `end-of-line' to stay on current line.
  19764. (call-interactively 'end-of-line))
  19765. (t (call-interactively move-fun)))))
  19766. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19767. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19768. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19769. (defun org-backward-sentence (&optional arg)
  19770. "Go to beginning of sentence, or beginning of table field.
  19771. This will call `backward-sentence' or `org-table-beginning-of-field',
  19772. depending on context."
  19773. (interactive "P")
  19774. (cond
  19775. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19776. (t (call-interactively 'backward-sentence))))
  19777. (defun org-forward-sentence (&optional arg)
  19778. "Go to end of sentence, or end of table field.
  19779. This will call `forward-sentence' or `org-table-end-of-field',
  19780. depending on context."
  19781. (interactive "P")
  19782. (cond
  19783. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19784. (t (call-interactively 'forward-sentence))))
  19785. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19786. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19787. (defun org-kill-line (&optional arg)
  19788. "Kill line, to tags or end of line."
  19789. (interactive "P")
  19790. (cond
  19791. ((or (not org-special-ctrl-k)
  19792. (bolp)
  19793. (not (org-at-heading-p)))
  19794. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19795. org-ctrl-k-protect-subtree)
  19796. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19797. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19798. (error "C-k aborted - would kill hidden subtree")))
  19799. (call-interactively
  19800. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  19801. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19802. (kill-region (point) (match-beginning 1))
  19803. (org-set-tags nil t))
  19804. (t (kill-region (point) (point-at-eol)))))
  19805. (define-key org-mode-map "\C-k" 'org-kill-line)
  19806. (defun org-yank (&optional arg)
  19807. "Yank. If the kill is a subtree, treat it specially.
  19808. This command will look at the current kill and check if is a single
  19809. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19810. cursor is at the beginning of a line or after the stars of a currently
  19811. empty headline, then the yank is handled specially. How exactly depends
  19812. on the value of the following variables, both set by default.
  19813. org-yank-folded-subtrees
  19814. When set, the subtree(s) will be folded after insertion, but only
  19815. if doing so would now swallow text after the yanked text.
  19816. org-yank-adjusted-subtrees
  19817. When set, the subtree will be promoted or demoted in order to
  19818. fit into the local outline tree structure, which means that the level
  19819. will be adjusted so that it becomes the smaller one of the two
  19820. *visible* surrounding headings.
  19821. Any prefix to this command will cause `yank' to be called directly with
  19822. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19823. will just
  19824. plainly yank the text as it is.
  19825. \[1] The test checks if the first non-white line is a heading
  19826. and if there are no other headings with fewer stars."
  19827. (interactive "P")
  19828. (org-yank-generic 'yank arg))
  19829. (defun org-yank-generic (command arg)
  19830. "Perform some yank-like command.
  19831. This function implements the behavior described in the `org-yank'
  19832. documentation. However, it has been generalized to work for any
  19833. interactive command with similar behavior."
  19834. ;; pretend to be command COMMAND
  19835. (setq this-command command)
  19836. (if arg
  19837. (call-interactively command)
  19838. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19839. (and (org-kill-is-subtree-p)
  19840. (or (bolp)
  19841. (and (looking-at "[ \t]*$")
  19842. (string-match
  19843. "\\`\\*+\\'"
  19844. (buffer-substring (point-at-bol) (point)))))))
  19845. swallowp)
  19846. (cond
  19847. ((and subtreep org-yank-folded-subtrees)
  19848. (let ((beg (point))
  19849. end)
  19850. (if (and subtreep org-yank-adjusted-subtrees)
  19851. (org-paste-subtree nil nil 'for-yank)
  19852. (call-interactively command))
  19853. (setq end (point))
  19854. (goto-char beg)
  19855. (when (and (bolp) subtreep
  19856. (not (setq swallowp
  19857. (org-yank-folding-would-swallow-text beg end))))
  19858. (org-with-limited-levels
  19859. (or (looking-at org-outline-regexp)
  19860. (re-search-forward org-outline-regexp-bol end t))
  19861. (while (and (< (point) end) (looking-at org-outline-regexp))
  19862. (hide-subtree)
  19863. (org-cycle-show-empty-lines 'folded)
  19864. (condition-case nil
  19865. (outline-forward-same-level 1)
  19866. (error (goto-char end))))))
  19867. (when swallowp
  19868. (message
  19869. "Inserted text not folded because that would swallow text"))
  19870. (goto-char end)
  19871. (skip-chars-forward " \t\n\r")
  19872. (beginning-of-line 1)
  19873. (push-mark beg 'nomsg)))
  19874. ((and subtreep org-yank-adjusted-subtrees)
  19875. (let ((beg (point-at-bol)))
  19876. (org-paste-subtree nil nil 'for-yank)
  19877. (push-mark beg 'nomsg)))
  19878. (t
  19879. (call-interactively command))))))
  19880. (defun org-yank-folding-would-swallow-text (beg end)
  19881. "Would hide-subtree at BEG swallow any text after END?"
  19882. (let (level)
  19883. (org-with-limited-levels
  19884. (save-excursion
  19885. (goto-char beg)
  19886. (when (or (looking-at org-outline-regexp)
  19887. (re-search-forward org-outline-regexp-bol end t))
  19888. (setq level (org-outline-level)))
  19889. (goto-char end)
  19890. (skip-chars-forward " \t\r\n\v\f")
  19891. (if (or (eobp)
  19892. (and (bolp) (looking-at org-outline-regexp)
  19893. (<= (org-outline-level) level)))
  19894. nil ; Nothing would be swallowed
  19895. t))))) ; something would swallow
  19896. (define-key org-mode-map "\C-y" 'org-yank)
  19897. (defun org-truely-invisible-p ()
  19898. "Check if point is at a character currently not visible.
  19899. This version does not only check the character property, but also
  19900. `visible-mode'."
  19901. ;; Early versions of noutline don't have `outline-invisible-p'.
  19902. (if (org-bound-and-true-p visible-mode)
  19903. nil
  19904. (outline-invisible-p)))
  19905. (defun org-invisible-p2 ()
  19906. "Check if point is at a character currently not visible."
  19907. (save-excursion
  19908. (if (and (eolp) (not (bobp))) (backward-char 1))
  19909. ;; Early versions of noutline don't have `outline-invisible-p'.
  19910. (outline-invisible-p)))
  19911. (defun org-back-to-heading (&optional invisible-ok)
  19912. "Call `outline-back-to-heading', but provide a better error message."
  19913. (condition-case nil
  19914. (outline-back-to-heading invisible-ok)
  19915. (error (error "Before first headline at position %d in buffer %s"
  19916. (point) (current-buffer)))))
  19917. (defun org-before-first-heading-p ()
  19918. "Before first heading?"
  19919. (save-excursion
  19920. (end-of-line)
  19921. (null (re-search-backward org-outline-regexp-bol nil t))))
  19922. (defun org-at-heading-p (&optional ignored)
  19923. (outline-on-heading-p t))
  19924. ;; Compatibility alias with Org versions < 7.8.03
  19925. (defalias 'org-on-heading-p 'org-at-heading-p)
  19926. (defun org-at-comment-p nil
  19927. "Is cursor in a line starting with a # character?"
  19928. (save-excursion
  19929. (beginning-of-line)
  19930. (looking-at "^#")))
  19931. (defun org-at-drawer-p nil
  19932. "Is cursor at a drawer keyword?"
  19933. (save-excursion
  19934. (move-beginning-of-line 1)
  19935. (looking-at org-drawer-regexp)))
  19936. (defun org-at-block-p nil
  19937. "Is cursor at a block keyword?"
  19938. (save-excursion
  19939. (move-beginning-of-line 1)
  19940. (looking-at org-block-regexp)))
  19941. (defun org-point-at-end-of-empty-headline ()
  19942. "If point is at the end of an empty headline, return t, else nil.
  19943. If the heading only contains a TODO keyword, it is still still considered
  19944. empty."
  19945. (and (looking-at "[ \t]*$")
  19946. (when org-todo-line-regexp
  19947. (save-excursion
  19948. (beginning-of-line 1)
  19949. (let ((case-fold-search nil))
  19950. (looking-at org-todo-line-regexp)
  19951. (string= (match-string 3) ""))))))
  19952. (defun org-at-heading-or-item-p ()
  19953. (or (org-at-heading-p) (org-at-item-p)))
  19954. (defun org-at-target-p ()
  19955. (or (org-in-regexp org-radio-target-regexp)
  19956. (org-in-regexp org-target-regexp)))
  19957. ;; Compatibility alias with Org versions < 7.8.03
  19958. (defalias 'org-on-target-p 'org-at-target-p)
  19959. (defun org-up-heading-all (arg)
  19960. "Move to the heading line of which the present line is a subheading.
  19961. This function considers both visible and invisible heading lines.
  19962. With argument, move up ARG levels."
  19963. (if (fboundp 'outline-up-heading-all)
  19964. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19965. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19966. (defun org-up-heading-safe ()
  19967. "Move to the heading line of which the present line is a subheading.
  19968. This version will not throw an error. It will return the level of the
  19969. headline found, or nil if no higher level is found.
  19970. Also, this function will be a lot faster than `outline-up-heading',
  19971. because it relies on stars being the outline starters. This can really
  19972. make a significant difference in outlines with very many siblings."
  19973. (let (start-level re)
  19974. (org-back-to-heading t)
  19975. (setq start-level (funcall outline-level))
  19976. (if (equal start-level 1)
  19977. nil
  19978. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19979. (if (re-search-backward re nil t)
  19980. (funcall outline-level)))))
  19981. (defun org-first-sibling-p ()
  19982. "Is this heading the first child of its parents?"
  19983. (interactive)
  19984. (let ((re org-outline-regexp-bol)
  19985. level l)
  19986. (unless (org-at-heading-p t)
  19987. (error "Not at a heading"))
  19988. (setq level (funcall outline-level))
  19989. (save-excursion
  19990. (if (not (re-search-backward re nil t))
  19991. t
  19992. (setq l (funcall outline-level))
  19993. (< l level)))))
  19994. (defun org-goto-sibling (&optional previous)
  19995. "Goto the next sibling, even if it is invisible.
  19996. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19997. when a sibling was found. When none is found, return nil and don't
  19998. move point."
  19999. (let ((fun (if previous 're-search-backward 're-search-forward))
  20000. (pos (point))
  20001. (re org-outline-regexp-bol)
  20002. level l)
  20003. (when (condition-case nil (org-back-to-heading t) (error nil))
  20004. (setq level (funcall outline-level))
  20005. (catch 'exit
  20006. (or previous (forward-char 1))
  20007. (while (funcall fun re nil t)
  20008. (setq l (funcall outline-level))
  20009. (when (< l level) (goto-char pos) (throw 'exit nil))
  20010. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20011. (goto-char pos)
  20012. nil))))
  20013. (defun org-show-siblings ()
  20014. "Show all siblings of the current headline."
  20015. (save-excursion
  20016. (while (org-goto-sibling) (org-flag-heading nil)))
  20017. (save-excursion
  20018. (while (org-goto-sibling 'previous)
  20019. (org-flag-heading nil))))
  20020. (defun org-goto-first-child ()
  20021. "Goto the first child, even if it is invisible.
  20022. Return t when a child was found. Otherwise don't move point and
  20023. return nil."
  20024. (let (level (pos (point)) (re org-outline-regexp-bol))
  20025. (when (condition-case nil (org-back-to-heading t) (error nil))
  20026. (setq level (outline-level))
  20027. (forward-char 1)
  20028. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20029. (progn (goto-char (match-beginning 0)) t)
  20030. (goto-char pos) nil))))
  20031. (defun org-show-hidden-entry ()
  20032. "Show an entry where even the heading is hidden."
  20033. (save-excursion
  20034. (org-show-entry)))
  20035. (defun org-flag-heading (flag &optional entry)
  20036. "Flag the current heading. FLAG non-nil means make invisible.
  20037. When ENTRY is non-nil, show the entire entry."
  20038. (save-excursion
  20039. (org-back-to-heading t)
  20040. ;; Check if we should show the entire entry
  20041. (if entry
  20042. (progn
  20043. (org-show-entry)
  20044. (save-excursion
  20045. (and (outline-next-heading)
  20046. (org-flag-heading nil))))
  20047. (outline-flag-region (max (point-min) (1- (point)))
  20048. (save-excursion (outline-end-of-heading) (point))
  20049. flag))))
  20050. (defun org-get-next-sibling ()
  20051. "Move to next heading of the same level, and return point.
  20052. If there is no such heading, return nil.
  20053. This is like outline-next-sibling, but invisible headings are ok."
  20054. (let ((level (funcall outline-level)))
  20055. (outline-next-heading)
  20056. (while (and (not (eobp)) (> (funcall outline-level) level))
  20057. (outline-next-heading))
  20058. (if (or (eobp) (< (funcall outline-level) level))
  20059. nil
  20060. (point))))
  20061. (defun org-get-last-sibling ()
  20062. "Move to previous heading of the same level, and return point.
  20063. If there is no such heading, return nil."
  20064. (let ((opoint (point))
  20065. (level (funcall outline-level)))
  20066. (outline-previous-heading)
  20067. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20068. (while (and (> (funcall outline-level) level)
  20069. (not (bobp)))
  20070. (outline-previous-heading))
  20071. (if (< (funcall outline-level) level)
  20072. nil
  20073. (point)))))
  20074. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20075. "Goto to the end of a subtree."
  20076. ;; This contains an exact copy of the original function, but it uses
  20077. ;; `org-back-to-heading', to make it work also in invisible
  20078. ;; trees. And is uses an invisible-ok argument.
  20079. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20080. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20081. ;; with many children and grandchildren etc, this can be much faster
  20082. ;; than the outline version.
  20083. (org-back-to-heading invisible-ok)
  20084. (let ((first t)
  20085. (level (funcall outline-level)))
  20086. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20087. ;; A true heading (not a plain list item), in Org-mode
  20088. ;; This means we can easily find the end by looking
  20089. ;; only for the right number of stars. Using a regexp to do
  20090. ;; this is so much faster than using a Lisp loop.
  20091. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20092. (forward-char 1)
  20093. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20094. ;; something else, do it the slow way
  20095. (while (and (not (eobp))
  20096. (or first (> (funcall outline-level) level)))
  20097. (setq first nil)
  20098. (outline-next-heading)))
  20099. (unless to-heading
  20100. (if (memq (preceding-char) '(?\n ?\^M))
  20101. (progn
  20102. ;; Go to end of line before heading
  20103. (forward-char -1)
  20104. (if (memq (preceding-char) '(?\n ?\^M))
  20105. ;; leave blank line before heading
  20106. (forward-char -1))))))
  20107. (point))
  20108. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20109. "Use Org version in org-mode, for dramatic speed-up."
  20110. (if (derived-mode-p 'org-mode)
  20111. (progn
  20112. (org-end-of-subtree nil t)
  20113. (unless (eobp) (backward-char 1)))
  20114. ad-do-it))
  20115. (defun org-end-of-meta-data-and-drawers ()
  20116. "Jump to the first text after meta data and drawers in the current entry.
  20117. This will move over empty lines, lines with planning time stamps,
  20118. clocking lines, and drawers."
  20119. (org-back-to-heading t)
  20120. (let ((end (save-excursion (outline-next-heading) (point)))
  20121. (re (concat "\\(" org-drawer-regexp "\\)"
  20122. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20123. (forward-line 1)
  20124. (while (re-search-forward re end t)
  20125. (if (not (match-end 1))
  20126. ;; empty or planning line
  20127. (forward-line 1)
  20128. ;; a drawer, find the end
  20129. (re-search-forward "^[ \t]*:END:" end 'move)
  20130. (forward-line 1)))
  20131. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20132. (point)))
  20133. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20134. "Move forward to the arg'th subheading at same level as this one.
  20135. Stop at the first and last subheadings of a superior heading.
  20136. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20137. non-nil it will also look at invisible ones."
  20138. (interactive "p")
  20139. (org-back-to-heading invisible-ok)
  20140. (org-at-heading-p)
  20141. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20142. (re (format "^\\*\\{1,%d\\} " level))
  20143. l)
  20144. (forward-char 1)
  20145. (while (> arg 0)
  20146. (while (and (re-search-forward re nil 'move)
  20147. (setq l (- (match-end 0) (match-beginning 0) 1))
  20148. (= l level)
  20149. (not invisible-ok)
  20150. (progn (backward-char 1) (outline-invisible-p)))
  20151. (if (< l level) (setq arg 1)))
  20152. (setq arg (1- arg)))
  20153. (beginning-of-line 1)))
  20154. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20155. "Move backward to the arg'th subheading at same level as this one.
  20156. Stop at the first and last subheadings of a superior heading."
  20157. (interactive "p")
  20158. (org-back-to-heading)
  20159. (org-at-heading-p)
  20160. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20161. (re (format "^\\*\\{1,%d\\} " level))
  20162. l)
  20163. (while (> arg 0)
  20164. (while (and (re-search-backward re nil 'move)
  20165. (setq l (- (match-end 0) (match-beginning 0) 1))
  20166. (= l level)
  20167. (not invisible-ok)
  20168. (outline-invisible-p))
  20169. (if (< l level) (setq arg 1)))
  20170. (setq arg (1- arg)))))
  20171. (defun org-forward-element ()
  20172. "Move forward by one element.
  20173. Move to the next element at the same level, when possible."
  20174. (interactive)
  20175. (cond ((eobp) (error "Cannot move further down"))
  20176. ((org-with-limited-levels (org-at-heading-p))
  20177. (let ((origin (point)))
  20178. (org-forward-heading-same-level 1)
  20179. (unless (org-with-limited-levels (org-at-heading-p))
  20180. (goto-char origin)
  20181. (error "Cannot move further down"))))
  20182. (t
  20183. (let* ((elem (org-element-at-point))
  20184. (end (org-element-property :end elem))
  20185. (parent (org-element-property :parent elem)))
  20186. (if (and parent (= (org-element-property :contents-end parent) end))
  20187. (goto-char (org-element-property :end parent))
  20188. (goto-char end))))))
  20189. (defun org-backward-element ()
  20190. "Move backward by one element.
  20191. Move to the previous element at the same level, when possible."
  20192. (interactive)
  20193. (cond ((bobp) (error "Cannot move further up"))
  20194. ((org-with-limited-levels (org-at-heading-p))
  20195. ;; At an headline, move to the previous one, if any, or stay
  20196. ;; here.
  20197. (let ((origin (point)))
  20198. (org-backward-heading-same-level 1)
  20199. (unless (org-with-limited-levels (org-at-heading-p))
  20200. (goto-char origin)
  20201. (error "Cannot move further up"))))
  20202. (t
  20203. (let* ((trail (org-element-at-point 'keep-trail))
  20204. (elem (car trail))
  20205. (prev-elem (nth 1 trail))
  20206. (beg (org-element-property :begin elem)))
  20207. (cond
  20208. ;; Move to beginning of current element if point isn't
  20209. ;; there already.
  20210. ((/= (point) beg) (goto-char beg))
  20211. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20212. ((org-before-first-heading-p) (goto-char (point-min)))
  20213. (t (org-back-to-heading)))))))
  20214. (defun org-up-element ()
  20215. "Move to upper element."
  20216. (interactive)
  20217. (if (org-with-limited-levels (org-at-heading-p))
  20218. (unless (org-up-heading-safe) (error "No surrounding element"))
  20219. (let* ((elem (org-element-at-point))
  20220. (parent (org-element-property :parent elem)))
  20221. (if parent (goto-char (org-element-property :begin parent))
  20222. (if (org-with-limited-levels (org-before-first-heading-p))
  20223. (error "No surrounding element")
  20224. (org-with-limited-levels (org-back-to-heading)))))))
  20225. (defvar org-element-greater-elements)
  20226. (defun org-down-element ()
  20227. "Move to inner element."
  20228. (interactive)
  20229. (let ((element (org-element-at-point)))
  20230. (cond
  20231. ((memq (org-element-type element) '(plain-list table))
  20232. (goto-char (org-element-property :contents-begin element))
  20233. (forward-char))
  20234. ((memq (org-element-type element) org-element-greater-elements)
  20235. ;; If contents are hidden, first disclose them.
  20236. (when (org-element-property :hiddenp element) (org-cycle))
  20237. (goto-char (or (org-element-property :contents-begin element)
  20238. (error "No content for this element"))))
  20239. (t (error "No inner element")))))
  20240. (defun org-drag-element-backward ()
  20241. "Move backward element at point."
  20242. (interactive)
  20243. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20244. (let* ((trail (org-element-at-point 'keep-trail))
  20245. (elem (car trail))
  20246. (prev-elem (nth 1 trail)))
  20247. ;; Error out if no previous element or previous element is
  20248. ;; a parent of the current one.
  20249. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20250. (error "Cannot drag element backward")
  20251. (let ((pos (point)))
  20252. (org-element-swap-A-B prev-elem elem)
  20253. (goto-char (+ (org-element-property :begin prev-elem)
  20254. (- pos (org-element-property :begin elem)))))))))
  20255. (defun org-drag-element-forward ()
  20256. "Move forward element at point."
  20257. (interactive)
  20258. (let* ((pos (point))
  20259. (elem (org-element-at-point)))
  20260. (when (= (point-max) (org-element-property :end elem))
  20261. (error "Cannot drag element forward"))
  20262. (goto-char (org-element-property :end elem))
  20263. (let ((next-elem (org-element-at-point)))
  20264. (when (or (org-element-nested-p elem next-elem)
  20265. (and (eq (org-element-type next-elem) 'headline)
  20266. (not (eq (org-element-type elem) 'headline))))
  20267. (goto-char pos)
  20268. (error "Cannot drag element forward"))
  20269. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20270. ;; body's length (without final blanks) and by the length of
  20271. ;; blanks between ELEM and NEXT-ELEM.
  20272. (let ((size-next (- (save-excursion
  20273. (goto-char (org-element-property :end next-elem))
  20274. (skip-chars-backward " \r\t\n")
  20275. (forward-line)
  20276. ;; Small correction if buffer doesn't end
  20277. ;; with a newline character.
  20278. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20279. (org-element-property :begin next-elem)))
  20280. (size-blank (- (org-element-property :end elem)
  20281. (save-excursion
  20282. (goto-char (org-element-property :end elem))
  20283. (skip-chars-backward " \r\t\n")
  20284. (forward-line)
  20285. (point)))))
  20286. (org-element-swap-A-B elem next-elem)
  20287. (goto-char (+ pos size-next size-blank))))))
  20288. (defun org-mark-element ()
  20289. "Put point at beginning of this element, mark at end.
  20290. Interactively, if this command is repeated or (in Transient Mark
  20291. mode) if the mark is active, it marks the next element after the
  20292. ones already marked."
  20293. (interactive)
  20294. (let (deactivate-mark)
  20295. (if (and (org-called-interactively-p 'any)
  20296. (or (and (eq last-command this-command) (mark t))
  20297. (and transient-mark-mode mark-active)))
  20298. (set-mark
  20299. (save-excursion
  20300. (goto-char (mark))
  20301. (goto-char (org-element-property :end (org-element-at-point)))))
  20302. (let ((element (org-element-at-point)))
  20303. (end-of-line)
  20304. (push-mark (org-element-property :end element) t t)
  20305. (goto-char (org-element-property :begin element))))))
  20306. (defun org-narrow-to-element ()
  20307. "Narrow buffer to current element."
  20308. (interactive)
  20309. (let ((elem (org-element-at-point)))
  20310. (cond
  20311. ((eq (car elem) 'headline)
  20312. (narrow-to-region
  20313. (org-element-property :begin elem)
  20314. (org-element-property :end elem)))
  20315. ((memq (car elem) org-element-greater-elements)
  20316. (narrow-to-region
  20317. (org-element-property :contents-begin elem)
  20318. (org-element-property :contents-end elem)))
  20319. (t
  20320. (narrow-to-region
  20321. (org-element-property :begin elem)
  20322. (org-element-property :end elem))))))
  20323. (defun org-transpose-element ()
  20324. "Transpose current and previous elements, keeping blank lines between.
  20325. Point is moved after both elements."
  20326. (interactive)
  20327. (org-skip-whitespace)
  20328. (let ((end (org-element-property :end (org-element-at-point))))
  20329. (org-drag-element-backward)
  20330. (goto-char end)))
  20331. (defun org-unindent-buffer ()
  20332. "Un-indent the visible part of the buffer.
  20333. Relative indentation (between items, inside blocks, etc.) isn't
  20334. modified."
  20335. (interactive)
  20336. (unless (eq major-mode 'org-mode)
  20337. (error "Cannot un-indent a buffer not in Org mode"))
  20338. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20339. unindent-tree ; For byte-compiler.
  20340. (unindent-tree
  20341. (function
  20342. (lambda (contents)
  20343. (mapc
  20344. (lambda (element)
  20345. (if (memq (org-element-type element) '(headline section))
  20346. (funcall unindent-tree (org-element-contents element))
  20347. (save-excursion
  20348. (save-restriction
  20349. (narrow-to-region
  20350. (org-element-property :begin element)
  20351. (org-element-property :end element))
  20352. (org-do-remove-indentation)))))
  20353. (reverse contents))))))
  20354. (funcall unindent-tree (org-element-contents parse-tree))))
  20355. (defun org-show-subtree ()
  20356. "Show everything after this heading at deeper levels."
  20357. (interactive)
  20358. (outline-flag-region
  20359. (point)
  20360. (save-excursion
  20361. (org-end-of-subtree t t))
  20362. nil))
  20363. (defun org-show-entry ()
  20364. "Show the body directly following this heading.
  20365. Show the heading too, if it is currently invisible."
  20366. (interactive)
  20367. (save-excursion
  20368. (condition-case nil
  20369. (progn
  20370. (org-back-to-heading t)
  20371. (outline-flag-region
  20372. (max (point-min) (1- (point)))
  20373. (save-excursion
  20374. (if (re-search-forward
  20375. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  20376. (match-beginning 1)
  20377. (point-max)))
  20378. nil)
  20379. (org-cycle-hide-drawers 'children))
  20380. (error nil))))
  20381. (defun org-make-options-regexp (kwds &optional extra)
  20382. "Make a regular expression for keyword lines."
  20383. (concat
  20384. "^#\\+\\("
  20385. (mapconcat 'regexp-quote kwds "\\|")
  20386. (if extra (concat "\\|" extra))
  20387. "\\):[ \t]*\\(.*\\)"))
  20388. ;; Make isearch reveal the necessary context
  20389. (defun org-isearch-end ()
  20390. "Reveal context after isearch exits."
  20391. (when isearch-success ; only if search was successful
  20392. (if (featurep 'xemacs)
  20393. ;; Under XEmacs, the hook is run in the correct place,
  20394. ;; we directly show the context.
  20395. (org-show-context 'isearch)
  20396. ;; In Emacs the hook runs *before* restoring the overlays.
  20397. ;; So we have to use a one-time post-command-hook to do this.
  20398. ;; (Emacs 22 has a special variable, see function `org-mode')
  20399. (unless (and (boundp 'isearch-mode-end-hook-quit)
  20400. isearch-mode-end-hook-quit)
  20401. ;; Only when the isearch was not quitted.
  20402. (org-add-hook 'post-command-hook 'org-isearch-post-command
  20403. 'append 'local)))
  20404. (org-fix-ellipsis-at-bol)))
  20405. (defun org-isearch-post-command ()
  20406. "Remove self from hook, and show context."
  20407. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  20408. (org-show-context 'isearch))
  20409. ;;;; Integration with and fixes for other packages
  20410. ;;; Imenu support
  20411. (defvar org-imenu-markers nil
  20412. "All markers currently used by Imenu.")
  20413. (make-variable-buffer-local 'org-imenu-markers)
  20414. (defun org-imenu-new-marker (&optional pos)
  20415. "Return a new marker for use by Imenu, and remember the marker."
  20416. (let ((m (make-marker)))
  20417. (move-marker m (or pos (point)))
  20418. (push m org-imenu-markers)
  20419. m))
  20420. (defun org-imenu-get-tree ()
  20421. "Produce the index for Imenu."
  20422. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  20423. (setq org-imenu-markers nil)
  20424. (let* ((n org-imenu-depth)
  20425. (re (concat "^" (org-get-limited-outline-regexp)))
  20426. (subs (make-vector (1+ n) nil))
  20427. (last-level 0)
  20428. m level head0 head)
  20429. (save-excursion
  20430. (save-restriction
  20431. (widen)
  20432. (goto-char (point-max))
  20433. (while (re-search-backward re nil t)
  20434. (setq level (org-reduced-level (funcall outline-level)))
  20435. (when (and (<= level n)
  20436. (looking-at org-complex-heading-regexp)
  20437. (setq head0 (org-match-string-no-properties 4)))
  20438. (setq head (org-link-display-format head0)
  20439. m (org-imenu-new-marker))
  20440. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  20441. (if (>= level last-level)
  20442. (push (cons head m) (aref subs level))
  20443. (push (cons head (aref subs (1+ level))) (aref subs level))
  20444. (loop for i from (1+ level) to n do (aset subs i nil)))
  20445. (setq last-level level)))))
  20446. (aref subs 1)))
  20447. (eval-after-load "imenu"
  20448. '(progn
  20449. (add-hook 'imenu-after-jump-hook
  20450. (lambda ()
  20451. (if (derived-mode-p 'org-mode)
  20452. (org-show-context 'org-goto))))))
  20453. (defun org-link-display-format (link)
  20454. "Replace a link with either the description, or the link target
  20455. if no description is present"
  20456. (save-match-data
  20457. (if (string-match org-bracket-link-analytic-regexp link)
  20458. (replace-match (if (match-end 5)
  20459. (match-string 5 link)
  20460. (concat (match-string 1 link)
  20461. (match-string 3 link)))
  20462. nil t link)
  20463. link)))
  20464. (defun org-toggle-link-display ()
  20465. "Toggle the literal or descriptive display of links."
  20466. (interactive)
  20467. (if org-descriptive-links
  20468. (progn (org-remove-from-invisibility-spec '(org-link))
  20469. (org-restart-font-lock)
  20470. (setq org-descriptive-links nil))
  20471. (progn (add-to-invisibility-spec '(org-link))
  20472. (org-restart-font-lock)
  20473. (setq org-descriptive-links t))))
  20474. ;; Speedbar support
  20475. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  20476. "Overlay marking the agenda restriction line in speedbar.")
  20477. (overlay-put org-speedbar-restriction-lock-overlay
  20478. 'face 'org-agenda-restriction-lock)
  20479. (overlay-put org-speedbar-restriction-lock-overlay
  20480. 'help-echo "Agendas are currently limited to this item.")
  20481. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  20482. (defun org-speedbar-set-agenda-restriction ()
  20483. "Restrict future agenda commands to the location at point in speedbar.
  20484. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  20485. (interactive)
  20486. (require 'org-agenda)
  20487. (let (p m tp np dir txt)
  20488. (cond
  20489. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20490. 'org-imenu t))
  20491. (setq m (get-text-property p 'org-imenu-marker))
  20492. (with-current-buffer (marker-buffer m)
  20493. (goto-char m)
  20494. (org-agenda-set-restriction-lock 'subtree)))
  20495. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  20496. 'speedbar-function 'speedbar-find-file))
  20497. (setq tp (previous-single-property-change
  20498. (1+ p) 'speedbar-function)
  20499. np (next-single-property-change
  20500. tp 'speedbar-function)
  20501. dir (speedbar-line-directory)
  20502. txt (buffer-substring-no-properties (or tp (point-min))
  20503. (or np (point-max))))
  20504. (with-current-buffer (find-file-noselect
  20505. (let ((default-directory dir))
  20506. (expand-file-name txt)))
  20507. (unless (derived-mode-p 'org-mode)
  20508. (error "Cannot restrict to non-Org-mode file"))
  20509. (org-agenda-set-restriction-lock 'file)))
  20510. (t (error "Don't know how to restrict Org-mode's agenda")))
  20511. (move-overlay org-speedbar-restriction-lock-overlay
  20512. (point-at-bol) (point-at-eol))
  20513. (setq current-prefix-arg nil)
  20514. (org-agenda-maybe-redo)))
  20515. (eval-after-load "speedbar"
  20516. '(progn
  20517. (speedbar-add-supported-extension ".org")
  20518. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  20519. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  20520. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  20521. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  20522. (add-hook 'speedbar-visiting-tag-hook
  20523. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  20524. ;;; Fixes and Hacks for problems with other packages
  20525. ;; Make flyspell not check words in links, to not mess up our keymap
  20526. (defun org-mode-flyspell-verify ()
  20527. "Don't let flyspell put overlays at active buttons, or on
  20528. {todo,all-time,additional-option-like}-keywords."
  20529. (let ((pos (max (1- (point)) (point-min)))
  20530. (word (thing-at-point 'word)))
  20531. (and (not (get-text-property pos 'keymap))
  20532. (not (get-text-property pos 'org-no-flyspell))
  20533. (not (member word org-todo-keywords-1))
  20534. (not (member word org-all-time-keywords))
  20535. (not (member word org-options-keywords))
  20536. (not (member word (mapcar 'car org-startup-options)))
  20537. (not (member word org-additional-option-like-keywords-for-flyspell)))))
  20538. (defun org-remove-flyspell-overlays-in (beg end)
  20539. "Remove flyspell overlays in region."
  20540. (and (org-bound-and-true-p flyspell-mode)
  20541. (fboundp 'flyspell-delete-region-overlays)
  20542. (flyspell-delete-region-overlays beg end))
  20543. (add-text-properties beg end '(org-no-flyspell t)))
  20544. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  20545. (eval-after-load "bookmark"
  20546. '(if (boundp 'bookmark-after-jump-hook)
  20547. ;; We can use the hook
  20548. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  20549. ;; Hook not available, use advice
  20550. (defadvice bookmark-jump (after org-make-visible activate)
  20551. "Make the position visible."
  20552. (org-bookmark-jump-unhide))))
  20553. ;; Make sure saveplace shows the location if it was hidden
  20554. (eval-after-load "saveplace"
  20555. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  20556. "Make the position visible."
  20557. (org-bookmark-jump-unhide)))
  20558. ;; Make sure ecb shows the location if it was hidden
  20559. (eval-after-load "ecb"
  20560. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  20561. "Make hierarchy visible when jumping into location from ECB tree buffer."
  20562. (if (derived-mode-p 'org-mode)
  20563. (org-show-context))))
  20564. (defun org-bookmark-jump-unhide ()
  20565. "Unhide the current position, to show the bookmark location."
  20566. (and (derived-mode-p 'org-mode)
  20567. (or (outline-invisible-p)
  20568. (save-excursion (goto-char (max (point-min) (1- (point))))
  20569. (outline-invisible-p)))
  20570. (org-show-context 'bookmark-jump)))
  20571. ;; Make session.el ignore our circular variable
  20572. (eval-after-load "session"
  20573. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  20574. ;;;; Experimental code
  20575. (defun org-closed-in-range ()
  20576. "Sparse tree of items closed in a certain time range.
  20577. Still experimental, may disappear in the future."
  20578. (interactive)
  20579. ;; Get the time interval from the user.
  20580. (let* ((time1 (org-float-time
  20581. (org-read-date nil 'to-time nil "Starting date: ")))
  20582. (time2 (org-float-time
  20583. (org-read-date nil 'to-time nil "End date:")))
  20584. ;; callback function
  20585. (callback (lambda ()
  20586. (let ((time
  20587. (org-float-time
  20588. (apply 'encode-time
  20589. (org-parse-time-string
  20590. (match-string 1))))))
  20591. ;; check if time in interval
  20592. (and (>= time time1) (<= time time2))))))
  20593. ;; make tree, check each match with the callback
  20594. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  20595. ;;;; Finish up
  20596. (provide 'org)
  20597. (run-hooks 'org-load-hook)
  20598. ;;; org.el ends here